Hello everone,


I ran into problems trying automating AutoCAD with ActivePython 2.1.1 
Build 212. 
What I try to do, is to pass arrays to a Com-Object in AutoCAD like:


VBA-Version:

Sub Ch3_ZoomWindow()
  ’ ZoomWindow
  MsgBox "Perform a ZoomWindow with:" & vbCrLf & "1.3, 7.8, 0" & vbCrLf 
& "13.7, -2.6, 0", , "ZoomWindow" 
  Dim point1(0 To 2) As Double
  Dim point2(0 To 2) As Double
  point1(0) = 1.3: point1(1) = 7.8: point1(2) = 0
  point2(0) = 13.7: point2(1) = -2.6: point2(2) = 0
  ThisDrawing.Application.ZoomWindow point1, point2
End Sub


which I tried to convert into Python this way:



import win32com.client.dynamic
from win32com.client import constants,Dispatch

from win32com.client import gencache
gencache.EnsureModule('{C094C1E2-57C6-11d2-85E3-080009A0C626}', 9, 1, 
1) 

app = win32com.client.Dispatch("AutoCAD.Application")
print "ACAD Version is", app.Version
app.Visible = 1

app.Documents.Open("C:\Lars\A-04-01.DWG")
app.WindowState = constants.acMax
p1 = [1.3, 7.8, 0]    
# p1 = 1.3, 7.8, 0.0 # doesn't work either
p2 = [13.7, -2.6,0 ]
# p2 = 13.7, -2.6, 0.0 # doesn't work either
app.ZoomWindow(p1,p2)


which results in 

>>> ACAD Version is 15.0
Traceback (most recent call last):
  File "C:\Lars\Python21\Pythonwin\pywin\framework\scriptutils.py", 
line 301, in RunScript 
    exec codeObject in __main__.__dict__
  File "C:\Lars\Python21\Work\AutoCAD3.py", line 20, in ?
    app.ZoomWindow(p1,p2)
"C:\Lars\Python21\win32com\gen_py\C094C1E2-57C6-11D2-85E3-080009A0C626
  File x9x1x1.py", line 1031, in ZoomWindow 
    return self._oleobj_.InvokeTypes(0x28, LCID, 1, (24, 0), ((12, 1), 
(12, 1)),LowerLeft, UpperRight) 
com_error: (-2147352567, 'Exception occurred.', (0, 'AutoCAD', 'Invalid 
argument LowerLeft in ZoomWindow', 'C:\\Program 
Files\\ACAD2000\\HELP\\OLE_ERR.HLP', -2145320939, -2147024809), None) 


What am I doing wrong?

Trying to learn more about COM and Python I investigated the 
testMSOffice.py from the Distribution and got another error message: 

Starting Word 8 for dynamic test
Starting Word 8 for non-lazy dynamic test
Starting MSWord for generated test
Starting Excel for Dynamic test...
Traceback (most recent call last):
  File "C:\Lars\Python21\win32com\test\testMSOffice.py", line 146, in 
TestAll 
    TextExcel(xl)
  File "C:\Lars\Python21\win32com\test\testMSOffice.py", line 134, in 
TextExcel 
    xl.Cells(6,2).NumberFormat = "d/m/yy h:mm"
  File "C:\Lars\Python21\win32com\client\dynamic.py", line 482, in 
__setattr__ 
    raise AttributeError, "Property '%s.%s' can not be set." % 
(self._username_, 
 attr)
AttributeError: Property '<unknown>.NumberFormat' can not be set.


Seems to be a completely different problem but makes it hard learn from 
it. 





Since I don't expect, that everyone knows about the object model used 
by AutoCAD I have added a paragraph about 
'Using Variants for Array Data' from the 'AutoCAD 2000 ActiveX and VBA 
Developer's GUide' which may give someone 
with more experience a hint where my problem is.
 
-------------------------

Using Variants for Array Data

Variants are used to pass array data in and out of AutoCAD ActiveX 
Automa-tion. 
This means that your arrays must be a variant to be accepted by
AutoCAD ActiveX Automation methods and properties. In addition, array
data output from AutoCAD ActiveX Automation must be handled as a
variant.

NOTE In AutoCAD, VBA input arrays are automatically converted to 
variants. 
This means that you don’t have to provide a variant array as input to 
the ActiveX 
Automation methods and properties when using them from VBA. However, 
all 
the output arrays will be in the form of variants, so remember to 
handle them 
appropriately.

Converting Arrays to Variants
AutoCAD ActiveX Automation provides a utility method to convert an 
array 
of data into a variant. This method is the CreateTypedArray method, 
which 
creates a variant that contains an array of integers, floating numbers, 
dou-bles, 
and so forth. You can pass the resulting variant into any AutoCAD
method or property that accepts an array of numbers as a variant.
The CreateTypedArray method takes as input the type of values that are 
in 
the array, and the array of data to be converted. It returns the array 
of values 
as a variant. The following code converts three arrays using 
CreateTypedAr-ray: 
the coordinates for a spline’s fit points, and the start and end 
tangent of 
the spline. It then passes the variant into the AddSpline method to 
create the 
spline.


Creating a spline using the CreateTypedArray method
Sub Ch2_CreateSplineUsingTypedArray()
  ’ This example creates a spline object in model space
  ’ using the CreateTypedArray method.
  Dim splineObj As AcadSpline
  Dim startTan As Variant
  Dim endTan As Variant
  Dim fitPoints As Variant
  Dim noOfPoints As Integer
  Dim utilObj As Object ’ late bind the Utility object
  Set utilObj = ThisDrawing.Utility
  ’ Define the Spline Object
  utilObj.CreateTypedArray startTan, vbDouble, 0.5, 0.5, 0
  utilObj.CreateTypedArray endTan, vbDouble, 0.5, 0.5, 0
  utilObj.CreateTypedArray fitPoints, vbDouble, 0, 0, 0, 5, 5, 0, 10, 
0, 0 
  noOfPoints = 3
  Set splineObj = ThisDrawing.ModelSpace.AddSpline (fitPoints, 
startTan, endTan) 
  ’ Zoom in on the newly created spline
  ZoomAll
End Sub


--------------------


Best regards,


Lars
_______________________________________________
ActivePython mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/activepython

Reply via email to