Using Python to count the number of layers in a AutoCAD file

1.6k views Asked by At

I've created a GUI using PyQt5 wich takes shapefiles and one .dwg file as inputs. In the .dwg file, I have a certain amount of standard layers that should be inside. If one or more of those layers is missing, I would like to be able to inform the user which layers are missing.

For example, if the .dwg files must have these layers ('Trees', Roads', 'Houses', 'Lakes' etc.), if 'Roads' is missing, I would like the user to be inform when he launches the GUI.

Using Python I have to use a Python extension/library I guess. But I've looked and PyAcad or PythonCAD don't seem to be very implemented yet. If anyone has used Python for AutoCAD, I am looking for some advice/tips. And if you have an alternative idea for my problem that would be greatly appreciated !

1

There are 1 answers

1
Augusto Goncalves On

There is a complication here: if you have a .dwg you cannot convert it to .dxf without AutoCAD installed on the machine.

If you have AutoCAD installed, then use it's APIs to open and manipulate the drawing. This can be done with .NET, C++, JavaScript or any language compatible with COM (including Python).

If you DON'T have AutoCAD installed, there is a cloud API for it: AutoCAD I/O. This is basically a running instance on the cloud that you can use for processing .dwg drawings. That way you can upload a .dwg with a script to list layers. Below is the sequence of commands to list layers:

-layer
?
*

Which should return a list of layers. You can have more with .NET code running on this cloud instance. See more at http://developer.autodesk.com

Let me know what you have (scenario and goal), then I can advise better.