[Solved] Get arc angle and radius

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

[Solved] Get arc angle and radius

Post by jfc4120 »

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.29177 (Git)
Build type: Release
Branch: releases/FreeCAD-0-20
Hash: 68e337670e227889217652ddac593c93b5e8dc94
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.2
Locale: English/United States (en_US)
Installed mods: 
  * Help 1.0.3
Image attached which explains. I Googled this but nothing comes up, I used terms like:
freecad python get arc angle
freecad python get last angle of arc
I am trying to get angle and radius.

I know I can multiple .0174533 x r x degree, but here it's unknown.

There has got to be a way. Also is there some documentation elsewhere which covers this, I didn't see it in the regular documentation?
Believe I searched, you would think a Google search would bring this up.
Attachments
arc.png
arc.png (42.11 KiB) Viewed 925 times
Last edited by jfc4120 on Wed Oct 05, 2022 9:28 pm, edited 1 time in total.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Get arc angle and radius

Post by heda »

have you tried

Code: Select all

App.ActiveDocument.Arc005.LastAngle
?
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Get arc angle and radius

Post by jfc4120 »

I won't know the name of the arc in python, I need to get angle and radius from selection.

Edit:

I got radius:

Code: Select all

RAD = Gui.Selection.getSelectionEx()[0].SubObjects[0].Curve.Radius * .039370078
print(RAD)
Haven't figured out angle yet, I tried several things.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Get arc angle and radius

Post by onekk »

It depends.

For a generic shape there could be or could be not an angle and radius.

In the specific case you have a Draft object that has some Properties tha usually you could access from Python.

The most easy way to know "property name" is to change it with GUI and see what is echoed in Python Console.

Hope it help.

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/
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Get arc angle and radius

Post by jfc4120 »

I do not understand. But it will be an arc.

Edit:

Where is all of the documentation that covers all of the possibilities of:

Gui.Selection.getSelectionEx()[0].SubObjects[0]?

I cannot find it anywhere.
User avatar
onekk
Veteran
Posts: 6146
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Get arc angle and radius

Post by onekk »

jfc4120 wrote: Tue Oct 04, 2022 11:48 pm ...
Where is all of the documentation that covers all of the possibilities of:

Gui.Selection.getSelectionEx()[0].SubObjects[0]?

I cannot find it anywhere.
What documentation you intend?

Python is object oriented, so:

Code: Select all

Gui.Selection.getSelectionEx()[0]
Is simply returning a DocumentObject you selected (the first as it is a list and you are retrieving first element using [0]).

Code: Select all

.SubObjects[0]
is returning first subobject and so on.

It is basic Python.

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/
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Get arc angle and radius

Post by heda »

well, a bit salty, so I will go a bit salty as well...
you have been here for a while now and there are plenty of posts of yours showing that you know how to get hold of a selected object.

when writing the earlier post i decided against putting in the phrase "if you want to make it complicated for yourself you can find that through the objects shape/curve", which it now turns out is the route you took (not overly surprising, but there is an easier route given in my first post, which you clearly did not even explore, but just threw in the garbage over something that you yourself already know).

one can search the web for "fixed vs growth mindset" and think a minute or two in which category one falls into (and what that generally means for learning things)...

the correct search term is "freecad properties", the two top links are correct hits in my search engine, although the 2nd one is the most informative - they are both freecad wiki pages...

a suggestion is to study some (any) basic python write-up on object oriented programming, as a self-taught hobby coder (speaking about myself) it took quite a while before the penny finally dropped when it comes to objects in python, but when it did, anything code related became a heck of a lot easier to make the computer to do what one wanted (and also easier to absorb documentation).

so considering a fact of life, python objects...
App.ActiveDocument.Arc005.LastAngle is exactly the same (yes, literally exactly the same, not similar) as Gui.Selection.getSelectionEx()[0].LastAngle
assuming that you have selected Arc005, or - you could as well have asked the question "how do I get an object property", selection has little to do with it, since you already know how to get an object from a selection...

try it yourself...

Code: Select all

docobj1 = App.ActiveDocument.Arc005
docobj2 = Gui.Selection.getSelectionEx()[0]

assert docobj1.LastAngle == docobj2.LastAngle

docobj1 is docobj2
how to get the radius in an analogue way I leave up to you to find out when you now have the path of how to get the last angle (that is presented in the property editor for a draft arc)...

going the route you have been shown (but for reasons beyond my comprehension discard) is the easy way.
anyhow, to get what you want in your own words...
Gui.Selection.getSelectionEx()[0].LastParameter*180/pi

btw, are you always going to assume that the starting angle is zero?
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Get arc angle and radius

Post by jfc4120 »

I tried:

Code: Select all

LA = Gui.Selection.getSelectionEx()[0].SubObjects[0].LastAngle
print(LA)
and

Code: Select all

LA = Gui.Selection.getSelectionEx()[0].LastAngle
print(LA)
and

Code: Select all

LA = Gui.Selection.getSelectionEx()[0].SubObjects[0].Curve.LastAngle
print(LA)
And I get:
LA = Gui.Selection.getSelectionEx()[0].SubObjects[0].LastAngle
<class 'AttributeError'>: 'Part.Edge' object has no attribute 'LastAngle'
The object has no attribute 'LastAngle' in all attempts.

This:

Code: Select all

LA = App.ActiveDocument.Arc005.LastAngle
Does work, but is of no use hard coding a named arc.

What if i click Arc002? But I have Arc005 hard coded, then what.

Edit:

You get radius via:

Code: Select all

RAD = Gui.Selection.getSelectionEx()[0].SubObjects[0].Curve.Radius
Logic would tell you that LastAngle should be:

Code: Select all

LA = Gui.Selection.getSelectionEx()[0].SubObjects[0].Curve.LastAngle
But that is not the case. Well I cannot even enter Arc005, when I try:

Code: Select all

VAR = input("Arc Name:")
LA = App.ActiveDocument.VAR.LastAngle
You cannot use a variable there. So it seems that to use python to achieve this, I have to only select an arc with a name of Arc005.
If I have to choose Arc004, I will have to modify the program and hard code in Arc004.

Is there a solution?
User avatar
Roy_043
Veteran
Posts: 8450
Joined: Thu Dec 27, 2018 12:28 pm

Re: Get arc angle and radius

Post by Roy_043 »

Code: Select all

Gui.Selection.getSelectionEx()[0].SubObjects[0].LastParameter
Parameter in radians.
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: Get arc angle and radius

Post by heda »

one has to keep track of which objects one has at hand...
(also valid for me :-))

LastAngle is a property of the document object (App.ActiveDocument.Arc), not the shape curve (App.ActiveDocument.Arc.Shape.Curve) (see long script at end)

so, the below will work

Code: Select all

Gui.Selection.getSelection()[0].LastAngle

probably other selection scheme constructs works as well, but if you want to get hold of LastAngle, it is the document object that is needed (if you want it directly, can of course also go via Curve, but it is more elaborate).


sure, one can pick up any attribute dynamically (anytime, anywhere), sample constructs are...

Code: Select all

VAR = input("Arc Name:")
LA = getattr(App.ActiveDocument, VAR).LastAngle
# with vanilla python, and
LA = App.ActiveDocument.getObject(VAR).LastAngle
# using getobject from fc...

Code: Select all

## exploring objects
>>> App.ActiveDocument.Arc
<Part::Part2DObject>

>>> App.ActiveDocument.Arc.LastAngle
116.56504903391658 deg

# selecting the arc in tree-view
>>> Gui.Selection.getSelection()
[<Part::Part2DObject>]

>>> Gui.Selection.getSelectionEx()[0]
<SelectionObject>

>>> Gui.Selection.getSelectionEx()[0].Object
<Part::Part2DObject>

>>> Gui.Selection.getSelectionEx()[0].SubObjects
()

# selecting the arc in 3d-view
>>> Gui.Selection.getSelection()
[<Part::Part2DObject>]

>>> Gui.Selection.getSelectionEx()[0].Object
<Part::Part2DObject>

>>> Gui.Selection.getSelectionEx()[0].SubObjects
(<Edge object at 0x55f0ede455b0>,)

>>> Gui.Selection.getSelectionEx()[0].SubObjects[0]
<Edge object at 0x55f0f6b6fd40>

>>> Gui.Selection.getSelectionEx()[0].SubObjects[0].Curve
Circle (Radius : 19.7996, Position : (-7.50708, 6.65722, 0), Direction : (0, 0, 1))

>>> Gui.Selection.getSelectionEx()[0].Object is App.ActiveDocument.Arc
True

>>> Gui.Selection.getSelectionEx()[0].SubObjects[0] is App.ActiveDocument.Arc
False

>>> Gui.Selection.getSelectionEx()[0].SubObjects[0] is App.ActiveDocument.Arc.Shape
False ## hm...

>>> Gui.Selection.getSelectionEx()[0].SubObjects[0]
<Edge object at 0x7f8880022b70>

>>> Gui.Selection.getSelectionEx()[0].SubObjects[0]
<Edge object at 0x7f88800151e0>

>>> App.ActiveDocument.Arc.Shape
<Edge object at 0x7f88800226c0>

>>> App.ActiveDocument.Arc.Shape
<Edge object at 0x7f88800166d0>

# so new objects and thus id's are created every time called...
# so a different route is needed to check if they contain the same..
# for simplicity, check content...

>>> Gui.Selection.getSelectionEx()[0].SubObjects[0].Curve.Content
'<GeoExtensions count="0">\n</GeoExtensions>\n<Circle CenterX="-7.50708" CenterY="6.65722" CenterZ="0" NormalX="0" NormalY="0" NormalZ="1" AngleXU="-0" Radius="19.7996"/>\n'

>>> App.ActiveDocument.Arc.Shape.Curve.Content
'<GeoExtensions count="0">\n</GeoExtensions>\n<Circle CenterX="-7.50708" CenterY="6.65722" CenterZ="0" NormalX="0" NormalY="0" NormalZ="1" AngleXU="-0" Radius="19.7996"/>\n'

# well, at least same content...

>>> dir(Gui.Selection.getSelectionEx()[0].SubObjects[0].Curve)
['AngleXU', 'Axis', 'Center', 'Content', 'Continuity', 'Eccentricity', 'FirstParameter', 'LastParameter', 'Location', 'MemSize', 'Module', 'Radius', 'Rotation', 'Tag', 'TypeId', 'XAxis', 'YAxis', '__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__', 'approximateBSpline', 'centerOfCurvature', 'clone', 'continuityWith', 'copy', 'curvature', 'deleteExtensionOfName', 'deleteExtensionOfType', 'discretize', 'dumpContent', 'getAllDerivedFrom', 'getD0', 'getD1', 'getD2', 'getD3', 'getDN', 'getExtensionOfName', 'getExtensionOfType', 'getExtensions', 'hasExtensionOfName', 'hasExtensionOfType', 'intersect', 'intersect2d', 'intersectCC', 'intersectCS', 'isClosed', 'isDerivedFrom', 'isPeriodic', 'length', 'makeRuledSurface', 'mirror', 'normal', 'parameter', 'parameterAtDistance', 'period', 'restoreContent', 'reverse', 'reversedParameter', 'rotate', 'scale', 'setExtension', 'tangent', 'toBSpline', 'toNurbs', 'toShape', 'transform', 'translate', 'trim', 'value']

>>> 'LastAngle' in dir(Gui.Selection.getSelectionEx()[0].SubObjects[0].Curve)
False

>>> dir(App.ActiveDocument.Arc)
['Area', 'Attacher', 'AttacherType', 'AttachmentOffset', 'Content', 'Document', 'ExpressionEngine', 'FirstAngle', 'FullName', 'ID', 'InList', 'InListRecursive', 'Label', 'Label2', 'LastAngle', 'MakeFace', 'MapMode', 'MapPathParameter', 'MapReversed', 'MemSize', 'Module', 'MustExecute', 'Name', 'NoTouch', 'OldLabel', 'OutList', 'OutListRecursive', 'Parents', 'Placement', 'PropertiesList', 'Proxy', 'Radius', 'Removing', 'Shape', 'State', 'Support', 'TypeId', 'ViewObject', 'Visibility', '__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__', 'addExtension', 'addProperty', 'adjustRelativeLinks', 'changeAttacherType', 'clearExpression', 'dumpContent', 'dumpPropertyContent', 'enforceRecompute', 'evalExpression', 'getAllDerivedFrom', 'getDocumentationOfProperty', 'getEditorMode', 'getEnumerationsOfProperty', 'getGlobalPlacement', 'getGroupOfProperty', 'getLinkedObject', 'getParentGeoFeatureGroup', 'getParentGroup', 'getPaths', 'getPathsByOutList', 'getPropertyByName', 'getPropertyNameOfGeometry', 'getPropertyOfGeometry', 'getPropertyStatus', 'getPropertyTouchList', 'getStatusString', 'getSubObject', 'getSubObjectList', 'getSubObjects', 'getTypeIdOfProperty', 'getTypeOfProperty', 'hasChildElement', 'hasExtension', 'isDerivedFrom', 'isElementVisible', 'isValid', 'positionBySupport', 'purgeTouched', 'recompute', 'removeProperty', 'resolve', 'resolveSubElement', 'restoreContent', 'restorePropertyContent', 'setDocumentationOfProperty', 'setEditorMode', 'setElementVisible', 'setExpression', 'setGroupOfProperty', 'setPropertyStatus', 'supportedProperties', 'touch']

>>> 'LastAngle' in dir(App.ActiveDocument.Arc)
True
Post Reply