Hi all,
First, As you can see I am new to this... so Thank you for your help...
I have this code to insert a block (the block has Attirbutes) it bring in the block but strips the attributes. Here is the C# code:
[CommandMethod("PUTBLK")] static public void test() { string comp = "C:\\test.dwg"; Document doc = acadApp.DocumentManager.MdiActiveDocument; //Editor ed = doc.Editor; Transaction tr = doc.TransactionManager.StartTransaction(); try { string dwgName = HostApplicationServices.Current.FindFile(comp, acadApp.DocumentManager.MdiActiveDocument.Database, FindFileHint.Default); Database db = new Database(false, false); db.ReadDwgFile(dwgName, System.IO.FileShare.Read, true, ""); System.IO.FileInfo fInfo = new System.IO.FileInfo(dwgName); String dwgNames = fInfo.Name.Substring(0, fInfo.Name.Length - fInfo.Extension.Length); ObjectId BlkId; BlkId = doc.Database.Insert(dwgNames, db, true); BlockTable bt = (BlockTable)tr.GetObject(doc.Database.BlockTableId, OpenMode.ForRead, true); BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); BlockReference bref = new BlockReference(new Autodesk.AutoCAD.Geometry.Point3d(0, 0, 0), BlkId); btr.AppendEntity(bref); tr.AddNewlyCreatedDBObject(bref, true); // bref.ExplodeToOwnerSpace(); // bref.Erase(); tr.Commit(); } finally { tr.Dispose(); } }
What am I missing?
Solved! Go to Solution.
Between your AppendEntity and your AddNewlyCreatedDBObject you need to hook the attributes up using their definitions in the block definition.
In this snip dbtr is the BlockTableRecord of the block definition.
bref is the BlockReference.
if (dbtr.HasAttributeDefinitions) { foreach (ObjectId boid in dbtr) { Entity ent = (Entity)tr.GetObject(boid, OpenMode.ForWrite); if (ent is AttributeDefinition) { AttributeDefinition adef = (AttributeDefinition)ent; AttributeReference aref = new AttributeReference(); aref.SetAttributeFromBlock(adef, bref.BlockTransform); bref.AttributeCollection.AppendAttribute(aref); tr.AddNewlyCreatedDBObject(aref, true); } } }
I don't mean to hijack this Post but I also have the same problem. My code includes;
btr.AppendEntity(blk)
If btr.HasAttributeDefinitions Then
For Each attID As ObjectId In btr
Dim ent As Entity = DirectCast(tr.GetObject(attID, OpenMode.ForWrite), Entity)
If TypeOf ent Is AttributeDefinition Then
Dim attDef As AttributeDefinition = DirectCast(ent, AttributeDefinition)
Dim attRef As New AttributeReference()
attRef.SetAttributeFromBlock(attDef, blk.BlockTransform)
Dim id As ObjectId = blk.AttributeCollection.AppendAttribute(attRef)
tr.AddNewlyCreatedDBObject(attRef, True)
End If
Next
End If
tr.AddNewlyCreatedDBObject(blk, True)
I am inserting a drawing I know has attributes. Indeed when the routine finishes the drawing is there, but without attributes. If I explode the block, the attribute definitions are there.
When I debug btr.HasAttributeDefinitions is False.
Again apologies, but I thought it would help move things along.
Can't find what you're looking for? Ask the community or share your knowledge.