In our previous blog posts, we have discussed how to create 2D and 3D objects and to play with their properties.
Now, in this blog post, we are going to learn, how to hatch objects using pywin32 in the AutoCAD template.
Setting up environment using pywin32 module
First of all, we will set up our work environment to integrate python and AutoCAD using the pywin32 module and import some packages.
import win32com.client import pythoncom import math acad = win32com.client.Dispatch("AutoCAD.Application") acadModel = acad.ActiveDocument.ModelSpace
Creating necessary constructors using pywin32 & pythoncom
While dealing with hatching objects in AutoCAD using python, we need certain types of an array of specific data types.
To hatch an object, we need to create a loop. That loop is a group of drawing objects resulting in a closed area.
There can be multiple such objects that can be passed into this array of loops. Hence, as per the AutoCAD documentation, we need an array of “Variants” to create this loop.
Here, we will use the VT_DISPATCH data type to fetch the drawing objects from the AutoCAD template.
Also, we need other constructors such as APoint and Double to draw objects. We have discussed the same in our previous blogs.
def variants(object): return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, (object))
Creating loop of objects in AutoCAD
Now, we need to create a closed space using a single or multiple drawing objects and convert the same into a loop.
This array can contain one or more drawing objects that form a closed space.
out_loop =  sq = acadModel.AddPolyline(ADouble([0,0,0,1000,0,0,1000,1000,0,0,1000,0])) arc = acadModel.AddArc(APoint(0, 500, 0), 500, 90*pi/180, 270*pi/180) out_loop.append(sq) out_loop.append(arc) outer = variants(out_loop)
Creating hatch object and appending the hatch
Now that we have created the closed object to be hatched, we will create a hatch object to add it to the closed-loop.
To create a hatch object, we need AddHatch command with the following syntax:
object.AddHatch(PatternType, PatternName, Associativity)
Here, Pattern Types are 0 for pre-defined hatch types, 1 for the pattern of lines using the current line type,& 2 for pattern name from a PAT file other than the acad.pat file.
At the place of parameter pattern name, we can pass the pattern name as provided by AutoCAD or as per the customized pattern names.
Associativity will contain a boolean value to specify whether the hatch is associative or non-associative. It can only be set while creating the hatch object.
An associative hatch is updated when its boundaries are modified. A nonassociative hatch is not updated when its boundaries are modified.
Let’s create the hatch object now.
hatch = acadModel.AddHatch(0, "ANSI37", True)
Now to add this hatch to the loop we have created, we will use the AppendOuterLoop method.
Properties of AutoCAD hatch object in Python
As we know about other AutoCAD objects, using different methods we can fetch or change properties of hatch objects too.
print(hatch.HatchStyle) print(hatch.PatternName) print(hatch.AssociativeHatch) print(round(hatch.Area,2)) print(hatch.PatternAngle) print(hatch.PatternDouble) print(hatch.PatternScale) print(hatch.PatternSpace) print(hatch.PatternType) print(hatch.NumberOfLoops) O/p: 0 ANSI37 True 1392699.08 0.0 False 1.0 1.0 1 1
Now, let’s change the pattern scale to 10.
hatch.PatternScale = 10
Adding inner loop to hatched AutoCAD object loop
If we want the hatch to be there around the inner object, we have to do reverse work which we are doing here.
We have hatched an outer object. Now we will add an inner object which I do not want to hatch.
Similarly, we can add multiple such inner loops to the hatched area one by one.
in_loop =  in_loop.append(acadModel.AddCircle(APoint(250, 250, 0), 100)) inner = variants(in_loop) hatch.AppendInnerLoop(inner)
Now, let’s check the hatched area after adding this inner loop.
print(round(hatch.Area,2)) O/p: 1361283.16
As we can see from the code output and Figure 3 that the area has been reduced by 31415.92 (area of the circle) since we added the inner loop to the existing hatched area.
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.