List Of Points With A Loop Example


Introduction

The example is based on where we left off in hands on Example 1: GC - Point By Function Example, so please complete that one first before continuing here.

For more examples refer to the GenerativeComponents Documentation page.

Now that we have created a Point Feature we may want to use a for loop to create multiple instances of it. This is similar to using a Series as an input to the X position for instance, but in this case we are writing the script ourselves rather then using an existing global function like Series. This gives us much more flexibility later one.

Adding a for loop

We will add a for loop to our Point script by using the statement builder again. Position your cursor into a new line after the opening bracket of the function and choose the for statement from the statement builder.

The script will look like this then:

function (CoordinateSystem cs)

{

    for (int index = 0; index < count; ++index)

    {

 

    }

    Point pt01 = new Point(this);

    pt01.ByCartesianCoordinates(cs, 3, 2, 0 );

}

We need again to replace the placeholders from the statement builder with things that make sense in this function context and also move the closing bracket of the for loop after the pt01.ByCartesianCoordinates statement.

As the index variable we usually use single letters such as I, j or k- let’s use i here. Count specifies how many times the content in the loop is being run, we can say for now 10 as a fixed value and later replace that number with a variable that may become an input argument like cs to change the number of Points from the outside.

The corrected script from above will look like this then

function (CoordinateSystem cs)

{

    for (int i = 0; i < 10; ++i)

    {

       Point pt01 = new Point(this);

       pt01.ByCartesianCoordinates(cs, 3, 2, 0 );

    }

}

To indent the section inside the for loop bracket highlight the lines and press on the indent line Indent tool  in the toolbar

The for loop statement

for (int i = 0; i < 10; ++i)

therefore declares a local variable i of Type int and sets its start value to 0 and increments its value by 1 based on the statement ++i which adds 1 every time the loop is iterated as long as i<10.

Using the for loop variable

Anything inside the for loop brackets is run as many times as the statement above specifies. Therefore our Point creation inside the loop creates ten Points as is, but they will all be positioned in the same location, so we need to adapt their X position for instance with respect to the loop variable i. if we do so the script will look like this

pt01.ByCartesianCoordinates(cs, 3*i, 2, 0 );

This will increment the X position of the Point by 3 with each cycle of the loop and create the following output if we close the script editor and apply the changes.

Making the number of for loops an input variable

If we want to control the number of Points through the input arguments instead of our hard coded 10 we can add it to the input argument list as follows. We add a new argument to the input arguments separated by a comma, and of Type int and give it a name, in this case we call it num. Then we replace the explicit value 10 with our new variable num.

function (CoordinateSystem cs, int num)

{

    for(int i = 0; i < num; ++i)

    {

        Point pt01 = new Point(this);

        pt01.ByCartesianCoordinates(cs, 3*i, 2, 0 );

    }

}

If we close the script and hit apply now we will see the following error from the script debugger saying that our Function require at least 2 argument(s) but only 1 were provided – this means we forgot to add the second argument in our argument list in the ByFunction method

Let’s close the debugger window and add the second argument to the List of FunctionArguments – so instead of {baseCS} we will write {baseCS, 5} to get 5 Points for instance. Hitting apply will create the following output as desired.

This concludes the introduction of simple loop structures in scripts.