Sub CATMain()
Dim rootProduct As Product
Set rootProduct = CATIA.ActiveDocument.Product
'first check if document is part or product
If InStr(CATIA.ActiveDocument.Name, ".CATProduct") < 1 Then
MsgBox rootProduct.Name & " Is NOT Assembly!"
Exit Sub
End If
Dim PartsCount As Integer
PartsCount = rootProduct.Products.Count 'count of parts and instances to use
'Enter name od new Part
Dim newPartName As String
newPartName = InputBox("Please enter new Part name.")
Dim newPart As Product
Set newPart = rootProduct.Products.AddNewComponent("Part", newPartName) 'create part paste in it
Dim targetPart As Part
Set targetPart = newPart.ReferenceProduct.Parent.Part
'create Geometical set in new part
'Dim geomSetTarget As HybridBody
'Set geomSetTarget = targetPart.HybridBodies.Add
'geomSetTarget.Name = "CopyOfReferences"
Dim vp As VisPropertySet
'loop trough parts
For i = 1 To PartsCount
Set instance = rootProduct.Products.Item(i)
'check if part is visible, so it takes only visible parts
If isVisible(instance) Then
'count published elements which will be copied
Dim publCount As Integer
publCount = rootProduct.Products.Item(i).Publications.Count
Dim selectedEL As Selection
Set selectedEL = CATIA.ActiveDocument.Selection
selectedEL.Clear
'loop trough published elements in every part
For j = 1 To publCount
Set publishedEL = rootProduct.Products.Item(i).Publications.Item(j)
selectedEL.Add publishedEL
selectedEL.Copy
selectedEL.Clear
selectedEL.Add targetPart
'uncomment if paste WITHOUT link
'selectedEL.PasteSpecial ("CATPrtResultWithOutLink")
'paste elements WITH link
selectedEL.PasteSpecial ("CATPrtResult")
selectedEL.Clear
Next
Else
'Debug.Print (instance.Name & " is NOT visible")
End If
Next
newPart.Update
'--------------------------------------------------------------------------------------
'Section for retreiving positions and rotation angle fo elements
Dim axisCount As Integer
Dim LinesCount As Integer
Dim Xaxis1 As Double
Dim Xaxis2 As Double
LinesCount = targetPart.HybridBodies.HybridBody.Item(1).GeometricElements.Count
Debug.Print LinesCount
'counting axissystems in new part
axisCount = targetPart.AxisSystems.Count
'Loop trough axissystems to retreive components
For i = 1 To axisCount
Set ASys = targetPart.AxisSystems.Item(i)
Set Oref = targetPart.CreateReferenceFromObject(ASys)
Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
Set TheMeasurable = TheSPAWorkbench.GetMeasurable(Oref)
Dim Components(11)
TheMeasurable.GetAxisSystem Components
Components(0) = Round(Components(0), 3)
Components(1) = Round(Components(1), 3)
Components(2) = Round(Components(2), 3)
Components(3) = Round(Components(3), 4)
Components(6) = Round(Components(6), 4)
Debug.Print targetPart.AxisSystems.Item(i).Name & " x "; Components(0) & " y "; Components(1) & " z"; Components(2)
Xaxis1 = Components(3)
Xaxis2 = Components(6)
If (Xaxis1 = 1) And (Xaxis2 = 0) Then 'Parallel to X
Debug.Print "Parallel to X"
ElseIf (Xaxis1 = -1) And (Xaxis2 = 0) Then 'Parallel to X 180° rotated
Debug.Print "Parallel to X 180° rotated"
ElseIf (Xaxis1 = 0) And (Xaxis2 = -1) Then '+90° rotarted
Debug.Print "+90° rotarted"
ElseIf (Xaxis1 = 0) And (Xaxis2 = 1) Then '-90° rotarted
Debug.Print "-90° rotarted"
ElseIf (Xaxis1 > 0) And (Xaxis2 < 0) Then 'First quadrant
Debug.Print "First quadrant"
ElseIf (Xaxis1 < 0) And (Xaxis2 < 0) Then 'Second quadrant
Debug.Print "Second quadrant"
ElseIf (Xaxis1 < 0) And (Xaxis2 > 0) Then 'Third quadrant
Debug.Print "Third quadrant"
ElseIf (Xaxis1 > 0) And (Xaxis2 > 0) Then 'Fourth quadrant
Debug.Print "Fourth quadrant"
End If
Next
End Sub
Function isVisible(ByRef object As Variant) As Boolean 'function for cecking hide/show of part
CATIA.ActiveDocument.Selection.Clear
CATIA.ActiveDocument.Selection.Add object
Dim vp As VisPropertySet
Dim showstate As CatVisPropertyStatus
Set vp = CATIA.ActiveDocument.Selection.VisProperties
vp.GetShow showstate
result = True
If showstate = catVisPropertyNoShowAttr Then result = False
CATIA.ActiveDocument.Selection.Clear
isVisible = result
End Function