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.

Options

Custon Feature to concatenate custom property and part name

Lewis_Maina_MuthoniLewis_Maina_Muthoni Member, csevp Posts: 10 ✭✭
Can someone please guide me OR share materials to help me achieve the following:

I have a custom property called BCP Part Number, I want to automatically transfer this number and concatenate it with the Part Name as per the below image. Such that the final part name is a combination of BCP Part Number + Part Name.

Tagged:

Answers

  • John_vd_WerffJohn_vd_Werff Member Posts: 65 PRO
    There is a Part Name Featurescript. But I don't think it can do what you are asking for.

    Why would you want to have the part number in the part name? If you need that for naming the exported files than you can use the export rules in the Onshape preferences. There you can create strings of concatenated properties. 
  • EvanReeseEvanReese Member, Mentor Posts: 2,477 ✭✭✭✭✭
    edited May 2023
    I agree with John that if it's for naming exported files, use the export rules. There might be other reasons you'd want to do this though, in which case read on.

    This is possible, but with some big caveats that might make it not worth it to you.

    Caveat 1: Custom features can edit part properties only if they haven't already been set by a user. That means if you've named it by typing a name, the feature won't work. To make it work you have to hit the blue "Reset all" button in the properties menu, but that unfortunately will clear everything that was there, including the part number. This isn't a big deal if you know ahead of time and work with that in mind, but this is an issue if you have a project with a lot of parts to fix.

    Caveat 2: Custom features can't read a part's properties. That means you can't make a feature that reads the PN and adds it to the name. The feature would need to set the PN, and set the Name, to even know what they are and be able to concatenate them. This means you can't take advantage of Onshape's auto part numbering.

    That's why it might be hard to implement retroactively on a project, but if you plan for this workflow ahead of time on the next one, you could create a custom feature for your org that helps you fill out properties and forces some naming conventions like putting the PN in the name. Here's an example of how simple the code can be.

    FeatureScript 2045;
    import(path : "onshape/std/common.fs", version : "2045.0");
    
    annotation { "Feature Type Name" : "Set name and part number" }
    export const setNameAndNum = defineFeature(function(context is Context, id is Id, definition is map)
        precondition
        {
            annotation { "Name" : "Part", "Filter" : EntityType.BODY, "MaxNumberOfPicks" : 1 }
            definition.body is Query;
    
            annotation { "Name" : "Part name" }
            definition.name is string;
    
            annotation { "Name" : "Part number" }
            definition.num is string;
    
            annotation { "Name" : "Concatenate" }
            definition.concat is boolean;
    
        }
        {
            // delcaring for easier use or substitution later
            const body = definition.body;
            const name = definition.concat ? definition.num ~ "_" ~ definition.name : definition.name; // if concatenated, use number_name, else, use just the name.
            const num = definition.num;
    
            // setting the part name
            setProperty(context, {
                        "entities" : body,
                        "propertyType" : PropertyType.NAME,
                        "value" : name
                    });
    
            // setting the part number
            setProperty(context, {
                        "entities" : body,
                        "propertyType" : PropertyType.PART_NUMBER,
                        "value" : num
                    });
        });

    Evan Reese
    The Onsherpa | Reach peak Onshape productivity
    www.theonsherpa.com
  • Andy_TailsAndy_Tails Member Posts: 5

    Hi Lewis,

    I want to do the same thing as you, our company, already had a preexisting part number scheme before we moved to Onshape, which was

    "Project #""part #"-"revision #"

    Now let us not get caught up on what is the best way to do a part numbering system, this is what we already had and have to stick with it.

    For us, we want to use the Onshape part number generator to keep track of the part numbers, but as the project number changes we would have to manually enter the project number in front of each part number, as that Project # gets referenced other places on our drawings, we have a separate Project # field.

    now we can export the BoM as a CSV and concatenate them together outside of Onshape easy enough, but our drawings need to have 3 columns to show our "singular" part number, this is where being able to combine multiple part properties into one with any desired formatting, symbols, letter, etc. in there as well would be very useful, similar to how you can do this for the export rules, but just applied to a custom property instead.

    I have done quite a bit of extensive feature script testing to get this to work and spoken to Onshape staff who told me in the end that users can't write feature scripts that read properties during feature regeneration, something to do with prevent loops being formed.

    While you can make a feature script that you can manually enter the data into and it will combine it into a new property, it is another whole step and entering data twice, introducing more chance for errors

  • MichaelPascoeMichaelPascoe Member Posts: 2,453 PRO
    edited July 23

    @Andy_Tails Custom feature CAN read part properties, but you have to read the properties within Edit Logic of the feature. Which means in order for it to update or refresh you have to open the feature and click the update button if it has one.

    You can use this feature to get and set properties in the part studio. Remember to click the refresh button when you want it to check for changes when getting:

    Property / Attribute Feature Get or set properties or attributes.
    https://forum.onshape.com/discussion/comment/118567…

    .


    Learn more about the Gospel of Christ  ( Here )

    CADSharp  -  We make custom features and integrated Onshape apps!   Learn How to FeatureScript Here 🔴
Sign In or Register to comment.