Implementing Python to AutoCAD

Implementing Python to AutoCAD

Anonymous
Not applicable
41,851 Views
2 Replies
Message 1 of 3

Implementing Python to AutoCAD

Anonymous
Not applicable

Good day people of AutoCAD,

firstly, in the link here, I have drew a section view of a model using point cloud, section lines and paperspace.

I am now trying to automate the process that I conducted.

 

Here is the progress so far.

As you can see in Script 2, I am simply repeating the procedure.

I have heard about pyAutoCAD, which connects python and autocad. Here is the link if anyone is interested.

https://media.readthedocs.org/pdf/pyautocad/latest/pyautocad.pdf

 

Unfortunately, with current documentation, I am not cleared about its functionality, since what I need are direct implementation of commands in Autocad to Python workspace, Selection of an object purely based on command line/code without using mouse and/or Assignment of variable based on data in AutoCAD

 

What I mean by this is that in my python script I wish to import commands directly (eg: PCExtractsection, 3dmove, etc) and select a point cloud without any filter. For now, as you could see, I can use fence and manually type in input, however it is not the most effective way. Also, if I can identify boundary of a point cloud and assign it as variables, all the work can be extremely efficient.

 

If you know a method for any of above or who to contact to, any help would be greatly appreciated.

 

 

 

***Script 1***
-DWGUNITS
6
2
4
Y
Y


_POINTCLOUDATTACH
"C:\Users\Charlie Lee\Desktop\2d_elevation\point clouds\Kuring_gai_fullpoint.rcp" 0,0,0 1 0
_ai_selall

_ai_selall
rotate3d X 0,0,0 -90


SECTIONPLANE
O
T
SELECT
l

_ZOOM
e
_Cancel


***Script 2***
PCEXTRACTSECTION
all
a
3DMOVE
FENCE
0,170,0
0,180,0


0,0,0
0,0,-0.1
-PCEXTRACTSECTION
all
3DMOVE
FENCE
0,170,0
0,180,0


0,0,0
0,0,-0.1
-PCEXTRACTSECTION
all
3DMOVE
FENCE
0,170,0
0,180,0


0,0,0
0,0,-0.1
-PCEXTRACTSECTION
all
3DMOVE
FENCE
0,170,0
0,180,0


0,0,0
0,0,-0.1
-PCEXTRACTSECTION
all
3DMOVE
FENCE
0,170,0
0,180,0


0,0,0
0,0,-0.1
-PCEXTRACTSECTION
all
3DMOVE
FENCE
0,170,0
0,180,0



 

0 Likes
41,852 Views
2 Replies
Replies (2)
Message 2 of 3

hgasty1001
Advisor
Advisor

Hi,

 

You can find some information here: using-ironpython-with-autocad

 

Gaston Nunez

0 Likes
Message 3 of 3

serag.hassouna
Advocate
Advocate

Well, you can use comtypes disutil, install it from cmd using

 

python -m pip install comtypes

then, the simplest python script to apply a command would be like that

 

 

#Import needed modules
import os
import comtypes.client
from comtypes import COMError
from comtypes.client import CreateObject, GetActiveObject

def main():
    try: #Get AutoCAD running instance
        acad = GetActiveObject("AutoCAD.Application.20")
state = True except(OSError,COMError): #If autocad isn't running, open it acad = CreateObject("AutoCAD.Application.20",dynamic=True)
state = False
if state: #If you have only 1 opened drawing doc = acad.Documents.Items(0)
else:
filepath = "E:/Dir1/Dir2/myDWG.dwg" #Replace it with the actual drawing path
doc = acad.Documents.Open(filepath)

#Our example command is to draw a line from (0,0) to (5,5) command_str = '._line 0,0 5,5 ' #Notice that the last SPACE is equivalent to hiting ENTER #You should separate the command's arguments also with SPACE #Send the command to the drawing doc.SendCommand(command_str) #Execution Part if __name__ == '__main__': main()

___________
To communicate between AutoCAD and python using COM technology you have to know about the AutoCAD Object Model (ActiveX), refer to these documentation files within your AutoCAD's installation path.

  1. acadauto.chm: The official reference for AutoCAD's Object Model (ActiveX).
  2. acad_aad.chm: ActiveX Developer's Guide.