 熟悉Inventor的朋友们都知道,部件中的镜像功能其实很鸡肋,其实很多部件是可以阵列并保持约束的。我们通常的干法可能是先做一次带镜像的衍生,然后再使用对称约束的功能,比较麻烦。以下代码提供了较好的办法。
- 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:tempmirrorPart.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
|
|