[Solved] How to get Sketcher point location, cartesion coords.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
[Solved] How to get Sketcher point location, cartesion coords.
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
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.
Re: How to get Sketcher point location, cartesion coords.
I know little about Python, but maybe this will help you:
Code: Select all
vector = App.ActiveDocument.getObject('Sketch').getPoint(0,1)
Re: How to get Sketcher point location, cartesion coords.
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.
Re: How to get Sketcher point location, cartesion coords.
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.
-
- Veteran
- Posts: 5513
- Joined: Thu Apr 05, 2018 1:53 am
Re: How to get Sketcher point location, cartesion coords.
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.
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
Re: How to get Sketcher point location, cartesion coords.
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?
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)
-
- Veteran
- Posts: 5513
- Joined: Thu Apr 05, 2018 1:53 am
Re: How to get Sketcher point location, cartesion coords.
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
>>>
Re: How to get Sketcher point location, cartesion coords.
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
https://forum.freecadweb.org/viewtopic.php?f=22&t=62126
-
- Posts: 29
- Joined: Fri Apr 02, 2021 2:36 pm
How to read named constraints from Sketcher from Python
It's three years ago that I posted here. Here we go: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.
- Assume you have a point in a sketch named "Sketch". Add driven dimensions like this:
- 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.