"Length constraint in a sketch for a line with a value obtained using an alias in a spreadsheet"

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
2cv001
Posts: 484
Joined: Wed Jan 01, 2020 9:30 am

"Length constraint in a sketch for a line with a value obtained using an alias in a spreadsheet"

Post by 2cv001 »

Hello,
I know the formula for adding a length constraint for a line, but I can only do it by putting a number (or number + unit) in the parameter corresponding to the length to be imposed.
This code is ok with my squetch :

Code: Select all

App.getDocument('test').getObject('Sketch').addConstraint(Sketcher.Constraint('DistanceY',1,1,1,2,App.Units.Quantity("123,00 mm"))) 
I yould like to replace this number (here ("123,00 mm") with a formula as it is entered in the dialog box and allows, for example, to reference the alias of a cell in a spreadsheet to retrieve a value."
I tried

Code: Select all

App.getDocument('test').getObject('Sketch').addConstraint(Sketcher.Constraint('DistanceY',1,1,1,2,App.Units.Quantity("Spreadsheet.Alias"))) 
I tried also

Code: Select all

App.getDocument('test').getObject('Sketch').addConstraint(Sketcher.Constraint('DistanceY',1,1,1,2,App.Units.Quantity("=Spreadsheet.Alias"))) 
So how can we use such formula : 'Spreadsheet.Alias" ?

error message : ValueError: syntax error
OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 2022.1128.26244 +5318 (Git)
Build type: Release
Branch: LinkDaily
Hash: d45d221edcc7a757eb4e4eb638da0db5ed2759aa
Python version: 3.8.10
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.6.2
Locale: French/France (fr_FR)
Macro Sketch Constraint From Spreadsheet :
https://wiki.freecad.org/Macro_Sketch_C ... adsheet/fr
edwilliams16
Veteran
Posts: 3108
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: "Length constraint in a sketch for a line with a value obtained using an alias in a spreadsheet"

Post by edwilliams16 »

Code: Select all

App.getDocument('test').getObject('Sketch').addConstraint(Sketcher.Constraint('DistanceY',1,1,1,2,Spreadsheet.Alias))) 
assuming you put 123 mm in the cell.
User avatar
2cv001
Posts: 484
Joined: Wed Jan 01, 2020 9:30 am

Re: "Length constraint in a sketch for a line with a value obtained using an alias in a spreadsheet"

Post by 2cv001 »

IThanks !
tried your code but I have an error :

So,
My spreadsheet :
Capture2.PNG
Capture2.PNG (4.97 KiB) Viewed 541 times
Capture0.PNG
Capture0.PNG (8.23 KiB) Viewed 549 times
Capture.PNG
Capture.PNG (4.46 KiB) Viewed 549 times




When I add a contraint directly like this
Capture1.PNG
Capture1.PNG (17.61 KiB) Viewed 549 times
It is ok with Spreadsheet.Alias

When I try your code

Code: Select all

App.getDocument('test').getObject('Sketch').addConstraint(Sketcher.Constraint('DistanceY',1,1,1,2,Spreadsheet.Alias))) 
I have this error :
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'Spreadsheet' is not defined
I try with a number

Code: Select all

App.getDocument('test').getObject('Sketch').addConstraint(Sketcher.Constraint('DistanceY',1,1,1,2,12))
and it is ok so the beginning of the code is ok
Macro Sketch Constraint From Spreadsheet :
https://wiki.freecad.org/Macro_Sketch_C ... adsheet/fr
edwilliams16
Veteran
Posts: 3108
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: "Length constraint in a sketch for a line with a value obtained using an alias in a spreadsheet"

Post by edwilliams16 »

I would have been less prone to have made a mistake if you had provided an appropriate FCStd file. All the indexes and names change when I try to make a test file.

Use

Code: Select all

App.ActiveDocument.getObject('Spreadsheet').Alias)
in place of the length.
It already returns the length with units.
User avatar
2cv001
Posts: 484
Joined: Wed Jan 01, 2020 9:30 am

Re: "Length constraint in a sketch for a line with a value obtained using an alias in a spreadsheet"

Post by 2cv001 »

Your code is ok, but it's not exacly what I yould like. Sorry, I wasn't clear enough.

I would like the code to generate a constraint where the length is not just a number but a formula that fetches the value from an array.
I want that once this constraint is created, in its parameter corresponding to the length, there is a formula like "Spreadsheet.Alias" and not just a number "123" for a length of 123
Thus, if we change the value in the array, the length of the line will change automatically.
Once the constraint is created by the formula, it will be exactly the same as if you had entered the constraint by hand in this way:
Capture.PNG
Capture.PNG (42.81 KiB) Viewed 409 times

Here is afile test
test.FCStd
(6.12 KiB) Downloaded 13 times
Made with

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.21.0.31340 (Git)
Build type: Release
Branch: master
Hash: 856af2a12287a2d7c0d5ab23646be43af26b14b1
Python 3.10.8, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.3
Locale: French/France (fr_FR)
Installed mods: 
  * Assembly3 0.11.4
  * Curves 0.6.5
  * fasteners 0.4.26
  * Help 1.0.3
  * ThreadProfile 1.84.0
Macro Sketch Constraint From Spreadsheet :
https://wiki.freecad.org/Macro_Sketch_C ... adsheet/fr
User avatar
2cv001
Posts: 484
Joined: Wed Jan 01, 2020 9:30 am

Re: "Length constraint in a sketch for a line with a value obtained using an alias in a spreadsheet"

Post by 2cv001 »

I succed
first to create the constraint with your code. Something like

Code: Select all

App.getDocument('test').getObject('Sketch').addConstraint(Sketcher.Constraint('DistanceY',1,1,1,2,12.000000)) 
and then to change the constraint :

Code: Select all

App.getDocument('test').Sketch.setExpression('Constraints[1]', u'Spreadsheet.Alias')
[1] is the constraint index
No way with only one code line ?

Edit just to say wy I did not find this before :
Becouse in tne python console with my everyday freecad version, the line such App.getDocument('test').Sketch.setExpression('Constraints[1]', u'Spreadsheet.Alias') does not apprear (branch LinkDayly). It does in 0.21 freecad version...

With this, I can manage, but isn't there a way to do it directly when creating the constraint?
Macro Sketch Constraint From Spreadsheet :
https://wiki.freecad.org/Macro_Sketch_C ... adsheet/fr
edwilliams16
Veteran
Posts: 3108
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: "Length constraint in a sketch for a line with a value obtained using an alias in a spreadsheet"

Post by edwilliams16 »

@2cv001

I wasn't explicit enough:

Code: Select all

App.ActiveDocument.getObject('Sketch').addConstraint(Sketcher.Constraint('DistanceY',1,1,1,2,App.ActiveDocument.getObject('Spreadsheet').Alias))
substituting the 12 with the reference to it.
Post Reply