Python programming in autocad

Python programming in autocad

Oliver_C3D
Advocate Advocate
34,546 Views
5 Replies
Message 1 of 6

Python programming in autocad

Oliver_C3D
Advocate
Advocate
Can I write to the AutoCAD program or extension with Python?
If you can provide a tutorial or reference file in this case?
0 Likes
34,547 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

You can use the pyautocad library with the AutoCAD COM API or IronPython with the AutoCAD .NET API (and also the COM API).



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 6

serag.hassouna
Advocate
Advocate

In addition to what @_gile said, there's the comtypes package.
If you understand AutoCAD's object model, you can invoke almost every method and fetch almost every value.
However, the main downside of comtypes is that you need to supply all arguments to every method you invoke, even the optional ones need to be explicitly supplied.
________
To catch the feeling of how it is "simple in theory" to connect to AutoCAD with python you can see the included code within this question of mine about an issue.
Postcommand doesn't read AutoLisp expression [Python 2.7 & AutoCAD 2016 Windows]

Message 4 of 6

rantaliran
Community Visitor
Community Visitor
hello serag.hassouna ,

I wanted to ask you for more detailed instructions, on how can I receive specific measurement from AutoCAD (maybe student addition) to a python program:)))

thank you for your help!

Ran
0 Likes
Message 5 of 6

Chuong.Ho
Advocate
Advocate

You can use project cadpythonshell, An IronPython console for Autocad and Civil 3d API, include full snoop Database to explore CAD.

https://github.com/chuongmep/CadPythonShell

 

Chuong Ho

EESignature

Message 6 of 6

r_mazari
Community Visitor
Community Visitor

# Génération d'un fichier DWG avec les éléments de base du plan

from ezdxf import new

# Création d'un nouveau fichier DWG
doc = new("R2010")
msp = doc.modelspace()

# Définition des murs principaux (exemple simplifié)
murs = [
[(0, 0), (6.3, 0)], # Mur bas
[(6.3, 0), (6.3, 7.3)], # Mur droit
[(6.3, 7.3), (0, 7.3)], # Mur haut
[(0, 7.3), (0, 0)], # Mur gauche
]

# Ajout des murs au dessin
for mur in murs:
msp.add_line(mur[0], mur[1])

# Ajout de quelques ouvertures (portes et fenêtres simplifiées)
portes = [
[(3, 0), (3.9, 0)], # Porte terrasse
[(4.7, 3), (4.7, 3.9)], # Porte séjour
]

fenetres = [
[(0, 5), (0, 5.6)], # Fenêtre chambre
[(6.3, 1.2), (6.3, 1.8)], # Fenêtre séjour
]

# Ajout des portes et fenêtres au dessin
for porte in portes:
msp.add_line(porte[0], porte[1], dxfattribs={"color": 1}) # Rouge

for fenetre in fenetres:
msp.add_line(fenetre[0], fenetre[1], dxfattribs={"color": 5}) # Bleu

# Enregistrement du fichier
dwg_path = "/mnt/data/plan_maison.dwg"
doc.saveas(dwg_path)

dwg_path

0 Likes