Sub CATMain()
'------------------------------------------------------------------------------------------
Dim oSourceDocument As PartDocument
Dim oSourceDocumentWindow As Window
Dim oTargetAssemblyProductDocument As ProductDocument
Dim oTargetAssemblyRootProduct As Product
Dim oTargetAssemblyWindow As Window
Dim oSourcePart As Part
Dim oBodyToCopy As Body
Dim nameLength As Long
Dim vBody As Variant
Dim bCopy As Boolean
'------------------------------------------------------------------------------------------
bCopyBodiesUsedInBoolean = False
' bCopyBodiesUsedInBoolean = True
'------------------------------------------------------------------------------------------
Set oSourceDocument = CATIA.ActiveDocument
Set oSourceDocumentWindow = GetWindowByname(oSourceDocument.Name)
'------------------------------------------------------------------------------------------
Set oTargetAssemblyProductDocument = CATIA.Documents.Add("Product")
Set oTargetAssemblyRootProduct = oTargetAssemblyProductDocument.Product
'------------------------------------------------------------------------------------------
Call oTargetAssemblyRootProduct.ApplyWorkMode(DESIGN_MODE) ' Make sure Design Mode is Applied
'------------------------------------------------------------------------------------------
Set oTargetAssemblyWindow = CATIA.Windows.item(CATIA.Windows.Count) ' As this will be the Last Window
' or
'Set oSourceDocumentWindow = GetWindowByname(oTargetAssemblyProductDocument.Name)
'------------------------------------------------------------------------------------------
Call CATIA.Windows.Arrange(catArrangeTiledVertical)
'------------------------------------------------------------------------------------------
partName = oSourceDocument.Name
nameLength = InStr(1, partName, ".CATPart")
oTargetAssemblyRootProduct.PartNumber = Mid(partName, 1, nameLength - 1)
'------------------------------------------------------------------------------------------
'oSourceDocumentWindow.Activate 'Not Required To Activate
'------------------------------------------------------------------------------------------
Set oSourcePart = oSourceDocument.Part
'------------------------------------------------------------------------------------------
For Each vBody In oSourcePart.Bodies
'------------------------------------------------------------------------------------------
Set oBodyToCopy = vBody
'------------------------------------------------------------------------------------------
bCopy = False
bCopy = CBool(CStr(oBodyToCopy.InBooleanOperation) = "False") Or bCopy
bCopy = CBool(CBool(CStr(oBodyToCopy.InBooleanOperation) = "True") And bCopyBodiesUsedInBoolean) Or bCopy
'------------------------------------------------------------------------------------------
If bCopy Then
'------------------------------------------------------------------------------------------
Dim newProductInTargetAssembly As Product
Set newProductInTargetAssembly = oTargetAssemblyRootProduct.Products.AddNewComponent("Part", oBodyToCopy.Name)
Set oTargetDocumentPart = newProductInTargetAssembly.ReferenceProduct.Parent.Part
'------------------------------------------------------------------------------------------
'Copy Body from SourceDocument
'------------------------------------------------------------------------------------------
Call oSourceDocument.Selection.Clear
Call oSourceDocument.Selection.Add(oBodyToCopy)
Call oSourceDocument.Selection.Copy
'------------------------------------------------------------------------------------------
' First select the new PartDocument Added
Call oTargetAssemblyProductDocument.Selection.Clear
Call oTargetAssemblyProductDocument.Selection.Add(oTargetDocumentPart)
'------------------------------------------------------------------------------------------
' Paste the Body in the new PartDocument Added
Call oTargetAssemblyProductDocument.Selection.PasteSpecial("CATPrtResult")
'------------------------------------------------------------------------------------------
Call oTargetAssemblyProductDocument.Selection.Clear
Call oTargetDocumentPart.Update
Call newProductInTargetAssembly.Update
'------------------------------------------------------------------------------------------
End If
Next
'------------------------------------------------------------------------------------------
Call oSourceDocument.Selection.Clear
Call oTargetAssemblyProductDocument.Selection.Clear
'------------------------------------------------------------------------------------------
'------------------------------------------------------------------------------------------
oTargetAssemblyWindow.WindowState = catWindowStateMaximized
Call oTargetAssemblyRootProduct.Update
Call oTargetAssemblyWindow.ActiveViewer.Reframe
Call oTargetAssemblyWindow.Activate
'------------------------------------------------------------------------------------------
End Sub
Private Function GetWindowByname(ByVal nameOfWindowToSearch As String)
Dim oFoundWindow As Window
'------------------------------------------------------------------------------------------
Set oFoundWindow = Nothing
'------------------------------------------------------------------------------------------
On Error Resume Next
Set oFoundWindow = CATIA.Windows.GetItem(nameOfWindowToSearch)
On Error GoTo 0
'------------------------------------------------------------------------------------------
If (oFoundWindow Is Nothing) Then
For Each vWindow In CATIA.Windows
Debug.Print vWindow.Name
If (vWindow.Name = nameOfWindowToSearch) Then
Set oFoundWindow = vWindow
Exit For
End If
Next
End If
'------------------------------------------------------------------------------------------
Set GetWindowByname = oFoundWindow
'------------------------------------------------------------------------------------------
End Function