'Load a product with 2 parts
'Select published body in Part1
'Run macro
'Will link the selected body to second part and add the new body to the partbody
Sub CATMain()
Dim oProductDocument as ProductDocument
Dim oProduct As Product
'Dim oPart1 As Part
Dim oPart2 As Part
Dim oSelection as Selection
Dim sBodyName as String
Dim oNewBody as Body
Dim oShapeFactory as Factory
Dim oRemove 'as Remove ???Not sure of the item type????
On Error Resume Next 'Do not show errors
Err.Clear 'Clear errors
Set oProductDocument = CATIA.ActiveDocument 'Try to set the product document
If Err.Number <> 0 then 'the open document is not a product
Msgbox "You must open a Product to continue"
End sub 'Closes macro
Else
Set oProduct = oProductDocument.Product 'Set the product variable
Set oSelection = oProductDocument.Selection
'Set oPart1 = oProduct.Products.Item(1).ReferenceProduct.Parent.Part
Set oPart2 = oProduct.Products.Item(2).ReferenceProduct.Parent.Part
Set oShapeFactory = oPart2.ShapeFactory
End if
Err.Clear 'Clear errors
'If there is one thing selected and its type is a Body or PartBody
If oSelection.Count = 1 and Instr(TypeName(oSelection.Item(1).Value), "Body") <> 0 then
'Need the name to find the body when it is pasted in Part2
'Will not work if you have multiple bodies with the same name!
sBodyName = oSelection.Item(1).Value.Name
CATIA.ActiveDocument.Selection.Copy 'Copy the body
CATIA.ActiveDocument.Selection.Clear 'Clear the selection
CATIA.ActiveDocument.Selection.Add oPart2 'Select Part2
CATIA.ActiveDocument.Selection.PasteSpecial "CATPrtResult" 'Paste with link
If Err.Number <> 0 Then 'body was not published and could not be pasted with link
MsgBox "Body must be published to continue"
End sub 'Closes macro
End If
End if
On Error GoTo 0 'show errors
'Find the new body in Part2
Set oNewbody = oPart2.FindObjectByName sBodyName
'When you paste the body it will be the InWorkObject
oPart2.InWorkObject = oPart2.Bodies.Item(1) 'Set the PartBody as the In work object
Set oRemove = oShapeFactory.AddNewRemove(oNewbody) 'add new body to part body in Part 2
End Sub