Question:
API allows me to create mirror feature in a part, but it looks not way to create mirror component in an assembly. Is there any method?
Solution:
This is no direct API function available for this task, but if you observe the built-in behavior of Inventor, you can find the mirror component is actually a derived part from the parent part. So you could create a new file (the derived component) from the parent part, insert the derived component by mirroring about the selected plane.
The challenge is the mirror matrix. There are abundant articles about mirroring(also called reflect transform) on internet. Here is one article I referred to
http://ami.ektf.hu/uploads/papers/finalpdf/AMI_40_from175to186.pdf
and, it indicates the mirror matrix is:
Where, Cx, Cy Cz are the normallized values of the workplane’s normal.
But, it does not consider the translation of the object off the plane. We could just multiply the transformation of the parent component. The following is a small code demo. It assumes a work plane is selected. It mirrors the first component.
Sub MirrorPartInAss()
Dim oAssDoc As AssemblyDocument
Set oAssDoc = ThisApplication.ActiveDocument
'mirror plane
Dim oMirrorWP As WorkPlane
Set oMirrorWP = oAssDoc.SelectSet(1)
Dim oPlane As Plane
Set oPlane = oMirrorWP.Plane
'get normal of the plane
Dim oNormalX As Double
oNormalX = oPlane.Normal.X
Dim oNormalY As Double
oNormalY = oPlane.Normal.Y
Dim oNormalZ As Double
oNormalZ = oPlane.Normal.Z
'create the mirroring matrix
Dim oMirrorMatrix As Matrix
Set oMirrorMatrix = ThisApplication.TransientGeometry.CreateMatrix()
Dim oMatrixData(15) As Double
oMatrixData(0) = 1 - 2 * oNormalX * oNormalX
oMatrixData(1) = -2 * oNormalX * oNormalY
oMatrixData(2) = -2 * oNormalX * oNormalZ
oMatrixData(3) = 0
oMatrixData(4) = -2 * oNormalX * oNormalY
oMatrixData(5) = 1 - 2 * oNormalY * oNormalY
oMatrixData(6) = -2 * oNormalZ * oNormalY
oMatrixData(7) = 0
oMatrixData(8) = -2 * oNormalX * oNormalZ
oMatrixData(9) = -2 * oNormalZ * oNormalY
oMatrixData(10) = 1 - 2 * oNormalZ * oNormalZ
oMatrixData(11) = 0
oMatrixData(12) = 0
oMatrixData(13) = 0
oMatrixData(14) = 0
oMatrixData(15) = 1
Call oMirrorMatrix.PutMatrixData(oMatrixData)
'get the first component
Dim oOcc As ComponentOccurrence
Set oOcc = oAssDoc.ComponentDefinition.Occurrences(1)
'multiply with the transformation of the parent component
oMirrorMatrix.PostMultiplyBy oOcc.Transformation
Dim oParentPartPath As String
oParentPartPath = oOcc.Definition.Document.FullFileName
' Create a new part file to derive the part in.
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
' Create a derived definition for the part.
Dim oDerivedPartDef As DerivedPartTransformDef
Set oDerivedPartDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.CreateTransformDef(oParentPartPath)
' Create the derived part.
Call oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Add(oDerivedPartDef)
'save the derived part.
Call oPartDoc.SaveAs("c:\temp\mirrorPart.ipt", False)
'add the derived part as a component
Call oAssDoc.ComponentDefinition.Occurrences.Add(oPartDoc.FullFileName, oMirrorMatrix)
oPartDoc.Close
'activate the assembly document
oAssDoc.Activate
End Sub