×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

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!
  • Students Click Here

*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

Jobs

Programmatically change the color of a part?

Programmatically change the color of a part?

Programmatically change the color of a part?

(OP)
does anybody know how to programmatically change the color of a part?

RE: Programmatically change the color of a part?

Use the following commands.  Not that the final parameter, 1 in this case, sets the inheritance value of the color.

  Set ActDoc = CATIA.ActiveDocument
  Set visProperties1 = ActDoc.Selection.Visproperties
  visProperties1.SetRealColor Rd(ColNum),Grn(ColNum),Blu(ColNum),1

RE: Programmatically change the color of a part?

(OP)
Thanks alot, this really helps.

Another question on the same topic.  Say i have an assembly, how can i programmatically change the color of just one of the parts in the assymble?

thanks for you help

RE: Programmatically change the color of a part?

We've used variations on the following bit of code to change the colours of individual parts within an assembly (another part of the code selected the items in the first place):

Set SelectedElements = productDoc.Selection
Set VisPropSet = SelectedElements.VisProperties
SelectedElements.Clear()

SelectedElements.Add(Assemblyproducts.Item("<User Specified Part Name>"))
VisPropSet.SetRealColor <Red Number>,<Green Number>,<Blue Number>,1
SelectedElements.Clear()


If I remember correctly, it took quite a bit of trial and error to get V5 to recognise all the inputs so make sure you use dimension statements where necessary.

"Pinky, are you thinking what I'm thinking?"
"Yes, Brain, but isn't that dangerous?"

RE: Programmatically change the color of a part?

Sorry

"(another part of the code selected the items in the first place)"  should read "(another part of the code read the part names in the first place)"

"Pinky, are you thinking what I'm thinking?"
"Yes, Brain, but isn't that dangerous?"

RE: Programmatically change the color of a part?

Hi,

From the Dassault docs. Maybe you can use it (modified, of course).

' COPYRIGHT DASSAULT SYSTEMES 2002
Option Explicit

' ***********************************************************************
'   Purpose     : Change the color of products using a color randomly selected in a table.
'   Assumptions : A CATProduct document should be active.
'   Author      :
'   Languages   : VBScript
'   Locales     : English
'   CATIA Level : V5R6
' ***********************************************************************
Dim iMax ' Number of colors in the table minus one
iMax = 19

' ***********************************************************************
'
' Purpose:  Add a product in a group.
'
' Inputs :  oNode    Product     the product
'           oGroup   Product     the array of group
'           iIndex   Integer     the index of the product
'
' ***********************************************************************
Sub TreatANode(ByRef oNode, ByRef oGroup, ByRef iIndex)

    Dim iNumberOfSubComponent As Integer
    iNumberOfSubComponent = oNode.Products.Count

    If (iNumberOfSubComponent = 0) Then
        ' Add the leaf to the group
        oGroup(iIndex).AddExplicit oNode
        ' Prepare the next index
        iIndex = iIndex + 1
        If (iIndex > iMax) Then
            iIndex = 0
        End If
    Else
       ' Treat the subcomponents
       Dim I As Integer
       For I = 1 to iNumberOfSubComponent
           Call TreatANode(oNode.Products.Item(I), oGroup, iIndex)
       Next
    End If

End Sub

