How to get updated (circle) properties after applying constraints

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
squirrel8475
Posts: 2
Joined: Sat Jul 17, 2021 4:36 pm

How to get updated (circle) properties after applying constraints

Post by squirrel8475 »

Hello FreeCAD users,

A Circle is created in a Sketch without knowing a priori the center and the radius. These parameters are supposed to be extracted from other features, here in this minimal code a simple point is created. Constraints are applied to make the center of the circle to coincide with the point, whereas the radius is set to a constant value. After the constraints are applied, and after recomputing the doc, the Circle is in the right position and with the expected radius. The question is now how to get the coordinates of the center and the value of the radius within the Python console? Using the attributes .center and .radius give the values set before applying the constraints (here zeros), and the target would be to have the values after applying them.

Below the code corresponding to the question.

Thank you in advance for your help!

Code: Select all

doc_name="minimal_example_cad"
this_doc=App.newDocument(doc_name)
 
# Create sketch
my_sketch_name='Test'
my_sketch=this_doc.addObject('Sketcher::SketchObject',my_sketch_name)
my_sketch.Placement = App.Placement(App.Vector(0.000000,0.000000,0.000000),App.Rotation(0.000000,0.000000,0.000000,1.000000))
my_sketch.MapMode = "Deactivated"
my_sketch = App.ActiveDocument.getObject(my_sketch_name)

# Create a point and fix it
point_A=Part.Point(App.Vector(40,30,0))
point_A_id=0
my_sketch.addGeometry(point_A)
my_sketch.addConstraint(Sketcher.Constraint('DistanceX', 0, 1, 40)) 
my_sketch.addConstraint(Sketcher.Constraint('DistanceY', 0, 1, 30)) 

# Create a circle, and add constraints
circle1=Part.Circle()
circle1_id=1
my_sketch.addGeometry(circle1,True)
my_sketch.addConstraint(Sketcher.Constraint('Coincident',circle1_id,3,point_A_id,1)) # Constraint for coincidence between center and point A
my_sketch.addConstraint(Sketcher.Constraint('Radius',circle1_id,50)) # Constraint for radius
this_doc.recompute()

Gui.SendMsgToActiveView("ViewFit")

print(circle1.Center) # Vector (0.0, 0.0, 0.0) ; target is to get 40,30,0
print(circle1.Radius) # 1.0 ; target is to get 50
Syres
Veteran
Posts: 2902
Joined: Thu Aug 09, 2018 11:14 am

Re: How to get updated (circle) properties after applying constraints

Post by Syres »

Hopefully the bit added on the end will point you in the right direction:

Code: Select all

doc_name="minimal_example_cad"
this_doc=App.newDocument(doc_name)
 
# Create sketch
my_sketch_name='Test'
my_sketch=this_doc.addObject('Sketcher::SketchObject',my_sketch_name)
my_sketch.Placement = App.Placement(App.Vector(0.000000,0.000000,0.000000),App.Rotation(0.000000,0.000000,0.000000,1.000000))
my_sketch.MapMode = "Deactivated"
my_sketch = App.ActiveDocument.getObject(my_sketch_name)

# Create a point and fix it
point_A=Part.Point(App.Vector(40,30,0))
point_A_id=0
my_sketch.addGeometry(point_A)
my_sketch.addConstraint(Sketcher.Constraint('DistanceX', 0, 1, 40)) 
my_sketch.addConstraint(Sketcher.Constraint('DistanceY', 0, 1, 30)) 

# Create a circle, and add constraints
circle1=Part.Circle()
circle1_id=1
my_sketch.addGeometry(circle1,True)
my_sketch.addConstraint(Sketcher.Constraint('Coincident',circle1_id,3,point_A_id,1)) # Constraint for coincidence between center and point A
my_sketch.addConstraint(Sketcher.Constraint('Radius',circle1_id,50)) # Constraint for radius
this_doc.recompute()

Gui.SendMsgToActiveView("ViewFit")

for eachGeo in my_sketch.Geometry:
    print(eachGeo)
    if eachGeo.TypeId == 'Part::GeomPoint':
        print(eachGeo.Content)
    elif eachGeo.TypeId == 'Part::GeomCircle':
        print(eachGeo.Radius)
squirrel8475
Posts: 2
Joined: Sat Jul 17, 2021 4:36 pm

Re: How to get updated (circle) properties after applying constraints

Post by squirrel8475 »

I see, the key is to access the Sketch.Geometry directly, thank you for your help.

Code: Select all

# List sketch geometry items to check content
for eachGeo in my_sketch.Geometry:
    print(eachGeo)
	
new_pt_A=my_sketch.Geometry[point_A_id]
new_circle=my_sketch.Geometry[circle1_id]

print(new_pt_A.X,new_pt_A.Y)
print(new_circle.Radius) 
Post Reply