Point By Function Example


Introduction

In this example we will build up a very simple function based script to create a Point Feature using the ByCartesianCoordinates method as the creation method.

For more examples refer to the GenerativeComponents Documentation page.

Create a new .gct file

To start we open a new transaction file using the new transaction file as in any other GenerativeComponents file.

Create a Point with the ByFunction method

We will create a point Feature as usual from the Feature List, but rather than directly using one of the model based methods we will use a function. To do so choose Point->ByFunction as the method.

Open up a script editor window to write the function

Clicking on the script editor  will invoke the script editor window. This is where we will write the function to generate the Point in using the GCScript language introduced before.

Setting up the function using the Statement Builder

Clicking on the statement builder  in the script editor console invokes a list of script statement templates that make it easier to create standard scripting expression. We will use it to first create a function wrapper.

Select function from the drop down list. The statement should look like this. The brackets denote the beginning and the end of the function body. Arguments is the placeholder for any input arguments we may define.

function (arguments)

{

 

}

 

Specifying the input arguments to the function

First we define the input arguments by replacing the placeholder argument with our own inputs to this specific function. Since we want to create a Point ByCartesianCoordiantes we will need a reference to a CoordinateSystem so we need to pass a reference to our function to an outside CoordinateSystem. To do so we need to specify the Type of the input argument and a variable name, that will contain the input value within the boundaries of our function. This concept is also referred to as scope. It means the variable we define here is only defined within the scope or range of our function. We can use any variable name, but to make understanding easier let’s choose cs as a short form for CoordinateSystem. As the type we declare it to be of type CoordinateSystem as that is what we expect the user to pass in as an argument.

function (CoordinateSystem cs)

{

 

}

 

Creating the internal Variable for the new Point Feature

Next we need to define a variable of Type Point to hold our to-be-created Point Feature inside the function. To do so we declare a new variable and call it pt01. To initialize it we call the new Point(parentFeature) function from the Feature function list in  under the tab Feature Types.

There are several initialization methods listed here, but in this case we want the Point in the function to become a parent of the Point Feature we are writing the ByFunction method for, therefore we are using the third method new Point(parentFeature) (the {,featureName] indicates an optional input that is not required, we will not use it here).

The reason to create the Point in the function as a child to the top level Point is to refer to it with the same name as its parent, which is useful if we make a list of points later.

The function should look like this now:

function (CoordinateSystem cs)

{

    Point pt01 = new Point(parentFeature)

}

 

In order to complete this step we need to replace the parentFeature placeholder with a reference to the parent. In this case we said we want to parent the point to the Point we are writing the function in. To do so one uses the keyword this. It makes the new Point a child of the Point this is being called in.

function (CoordinateSystem cs)

{

    Point pt01 = new Point(this)

}

 

The last thing is to add a ";" at the end of the line to let our script editor know that it is the end of the expression. The script should look like this now. 

function (CoordinateSystem cs)

{

    Point pt01 = new Point(this);

}

 

Instantiating the Point Feature using ByCartesianCoordinates

Although we initialized the Point pt01 it still is not created yet. To do so we need to choose a method from our method list for Points. We will use ByCartesianCoordinates. But we will use the variable pt01 that refers to our new Point object to call the method using the dot operator. Once we type pt01. The drop down list will display all the available methods.

Choosing the ByCartesianCoordinates method inserts the template shown below:

function (CoordinateSystem cs)

{

    Point pt01 = new Point(this);

    pt01.ByCartesianCoordinates(CoordinateSystem, Xtranslation, Ytranslation, Ztranslation [, Origin] )

}

 

We need to replace the placeholder input arguments with values or variables that are valid in our function. The CoordinateSystem will be replaced with a reference to our input argument CoordinateSystem, which we called cs in this function. The X ,Y, Z value we can replace with fixed values for now – the [, Origin} again shows an optional value, which we will not use for now so we can delete it. Then the function looks like this.

function (CoordinateSystem cs)

{

    Point pt01 = new Point(this);

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

}

 

Using the ByFunction method with input arguments

Now that our simple function is completed we can close the script editor and use it. To do so we need to pass it one input argument, since we defined an input argument of type CoordinateSystem. This is specified in the ByFunction method in the input field below the Function and it is a list of input arguments, in our case with one element in it, the baseCS. So it will read {baseCS}. Pressing Apply will pass the argument to our function and through the cs variable we defined to the ByCartesianCoordinates method that creates the Point. The Point appears in red as a child Feature of the top level Point point01 that hosts our function. It will look like this.

This example continues with the GC - List Of Points With A Loop Example.