' ***********************************************************************
'
' Purpose:  Main.
'
' ***********************************************************************
Sub CATMain()

    ' Build the color table (oColor(i,0) = Red, oColor(i,1) = Green, oColor(i,2) = Blue)
    Dim oColor(19,2)
    
    'Red
    ocolor(0,0)=255
    ocolor(0,1)=0
    ocolor(0,2)=0
    'Dark Red
    ocolor(1,0)=220
    ocolor(1,1)=90
    ocolor(1,2)=90
    'Light Red
    ocolor(2,0)=255
    ocolor(2,1)=150
    ocolor(2,2)=150
    'Fushia
    ocolor(3,0)=255
    ocolor(3,1)=150
    ocolor(3,2)=230
    'Green
    ocolor(4,0)=60
    ocolor(4,1)=255
    ocolor(4,2)=30
    'Dark Green
    ocolor(5,0)=30
    ocolor(5,1)=190
    ocolor(5,2)=30
    'Light Green
    ocolor(6,0)=190
    ocolor(6,1)=255
    ocolor(6,2)=130
    'Green-blue
    ocolor(7,0)=80
    ocolor(7,1)=255
    ocolor(7,2)=160
    'Blue
    ocolor(8,0)=80
    ocolor(8,1)=225
    ocolor(8,2)=255
    'Dark Blue
    ocolor(9,0)=90
    ocolor(9,1)=140
    ocolor(9,2)=255
    'Light Blue
    ocolor(10,0)=180
    ocolor(10,1)=255
    ocolor(10,2)=255
    'Yellow
    ocolor(11,0)=255
    ocolor(11,1)=255
    ocolor(11,2)=30
    'Dark Yellow
    ocolor(12,0)=128
    ocolor(12,1)=128
    ocolor(12,2)=30
    'Yellow-orange
    ocolor(13,0)=255
    ocolor(13,1)=190
    ocolor(13,2)=70
    'Brown
    ocolor(14,0)=160
    ocolor(14,1)=90
    ocolor(14,2)=60
    'Light Brown
    ocolor(15,0)=200
    ocolor(15,1)=170
    ocolor(15,2)=140
    'Pink
    ocolor(16,0)=220
    ocolor(16,1)=0
    ocolor(16,2)=220
    'Mauve
    ocolor(17,0)=190
    ocolor(17,1)=90
    ocolor(17,2)=255
    'Grey
    ocolor(18,0)=128
    ocolor(18,1)=128
    ocolor(18,2)=128
    'White
    ocolor(19,0)=200
    ocolor(19,1)=200
    oColor(19,2)=200

    ' Retrieve the root product
    Dim oRoot_Product As AnyObject
    Set oRoot_Product = CATIA.ActiveDocument.Product

    ' Retrieve the selection object
    Dim oSelection As Selection
    Set oSelection = CATIA.ActiveDocument.Selection
    
    ' Build the groups of products which will share the same color
    Dim oGroup(20)
    Dim cGroups As Groups
    Set cGroups = oRoot_Product.GetTechnologicalObject("Groups")
    Dim I as integer
    For I = 0 to 19
        Set oGroup(I) = cGroups.Add
    Next

    ' Build the groups by exploring recursively all nodes in the product structure
    Dim iIndex
    iIndex = 0
    Call TreatANode(oRoot_Product, oGroup, iIndex)

    ' Set the colors
    For I = 0 to 19
        oGroup(I).FillSelWithExtract
        oSelection.VisProperties.SetRealColor oColor(I,0), oColor(I,1), oColor(I,2), 1
        cGroups.Remove oGroup(I).Name
        Set oGroup(I) = Nothing
    Next
    oSelection.Clear
    
    Set oSelection = Nothing
    Set oRoot_Product = Nothing

End Sub

Regards
Fernando

RE: Programmatically change the color of a part?

(OP)
Ok, i'm must be doing something wrong.  I just can't seem to figure out how to programmatically select the darn part in the assembly and then apply the color to just it.  I keep turning the whole assembly a different color instead of just the part i have.  Thanks so much for your help, i appologize for my stupidity.  To bad the record macro feature doesn't record everthing.

RE: Programmatically change the color of a part?

(OP)
alright, almost there.  I finally got it to change the color of just one of the parts in my assembly (i preselected it before runing the script).  Now if i can just figure out how to programmatically select the part for me i'll be good to go.

RE: Programmatically change the color of a part?

Here is the code to select a product object;
Dim oSelection As Selection
Set oSelection = CATIA.ActiveDocument.Selection
oSelection.Clear
oSelection.Add oRoot_Product

If you don't first clear the current selection it adds to the current selection, i.e. if you had one part selected in an assembly you would now have two selected.
As far as choosing the correct part you probably want to access the oRoot_Product.Products object and do some sort of choce from there. For example looping through the products in the oRoot_Product;
Set oSubProducts = oRoot_Product.Products
For Each oProduct In oSubProducts
     oSelection.Clear
     oSelection.Add oProduct
     (your code here)
Next

RE: Programmatically change the color of a part?

