Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Randomize surface colors in a CATPart 1

Status
Not open for further replies.

PedroReg

Mechanical
Oct 1, 2021
5
Hi Guys
I have recently understood the power of macros and have been trying to learn a few things.

I work a lot on the GSD, so I tried to create a macro to randomize the surface colors (only the default) that are displayed on my screen.
Unfortunately, I couldn't do that.
Here is the code that I tried:

-----------------------------

Sub CATMain()

Dim vis As Variant

Set objSel = CATIA.ActiveDocument.Selection
objSel.Search "CATGmoSearch.Surface.Color='(243,254,177)',scr"

Dim I As Integer
Dim r As Integer
Dim g As Integer
Dim b As Integer

For I = 1 To objSel.Count

r = I * 5
g = I * 10
b = I * 15
If r > 255 Then
r = r - 255
End If
If g > 255 Then
r = r - 255
End If
If b > 255 Then
r = r - 255
End If

vis = objSel.Item(I).VisProperties
vis.SetRealColor r, g, b, 0

Next I

End Sub

-----------------------------

Here's the following questions that I have:
1. Can I separate the selections I get from my search?
2. Why is " vis = objSel.Item(I).VisProperties " getting an error?

Thanks in advance
 
Replies continue below

Recommended for you

you need to put set in from of vis = objSel.Item(I).VisProperties
i.e. set vis = objSel.Item(I).VisProperties

regards,
LWolf
 
Hey LucasC,

Thanks for your reply!
I tried to follow your thread, I came up with this:

Sub CATMain()

Dim vis As Variant

Dim objsel As Selection
Set objsel = CATIA.ActiveDocument.Selection
objsel.Search "CATGmoSearch.Surface.Color='(243,254,177)',scr"

Dim mySurface As HybridShapeSurfaceExplicit

Dim I As Integer
Dim r As Integer
Dim g As Integer
Dim b As Integer

I = 1

For Each mySurface In objsel

r = I * 5
g = I * 10
b = I * 15
If r > 255 Then
r = r - 255
End If
If g > 255 Then
r = r - 255
End If
If b > 255 Then
r = r - 255
End If

objsel.Clear
objsel.Add mySurface
Set vis = objsel.VisProperties
vis.SetRealColor r, g, b, 0

I = I + 1

Next
End Sub

------------------
The code is currently give me erros on the following line "For Each mySurface In objsel". I believe it's because the "mySurface" is a HybridShapeSurfaceExplicit and the "objSel" is a Selection.
Any tips?
 
Hi LWolf,

I'm sorry for missing that one! (rookie mistake)
However, it comes with the same error "Object doesn't support this property or method"

Any tips?
Thanks
 
for each loop doesn't work on selections. use
Code:
dim i as integer
for i=1 to objsel.count
[indent]...[/indent]
next

regards,
LWolf
 
also, if you are working in the selection, you cannot clear it downstream in the code...

regards,
LWolf
 
That's what I have on my first code and still does not work

I need to separate the selecions from my search, i just don't know how.

Thanks in advance
 
this works :)
Code:
Sub CATMain()
Set objsel = CATIA.ActiveDocument.Selection
objsel.Search "CATGmoSearch.Surface.Color='(243,254,177)',scr"

Dim r As Integer
Dim g As Integer
Dim b As Integer
Dim i, k As Integer
If objsel.Count > 0 Then
    ReDim SelArray(objsel.Count - 1)
    For k = 1 To objsel.Count
        Set SelArray(k - 1) = objsel.item(k).Value
    Next
    objsel.Clear
    For i = 0 To UBound(SelArray)
        r = i * 5 + 100
        g = i * 10 + 100
        b = i * 15 + 100
        If r > 255 Then r = r - 255
        If g > 255 Then r = r - 255
        If b > 255 Then r = r - 255
        objsel.Add SelArray(i)
        objsel.VisProperties.SetRealColor r, g, b, 0
        objsel.Clear
    Next i
    Else
        MsgBox "Nothing selected"
    End If
End Sub

regards,
LWolf
 
You may want to explore a different search parameter. I think ,scr will only search what visible on your screen as you are viewing it. in other words, if you are zoomed in on one area, only that little bit will be captured when the macro is run.
 
Lwolf you are a Legend
Thank you so much!!
I just need to tweak the R, g and b equations
Thanks for your patience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor