Add attribute to block: Autocad API VB.net

匿名 (未验证) 提交于 2019-12-03 01:19:01

问题:

I am using below code to add attribute to certain block,

But it does not work, I am not getting what exactly going wrong and there is no error.

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 

回答1:

I believe the main problem are the BlockReference location and that you did not save the file. I made some adjusts on the code, but could not fully test it, check the comments below.

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 


回答2:

I notice you're trying to add an AttributeDefinition to a Block Reference.

You'll need to add the AttributeDefinition to the BlockTableRecord and then update the AttributeReference in the BlockReference.

See here for more info: http://adndevblog.typepad.com/autocad/2012/07/changing-block-definition-of-an-block-reference.html

Also, are you running the ATTSYNC command after to make sure your block reference is displaying correctly?



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!