Region object in AutoCAD with Python

In this blog post I show how to create and manipulate any region object in AutoCAD with pyautocad. I will do so using the AddRegion()-method in pyautocad. pyautocad is one of several Python modules for AutoCAD. I have already covered AutoCAD scripting in Python extensively on this blog.

Region object in AutoCAD

The AutoCAD region object represents 2D enclosed areas. AutoCAD region objects can be polylines, circles, arcs, splines, rectangles etc., as long as they are connected and thereby form a closed entity.

AutoCAD region objects can be combined and used as a single regions. This can be done by utilizing some methods. This includes methods such as Union, Intersection or Subtraction.

Collectively these methods are called AutoCAD boolean methods.

Creating a region object in AutoCAD (win32com etc.)

It is not possible to a create region object in AutoCAD using pyautocad. This is due to some module limitations. Hence in this article I will use the AddRegion() method using the win32com and pythoncom Python modules.

Before creating any AutoCAD region object I bootstrapped my application. This facilitates a connection to AutoCAD. It furthermore allows me to fetch the model space.

In the figure below I display an enclosed AutoCAD polyline object with an overlapping circle.

Creating a region object in AutoCAD

I created the objects in above figure using the code below:

#Enclosed polyline
pl1 = acad.AddPolyline(ad(0,0,0,1000,0,0,1000,500,0,750,500,0,750,1000,0,250,1000,0,250,500,0,0,500,0,0,0,0))

#Circle
c1 = acad.AddCircle(ap(500, 1000, 0), 250)

Now, to create a region I must pass an array of enclosed AutoCAD objects to the AddRegion()-method. I do so below:

#Syntax:
#object.AddRegion(array_of_enclosed_objects)
#Here object can be Block, ModelSpace or PaperSpace

acad.AddRegion(win32com.client.VARIANT(VT_ARRAY | VT_DISPATCH, (pl1, c1)))
Adjusting region object in AutoCAD

Here I can see that the object is converted into a region. The same is true for the polyline. The region object the original one.

Performing boolean operations on region objects

Boolean operations can be performed on 3D solids as well as on AutoCAD region objects.

To use the region object I need to fetch the same from the AutoCAD template using iteration. This is because the AddRegion()-method returns nothing but an array of AutoCAD objects.

In the Python coding example shown below I use a dictionary to save AutoCAD region objects that I create.

regions = {}
i=0
for l in acad1.iter_objects_fast(object_name_or_list="Region"):
    print(str(l.ObjectID) + ": " + l.ObjectName)
    key = "reg" + str(i)
    regions[key] = l
    i+=1

As mentioned by me earlier in this post I can perform three types of boolean operations to create a single union of regions:

  • Union (0)
  • Intersection (1)
  • Subtraction (2).

I will use the numbers mentioned in the brackets of the above listed methods as input parameters. When calling the Boolean()-method they declare the type of boolean operation that I want to perform. At the same time I will move the region object to the right. This will make the difference more clear.

#Syntax:
#object1.Boolean(operation_id, object2)
#Here objects could be 3DSolids or Regions

regions["reg1"].Boolean(2, regions["reg0"])

for l in acad1.iter_objects_fast(object_name_or_list="Region"):
    print(str(l.ObjectID) + ": " + l.ObjectName)
    l.Move(APoint(0, 0, 0), APoint(1500, 0, 0))
Final step of adjusting region object in AutoCAD

It is clear that the new object is created as a single unified region from its parent regions.

Concluding remarks and related content

For further blog posts covering AutoCAD automatization please check my other blog posts related to pyautocad and pywin32. Please leave any questions that you might have as a comment below. Feel free to contact me for any technical assistance. You can do so by using our contact form.

Here are some related articles covering AutoCAD automatization and AutoCAD scripting in Python:

Leave a Reply

Leave a Reply

Your email address will not be published.

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

Close

Meta