Skip to main content

Create custom C# component

· 4 min read
Marco Pellegrino
Nerd Structural Engineer

FEM-Design API is a dotnet library that can be run inside Grasshopper. Our toolbox is intensively develop and you get access to 99% of our functionality/methods.

However, there are cases where writing our own code reduce the amount of components to use and speed-up the maintenance of the code.

How do you do it? Well. I am here to get you covered. Let's start!

Our goal is to be able to create the object PeriodicDiagramRecord which can be found in FemDesign.Core.dll

record

FEM-Design Periodic excitation table

public PeriodicDiagramRecord(string name, double frequency, List<PeriodicCase> cases)

where PeriodicCase is:

public PeriodicCase(double factor, Shape phase, LoadCase LoadCase)

and Shape is:

public enum Shape
{
Cos,
Sin,
}

The step to follow will be described step by step below:

  • place an empty C# component on the canvas

step-1

  • right click on the component and select Manage Assemblies...

manage-assemblies

  • Add FemDesign.Core.dll. If you have install the API through package manager, look for the folder where Rhino install the plug-ins. (C:\Users\XXX\AppData\Roaming\McNeel\Rhinoceros\packages\7.0). Otherwise, you can download the .dll from GitHub.

reference-fem-design-core

  • Rename the input variable and specify the input type.

    name = Item Access - string
    frequency = Item Access - double
    cases = List Access - System.Object

  • Rename the output variable to periodicRecord

components

  • Double click on the C# component and write the code inside private void
var periodicCases = cases.Cast<FemDesign.Loads.PeriodicCase>().ToList();

var record = new FemDesign.Loads.PeriodicDiagramRecord(name, frequency, periodicCases);

PeriodicRecord = record;
  • On the top part of the code editor specify the library to use with using:
using FemDesign;
using FemDesign.Loads;

using System.Linq;

The code on your component should look like the following.

final-component

What the code is doing?

The following section is to specify which tools are necessary to do the job. Think of them as "softwares" that give you some functionalities.

using FemDesign;
using FemDesign.Loads;

using System.Linq;

The next section is where you create the object

var periodicCases = cases.Cast<FemDesign.Loads.PeriodicCase>().ToList();

var record = new FemDesign.Loads.PeriodicDiagramRecord(name, frequency, periodicCases);

PeriodicRecord = record;

name and frequency input variable can be use without any additional code as we have specified them as string and double respectively.

var record = new FemDesign.Loads.PeriodicDiagramRecord(name, frequency, cases);

However, in order to use our variable cases in the code, the object needs to be cast in the object type required by the method. If you remember, we specified the cases input variable as List of System.Object. The Cast method will do the action to cast the object to the right type.

var periodicCases = cases.Cast<FemDesign.Loads.PeriodicCase>().ToList();

You can now use the input and try to create a PeriodicDiagramRecord object.

periodic

You can create name as it is a string, frequency as it is a double. What about cases input?

cases is a FemDesign.Loads.PeriodicCase object and, therefore, you need to create a different component.

public PeriodicCase(double factor, Shape phase, LoadCase LoadCase)

Try to follow the step as you have done before.

periodic-case-comp

  • input type

    factor = Item Access - string
    shape = Item Access - System.Object loadCase = Item Access - System.Object

var oShape = (FemDesign.Loads.PeriodicCase.Shape) Enum.Parse(typeof(FemDesign.Loads.PeriodicCase.Shape), shape);
var periodicCase = new FemDesign.Loads.PeriodicCase(factor, oShape, (FemDesign.Loads.LoadCase) loadCase);

PeriodicCase = periodicCase;

LoadCase is something that it has already been implemented in Grasshopper so you can use the already made component without creating a new one.

workflow

Congratulations! You have finally created your own workflow to create a PeriodicExcitation object in Grasshopper :)

You can download the Grasshopper definition used in this tutorial from here 👉Grasshopper Definition