How to attach a property to individual sketch lines?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
georges.duperon
Posts: 3
Joined: Sun Nov 20, 2016 12:57 am

How to attach a property to individual sketch lines?

Post by georges.duperon »

Hi all!

I'm creating 2D designs for paper constructions (e.g. pop-up books) using FreeCAD's Sketcher. My designs have two types of lines, cut lines and fold lines (both types are sent separately to a small paper CNC cutter). Currently, I'm exporting to SVG, and sorting out the cut vs. fold lines in Inkscape, but it is a bit tedious.

I'm looking for a way (in python) to attach a custom string, boolean or numeric property to individual sketch lines, which would allow me to automatically select the cut or fold lines when exporting.

I'm a programmer, but I'm new to FreeCAD scripting. Is it possible to add a custom property similar to the

Code: Select all

.Construction
one on sketcher geometry without deep changes to the sketcher code? Where should I start looking?

FreeCAD info:

Code: Select all

OS: Linux
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.16.Unknown
Build type: Release
Python version: 2.7.12
Qt version: 4.8.7
Coin version: 3.1.3
OCC version: 6.8.0.oce-0.17
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: How to attach a property to individual sketch lines?

Post by DeepSOIC »

Interesting question.
If you are ok with diving into C++, it is relatively easy to add another attribute to sketcher entities. However, this has to be done in Part module, which is somewhat annoying. So it's unlikely your attribute will be merged into FreeCAD, so you'll have to live with your own builds, which isn't great.

In python, you can make a custom feature based on sketcher, by basing on Sketcher::SketchObjectPython. This won't magically let you add attributes to geometry, and you'll have to hack around by creating a special property that tracks geometry, and some hack in viewprovider to highlight the lines in 3d view somehow...
georges.duperon
Posts: 3
Joined: Sun Nov 20, 2016 12:57 am

Re: How to attach a property to individual sketch lines?

Post by georges.duperon »

Thanks DeepSOIC! Indeed, I would rather avoid using custom builds for such a small change.

In time I might try to add names and/or a general user property (say, a dictionary of key/value properties) to Part in the C++ code, as a general-purpose mechanism could hopefully have better chances of being merged.

Inheriting from Sketcher::SketchObjectPython sounds like an interesting alternative, but tracking the association between a property and the corresponding geometry likely means a lot of trouble when deletions and undo come into play :-( .

I think I found a hack which would let me solve my problem in the short term: when flagging a segment as a fold line, I can add a single point to the geometry, and put a coincidence constraint on that new point and the segment's starting point. Since constraints can be named, I can easily give that constraint the name "Fold001", which can subsequently be recognised. That's just a temporary hack, though, as there wouldn't be any visual display of the property, and it is far from being a clean approach.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: How to attach a property to individual sketch lines?

Post by DeepSOIC »

georges.duperon wrote:In time I might try to add names and/or a general user property (say, a dictionary of key/value properties) to Part in the C++ code, as a general-purpose mechanism could hopefully have better chances of being merged.
I vote for name. Names for geometry in sketcher can be a starting point toward toponaming :ugeek: .
Post Reply