Hi. I inserted a block from a dwg drawing with the following method:
public static ObjectId InsertBlock(string fileName, Point3d insPoint) { var acadDoc = Application.DocumentManager.MdiActiveDocument; var blockId = default(ObjectId); using (var acadLockDoc = acadDoc.LockDocument()) { var dbTemp = new Database(false, true); dbTemp.ReadDwgFile(fileName, FileShare.Read, false, ""); var blockName = Path.GetFileNameWithoutExtension(fileName); SymbolUtilityServices.ValidateSymbolName(blockName, false); var acadDb = acadDoc.Database; blockId = acadDb.Insert(blockName, dbTemp, false); var blockToInsert = new BlockReference(insPoint, blockId); using (Transaction acadTrans = acadDoc.TransactionManager.StartTransaction()) { var blockTable = (BlockTable)acadTrans.GetObject(acadDoc.Database.BlockTableId, OpenMode.ForRead); var activeSpace = (BlockTableRecord)acadTrans.GetObject(_db.CurrentSpaceId, OpenMode.ForWrite); activeSpace.AppendEntity(blockToInsert); acadTrans.AddNewlyCreatedDBObject(blockToInsert,true); var block = (BlockTableRecord)acadTrans.GetObject(blockId, OpenMode.ForWrite); foreach (ObjectId elementId in block) { var element = acadTrans.GetObject(elementId, OpenMode.ForRead); if (element.GetType().Name.Equals("AttributeDefinition")) { var attDef = (AttributeDefinition)acadTrans.GetObject(elementId, OpenMode.ForRead); var attRef = new AttributeReference(); attRef.SetAttributeFromBlock(attDef, blockToInsert.BlockTransform); blockToInsert.AttributeCollection.AppendAttribute(attRef); acadTrans.AddNewlyCreatedDBObject(attRef, true); } } acadTrans.Commit(); } } Application.UpdateScreen(); return blockId; }
After inserting a block, i can access it's BlockTableRecord like this:
var block = (BlockTableRecord)trans.GetObject(blockRefId, OpenMode.ForWrite);
but i can't access the BlockReference object like this:
var blockRef = trans.GetObject(blockRefId, OpenMode.ForWrite) as BlockReference;
It's a null. What is going on?
Solved! Go to Solution.
Solved by Alfred.NESWADBA. Go to Solution.
Hi,
misstyped the two codelines on the end?
You will get the blocktablerecord by this:
trans.GetObject(blockId, OpenMode.ForWrite)
and the blockreference with that code:
trans.GetObject(blockToInsert.ObjectID, OpenMode.ForWrite)
in both cases you have used the variable <blockrefid> which I could not find within your snippet.
HTH, - alfred -
Thank you Alfred, this worked. You couldn't find blockrefid variable because I'm calling the above method from another method where I have this variable, which holds the ObjectId of the inserted block.
But I'm not clear about one thing in the above method. I actually found this method on the internet. Why do I have to insert the block twice? Once like this, before starting transaction:
blockId = acadDb.Insert(blockName, dbTemp, false);
, and once like this in the transaction:
activeSpace.AppendEntity(blockToInsert);
acadTrans.AddNewlyCreatedDBObject(blockToInsert,true);
This looks like doing one and the same thing twice. Is there something that is not needed there?
Haris
Hi,
this line:
blockId = acadDb.Insert(blockName, dbTemp, false);
just defines the blockdefintion.
and that line:
activeSpace.AppendEntity(blockToInsert);
creates the reference (the insert) in the current space (modelspace or paperspace)
HTH, - alfred -