对装配中组件进行镜像

来源:互联网 发布:美剧网络犯罪调查 编辑:程序博客网 时间:2024/04/20 06:11

大家新年好!

本想开年写点啥,正好今天遇到一位用户询问我,如何对装配中组件进行镜像。前面我们知道,API提供了零件中镜像特征或实体的方法,但看上去没有装配中组件镜像的方法。不过如果观察Inventor本身的镜像,可以发现,其实首先是对原零件做了个衍生件,然后插入这个衍生件,根据镜像面做变换。最难的是这个镜像变换。我没看到API给我们写了现成的。所以从网上找到算法,写了个小例子。

这个例子假定装配中拾取了一个工作面,它将对第一个组件进行围绕这个面进行镜像。希望对大家有所帮助。

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.ActivateEnd Sub




0 0
原创粉丝点击