So inserting the same block multiple times from a List(of T), and attempting to fill it out with data from that same list. The data in the list is correct, but the attributes are going to the next instance of the block (data associated with item 0 is being placed in block at item 2, etc.) I've tried multiple variations of the code, and all result in the same thing.
the code:
For i As Integer = 0 To FinList.Count - 1 Step 2 Dim inspt As Point3d = FinList.Item(i).objpoint Using tr As Transaction = db.TransactionManager.StartTransaction() Dim bt As BlockTable = DirectCast(tr.GetObject(db.BlockTableId, OpenMode.ForRead), BlockTable) Dim id As ObjectId = bt(startblock) Dim br As New BlockReference(inspt, id) Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.PaperSpace), OpenMode.ForWrite) btr.AppendEntity(br) tr.AddNewlyCreatedDBObject(br, True) br.Layer = "S-Text" Dim btrref As BlockTableRecord = tr.GetObject(br.BlockTableRecord, OpenMode.ForRead) For Each bid As ObjectId In btrref Dim ent As Entity = tr.GetObject(bid, OpenMode.ForWrite) If TypeOf ent Is AttributeDefinition Then Dim attdef As AttributeDefinition = ent Dim attref As AttributeReference = New AttributeReference attref.SetAttributeFromBlock(attdef, br.BlockTransform) If attdef.Tag = "TITLE2" Then attdef.TextString = FinList.Item(i).objcontents ElseIf attdef.Tag = "SCALE" Then attdef.TextString = "NTS" ElseIf attdef.Tag = "TITLE1" Then attdef.TextString = " " End If br.AttributeCollection.AppendAttribute(attref) tr.AddNewlyCreatedDBObject(attref, True) End If Next tr.Commit() End Using Next
Output from the FinList:
[0] titlestart, objcontents=TYPICAL COLUMN BASE PLATE, TYPE 1, objpoint=(1.65539522805035,22.4812564588351,0) [1] titleend, objcontents=1, objpoint=(8.34753401687366,22.4812564588351,0) [2] titlestart, objcontents=TYPICAL COLUMN BASE PLATE, TYPE 2, objpoint=(9.09879207533965,22.4812564588351,0) [3] titleend, objconents=2, objpoint=(15.8029608581202,22.4812564588351,0)
before:
after:
Solved! Go to Solution.
Solved by BKSpurgeon. Go to Solution.
I'm not sure if i understand you correctly - and i'm no expert - but i suspect this may be the issue:
Here is where i suspect your problem is:
If TypeOf ent Is AttributeDefinition Then Dim attdef As AttributeDefinition = ent Dim attref As AttributeReference = New AttributeReference attref.SetAttributeFromBlock(attdef, br.BlockTransform) If attdef.Tag = "TITLE2" Then attdef.TextString = FinList.Item(i).objcontents ElseIf attdef.Tag = "SCALE" Then attdef.TextString = "NTS" ElseIf attdef.Tag = "TITLE1" Then attdef.TextString = " " End If
you do not seem to be discrimminating between constant and non-constant attribute definitions - moreover you are simply editing the attribute definition which lies in the block definition, rather than the attribute references. Of course, if you change the attribute definitions, then they will all read the same.
If attdef.Tag = "TITLE2" Then attdef.TextString = FinList.Item(i).objcontents ElseIf attdef.Tag = "SCALE" Then attdef.TextString = "NTS" ElseIf attdef.Tag = "TITLE1" Then attdef.TextString = " " End If
Then all your blocks will have the same values, because you are changing the attribute definitions.
Compare to the code another user has pasted:
//Iterate block definition to find all non-constant // AttributeDefinitions foreach (ObjectId id in blockDef) { DBObject obj = id.GetObject( OpenMode.ForRead); AttributeDefinition attDef = obj as AttributeDefinition; if ((attDef != null) && (!attDef.Constant)) { //This is a non-constant AttributeDefinition //Create a new AttributeReference using (AttributeReference attRef = new AttributeReference()) { attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform); attRef.TextString = "Hello World"; //Add the AttributeReference to the BlockReference blockRef.AttributeCollection.AppendAttribute(attRef); myT.AddNewlyCreatedDBObject(attRef, true); } } } } //Our work here is done myT.Commit();
In the above the attribute reference is altered, rather than the attribute definition, accordingly all block references will have differences in them.
ah your are correct, it was the def vs ref issue. A second pair of eyes to the rescue! The reason i am not dealing with constant attributes is the block i am inserting is the always the same one, which i know to not have any constant attributes, thus no need to check for them.