14

I have a file with extension .dwg (AutoCAD), and I want to call that file from a Python console and present it on the web. Is there a module for the .dwg extension or some other solution?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ballon
  • 6,882
  • 18
  • 49
  • 63

4 Answers4

8

GNU LibreDWG (GitHub mirror) is the canonical answer in late 2021. Hilariously forked from the now defunct LibDWG project documented in ...wait for it Esperanto, LibreDWG:

  • Optionally installs Python 3 bindings (enabled by default).
  • Optionally installs DWG→DXF converters (enabled by default).
  • Is unsurprisingly licensed under the GPL 3.0 (fine for web backends; less fine everywhere else).
  • Supports almost the entirety of R2010+.

Seriously, it's hot. LibreDWG is the only sane choice that still matters. Let's embrace sanity, everybody.

Cecil Curry
  • 9,789
  • 5
  • 38
  • 52
  • I saw this library a while back and was hoping to use it just to read the dwg content, unfortunately I am unable to understand the installation process of this library since I am inexperienced. Is there any resource you can point to get my wheels rolling? – Juan Carlos Asuncion Mar 31 '22 at 01:57
6

The best format for displaying these online would (imo) definitely be SVG. Recent browsers support SVG rendering natively; older ones (think IE6) may require an SVG plugin

So your best bet is probably using a command line convert tool like cad2svg (this is a free linux command line tool) which converts the DWG files to SVG. You can easily do this that from your Python program (using subprocess).

ChristopheD
  • 112,638
  • 29
  • 165
  • 179
2

It's not easy to get data just from .dwg file, but much more easier from a .dxf file. So I choose to convert a .dwg file to a .dxf file, and just handle the .dxf file. This is not fast, but it is also an alternative, since there is no other easy way to handle .dwg files.

The converter is at https://www.opendesign.com/guestfiles/TeighaFileConverter.

My OS is CentOS 6.5 (GCC 4.4.7), so I choose Teigha File Converter for Linux 64-bit (RPM).

#Install some qt5 lib
yum install -y qt5*

# If your libstdc++.so.6 has GLIBCXX>=15, you can pass the following three steps (using strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX to find)
mv libstdc++.so.6.0.20 /usr/lib64/libstdc++.so.6.0.20
mv /usr/lib64/libstdc.so.6 /usr/lib64/libstdc.so.6.bk
ln /usr/lib64/libstdc++.so.6.0.20 /usr/lib64/libstdc.so.6

# Install TeighaFileConverter
rpm -i --nodeps TeighaFileConverter_QT5_lnxX64_4.7dll.rpm

If you want to use the graphical interface, you need to install Qt 5, or you can just use it in terminal or use it as shell commands in your program.

TeighaFileConverter 'input_folder' 'output_folder' "output_version" "output_type" "recurse_folder" "audit" -platform offscreen
# 'input_folder' can't be same with output_folder
# For example, convert dwg to dxf
TeighaFileConverter ./ ./dxf "ACAD10" "DXF" "0" "0" -platform offscreen
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kardos
  • 29
  • 1
0

Maybe there will be something you can use from this Summer Of Code project

http://groups.fsf.org/wiki/LibreDWG/SummerOfCode

http://groups.fsf.org/wiki/LibreDWG

John La Rooy
  • 295,403
  • 53
  • 369
  • 502