How to intersect a line with a part

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

How to intersect a line with a part

Post by microelly2 »

Given two points P1,P2 I want to find the intersection of the line P1P2 with a Part-Object.

I use the following code

Code: Select all


import Part,PartGui


App.newDocument()
App.setActiveDocument("Unnamed")
App.ActiveDocument=App.getDocument("Unnamed")

# the test part
App.ActiveDocument.addObject("Part::Box","Box")
myBody=App.activeDocument().Box


App.ActiveDocument.addObject("Part::Line","Line")

# Point 1
App.ActiveDocument.Line.X1=0.00
App.ActiveDocument.Line.Y1=0.00
App.ActiveDocument.Line.Z1=0.00

# Point 2
App.ActiveDocument.Line.X2=100.00
App.ActiveDocument.Line.Y2=30.00
App.ActiveDocument.Line.Z2=400.00
# App.ActiveDocument.Line.Placement=Base.Placement(Base.Vector(0.00,0.00,0.00),Base.Rotation(0.00,0.00,0.00,1.00))

myLine=App.activeDocument().Line


#
#
#
#
#

# is there a shorter way th an the next lines?

App.activeDocument().addObject("Part::MultiCommon","Common")
App.activeDocument().Common.Shapes = [myLine,myBody]

App.ActiveDocument.recompute()

y=App.activeDocument().Common

z=y.Shape.Edges[0].Vertexes[1]

# RESULT OUTPUT

z.X
z.Y
z.Z



I hope that there is function to calculate the coordiantes without the helper common object.
thank you for some tipps.
ulrich1a
Veteran
Posts: 1957
Joined: Sun Jul 07, 2013 12:08 pm

Re: How to intersect a line with a part

Post by ulrich1a »

microelly2 wrote:I hope that there is function to calculate the coordiantes without the helper common object.
The common object seems to me a good solution. You just need not so much code, when the common object should not be visible in your document, as you can see here: http://www.freecadweb.org/wiki/index.ph ... tersection

Code: Select all

y = myline.common(myBody)
Regards
Ulrich
User avatar
wandererfan
Veteran
Posts: 6265
Joined: Tue Nov 06, 2012 5:42 pm
Contact:

Re: How to intersect a line with a part

Post by wandererfan »

microelly2 wrote:I hope that there is function to calculate the coordiantes without the helper common object.
thank you for some tipps.
This should get you started. It works with a line and a solid, but won't work for 2 lines or a line and a face.

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# example for intersection of shape(Solid) & line(Edge)
# Usage: select a line and a solid, then run this macro
# http://www.forum.freecadweb.org/viewtopic.php?f=22&t=5456

import FreeCAD
import Part

s = FreeCADGui.Selection.getSelection()
if len(s) < 2:
    print "not enough objects selected - ", len(s)

s1 = s[0].Shape
s2 = s[1].Shape        

cs = s1.common(s2)
if cs.Vertexes:
    for v in cs.Vertexes:
        print "intersection point : ", v.Point
else:
    print "can't find an intersection point"
wf
mario52
Veteran
Posts: 4673
Joined: Wed May 16, 2012 2:13 pm

Re: How to intersect a line with a part

Post by mario52 »

hi wandererfan

I create a macro with your snippet Macro_FCSpring_On_Surface for Zwei Helix - ein Sweep

ImageImage

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.
Post Reply