我正在尝试使用python和win32com自动化Microstation但是我尝试重新发送通过COM接口创建的对象时出现以下错误。任何人都可以帮我排除故障吗?
import win32com.client
MS = win32com.client.Dispatch("MicroStationDGN.Application")
MS.Visible = 1
DF = MS.OpenDesignFile("C:\\mydgn.dgn", False)
print(DF.Models.Count)
startPoint = MS.Point3dFromXYZ(2.0,2.0,0.0)
endPoint = MS.Point3dFromXYZ(4.0,4.0,0.0)
line = MS.CreateLineElement2(None, startPoint, endPoint)
print(line)
MS.ActiveModelReference.AddElement(line)
line.redraw()
在AddElement
行上的我收到以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-54f93cd6e4bc> in <module>()
16 line = MS.CreateLineElement2(None, startPoint, endPoint)
17 print(line)
---> 18 MS.ActiveModelReference.AddElement(line)
19 line.redraw()
20
C:\Users\user\AppData\Local\Temp\gen_py\3.6\CF9F97BF-39F2-4B8E-835C-8BE9E99DAF5Bx0x8x0.py in AddElement(self, Element)
16408
16409 def AddElement(self, Element=defaultNamedNotOptArg):
> 16410 return self._oleobj_.InvokeTypes(1610743813, LCID, 1, (24, 0), ((9, 1),),Element
16411 )
16412
TypeError: The Python instance can not be converted to a COM object
但是打印给了我一个有效的com对象......
(<win32com.gen_py.Bentley MicroStation DGN 8.9 Object
Library._LineElement instance at 0x119452224>, com_struct(X=2.0,
Y=2.0, Z=0.0), com_struct(X=4.0, Y=4.0, Z=0.0))
ExcelVBA中的等价物如下,假设Microstation DGN Library作为参考模块加载。
Sub MSFromXlReference()
Dim MS As MicroStationDGN.Application
Dim DesignFile As DesignFile
Dim Line As LineElement
Dim startPoint As Point3d
Dim endPoint As Point3d
Set MS = New MicroStationDGN.Application
MS.Visible = 1
Set DesignFile =
MS.OpenDesignFile("C:\\mydgn.dgn", False)
startPoint = MS.Point3dFromXYZ(2#, 2#, 0#)
endPoint = MS.Point3dFromXYZ(4#, 4#, 0#)
Set Line = MS.CreateLineElement2(Nothing, startPoint, endPoint)
MS.ActiveModelReference.AddElement Line
Line.redraw
End Sub