添加属性到块:Autocad API VB.net [英] Add attribute to block: Autocad API VB.net

查看:24
本文介绍了添加属性到块:Autocad API VB.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码向某些块添加属性,

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:Userssesa388372DocumentsVisual Studio 2015ProjectsSchneiderMacrosWtemplate.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

推荐答案

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

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:Userssesa388372DocumentsVisual Studio 2015ProjectsSchneiderMacrosWtemplate.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

这篇关于添加属性到块:Autocad API VB.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