Mirror object on a 2D plane with pyautocad (Python)

2d mirror image

We have seen how to draw some of the basic objects, fetching their basic properties and some other features too in our previous blog posts from this pyautocad series. In this particular blog post, we will look at, how we can create a mirror image of any object.

1. Setting up the environment for mirror objects in Python

As usual, we will start by importing our pyautocad library and will draw a line to create a mirror image of the same.

from pyautocad import Autocad, APoint

acad = Autocad(create_if_not_exists=True)

l1 = acad.model.AddLine(APoint(0, 0, 0), APoint(100, 100, 0))
Figure 1: Line

2. Creating mirror images with pyautocad

Creating a mirror image from an existing object needs a very simple command. Please check the syntax given below.

# Syntax:

object.Mirror(point1, point2)

Here, point1 & point2 are the start point and endpoint of the line with reference to which the object is being mirrored.

For instance, we will create a mirror image of the existing line with reference to an invisible line starting from point (100, 100, 0) to point (0, 100, 0).

l1.Mirror(APoint(100, 100, 0), APoint(100, 0, 0))
Figure 2.1: Mirror image of line l1

Now, let’s move this imaginary line of reference 50 units away from the point (150, 0, 0) and check.

l1.Mirror(APoint(150, 100, 0), APoint(150, 0, 0))
Figure 2.2: Mirror image of line l1 (Reference line shifted by 50 units)

As we can see from Figure 2.2, I have purposely drawn the reference line in the middle.

With reference to this central line, the mirror image of line l1 is created on the right-hand side of the reference line.

3. Concluding remarks regarding mirror functions with pyautocad

The reference line we specify in the mirror command can start from any point and end at any point.

E.g. rather than specifying start point, endpoint as (100, 100, 0), (100, 0, 0) we can also write (100, 1582, 0), (100, -652.28, 0).

i.e. In both cases, the result will be the same as shown in Figure 2.1. Because just the length of the reference line is changing and not the position.

For more information on AutoCAD itself, you can also review the Autodesk documentation.

Leave a Reply

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.

Close

Meta