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.

For Loop For Dummies

EvanReeseEvanReese Member, Mentor Posts: 2,117 ✭✭✭✭✭
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
Tagged:

Best Answer

Answers

  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,671
    If you search public docs for “Pretty Patterns” you will find how to do this using regular features. If you want to do this in code I will take a look later. 
    Senior Director, Technical Services, EMEAI
  • EvanReeseEvanReese Member, Mentor Posts: 2,117 ✭✭✭✭✭
    NeilCooke said:
    If you search public docs for “Pretty Patterns” you will find how to do this using regular features. If you want to do this in code I will take a look later. 
    Thanks, Neil! I'm more interested in the exercise than the results, so I'd really appreciate input on doing it with featurescript.

    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.
    Evan Reese
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,671
    In the j loop you put j in the evaluation part of the for loop - that is why it is infinite - should it be i ? Also i would evaluate all the transforms for all the loops first, then just call opPattern once.
    Senior Director, Technical Services, EMEAI
  • EvanReeseEvanReese Member, Mentor Posts: 2,117 ✭✭✭✭✭
    Thanks, Neil! I don't have time to review at the moment, but it sounds like this ought to be enough to get me there. In the meantime, I've been trying to figure out the logic for an additional Fibonacci pattern function of my Speaker Pattern custom feature and I just got it to work!

    Evan Reese
  • NeilCookeNeilCooke Moderator, Onshape Employees Posts: 5,671
    Nice
    Senior Director, Technical Services, EMEAI
  • EvanReeseEvanReese Member, Mentor Posts: 2,117 ✭✭✭✭✭
    NeilCooke said:
    I 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>

    Thanks again for the help, Neil! I ended up needing your "spoiler" but I made sure I re-wrote it to prove some level of understanding. For example, I didn't know you could just call skplane.x for the x-direction and some things that must be basic. This also helped me make progress on the Fibonacci version of the script. Now I just need to polish up the UI, and add some simpler stuff like draft and booleans, and combine my different pattern styles into one custom feature. I'm pretty jazzed up about it.


    Evan Reese
  • romeograhamromeograham Member, csevp Posts: 676 PRO
    This looks like a great exercise, and a great feature. I hope you share it when you feel it's ready!

    romeo
  • EvanReeseEvanReese Member, Mentor Posts: 2,117 ✭✭✭✭✭
    This looks like a great exercise, and a great feature. I hope you share it when you feel it's ready!

    romeo
    Sure! I'll keep it public once it's done.
    Evan Reese
Sign In or Register to comment.