Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.

First time visiting? Here are some places to start:
  1. Looking for a certain topic? Check out the categories filter or use Search (upper right).
  2. Need support? Ask a question to our Community Support category.
  3. Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
  4. Be respectful, on topic and if you see a problem, Flag it.

If you would like to contact our Community Manager personally, feel free to send a private message or an email.

Custom Feature: Part Color

EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
I made a Part Color feature here. It's useful to change the appearance of a part with configurations. I know you can configure the appearance of parts in the Configured Properties, but you're limited to setting up each possible permutation of the design, which is onerous especially when using a variable configuration that could be changed to anything. I needed something like this for something I was working on, then my wheels got turning on whether I'd be able to figure out how to drive it with a hexadecimal code, and it turned into a project-based learning exercise for me. I hope it's useful to someone else too.
Evan Reese

Comments

  • romeograhamromeograham Member, csevp Posts: 676 PRO
    edited May 2020
    Nice work @Evan_Reese

    This is interesting, since changing color in the Feature List means that you can use color to represent different levels of development in the Feature List ... almost a progress bar of sorts.

    Sometimes I'll use neutral (or just Onshape standard) colors for parts when they are "development", "parent" or "master model" type parts...then, when they get closer to being done (or if they are derived into a more finished part studio), I'll use colors that are closer to the real color of the finished real-world parts. This will make that easier.

    Having a little trouble with an error, though: I set a color on a part using the regular "Edit appearance" method, then tried to use your FS.
    I thought it was working (since I had set the color with the regular method to get a hex code from one of my custom colors), but now I realize that the FS will not change the color. 

    If I use your FS on a part with default Onshape color, it seems to work, unless I try "Use hex code".
    When I try to use a Hex code to set the color, the feature throws an error, and doesn't do the color change:

    When I deselect the Use hex code box, the error goes away.


    Any ideas?

  • EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
    edited May 2020
    ooh that's an interesting use I'd not thought of. could be handy.

    Of course, my first user finds a bug immediately, haha. I've updated it to accept upper and lower case letters, which was the issue.

    I think it's like the other features that change part properties in that the part you're changing can't have been edited manually before. To get it working, right click the part in the parts list and select "properties" then pick, "reset all". after that it will work. be aware that this will REMOVE ALL PROPERTIES from your part (like name, part number, revision, etc), so if this is a part that's late in development, especially one that's been released, you'll want to watch out. It helps to plan for it. if you didn't you can always re-enter the data you need. Once you're really far into a project you may not want to do this. I believe the change can just be rolled back if you don't like it though.

    Evan Reese
  • AlexPittAlexPitt Member Posts: 22 ✭✭
    A nice development @Evan_Reese! Great little icon too! I copied, liked it and had a play, setting up a config table as with Neil's.  As you say, just need to remember to not use the OS native colour picker which overrides the FS (as per any feature script affecting properties).  The ability to use hex code is great for quickly pasting colour refs in from the web.
  • Cache_River_MillCache_River_Mill Member Posts: 225 PRO
    Legit!
  • EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
    The feature accepts face selections now too
    Evan Reese
  • MichaelPascoeMichaelPascoe Member Posts: 1,989 PRO
    I was about to request a "color all" button but its already there!



    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
    I was about to request a "color all" button but its already there!


    nice! I'm glad I'm not the only one. I was working recently on a design for a new shed I want to build and got annoyed with always trying to make it look like wood or leaving it all different colors
    Evan Reese
  • EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
    I just pushed a V14 update to this feature giving it some preset palettes to make it easier to just slap some color on without having to fiddle with numbers.  Check it out and let me know what you think.



    Evan Reese
  • MichaelPascoeMichaelPascoe Member Posts: 1,989 PRO
    Legit, thanks!

    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
  • eric_pestyeric_pesty Member Posts: 1,885 PRO
    Nice! Wish the standard appearance tool had this too!
    Now if we could just get some more refined appearances (and also be able to have default appearances linked to materials)!
  • eric_pestyeric_pesty Member Posts: 1,885 PRO
    edited March 2022
    @Evan_Reese
    Don't know if there's anything you can do about it but I just noticed I can't seem to use a variable for the Hex value... I was hoping to use this to apply any random color using a configuration variable (with the Hex value) but I don't think I can make it work.
  • EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
    edited March 2022
    @Evan_Reese
    Don't know if there's anything you can do about it but I just noticed I can't seem to use a variable for the Hex value... I was hoping to use this to apply any random color using a configuration variable (with the Hex value) but I don't think I can make it work.
    I think I'd have to add a "Use expression" checkbox or something and make it go to a different UI parameter that accepts anything, not just strings. Since it seems like a niche use-case, I don't think I'm going to add it, but I know you know some FS so you could probably knock it out one of two ways.
    1. Copying my code and making that tweak to the UI to have your own version of Part Color.
    2. Import the Part Color Feature Studio into your own custom Feature Studio and call my feature as a function, just doing a tiny bit of work to take an expression and convert it to a string.
    I actually wanted to check my understanding of the second one and by the time I did I made this. you can see my code is super short, because it's just essentially giving you a new UI on top of the Part Color code. Note, since it's now looking for an expression as input, you have to add quotation marks around your hex code entries (or give it something that's already a string, like the config variable is doing).
    Evan Reese
  • eric_pestyeric_pesty Member Posts: 1,885 PRO
    @Evan_Reese
    Thanks, I'll take a look! "importing" isn't something I've played with before so I will need to do a bit of reading.
  • EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
    edited March 2022
    I haven't much either, but you know how if you want to extrude something in FS you can either use opExtrude() or extrude()? opExtrude is just the basic function to extrude some geometry, but extrude() is the actual extrude feature with drafts, surface mode, sheet metal capability, and a merge scope. I'm calling the entire Part Color feature as a function here via colorPart(), and since my UI inputs are named the same things as the ones Part Color is looking for I can just pass the whole definition map in and it works. Like this:

    colorPart(context, id, definition);

    I did have to set definition.colorMode = ColorMode.Hexadecimal; since leaving it undefined caused a failure. I could have also done this, but I didn't need to because the names of the map keys match anyway:

    colorPart(context, id, {
    "colorMode" : ColorMode.Hexadecimal,
    "parts" : definition.parts,
    "faces" : definition.faces,
    "features" : definition.features,
    "invertParts" : definition.invertParts,
    "alpha" : definition.alpha,
    "hexCode" : definition.hexCode
    });
    Evan Reese
  • eric_pestyeric_pesty Member Posts: 1,885 PRO
    Right,
    I had a look and from a programming perspective I understand calling a function but I don't really get how it works in FS...

    I guess looking at this the main question is why won't a string input accept a string variable? And probably related why do "regular" variables don't have a "string" option... I expected it to just work so I had a plan to use it that way but it doesn't allow it!

    Having to put quotes around your hex value would be annoying! I guess the only other option I can think of would require adding yet another type of input option i.e. "Variable". I gave a shot at allowing either a regular "input" hex or "variable" adding a "Hexmode" dropdown to the wrapper.

    When used it basically treat it as hex and do something like this:

           if (definition.Hexmode == Hexmode.Variable)
           { definition.hexCode = definition.hexCodeVar;
           } 

    Before calling the Part color: 
    https://cad.onshape.com/documents/6bc71eac4d11d3aecd10fc74/w/01f622cee2fa8a4b64e1c3f7/e/2e5523cca33923b9f6cf2a45

  • eric_pestyeric_pesty Member Posts: 1,885 PRO
    @Evan_Reese
    I'm having an issue with a specific document where I only seem to be able to apply color to faces or features but not bodies...

    It seems to work as expected when I checked a random doc (even if I apply face appearance to specific faces from the built in Onshape controls, which I thought might have been causing the issue) so I'm not sure what's going on with this specific part studio. Am I missing something obvious?

    https://cad.onshape.com/documents/ecbaa424110effa81de8e4b5/w/c24d984b5be96316a749a670/e/2ff8279e857fe28ea1b50543
  • EvanReeseEvanReese Member, Mentor Posts: 2,136 ✭✭✭✭✭
    edited March 2022
    Open the part properties and hit "Reset all" (note, this will get rid of all part metadata like name and part number unfortunately). If you've set the color manually at some point, it won't work. I'm not really sure why that's how it works, but I'm pretty sure its beyond the scope of FS to solve it. It would have to be changed by Onshape. I've considered adding a checkbox so that you can override it by selecting a body, but actually coloring the faces of the body.
    Evan Reese
  • eric_pestyeric_pesty Member Posts: 1,885 PRO
    Thanks, that solved it...
    For some reason when I didn't have the issue on a "test" part I made but I guess I hadn't applied appearances quite the same way before...
Sign In or Register to comment.