Scripting Guide 3.1.4
  • Welcome to Karamba3D Scripting Guide
  • 1. Introduction
    • 1.1: Scripting with Karamba3D
    • 1.2: Basics
  • 2. Scripting with Karamba3D inside Grasshopper
    • 2.1: Hello Karamba3D
    • 2.2: Data Retrieval from Models
    • 2.3: How to Create Structural Models
    • 2.4: How to Modify Structural Models
      • 2.4.1: Cross section Optimization
      • 2.4.2: Activation and Deactivation of Elements
    • 2.5: Data Export from Karamba3D
    • 2.6: The VB Script Component
    • 2.7: The IronPython Component
      • 2.7.1: Results Retrieval on Shells
      • 2.7.2: A Simplified ESO-Procedure on Shells
    • 2.8: The Python 3 Component
      • 2.8.1: Hello Karamba3D
      • 2.8.2: Data Retrieval from Models
      • 2.8.3: How to Create Structural Models
      • 2.8.4: How to Modify Structural Models
        • 2.8.4.1: Cross section Optimization
        • 2.8.4.2: Activation and Deactivation of Elements
  • 3. How to create your own Karamba3D Grasshopper-component
    • 3.1: Setting up a Visual Studio Project for GH Plug-ins
    • 3.2: Basic Component Setup
    • 3.3: How to Reference Karamba3D Assemblies
    • 3.4: Input- and Output-Plugs
    • 3.5: Adding Functionality to a GH Component
  • Bibliography
Powered by GitBook
On this page
  1. 2. Scripting with Karamba3D inside Grasshopper

2.3: How to Create Structural Models

Previous2.2: Data Retrieval from ModelsNext2.4: How to Modify Structural Models

Last updated 6 months ago

Karamba3D-components are split in two parts: one manages the graphical user interface, unit conversions and default values, the other handles the functionality.

Let’s take the “LineToBeam”-component as an example:

  • The class “Component_LineToBeam_GUI” in namespace “Karamba.GHopper.Elements” derives from Grasshopper’s “GH_Component” and provides the visual component properties.

  • “LineToBeam”, a static class in namespace “Karamba.Elements” features the static method “solve(...)” which executes the actual tasks and gets used by “Component_LineToBeam_GUI”.

Generally the names of classes which belong to the GUI start with “Component” and belong to the namespace “Karamba.GHopper”. Since there are static solve-methods for all components it would be possible to build a model using only these. This would entail two disadvantages:

  • The solve-methods do not provide default values for their arguments, so one has to provide them explicitly.

  • In later versions of Karamba3D the order and number of arguments of the solve-methods might change.

One way to mitigate these problems is to refrain from direct object creation and use a factory-pattern instead. See for further information on this topic. In the script below a structural model gets assembled and output: it consists of a vertical cantilever-beam with a point-load on top (see fig. 2.3.1).

This is the source-code inside the C#-component (see example “ModelCreation.gh”):

...
using Karamba.Utilities;
using Karamba.Geometry;
using Karamba.CrossSections;
using Karamba.Supports;
using Karamba.Loads;
...
private void RunScript(ref object Model_out)
{
  var logger = new MessageLogger();
  var k3d = new KarambaCommon.Toolkit();

  var p0 = new Point3(0, 0, 0);
  var p1 = new Point3(0, 0, 5);
  var L0 = new Line3(p0, p1);

  var nodes = new List<Point3>();

  var elems = k3d.Part.LineToBeam(new List<Line3>(){L0}, new List<string>(){ "B1" },
    new List<CroSec>(), logger, out nodes);

  var cond = new List<bool>(){ true, true, true, true, true, true};
  var support = k3d.Support.Support(0, cond);
  var supports = new List<Support>(){support};

  var pload = k3d.Load.PointLoad(1, new Vector3(0, 0, -10), new Vector3());
  var ploads = new List<Load>(){pload};

  double mass;
  Point3 cog;
  bool flag;
  string info;
  var model = k3d.Model.AssembleModel(elems, supports, ploads,
    out info, out mass, out cog, out info, out flag);

  // calculate Th.I response
  IReadOnlyList<double> max_disp;
  IReadOnlyList<Vector3> out_force;
  IReadOnlyList<double> out_energy;
  string warning;
  model = k3d.Algorithms.Analyze(model, new List<string>(){"LC0"}, out max_disp, out out_force, out out_energy, out warning);

  var ucf = UnitsConversionFactories.Conv();
  UnitConversion cm = ucf.cm();
  Print("max disp: " + cm.toUnit(max_disp[0]) + cm.unitB);

  Model_out = new Karamba.GHopper.Models.GH_Model(model);
}

As a means of reporting problems a “logger”-objects gets instantiated in line 10. This class limits the amount of text to a preset maximum so that in case of multiple errors the log-file does not grow without limits. Next comes the factory “k3d” which further on serves as the main hub of object creation. The classes “Point3” and “Line3” represent the Karamba3D equivalent of Grasshopper’s “Point3d” and “Line”. Their instantiations “p0”, “p1” and “L0” make up the model’s geometry. In line 19 the k3d-factory creates a list of objects of type “BuilderBeam” – with one entry in this case. This is not yet an element which forms part of a model – this would be “ModelBeam”. It rather represents a recipe for creating them. This concept applies to all elements in Karamba3D: Via the assemble-step objects of type “BuilderBeam” or “BuilderShell” produce “ModelBeams”-, “ModelTruss”-, “ModelSpring” and “ModelShell”-objects which form part of the C# structural model. What the user sees as “Element” in the Grasshopper GUI are the element-builders not the model-elements. Since C# structural models can be disassembled and reassembled the model-elements need to keep a reference to their builder-elements. This is achieved via the protected property “builder_element”.

The creation of supports and loads works similarly as for the element-builder. In Line 33 follows the model-assemble step, in line 41 the first order theory calculation of the model-response.

User defined objects that get piped through grasshopper definitions need to be wrapped: In line 47 a GH_Model wrapper object is created from the model-object that contains the final results. Similar wrapper classes exist for all Karamba3D entities that can populate a Grasshopper definition. Their names start with “GH_” which makes them easy to find.

[2]
18KB
ModelCreation_RH7.gh
20KB
ModelCreation_RH8.gh
Fig. 2.3.1: A model can be created from scratch using a C# script.