1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

New Script - Colorize

Discussion in 'Alibre Script' started by idslk, Mar 5, 2019.

  1. idslk

    idslk Senior Member

    Hello colleagues,
    I've made a little script called "Colorize". Triggered from this Thread and the idea of @simonb65 .
    Colorize-Script can set part-colors in an assembly or in a part.
    It can not change feature colors (it's V0.1...)
    It makes sense to Change the color properties of the part/assembly, otherwise you will get "funny" results (depends on the features of the part):
    Colorize_1.PNG
    After setting the properties in the highest level of an assembly you can run the script.
    First you should choose your color:
    Colorize_2.PNG
    Then you have to confirm it with ok. The choose face window will appear.
    Colorize_3.PNG
    Once the color is choosen you have to click on a face and click ok, next face and ok, next face and ok...until the color gets empty ;-)
    If you want to change the color for the next faces, check the "Change color" box and press ok.
    You will get the change color window.
    Change to the new Color, press ok and the coloring starts again.
    If your colored all you want, you can check the "End coloring" box, press ok and window will disappear.
    Code:
    #ScriptName = 'Colorize'
    #Version = 0.1
    #Date = 20190305
    #Startdate = 20190305
    #author = 'IDSLK'
    #Infographic = None
    
    Win = Windows()
    WT = 'IDSLK '
    ScriptName = 'Colorize'
    #ColorList is for the StringListInput
    Colorlist = ['Red','Green','Blue','Magenta','Yello','Gunmetal']
    #ColorValues have to be set for each Item in ColorList / Format is [Red 0..255,Green 0..255,Blue 0..255]
    ColorValues = [[255,0,0],[0,0,255],[0,255,0],[255,255,0],[255,0,255],[55,55,60]]
    Colors = []
    R=255
    G=0
    B=0
    Loop=0
    
    #Get Enviroment----------
    try:
      Enviroment = CurrentAssembly()
      EnviromentText = 'Assembly ' + str(Enviroment)
    except:
      Enviroment = CurrentPart()
      EnviromentText = 'Part, Name: ' + str(Enviroment)
    
    #Define functions--------
    def User_Window_Color():
      global Options, Colors ,R,G,B
      Options = []
      Colors = []
      Options.append(['Color', WindowsInputTypes.StringList, Colorlist])
      Colors = Win.OptionsDialog(WT+ScriptName, Options, 155)
      R = ColorValues[Colors[0]][0]
      B = ColorValues[Colors[0]][1]
      G = ColorValues[Colors[0]][2]
    
    def User_Window():
      global Options, Values
      Options = []
      Values = []
      Options.append(['Face', WindowsInputTypes.Face, None])
      Options.append(['End Coloring?', WindowsInputTypes.Boolean, False])
      Options.append(['Change Color?', WindowsInputTypes.Boolean, False])
      Values = Win.OptionsDialog(WT+ScriptName, Options, 155)
      if Values == None:
        print 'Canceled'
        sys.exit()
    
    #main--------------------
    User_Window_Color()
    
    while Loop == 0:
      User_Window()
      print Values[0]
      if Values[0] <> None:
        Dummy = Values[0].GetPart()
        Dummy.SetColor(R,G,B)
      if Values[2] == True:
        User_Window_Color()
      if Values[1] == True:
        Loop=1
    Test it and have fun!

    Regards
    Stefan
    PS.: i hope i get some comments ;-)
     

    Attached Files:

    cadtec, ajayre and simonb65 like this.
  2. simonb65

    simonb65 Alibre Super User

    Mine also sets the Opacity and Reflectivity as part of the colourisation process, so it brings a touch of material to it too! Thanks for the credit :)
     
  3. idslk

    idslk Senior Member

    I don't know yet how to set the values from a running Alibre Script...maybe some day...:rolleyes:
    Regardsless such little things, here comes V0.2 (little drum roll)
    Improvment new colorwindow.

    When you start the script, it brings up a system color window. Choose your wanted color as usual.

    upload_2019-3-7_12-24-4.png

    After clicking OK you will get the 'Colorize' window.

    upload_2019-3-7_12-23-38.png

    Choose a face by clicking on it in your design and click OK. The part, on which the selected face is located, will be colored in the choosen color.
    Choose next face/part and click OK again and so on...
    If you want to use a new color, check the box 'New Color?...' and click OK.
    The color window will be called, but unfortunately displayed behind the Alibre window (Tips are wellcome...).
    You have to bring it to front e.g. using ALT and TAB. (Sorry, ...V0.2)
    If you want to end you can check "End coloring ?..." and click OK or simply click 'Cancel'

    Test and have some fun.

    Regards
    Stefan
     

    Attached Files:

    NateLiqGrav likes this.
  4. oldfox

    oldfox Alibre Super User

    Hi Stefan,
    The script seems to be running OK with only one detail that I may be causing it myself. After I change a color (works) I click the second option to
    change color again, after clicking OK for the first color. Clicking Alt-Tab doesn't work. The color window is gone. The first thing I did before using
    "Colorize" was to un-maximize (is that really a good word?) the workspace window. The Windows Color Window always comes up in the middle
    of the screen on my rig so I just drag the side border of the workspace window to where I can see the color window behind it. It still goes away.

    Bottom line...
    Good work!! ;)
     
  5. oldfox

    oldfox Alibre Super User

    Now if it were possible to assign a name (e.g. Silver, Gold, Coal, Lime Green, etc., etc.) it sure would be easier than guessing which color of 2 or 3
    or more, very close shades is the right one. This is of course with "Custom Colors".
     
  6. idslk

    idslk Senior Member

    Hello Chris,
    if you want to use Names, you can use the very first version of the script and add some colors to the list.
    The system names can be foud here: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value
    If you will add e.g 'Maroon' it has the color code #80 00 00 which is R=128, G=0, B=0
    The lines from the original code extended with 'Maroon':
    Code:
    #ColorList is for the StringListInput
    Colorlist = ['Red','Green','Blue','Magenta','Yello','Gunmetal','Maroon']
    #ColorValues have to be set for each Item in ColorList / Format is [Red 0..255,Green 0..255,Blue 0..255]
    ColorValues = [[255,0,0],[0,0,255],[0,255,0],[255,255,0],[255,0,255],[55,55,60][128,0,0]]
    
    With the hidden color window in V02 i'm not able to set the Form property to "TopMost', so it behaves as a normal task window. Therefor you can use windows task switching keys.
    But as i said, it's a 0.2 Version :) and you are able to modify it to your needs (i know);)

    Regards
    Stefan
     
  7. oldfox

    oldfox Alibre Super User

    Hi Stefan,

    Your original version will work for me as I very seldom use the default colors in the Windows version. I'll start using the Version01 until
    I can make V02 do what I want. :cool: ("I think I can, I think I can, I think I can." o_O

    Thanks,
    Chris
     
  8. NateLiqGrav

    NateLiqGrav Alibre Super User

    Hey Stafan,
    This is the method to add custom colors by name to the palette at the bottom of the dialog. I'll let you add it to your script.

    Code:
    import clr
    
    clr.AddReference('System.Windows.Forms')
    import System.Windows.Forms
    from System.Windows.Forms import ColorDialog
    from System import Array
    
    clr.AddReference('System.Drawing')
    import System.Drawing
    from System.Drawing import Color, ColorTranslator
    
    MyDialog = ColorDialog()
    # Allows the user to select or edit a custom color.
    MyDialog.FullOpen = 1
    
    # Create an array of custom colors
    MyPalette = Array[int]([
        ColorTranslator.ToOle(Color.FromName("Khaki")),
        ColorTranslator.ToOle(Color.FromName("Gold")),
        ColorTranslator.ToOle(Color.FromName("Goldenrod")),
        ColorTranslator.ToOle(Color.FromName("BurlyWood")),
        ColorTranslator.ToOle(Color.FromName("CadetBlue")),
        ColorTranslator.ToOle(Color.FromName("ForestGreen")),
        ColorTranslator.ToOle(Color.FromName("Gainsboro")),
        ColorTranslator.ToOle(Color.FromName("LightGray")),
        ColorTranslator.ToOle(Color.FromName("Silver")),
        ColorTranslator.ToOle(Color.FromName("DarkGray")),
        ColorTranslator.ToOle(Color.FromName("Gray")),
        ColorTranslator.ToOle(Color.FromName("DimGray")),
        ColorTranslator.ToOle(Color.FromName("LightSlateGray")),
        ColorTranslator.ToOle(Color.FromName("SlateGray")),
        ColorTranslator.ToOle(Color.FromName("DarkSlateGray")),
        ColorTranslator.ToOle(Color.FromName("Black"))
        ])
      
    
    # Assigns an array of custom colors to the CustomColors property
    MyDialog.CustomColors =MyPalette
    
    # Sets the initial color select to the current text color,
    # so that if the user cancels out, the original color is restored.
    MyDialog.Color = Color.FromName("Khaki")
    MyDialog.ShowDialog()
    
    PartColor =  MyDialog.Color
    print(PartColor)
    
    StoredPalette = MyDialog.CustomColors
    print(StoredPalette)
    
    
    Color names can be found sorted here: https://html-color-codes.info/color-names/

    I did find out making it modal (on top of parent) requires passing the ShowDialog() function the parent window. I couldn't find an easy way to get it to get the parent though.
     
  9. idslk

    idslk Senior Member

    Hello colleagues,

    new version v0.3
    plus:
    - i've added Nate's predefined custom color palette. Thanks Nate.:)
    - I've made the colorwindow to appear on top position!;)
    - the color will be instantaniusly changed after clicking on a face. No OK needed.
    minus:
    - after returning from the colorwindow for the second time, the face input box loses the focus, so you have to click into the box before you can select the next face. (does anyone know haow to assign the focus automatically?)
    - I haven't added to save a custom color palette

    If you want to you, can give it a try...

    Regards
    Stefan
     

    Attached Files:

    NateLiqGrav and cadtec like this.
  10. NateLiqGrav

    NateLiqGrav Alibre Super User

    Nice interesting use of InputChanged! - However It would be best to clear selection before the OptionsDialog is popped up and include some instruction. I wasn't expecting it to work immediately before I see the dialog.
     

Share This Page