Public Sub CreateAssy()
ThisApplication.SilentOperation = True
' Open the existing sample assembly.
Dim ptDoc As PartDocument
Set ptDoc = ThisApplication.ActiveDocument
' Set a reference to the transient geometry object.
'Transient Geometry needed to create new position/rotation matrix
Dim oTG As TransientGeometry
Set oTG = ThisApplication.TransientGeometry
' Create a position/rotation matrix. All positions/rotations set to origin.
Dim oMatrix As Matrix
Set oMatrix = oTG.CreateMatrix
Dim ptcdDef As PartComponentDefinition
Set ptcdDef = ptDoc.ComponentDefinition
'declare and set variables to copy the occurrence's location and name
Dim strFilePath As String
Dim strFileName As String
strFilePath = ptDoc.FullFileName
strFileName = ptDoc.ComponentDefinition.Document.DisplayName
strFilePath = Replace(strFilePath, strFileName, "")
strFileName = Replace(strFileName, ".ipt", "")
'declare and set variables for the full file name of parts to be created
Dim strGroupFileName As String
Dim sbBody As SurfaceBody
Dim sbsBodies As SurfaceBodies
Set sbsBodies = ptcdDef.SurfaceBodies
Dim ocGroup As ObjectCollection
Set ocGroup = ThisApplication.TransientObjects.CreateObjectCollection
Dim lngOccCount As Long
Dim lngGroupCount As Long
' Create a part for each collection of solids.
Dim pdTemp As PartDocument
Dim adAssy As AssemblyDocument
Dim coBase As ComponentOccurrence
Dim coCopy2 As ComponentOccurrence
If sbsBodies.Count > 50 Then
'Create and save new assembly housing the three parts
Set adAssy = ThisApplication.Documents.Add(kAssemblyDocumentObject, _
ThisApplication.FileManager.GetTemplateFile _
(kAssemblyDocumentObject), True)
Call adAssy.SaveAs(strFilePath & strFileName & ".iam", False)
Call adAssy.ComponentDefinition.Occurrences.Add(ptDoc.FullFileName, oMatrix)
Set coBase = adAssy.ComponentDefinition.Occurrences.Item(1)
Dim lngPtCount As Long
lngPtCount = CLng(Fix(sbsBodies.Count / 50))
Dim lngCount As Long
For lngOccCount = 1 To lngPtCount
strGroupFileName = strFilePath & strFileName & "_" & lngOccCount & ".ipt"
Set pdTemp = ThisApplication.Documents.Add(kPartDocumentObject, _
ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
Call pdTemp.SaveAs(strGroupFileName, True)
Call adAssy.ComponentDefinition.Occurrences.Add(strGroupFileName, oMatrix)
Set coCopy2 = adAssy.ComponentDefinition.Occurrences.Item(1 + lngOccCount)
Call BodyCopy(coBase, coCopy2, lngOccCount * 50, lngOccCount * 50 - 49)
' Call pdTemp.Save
pdTemp.Close (True)
Next lngOccCount
adAssy.Update
ThisApplication.ActiveView.Update
Call adAssy.Save
Call adAssy.Close
End If
End Sub
Sub BodyCopy(coPart As ComponentOccurrence, _
coCopyObject As ComponentOccurrence, _
lngHigh As Long, lngLow As Long)
' Get the component definition of the base part.
Dim baseDef As PartComponentDefinition
Set baseDef = coPart.Definition
' Get the component definition of the base part.
Dim pcdCopy As PartComponentDefinition
Set pcdCopy = coCopyObject.Definition
'** Create an associative surface base feature in the second part.
' Create a definition object in the context of the first part.
Dim baseFeatureDef As NonParametricBaseFeatureDefinition
Set baseFeatureDef = baseDef.Features.NonParametricBaseFeatures.CreateDefinition
' Add the body of the second part to the list of items to be copied. Since this
' is getting the body from the occurrence it is actually a SurfaceBodyProxy
' object in the context of the assembly.
Dim bodyColl As ObjectCollection
Set bodyColl = ThisApplication.TransientObjects.CreateObjectCollection
Dim sbTemp As SurfaceBody
Dim lngI As Long
Dim sbpSourceBodyProxy As SurfaceBodyProxy
Dim baseFeature As NonParametricBaseFeature
For lngI = lngLow To lngHigh
Set sbTemp = baseDef.SurfaceBodies.Item(lngI)
Call coPart.CreateGeometryProxy(sbTemp, sbpSourceBodyProxy)
bodyColl.Add sbpSourceBodyProxy
' Set up the definition object. When setting the IsAssociative flag to True, the
' Output type must be either a Surface or Composite. A solid is not valid in that case.
baseFeatureDef.BRepEntities = bodyColl
baseFeatureDef.OutputType = kSolidOutputType
baseFeatureDef.TargetOccurrence = coCopyObject
'baseFeatureDef.IsAssociative = True
' Create the associative copy.
Set baseFeature = pcdCopy.Features.NonParametricBaseFeatures.AddByDefinition(baseFeatureDef)
bodyColl.Clear
Next lngI
End Sub
For anyone who can gain from it, here's what I ended up with.