[solved] get array of crosslines between 2 objects

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
lagunax
Posts: 55
Joined: Wed May 25, 2022 5:12 pm

[solved] get array of crosslines between 2 objects

Post by lagunax »

another one little question.
where i can find info about result-format of distToShape method?
i understand a little bit from different topics about [(Vertex..)...] part, but now i need info about [('Edge',...)...] part. for axample i have result

Code: Select all

(1.1368683772161603e-13, 
[
    (Vector (-25.0, 528.6446609406725, 0.0), Vector (-25.0, 528.6446609406726, 0.0)), 
    (Vector (-25.0, 599.3553390593278, 0.0), Vector (-25.0, 599.3553390593274, 0.0)), 
    (Vector (25.0, 478.6446609406721, 0.0), Vector (24.999999999999602, 478.6446609406729, 0.0)), 
    (Vector (25.0, 549.355339059327, 0.0), Vector (25.0, 549.3553390593274, 0.0)), 
    (Vector (-25.0, 528.6446609406725, -100.0), Vector (-25.0, 528.6446609406726, -100.0)), 
    (Vector (-25.0, 599.3553390593278, -100.0), Vector (-25.0, 599.3553390593274, -100.0)), 
    (Vector (25.0, 478.6446609406721, -100.0), Vector (24.999999999999602, 478.6446609406729, -100.0)), 
    (Vector (25.0, 549.355339059327, -100.0), Vector (25.0, 549.3553390593274, -100.0))
], [
    ('Edge', 0, 528.6446609406725, 'Edge', 0, 717.4621202458749), 
    ('Edge', 0, 599.3553390593278, 'Edge', 2, 5232.537879754125), 
    ('Edge', 2, 5521.355339059328, 'Edge', 0, 646.7514421272207), 
    ('Edge', 2, 5450.644660940673, 'Edge', 2, 5303.24855787278), 
    ('Edge', 5, 528.6446609406725, 'Edge', 5, 717.4621202458749), 
    ('Edge', 5, 599.3553390593278, 'Edge', 8, 5232.537879754125), 
    ('Edge', 8, 5521.355339059328, 'Edge', 5, 646.7514421272207), 
    ('Edge', 8, 5450.644660940673, 'Edge', 8, 5303.24855787278)])
formatted a litle bit.

so, i need to get Edges from this, but still can not understand how edges linked to Vertexes.
Last edited by lagunax on Sun Jun 19, 2022 5:57 pm, edited 1 time in total.
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: distToShape info

Post by edwilliams16 »

The distToShape() tooltip tells the story. Here we find the closest points between two shapes.

Code: Select all

doc = App.ActiveDocument
cubeshp = doc.getObject('Box').Shape
coneshp = doc.getObject('Cone').Shape
dist, vectors, infos = cubeshp.distToShape(coneshp)
print(infos) #[('Edge', 0, 5.0, 'Edge', 0, 4.71238898038469)]
cubeedgeindex=infos[0][1]
cubeedgeparameter = infos[0][2]
cubeclosestpoint = cubeshp.Edges[cubeedgeindex].valueAt(cubeedgeparameter)
cpcube = Part.show(Part.Point(cubeclosestpoint).toShape(), 'Cubeclosestpoint')
Gui.ActiveDocument.getObject('Cubeclosestpoint').PointSize=10

vertexes = cubeshp.Edges[cubeedgeindex].Vertexes
print (f'Cube Edge from {vertexes[0].Point} to {vertexes[1].Point}')

Attachments
toShapeDemo.FCStd
(5.04 KiB) Downloaded 5 times
User avatar
wandererfan
Veteran
Posts: 6268
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: distToShape info

Post by wandererfan »

lagunax wrote: Wed Jun 15, 2022 7:17 pm another one little question.
where i can find info about result-format of distToShape method?
From the old https://wiki.freecadweb.org/TopoShape_API

distToShape( TopoShape )
Description: Calculates the minimum distance between this and a given TopoShape.
Returns: float<minimum distance>,list<nearest points>,list<nearest subshapes & parameters>

So your
('Edge', 0, 528.6446609406725, 'Edge', 0, 717.4621202458749),
means that a point on Edge0 of Shape1 is close to a point on Edge0 of Shape2. The actual point on the Edges can be retrieved with Shape1.Edges[0].valueAt(528.664...) and Shape2.Edges[0].valueAt(717.462...).
lagunax
Posts: 55
Joined: Wed May 25, 2022 5:12 pm

Re: distToShape info

Post by lagunax »

sorry for misinformation. i mean situation with building crossection. but experiments shows that distToShape is bad way working only for 2 planes.
with shapes it do not gives right result. example with cubes 1000mmX1000mmX1000mm:

Code: Select all

>>> obj1.Placement
Placement [Pos=(600,240,853), Yaw-Pitch-Roll=(0.997651,-40.765,29.2061)]
>>> obj2.Placement
Placement [Pos=(0,0,0), Yaw-Pitch-Roll=(0,0,0)]
>>> dtf2=obj1.Shape.distToShape(obj2.Shape)
>>> dtf2
(0.0, [(Vector (600.0, 240.0, 853.0), Vector (600.0, 240.0, 853.0))], [('Vertex', 1, None, 'Vertex', -1, None)])
as a result only 1 vertex. as i see i need search crossections plane by plane (face by face)
lagunax
Posts: 55
Joined: Wed May 25, 2022 5:12 pm

Re: distToShape info

Post by lagunax »

this is my solution for objects (tested on Part::FeaturePython)

Code: Select all

import Part


def faces_intersection_line(plane1, plane2):
    dts = plane1.distToShape(plane2)
    if dts[0] > 1e-7:
        return None
    else:
        return Part.makeLine(dts[1][0][0],dts[1][1][0])

def find_faces_intersections(object1, object2):
    res=[]
    for face1 in object1.Shape.Faces:
        for face2 in object2.Shape.Faces:
            line=faces_intersection_line(face1,face2)
            if line != None:
                res.append(line)
    return res

Post Reply