# Sample Code: Animation in 3DF csharp\_simple Project

**URL:** https://forum.techsoft3d.com/t/sample-code-animation-in-3df-csharp-simple-project/1417
**Category:** HOOPS Visualize Desktop
**Tags:** code, animation, knowledge-base, sample, learn
**Created:** [August 16, 2021, 4:00am UTC](https://forum.techsoft3d.com/t/sample-code-animation-in-3df-csharp-simple-project/1417 "2021-08-16T04:00:00Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![gabriel.peragine](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.techsoft3d.com/gabriel.peragine/32/1535_2.png) [@gabriel.peragine](https://forum.techsoft3d.com/u/gabriel.peragine)
#### Post date: [August 16, 2021, 4:00am UTC](https://forum.techsoft3d.com/t/sample-code-animation-in-3df-csharp-simple-project/1417/1 "2021-08-16T04:00:00Z")

</div>

Language: C#

This is a C# variant of the C++ animation sample presented [here](https://forum.techsoft3d.com/t/sample-code-animation-in-3df-part-viewer/1415).

The 3DF Visualize service pack has a basic sandbox Visual Studio project based in C#. The project is called “csharp\_simple\_csXXX”. The project is part of the solution called “samples\_v14X.sln” found immediately under the root directory of the 3DF service package. Once the aforementioned solution is open, expand the “demo” folder:

![csharp_simple](https://us1.discourse-cdn.com/flex020/uploads/techsoft3d/original/1X/a5e4c5d5dba2acb61ebf16c459fd17e9c9d24449.png)

In contrast to other sandboxes, csharp\_simple does not have empty user code function slots. To get around that, we can just “re-use” and existing function called `actionAnnotateMenu_Click()`.

Locate the file “SimpleHNForm.cs” and _replace_ the existing function:  
`public void actionAnnotateMenu_Click(object sender, EventArgs e)`  
`{`  
`m_pHNPanel.SetCurrentOperator(new HOpMarkupAnnotate(m_pHNPanel.m_pHView));`  
`}`

with the following code:

```auto
void insertCube(String x, HBaseView view, HPoint pos, String color = "green")
{
    int points = 8, faces = 30;
    float[] pts = {
                    -0.1f + pos.x, -0.1f + pos.y, -0.1f + pos.z,
                    0.1f + pos.x, -0.1f + pos.y, -0.1f + pos.z,
                    0.1f + pos.x, 0.1f + pos.y, -0.1f + pos.z,
                    -0.1f + pos.x, 0.1f + pos.y, -0.1f + pos.z,
                    -0.1f + pos.x, -0.1f + pos.y, 0.1f + pos.z,
                    0.1f + pos.x, -0.1f + pos.y, 0.1f + pos.z,
                    0.1f + pos.x, 0.1f + pos.y, 0.1f + pos.z,
                    -0.1f + pos.x, 0.1f + pos.y, 0.1f + pos.z
                    };

    int[] faceList = { 4, 0, 1, 2, 3,
                        4, 1, 5, 6, 2,
                        4, 5, 4, 7, 6,
                        4, 4, 0, 3, 7,
                        4, 3, 2, 6, 7,
                        4, 0, 4, 5, 1 };

    HCS.Open_Segment(x);
    {
        HCS.Set_Visibility("edges");
        HCS.Set_Color(color);

        HCS.Insert_Shell(points, pts, faces, faceList);
    }
    HCS.Close_Segment();
}

float gen_random(Random ran)
{
    float r = ran.Next(0, 100) / 100.0f;

    return r;
}

public void actionAnnotateMenu_Click(object sender, EventArgs e)
{
    //m_pHNPanel.SetCurrentOperator(new HOpMarkupAnnotate(m_pHNPanel.m_pHView));

    Random ran = new Random();

    HCS.Open_Segment_By_Key(m_pHNPanel.m_pHView.GetSceneKey());
    {
        float[] pos = { 5.971055f, 5.972775f, 5.967750f };
        float[] tar = { 0.977285f, 0.979005f, 0.973980f };
        float[] up = { -0.408248f, -0.408248f, 0.816497f };
        HCS.Set_Camera(pos, tar, up, 3.459487f, 3.459487f, "orthographic");
    }
    HCS.Close_Segment();

    HBhvBehaviorManager bm = m_pHNPanel.m_pHView.GetModel().GetBhvBehaviorManager();

    String cubeSegName = "";
    String animationName = "";
    String spathName = "";
    String depthSegName = "";
    String runningSegName = "";
    int depth = 1; // control how many subsegments are created

    for (int i = 0; i < 200; i++)
    {
        for (int j = 0; j < depth; j++)
        {
            depthSegName = String.Format("{0}_parent_{1}", i, j);
            if (j < 1)
            {
                runningSegName = String.Format("{0}", depthSegName);
            }
            else
            {
                runningSegName = String.Format("{0}/{1}", runningSegName, depthSegName);
            }

            HCS.Open_Segment_By_Key(m_pHNPanel.m_pHView.GetModelKey());
            {
                HCS.Open_Segment(runningSegName);
                {
                }
                HCS.Close_Segment();
            }
            HCS.Close_Segment();
        }

        cubeSegName = String.Format("cube_{0}", i);
        animationName = String.Format("move_{0}", i);
        spathName = String.Format("SPATH:MODEL/{0}/cube_{1}", runningSegName, i);

        HCS.Open_Segment_By_Key(m_pHNPanel.m_pHView.GetModelKey());
        {
            HCS.Open_Segment(runningSegName);
            {
                insertCube(cubeSegName, m_pHNPanel.m_pHView, new HPoint(gen_random(ran), gen_random(ran), gen_random(ran)));
            }
            HCS.Close_Segment();
        }
        HCS.Close_Segment();

        HPoint p0 = new HPoint(0, 0, 0);
        bm.AddAnimation(animationName, spathName, p0);

        HPoint rand_start_end = new HPoint(gen_random(ran), gen_random(ran), gen_random(ran));
        HPoint posB = new HPoint(gen_random(ran), gen_random(ran), gen_random(ran));
        HPoint posC = new HPoint(gen_random(ran), gen_random(ran), gen_random(ran));
        HPoint posD = new HPoint(gen_random(ran), gen_random(ran), gen_random(ran));

        bm.AddPositionKeyframe(animationName, 0, rand_start_end, true);
        bm.AddPositionKeyframe(animationName, 10, posB, true);
        bm.AddPositionKeyframe(animationName, 20, posC, true);
        bm.AddPositionKeyframe(animationName, 30, posD, true);
        bm.AddPositionKeyframe(animationName, 40, rand_start_end, true);
    }

    bm.SetContinuousPlay(true);
    bm.Play();

    m_pHNPanel.m_pHView.Update();
}

```

Below is a short video of the animation:

[![](https://us1.discourse-cdn.com/flex020/uploads/techsoft3d/original/1X/a15ecdb5dc3f48573428f8cb3af1e197ed465283.jpeg "Animation in 3DF csharp\_simple") ](https://www.youtube.com/watch?v=hsdQidZ4-NI)
