Autocad Draw Circle With Polyline

In this post I will demonstrate how to draw a polygon past creating polylines in AutoCAD with Python, using pyautocad. I will brand use of the aDouble constructor and the AddPolyline method. In previous posts we already demonstrated a unproblematic use instance implementing pyautocad for AutoCAD in Python. Nosotros likewise wrote posts documenting the implementation of e.g. a pyautocad helice, pyautocad spline, and pyautocad solid object.

Motivation for drawing polylines in AutoCAD with pyautocad

In pyautocad nosotros do non have any method assigned to drawing a polygon, rectangle, or triangle. Thus, we have to depict such objects using a polyline, i.e. by using the AddPolyline method in pyautocad.

Importing relevant libraries: pyautocad, os, and math

                      # Importing necessary methods from pyautocad library to describe a polygon: from pyautocad import Autocad, APoint, aDouble  # bone.X_OK: Checks if path tin be executed: from os import X_OK  # Importing math library to compute coordinates for a polygon: from math import *        

Open or create a template from AutoCAD in pyautocad

We tin can open any pre-created AutoCAD template in pyautocad.

Alternatively, we tin set the "create_if_not_exists" parameter to "True" so that the code will automatically generate an autocad template which tin can be saved later:

          acad = Autocad(create_if_not_exists=Truthful)        

Request and embed user input into workflow and lawmaking

With this code we tin create an inscribed polygon.

To draw this polygon we demand some user inputs to generate the cartoon. This includes input apropos center point coordinate for circle, radius of circle.

We can code this as per our preferred coding language. In this example Python, using pyautocad:

          # one. Number of vertices for polygon na = int(input("Enter the number of vertices for the polygone: "))  # 2. Center & Radius of Circle # Eye cc = input("Enter ten, y coordinates for center of circumvolve with comma separators e.yard. x, y: ") ccc = (map(float, cc.split(", "))) # Converting list to tuple as pyautocad accepts tuple as input ccct = tuple(ccc) print('The coordinates for the center of circle : ',ccct) # Radius rc = float(input("Enter the radius of Circle: "))        

Calculate coordinates for vertices of AutoCAD polygon

We can calculate coordinates for vertices of the polygon using the formula below:

          x  +  r * cos( 2 * pi * i / northward ),  y + r * sin(2 * pi * i / due north)        

The components of this formula are list below:

ten, y = Coordinates for the center of the circumvolve

r = Radius of circle

i = Iteration

due north = Number of vertices

I have encoded this equally mentioned below:

          # iii. Summate coordinates i=0 # Creating an empty list for coordinates pgonc=[] for i in range(na):     ten=circular(ccct[0]+rc*cos(2*pi*i/na),2)     y=round(ccct[1]+rc*sin(2*pi*i/na),ii)     z=0     crd = [10, y, z]     pgonc.extend(crd)     i += one # Addind first point again to complete the loop of polygon fp = [pgonc[0], pgonc[1], pgonc[2]] pgonc.extend(fp) # Converting list to tuple as pyautocad accepts tuple as input pgont=tuple(pgonc) impress("Coordinates of polygon: ") print(pgont)        
Figure 1: User Input

Depict the AutoCAD polygon using polylines in pyautocad

To describe the polygon, nosotros need to pass the coordinates to aDouble method in a tuple format:

          polygon = aDouble(pgont)        

Now with the AddPolyline method we can describe the polygon according to the coordinates we passed to aDouble:

          polygond = acad.model.AddPolyline(polygon)        

Cross verification of polylines in pyautocad

We tin bank check whether the centre betoken and radius of the circumvolve are correct in AutoCAD, as depicted on Figures 1, ii & 3:

Polygon drawn with pyautocad
Effigy two: Center of Circle
Circle drawn with pyautocad
Figure 3: Circumvolve Properties

We can also check whether the coordinates calculated every bit shown in Effigy one (175.0, 25.0, 0, 123.18, 96.33, 0, 39.32, 69.08, 0, 39.32, -19.08, 0, 123.18, -46.33, 0, 175.0, 25.0, 0) are matching with the vertices drawn on the template. I have used the ID command from autocad for each endpoint of the vertices. Refer to Figures 4.ane to iv.5.

Figure 4.1: Signal 1
Figure iv.2: Point 2
Figure 4.3: Signal 3
Figure four.4: Point 4
Figure 4.v: Point 5

Rotate the AutoCAD polygon with pyautocad

We can rotate the polygon using rotate method on the variable assigned to polyline:

Here nosotros will attempt rotating the polygon effectually its heart i.e. (100, 25, 0), at 185 degrees.

Syntax for rotation:

object.Rotate(Coordinates for Axis, Bending)

          #Rotate the polygon polygond = polygond.Rotate(APoint(100, 25, 0), 3.14*185/180)        

Annotation: We shall convert the angle in radians if we want to enter the aforementioned in degrees.

Figure 5: Rotated polygon

As per Figure 5, we tin encounter that the new polygon has been created afterward rotation.

Other properties of polylines in pyautocad

We can find other backdrop of the created objects, i.e. hither polygon as well; using some of the methods mentioned below:

          #Surface area = Specifies the area of closed entities: pa = polygond.Area print("Expanse of polygon: " + str(pa))  #Length = Specifies the length of whatever object: pl = polygond.Length print("Perimeter of polygon: " + str(pl))  #Airtight = Specified whether the created object is open or closed: is_closed = polygond.Closed print("Is the Polyline Object a Closed infinite: " + str(is_closed))  #Coordinate: Specify coordinates of a single vertex by passing vertex index no equally a parameter: print("Pinnacle of Vertex: ") print(polygond.Coordinate(one))  #Coordinates: Nosotros tin can become coordinates of all the vertices of polygon: print(polygond.Coordinates)  #Layer = Specifies the layer used to describe the object: print("Perimeter of polygon: " + polygond.Layer)  O/p: Expanse of Polygon: 13374.603200000005 Perimeter of Polygon: 440.84505325868145 Is the Polyline Object a Airtight space: Truthful Elevation of Vertex: (83.00542638047868, -48.05111749652207, 0.0) Elevation of Polyline: (25.27479789063051, eighteen.585627878437045, 0.0, 83.00542638047868, -48.05111749652207, 0.0, 164.22761049486715, -thirteen.72883744859422, 0.0, 156.68772987837394, 74.1081467908993, 0.0, 70.80443535564973, 94.08618027577995, 0.0, 25.27479789063051, 18.585627878437045, 0.0)  Perimeter of Polygon: 0        

Finally, here is a link to the official Autodesk documentation.

championrusestiond.blogspot.com

Source: https://www.supplychaindataanalytics.com/polylines-in-pyautocad-for-drawing-autocad-polygons/

0 Response to "Autocad Draw Circle With Polyline"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel