Thanks to Ed for the latest update. He has added the copysign() function and also documented previously undocumented sgn() function.
installation: install from addon manager to ensure you are getting the latest version.
wiki page:
https://wiki.freecadweb.org/Macro_Parametric_Curve_FP
full documentation on github:
https://github.com/mwganson/Parametric_Curve_FP
This is based on the macro 3d parametric curve, by Gomez Lucio, Modified by Laurent Despeyroux on 9th feb 2015.
https://wiki.freecadweb.org/Macro_3D_Parametric_Curve
The differences are:
1) creates a feature python object
2) adds spreadsheet integration
3) adds JSON integration, double click object in the tree/combo view to open the formula editor
4) uses pyparsing to parse rather than insecure eval()
5) allows comments in formulas: python style (all that follows # is ignored) and {braced style} all within braces ignored
6) adds additional variables in the form of d1, d2, d3, d4, etc., up to as many as you like.
7) Supports shape type output as bspline, polygon, or points
Full documentation at the github link above. Run the Parametric_Curve_FP.FCMacro file as a macro. The Parametric_Curve_FP.py file the macro file creates should not be run directly. It is imported by FreeCAD when loading a document containing one of the ParametricCurve objects.
With JSON you can have more than one formula in a single JSON file. You can have as many as you like. When the file is connected it is read in and an enumeration property Formulas is populated with all the formulas. In the formula editor it is easy to manage formulas across JSON files. JSON files are just text files containing string representation of a python dictionary of dictionaries stored in JSON format. The top level dictionary is keyed to the formula names. Each formula name key links to another dictionary, which contains as keys all the variable names, such as a, b, c, d1, d2, X, Y, Z, t_min, t_max, and interval.
If you would like to use the parser from python you can do so.
Code: Select all
from Parametric_Curve_FP import evaluate
add your own constants in a dictionary passed to evaluate as a parameter:
Code: Select all
my_dict = {"a":32,"b":16}
evaluate("a+b", my_dict)# -> 48
Code: Select all
fp = FreeCAD.ActiveDocument.getObject("ParametricCurve")
fp.Proxy.stripComments("{commented} 3 * 9 #also a comment, and all that follows")# -> ' 3 * 9 '
We know t because it is a constant float type property, so we set the "t" key in the dictionary, and then call a = evaluate(a, dict) with a being the string formula that can only refer to t and other constants. Then, now that we know "a" we set it in the dictionary and pass the dictionary to evaluate in order to get b. b = evaluate(b, {"t":t, "a":a}) or something to that effect. We do the same all the way to Z.