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.

Writing a measured value to a variable in the model tree and keeping it updated.

Chris_LoughnaneChris_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.

Comments

  • abefeldmanabefeldman Member Posts: 166 ✭✭✭
    edited July 2017
  • Chris_LoughnaneChris_Loughnane Member Posts: 4 PRO
    Using the suggestions above made a slight adjustment in order to measure length

    <div>annotation { "Feature Type Name" : "Measure Length", "Feature Name Template" : "###name = #value", "UIHint" : "NO_PREVIEW_PROVIDED" } &nbsp; &nbsp;</div><div>export const measureLength = defineFeature(function(context is Context, id is Id, definition is map)</div><div>&nbsp; &nbsp; precondition</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; annotation { "Name" : "Name", "Default" : "length" }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; definition.name is string;</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; annotation { "Name" : "First entities", "Filter" : EntityType.EDGE }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; definition.first is Query;</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; // annotation { "Name" : "Second entities", "Filter" : EntityType.FACE || EntityType.EDGE || EntityType.VERTEX || EntityType.BODY }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // definition.second is Query;</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; // annotation { "Name" : "Maximum" }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // definition.maximum is boolean;</div><div>&nbsp; &nbsp; }</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; if (!match(definition.name, '[a-zA-Z_][a-zA-Z_0-9]*').hasMatch)</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw regenError(ErrorStringEnum.VARIABLE_NAME_INVALID);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; const lengthResult is ValueWithUnits = evLength(context, {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "entities" : definition.first</div><div>&nbsp; &nbsp; &nbsp; &nbsp; });</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; // if (!tolerantEquals(p0, p1))</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; &nbsp; debug(context, line(p0, p1 - p0));</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; &nbsp; debug(context, line(p1, p0 - p1));</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // }</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // else // If it's the same point, just draw the point</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // &nbsp; &nbsp; debug(context, p0);</div><div>&nbsp; &nbsp; &nbsp; &nbsp; // }</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; setVariable(context, definition.name, lengthResult);</div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; setFeatureComputedParameter(context, id, { "name" : "value", "value" : lengthResult });</div><div>&nbsp; &nbsp; });</div><br>

Sign In or Register to comment.