我有大量类似的 DWG 文件,每个文件都有一个通过数据链接链接到 excel 的表格。这些表在每个文件中都会随着时间的推移而变化。
我想要做的是转到每个 DWG 文件并更改数据链接,使其指向 excel 中的适当命名范围。
到目前为止,除了更改数据链接指向的位置外,我已经成功完成了所有工作
请注意,发送命令不起作用,因为无法从命令行控制数据链路
这是 autocad 论坛中的一篇文章,它提供了一些启示,但我不知道在哪里可以找到或如何使用 cao 库 https://forums.autodesk.com/t5/visual-basic-customization/repath-the-excel -参考通过-vba/td-p/5432417
'change data link on floor to point to coresponding named range
'here is where the issue starts, open to sugestions
Dim activeDict As Object 'each dictionary to loop thru
Dim activeDataLink As Object 'each data link in the file to loop thry
For Each activeDict In dwgFile.Database.Dictionaries 'loop thru all dictionaries in the file
On Error Resume Next 'some dictionaries don't have the "name" property
If activeDict.Name = "ACAD_DATALINK" Then 'check if the active dictionary is the one for Data links
For Each activeDataLink In activeDict 'loop thru all the data links in the dictionary
Dim dictObj As AcadDictionary
Dim datalinkObj As Object
'another way to acces the data link dictionary
Set dictObj = acadFile.Database.Dictionaries.Item("ACAD_DATALINK") ''test to see if the object is created, it is
'another way to access the datalink in the dictionary
Set datalinkObj = dictObj.Item("SYSTEM SUMMARY NOTES") ''test to see if the object is created, it is 'HERE HERE HERE HERE
'the data link i want to change is called "SYSTEM SUMMARY NOTES" and is present in every file
TEST = datalinkObj.Name 'doesn't work
TEST = datalinkObj.Value 'doesn't work
TEST = datalinkObj.PATH 'doesn't work HERE HERE HERE HERE
Next activeDataLink
End If
On Error GoTo 0
Next activeDict
理想的结果会改变数据链接指向的位置
更新:这让你循环遍历所有字典,直到你到达通过 CAO 库访问的数据链路字典。然后循环通过所有数据链路(我又卡住了)
Sub repathDatalink(dwgFile As AcadDocument, datalinkName As String, xlsFilePath As String, xlsNamedRange As String)
Dim activeDict As Object 'each dictionary to loop thru
Dim activeDataLink As Object 'each data link in the file to loop thry
Dim test
Dim caoLib As Object
Set caoLib = AutoCAD.GetInterfaceObject("CAO.DbConnect.20")
'For Each activeDict In dwgFile.Database.Dictionaries 'loop thru all dictionaries in the file
For Each activeDict In caoLib.GetLinks.Document.Dictionaries 'loop thru all dictionaries in the file
On Error Resume Next
If activeDict.Name = "ACAD_DATALINK" And Not activeDict Is Nothing Then
For Each activeDataLink In activeDict
caoLib.GetLinks.Document.Dictionaries.Item(2).Item (0)
activeDict 'dictionary with all the data links
activeDataLink 'each data link in the datalink dictionary
Next activeDataLink
End If
On Error GoTo 0
Next activeDict
End Sub
据我所知,它通过活动 dwg(我可以处理)