+发表新主题
爱梦颜泥 发布于7 天前 22 次浏览 2 位用户参与讨论
跳转到指定楼层
熟悉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  
回复

使用道具 举报

已有2人评论

soarzfz 发表于 6 天前
多谢楼主分享代码。
在AIP 2011中尝试了下,即使对于单个零件的镜像也保持不了约束关系。能否请楼主详细说下步骤?
回复

使用道具 举报

a510124011 发表于 5 天前
多谢楼主!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ| Archiver|手机版|小黑屋| 碧波制图网 Published by Stonespider

Copyright © 2021-2023 Kangli Wu   All Rights Reserved.

Powered by Discuz! X3.5( 苏ICP备18011607号-1 )

快速
回复
返回
列表
返回
顶部