2.5: Data Export from Karamba3D
When it comes to exporting data from a Karamba3D model to another data-format one could simply go through the object tree of the model and iterate manually over the existing entities like e.g. nodes, elements, materials, . . . . The use of a builder pattern removes some of the bureaucratic overhead involved in this approach. The script in the example “ModelExport.gh” shows how to generate an XML-file based on a given Karamba3D-model. The output consists of a string which can be streamed to a file. When opened with a web-browser a nicely formatted tree results (see fig. 2.5.1).
Fig. 2.5.1: ModelExport.gh
By inheriting from "Karamba.Exporters.ExportBuilder" and overriding "builder"-methods a builder-class can be configured to export Karamba3D-models to any format - here XML.
The corresponding source-code looks like this:
1
...
2
using System.Xml;
3
using System.IO;
4
using Karamba.Models;
5
using Karamba.Nodes;
6
using Karamba.CrossSections;
7
using Karamba.Elements;
8
using Karamba.Loads;
9
using Karamba.Materials;
10
using Karamba.Supports;
11
using Karamba.Geometry;
12
...
13
private void RunScript(object Model_in, ref object XML)
14
{
15
var model = Model_in as Model;
16
if (model == null) {
17
throw new ArgumentException("The input is not of type model!");
18
}
19
20
var builder = new BuilderXML();
21
var director = new Karamba.Exporters.ExportDirector();
22
director.ConstructExport(model, builder);
23
24
XML = builder.getProduct();
25
}
26
27
// <Custom additional code>
28
public class BuilderXML : Karamba.Exporters.ExportBuilder {
29
// the XML document
30
private XmlDocument doc_ = new XmlDocument();
31
// the model inside the xml-document
32
private XmlElement model_;
33
34
public override void newProduct() {
35
model_ = (XmlElement) doc_.AppendChild(doc_.CreateElement("K3DModel"));
36
}
37
38
public override void buildMaterial(FemMaterial m, int ind) {
39
var xml_node = (XmlElement) model_.AppendChild(doc_.CreateElement("FemMaterial"));
40
xml_node.InnerText = "ind: " + ind + ":" + m.ToString();
41
}
42
43
public override void buildVertex(Node v) {
44
var xml_node = (XmlElement) model_.AppendChild(doc_.CreateElement("Node"));
45
xml_node.InnerText = v.ToString();
46
}
47
48
public override void buildCroSec(CroSec crosec) {
49
var xml_node = (XmlElement) model_.AppendChild(doc_.CreateElement("CroSec"));
50
xml_node.InnerText = crosec.ToString();
51
}
52
53
public override void buildElement(ModelElement e, Model model) {
54
var xml_node = (XmlElement) model_.AppendChild(doc_.CreateElement("ModelElement"));
55
xml_node.InnerText = e.ToString();
56
}
57
58
public override void buildElementLoad(ModelElement elem, Model model) {
59
foreach (var l in elem.Elem_loads) {
60
var xml_node = (XmlElement) model_.AppendChild(doc_.CreateElement("ElementLoad"));
61
xml_node.InnerText = l.ToString();
62
}
63
}
64
65
public override void buildLoadCase(int lc_ind, Model model) {
66
Vector3 g_vec = new Vector3(0, 0, 0);
67
foreach (GravityLoad g in model.gravities.Values) {
68
if (g.loadcase < 0 || g.loadcase == lc_ind) {
69
g_vec = g.force;
70
};
71
}
72
73
var xml_node = (XmlElement) model_.AppendChild(doc_.CreateElement("LoadCase"));
74
xml_node.InnerText = "load-case: " + lc_ind + " g =" + g_vec;
75
76
}
77
78
public override void buildSupport(Support s) {
79
var xml_node = (XmlElement) model_.AppendChild(doc_.CreateElement("Support"));
80
xml_node.InnerText = s.ToString();
81
}
82
83
public override void buildPointLoad(PointLoad p) {
84
var xml_node = (XmlElement) model_.AppendChild(doc_.CreateElement("PointLoad"));
85
xml_node.InnerText = p.ToString();
86
}
87
88
public override void buildMeshLoad(MeshLoad m) {
89
var xml_node = (XmlElement) model_.AppendChild(doc_.CreateElement("MeshLoad"));
90
xml_node.InnerText = m.ToString();
91
}
92
93
public string getProduct() {
94
StringWriter sw = new StringWriter();
95
XmlTextWriter tx = new XmlTextWriter(sw);
96
doc_.WriteTo(tx);
97
string str = sw.ToString();//
98
return str;
99
}
100
}
101
// </Custom additional code>
Copied!
The first part of the script comprises the “RunScript”-method. There the “Model_in”-object gets converted to a Karamba3D-Model. In line 20 follows the creation of a builder-object which does the conversion-work. The class “BuilderXML” gets defined further below in the script. A director-object is instantiated in the next line. Its task consists of iterating over the model constituents and presenting them to the builder. Calling “ConstructExport” in line 22 initiates the creation process. Line 24 assigns the builder’s product to the output variable “XML”.
The “BuilderXML”-class derives from Karamba.Exporters.ExportBuilder and overrides some of its methods. In order to keep things simple each build-method adds a XML-node which contains the string-representation of the corresponding argument. In case of “ModelElement” the corresponding build-method does not know which type of element gets passed. Thus one has to apply pattern matching to find get the right type (e.g. “ModelBeam”, “ModelTruss”, “ModelShell” or “ModelSpring”)
Last modified 8mo ago
Copy link