Hatching objects in AutoCAD with pywin32

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.

hatch.AppendOuterLoop(outer)
Figure 1: Hatched the closed-loop using pywin32 on AutoCAD template

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
Figure 2: Changed pattern scale using pywin32

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)
Figure 3: Added an inner loop inside the hatched area using pywin32

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.

You May Also Like

Leave a Reply

3 comments

Ben says:

Thanks for the thorough tutorial. ADouble and APoint give out errors, Python says they are not define, eventough I just copied your code. Any thoughts on That?

Maybe you have the complete Code somewhere for us to share?

Kind regards and keep up the good work 😉

Hi Ben,

Thanks for the appreciation, that motivates me to post more use cases of pyautocad.

You can check my git repository, “https://github.com/tanmayms3009/autocad_automation/blob/master/”, if you still come across any issues feel free to comment here or for any consultation contact me through our contact form.

Kind regards

Viktor says:

just them were forgotten

def APoint(x, y, z =0):
return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x,y,z))

def ADouble(xyz):
return win32com.client.VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (xyz))

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.