[Solved} Looking for plane to vertex distance.

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
dan-miel
Posts: 391
Joined: Thu Sep 13, 2018 12:29 am
Location: Spokane WA. USA

[Solved} Looking for plane to vertex distance.

Post by dan-miel »

I’m working on a new measuring program. The idea is the first click on a feature will give you information on that feature, such as selecting a sphere will give you the x, y, z to origin, surface area, and radius. Selecting a second feature, say a vertex will give you the distance between the features plus the difference in x, y and z. I’m attaching the program if someone wants to try it out. Simply unzip the Folder from the file and drop it in your Mod folder.
My question: I have been trying to get a distance from a plane to a vertex. I have been using FreeCAD’s “distanceToShape” I can only get dimension “A” in the picture, which is nearest part if plane, I would like to get dimension “B”. Am I using it wrong or is there another way to get dimension ”B”?
Dan Miel
Attachments
Plane to Vertex.JPG
Plane to Vertex.JPG (29.33 KiB) Viewed 1020 times
QuickMeasure.zip
(45.13 KiB) Downloaded 9 times
Last edited by dan-miel on Sat Oct 08, 2022 2:49 pm, edited 1 time in total.
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Looking for plane to vertex distance.

Post by onekk »

dan-miel wrote: Fri Oct 07, 2022 12:56 am ...
Strange as a Plane is infinite.

Strangely I don't find a distancetoShape() method of FreeCAD.Vector.

Where do you have found it?

I have done a test code, see maybe it:

Code: Select all

"""Sample code.

This code was written as an sample code

Name: 20221007-distitoplane.py

Author: Carlo Dormeletti
Copyright: 2022
Licence: CC BY-NC-ND 4.0 IT
"""
import os # noqa
import sys  # noqa
import math # noqa
from math import pi, sin, cos # noqa

import FreeCAD
import FreeCADGui # noqa
from FreeCAD import Placement, Rotation, Vector # noqa
import Part # noqa

DOC = FreeCAD.activeDocument()

DOC_NAME = "dist_toplane"

def clear_doc():
    """Clear active document deleting all the objects."""
    for obj in DOC.Objects:
        DOC.removeObject(obj.Name)


def setview():
    """Rearrange View."""
    FreeCAD.Gui.SendMsgToActiveView("ViewFit")
    FreeCAD.Gui.activeDocument().activeView().viewAxometric()


if DOC is None:
    FreeCAD.newDocument(DOC_NAME)
    FreeCAD.setActiveDocument(DOC_NAME)
    DOC = FreeCAD.activeDocument()

else:

    clear_doc()

# EPS= tolerance to use to cut the parts
EPS = 0.10
EPS_C = EPS * -0.5
ROT0 = Rotation(0, 0, 0)

p_point = Vector(30, 30, 0)
p_norm = Vector(0, 0, 1)

t_point = Vector(0, 0, 50)

plane = Part.Plane(p_point, p_norm)

Part.show(plane.toShape(), "Plane")

Point = Part.Vertex(t_point)

point_do = Part.show(Point, "Point")
point_do.ViewObject.PointSize = 5.00
point_do.ViewObject.PointColor = (1.00,0.00,1.00)

print(Point.Point.distanceToPlane(p_point, p_norm))

setview()

if you modify:

Code: Select all

p_point = Vector(10,10,0)

modifying the Z value of the plane as example putting it at 10 the result of 50 became 40, take note that the point that define the plane has been placed in an arbitrary position.


Tested on:

Code: Select all

OS: Artix Linux (openbox)
Word size of FreeCAD: 64-bit
Version: 0.20.1.29410 (Git) AppImage
Build type: Release
Branch: (HEAD detached at 0.20.1)
Hash: f5d13554ecc7a456fb6e970568ae5c74ba727563
Python 3.10.5, Qt 5.15.4, Coin 4.0.0, Vtk 9.1.0, OCC 7.6.2
Locale: Italian/Italy (it_IT)
Installed mods: 
  * Assembly4 0.12.4
  * toSketch 1.0.1
  * Curves 0.5.10
Hope it helps.

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/
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Looking for plane to vertex distance.

Post by wandererfan »

dan-miel wrote: Fri Oct 07, 2022 12:56 am My question: I have been trying to get a distance from a plane to a vertex. I have been using FreeCAD’s “distanceToShape” I can only get dimension “A” in the picture, which is nearest part if plane, I would like to get dimension “B”. Am I using it wrong or is there another way to get dimension ”B”?
distanceToShape returns much more that just the minimum distance. For each solution, it provides the points on each shape that were used to compute the minimum distance. You can use these point pairs to get vertical or horizontal distance.

Code: Select all

>>> dts = shp1.distToShape(shp2)
>>> dts
(11.36018777757043, [(Vector (0.0, 0.0, 0.0), Vector (0.0, 4.739978710393904, 10.324072266633603))], [('Vertex', 0, None, 'Edge', 2, 5.215679125550999)])
minimum distance (11.36018777757043, 
closest point pairs [(Vector (0.0, 0.0, 0.0), Vector (0.0, 4.739978710393904, 10.324072266633603))],
support geometry and parameters [('Vertex', 0, None, 'Edge', 2, 5.215679125550999)])
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: Looking for plane to vertex distance.

Post by dprojects »

dan-miel wrote: Fri Oct 07, 2022 12:56 am I can only get dimension “A” in the picture, which is nearest part if plane, I would like to get dimension “B”. Am I using it wrong or is there another way to get dimension ”B”?
I already added this option vertex - > face. This use face plane recognition function from my library. Also there is vertices replacement.
This will be available with next commit.

