It's been quite the challenge, but I've managed to piece together a routine that will find via iLogic the point of intersection of three planes within an Assembly file. This investigation highlighted for me errors in matrix return results (at least from what would be expected) from Inventor as well as incomplete documentation of the API help menu's and a serious lack of good examples to follow. To contribute towards a solution, I am including the results of my efforts for all to share.
Public Enum enCoordinate
x = 1
y = 2
z = 3
End Enum
Sub Main
Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oAsmCompDef As ComponentDefinition = oDoc.ComponentDefinition
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim oAssyPlane(3) As WorkPlane
'Select existing work planes within current assembly file
oAssyPlane(1) = oAsmCompDef.WorkPlanes.Item("Plane 1")
oAssyPlane(2) = oAsmCompDef.WorkPlanes.Item("Plane 2")
oAssyPlane(3) = oAsmCompDef.WorkPlanes.Item("Plane 3")
Dim rootPt(3) As Double
Dim normal(3) As Double
Dim planeEqs(3) As Point
Dim d(3) As Double
'Get equation of plane reference https://keisan.casio.com/exec/system/1223596129
For i = 1 To 3 'Loop for three planes
' Get the plane root point and normal vector which defines the equation of the plane
Call oAssyPlane(i).Plane.GetPlaneData(rootPt, normal)
' Define the root point of the plane
Dim xRoot As Double: xRoot = rootPt(0)
Dim yRoot As Double: yRoot = rootPt(1)
Dim zRoot As Double: zRoot = rootPt(2)
' Define the normal vector of the plane. This defines the equation of the plane.
Dim xNormal As Double: xNormal = normal(0)
Dim yNormal As Double: yNormal = normal(1)
Dim zNormal As Double: zNormal = normal(2)
d(i) = (xNormal * xRoot) + (yNormal * yRoot) + (zNormal * zRoot)
' The equation of the plane
planeEqs(i) = oTG.CreatePoint(xNormal, yNormal, zNormal)
Next 'End loop
Dim intersectPt As Point
intersectPt = GetIntersectionOfThreePlanes(planeEqs, d)
Dim interPt As Point = oTG.CreatePoint(intersectPt.X, intersectPt.Y, intersectPt.Z )
oPt1 = oAsmCompDef.WorkPoints.AddFixed(interPt, False) 'False is undocumented & doesn't work without. Found in a random web search.
oPt1.Name = "POI"
oPt1.Grounded = True
End Sub
' *** Compute intersection point given the equation of three planes.
' *** Plane Equation format: ax + by + cz = d
' *** Where:
' *** planeEQ must contain the coefficients (a, b, c) of the equation of each plane
' *** d is the equation value
' *** Returns the intersection point
' *** See URL: https://keisan.casio.com/exec/system/1223596129
Function GetIntersectionOfThreePlanes(planeEq() As Point, d() As Double) As Point
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim N As Integer: N = UBound(d)
Dim matrx As Matrix2d = oTG.CreateMatrix2d
Dim Row As Integer
Dim col As Integer
For Row = 1 To N
Dim coeffs As Point: coeffs = planeEq(Row)
Dim pt As Double
For col = 1 To N
If col = 1 Then
pt = coeffs.X
ElseIf col = 2 Then
pt = coeffs.Y
ElseIf col = 3 Then
pt = coeffs.Z
End If
matrx.Cell(Row, col) = pt
Next col
Next Row
' *** Get the determinant of the main matrix before any substitution
Dim deter As Double
deter = Determinant(matrx) ' matrx.Determinant
' !!! If Determinant = 0, there is no intersection point.
If deter = 0 Then GoTo errHandler
Dim x As Double: x = ComputeCoordinate(matrx, d, enCoordinate.x, deter)
Dim y As Double: y = ComputeCoordinate(matrx, d, enCoordinate.y, deter)
Dim z As Double: z = ComputeCoordinate(matrx, d, enCoordinate.z, deter)
' *** The intersection point.
GetIntersectionOfThreePlanes = oTG.CreatePoint(x, y, z)
Return GetIntersectionOfThreePlanes
errHandler:
MsgBox("The planes do not intersect at a point")
End Function
' *** *************************************************************************
' ***
' *** Compute the X, Y, or Z coordinate given the Matrix containing the
' *** coefficients of the equation for each plane.
' ***
' *** Returns the x, y, or z coordindate of the intersection point
' ***
' *** See URL: http://www.ambrsoft.com/TrigoCalc/Plan3D/3PlanesIntersection_.htm
' ***
Function ComputeCoordinate(m As Matrix2d, d() As Double, rp As enCoordinate, determinat As Double) As Double
Try
Dim N As Integer: N = UBound(d)
Dim oTG As TransientGeometry = ThisApplication.TransientGeometry
Dim newMatrix As Matrix2d = oTG.CreateMatrix2d
' *** Find coordinate
Dim row As Integer
Dim col As Integer
For row = 1 To N
For col = 1 To N
If col = rp Then
newMatrix.Cell(row, col) = d(row)
Else
newMatrix.Cell(row, col) = m.Cell(row, col)
End If
Next col
Next row
Dim newDeter As Double: newDeter = Determinant(newMatrix) ' newMatrix.Determinant
ComputeCoordinate = newDeter / determinat
Catch Ex As Exception
MsgBox("Exception: " & Ex.Message, "iLogic: ComputeCoordinate")
End Try
Return ComputeCoordinate
End Function
' *** *************************************************************************
' ***
' *** Compute the Determinant of a matrix
' *** The Matrix2d.Determinant does NOT return the correct value.
' ***
' *** Returns the Determinant for the matrix.
' ***
' *** See URL: http://www.ambrsoft.com/MathCalc/Matrix/Matrix3.htm#determinants
' ***
Function Determinant(ByVal src As Matrix2d) As Double
Dim a11 As Double = src.Cell(1, 1)
Dim a12 As Double = src.Cell(1, 2)
Dim a13 As Double = src.Cell(1, 3)
Dim a21 As Double = src.Cell(2, 1)
Dim a22 As Double = src.Cell(2, 2)
Dim a23 As Double = src.Cell(2, 3)
Dim a31 As Double = src.Cell(3, 1)
Dim a32 As Double = src.Cell(3, 2)
Dim a33 As Double = src.Cell(3, 3)
Dim pt1 As Double = a11 * (a22*a33 - a23*a32)
Dim pt2 As Double = a12 * (a21*a33 - a23*a31)
Dim pt3 As Double = a13 * (a21*a32 - a22*a31)
Dim result As Double = pt1 - pt2 + pt3
Return result
End Function
Autodesk Inventor Certified Professional