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.1: Cross section Optimization

Previous2.8.4: How to Modify Structural ModelsNext2.8.4.2: Activation and Deactivation of Elements

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.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!")

for crosec in CroSecs_in:
    if not isinstance(crosec, Karamba.CrossSections.CroSec_Beam):
        raise Exception("The input in 'CroSecs_in' contains objects which are not of type Karamba.CrossSections.CroSec_Beam!")

# avoid side effects
model = model.Clone();
model.cloneElements();

k3d = KarambaCommon.Toolkit()
for i in range(niter):
    loadCases = List[String]([lcName])
    model, max_disp, outForce, outEnergy, Warning = k3d.Algorithms.Analyze(model, loadCases);

    for elem_ind in range(model.elems.Count):
        beam = model.elems[elem_ind]
        if not isinstance(beam, Karamba.Elements.ModelBeam):
            continue

        # avoid side effects
        beam = beam.Clone();
        model.elems[elem_ind] = beam;

        elemId = List[String](str(elem_ind))
        N, V, M = Karamba.Results.BeamResultantForces.solve(model, elemId, lcName, 100, 1);

        for crosec in CroSecs_in:
            beam.crosec = crosec
            maxSigma = abs(N[0][0]) / crosec.A + M[0][0] / crosec.Wely_z_pos;
            ft = crosec.material.ft()
            if (maxSigma < ft):
                break
          
    model.initMaterialCroSecLists();
    model.buildFEModel();

model, max_disp, outForce, outEnergy, Warning = k3d.Algorithms.Analyze(model, loadCases);
Disp_out = max_disp[0];
Model_out = Karamba.GHopper.Models.GH_Model(model);
section 2.4.1
36KB
CrossSectionOptimization_Py3.gh