[Solved] How to get Sketcher point location, cartesion coords.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
freedman
Veteran
Posts: 3466
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

[Solved] How to get Sketcher point location, cartesion coords.

Post by freedman »

While I'm in Sketcher I want to know the X,Y (relative to sketch 0,0) data of a user placed point. I have the global placements (X,Y,Z) of the two points (one of them being sketch center) but I have no idea how to get the local X,Y data of the user placed point. I understand that Z is important but not to me in this application because it's a sketch.

I'm try to build a Sketcher tool, when the user places a point I want to know the XY location. This is kind of like applying a dimension but I don't see how to do that from the console.

If you know of a macro that does this I can probably check that out.

Thank you
Last edited by freedman on Sun Sep 12, 2021 5:45 pm, edited 1 time in total.
heron
Posts: 307
Joined: Mon Apr 20, 2020 5:32 pm

Re: How to get Sketcher point location, cartesion coords.

Post by heron »

I know little about Python, but maybe this will help you:

Code: Select all

vector = App.ActiveDocument.getObject('Sketch').getPoint(0,1)
freedman
Veteran
Posts: 3466
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: How to get Sketcher point location, cartesion coords.

Post by freedman »

Thank you heron, that got me on the right track. The coords are in a different order than I expected, XZY. Maybe this due to the plane the sketch was originally attached to. I'll try to post a print out when I figure it out. :)
chrisb
Veteran
Posts: 54213
Joined: Tue Mar 17, 2015 9:14 am

Re: How to get Sketcher point location, cartesion coords.

Post by chrisb »

You could add two driven (blue) constraints for x and y.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: How to get Sketcher point location, cartesion coords.

Post by TheMarkster »

Usage:

Use the attached file, or:

Create a new sketch named "Sketch".
Add some Points
Rotate the sketch some angle, like 20 degrees, with rotation axis = 1,1,1 so that it moves off the xy plane.
Run the macro.

Code: Select all

sketch = App.ActiveDocument.Sketch
place = sketch.Placement
for geo in sketch.Geometry:
    # It must contain at least one Part::GeomPoint.
    if (hasattr(geo, 'X')
            and hasattr(geo, 'Y') and hasattr(geo, 'Z')):
        point = geo.copy()
        #I believe at this juncture point.X, point.Y will be local coordinates relative to sketch origin and point.Z will be 0.
        #then to set the point to global coordinates
        FreeCAD.Console.PrintMessage("before translate and rotate -> "+str(point.X)+","+str(point.Y)+","+str(point.Z)+"\n")
        point.translate(place.Base)
        point.rotate(place)
        FreeCAD.Console.PrintMessage("after translate and rotate -> "+str(point.X)+","+str(point.Y)+","+str(point.Z)+"\n")
Attachments
sketch_points.FCStd
(5 KiB) Downloaded 49 times
freedman
Veteran
Posts: 3466
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: How to get Sketcher point location, cartesion coords.

Post by freedman »

Thanks, theMarkster. That is really what I needed, that works in all axis.

My ultimate goal is to click on a point and get the local X,Y coords of that point. As I understand it the geometry is separate from the Gui stuff so I need to figure out how to pick the selected point from the geometry list. My first thought; get global placement of the selected point and then go thru the list of translated geometry data and pick the one close. I have noticed there appears to be some rounding errors.

Here is the code I use in a Selection observer to get global point coords, the printed coords agree with the status bar at screen lower. The calculated/translated coords from your code have some rounding errors or calculated errors, just slight differences. That makes it kind of tough to compare data sets. I can do rounding in the compares. Any idea why the different data?

Code: Select all

self.sel_pv = Gui.Selection.getSelectionEx()
    self.sel = self.sel_pv[0]
    if self.sel.HasSubObjects:
        self.vect = self.sel.PickedPoints
        self.clicked_loc = self.vect[0]
        print("Point coords",self.clicked_loc)
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: How to get Sketcher point location, cartesion coords.

Post by TheMarkster »

App.Vector.isEqual(other_vector,tolerance) will be True if the distance is less than or equal to tolerance. I think OCC uses 1e-7.

Code: Select all

>>> A = App.Vector(3.2,3.2,3.2)
>>> B = App.Vector(3.2000001,3.2000001,3.2000001)
>>> A.isEqual(B,1e-3)
True
>>> A.isEqual(B,1e-7)
False
>>> 
freedman
Veteran
Posts: 3466
Joined: Thu Mar 22, 2018 3:02 am
Location: Washington State, USA

Re: How to get Sketcher point location, cartesion coords.

Post by freedman »

Here is a link to an additional topic that solved the question. Marking this solved.
https://forum.freecadweb.org/viewtopic.php?f=22&t=62126
concifederico
Posts: 29
Joined: Fri Apr 02, 2021 2:36 pm

Re: How to get Sketcher point location, cartesion coords.

Post by concifederico »

chrisb wrote: Mon Aug 30, 2021 9:56 pm You could add two driven (blue) constraints for x and y.
Yep but how to read them from a script! I'm stuck on this reading docs and docs. I'm so lost I'm thinking on referencing to a spreadsheet and then reading the spreadsheet value.
Tks in advance.

Federico
chrisb
Veteran
Posts: 54213
Joined: Tue Mar 17, 2015 9:14 am

How to read named constraints from Sketcher from Python

Post by chrisb »

concifederico wrote: Wed Mar 27, 2024 1:34 am Yep but how to read them from a script! I'm stuck on this reading docs and docs.
It's three years ago that I posted here. Here we go:
  • Assume you have a point in a sketch named "Sketch". Add driven dimensions like this:
    SnipScreenshot-b5f871.png
    SnipScreenshot-b5f871.png (7.14 KiB) Viewed 258 times
  • In Python define a function

    Code: Select all

    def getConstraintValue(sketch,name):
      for con in sketch.Constraints:
        if con.Name == name:
          return con.Value
    
  • Call the function like this:

    Code: Select all

    sketch = App.ActiveDocument.Sketch
    x = getConstraintValue(sketch,"xValue")
    y = getConstraintValue(sketch,"yValue")
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply