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. 2.8: The Python 3 Component
  3. 2.8.4: How to Modify Structural Models

2.8.4.2: Activation and Deactivation of Elements

Previous2.8.4.1: Cross section OptimizationNext3.1: Setting up a Visual Studio Project for GH Plug-ins

Last updated 6 months ago

The equivalent Python 3 code of the example in looks like this:

import Karamba.Models
import Karamba.CrossSections
import Karamba.Elements
import Karamba.Results
import KarambaCommon
from Karamba.Elements.States.Selectors import StateElement1DSelectorIndex
from Karamba.Geometry import *
from System.Collections.Generic import List
from System import String

model = Model_in
if not isinstance(model, Karamba.Models.Model):
      raise Exception("The input in 'Model_in' is not of type Karamba.Models.Model!")

# load case to consider for elimination of elements
lcName = "LC0";
lcNames = List[String]([lcName])
# get load-case combination
success, lcc = model.lcActivation.TryGetLoadCaseCombination(lcName)
# select load case 0 of load-case combination "LC0"
stateSelector = 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();

k3d = KarambaCommon.Toolkit()

# do the iteration and remove elements with tensile axial forces
for iter in range(maxiter):
    # create a deform and response object for calculating and retrieving results
    model, max_disp, outForce, outEnergy, Warning = k3d.Algorithms.Analyze(model, lcNames)

    # check the normal force of each element and deactivate those under tension
    has_changed = False;
    for elem in model.elems:
        # retrieve resultant cross section forces
        N, V, M = elem.resultantCroSecForces(model, stateSelector, 0.3, 3)

        # 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 not has_changed:
        break

    # rebuild the C++ model
    model.buildFEModel()

# update model to its final state
model, max_disp, outForce, outEnergy, Warning = k3d.Algorithms.Analyze(model, lcNames)

# set up list of true/false values that corresponds to the elemment states
elem_activity = []
for elem in model.elems:
    elem_activity.append(elem.IsActive);


isActive = elem_activity;
maxDisp = max_disp;
Model_out = Karamba.GHopper.Models.GH_Model(model)

print("Everything OK");
section 2.4.2
24KB
ActivationDeactivationOfElements_Py3.gh