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.

Options

Creating polygons with a for loop?

I would like to create multiple polygons with a for loop depending on some info from the user, right now i just create one at a time because i cant iterate an id name?

Now i go like:

skRegularPolygon(sketch, "polygon2", {
                "center" : pointArray[2],
                "firstVertex" : vector(pointArray[2][0]+(4)*millimeter, pointArray[2][1]+(4)*millimeter),
                "sides" : 4
            });
            skRegularPolygon(sketch, "polygon3", {
                "center" : pointArray[3],
                "firstVertex" : vector(pointArray[3][0]+(4)*millimeter, pointArray[3][1]+(4)*millimeter),
                "sides" : 4
            });
            skRegularPolygon(sketch, "polygon4", {
                "center" : pointArray[4],
                "firstVertex" : vector(pointArray[4][0]+(4)*millimeter, pointArray[4][1]+(4)*millimeter),
                "sides" : 4
            });


BUT.. i would like to do something like

        for (var i = 0; i < count; i += 1)
        {
             skRegularPolygon(sketch, "polygon" + i, {
                "center" : pointArray[i],
                "firstVertex" : vector(pointArray[i][0]+(4)*millimeter, pointArray[i][1]+(4)*millimeter),
                "sides" : 4
            });
        }

It is the ID that gives me an error because it needs to be unique..

Best Answer

Answers

  • Options
    lemon1324lemon1324 Member, Developers Posts: 223 EDU
    You can also do something like id + i + "polygon", which will also work.  I tend to use this structure when there's multiple different features within the loop, so all features at loop iteration i have Id prefix of id + i.
    Arul Suresh
    PhD, Mechanical Engineering, Stanford University
  • Options
    lanalana Onshape Employees Posts: 693
    @lemon1324
    That is a sketch entity id - it has to be a string, not Id. Sorry we made it confusing. 
Sign In or Register to comment.