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.
For Loop For Dummies
EvanReese
Member, Mentor Posts: 2,081 ✭✭✭✭✭
I'm trying to create a feature that will make a hole pattern like this ubiquitous one from Dieter Rams. I've got the intent represented with standard features and an equation to calc the number of holes for each ring. I've also made some good progress fumbling through writing the code with help from a friend that does some other kinds of coding, but doesn't know featurescript or CAD. Together, we were able to get a cylinder extruded and patterned linearly using a for loop, which is the first step. Separately, I've also got a circular pattern working with a for loop, but I don't know how to combine them. I'm hung up on addressing each iteration of the first loop individually to also do a circular pattern. Do I need a nested for loop? Or should I somehow evaluate each instance after my first for loop and do a second for loop after my first one? I think I accidentally created an infinite loop trying this. You can check out my model and code here. I'm looking for hints more than answers since this is mostly a learning exercise and I want it to stick. My coding skill level is "beginner", so don't be afraid to say something that seems obvious. Any helpful nudges are very appreciated!
Evan Reese
0
Best Answer
-
NeilCooke Moderator, Onshape Employees Posts: 5,648I know you wanted to work it out for yourself, so if that is still the case, don't watch the spoiler
var axis = line(origin, normal); var transforms = []; var instanceNames = []; for (var i = 1; i < rings; i += 1) { var num = round(((i * (dist) * PI * 2) / (dist))); for (var j = 0; j < num; j += 1) { transforms = append(transforms, rotationAround(axis, (360 / num) * j * degree) * transform(skplane.x * i * dist)); instanceNames = append(instanceNames, toString(i) ~ "-" ~ j); } } opPattern(context, id + "pattern1", { "entities" : body, "transforms" : transforms, "instanceNames" : instanceNames });<br>
Senior Director, Technical Services, EMEAI7
Answers
The image I showed is my own model, so I know I can accomplish it with standard features anyway. That Pretty Patterns doc has some interesting stuff though and I did work through setting mine up more parametrically by incrementing my variables like the "Circular Pattern" example so thanks for the pointer. I learned something new. I did notice that in the example both circular patterns have "apply per instance" checked, when only the last one needs it. Unchecking the first one made the second pattern compute almost 5 times faster. since my feature has so many more hole instances, the difference is even more extreme. I think that a custom feature could be even faster since there's no need to pattern sketches and extrude each one. It would be as lightweight as a face pattern.
romeo