updating a sketch's constraints from python
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
-
- Posts: 31
- Joined: Tue May 24, 2016 7:42 pm
updating a sketch's constraints from python
Hi,
I am thinking of making a 2DOF cable robot. Something like a polar graph robot (such that draws on wall), but using 4 cables insteads of 2.
I think I have the mathematical model right. It shouldn't be too hard to make a motor algorithm to drive it.
However, before I actually start designing parts, cutting/milling/printing/ordering/etc. I would like to check that my model works.
In essence, I could just make a sketch in freecad of the geometry. Then I want to create a script that update some of the sketch distance and angular constraints in a loop while measuring some other distance/angle and check that they do match my expected values.
=> observe the "animated" sketch and hope that it works as I intend it to work
Reading the python manual of freecad, I see stuff about UI, about parts, about meshes and draft workbenches, but i do not see anything about sketches.
How can I modify a sketch dimension constraint from python, recompute the sketch model, and measure a given distance of a segment in that sketch from a python script?
If I have to, i am fine creating the sketch entirely from python.
I just found this python sniuppet related to sketch => https://www.freecadweb.org/wiki/Macro_Shake_Sketch
but this is no example of constraint values manipulation (unless i'm mistaken)
thanks
I am thinking of making a 2DOF cable robot. Something like a polar graph robot (such that draws on wall), but using 4 cables insteads of 2.
I think I have the mathematical model right. It shouldn't be too hard to make a motor algorithm to drive it.
However, before I actually start designing parts, cutting/milling/printing/ordering/etc. I would like to check that my model works.
In essence, I could just make a sketch in freecad of the geometry. Then I want to create a script that update some of the sketch distance and angular constraints in a loop while measuring some other distance/angle and check that they do match my expected values.
=> observe the "animated" sketch and hope that it works as I intend it to work
Reading the python manual of freecad, I see stuff about UI, about parts, about meshes and draft workbenches, but i do not see anything about sketches.
How can I modify a sketch dimension constraint from python, recompute the sketch model, and measure a given distance of a segment in that sketch from a python script?
If I have to, i am fine creating the sketch entirely from python.
I just found this python sniuppet related to sketch => https://www.freecadweb.org/wiki/Macro_Shake_Sketch
but this is no example of constraint values manipulation (unless i'm mistaken)
thanks
Re: updating a sketch's constraints from python
Show the Python console and change the value of a consraint. Now you can see in the console the corresponding python code ,e.g something like
If you want to measure something else you can use a so called driven constraint (shown in blue), and read its value.
Code: Select all
App.ActiveDocument.Sketch.setDatum(9,App.Units.Quantity('25.000000 mm'))
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
-
- Posts: 31
- Joined: Tue May 24, 2016 7:42 pm
Re: updating a sketch's constraints from python
great! i found it.
you can find the id of a given segment, by turing it blue or not, and then appears the "App.ActiveDocument.Sketch.toggleConstruction(10)"
can you please elaborate on
what is the command to issue exactly?
I see many items/objects/functions in App.ActivreDocument.Sketch but nothing that contains "distance" or "length"
you can find the id of a given segment, by turing it blue or not, and then appears the "App.ActiveDocument.Sketch.toggleConstruction(10)"
can you please elaborate on
?you can use a so called driven constraint (shown in blue), and read its value.
what is the command to issue exactly?
I see many items/objects/functions in App.ActivreDocument.Sketch but nothing that contains "distance" or "length"
Code: Select all
>>> dir(App.ActiveDocument.Sketch)
['Attacher',
'AttacherType',
'AttachmentOffset',
'AxisCount',
'ConstraintCount',
'Constraints',
'Content',
'DeleteUnusedInternalGeometry',
'Document',
'ExposeInternalGeometry',
'ExpressionEngine',
'ExternalGeometry',
'Geometry',
'GeometryCount',
'InList',
'InListRecursive',
'Label',
'MapMode',
'MapPathParameter',
'MapReversed',
'MemSize',
'MissingLineEqualityConstraints',
'MissingPointOnPointConstraints',
'MissingRadiusConstraints',
'MissingVerticalHorizontalConstraints',
'Module',
'Name',
'OpenVertices',
'OutList',
'OutListRecursive',
'Placement',
'PropertiesList',
'Shape',
'State',
'Support',
'TypeId',
'ViewObject',
'__class__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__le__',
'__lt__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'addConstraint',
'addCopy',
'addExtension',
'addExternal',
'addGeometry',
'addMove',
'addProperty',
'addRectangularArray',
'addSymmetric',
'analyseMissingPointOnPointCoincident',
'autoRemoveRedundants',
'autoconstraint',
'calculateAngleViaPoint',
'calculateConstraintError',
'carbonCopy',
'changeAttacherType',
'changeConstraintsLocking',
'convertToNURBS',
'delConstraint',
'delConstraintOnPoint',
'delExternal',
'delGeometry',
'deleteAllConstraints',
'deleteAllGeometry',
'deleteUnusedInternalGeometry',
'detectMissingEqualityConstraints',
'detectMissingPointOnPointConstraints',
'detectMissingVerticalHorizontalConstraints',
'dumpContent',
'dumpPropertyContent',
'enforceRecompute',
'exposeInternalGeometry',
'extend',
'fillet',
'getAllDerivedFrom',
'getAxis',
'getDatum',
'getDocumentationOfProperty',
'getDriving',
'getEditorMode',
'getGeometryWithDependentParameters',
'getGlobalPlacement',
'getGroupOfProperty',
'getParentGeoFeatureGroup',
'getParentGroup',
'getPaths',
'getPathsByOutList',
'getPoint',
'getPropertyByName',
'getPropertyNameOfGeometry',
'getTypeIdOfProperty',
'getTypeOfProperty',
'getVirtualSpace',
'hasExtension',
'increaseBSplineDegree',
'isDerivedFrom',
'isPointOnCurve',
'makeMissingEquality',
'makeMissingPointOnPointCoincident',
'makeMissingVerticalHorizontal',
'modifyBSplineKnotMultiplicity',
'moveDatumsToEnd',
'movePoint',
'positionBySupport',
'purgeTouched',
'recompute',
'removeProperty',
'renameConstraint',
'restoreContent',
'restorePropertyContent',
'setConstruction',
'setDatum',
'setDatumsDriving',
'setDriving',
'setEditorMode',
'setExpression',
'setVirtualSpace',
'solve',
'supportedProperties',
'toggleConstruction',
'toggleDriving',
'toggleVirtualSpace',
'touch',
'trim']
-
- Posts: 31
- Joined: Tue May 24, 2016 7:42 pm
Re: updating a sketch's constraints from python
forget it. i think I found it
>>> App.ActiveDocument.Sketch.getDatum(10)
80 mm
i'll be playing with this and report if i have further questions
>>> App.ActiveDocument.Sketch.getDatum(10)
80 mm
i'll be playing with this and report if i have further questions
-
- Posts: 31
- Joined: Tue May 24, 2016 7:42 pm
Re: updating a sketch's constraints from python
After a bit of struggle (first script in freecad ), i managed to do what I wanted.
=> calculate A, B and C constraints, then recompute the model.
here's a code snippet
However, the animation kinda flickers. It seems to me Freecad does
update A
recompute
update B
recompute
update C
recompute
a video of the behavior => https://drive.google.com/open?id=1i4BI- ... L5sRoe2cXt
How can I get rid of the intermediary "recompute" (force freecad to update the model only when i say so, instead of anytime it think there needs a refresh?)
my Freecad environment:
=> calculate A, B and C constraints, then recompute the model.
here's a code snippet
Code: Select all
def update_model(x, y):
(a, b, c, d) = get_abcd(x, y, w, h, w0, h0)
print(a, b, c, d)
# a
App.ActiveDocument.Sketch.setDatum(a_constraint_id,App.Units.Quantity(str(a)+' mm'))
# b
App.ActiveDocument.Sketch.setDatum(b_constraint_id,App.Units.Quantity(str(b)+' mm'))
# c
App.ActiveDocument.Sketch.setDatum(c_constraint_id,App.Units.Quantity(str(c)+' mm'))
# recompute
App.ActiveDocument.recompute()
update A
recompute
update B
recompute
update C
recompute
a video of the behavior => https://drive.google.com/open?id=1i4BI- ... L5sRoe2cXt
How can I get rid of the intermediary "recompute" (force freecad to update the model only when i say so, instead of anytime it think there needs a refresh?)
my Freecad environment:
Code: Select all
OS: Windows 7
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.16110 (Git)
Build type: Release
Branch: (HEAD detached at upstream/releases/FreeCAD-0-18)
Hash: f7dccfaa909e5b9da26bf50c4a22ccca9bb10c40
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: French/France (fr_FR)
Re: updating a sketch's constraints from python
Code: Select all
App.ActiveDocument.RecomputesFrozen = True
Think about unfreezing at the end of your macro.
-
- Posts: 31
- Joined: Tue May 24, 2016 7:42 pm
Re: updating a sketch's constraints from python
it goes faster. but i still observe that the sketch tries to update itself before my call to App.ActiveDocument.recompute()
one way to observe it is to give A a distance that is too high before i update constraints B and C => i then receive ValueError: Invalid constraint index: 25
is it a known limitation of am i missing something?
one way to observe it is to give A a distance that is too high before i update constraints B and C => i then receive ValueError: Invalid constraint index: 25
is it a known limitation of am i missing something?
Re: updating a sketch's constraints from python
I guess that the Sketch runs its .solve() routine, which generates this error.
I don't know if it's possible to freeze it also.
I don't know if it's possible to freeze it also.
Re: updating a sketch's constraints from python
Hello everyone,
Sorry to revive this old thread, but I think it might still be relevant as I am facing the exact same problem as fourchette. I am working with hollow bodies and i am getting when my python script modifies the sketch. I can build the geometry "by hand" without having this issue. My guess is that since "setDatum" call solve each time a constraint is modified , it causes reconstruction problems.
I tried to simplify as much as possible my sketch, changed the order of application of each of my setDatum but it didn't solve the problem( I can't share my model due to privacy issue, but I will try to build a minimal working example ) .
Since this thread was posted, is there a way to postpone the solver until every constraint have been modified ?
Sorry to revive this old thread, but I think it might still be relevant as I am facing the exact same problem as fourchette. I am working with hollow bodies and i am getting
Code: Select all
ValueError: Invalid constraint index
I tried to simplify as much as possible my sketch, changed the order of application of each of my setDatum but it didn't solve the problem( I can't share my model due to privacy issue, but I will try to build a minimal working example ) .
Since this thread was posted, is there a way to postpone the solver until every constraint have been modified ?
Code: Select all
OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.2.29177 +426 (Git)
Build type: Release
Branch: (HEAD detached from 0.20.2)
Hash: 930dd9a76203a3260b1e6256c70c1c3cad8c5cb8
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.3
Locale: French/France (fr_FR)
Re: updating a sketch's constraints from python
Probably this is the right way.
Part Design and Sketcher are "meant to be scripted" on some extend, not in every of their aspects.
They are written in C++ and if developer has not made some "Python Wrapper" you have some problems to access things from Python, and from what I know stopping the Solver is not possible.
Many thinks however are doable, some of them I have done even if they are not apparently feasible, see as example:
https://github.com/onekk/freecad-doc/bl ... tut-cv1.py
For some "free hints" on how some things could be done.
I hope this may help you to some extent.
Regards
Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/