public void ResurrectDeletedBlockReferenceFromDatabase(List<long> deletedBlockReferences, List<DbBlockReference> dbBlockReferences, string layer)
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Transaction tr = db.TransactionManager.StartTransaction();
List<DbBlockReference> bRefToResurrect = new List<DbBlockReference>();
foreach (DbBlockReference item in dbBlockReferences)
{
if (deletedBlockReferences.Contains(item.Handle))
{
bRefToResurrect.Add(item);
}
}
using ( tr )
{
BlockTable bt = (BlockTable) db.BlockTableId.GetObject(OpenMode.ForRead);
for ( int i = 0; i < bRefToResurrect.Count; i++ )
{
string line = bRefToResurrect[ i ].Position.Trim('(', ')');
double[] values = line.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries).Select(s => double.Parse(s)).ToArray();
Point3d position = new Point3d(values);
BlockTableRecord btr = (BlockTableRecord) bt[ bRefToResurrect[ i ].Name ].GetObject(OpenMode.ForRead);
BlockTableRecord curSpace_btr = (BlockTableRecord) tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
BlockReference bRef = new BlockReference(position, btr.ObjectId);
bRef.SetDatabaseDefaults();
bRef.Layer = layer;
bRef.Linetype = bRefToResurrect[ i ].Linetype;
bRef.LineWeight = bRefToResurrect[ i ].Lineweight;
bRef.ColorIndex = 10;
bRef.Rotation = bRefToResurrect[ i ].Rotation;
bRef.ScaleFactors = new Scale3d(bRefToResurrect[ i ].ScaleX, bRefToResurrect[ i ].ScaleY, 1);
curSpace_btr.AppendEntity(bRef);
tr.AddNewlyCreatedDBObject(bRef, true);
if ( btr.HasAttributeDefinitions )
{
foreach ( ObjectId objID in btr )
{
DBObject dbObject = tr.GetObject(objID, OpenMode.ForRead);
if ( dbObject is AttributeDefinition )
{
AttributeDefinition attDef = dbObject as AttributeDefinition;
if ( attDef.Constant == false )
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, bRef.BlockTransform);
attRef.Position = attDef.Position.TransformBy(bRef.BlockTransform);
attRef.TextString = attDef.TextString;
bRef.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
}
}
if ( bRef.IsDynamicBlock )
{
// BlockTableRecord dynBtr = (BlockTableRecord)tr.GetObject(bRef.DynamicBlockTableRecord, OpenMode.ForRead, false, true);
DynamicBlockReferencePropertyCollection props = bRef.DynamicBlockReferencePropertyCollection;
foreach ( DynamicBlockReferenceProperty prop in props )
{
object[] values0 = prop.GetAllowedValues();
//Switch Property
if ( (prop.PropertyName == "Visibility") && !prop.ReadOnly )
{
prop.Value = bRefToResurrect[ i ].VisibilityState;
}
if ((prop.PropertyName.Contains("Flip") && prop.VisibleInCurrentVisibilityState))
{
prop.Value = bRefToResurrect[i].Flip;
}
}
}
}
tr.Commit();
}
}