iLogic vector intersection point

iLogic vector intersection point

thomas_wegerBYFE5
Contributor Contributor
889 Views
3 Replies
Message 1 of 4

iLogic vector intersection point

thomas_wegerBYFE5
Contributor
Contributor

Hello,
I have trouble to find the intersection point of two working axes which I created.
I would like to use the point in a sketch later.
I was expecting to find a method in the line object...but I could not. Maybe I oversaw it?
Here is my code so far, which is not working...and my math class days are quite a bit ago...(plus I would like with inventor methods where it is possible.) 

Attached also my part becuase I maybe overcomplicating things(?)....the rule "CreateAssembly" is creating an ipt file. On the intersection of the volumes I need to have a cut out feature. After that I create with "CreateMultiBody" the assembly. My first attemp was to bottom up but with top down I expect to handle modifications in the ipt faster and once the configuration is done I produce the assembly parts. 

Sub main()
' Get the active document
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

' Get the component definition (part)
Dim oCompDef As ComponentDefinition
oCompDef = oDoc.ComponentDefinition

' Get references to the two work axes by name
Dim oAxis1 As WorkAxis
Dim oAxis2 As WorkAxis

oAxis1 = oCompDef.WorkAxes.Item("Arbeitsachse1") ' Replace with the name of your first work axis
oAxis2 = oCompDef.WorkAxes.Item("Arbeitsachse2") ' Replace with the name of your second work axis

' Get the geometry of the work axes
Dim oLine1 As Line
Dim oLine2 As Line

oLine1 = oAxis1.Line
oLine2 = oAxis2.Line

' Call the function to find the intersection point
Dim intersectionPt As Point
intersectionPt = FindIntersectionPoint(oLine1, oLine2)

' Output the intersection point
If intersectionPt IsNot Nothing Then
    MsgBox("Intersection Point: X=" & intersectionPt.X & ", Y=" & intersectionPt.Y & ", Z=" & intersectionPt.Z)
Else
    MsgBox("No intersection point found.")
End If


End Sub

' Function to calculate the intersection point between two lines
Function FindIntersectionPoint(line1 As Line, line2 As Line) As Point
    ' Get the direction vectors of the lines
    Dim dir1 As Vector
    Dim dir2 As Vector
    dir1 = line1.Direction.AsVector
    dir2 = line2.Direction.AsVector

    ' Get the point on the first line (origin)
    Dim pt1 As Point
    pt1 = line1.RootPoint

    ' Get the point on the second line (origin)
    Dim pt2 As Point
    pt2 = line2.RootPoint

    ' Calculate the cross product of the direction vectors
    Dim crossDir As Vector
    crossDir = dir1.CrossProduct(dir2)

    ' If the cross product is nearly zero, the lines are parallel or coincident
    If crossDir.Length < 0.000001 Then
        MsgBox("The lines are parallel or coincident, no unique intersection point.")
        Return Nothing
    End If


    ' Calculate the intersection point using the parameter of the first line
    Dim t As Double
    t = (pt2 - pt1).DotProduct(crossDir) / dir1.DotProduct(crossDir)

    ' Calculate the intersection point
    Dim intersectionPoint As Point
    intersectionPoint = pt1 + dir1 * t

    Return intersectionPoint
End 

 

0 Likes
Accepted solutions (1)
890 Views
3 Replies
Replies (3)
Message 2 of 4

J-Camper
Advisor
Advisor
Accepted solution

@thomas_wegerBYFE5,

Line Objects have an "IntersectionWithCurve" Method, which should give you what you want.

0 Likes
Message 3 of 4

thomas_wegerBYFE5
Contributor
Contributor

Thank you for your quick reply! Your solution worked perfectly. 

Here the modified code as a example:

Sub main()
' Get the active document
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

' Get the component definition (part)
Dim oCompDef As ComponentDefinition
oCompDef = oDoc.ComponentDefinition

' Get references to the two work axes by name
Dim oAxis1 As WorkAxis
Dim oAxis2 As WorkAxis

oAxis1 = oCompDef.WorkAxes.Item("Arbeitsachse1") ' Replace with the name of your first work axis
oAxis2 = oCompDef.WorkAxes.Item("Arbeitsachse14") ' Replace with the name of your second work axis

' Get the geometry of the work axes
Dim oLine1 As Line
Dim oLine2 As Line

oLine1 = oAxis1.Line
oLine2 = oAxis2.Line

Dim crossingPointEnum As ObjectsEnumerator
crossingPointEnum = oLine1.IntersectWithCurve(oLine2)

Dim xPoint As Point

'Get the first crossing point
xPoint = crossingPointEnum.Item(1) 

' Output the intersection point
If crossingPointEnum IsNot Nothing Then
    MsgBox("XPoint" & xPoint.X.ToString & "YPoint" & xPoint.Y.ToString & "ZPoint" & xPoint.Z.ToString)
Else
    MsgBox("No intersection point found.")
End If
End Sub
0 Likes
Message 4 of 4

_dscholtes_
Advocate
Advocate

@thomas_wegerBYFE5 wrote:

 

<SNIP>
    ' If the cross product is nearly zero, the lines are parallel or coincident
    If crossDir.Length < 0.000001 Then
        MsgBox("The lines are parallel or coincident, no unique intersection point.")
        Return Nothing
    End If
</SNIP>

 


Side-note: Did you know that the vector object has an IsParallelTo method which you can use to easily check if two vectors are parallel? (easily meaning no calculation of cross-product and no checking of length against a very small value/tolerance).