Parametric Curve FP discussion board

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Parametric Curve FP discussion board

Post by TheMarkster »

latest: 0.2023.05.06

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
Snip macro screenshot-6d573f.png
Snip macro screenshot-6d573f.png (47.03 KiB) Viewed 18723 times
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
evaluate("3*9") --> 27

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
Note: evaluate() does not strip the comments. This is done elsewhere in the code. If you need that you can call stripComments() directly on the feature python object via its proxy.

Code: Select all

fp = FreeCAD.ActiveDocument.getObject("ParametricCurve")
fp.Proxy.stripComments("{commented} 3 * 9 #also a comment, and all that follows")# -> ' 3 * 9 '
The way the formulas are evaluated is as follows:

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.
Last edited by TheMarkster on Sun May 07, 2023 4:29 pm, edited 22 times in total.
User avatar
Vincent B
Veteran
Posts: 4713
Joined: Sun Apr 05, 2015 9:02 am
Location: La Rochelle, France

Re: Parametric Curve FP Macro v2021.08.31.rev2

Post by Vincent B »

Hi,
I've just find out your macro. Nice Job.
Few mouth ago I've also tried to write my own macro.
I thought 5 parameters should be useful. And a "number of points" more easier to handle instead an interval.
Attachments
3DMathCurve.py
(5.67 KiB) Downloaded 144 times
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Parametric Curve FP Macro v2021.08.31.rev2

Post by TheMarkster »

Ah, you worked on this, too. Great minds think alike, as they say. Adding more parameters is a good idea. I might add more. "e" would be misinterpreted as Euler's constant by the parser I'm using, but I have some ideas.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Parametric Curve FP Macro v2021.08.31.rev2

Post by TheMarkster »

GlouGlou wrote: Sat Sep 11, 2021 4:25 pm Hi,
I've just find out your macro. Nice Job.
Few mouth ago I've also tried to write my own macro.
I thought 5 parameters should be useful. And a "number of points" more easier to handle instead an interval.
I have updated to now have unlimited parameters, as many as the user wants to have. This is done with a property of type App::PropertyStringList that holds the values for "d" as d1, d2, d3, etc.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Parametric Curve FP Macro v2021.08.31.rev2

Post by openBrain »

TheMarkster wrote: Thu Sep 16, 2021 1:51 am I have updated to now have unlimited parameters, as many as the user wants to have. This is done with a property of type App::PropertyStringList that holds the values for "d" as d1, d2, d3, etc.
yorik wrote: Ping
@Yorik, how about integrating this in Draft ?
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Parametric Curve FP discussion board

Post by TheMarkster »

I have incorporated now a default JSON file directly into the source code. If you have some interesting / useful formulas you would like to have included post them there.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Parametric Curve FP discussion board (latest: 0.2022.03.05.rev2)

Post by TheMarkster »

As of 0.2022.03.05.rev2 python style comments are now allowed in variable definitions.
Snip macro screenshot-39950f.png
Snip macro screenshot-39950f.png (100.77 KiB) Viewed 17287 times
Notice the comment for the b variable definition.

The way this works is the pound symbol (#) and all that follow are stripped out prior to sending the string to be evaluated.

Note: Spreadsheets cannot use the comments, so they are also stripped off prior to sending to spreadsheets, and thus if the spreadsheet is updated back into the macro the comments will be overwritten. Comments are okay in the JSON files, and will be saved to JSON file.
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Parametric Curve FP discussion board (latest: 0.2022.03.05.rev2)

Post by edwilliams16 »

It would be nice if expressions could read numeric values from the parameter list. They come over as strings, and there doesn't appear to be a string to number function available in expressions. I can see you wouldn't want people to use expressions to reference within the parameters (they should use the published mechanism), but it's useful to be able to reference them later in the model to make it more parametric. A work-around is to reference both the parameters and later expressions to a dynamic data object or a spreadsheet. Maybe it is better to encourage that.

Here's the circular braid, using dynamic data.
Screen Shot 2022-03-05 at 2.09.05 PM.png
Screen Shot 2022-03-05 at 2.09.05 PM.png (39.23 KiB) Viewed 17225 times

EDIT: I'm guessing the linked spreadsheet capability, that I've yet to experiment with anticipates the issue above...
Attachments
braid.FCStd
(478.02 KiB) Downloaded 111 times
Last edited by edwilliams16 on Sun Mar 06, 2022 12:47 am, edited 1 time in total.
User avatar
NewJoker
Veteran
Posts: 3014
Joined: Sun Oct 11, 2020 7:49 pm

Re: Parametric Curve FP discussion board (latest: 0.2022.03.05.rev2)

Post by NewJoker »

TheMarkster wrote: Sat Mar 05, 2022 9:23 pm
It looks great :D Parametric curves can be very useful. Do you plan to eventually turn this macro into a built-in functionality ? It would be awesome.
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Parametric Curve FP discussion board (latest: 0.2022.03.05.rev2)

Post by TheMarkster »

edwilliams16 wrote: Sun Mar 06, 2022 12:10 am It would be nice if expressions could read numeric values from the parameter list. They come over as strings, and there doesn't appear to be a string to number function available in expressions.
New version: 0.2022.03.05.rev3

Added F_??? float properties to serve as aliases for a, b, c, d, X, Y, and Z.

F_a = a evaluated to a floating point value if it can be evaluated as a constant, else 0.0.
F_b, F_c, F_d, F_X, F_Y, F_Z are all similar, except F_d.

F_d is a list of floats, 1-indexed (not 0-indexed). So, F_d[1] = d1, evaluated as a float if it can be.

Note: if for example b = a, the F_b = 0.0 because a cannot be evaluated as a constant no matter even if a = 5.

a = 5 (okay, can be evaluated as a constant)
a = pi * 5 (okay, can be evaluated as a constant)
a = t * 3 (not okay, F_a = 0.0)

It's kind of a dumb evaluation. Think of it as a compile time evaluation, even though it's not. These values are readonly since they would be overwritten during each recompute anyway. Should they also be hidden is a question? They add clutter to the property list, but this way users know they exist.

Another change: when changing to a new formula if an existing variable is set with an expression, then the expression overrides the new value held in the new formula. My solution is rather radical: all expressions are cleared when changing formulas, and a warning is sent to the report view. Use expressions if you need/want to, but just be advised if you change formulas via the Formulas enumeration property, then your expressions will be zapped, permanently. The expectation should be in all cases anyway that all variables: a, b, c, d, X, Y, Z, t, t_max, interval, are getting reset when changing formulas.
Post Reply