Of course it works only if the plane is according to the XYZ coordinate axes. Probably someday I will improve the face plane recognition function from my library to work also with rotated faces. This need a little bit trick... to recognize the plane like peoples do.

Image

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
dan-miel
Posts: 391
Joined: Thu Sep 13, 2018 12:29 am
Location: Spokane WA. USA

Re: Looking for plane to vertex distance.

Post by dan-miel »

onekk wrote: Fri Oct 07, 2022 7:32 am Strangely I don't find a distancetoShape() method of FreeCAD.Vector.
Onekk, the distanceToShape should have been distToShape. Sorry for the miss spelling. Thanks wandererfan for the example in your post.
onekk wrote: Fri Oct 07, 2022 7:32 am Strange as a Plane is infinite.
Onekk, your code is what helped me. After using the distToShape I kept trying to add the face to the command, I didn’t realize that the (0, 0) meant a vector, which makes the face an infinite plane. I got the vector from the face by using “normalAt’. I know the code below doesn’t work, it is just and explanation of how I would describe how “distanceToPlane” works.

Code: Select all

VectorOfFace = selection[1].Face.normalAt(0,  0)
pointOnPlane = selection[1].Face.CenterOfMass
measurefrom = selection[0].Point
distance = measurefrom.distanceToPlane(pointOnPlane,VectorOfFace)
dprojects wrote: Sat Oct 08, 2022 10:47 am Of course it works only if the plane is according to the XYZ coordinate axes
dprojects: This code seems to work no matter how the part is rotated. My program just reports the distance so I’m not sure how it would work with dimensions.

Thanks for the replies.
Dan
User avatar
onekk
Veteran
Posts: 6149
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Looking for plane to vertex distance.

Post by onekk »

dan-miel wrote: Sat Oct 08, 2022 2:46 pm ...
Thanks for the replies.
Dan
Hello

normalAt(0.0)

is using the U V coordinates so if you want the center of the face you maybe want to use (0.5, 0.5) that is the center of face if exist such point. Planar faces have no problem, but from some explanation maybe a face derived from a sphere could not have all UV values defined for each point of the (0, 1) interval of U or V values.


EDIT: See @wandererfan reply to this post some posts below for the right answer.
END EDIT

distToShape is interesting as it returns many values. I have to make some experiments thanks @wandererfan for having teached me another thing to note down.

Kind Regards to all.

Carlo D.
Last edited by onekk on Sun Oct 09, 2022 7:03 am, edited 1 time in total.
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/
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: Looking for plane to vertex distance.

Post by dprojects »

dan-miel wrote: Sat Oct 08, 2022 2:46 pm

Code: Select all

VectorOfFace = selection[1].Face.normalAt(0,  0)
pointOnPlane = selection[1].Face.CenterOfMass
measurefrom = selection[0].Point
distance = measurefrom.distanceToPlane(pointOnPlane,VectorOfFace)
This code seems to work no matter how the part is rotated.
You mean the code above? The distanceToPlane FreeCAD function not show distance to plane even if the object is only moved 500 mm into each axis XYZ. I guess, all the FreeCAD functions for measure shows only distance between two points. The difference is only you give point at face. But CenterOfMass not always is the distance to face from vertex.

Image

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
dprojects
Posts: 721
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: Looking for plane to vertex distance.

Post by dprojects »

dan-miel wrote: Sat Oct 08, 2022 2:46 pm

Code: Select all

pointOnPlane = selection[1].Face.CenterOfMass
Using CenterOfMass might be little tricky because this is not the same as coordinate center of object, face or plane.
You can see it clearly at Cone object, where the mass is bigger at the top so the CenterOfMass is moved a little bit higher. Also some object like containers LinkGroup don't have CenterOfMass defined at all.

The distanceToPlane might be good if you want only print the distance at the console and the mass is equally divided. But if you want to draw the measurements from point to point this not works.

Image

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: Looking for plane to vertex distance.

Post by wandererfan »

onekk wrote: Sat Oct 08, 2022 3:16 pm normalAt(0.0)

is using the U V coordinates so if you want the center of the face you maybe want to use (0.5, 0.5) that is the center of face if exist such point. Planar faces have no problem, but from some explanation maybe a face derived from a sphere could not have all UV values defined for each point of the (0, 1) interval of U or V values.
You can not count on a face being parameterized over [0, 1]. If you want the normal at the "center" of the face, you have to calculate uMid and vMid from the parameter range.

Code: Select all

>>> obj = doc.getObject("Box")
>>> shp = obj.Shape
>>> sub = obj.getSubObject("Face3")
>>> sub.ParameterRange
(0.0, 10.0, 0.0, 10.0)
>>> (uLow, uHigh, vLow, vHigh) = sub.ParameterRange
>>> uLow
0.0
dan-miel
Posts: 391
Joined: Thu Sep 13, 2018 12:29 am
Location: Spokane WA. USA

Re: [Solved} Looking for plane to vertex distance.

Post by dan-miel »

dprojects wrote: Sat Oct 08, 2022 5:50 pm Using CenterOfMass might be little tricky because this is not the same as coordinate center of object, face or plane.
You can see it clearly at Cone object, where the mass is bigger at the top so the CenterOfMass is moved a little bit higher. Also some object like containers LinkGroup don't have CenterOfMass defined at all.
You are right. I had used Face.Vertexes[0].Point in my code and only used it on flat planes.
Darek: If you don't mind could you attach your Macro?
Thanks all
Dan
Edited(Added .Point to Face.Vertexes[0])
Last edited by dan-miel on Sun Oct 09, 2022 11:09 am, edited 2 times in total.
Post Reply