(OP)
Well, after some trial and error, your great help, and a little bit of luck i've finally manage to piece together what i was trying to get at.  I've included the script that i've written, with which you both helped me get to, should you ever have any use for it and for other forum members to reference.  

to give an idea of what's going on here:  I have a list of parts in a text file created by another program. The list represents parts that need to change, revisioned, etc.  in a large subassembly i don't want to go chasing these parts down, so i change the color to find them.  (Well the true reason for highlighting them is product/system specific to my company and would mean nothing to you, but i think you get the jist of it)  Again, thanks


Sub CATMain()

Dim PARTS() As String
Dim PARTSval As Integer

Open "myfile.txt" For Input As #1
ReDim PARTS(PARTSval)
Do While Not EOF(1)
    ReDim Preserve PARTS(PARTSval)
    Input #1, PARTS(PARTSval)
    PARTSval = PARTSval + 1
Loop
Close #1

Dim i As Integer
i = 1

Dim productDocument1 As ProductDocument
Set productDocument1 = CATIA.ActiveDocument

Dim product1 As Product
Set product1 = productDocument1.Product

Dim products1 As Products
Set products1 = product1.Products

Dim selection1 As Selection
Set selection1 = CATIA.ActiveDocument.Selection

selection1.Clear
For PARTSval = LBound(PARTS) To UBound(PARTS)
    i = 1
    For Each Product In products1
    If products1.Item(i).PartNumber = PARTS(PARTSval) Then
        selection1.Add products1.Item(i)
    End If
        i = i + 1
    Next
Next

Dim visproperties1 As VisPropertySet
Set visproperties1 = productDocument1.Selection.VisProperties

selection1.VisProperties.SetRealColor 255, 0, 0, 1
End Sub

I'm sure there are way's of simplifing this but it's good enough.

RE: Programmatically change the color of a part?

One comment on the "For Each" statement. You don't need to use the i=1 and i=1+1 before and following the loop. all you need to do is use the "Product" declared in the "for Each" statement.

selection1.Clear
For PARTSval = LBound(PARTS) To UBound(PARTS)
  For Each Product In products1
    If Product.PartNumber = PARTS(PARTSval) Then
      selection1.Add Product
    End If
  Next
Next

The way to read the "For Each" statment is to say that for each Product inside the products1 array do the code contained within the loop. The Product in the "for each" statement allows you to access the item of an array or collection indvidually. I would recomend a website that contains some good information that is in a bit easier format then the MSDN site; devguru.com. If you are using Option Explicit you will need to first Dim the Product used in the for each loop as a Product.

RE: Programmatically change the color of a part?

(OP)
i realize that.  That was just a script i used to test the methodolgy.  The actuall program i'm creating has to cycle through subassemblies and i have multiple arrays to cylce through, so on and so forth.  Thanks for your posts.

RE: Programmatically change the color of a part?

Sorry I must have missed that part, so you are looking at recusivly parsing the entire assembly tree, not just the first level children but all levels. Sometimes it may also be productive to use a Dictionary instead of an array this would allow you to search without the need to loop through each item of the array.

(simplified example of recursive call using dictionary)
Sub ParseAsyTree(ByRef oSelection, oProducts, PARTS)
  For Each oProduct In oProducts
    'If found in the list of parts to select then select
    If PARTS.Exists(oProduct.PartNumber) Then
      oSelection.Add oProduct
    'If not in the list of parts and is an assembly
    ElseIf oProduct.Products.Count > 0 Then
      ParseAsyTree(oSelection, oProduct.Products, PARTS)
    End If
  Next
End Sub
 

RE: Programmatically change the color of a part?

(OP)
WOW, thanks alot, that greatly simplifies things

RE: Programmatically change the color of a part?

I forgot to mention a few things in using VBA you first need to add the reference to the Microsoft Scripting Runtime to be able to create a Dictionary. Then the declaration is rather simple;

Dim PARTS As Dictionary
Set PARTS = New Dictionary

The nice thing about a Dictionary is the .Exists(Key) method which returns True if the Key is found in the dictionary, no more looping find out if what you are looking for is in the array. Also adding items to a dictionary is much easier then ReDiming an array and then adding the new value.

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!


Resources