In our last blog post we learned how to build the APoint constructor for drawing objects on AutoCAD using python.
In continuation with the same, we will learn how to create the second major constructor i.e. aDouble in this blog post using pywin32.
Setting up AutoCAD environment using pywin32 (win32com)
The detailed discussion on initializing an AutoCAD template using pywin32 is done in one of our previous blogs. Please check the same for more details.
import win32com.client
import pythoncom
acad = win32com.client.Dispatch("AutoCAD.Application")
acad.Visible = True
acadModel = acad.ActiveDocument.ModelSpace
About the aDouble method in pyautocad
Unlike APoint, the aDouble constructor takes x, y & z coordinates as parameters for one or more than one point.
This is to build objects connecting these multiple points and form polylines or polygons.
If we look at the official LISP documentation provided by ActiveX for creating such array we get as code as given below:
(vl-load-com)
(defun c:Example_AddLightWeightPolyline()
;; This example creates a lightweight polyline in model space.
(setq acadObj (vlax-get-acad-object))
(setq doc (vla-get-ActiveDocument acadObj))
;; Define the 2D polyline points
(setq points (vlax-make-safearray vlax-vbDouble '(0 . 9)))
(vlax-safearray-fill points '(1 1
1 2
2 2
3 2
4 4
)
)
;; Create a lightweight Polyline object in model space
(setq modelSpace (vla-get-ModelSpace doc))
(setq plineObj (vla-AddLightWeightPolyline modelSpace points))
(vla-ZoomAll acadObj)
)
They have used, “vlax-make-safearray”, “vlax-vbDouble” for passing multiple points.
According to the AutoLISP reference guide, the “vlax-make-safearray” creates a safe array of type “vlax-vbDouble” which lets us insert double-precision floating-point numbers.
This array of double-precision floating points of type variant is the necessary parameter for creating polylines or polygons.
Creating the aDouble method using pywin32 & pythoncom
Considering that we need an array of double-precision floating points of type variant we shall create an array that can carry multiple coordinate points.
With the help of Windows documentation for the Variant structure, we will choose VT_ARRAY and VT_R8 variants.
Because, VT_ARRAY = A safe array pointer & VT_R8 = An 8-byte real numbers.
There is one more variant type for providing float numbers with lesser byte space i.e. VT_R4 = A 4-byte real numbers.
Now, to create the aDouble function we have to use win32com.client with the variant method.
To create this array we need the pythoncom package too.
This pythoncom package translates between python objects and variant structures listed in Windows documentation. When we call a COM object and pass a python object, pythoncom automatically creates a variant of the right type and passes the variant to COM.
Also, we can use *args as a parameter to be passed in aDouble, specifying the coordinate points
This special syntax *args in function definitions in python is used to pass a variable number of arguments to a function. It is used to pass a non-key worded, variable-length argument list.
Let’s create the aDouble constructor now.
def aDouble(*argv):
return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (argv))
Usage of this aDouble method in AutoCAD automatization
We can draw objects which need multiple points to be passed as parameters.
As discussed above, we can use this constructor for creating polylines or polygons.
To know more about the usage of the aDouble constructor, do check our previous blogs on pyautocad.
For more information do visit Autodesk documentation, Windows documentation & also Windows object model documentation.
Civil engineer interested in automation in core subjects such as civil, mechanical and electrical, using IT skills comprising cloud computing, devops, programming languages and databases along with the technical skills gained while working as a civil engineer since past 3 years.
Leave a Reply