2.3: How to Create Structural Models
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 [2] 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).
Fig. 2.3.1: A model can be created from scratch using a C# script.
This is the source-code inside the C#-component (see example “ModelCreation.gh”):
1
...
2
using Karamba.Utilities;
3
using Karamba.Geometry;
4
using Karamba.CrossSections;
5
using Karamba.Supports;
6
using Karamba.Loads;
7
...
8
private void RunScript(ref object Model_out)
9
{
10
var logger = new MessageLogger();
11
var k3d = new KarambaCommon.Toolkit();
12
13
var p0 = new Point3(0, 0, 0);
14
var p1 = new Point3(0, 0, 5);
15
var L0 = new Line3(p0, p1);
16
17
var nodes = new List<Point3>();
18
19
var elems = k3d.Part.LineToBeam(new List<Line3>(){L0}, new List<string>(){ "B1" },
20
new List<CroSec>(), logger, out nodes);
21
22
var cond = new List<bool>(){ true, true, true, true, true, true};
23
var support = k3d.Support.Support(0, cond);
24
var supports = new List<Support>(){support};
25
26
var pload = k3d.Load.PointLoad(1, new Vector3(0, 0, -10), new Vector3());
27
var ploads = new List<Load>(){pload};
28
29
double mass;
30
Point3 cog;
31
bool flag;
32
string info;
33
var model = k3d.Model.AssembleModel(elems, supports, ploads,
34
out info, out mass, out cog, out info, out flag);
35
36
// calculate Th.I response
37
List<double> max_disp;
38
List<double> out_g;
39
List<double> out_comp;
40
string message;
41
model = k3d.Algorithms.AnalyzeThI(model, out max_disp, out out_g, out out_comp, out message);
42
43
var ucf = UnitsConversionFactories.Conv();
44
UnitConversion cm = ucf.cm();
45
Print("max disp: " + cm.toUnit(max_disp[0]) + cm.unitB);
46
47
Model_out = new Karamba.GHopper.Models.GH_Model(model);
48
}
Copied!
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.
Last modified 8mo ago
Copy link