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.

Issue Assigning Value to Company Custom Property

Zane_MorrisZane_Morris Member Posts: 3 PRO

I attempted to create a feature script, and when trying to use it, am getting regeneration errors.

Our company uses mechanical cables. For these mechanical cable parts, I want a 'Cut Length' property. These parts are modelled using sweeps, and I want the cut length value to be linked to the sketch entities used as the sweep path, so that subtle changes to the surrounding geometry also update the cut length.

Below is the desired functionality of the feature script.

  • Within a part studio, select a series of sketch entities or a polyline that make up a sweep's 'path'.
  • Then select the part to which I want to update the 'Cut Length' property.
  • Have the value of the 'Cut Length' custom property for this part be updated as the sum of all selected sketch entities and/or polylines.
  • It is likely that there will be multiple mechanical cable parts designed within a single part studio. So, the solution must be able to handle this part-specific property assignment.

Here are the steps I took to implement such functionality.

  1. Created a 'Mechanical Cable' category in my company settings. The scope of this category is 'Part'. The parent category for this category is 'Onshape Part'.
  2. I made active (or published) the category.
  3. Created a 'Cut Length' custom property in my company settings. It's type is 'Value with units', the unit type is 'Length', and its default value is 0 mm. I have toggled on both the 'Edit value in version' and 'Edit value in workspace' options, but left the subsequent 'Edit only through the API' setting toggled off. I also left toggled off the 'Compute part property' option. I assigned the property to the 'Mechanical Cables' category, and made it active.
  4. I made the feature script included below. This was made with the help of ChatGPT (I am not familiar at all with JavaScript). The script was committed without any warnings.
  5. In the same document that the feature studio was created, I made a part via a simple sweep.
  6. I assigned the category 'Mechanical Cable' to it, and saved the property changes.
  7. Going back into the properties window, the 'Cut Length' property shows up, with the correct default value of '0 mm'.
  8. I tried running the script. For the first test, I used a circular profile. The path entities were the sketch entities used to originally make the sweep. I was informed (by ChatGPT) that sketch entities may not work the best, and that I should try actual edges of the swept body. So, I tried a square profile sweep and selected one of its edges as the path. Again, did not work. Both times I received the error 'Error Regenerating'.

Here is the aforementioned script.

FeatureScript 2909;
import(path : "onshape/std/common.fs", version : "2909.0");

annotation { "Feature Type Name" : "Compute CL Length" }
export const assignPathLength = defineFeature(function(context is Context, id is Id, definition is map)
  precondition
  {
  annotation {
    "Name" : "Path edges",
    "Filter" : EntityType.EDGE,
    "MaxNumberOfPicks" : 1000
    }
    definition.path is Query;
      
  annotation {
                  "Name" : "Target parts",
                  "Filter" : EntityType.BODY,
                  "MaxNumberOfPicks" : 1
              }
  definition.parts is Query;
  }
  {
      var totalLength = evLength(context, {
              "entities" : definition.path
          });
  
      for (var part in evaluateQuery(context, definition.parts))
      {
          setProperty(context, {
                      "entities" : part,
                      "name" : "Cut Length",
                      "value" : totalLength
                  });
      }
  });

Does anyone know what I'm doing wrong? Is it possible to write a value in this way to a category-specific company custom property? Are any of my selection filters incorrect? Or do I need to use a different setProperty function? Any help would be greatly appreciated. PS. the indentation in the code above may not be perfect due to copy+paste into the post's window.

Sign In or Register to comment.