2.4.2: Activation and Deactivation of Elements

#region Usings
using System;
#r "C:\Program Files\Rhino 8\Plug-ins\Karamba\KarambaCommon.dll"
#r "C:\Program Files\Rhino 8\Plug-ins\Karamba\Karamba.gha"
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using Karamba.Models;
using Karamba.GHopper.Models;
using Karamba.Utilities;
using Karamba.Loads.Combination;
using Karamba.Elements.States.Selectors;
using Karamba.Geometry;
#endregion
public class Script_Instance : GH_ScriptInstance
{
private void RunScript(
object Model_in,
int maxiter,
ref object Model_out,
ref object isActive,
ref object maxDisp)
{
var k3d = new KarambaCommon.Toolkit();
IReadOnlyList<double> max_disp;
IReadOnlyList<Vector3> out_force;
IReadOnlyList<double> out_energy;
string warning;
var model = Model_in as Model;
if (model == null) {
throw new ArgumentException("The input in 'Model_in' is not of type karamba.Models.Model!");
}
// load case to consider for elimination of elements
string lcName = "LC0";
// get load-case combination
LoadCaseCombination lcc;
model.lcActivation.TryGetLoadCaseCombination(lcName, out lcc);
// select load case 0 of load-case combination "LC0"
var stateSelector = new StateElement1DSelectorIndex(model, lcc, 0);
// clone the model and its list of elements to avoid side effects
model = model.Clone();
// clone its elements to avoid side effects
model.cloneElements();
// do the iteration and remove elements with tensile axial forces
for (int iter = 0; iter < maxiter; iter++) {
// create a deform and response object for calculating and retrieving results
model = k3d.Algorithms.Analyze(model, new List<string>(){lcName}, out max_disp, out out_force, out out_energy, out warning);
// check the normal force of each element and deactivate those under tension
double N, V, M;
bool has_changed = false;
foreach (Karamba.Elements.ModelElement elem in model.elems) {
// retrieve resultant cross section forces
elem.resultantCroSecForces(model, stateSelector, 0.3, 3, out N, out V, out M);
// check whether normal force is tensile
if (N > 0) {
// set element inactive
elem.set_is_active(model, false);
has_changed = true;
}
}
// leave iteration loop if nothing changed
if (!has_changed) break;
// rebuild the C++ model
model.buildFEModel();
}
// update model to its final state
model = k3d.Algorithms.Analyze(model, new List<string>(){lcName}, out max_disp, out out_force, out out_energy, out warning);
// set up list of true/false values that corresponds to the elemment states
List<bool> elem_activity = new List<bool>();
foreach (var elem in model.elems) {
elem_activity.Add(elem.IsActive);
}
isActive = elem_activity;
maxDisp = max_disp;
Model_out = new GH_Model(model);
Print("Everything OK");
}
}
```Last updated