Summerizing dwg files
I recently had a paid project that required working with ".dwg" files. Which is a file format I never heard about before this project. So I though I would explain what did I do and how.
Requested result
The expected product was a windows application that would view .dwg files and summerize them. The example I was shown was an image of a 2d list of signs, where the goal was for the program to return "in this file there's X blue signs and Y red signs". At first, this didn't seem too complicated. But the more I interacted with AutoCAD, the more I learned how complicated this is.
what is dwg, dxf, and what is AutoCAD?
"DWG is a proprietary binary file format used for storing two- and three- dimensional design data and metadata." - Wikipedia
Basically, the ".dwg" format is used for creating 2d and 3d data files as part of their application, AutoCAD. At first I thought this format was similar to blender's format (.blend). But it's quite different. Unlike blender and ".blend", AutoCAD belongs to the company autodesk. As such, the ".dwg" format is proprietary, since it belongs to them, and handling it is far more complicated.
Luckily, there's ".dxf", a file format meant to bridge between the proprietary ".dwg" format and external applications. However, the ".dxf" format doesn't have any good documentation as is still quite difficult to work with.
part 1: dwg to dxf
How do I switch from dwg to dxf? at first it seems easy, just use a library. But there's an issue... autodesk don't explain how to convert dwg to dxf, and so the only way to convert dwg to dxf is using autoCAD.
There's three ways to do this:
- Changing file formats manually, using autoCAD
- automating the desk via win32COM, which allows interacting with the autoCAD application via windows.
- using an API
At first I tried making a win32COM script using python. But after struggling for an hour to even make autoCAD open the dwg file, I realized it might not be a good idea to waste time on this.
I then moved on to using an API. I made this rust package, which suprisingally worked. However, it uses the cloudconvert API, which has a limit of 10 conversions per day (for free tier).
So until nearly the end of the project, I turned dwg files to dxf manually using autoCAD.
part 2: displaying a dxf file
Simple, right? now that we have a representation of the dwg file, what's the issue in parsing it and displaying it? during my python tests this was very easy, I managed to make a function that does this in under 20 lines of code.
But then I moved over to rust, where there was no package that does this... I ended up making this package as well In order to handle basic conversion, but there was no way I could support all 30+ internal structured of dxf.
So I also used my cloudconvert crate in order to turn my dxf file into a svg, which worked fine, but it mean I couldn't view spesific parts of the dxf file, only display the entire file.
part 3: simplifying the file using AI
I used to think programing variable names were awful, but the variable named in the files I received were even worse. I still have no idea what does layer "NY-MIKRA" mean, and yet somehow I'm supposed to summerize it using AI?
Using a very long prompt, I managed to use gemini and make the names make a little bit more sense. I also used it to give a title to every dxf file and to give a short text explanation of the file.
part 4: packging it all together
I used tauri to create UI for selecting a dwg file, converted it to a dxf file and an svg file using cloudconvert, And then gave the parsed dxf file to gemini in order to get names for every file.
I then made a main page the shows the image, the layers and the title/explanation and built a final file output.