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.
How can I make an array of points created by a for loop in featurescript?
emory_krall
Member Posts: 15 PRO
for (var i = 0; i < numberOfDowels; i += 1)
{
opPoint(context, id + ("locationPoint" ~ i), {
"point" : toWorld(cSys, vector((fenceDistance + (dowelSpacing * i)), 0 * inch, 0 * inch))
});
}
var locationPoints is array = evaluateQuery(context, qCreatedBy(id + ("locationPoint" ~ i), EntityType.BODY));
This gives me an error that the variable "i" is not found. If place the variable declaration within the for loop I can't reference it outside the loop, which is what I need to do.
Tagged:
0
Best Answers
-
NeilCooke Moderator, Onshape Employees Posts: 5,671Another way (more id compliant) is:
var locationPoints = []; for (var i = 0; i < numberOfDowels; i += 1) { opPoint(context, id + ("locationPoint" ~ i), { "point" : toWorld(cSys, vector((fenceDistance + (dowelSpacing * i)), 0 * inch, 0 * inch)) }); locationPoints = append(locationPoints, qCreatedBy(id + ("locationPoint" ~ i), EntityType.BODY)); } locationPoints = evaluateQuery(context, qUnion(locationPoints));
There are a few other ways besides.Senior Director, Technical Services, EMEAI6 -
Jake_Rosenfeld Moderator, Onshape Employees, Developers Posts: 1,646I would do this like this:
const parentId = id + "points"; for (var i = 0; ...) { opPoint(context, parentId + toString(i), {...}); } const locationPoints = evauateQuery(context, qCreatedBy(parentId, EntityType.BODY));
Since ids are hierarchical, if you nest a bunch of child ids under a parent, you can just evaluate a query on the entire parent, and the query will be evaluated for the entire group of ids contained by the parent.
Note that:id + ("points" ~ i)
creates ids that are like this "rootId > points0" "rootId > points1" "rootId > points2" (where the > is representing the transition of one id component to the next)
but:(id + "points") + toString(i) // which is equivalent to: const parentId = id + "points"; parentId + toString(i)
creates ids that are like this "rootId > points > 0" "rootId > points > 1" "rootId > points > 2". Then, because the ids are structured in a nice way, you can just do evaluations on parentId ("rootId > points"), and it will capture all of the ids which are children of that.Jake Rosenfeld - Modeling Team6
Answers
This is the most elegant way but may fall down if you have other similar loops in your code (due to id hierarchy):
There are a few other ways besides.
Since ids are hierarchical, if you nest a bunch of child ids under a parent, you can just evaluate a query on the entire parent, and the query will be evaluated for the entire group of ids contained by the parent.
Note that:
creates ids that are like this "rootId > points0" "rootId > points1" "rootId > points2" (where the > is representing the transition of one id component to the next)
but:
creates ids that are like this "rootId > points > 0" "rootId > points > 1" "rootId > points > 2". Then, because the ids are structured in a nice way, you can just do evaluations on parentId ("rootId > points"), and it will capture all of the ids which are children of that.