.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help with inserting and filling out attributed block

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
conormccartney3897
498 Views, 5 Replies

Help with inserting and filling out attributed block

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:

before.JPG

 

after:

after.JPG

5 REPLIES 5
Message 2 of 6

Hello - I couldn't understand what you wrote

 

 

Message 3 of 6

Uh okay.... lets see if this helps... So im attempting to insert a block and fill out the attributes, the data for the insertion point and attributes is contained in the FinList (List(of T)). What happens is the data for detail 1 is inserted into the block for detail 2 (as shown in in the pictures).
Message 4 of 6
osquro
in reply to: conormccartney3897
Message 5 of 6

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.

 

 

 

 

Message 6 of 6

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.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost