添加属性到块:Autocad API VB.net

时间:2016-02-10 20:25:16

标签: vb.net autocad autocad-plugin

我使用下面的代码将属性添加到某个块,

但是它不起作用,我没有得到确切的错误并且没有错误。

Public Class addattribute
    Public Function addnewattribute()


        Dim attdef As New AttributeReference
        Dim templatepath As String = "C:\Users\sesa388372\Documents\Visual Studio 2015\Projects\SchneiderMacros\Wtemplate.DWG"
        Dim db As Database = New Database
        db.ReadDwgFile(templatepath, System.IO.FileShare.ReadWrite, False, "")
        Using tr As Transaction = db.TransactionManager.StartTransaction
            attdef.SetDatabaseDefaults(db)
            attdef.Tag = "Cell location"
            attdef.TextString = "AAA"


            Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForWrite)
            Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.PaperSpace), OpenMode.ForWrite)
            For Each objid As ObjectId In btr
                If objid.ObjectClass.Name = "AcDbBlockReference" Then
                    Dim blkref As BlockReference = tr.GetObject(objid, OpenMode.ForWrite)
                    If blkref.Name = "TB-D-ATTR" Then

                        blkref.AttributeCollection.AppendAttribute(attdef)

                    End If
                End If
            Next

            tr.AddNewlyCreatedDBObject(attdef, True)
            tr.Commit()

        End Using

        Return Nothing
    End Function
End Class

2 个答案:

答案 0 :(得分:1)

我认为主要问题是BlockReference位置,并且您没有保存文件。我对代码进行了一些调整,但无法对其进行全面测试,请查看下面的评论。

Public Sub addnewattribute() ' don't you mean define as Sub?
  Dim templatepath As String = "C:\Users\sesa388372\Documents\Visual Studio 2015\Projects\SchneiderMacros\Wtemplate.DWG"
  ' you must dispose this side database, the 'using' will take care of it
  Using db As Database = New Database(False, True) ' specify the parameters
    db.ReadDwgFile(templatepath, System.IO.FileShare.ReadWrite, False, "")
    db.CloseInput() ' this should help the Save() method
    Using tr As Transaction = db.TransactionManager.StartTransaction
      Dim bt As BlockTable = tr.GetObject(db.BlockTableId, OpenMode.ForWrite)
      Dim btr As BlockTableRecord = tr.GetObject(bt(BlockTableRecord.PaperSpace), OpenMode.ForWrite)
      For Each objid As ObjectId In btr
        If objid.ObjectClass.Name = "AcDbBlockReference" Then
          Dim blkref As BlockReference = tr.GetObject(objid, OpenMode.ForWrite)
          If blkref.Name = "TB-D-ATTR" Then
            ' define this variable inside the loop, you cannot reuse it
            Dim attdef As New AttributeReference
            attdef.SetDatabaseDefaults(db)
            attdef.Tag = "Cell location"
            attdef.TextString = "AAA"
            attdef.SetAttributeFromBlock(blkref.BlockTransform) ' adjust the location
            blkref.AttributeCollection.AppendAttribute(attdef)
            tr.AddNewlyCreatedDBObject(attdef, True)
          End If
        End If
      Next
      tr.Commit()
    End Using
    'Return Nothing ' in this case, a Sub (instead function) should be better
    db.Save() ' did you miss to save changes?
  End Using
End Sub

答案 1 :(得分:0)

我注意到你正在尝试将一个AttributeDefinition添加到Block Reference。

您需要将AttributeDefinition添加到BlockTableRecord,然后更新BlockReference中的AttributeReference。

有关详细信息,请参阅此处:http://adndevblog.typepad.com/autocad/2012/07/changing-block-definition-of-an-block-reference.html

此外,您是否正在运行ATTSYNC命令以确保您的块引用正确显示?