Python + AutoCAD polyline drawing summary under Windows

Introduction

Under Windows, use the comtypes library to operate autocad through ActiveX, so as to read AutoCAD data

  • comtypes.client

AutoCAD ActiveX

  • GetActiveObject('AutoCAD.Application')
  • Document
  • ModelSpace

1. Get the AutoCAD object

import comtypes.client #Get
 the AutoCAD application object 
acad = comtypes.client.GetActiveObject( ' AutoCAD.Application ' )
 #Get the current document 
doc = acad.ActiveDocument #Get
 the current document model space 
model_space = doc.ModelSpace

2. Create a 2-dimensional polyline (LightWeightPolyline)

import array
points = array.array('d', range(10))
acad.ActiveDocument.ModelSpace.AddLightWeightPolyline(points)

3. Read 2-dimensional polyline (LightWeightPolyline)

apline = comtypes.client.GetBestInterface(apline)
print(apline.Coordinates)

4. Add polyline complete example

#!/usr/bin/env python
# -*- coding:utf-8 -*-

#Import the standard library 
import comtypes.client
 import array

#Get the AutoCAD application object 
acad = comtypes.client.GetActiveObject( ' AutoCAD.Application ' )
 #Get the current document 
doc = acad.ActiveDocument #Get
 the current document model space 
model_space = doc.ModelSpace

#Generate polyline points 
points = array.array( ' d ' , range(10 ))
 #Add polyline 
model_space.AddLightWeightPolyline(points)

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325832167&siteId=291194637