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.

Iterating FeatureScript code not running

aaron_skidmoreaaron_skidmore Member Posts: 34 ✭✭
edited October 2020 in FeatureScript

Hi Guys, I got my feature script running ok for 1 selected edge, then I tried to add the looping code for multiple edges.

But I keep getting the sketch.0 used at two non-contiguous points in operation history.

Relevant code below


var sketchId = id + "sketch";

var MyEdges = evaluateQuery(context, definition.edgesel);

 var cnt = size(MyEdges);

    for (var i = 0; i < cnt; i += 1)

  {  sketchId = sketchId + i;

       var edge = MyEdges[i];

// xDirection etc set for each edge - code ommitted

      var cSys is CoordSystem = coordSystem(endPosition, xDirection, zDirection);

       var sketchPlane is Plane = plane(cSys);

       var sketch1 = newSketchOnPlane(context, sketchId, {

          "sketchPlane" : sketchPlane });

       skRectangle(sketch1, "rect1", {"firstCorner" : vector(0, 1) * millimeter, "secondCorner" : vector(10, -10) * millimeter          });

       skSolve(sketch1);

/// do some stuff with sketch here - code ommitted

       opDeleteBodies(context, id + "delete_sketch", { "entities" : qCreatedBy(sketchId, EntityType.BODY) });

} //end of loop

Comments

  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565
    edited October 2020

    The documentation for Id gives a clearer picture of why you're getting the error "Parent Id asdf1234.myOperationId used at two non-contiguous points in operation history"

    In your case you can fix it by changing id + "delete_sketch" to sketchId + "delete_sketch" , (possibly changing other ids in the omitted code too). This makes sure that opDeleteBodies is performed with same parent Id as the operations before and after it, changing the multiple "non-contiguous" uses of that sketchIdinto one long, contiguous use.

    Notably, it also fixes the next issue the script would have run into, a duplicate id id + "delete_sketch" at the end of the loop!

  • lanalana Onshape Employees Posts: 689
    edited October 2020
    Also you probably wanted to have 
    const sketchId = id + "sketch" + i;

    inside loop, The way it is written above you'll get ids ...sketch.0, ....sketch.0.1, etc.

  • kevin_o_toole_1kevin_o_toole_1 Onshape Employees, Developers, HDM Posts: 565

    Separately:

    For cases like this, where you are performing operations on several individual entities from a query, I recommend the new forEachEntity() function, just released in the latest version of std and Onshape. Not only does it stop you from ever accidentally using a non-unique "id" inside the loop, it also ensures the parts you're creating inside the loop will maintain their identity in a more reasonable way in future operations, whenever your user changes the contents of "MyEdges".

    You can learn more (and see its origin story) in this forum post!

Sign In or Register to comment.