Sandy_Man
Mechanical
- Jun 7, 2024
- 3
I am writing some code that takes the users selection and changes the colour of the feature randomly.
Ideally I would like the user to be able to select the feature from the tree or the geometry itself, and the macro behave in the same way: as an explicit selection. Currently if the user selects the geometry only the selected patch of the feature changes colour, I would like it to behave as if the user has selected the element in the tree.
I would also ideally like the macro to be able to accept user pre-selection, my current version does achieve this.
As a bonus, if the code could change if the user is at the product level to change the colour of the PartBody it would be great!
Having this selection issue solved would help massively for a few other macro projects I have in mind, any help would be greatly appreciated.
Current code below for reference:
Sub CATMain()
Dim UserSel
Dim rcol As Integer
Dim gcol As Integer
Dim bcol As Integer
Dim MyObjectName
Dim InputObject(0)
Dim Status
Dim RandNum As Integer
rcol = 0
gcol = 0
bcol = 0
Set UserSel = CATIA.ActiveEditor.Selection
Set MyEditor = CATIA.ActiveEditor
InputObject(0) = "AnyObject"
Set UselLB = UserSel
oStatus = UselLB.SelectElement(InputObject, "Select geometry to change its colour", True)
If (oStatus = "Normal") Then
RandNum = Int((255 * Rnd) + 1)
rcol = RandNum
RandNum = Int((255 * Rnd) + 1)
gcol = RandNum
RandNum = Int((255 * Rnd) + 1)
bcol = RandNum
UselLB.VisProperties.SetRealColor rcol, gcol, bcol, 0
Else
Exit Sub
End If
UselLB.Clear
End Sub
Ideally I would like the user to be able to select the feature from the tree or the geometry itself, and the macro behave in the same way: as an explicit selection. Currently if the user selects the geometry only the selected patch of the feature changes colour, I would like it to behave as if the user has selected the element in the tree.
I would also ideally like the macro to be able to accept user pre-selection, my current version does achieve this.
As a bonus, if the code could change if the user is at the product level to change the colour of the PartBody it would be great!
Having this selection issue solved would help massively for a few other macro projects I have in mind, any help would be greatly appreciated.
Current code below for reference:
Sub CATMain()
Dim UserSel
Dim rcol As Integer
Dim gcol As Integer
Dim bcol As Integer
Dim MyObjectName
Dim InputObject(0)
Dim Status
Dim RandNum As Integer
rcol = 0
gcol = 0
bcol = 0
Set UserSel = CATIA.ActiveEditor.Selection
Set MyEditor = CATIA.ActiveEditor
InputObject(0) = "AnyObject"
Set UselLB = UserSel
oStatus = UselLB.SelectElement(InputObject, "Select geometry to change its colour", True)
If (oStatus = "Normal") Then
RandNum = Int((255 * Rnd) + 1)
rcol = RandNum
RandNum = Int((255 * Rnd) + 1)
gcol = RandNum
RandNum = Int((255 * Rnd) + 1)
bcol = RandNum
UselLB.VisProperties.SetRealColor rcol, gcol, bcol, 0
Else
Exit Sub
End If
UselLB.Clear
End Sub