By Brian Ekins
Question: The “Associative body copy API Sample” in the API help doesn’t work correctly when creating a non-associative solid copy of a body. The body copy is not positioned correctly.
Answer: There’s a problem in Inventor where the transformation defined by an occurrence in the assembly is not being taken into account when a SurfaceBodyProxy is copied.
The following is an updated version of the original sample that has a simpler UI and also takes the issue described above into account.
' Set a reference to the active assembly document.
Dim asmDoc As AssemblyDocument
Set asmDoc = ThisApplication.ActiveDocument
Dim asmDef As AssemblyComponentDefinition
Set asmDef = asmDoc.ComponentDefinition
' Select the body to copy. This will be a proxy since it's
' being selected in the context of the assembly.
Dim sourceBody As SurfaceBodyProxy
Set sourceBody = ThisApplication.CommandManager.Pick( _
kPartBodyFilter, _
"Select a body to copy.")
' Get the occurrence to create the new body within.
Dim targetOcc As ComponentOccurrence
Set targetOcc = ThisApplication.CommandManager.Pick( _
kAssemblyLeafOccurrenceFilter, _
"Select an occurrence to copy the body into.")
Dim targetDef As PartComponentDefinition
Set targetDef = targetOcc.Definition
' Create a base feature definition. This is used to define the
' various inputs needed to create a base feature.
Dim nonPrmFeatures As NonParametricBaseFeatures
Set nonPrmFeatures = targetDef.Features.NonParametricBaseFeatures
Dim featureDef As NonParametricBaseFeatureDefinition
Set featureDef = nonPrmFeatures.CreateDefinition
Dim transObjs As TransientObjects
Set transObjs = ThisApplication.TransientObjects
Dim col As ObjectCollection
' Ask if an associative or non-associative copy should be made.
Dim answer As VbMsgBoxResult
answer = MsgBox("Choose the type of copy to create." & _
vbCrLf & vbCrLf & _
" Yes - Associative surface" & vbCrLf & _
" No - Non-associative solid", vbYesNoCancel)
' Define the geometry to be copied. In this case,
' it's the selected body.
Set col = transObjs.CreateObjectCollection
col.Add sourceBody
' This creates an associative copy of the model. To
' create an associative copy inventor only supports creating
' a surface of composite result, not a solid.
featureDef.BRepEntities = col
featureDef.OutputType = kSurfaceOutputType
featureDef.TargetOccurrence = targetOcc
featureDef.IsAssociative = True
Dim baseFeature As NonParametricBaseFeature
Set baseFeature = nonPrmFeatures.AddByDefinition(featureDef)
ElseIf answer = vbNo Then
' The selected body is a body proxy in the context of
' the assembly. However, there's a problem with the
' body that ignores the transorm. The code below creates
' the copy and then performs an extra step to apply the
' transform.
Dim newBody As SurfaceBody
Set newBody = ThisApplication.TransientBRep.Copy(sourceBody)
Call ThisApplication.TransientBRep.Transform(newBody, _
sourceBody.ContainingOccurrence.Transformation)
' Transform the body into the parts space of the
' target occurrence.
Dim trans As Matrix
Set trans = targetOcc.Transformation
trans.Invert
Call ThisApplication.TransientBRep.Transform(newBody, trans)
Set col = transObjs.CreateObjectCollection
col.Add newBody
' This creates an non-associative copy that is a solid.
featureDef.BRepEntities = col
featureDef.OutputType = kSolidOutputType
featureDef.TargetOccurrence = targetOcc
featureDef.IsAssociative = False
Set baseFeature = nonPrmFeatures.AddByDefinition(featureDef)
End If
End Sub