Modify constrain value.

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
paddle
Veteran
Posts: 1392
Joined: Mon Feb 03, 2020 4:47 pm

Modify constrain value.

Post by paddle »

Say I want to modify a distance constrain value from my cpp function.
How should I do it?

I think it's probably something like

Code: Select all

       Gui::cmdAppObjectArgs(Obj, "setDriving(%i,%s)", ConStr.size() - 1, "False");
       Gui::Command::commitCommand();
Which sets a constrain as driving.

Is there a setValue(%i) ? I'd rather not poke blindly for the correct function name. And I can't find the answer by browsing the source code because those functions are sent as string.
Thanks

Beside if there's a way without Gui::Command it would be better because I don't want to create an action in the history of the user for this modification.

Edit: Does that do the job?

Code: Select all

                ConStr[ConStr.size() - 1]->setValue(sketchgui->toolSettings->widget->toolParameters[0]);
Edit2: It does work, the value of the constrain is changed, but it doesn't update the geometry. Not until you move something then the geometry jump to the correct length.
How to recompute ?
Edit3 :

Code: Select all

                ConStr[ConStr.size() - 1]->setValue(sketchgui->toolSettings->widget->toolParameters[0]);
                Obj->solve(true);
Does the job
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

Re: Modify constrain value.

Post by TheMarkster »

paddle wrote: Tue Jan 18, 2022 5:55 pm
Beside if there's a way without Gui::Command it would be better because I don't want to create an action in the history of the user for this modification.
You could do something similar to doCommand() with, for example:

Code: Select all

Base::Interpreter().runString("import FreeCAD")
and it won't appear in the python console.

I would prefer, I think, doing things in the Gui and watching the python console to get the python needed for doing this thing, such as changing a constraint value. Then do it using runString() or doCommand() rather than trying to do it all in c++ by accessing the sketch object directly. Keep in mind that when the user is recording a macro it needs to appear in the console for it to get into the macro.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Modify constrain value.

Post by openBrain »

I think you can just test by changing a constraint from the Property pane and see what happens in the Python console. ;)
Post Reply