find the center of a circle with python

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
synaption
Posts: 1
Joined: Mon Apr 19, 2021 6:17 pm

find the center of a circle with python

Post by synaption »

I made a circle that is tangent to two circles and a point, now I would like to find the center of the circle using python. I know I can just click on it and it will tell me in the python console, but I need to automate this process. Any thoughts?
chrisb
Veteran
Posts: 54273
Joined: Tue Mar 17, 2015 9:14 am

Re: find the center of a circle with python

Post by chrisb »

If it is shown in the Python console, can't you use the commands shown there as well?

If you need more help, it could be helpful to attach the file. We don't even know if you use Sketcher or Draft workbench for the circle.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
mario52
Veteran
Posts: 4696
Joined: Wed May 16, 2012 2:13 pm

Re: find the center of a circle with python

Post by mario52 »

hi

with object selected in 3D view

Code: Select all

try:
    selectedEdge = FreeCADGui.Selection.getSelectionEx()[0].SubObjects[0]   # select one element
    print(selectedEdge.Curve)
    print(selectedEdge.Curve.Radius)
    print(selectedEdge.Curve.Center)
except Exception:
	print("Oups")
given the name

Code: Select all

selectedEdge = App.ActiveDocument.getObject("Circle") # name of circle
print(selectedEdge.Shape.Edge1.Curve)                 # Edge1 = numero of edge
print(selectedEdge.Shape.Edge1.Curve.Radius)
print(selectedEdge.Shape.Edge1.Curve.Center)

EDIT: 20/04/2021 12h21 Paris adding the code for search radius given name

mario
Last edited by mario52 on Tue Apr 20, 2021 10:23 am, edited 1 time in total.
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: find the center of a circle with python

Post by onekk »

When you are asking for python help, better to put some code on which someone could work on.

Maybe answer is a matter of a line or two.

But it depends on what you want to achieve.

You made circle how. With scripting or using the GUI?
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/
mario52
Veteran
Posts: 4696
Joined: Wed May 16, 2012 2:13 pm

Re: find the center of a circle with python

Post by mario52 »

Hi
onekk wrote: Tue Apr 20, 2021 7:04 am You made circle how. With scripting or using the GUI?
good reflection I added the code for a named object

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
Post Reply