Code:
Sub test()Dim NC_Parts As Selection
'Dim m As Integer
Dim part2 As Part
Dim referencepart As Part
Dim bodies2 As bodies
Dim body2 As body
Dim NC_coords(8)
Dim sketch 'As sketch
Dim measuredplane As HybridShapePlane2Lines
Dim reference1 As Reference
Dim reference2 As Reference
Dim reference3 As Reference
Dim reference4 As Reference
Dim angle_XY As Double
Dim angle_ZX As Double
Dim angle_YZ As Double
Dim Plaszczyzna_NC As Plane
Dim TheSpaWorkbench As Workbench
Dim Themeasurable As Measurable
Set NC_Parts = CATIA.ActiveDocument.Selection
NC_Parts.Search "(Name=*konturstueck* & CATPrtSearch.PartFeature),all"
Set part2 = NC_Parts.Item(3).Value 'fixed to item3, which is on purpose rotated 30 degrees around Y-axis of the car zero Axis system
Set bodies2 = part2.bodies
Set body2 = bodies2.Item("Funktionsteil")
Set sketch = body2.Sketches.Item(1)
Dim hybridshapefactory As hybridshapefactory
Set hybridshapefactory = part2.hybridshapefactory
Dim hybridBodies2 As hybridbodies
Set hybridBodies2 = part2.hybridbodies
Dim hybridBody2 As HybridBody
Set hybridBody2 = hybridBodies2.Add()
hybridBody2.Name = "NEW GEOMETRICAL SET"
'sets the newly created geometrical set as the in work object
part2.InWorkObject = hybridBody2
part2.Update
'sketch is located on the plane which is to be measured
Set reference1 = part2.CreateReferenceFromGeometry(sketch.AbsoluteAxis.HorizontalReference)
Set reference2 = part2.CreateReferenceFromGeometry(sketch.AbsoluteAxis.VerticalReference)
Set measuredplane = hybridshapefactory.AddNewPlane2Lines(reference1, reference2)
measuredplane.Name = "measuredplane"
hybridBody2.AppendHybridShape measuredplane
part2.Update
Dim productDocument2 As ProductDocument
Set productDocument2 = CATIA.ActiveDocument
Dim selection2 As Selection
Set selection2 = productDocument2.Selection
selection2.Search "Name=*adapter,all" 'adapter is another part, which is locatated always in 0,0,0 of the Car Axis system
Dim adapter As Part
Set adapter = selection2.Item(1).Value
Set referenceplane = adapter.OriginElements.PlaneXY
'Set referenceplane = adapter.
Set reference3 = adapter.CreateReferenceFromObject(referenceplane)
Set reference4 = part2.CreateReferenceFromObject(measuredplane)
Set TheSpaWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
Set Themeasurable = TheSpaWorkbench.GetMeasurable(reference3)
angle_XY = Themeasurable.GetAngleBetween(reference4)
'If angle_XY > 90 Then
' angle_XY = Round((180 - angle_XY), 2)
'End If
Set referenceplane = adapter.OriginElements.PlaneZX
Set reference3 = adapter.CreateReferenceFromObject(referenceplane)
Set reference4 = part2.CreateReferenceFromObject(measuredplane)
Set TheSpaWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
Set Themeasurable = TheSpaWorkbench.GetMeasurable(reference3)
angle_ZX = Themeasurable.GetAngleBetween(reference4)
'If angle_ZX > 90 Then
' angle_ZX = Round((180 - angle_ZX), 2)
'End If
Set referenceplane = adapter.OriginElements.PlaneYZ
Set reference3 = adapter.CreateReferenceFromObject(referenceplane)
Set reference4 = part2.CreateReferenceFromObject(measuredplane)
Set TheSpaWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")
Set Themeasurable = TheSpaWorkbench.GetMeasurable(reference3)
angle_YZ = Themeasurable.GetAngleBetween(reference4)
'If angle_YZ > 90 Then
' angle_YZ = Round((180 - angle_YZ), 2)
' End If
'Plaszczyzna_NC is the Frontview plane, which is used to generate the view
If (angle_XY < angle_ZX And angle_XY < angle_YZ) Then
Set Plaszczyzna_NC = adapter.OriginElements.PlaneXY
ElseIf (angle_ZX < angle_XY And angle_ZX < angle_YZ) Then
Set Plaszczyzna_NC = adapter.OriginElements.PlaneZX
Else
Set Plaszczyzna_NC = adapter.OriginElements.PlaneYZ
End If
Dim rem_sel As Selection
Set rem_sel = CATIA.ActiveDocument.Selection
rem_sel.Add hybridBody2
rem_sel.Delete
rem_sel.Clear
End Sub