Author Topic: block attribute definition  (Read 2095 times)

0 Members and 1 Guest are viewing this topic.

sybold

  • Newt
  • Posts: 62
block attribute definition
« on: September 18, 2012, 05:16:00 PM »
i'm trying to list block attribute definitions tags from blocks which aren't inserted yet but exist in the dwg.
i'm filling a combobox with the blocks only having attributes, but i'm stuck at the next step,
listing the attribute tags in the next combobox from the selected block.


Code: [Select]
Using trx As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = TryCast(db.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)
            For Each objID As ObjectId In bt
                Dim btr As BlockTableRecord = TryCast(objID.GetObject(OpenMode.ForRead), BlockTableRecord)
                If Not btr.IsLayout = True _
                    And Not btr.IsAnonymous = True _
                    And Not btr.IsFromExternalReference = True _
                    And Not btr.IsFromOverlayReference = True _
                    And Not btr.IsAProxy = True _
                    And Not btr.IsDependent = True _
                    And btr.HasAttributeDefinitions = True Then

                    Dim blkName As String = btr.Name
                    Dim blkval As Object = btr.ObjectId
                    ComboBoxblockname.Items.Add(blkName)

                End If
            Next
            ' End foreach
            trx.Commit()
        End Using

n.yuan

  • Bull Frog
  • Posts: 348
Re: block attribute definition
« Reply #1 on: September 18, 2012, 05:50:42 PM »
Well, you probably do it in comboboxblockname_SelectedIndexChanged event handler, something like this (i.e. add following subroutine in the event handler and pass the comboboxbloxkname.Text in):

Private Sub ListBlockAttribute(blockName As string)
    Dim attNames As New List(of String)()
    Using tran As Transaction.......
        Dim bt As BlockTable = ....''Get blockTable from db
        ''We do not have to test if the block with the name exists in block table and if the block has attribute or not
        ''because we have already done that when loading the block combobox.
        Dim blk As BlockTableRecord = tran.GetObject(bt(blockName), OpenMode.ForRead)
        For Each id As ObjectId in blk
            Dim att As AttributeDefinition=TryCast(tran.GetObject(id, OpenMode.ForRead), AttributeDefinition)
            If att IsNot Nothing Then attNames.Add(att.Tag)
        Next
    End Using

    ''Now you can use the list to populate the other combobox
    comboboxAttribute.Items.Clear()
    If (attName.Count=0)
        comboboxAttribute.Enabled=False
    Else
        comboboxAttribute.Enabled=True
        For Each att As String in attNames
            comboboxAttribute.Items.Add(att)
        Next
    End If
End Sub

sybold

  • Newt
  • Posts: 62
Re: block attribute definition
« Reply #2 on: September 20, 2012, 02:29:34 AM »
great got it working now

Code: [Select]
        Dim attdefs As New List(Of String)()
        Using trx As Transaction = db.TransactionManager.StartTransaction()
            Dim bt As BlockTable = TryCast(db.BlockTableId.GetObject(OpenMode.ForRead), BlockTable)
            Dim blk As BlockTableRecord = trx.GetObject(bt(ComboBoxblocks.Text), OpenMode.ForRead)
            For Each id As ObjectId In blk
                Dim att As AttributeDefinition = TryCast(trx.GetObject(id, OpenMode.ForRead), AttributeDefinition)
                If att IsNot Nothing Then attdefs.Add(att.Tag)
            Next
        End Using
        For Each att As String In attdefs
             ComboBoxatts.Items.Add(att)
        Next

fixo

  • Guest
Re: block attribute definition
« Reply #3 on: September 20, 2012, 05:00:38 AM »
Another way is to use DataSource for combobox, e.g.:
Code: [Select]
       Dim attNames As New List(Of String)()
        ' Your rest code here
        'Fill combobox named cbxAttributes
        cbxAttributes.DataSource = Nothing '<-- avoid conflict to rewrite binding list
        If (attNames.Count > 0) Then
            cbxAttributes.DataSource = attNames
            cbxAttributes.SelectedIndex = -1 ' <--avoid to select a first item in the combo
        End If