# 3.5: Adding Functionality to a GH Component

In order to make the component do something one has to add code to the **“SolveInstance”** function in **“TensionElimComponents”**:

```csharp
protected override void SolveInstance(IGH_DataAccess DA)
{
    GH_Model in_gh_model = null;
    if (!DA.GetData<GH_Model>(0, ref in_gh_model)) return;
    var model = in_gh_model.Value;

    // maximum number of iterations
    int max_iter = 10;
    DA.GetData<int>(1, ref max_iter);

    // load case to consider for elimination of elements
    int lc_num = 0;

    // clone model to avoid side effects
    model = (Karamba.Models.Model)model.Clone();

    // clone its elements to avoid side effects
    model.cloneElements();

    // clone the feb-model to avoid side effects
    model.deepCloneFEModel();

    string singular_system_msg = "The stiffness matrix of the system is singular.";

    // do the iteration and remove elements with tensile axial forces
    for (int iter = 0; iter < max_iter; iter++)
    {
        // create an analysis and response object for calculating and retrieving results
        feb.Deform analysis = new feb.Deform(model.febmodel);
        feb.Response response = new feb.Response(analysis);

        try
        {
            // calculate the displacements
            response.updateNodalDisplacements();
            // calculate the member forces
            response.updateMemberForces();
        }
        catch
        {
            // send an error message in case something went wrong
            throw new Exception(singular_system_msg);
        }

        // check the normal force of each element and deactivate those under tension
        double N, V, M;
        bool has_changed = false;
        foreach (var elem in model.elems)
        {
            // retrieve resultant cross section forces
            elem.resultantCroSecForces(model,  new LCSuperPosition(lc_num, model), 
                out N, out V, out M);
            // check whether normal force is tensile
            if (!(N >= 0)) continue;
            // set element inactive
            elem.set_is_active(model, false);
            has_changed = true;
        }

        // leave iteration loop if nothing changed
        if (!has_changed) break;

        // if something changed inform the feb-model about it (otherwise it won't recalculate)
        model.febmodel.touch();

        // this guards the objects from being freed prematurely
        GC.KeepAlive(analysis);
        GC.KeepAlive(response);
    }

    // update model to its final state
    double max_disp = 0;
    try
    {
        // create an analysis and response object for calculating and retrieving results
        feb.Deform analysis = new feb.Deform(model.febmodel);
        feb.Response response = new feb.Response(analysis);

        // calculate the displacements
        response.updateNodalDisplacements();
        // calculate the member forces
        response.updateMemberForces();

        max_disp = response.maxDisplacement();

        // this guards the objects from being freed prematurely
        GC.KeepAlive(analysis);
        GC.KeepAlive(response);
    }
    catch
    {
        // send an error message in case something went wrong
        throw new Exception(singular_system_msg);
    }

    // set up list of true/false values that corresponds to the element states
    List<bool> elem_activity = new List<bool>();
    foreach (var elem in model.elems)
    {
        elem_activity.Add(elem.IsActive);
    }

    DA.SetData(0, new GH_Model(model));
    DA.SetDataList(1, elem_activity);
    DA.SetData(2, max_disp);
}
```

In line 3 a wrapper-object for a Karamba3D model gets initialized to null and set to the value of the input plug of index **“0”**. The **“if”** statement in line 4 checks whether there is any data to process. In line 5 the Karamba3D-model gets retrieved from the GH-wrapper. In lines 8 to 9 the value of the **“maxiter”** plug-in gets read.

All the rest down to line 101 constitutes more or less a replica of the code of section [2.4.2](/2.-scripting/2.4-how-to-modify-structural-models/2.4.2-activation-and-deactivation-of-elements.md). Lines 102 to 104 transfer the results of the algorithm to the output-plugs.

The example **“ActivationDeactivationOfElements\_CustomComponent”** (see fig. 3.5.1) shows that the results using the GH custom component are the same as in section [2.4.2](/2.-scripting/2.4-how-to-modify-structural-models/2.4.2-activation-and-deactivation-of-elements.md).

A comparison with the listing in section [2](/2.-scripting/2.1-hello-karamba3d.md) shows that only the first and last parts differ significantly:

![Fig. 3.5.1: Elimination of elements under tension. this time using a custom GH-component.](/files/-MXH_8myEJNQ7wI6gcCZ)

Visual Studio Project File:

{% file src="/files/n6OZsVY21rdyV7ohe7sH" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://scripting-2.karamba3d.com/3.-how-to-create-your-own-karamba3d-grasshopper-component/3.5-adding-functionality-to-a-gh-component.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
