How To Add Functions To OpenEco

In today’s tutorial the user will understand the framework of OpenEco Programming and how to alter the program to add/delete functions. At the conclusion of the tutorial the Meshing component will have an additional input dictating Project North, and the user will be able to change the orientation of North in Ecotect (A zero value indicates North as the Y-axis) through Grasshopper.

To refresh everyone’s memory, the basic programming structure of OpenEco is segregated in to two parts: Ecotect Functions and Grasshopper Components. An outline of the hierarchy can be found in the original OpenEco tutorial. The Ecotect functions are written in C# and the GH Components are written in VB.Net. It would benefit the user to become familiar with both languages prior to altering the code.

Initially the user should open up the folder labeled Open-Eco and navigate to the OpenEco.CSPROJ file, open the file in a scripting program and make sure the OpenEco class as well as any other class is open. In the case of the tutorial we will only be adding a function to the model class therefore I have that class open. If you are attempting to add a new class, you must first copy another class file, rename it, and add an existing item to the project.

If you are prompted to upgrade or see an upgrade tab DO NOT do so because it may alter the program’s functionality. The screen should look somewhat like the image below:

First, you decided to add a new class, you must go into the OpenEco Class to define the new class as well as a short nickname. For example the code currently looks like the following:

 

	public class OpenEco : IOpenEco
	{
		//PUBLIC OBJECTS
	public Application app;
        public Model model;
	public Weather weather;
        public Object obj;
        public Calculation calc;
        public ObjectRequest objreq;
        public FitGrid fit;
        public Zone zon;

		//PUBLIC PROPERTIES

		//PUBLIC METHODS

		//connect
		public void connect(){
	    cmd = new NDde.Client.DdeClient("Ecotect", "execute");
	    cmd.Connect();
	    rq = new NDde.Client.DdeClient("Ecotect", "request");
	    rq.Connect();

			//Initialize public objects
	    app = new Application(cmd, rq);
	    model = new Model(cmd, rq);
	    weather = new Weather(cmd, rq);
	    obj = new Object(cmd, rq);
            calc = new Calculation(cmd, rq);
            objreq = new ObjectRequest(cmd, rq);
            fit = new FitGrid(cmd,rq);
            zon = new Zone(cmd,rq);
		}

All that needs to be done to add an additional class would be to define a new public object and then initialize the public object. Again for this tutorial we will only be adding a function to an existing class, therefore open the model.cs. In order to add an Ecotect function we must speak the language which Ecotect understands. the scripting commands can be found under the help tab with in Ecotect. The function this tutorial works with is the following:

The string we must provide will look slightly different then defined in the help file. set(“project.north”, north) actually translates to “set.project.north 0″ where the 0 must be defined as an input so the following changes must be made:

using System;

namespace openEco
{
	public class Model : IOpenEco
	{
		//PRIVATE VARIABLES

		//PUBLIC PROPERTIES

		//PUBLIC METHODS

		//create a new model
		public void newModel(){
			string myNewModel = "model.new";
			cmd.Execute(myNewModel, 1000);
		}

		//update the current model
		public void update(){
			string myUpdate = "model.update";
			cmd.Execute(myUpdate, 500);
		}

		public void material(int obje, int mat){
			string matname = "set.object.material " + obje + "" + "Plastic";
			cmd.Execute(matname,1000);
		}

		//CONSTRUCTORS

		//call the base constructor
		public Model(NDde.Client.DdeClient c, NDde.Client.DdeClient r) : base(c,r){
		}

	}
}

We will be adding a new public void called projectnorth, set a string with the command line, and submit the request similar to the functions already defined in the class. Set the base function to look like the following:

public void projectnorth(int north){

		}

Notice, we are declaring a integer north, this tells the function to expect a variable when executing. Therefor when we call to the function from within our grasshopper component we will be prompted to submit an integer. The next step involves setting the string command within the function:

string setnorth = "set.project.north " + north;

This must be followed by a request to Ecotect in the following manner, declaring the string command and the timeout value:

cmd.Execute(setnorth,1000);

The change should look like the following:

using System;

namespace openEco
{
	public class Model : IOpenEco
	{
		//PRIVATE VARIABLES

		//PUBLIC PROPERTIES

		//PUBLIC METHODS

		//create a new model
		public void newModel(){
			string myNewModel = "model.new";
			cmd.Execute(myNewModel, 1000);
		}

		//update the current model
		public void update(){
			string myUpdate = "model.update";
			cmd.Execute(myUpdate, 500);
		}

		public void material(int obje, int mat){
			string matname = "set.object.material " + obje + "" + "Plastic";
			cmd.Execute(matname,1000);
		}
		public void projectnorth(int north){
			string setnorth = "set.project.north " + north;
			cmd.Execute(setnorth,1000);
		}

		//CONSTRUCTORS

		//call the base constructor
		public Model(NDde.Client.DdeClient c, NDde.Client.DdeClient r) : base(c,r){
		}

	}
}

Build the solution and this concludes the changes necessary in OpenEco.CSProj. Next we will open the VB.Net Project File. Navigate to the folder Grasshopper Components->G2E Node -> G2E Node -> G2E_Node.vbproj. Make sure to click on the one shown below:

Make sure the meshing class is open. If you plan to add an additional component add a new class in the project, and follow the tutorial “How To Build A Grasshopper Component in Visual Studio in VB.Net”. From here all that is necessary if to declare our new input, assign a variable to send to ecotect, and call our external function.

First add the new input parameter:

        pManager.Register_StringParam("Run Import", "R", "Import the Mesh", "False")
        pManager.Register_MeshParam("Mesh", "M", "Import Mesh", GH_ParamAccess.tree)
        pManager.Register_MeshParam("Shader", "S", "Shading Element", GH_ParamAccess.tree)
        pManager.Param(2).Optional = True
        pManager.Register_IntegerParam("Scale Model", "[Sc]", "Scale model if not in mm", 0)
        pManager.Register_IntegerParam("Material", "Mat", "Set Material (Default is Plastic)", 5)
        pManager.Register_IntegerParam("Project North", "N", "Set Project North in Angle Deviation from the Y-Axis", 0)

The parameter we set has a default value of 0 thus setting our Project North to Y-Axis if no other input is provided. Next define a new variable and use the DA.GetData function to retrieve the input:

        Dim Run As String = Nothing
        Dim FL As String = Nothing
        Dim Meshes As New Data.GH_Structure(Of GH_Mesh)
        Dim Shader As New Data.GH_Structure(Of GH_Mesh)
        Dim MeshFace As New Rhino.Geometry.MeshFace
        Dim eco As openEco.OpenEco = New openEco.OpenEco
        Dim myPointList As IEnumerable(Of Point3f)
        Dim myPointA As Point3f
        Dim mypointB As Point3f
        Dim mypointC As Point3f
        Dim mypointD As Point3f
        Dim currentmesh As GH_Mesh
        Dim numfaces As Integer
        Dim masknum As Integer = 0
        Dim scale As Integer
        Dim material As Integer
        Dim north As Integer

        If (Not DA.GetData(0, Run)) Then Return
        DA.GetDataTree(1, Meshes)
        DA.GetDataTree(2, shader)
        DA.GetData(3, scale)
        DA.GetData(4, material)
        DA.GetData(5, north)

Finally, call to the external function in the following way (I placed it right after the function to create a new model):

eco.model.projectnorth(north)

Build the solution and the modified component is now available in grasshopper:

North = 0
North = 90

Leave a Reply