VB.NET block with attributes problem

VB.NET block with attributes problem

Anonymous
Not applicable
773 Views
3 Replies
Message 1 of 4

VB.NET block with attributes problem

Anonymous
Not applicable

Hi everyone,

I'm trying to work with block + attributes, I can create it but if I insert it by .net hte block don't have attributes, if I 

insert it manually the attributes are ok why?

Senzanome.jpg

<CommandMethod("AddingAttributeToABlock")>
            Public Sub AddingAttributeToABlock()
                ' Get the current database and start a transaction
                Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
                Dim acCurDb As Database = acDoc.Database
                Dim blkRecId As ObjectId = ObjectId.Null

                Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
                    ' Open the Block table for read
                    Dim acBlkTbl As BlockTable
                    acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead)

                    If Not acBlkTbl.Has("CircleBlockWithAttributes") Then
                        Using acBlkTblRec As New BlockTableRecord
                            acBlkTblRec.Name = "CircleBlockWithAttributes"

                            ' Set the insertion point for the block
                            acBlkTblRec.Origin = New Point3d(0, 0, 0)

                            ' Add a circle to the block
                            Using acCirc As New Circle
                                acCirc.Center = New Point3d(0, 0, 0)
                                acCirc.Radius = 2

                                acBlkTblRec.AppendEntity(acCirc)

                                ' Add an attribute definition to the block
                                Using acAttDef As New AttributeDefinition
                                    acAttDef.Position = New Point3d(0, 0, 0)
                                    acAttDef.Verifiable = True
                                    'acAttDef.Visible = False
                                    acAttDef.Prompt = "Door #: "
                                    acAttDef.Tag = "Door#"
                                    acAttDef.TextString = "DXX"
                                    acAttDef.Height = 1
                                    acAttDef.Justify = AttachmentPoint.MiddleCenter
                                    acAttDef.LockPositionInBlock = True
                                    acBlkTblRec.AppendEntity(acAttDef)

                                    acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForWrite)
                                    acBlkTbl.Add(acBlkTblRec)
                                    acTrans.AddNewlyCreatedDBObject(acBlkTblRec, True)

                                End Using
                            End Using
                            blkRecId = acBlkTblRec.Id
                        End Using
                    End If

                    Dim pPtRes As PromptPointResult
                    Dim pPtOpts As PromptPointOptions = New PromptPointOptions("")
                    '' Prompt for the start point
                    pPtOpts.Message = vbLf & "Enter the point: "
                    pPtRes = acDoc.Editor.GetPoint(pPtOpts)
                    Dim ptStart As Point3d = pPtRes.Value

                    '' Exit if the user presses ESC or cancels the command
                    If pPtRes.Status = PromptStatus.Cancel Then Exit Sub

                    ' Insert the block into the current space
                    If blkRecId <> ObjectId.Null Then
                        Using acBlkRef As New BlockReference(ptStart, blkRecId)

                            Dim acCurSpaceBlkTblRec As BlockTableRecord
                            acCurSpaceBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)

                            acCurSpaceBlkTblRec.AppendEntity(acBlkRef)
                            acTrans.AddNewlyCreatedDBObject(acBlkRef, True)
                        End Using
                    End If

                    ' Save the new object to the database
                    acTrans.Commit()

                    ' Dispose of the transaction
                End Using
            End Sub

        End Class

 

0 Likes
774 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

Different from VBA/COM API used for inserting block reference (Model[Paper]Space.InsertBlock(....), if you have ever programmed in AutoCAD VBA, in Acad .NET API/ObjectARX API, you need to write code the add AttributeReference to the BlockReference explicitly. Something like:

...

'' Create new block reference here

'' Append block reference into space

'' Add block reference to Transaction

'' Create attributereference

For Each id As ObjectId in [theBlockTableRecord]

    If id.ObjectClass.DxfName.ToUpper()="ATTDEF" Then

        Dim attDef=DirectCast(trans.GetObject(id, OpenMode.ForRead), AttributeDefinition)

        If Not attDef.Constant Then

            Dim att =New AttributeReference()

            att.SetAttributeFromBlock(attDef, [blkRef].BlockTransform)

            att.TextString="zzzzzzzz"

            [blkRef].AttributeCollection.AppendAttribute(att)

            trans.AddNewlyCreatedObject(att, True)

        End If

    End If

Next

 

trans. Commit()

...

 

HTH

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

twmerren
Explorer
Explorer

Your code is setting attribute visibility to false.  Set to true and it should be visible.

0 Likes
Message 4 of 4

norman.yuan
Mentor
Mentor

@twmerren , what do you talk about? The OP's code not creating and adding AttributeReference to the BlockReference's AttributeCollection is the reason. No having AttributeReference created, how/where the OP's code can set Visible property? I think you must mis-read the OP's code where a line of code setting AttributeDefinition(!)'s Visible property, which is commented out.

 

Norman Yuan

Drive CAD With Code

EESignature