vangilme
Mechanical
- Sep 28, 2024
- 2
Hi all. First time posting. I wanted to create a macro that would allow the user to choose a geometrical set containing points and an axis system, and the macro would generate a series of axis systems with their origins at the points in the geometrical set, and aligned with the axis system chosen. I can get the axes to the right location, but trying to align them has been a nightmare.
As can be seen, I've used both the GetXAxis/PutXAxis methods and the XDirection property, and CATIA chokes on both. The original code came from ChatGPT, which initially did not try to align the axes, but then when that was pointed out, it started making up properties/methods that aren't documented in the VB help. I'm at my wits end. Please let me know what I'm doing wrong. Thanks.
Code:
Sub CopyAxisToPoints()
Dim fil(0)
Dim oPoint As Point
Dim newAxisSystem As AxisSystem
Dim oldAxisSystem As AxisSystem
' Get active document
Set Document = CATIA.ActiveDocument
Set Part = Document.Part
Set oSelection = Document.Selection
' Prompt user to select the geometrical set
oSelection.Clear
fil(0) = "HybridBody"
oSelection.SelectElement2 fil, "Select a geometrical set", False
Set geometricalSet = oSelection.Item(1).Value
' Prompt user to select the axis system
oSelection.Clear
fil(0) = "AxisSystem"
oSelection.SelectElement2 fil, "Select an axis system", False
Set oldAxisSystem = oSelection.Item(1).Value
' Dim vXValues(2), vZValues(2)
' vXValues(0) = 1
' vXValues(1) = 0
' vXValues(2) = 0
'
' vZValues(0) = 0
' vZValues(1) = 0
' vZValues(2) = 1
' Dim vXValues(2) As Double, vZValues(2) As Double
'
' oldAxisSystem.GetXAxis vXValues
' oldAxisSystem.GetZAxis vZValues
Dim vXValues As Reference, vZValues As Reference
Set vXValues = oldAxisSystem.XAxisDirection
Set vZValues = oldAxisSystem.ZAxisDirection
' Loop through all points in the geometrical set
For i = 1 To geometricalSet.HybridShapes.Count
If TypeOf geometricalSet.HybridShapes.Item(i) Is Point Then
Set oPoint = geometricalSet.HybridShapes.Item(i)
' Create a new axis system based on the original
' Set newAxisSystem = part.HybridShapes.AddNewAxisSystem(point)
Set newAxisSystem = Part.AxisSystems.Add()
newAxisSystem.Type = catAxisSystemStandard
' Set properties of the new axis system if needed
newAxisSystem.Name = "AxisSystem_at_" & oPoint.Name
' Update the new axis system position (optional adjustments)
newAxisSystem.OriginPoint = oPoint
newAxisSystem.XAxisType = 1
newAxisSystem.ZAxisType = 1
' newAxisSystem.PutXAxis (vXValues)
' newAxisSystem.PutZAxis (vZValues)
newAxisSystem.XAxisDirection = vXValues
newAxisSystem.ZAxisDirection = vZValues
' Add the new axis system to the part
End If
Next
' Update the part
Part.update
MsgBox "Axis systems copied to all points in the geometrical set."
End Sub
As can be seen, I've used both the GetXAxis/PutXAxis methods and the XDirection property, and CATIA chokes on both. The original code came from ChatGPT, which initially did not try to align the axes, but then when that was pointed out, it started making up properties/methods that aren't documented in the VB help. I'm at my wits end. Please let me know what I'm doing wrong. Thanks.