Macro FCBmpImport

Need help, or want to share a macro? Post here!
Post Reply
Posts: 2681
Joined: Thu Apr 05, 2018 1:53 am

Macro FCBmpImport

Post by TheMarkster »

I've updated this old macro to work with python3. There is an older version in the addon manager called BmpImport, hosted on the FreeCAD Macros repo on github. There is a PR pending to remove that one in favor of the one on the wiki. The older one only works on python2.

I wrote this a few years ago as a project to learn FreeCAD and to learn python. I've learned a few things since then and there are things I would do differently. The code is a complete mess, but it is what it is and provides as far as I know the only option for importing bmp images into FreeCAD as FreeCAD objects.

Importing as SVG is a far better option, which is why I hadn't bothered trying to figure out why the macro was failing with python3 before now. It took some sleuthing, but I figured out the issue. In python2 some numbers were being treated as ints where in python3 they are interpreted as floats, giving different results. The fix was to cast these explicitly as int().

Bmp images, by their nature are going to be pixelated, and the results are likely to be quite ugly in FreeCAD. Images with all vertical and horizontal edges would be ideal (no diagonals and no curves). High resolution images should be avoided as they will quickly produce too many objects in the tree and the risk there is running out of memory and crashing, so be sure to save anything important. Save early and save often. Even a relatively low resolution image of 120x120 has 14,400 pixels. Each pixel won't be a separate object, but worst case would be 7,200 objects in the tree. Ouch.
screenshot2.png (37.82 KiB) Viewed 595 times
Sketches produced are entirely unconstrained, but the endpoints are guaranteed (money back guarantee) to be coincident because they are placed exactly where needed. There is an algorithm for dealing with crossed lines and T intersections. I call them "cheats". A tiny distance is placed between the intersecting points. Consider this sketch imported using the macro. Will it extrude into a solid?
Snip macro screenshot-2e7328.png
Snip macro screenshot-2e7328.png (46.13 KiB) Viewed 595 times
Snip macro screenshot-e879a3.png
Snip macro screenshot-e879a3.png (62.35 KiB) Viewed 595 times
Zoom in closely to see the cheat trick.
Snip macro screenshot-6fc5c7.png
Snip macro screenshot-6fc5c7.png (14.37 KiB) Viewed 595 times
Full documentation can be found on github:
(131.83 KiB) Downloaded 16 times
My FreeCAD video series on youtube:
Post Reply