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

  • Options
    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. 
  • Options
    Evan_ReeseEvan_Reese Member Posts: 2,064 PRO
    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 / Principal and Industrial Designer with Ovyl
    Website: ovyl.io
Sign In or Register to comment.