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.
Iterating FeatureScript code not running
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
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"
tosketchId + "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 thatsketchId
into 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!inside loop, The way it is written above you'll get ids ...sketch.0, ....sketch.0.1, etc.
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!