我创建了一个从autocad获取对象的程序(例如,line,blocks),然后使用VBA分析对象,然后在excel工作簿中写入分析结果。我使用VBA因为它在excel中可用。但是从VBA到excel和从VBA到autocad的通信速度一直很慢。我正在寻找使用pyautocad。在我深入研究一门新语言之前,如果pyautocad比VBA更快,我想要一些建议,尤其是在与autocad和excel进行通信时。
我已经粘贴了下面代码的一小部分,因此您可以了解我如何使用autocad和excel。我有几个其他对象的相同程序。问题是当我对autocad所需的所有对象执行此操作时,此过程会花费大量时间。
从Autocad读取并写入excel的示例代码:
'-----------------Select all lines------------
'set variables to be used
Set MySelection = acadDoc.SelectionSets.Add("MySelection")
FilterType(0) = 0: FilterData(0) = "line" 'object type
FilterType(1) = 8: FilterData(1) = "MyLayer" 'layer name
'select all the lines in acad
MySelection.Select acSelectionSetAll, , , FilterType, FilterData
'---------------
LineWB.Worksheets(CurrentWorkSheet).Range("A1") = MySelection.Count
'---iterate through the selection and update or make pipes in the database-----
For x = 0 To MySelection.Count - 1
Set AcadLineVar = MySelection.Item(x)
CellVar = "A" & x + 5
LineWB.Worksheets(CurrentWorkSheet).Range(CellVar) = AcadLineVar.StartPoint(0)
CellVar = "B" & x + 5
LineWB.Worksheets(CurrentWorkSheet).Range(CellVar) = AcadLineVar.StartPoint(1)
CellVar = "C" & x + 5
LineWB.Worksheets(CurrentWorkSheet).Range(CellVar) = AcadLineVar.StartPoint(2)
CellVar = "D" & x + 5
LineWB.Worksheets(CurrentWorkSheet).Range(CellVar) = AcadLineVar.EndPoint(0)
CellVar = "E" & x + 5
LineWB.Worksheets(CurrentWorkSheet).Range(CellVar) = AcadLineVar.EndPoint(1)
CellVar = "F" & x + 5
LineWB.Worksheets(CurrentWorkSheet).Range(CellVar) = AcadLineVar.EndPoint(2)
Next x
'-----------
从excel到autocad的示例代码:
'Get Properties
InterfacePropVar = BlocksWB.Worksheets("Interface").Range("D5:M100000")
'-----For interface tags-----
NumberOfInterface = BlocksWB.Worksheets("Interface").Range("A1")
For a = 1 To NumberOfInterface
PtS(0) = InterfacePropVar(a, 1) + 5
PtS(1) = InterfacePropVar(a, 2) + 5
PtS(2) = InterfacePropVar(a, 3)
Name = InterfacePropVar(a, 4)
Set TextVar = acadDoc.ModelSpace.AddText(Name, PtS, 3)
TextVar.Layer = "InterfaceTagLayer"
TextVar.Color = acRed
'-----
Next a
'-----
答案 0 :(得分:0)
我在AutoCAD和Excel之间运行了一个宏,发现所有主要的耗时都是在VBA读取和写入AutoCAD和Excel的时间超过绝对必要的时间。如果您可以通过大量的单步执行所有装载和卸载,那么这确实可以节省时间。由于用户自己的学习曲线,我会在切换语言之前首先采用这种方法。但是如果你想学习Python,那么这也许是你的机会......