Welcome to the Onshape forum! Ask questions and join in the discussions about everything Onshape.
First time visiting? Here are some places to start:- Looking for a certain topic? Check out the categories filter or use Search (upper right).
- Need support? Ask a question to our Community Support category.
- Please submit support tickets for bugs but you can request improvements in the Product Feedback category.
- 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.
Writing a measured value to a variable in the model tree and keeping it updated.
Chris_Loughnane
Member Posts: 4 PRO
Essentially I'm trying to recreate a 'sensor' or User-Defined Measurement. In the simplest case I have an arc, would like to evaluate it's length, then store that number in a variable so that I can reference it later in my model tree.
I'm just jumping into featurescript so it's not immediately obvious to me how to do this, if I can do it, or if it's already been done.
I'm just jumping into featurescript so it's not immediately obvious to me how to do this, if I can do it, or if it's already been done.
Tagged:
0
Comments
https://cad.onshape.com/documents/77baa8153589a7fc5f289829/w/cffd0f2a7077380d5378a885/e/d3174bf5315e6aafcb889367
UX/PD/Community Support
<div>annotation { "Feature Type Name" : "Measure Length", "Feature Name Template" : "###name = #value", "UIHint" : "NO_PREVIEW_PROVIDED" } </div><div>export const measureLength = defineFeature(function(context is Context, id is Id, definition is map)</div><div> precondition</div><div> {</div><div> annotation { "Name" : "Name", "Default" : "length" }</div><div> definition.name is string;</div><div><br></div><div> annotation { "Name" : "First entities", "Filter" : EntityType.EDGE }</div><div> definition.first is Query;</div><div><br></div><div> // annotation { "Name" : "Second entities", "Filter" : EntityType.FACE || EntityType.EDGE || EntityType.VERTEX || EntityType.BODY }</div><div> // definition.second is Query;</div><div><br></div><div> // annotation { "Name" : "Maximum" }</div><div> // definition.maximum is boolean;</div><div> }</div><div> {</div><div> if (!match(definition.name, '[a-zA-Z_][a-zA-Z_0-9]*').hasMatch)</div><div> throw regenError(ErrorStringEnum.VARIABLE_NAME_INVALID);</div><div> </div><div> const lengthResult is ValueWithUnits = evLength(context, {</div><div> "entities" : definition.first</div><div> });</div><div><br></div><div> // if (!tolerantEquals(p0, p1))</div><div> // {</div><div> // debug(context, line(p0, p1 - p0));</div><div> // debug(context, line(p1, p0 - p1));</div><div> // }</div><div> // else // If it's the same point, just draw the point</div><div> // {</div><div> // debug(context, p0);</div><div> // }</div><div><br></div><div> setVariable(context, definition.name, lengthResult);</div><div><br></div><div> setFeatureComputedParameter(context, id, { "name" : "value", "value" : lengthResult });</div><div> });</div><br>