djee 发表于 2019-1-21 09:57:47

更新侧数据库中的属性(AutoCAD 2019)

此代码不再适用于AutoCAD 2019。我的目标是框架4.7。托管.NET参考指南指定函数<pre>Database.ReadDwgFile(string, FileShare, bool, string) Method
已过时,将在未来版本中删除。调试时,它不会遍历BlockTableRecord中的对象ID?功能是否已被替换?我正在研究图形系统的变化…但任何输入都很好…
谢谢。
Public Shared Sub UpdateAttributesInSideDatabase(ByVal FilePath As String, blockName As String, attbName As String, attbValue As String)
      Dim doc As Document = Application.DocumentManager.MdiActiveDocument
      Dim currentDb As Database = doc.Database
      Dim ed As Editor = doc.Editor
      Using targetDb As New Database(False, True)
            Try
                targetDb.ReadDwgFile(FilePath, System.IO.FileShare.ReadWrite, True, "")
                Using tr As Transaction = targetDb.TransactionManager.StartTransaction()
                  Dim layoutDict As DBDictionary = tr.GetObject(targetDb.LayoutDictionaryId, OpenMode.ForRead)
                  For Each dicEnt As DBDictionaryEntry In layoutDict
                        Dim lay As Layout = DirectCast(dicEnt.Value.GetObject(OpenMode.ForRead), Layout)
                        ''do your stuffs in every layout
                        If lay.LayoutName"Model" Then
                            Dim btr As BlockTableRecord = tr.GetObject(lay.BlockTableRecordId, OpenMode.ForRead)
                            For Each id As ObjectId In btr
                              If id.ObjectClass.DxfName = "INSERT" Then
                                    Dim br As BlockReference = DirectCast(tr.GetObject(id, OpenMode.ForRead), BlockReference)
                                    ' ... to see whether it's a block with
                                    ' the name we're after
                                    If br.Name.ToUpper() = blockName.ToUpper() Then
                                        For Each arId As ObjectId In br.AttributeCollection
                                          Dim ar As AttributeReference = DirectCast(tr.GetObject(arId, OpenMode.ForRead), AttributeReference)
                                          ' ... to see whether it has
                                          ' the tag we're after
                                          If ar.Tag.ToUpper() = attbName.ToUpper() Then
                                                ' If so, update the value
                                                ' and increment the counter
                                                ar.UpgradeOpen()
                                                ar.TextString = attbValue
                                                'realign attributes after editing their values
                                                'ar.AdjustAlignment(targetDb)
                                                'ar.DowngradeOpen()
                                          End If
                                        Next
                                    End If
                              End If
                            Next
                        End If
                  Next
                  'end of code from template insert
                  tr.Commit()
                End Using
                targetDb.SaveAs(FilePath, DwgVersion.Current)
            Catch ex As Autodesk.AutoCAD.Runtime.Exception
                ed.WriteMessage(vbLf & "Error while running > " + ex.Message)
            End Try
      End Using
    End Sub
**** Hidden Message *****

Jeff_M 发表于 2019-1-21 10:25:10

更改此选项:
目标数据库。ReadDwgFile(文件路径,System.IO.FileShare.ReadWrite,True,“”
指向以下对象:
targetDb。ReadDwgFile(文件路径,FileOpenMode.OpenForReadAndWriteNoShare,True,“”)
页: [1]
查看完整版本: 更新侧数据库中的属性(AutoCAD 2019)