We have seen in many of our previous blog posts that, we need certain additional functions for creating objects.
APoint is one such method we used with pyautocad while automatizing AutoCAD objects using python.
In this blog post, we will discuss how to create this APoint method while using pywin32 (win32com).
As we have discussed in our previous blog posts, we can use pywin32 same as that we use pyautocad for AutoCAD automatization.
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 APoint method in pyautocad
According to the ActiveX documentation, methods such as AddLine, AddCircle, AddEllipse, etc. having single point reference as their parameter needs single point parameter to be passed of type “Variant”.
The type variant is simply a three-element array of numbers, since we need to pass coordinates.
The APoint method we use while working with pyautocad is its builtin method to pass this array in the form of a tuple .
Now unlike pyautocad, pywin32 or pythoncom doesn’t have any such inbuilt method.
Hence we have to create this manually.
Creating the APoint method using pywin32 & pythoncom
We know that we need an array, and specifically to provide users to pass coordinates even in decimal values, we need array of float.
With the help of Windows documentation for 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 flot numbers with lesser byte space i.e. VT_R4 = A 4-byte real numbers.
Now, to create an APoint function we have to use win32com.client with Variant method.
To create this array we need the pythoncom package too.
Basically, pythoncom can translate 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.
Let’s create the APoint method now:
def APoint(x, y = 0, z = 0):
return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))
As we can see, we have created a method APoint which takes, x, y, z as parameters having default values for y & z set to 0.
Usage of this APoint method in AutoCAD automatization
As I said earlier, the APoint is mainly used in the commands having single point as their parameters.
For instance, when we want to create a circle we pass centerpoint using APoint method, while creating a line we pass the start and end points using APoint method.
To know more about usage of APoint method, do check our previous blogs on pyautocad.
Also, make sure to not to use this method for creating a polyline. Since, AddPolyine method takes a series of coordinate points to connect those resulting into a polyline.
And as we can see from the code above, APoint just takes 3 parameters i.e. x, y & z.
Using APoint for creating polyline will fetch the below-mentioned error:
TypeError: APoint() takes from 2 to 3 positional arguments...
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