我有一张AutoCAD图纸,这是一张电力系统的单线图。我想用VBA来改变颜色。怎么做?抱歉'我的英语水平:)。谢谢!
答案 0 :(得分:1)
此示例将图形中的所有线条都用红色标记:
Sub color_all_lines()
Dim c As Long
Dim lineObj As AcadLine
For c = 0 To ThisDrawing.ModelSpace.Count - 1
If ThisDrawing.ModelSpace.Item(c).ObjectName = "AcDbLine" Then
Set lineObj = ThisDrawing.ModelSpace.Item(c)
lineObj.color = acRed
End If
Next c
End Sub
如果要更改图层(在必须创建名为“ layerRED”的新图层之前) 然后:
Sub change_layer()
Dim c As Long
Dim lineObj As AcadLine
For c = 0 To ThisDrawing.ModelSpace.Count - 1
If ThisDrawing.ModelSpace.Item(c).ObjectName = "AcDbLine" Then
Set lineObj = ThisDrawing.ModelSpace.Item(c)
lineObj.layer = "layerRED"
End If
Next c
End Sub
如果要将图层更改为所有对象:
Sub color_all()
Dim c As Long
Dim lineObj As AcadLine
For c = 0 To ThisDrawing.ModelSpace.Count - 1
Set lineObj = ThisDrawing.ModelSpace.Item(c)
Next c
End Sub
答案 1 :(得分:0)
有很多可用的信息。您可能需要学习 AxDbDocument 。
以下是直接从this网站获取的信息:
如果你在这个论坛中搜索了“AxDbDocument”或“DBX”,那么你 本来可以找到很多关于使用主题的讨论 ObjectDBX用AutoCAD打开“侧面”绘图(即读取绘图 AutoCAD进程的内存无需在AutoCAD编辑器中打开。
关键步骤是创建一个AxDbDocument并将* .dwg文件读入其中。 然后,您可以使用与VBA相同的方式访问图形文件中的数据 ThisDrawing对象。
以下是一个非常简单的代码片段(我使用的是Acad2015, 因此版本代码(xxxxx.20):
Option Explicit Public Sub DbxTest() Dim dwgFiles(0 To 1) As String dwgFiles(0) = "C:\Temp\DbxTest01.dwg" dwgFiles(1) = "C:\Temp\DbxTest02.dwg" Dim doc As AXDBLib.AxDbDocument Dim i As Integer For i = 0 To 1 Set doc = ThisDrawing.Application.GetInterfaceObject("ObjectDBX.AxDbDocument.20") doc.Open dwgFiles(i) DoWorkWithDwg doc Next End Sub Private Sub DoWorkWithDwg(dwg As AXDBLib.AxDbDocument) ''MsgBox dwg.Name Dim count As Integer Dim ent As AcadEntity For Each ent In dwg.ModelSpace ''MsgBox ent.ObjectName count = count + 1 Next MsgBox "Drawing: " & dwg.Name & vbCrLf & "Entity Count: " & count End Sub