[Solved] Two lines intersection point

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

[Solved] Two lines intersection point

Post by jfc4120 »

Code: Select all

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.2.29177 +426 (Git)
Build type: Release
Branch: (HEAD detached from 0.20.2)
Hash: 930dd9a76203a3260b1e6256c70c1c3cad8c5cb8
Python 3.8.10, Qt 5.15.2, Coin 4.0.1, Vtk 8.2.0, OCC 7.6.3
Locale: English/United States (en_US)
Installed mods: 
  * Help 1.0.3


I found the post viewtopic.php?style=1&p=171776

But they never showed how to get the x,y,z coordinates of the intersection.

I tried

Code: Select all

import DraftGeomUtils
s = FreeCADGui.Selection.getSelection()
L1 = s[0]
L2 = s[1]
v = DraftGeomUtils.findIntersection(L1.Shape.Edges[0],L2.Shape.Edges[0])
print(v.x)    # just to test
But that wasn't it. How do I get the actual coordinates? Thanks.
Last edited by jfc4120 on Fri Feb 03, 2023 11:55 pm, edited 1 time in total.
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Two lines intersection point

Post by mario52 »

Hi

complement:

Code: Select all

>>> import DraftGeomUtils
>>> s = FreeCADGui.Selection.getSelection()
>>> L1 = s[0]
>>> L2 = s[1]
>>> v = DraftGeomUtils.findIntersection(L1.Shape.Edges[0],L2.Shape.Edges[0])
>>> v
[Vector (-1.3432017072221727, 0.28294823146762915, 0.0)]
>>> v[0]
Vector (-1.3432017072221727, 0.28294823146762915, 0.0)
>>> v[0].x
-1.3432017072221727
>>> v[0].y
0.28294823146762915
>>> v[0].z
0.0
>>> # Gui.Selection.clearSelection()
>>> 
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Two lines intersection point

Post by jfc4120 »

@mario52 when I test print:

Code: Select all

import DraftGeomUtils
s = FreeCADGui.Selection.getSelection()
L1 = s[0]
L2 = s[1]
v = DraftGeomUtils.findIntersection(L1.Shape.Edges[0],L2.Shape.Edges[0])
print(v[0].x)
I get this error:
File "C:/Users/Owner/AppData/Roaming/FreeCAD/Macro/line_intersect_test.py", line 25, in <module>
print(v[0].x)
<class 'IndexError'>: list index out of range
Edit:

File attached:

Edit2:

In console I get:

Code: Select all

>>> import DraftGeomUtils
>>> s = FreeCADGui.Selection.getSelection()
>>> L1 = s[0]
>>> L2 = s[1]
>>> v = DraftGeomUtils.findIntersection(L1.Shape.Edges[0],L2.Shape.Edges[0])
>>> v
[]
>>> 
Attachments
angle_test22.FCStd
(5.63 KiB) Downloaded 18 times
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Two lines intersection point

Post by mario52 »

Hi

your lines hare not intersected (superposed)

this is other thing

mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Two lines intersection point

Post by jfc4120 »

I am trying to figure out the point where the intersection is.
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Two lines intersection point

Post by edwilliams16 »

Code: Select all

>>> help(DraftGeomUtils.findIntersection)
Help on function findIntersection in module draftgeoutils.intersections:

findIntersection(edge1, edge2, infinite1=False, infinite2=False, ex1=False, ex2=False, dts=True, findAll=False)
    Return a list containing the intersection points of 2 edges.
    
    You can also feed 4 points instead of `edge1` and `edge2`.
    If `dts` is used, `Shape.distToShape()` is used, which can be buggy.
If you use

Code: Select all

Gui.Selection.getSelection()
you get a list of selection objects. If you pick two lines, you get two objects, each an edge. If you select two edges of a single object, you only get one selection object. The two edges are sub-objects of that object and are not accessible.

To access sub-object selections you need Gui.Selection.getSelectionEx(), and to deal with a selection being either the entire object or a subobject, you can do the following:

Code: Select all

subObjs = []# create list of selected (sub) objects
for sel in Gui.Selection.getSelectionEx(): 
  for path in sel.SubElementNames if sel.SubElementNames else ['']:#the else clause handles the case the entire object is selected
      subObj = sel.Object.getSubObject(path)
      subObjs.append(subObj)
      print(f'{sel.Object.Name} {path} ')
      # do stuff with your selections, like
      if subObj.ShapeType == 'Vertex':
          print(f'Vertex Loc = {sub.Point}')


#do stuff with your list, like
import DraftGeomUtils
edges = [sub for sub in subObjs if sub.ShapeType == 'Edge']
if len(edges) == 2:
    xns = DraftGeomUtils.findIntersection(edges[0],edges[1], findAll = True)
    if xns:
        for xn in xns:
            print(f'Edge intersection at ({xn.x}, {xn.y}, {xn.z})')
    else:
        print(f'Edges do not intersect')
else:
    print(f'Select two edges')

if you replace Gui.Selection.getSelectionEx() with Gui.Selection.getSelectionEx('',0), data is returned in the global coordinate system instead of the local one (or more precisely, the local coordinate system of the linked object, if it is a link).
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Two lines intersection point

Post by jfc4120 »

@edwilliams16 I keep getting
16:35:04 Line001 Edge1
16:35:04 Line Edge1
16:35:04 Edges do not intersect
I am after the point they would intersect.
Attachments
int.png
int.png (16.06 KiB) Viewed 794 times
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: Two lines intersection point

Post by mario52 »

Hi

aloha edwilliams16, good

jfc4120 with your example file (angle_test22.FCStd):

Code: Select all

import DraftGeomUtils
s = FreeCADGui.Selection.getSelection()
L1 = s[0]
L2 = s[1]
v = DraftGeomUtils.findIntersection(L1.Shape.Edges[0],L2.Shape.Edges[0], infinite1=True, infinite2=True, ex1=False, ex2=False, dts=True, findAll=False)
print(v)

#result [Vector (-543.56428337233, -4.0574187284443696e-16, -903.1914471949784)]
mario
Maybe you need a special feature, go into Macros_recipes and Code_snippets, Topological_data_scripting.
My macros on Gist.github here complete macros Wiki and forum.
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Two lines intersection point

Post by edwilliams16 »

jfc4120 wrote: Fri Feb 03, 2023 10:36 pm @edwilliams16 I keep getting
16:35:04 Line001 Edge1
16:35:04 Line Edge1
16:35:04 Edges do not intersect
I am after the point they would intersect.
I quoted the help on DraftGeomUtils.findIntersection for two reasons
  • So you learn how to answer your own question on how to get the locations
  • To point out it that has options.

If you want to find where the extended edges intersect, you need the options:

Code: Select all

xns = DraftGeomUtils.findIntersection(edges[0],edges[1], infinite1=True, infinite2=True, findAll = True)
without that you only get those of the actual edges. Note that this routine does not assume the edges are straight lines. That is why there might be more than one. Try a circle and intersecting line, for instance.
jfc4120
Posts: 448
Joined: Sat Jul 02, 2022 11:16 pm

Re: Two lines intersection point

Post by jfc4120 »

@edwilliams16 works great. I will test more.
Post Reply