×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

# Contact US

Log In

#### Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
To Your Posts
• Keyword Search
• One-Click Access To Your
Favorite Forums
• Automated Signatures
On Your Posts
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

# Randomize surface colors in a CATPart

 Forum Search FAQs Links MVPs

## Randomize surface colors in a CATPart

(OP)
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

### RE: Randomize surface colors in a CATPart

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

regards,
LWolf

### RE: Randomize surface colors in a CATPart

(OP)
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?

### RE: Randomize surface colors in a CATPart

(OP)
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

### RE: Randomize surface colors in a CATPart

for each loop doesn't work on selections. use

#### CODE --> vbscript

dim i as integer
for i=1 to objsel.count
...next 

regards,
LWolf

### RE: Randomize surface colors in a CATPart

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

regards,
LWolf

### RE: Randomize surface colors in a CATPart

(OP)
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

### RE: Randomize surface colors in a CATPart

this works :)

#### CODE --> vbscript

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

### RE: Randomize surface colors in a CATPart

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.

### RE: Randomize surface colors in a CATPart

(OP)
Lwolf you are a Legend
Thank you so much!!
I just need to tweak the R, g and b equations
Thanks for your patience

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

### Reply To This Thread

#### Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login

#### Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Design for Additive Manufacturing (DfAM)
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a partâ€™s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!

Already a member? Close this window and log in.