.NET

Reply
Active Contributor
ditran
Posts: 27
Registered: ‎01-28-2013
Message 11 of 15 (132 Views)

Re: Get block reference information error

03-22-2013 02:46 AM in reply to: Alexander.Rivilis

AttDef.textstring ="" although AttributeDefinition has value. What happen?

Moderator
Alexander.Rivilis
Posts: 1,406
Registered: ‎04-09-2008
Message 12 of 15 (129 Views)

Re: Get block reference information error

03-22-2013 02:54 AM in reply to: ditran

ditran wrote:

AttDef.textstring ="" although AttributeDefinition has value. What happen?


I do not know what happen. Maybe you again confused AttributeDefinition in BlockTableRecord and AttributeReference in inserted BlockReference.

Please attach dwg-file with block and explain what do you mean.


Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
ditran
Posts: 27
Registered: ‎01-28-2013
Message 13 of 15 (124 Views)

Re: Get block reference information error

03-22-2013 03:18 AM in reply to: Alexander.Rivilis
 <CommandMethod("test")> _
    Public Sub test()
        Dim blockName As String = "blank_sheet"
        Dim ofd As New OpenFileDialog("CAD FILE", Nothing, "dwg; dwt", "CAD FILE", Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles)
        Dim dr As System.Windows.Forms.DialogResult = ofd.ShowDialog()
        If dr <> System.Windows.Forms.DialogResult.OK Then
            Return
        End If
        Dim sourcefile As String = ofd.Filename

        Dim doc As Document = Application.DocumentManager.MdiActiveDocument
        Dim doclock As DocumentLock = doc.LockDocument()
        Dim ed As Editor = doc.Editor
        Dim acCurDb As Database = doc.Database

        Using doclock
            Using OpenDb As New Database(False, True)
                OpenDb.ReadDwgFile(sourcefile, System.IO.FileShare.ReadWrite, True, "")
                Using opentrans As Transaction = OpenDb.TransactionManager.StartTransaction()
                    Dim openBT As BlockTable = DirectCast(opentrans.GetObject(OpenDb.BlockTableId, OpenMode.ForRead), BlockTable)
                    If openBT.Has(blockName) Then
                        Dim id As ObjectId = openBT(blockName)
                        Dim btAttRec As BlockTableRecord = opentrans.GetObject(id, OpenMode.ForRead)
                        Dim idAtt As ObjectId
                        For Each idAtt In btAttRec
                            Dim ent As Entity = opentrans.GetObject(idAtt, OpenMode.ForRead)
                            If TypeOf ent Is AttributeDefinition Then
                                Dim attDef As AttributeDefinition = CType(ent, AttributeDefinition)
                                ed.WriteMessage(vbCrLf + "attdef.TextString = {0}", attDef.TextString)
                                ed.WriteMessage(vbCrLf + "attdef.Tag = {0}", attDef.Tag)
                            End If
                        Next
                    Else
                        opentrans.Commit()
                        Exit Sub
                    End If
                    opentrans.Commit()
                End Using
            End Using
        End Using

    End Sub

 

Thanks.

Moderator
Alexander.Rivilis
Posts: 1,406
Registered: ‎04-09-2008
Message 14 of 15 (118 Views)

Re: Get block reference information error

03-22-2013 04:15 AM in reply to: ditran

I was right and you confuse BlockTableRecord and BlockReference, AttributeDefinition and AttributeReference.

 

Try this code (no error checking done):

 

<CommandMethod("test")> _
Public Sub test()
  Dim blockName As String = "blank_sheet"
  Dim ofd As New Autodesk.AutoCAD.Windows.OpenFileDialog( _
   "CAD FILE", Nothing, "dwg; dwt", "CAD FILE", _
   Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.DoNotTransferRemoteFiles)
  Dim dr As System.Windows.Forms.DialogResult = ofd.ShowDialog()
  If dr <> System.Windows.Forms.DialogResult.OK Then
    Return
  End If
  Dim sourcefile As String = ofd.Filename

  Dim doc As Document = _
   Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument()
  Dim doclock As DocumentLock = doc.LockDocument()
  Dim ed As Editor = doc.Editor
  Dim acCurDb As Database = doc.Database

  Using doclock
    Using OpenDb As New Database(False, True)
      OpenDb.ReadDwgFile(sourcefile, System.IO.FileShare.ReadWrite, True, "")
      Using opentrans As Transaction = OpenDb.TransactionManager.StartTransaction()
        Dim openBT As BlockTable = _
         DirectCast(opentrans.GetObject(OpenDb.BlockTableId, OpenMode.ForRead), BlockTable)
        If openBT.Has(blockName) Then
          Dim id As ObjectId = openBT(blockName)
          Dim btAttRec As BlockTableRecord = opentrans.GetObject(id, OpenMode.ForRead)
          For Each idBref As ObjectId In btAttRec.GetBlockReferenceIds(True, True)
            Dim bRef As BlockReference = opentrans.GetObject(idBref, OpenMode.ForRead)
            For Each idAttRef In bRef.AttributeCollection
              Dim attRef As AttributeReference = opentrans.GetObject(idAttRef, OpenMode.ForRead)
              ed.WriteMessage(vbCrLf + "attRef.Tag = {0}", attRef.Tag)
              ed.WriteMessage(vbCrLf + "attRef.TextString = {0}", attRef.TextString)
            Next
          Next
        End If
        opentrans.Commit()
      End Using
    End Using
  End Using
End Sub

Пожалуйста не забывайте про Утвердить в качестве решения! Утвердить в качестве решения и Give Kudos!Баллы
Please remember to Accept Solution! Accept as Solution and Give Kudos!Kudos

Active Contributor
ditran
Posts: 27
Registered: ‎01-28-2013
Message 15 of 15 (86 Views)

Re: Get block reference information error

03-24-2013 11:45 PM in reply to: Alexander.Rivilis

Oh. Thanks so much.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.