elmasea Posted June 1, 2010 Posted June 1, 2010 Hi I am seeking “partners” on studing python for autocad I am a mechanical designer, an (italian) autocad user. Until a few years ago I programmed in lisp, but then I have used VBA. Now I'm learning python, as you know, powerful and easy to use Of course I use win32com class So I did the first routine about Python/Autocad that I try to explain briefly: 1) Reading all the characteristics of a selected entity: acad = win32com.client.Dispatch("AutoCAD.Application") doc = acad.ActiveDocument # Document object ms = doc.ModelSpace # Modelspace "collection" returnObj = doc.Utility.GetEntity() # "Select an object") then I can display the properties of the selected entity eg for a polyline: returnObj[0].Coordinates ... or other Layer, Length, Linetype, LinetypeScale, Lineweight But this is true only for a selected entity (GetEntity method), 2) Reading all the characteristics of a indexed entity: When I get an entity at a given index ms = doc.ModelSpace i = 5 # index of the entity in the drawing (Modelspace) entObj = ms.Item(i) I can display only few properties for example, always for a polyline : entObj.Layer entObj.ObjectName but for other properties python tells me the object has no attribute 'for example Length, Coordinates, Instead VBA does that who wants to help me? who wants to partecipate? elmasea Quote
Lee Mac Posted June 1, 2010 Posted June 1, 2010 You might find more interest in Python here... Enjoy, Lee Quote
elmasea Posted July 18, 2010 Author Posted July 18, 2010 """Sample to automate AutoCAD: 1) run an autocad drawing 2) read some drawing data: number of objects in the ModelSpace; the value of an AutoCAD system variable; 2) add to the drawing: point; line; polyline. In this code some examples of access to Autocad objects are presented. We must handle variants used to pass array data in and out of AutoCAD COM. Different variant types are possible using comtypes (this is not true with win32com.client package). This example should be improved especially in the user interface. """ import array import comtypes.client #Get running instance of the AutoCAD application acad = comtypes.client.GetActiveObject("AutoCAD.Application") # Document object doc = acad.ActiveDocument # Get the ModelSpace object ms = doc.ModelSpace # Get some useful data DwgName = doc.Name # File name count = ms.Count # Number of items in modelspace # Get a variable name sysVarName = 'DWGPREFIX' # directory of the drawing varData = doc.GetVariable(sysVarName) # view in Idle print 'File: ', DwgName print 'N. Items: ', count # >>>> In ModelSpace: # Add a POINT object # Sets the value of an AutoCAD system variable. # PDMODE and PDSIZE system variables control the appearance of point objects sysVarName = "PDMODE" # specifies a Point shape doc.SetVariable(sysVarName,3) # code 3 for X shape sysVarName = "PDSIZE" # controls the size of the point doc.SetVariable(sysVarName,3) # absolute size (3 units half height of icon point) pt = array.array('d', [0,0,0]) # to convert in variant point = ms.AddPoint(pt) # Add a LINE pt1 = array.array('d', [0.0,0.0,0]) # start point pt2 = array.array('d', [20.0,20.0,0]) # end point line = ms.AddLine(pt1, pt2) # Add a POLYLINE # We assign the vertices ptl = [] pt = [20.0,20.0,0] ptl = ptl + pt pt = [40.0,30.0,0] ptl = ptl + pt pt = [70.0,40.0,0] ptl = ptl + pt ptlst = array.array('d', ptl) pline = ms.AddPolyline(ptlst) # Select an item # The user selects an object (the polyline) by picking a point on the screen returnObj = doc.Utility.GetEntity("Select Object:") """Each entity in autocad has a unique value. Ex. for a line object the ObjectName is AcDbLine and the EntityType value is 19 """ # common code for line, polyline print 'DXF entity name: ', returnObj[0].EntityName # it is the class name of the object print returnObj[0].EntityType # the type of entity print returnObj[0].Layer print returnObj[0].Length # only polyline retCoord = returnObj[0].Coordinates num_vertex = len(retCoord)/3 for i in range(num_vertex): print returnObj[0].Coordinate(i) print ">>>>>>>>>>" Quote
lode Posted October 4, 2010 Posted October 4, 2010 Yes, I am also playing with the idea to move from vba. And was wondering if everything one does with vba is possible with python. Is it reliable? Is it depending on someone and some updates? Python is fun, so, I am in! Lode Quote
Jeff H Posted October 13, 2010 Posted October 13, 2010 If you like python you could also consider F#. Python is great but if you are mainly focusing on AutoCAD customization, you probably would benefit more using F# Quote
Hioushi Posted November 6, 2010 Posted November 6, 2010 Hello, Does anyone know a workaround to the problem the OP commented? When I get an entity at a given index ms = doc.ModelSpace i = 5 # index of the entity in the drawing (Modelspace) entObj = ms.Item(i) I can display only few properties for example, always for a polyline : entObj.Layer entObj.ObjectName but for other properties python tells me the object has no attribute 'for example Length, Coordinates, Instead VBA does that I have the same problem. Quote
a_lobo Posted December 11, 2015 Posted December 11, 2015 hello, I try to execute this code : pt = array.array('d', [0.0,0.0,0.0]) pt2 = array.array('d', [0.0,100.0,0.0]) linea =ms.AddLine(pt,pt2)and I have the follow error linea =ms.AddLine(pt,pt2) File "C:\IAcadModelSpace.py", line 281, in AddLine ret = self._oleobj_.InvokeTypes(1581, LCID, 1, (9, 0), ((12, 1), (12, 1)),StartPoint, EndPoint) pywintypes.com_error: (-2147352567, 'Ocurri\xf3 una excepci\xf3n.', (0, None, None, None, 0, -2147024809), None) Could you help me? thanks, Quote
pravu87 Posted December 22, 2015 Posted December 22, 2015 Excellent post! Could you help me with a small problem? I have to MOVE AND ROTATE the coordinates of a certain polyline and I would like to use Python to do that. How would the command structure look like? Thanks in Advance! Quote
tgibbo Posted March 31, 2020 Posted March 31, 2020 I know your post was many years ago. Are you still using Python? Quote
Danielm103 Posted June 30, 2023 Posted June 30, 2023 On 6/28/2023 at 7:02 PM, syevale111 said: I am using Pyhon with CAD? Quote
Danielm103 Posted February 3 Posted February 3 PyRx (Python for AutoCAD) wrappers on GitHub are starting to come together if anyone wants to play https://github.com/CEXT-Dan/PyRx , All open source There’s full ActiveX wrappers, and lots of the ARX wrappers are done, just posted version 1.1.157 Quote
asorg Posted July 29 Posted July 29 On 03/02/2024 at 14:44, Danielm103 said: PyRx (Python for AutoCAD) wrappers on GitHub are starting to come together if anyone wants to play https://github.com/CEXT-Dan/PyRx , All open source There’s full ActiveX wrappers, and lots of the ARX wrappers are done, just posted version 1.1.157 There is something about Autocad Plant 3D? Quote
Danielm103 Posted July 29 Posted July 29 Vanilla AutoCAD, however, if AutoCAD Plant 3D has a base of AutoCAD, it may load. There may be something useful, not familiar with that product. Quote
SLW210 Posted July 29 Posted July 29 I no longer have Plant 3D loaded, maybe I'll check into getting it again. This is pretty old now, might be something newer. How to script components for AutoCAD Plant 3D (autodesk.com) A little newer. How can I build a own Library of custom Parts written as python scripts? - Autodesk Community - AutoCAD Plant 3D Even more recent and even more if you check out the related videos. AutoCAD Plant 3D Tutorial: Python Integration in AutoCAD Plant 3D. A Step-by-Step Guide (youtube.com) 1 Quote
Danielm103 Posted July 29 Posted July 29 Interesting! “In Plant 3D, AutoCAD and Python work together by embedding the Python interpreter in our application and providing a few C++/Python bindings as a Python extension module” This is exactly what I’m doing, C++/Python bindings around ARX, with extra flavoring to make it more Pythonic. I doubt what I’m working on would be useful in a Plant 3d context. My goal is more general purpose, expose tools like pandas, SciPy to AutoCAD Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.