Trouble Setting Circle Radius Programatically

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
browntoastjam
Posts: 37
Joined: Fri Aug 20, 2021 9:51 pm

Trouble Setting Circle Radius Programatically

Post by browntoastjam »

Hello All,


I am trying to set the radius of a circle in a sketch programmatically:

Code: Select all

sheet = (FreeCAD.getDocument('model').getObject("Spreadsheet")

avgdia = (sheet.shankdia1  + sheet.shankdia2)/2.0

sketch = FreeCAD.getDocument('model').getObject('Sketch018')

for count, value in enumerate(sketch.Constraints):
	if value.Type == "Radius":
		print(count,value)
		sketch.addConstraint(Sketcher.Constraint('Radius',count,avgdia)) 

App.ActiveDocument.recompute()
However I see no changes to the sketch. Any idea about what I am doing wrong?
There are no error messages in the report view.
Thanks.

OS: Ubuntu 20.04.3 LTS (XFCE/xubuntu)
Word size of FreeCAD: 64-bit
Version: 0.20.25645 (Git) AppImage
Build type: Release
Branch: master
Hash: 37d9757399b4c2ec30318eb88d7cd7c508246345
Python version: 3.9.7
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.2
Locale: English/United States (en_US)
User avatar
chennes
Veteran
Posts: 3904
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Trouble Setting Circle Radius Programatically

Post by chennes »

Does your sketch already have a radius constraint, which you are trying to update? Or is it unconstrained, and you are trying to add a new one? To me this code looks like it's trying to do some combination of both.
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
TheMarkster
Veteran
Posts: 5508
Joined: Thu Apr 05, 2018 1:53 am

Re: Trouble Setting Circle Radius Programatically

Post by TheMarkster »

try this (untested)

Code: Select all

sketch.setDatum(count,value)
User avatar
chennes
Veteran
Posts: 3904
Joined: Fri Dec 23, 2016 3:38 pm
Location: Norman, OK, USA
Contact:

Re: Trouble Setting Circle Radius Programatically

Post by chennes »

TheMarkster has it, I think: for your case, something like

Code: Select all

for count, value in enumerate(sketch.Constraints):
    if value.Type == "Radius":
        sketch.setDatum(count, avgdia)
Chris Hennes
Pioneer Library System
GitHub profile, LinkedIn profile, chrishennes.com
edi
Posts: 481
Joined: Fri Jan 17, 2020 1:32 pm

Re: Trouble Setting Circle Radius Programatically

Post by edi »

browntoastjam wrote: Fri Sep 17, 2021 11:44 pm

Code: Select all

for count, value in enumerate(sketch.Constraints):
	if value.Type == "Radius":
		print(count,value)
		sketch.addConstraint(Sketcher.Constraint('Radius',count,avgdia)) 
I think, in this loop you are running through all existing constraints. If a 'Radius'-constraint is found, a new constraint is appended at the end of the list. So you have two constraints at the object with the GeoId "count", having different radius values.
Post Reply