前些日子做了一个测试,VB,python,vb.net
VB6.0利用ActiveX 操作CAD 画圆 10000个圆 用时 10.05秒
VB.net 利用objectARX技术 操作CAD画圆速度最快 ,画10000圆,用时 0.39 秒
python 利用ActiveX 操作CAD画圆 10000个圆 用时 27.95秒
下面是代码
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim Acadapp As Object
Dim acaddoc As Object
Dim StartT As Long '获取运算开始时间
Dim SpendT As Long '获取运算结束时间
Private Sub Form_Load()
StartT = GetTickCount
Set Acadapp = GetObject(, "autocad.application")
Set acaddoc = Acadapp.ActiveDocument
Dim pt(0 To 2) As Double
Dim a As Object
Dim i As Long
pt(0) = 0: pt(1) = 0
For i = 1 To 10000
Set a = acaddoc.ModelSpace.AddCircle(pt, 10) '画圆
Set a = acaddoc.ModelSpace.AddText(pt(1), pt, 5) '写文字
pt(1) = pt(1) + 10
Next i
SpendT = GetTickCount - StartT
acaddoc.Utility.Prompt ("本次操作耗时:" & Format(SpendT / 1000, "0.00") & "秒")
End
End Sub
#python 利用ActiveX 操作CAD画圆 10000个圆 用时 27.95秒
#代码量最少
# 加一个添加文字用时56.60秒
from pyautocad import Autocad,APoint
import time
time_start=time.time()
acad=Autocad()
p1=APoint(0,0)
for i in range(0,10000):
acad.model.AddCircle(p1,10)
acad.model.AddText(p1.y, p1,5)
p1.y+=10
time_end=time.time()
acad.prompt(time_end-time_start)
'VB.net 利用objectARX技术 操作CAD画圆速度最快 ,画10000圆,用时 0.39 秒
'引用CAD安装目录下的 acdbmgd.dll,acmgd.dll这两个文件,将本地复制设置为false
'编译生成,用 netload 命令加载程序,执行命令 netCircle
' 加一个添加文字用时1.89秒
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Colors
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Public Class Class1
Public StartTime As System.DateTime
Public EndTime As System.DateTime
Public ts As System.TimeSpan
Public Sub CreateCircle()
StartTime = System.DateTime.Now
Dim pt(0 To 2) As Double
Dim i As Integer
pt(0) = 0 : pt(1) = 0 : pt(2) = 0
For i = 1 To 10000 '画10000个圆
Dim circleId As ObjectId = AddCircle(New Point3d(pt(0), pt(1), pt(2)), 5)
pt(1) = pt(1) + 10
Next i
EndTime = System.DateTime.Now
ts = EndTime.Subtract(StartTime)
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage("画" & i - 1 & "圆,用时 " & ts.TotalSeconds.ToString & " 秒")
'MsgBox(ts.TotalSeconds.ToString)
End Sub
Public Shared Function AddCircle(ByVal cenPt As Point3d, ByVal radius As Double) As ObjectId
' 在内存中创建一个圆对象
Dim ent As New Circle(cenPt, Vector3d.ZAxis, radius)
' 调用AppendEntity函数,将圆加入到模型空间.
Dim entId As ObjectId = AppendEntity(ent)
Return entId
End Function
' 将图形对象加入到模型空间的函数.
Public Shared Function AppendEntity(ByVal ent As Entity) As ObjectId
' 得到当前文档图形数据库.
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim entId As ObjectId
Using trans As Transaction = db.TransactionManager.StartTransaction
' 以读方式打开块表.
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
' 以写方式打开模型空间块表记录.
Dim btr As BlockTableRecord = trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
' 将图形对象的信息添加到块表记录中,并返回ObjectId对象.
entId = btr.AppendEntity(ent)
' 把图形对象添加到事务处理中.
trans.AddNewlyCreatedDBObject(ent, True)
' 提交事务处理.
trans.Commit()
End Using
Return entId
End Function
' 由插入点、文字内容、文字高度和倾斜角度创建单行文字的函数.
Public Shared Function AddText(ByVal position As Point3d, ByVal textString As String, ByVal height As Double, ByVal oblique As Double) As ObjectId
Try
' 在内存中创建单行文字对象.
Dim ent As New DBText()
' 设置文字插入点.
ent.Position = position
'设置文字内容.
ent.TextString = textString
'设置文字高度.
ent.Height = height
'设置文字倾斜角.
ent.Oblique = oblique
' 调用EntityToModelSpace函数,将单行文字加入到模型空间.
Dim entId As ObjectId = AppendEntity(ent)
Return entId
Catch
' 创建失败,则返回一个空的ObjectId.
Dim nullId As ObjectId = ObjectId.Null
Return nullId
End Try
End Function
End Class
复制代码
不知道为什么他依然出现了同样的错误:
PyDev console: starting.
Python 3.9.8 (tags/v3.9.8:bb3fdcf, Nov 5 2021, 20:48:33) [MSC v.1929 64 bit (AMD64)] on win32
runfile('C:/pycharm_project/pythonProject666/autocad/pywin32/demo555.py', wdir='C:/pycharm_project/pythonProject666/autocad/pywin32')
test.dwg
Traceback (most recent call last):
File "C:\Python3.9.8\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "[i]", line 1, in
File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/pycharm_project/pythonProject666/autocad/pywin32/demo555.py", line 36, in
RetVal = msp.AddExtrudedSolidAlongPath(reval_region2,RetVal_1)
File "", line 2, in AddExtrudedSolidAlongPath
TypeError: The Python instance can not be converted to a COM object