Sample Code: Animation in 3DF csharp_simple Project

Language: C#

This is a C# variant of the C++ animation sample presented here.

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

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:

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: