[Solved] Get a named point coordinates

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

Re: Get a named point coordinates [Solved]

Post by jfc4120 »

Thanks,

Code: Select all

(x, y, z) = (doc.Point.Shape.Point.x, doc.Point.Shape.Point.y, doc.Point.Shape.Point.z)
worked, so now I can use a point:

Code: Select all

(X1, Y1, Z1) = (doc.getObject('Point').Shape.Point.x, doc.getObject('Point').Shape.Point.y, doc.getObject('Point').Shape.Point.z)
#print(x, y)
X1 = X1 * .039370078
Y1 = Y1 * .039370078
Z1 = Z1 * .039370078
print(X1, Y1)
Works. I cannot believe a search did not uncover this.
edwilliams16
Veteran
Posts: 3183
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Get a named point coordinates

Post by edwilliams16 »

It's tidier and clearer to use vectors. It's likely you are going to make lines or arcs or whatever with the result and they take vector arguments. So it is code-clutter to convert back and forth unnecessarily.

Code: Select all

V1 = doc.getObject('Point').Shape.Point #returns the App.Vector(X1, Y1, Z1)
V1 *=  0.039370078   #multiply in place
print( V1.x, V1.y)# or just print(V1) if this is debug code
edwilliams16
Veteran
Posts: 3183
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Get a named point coordinates

Post by edwilliams16 »

So, working in the Draft Workbench, but using the Gui to pick points/objects.
As an exercise, we put a Draft.Arc and Draft.Point on the XY-Plane (top view).
Screen Shot 2022-07-25 at 2.12.49 PM.png
Screen Shot 2022-07-25 at 2.12.49 PM.png (8.39 KiB) Viewed 485 times
as in enclosed joinpointtoarc.FCStd

Let's write some code that allows us to first multiselect the point and the arc then the Macro makes 5 equidistant lines from the point to the arc like this.
Screen Shot 2022-07-25 at 2.16.27 PM.png
Screen Shot 2022-07-25 at 2.16.27 PM.png (10.46 KiB) Viewed 485 times
Here's the code:

First we grab the two selections. Gui.Selection.getSelectionEx() is often the one you want. Gui.Selection.getSelection() just gets the entire Document Object, without information about the particular SubObject (Edge, Vertex, Face) you selected.

Code: Select all

#multiselect the point then the arc
pointsel, arcsel = Gui.Selection.getSelectionEx()
Now we get the location of the point. We just did this.

Code: Select all

pointloc = point.Object.Shape.Point
print(pointloc)
Our second selection was the arc, we dig into its topology then geometry
arcsel.Object is the Document object (Arc). Its topology is inside arcsel.Object.Shape
We get the locations of its end-point vertexes

Code: Select all

arcedge = arcsel.Object.Shape
print(arcedge)  # it's an edge object - topology
v0, v1 = arcedge.Vertexes
print(v1.Point, v2.Point)
To find out the geometry, we need to go down from the topology level (vertex, edge, face, solid etc.) to the geometry (line, arc, BSpline etc.) level. For an edge it is Curve, for a face it's Surface.

Code: Select all

#now what geometry is Edge?
arc.edge.Curve  # Circle (Radius : 27.1395, Position : (-34.7107, 9.03188, 0), Direction : (0, 0, 1))
#note that this is an entire circle - but we want the arc
print(arcedge.Curve.trim(*arcedge.ParameterRange)) #
#ArcOfCircle (Radius : 27.1395, Position : (-34.7107, 9.03188, 0), Direction : (0, 0, 1), Parameter : (6.26342, 6.82674))
arcedgecurve = arcedge.Curve.trim(*arcedge.ParameterRange)
print(arcedgecurve.Center) #Vector (-34.71074295043945, 9.031879425048828, 0.0)
There's some tricky stuff there, that took me a while to get figured out. LineSegments are trimmed infinite Lines, ArcOfCircle are trimmed Circles, with a restricted parameter range.
Note that arcedge.Curve returns the entire Circle, you want just the portion that describes the arc: arcedge.Curve.trim(* arcedge.ParameterRange)

Now let's draw the desired lines:

Code: Select all

arcpoints = arcedgecurve.discretize(5) #five equally-spaced points along arc
for arcpoint in arcpoints:
    Draft.makeLine(pointloc, arcpoint)
The entire script:

Code: Select all

#multiselect the point then the arc
pointsel, arcsel = Gui.Selection.getSelectionEx()
pointloc = point.Object.Shape.Point
print(pointloc)
arcedge = arcsel.Object.Shape
v0, v1 = arcedge.Vertexes
print(v1.Point, v2.Point)
#now what geometry is Edge?
arc.edge.Curve  # Circle (Radius : 27.1395, Position : (-34.7107, 9.03188, 0), Direction : (0, 0, 1))
#note that this is an entire circle - but we want the arc
print(arcedge.Curve.trim(*arcedge.ParameterRange)) #
#ArcOfCircle (Radius : 27.1395, Position : (-34.7107, 9.03188, 0), Direction : (0, 0, 1), Parameter : (6.26342, 6.82674))
arcedgecurve = arcedge.Curve.trim(* arcedge.ParameterRange)
print(arcedgecurve.Center) #Vector (-34.71074295043945, 9.031879425048828, 0.0)
arcpoints = arcedgecurve.discretize(5) #five equally-spaced points along arc
for arcpoint in arcpoints:
    Draft.makeLine(pointloc, arcpoint)

Attachments
joinpointtoarc.FCStd
(4.85 KiB) Downloaded 8 times
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Get a named point coordinates

Post by jfc4120 »

I am going to study all code given.

Also is it possible to click a point and get the coordinates in python.
edwilliams16
Veteran
Posts: 3183
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Get a named point coordinates

Post by edwilliams16 »

jfc4120 wrote: Tue Jul 26, 2022 2:54 am
Also is it possible to click a point and get the coordinates in python.
As usual, your questions lack context. We've already told you about PickedPoints, if you click on an object in the 3D view. I'm guessing you want the coordinates when you click in empty space. Since coordinates are 3D and the screen is 2D you need to set the working plane somehow. Then a few seconds google gave
https://forum.freecadweb.org/viewtopic.php?t=9215
Post Reply