# Sample Code: Animation in 3DF Part Viewer

**URL:** <https://forum.techsoft3d.com/t/sample-code-animation-in-3df-part-viewer/1415>\
**Category:** HOOPS Visualize Desktop\
**Tags:** code, sample, learn, knowledge-base, animation\
**Created:** [August 12, 2022, 4:00am UTC](https://forum.techsoft3d.com/t/sample-code-animation-in-3df-part-viewer/1415 "2022-08-12T04: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 12, 2022, 4:00am UTC](https://forum.techsoft3d.com/t/sample-code-animation-in-3df-part-viewer/1415/1 "2022-08-12T04:00:00Z")

</div>

**Language: C++**

The code provided below can be executed in the HOOPS 3D PartViewer Visual Studio project:

1. Open “samples\_v14x.sln” found in the root directory of the 3DF service package.
2. In the VS project “hoopspartviewer\_v14x”, navigate to the C++ file called “CSolidHoopsView.cpp”. The user slots are at the bottom of the file. Add the following code:

```auto
void insertCube(char *x, HBaseView *view, HPoint pos, char *color = "green")
{
	int points = 8, faces = 30;
	HPoint pts[8];
	int faceList[30] = { 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 };

	HC_Open_Segment_By_Key(view->GetModelKey()); {
		HC_Open_Segment(x); {
			HC_Set_Visibility("edges");
			char mycolor[128];
			sprintf_s(mycolor, "faces=%s", color);
			HC_Set_Color(mycolor);

			pts[0].Set(-0.1 + pos.x, -0.1 + pos.y, -0.1 + pos.z);
			pts[1].Set(0.1 + pos.x, -0.1 + pos.y, -0.1 + pos.z);
			pts[2].Set(0.1 + pos.x, 0.1 + pos.y, -0.1 + pos.z);
			pts[3].Set(-0.1 + pos.x, 0.1 + pos.y, -0.1 + pos.z);
			pts[4].Set(-0.1 + pos.x, -0.1 + pos.y, 0.1 + pos.z);
			pts[5].Set(0.1 + pos.x, -0.1 + pos.y, 0.1 + pos.z);
			pts[6].Set(0.1 + pos.x, 0.1 + pos.y, 0.1 + pos.z);
			pts[7].Set(-0.1 + pos.x, 0.1 + pos.y, 0.1 + pos.z);

			HC_Insert_Shell(points, pts, faces, faceList);
		} HC_Close_Segment();
	} HC_Close_Segment();
}

void CSolidHoopsView::OnExtraSlot1()
{
	HC_Open_Segment_By_Key(m_pHView->GetSceneKey()); {
		HPoint pos(5.586103f, -3.646103f, 5.586103);
		HPoint tar(0.960000f, 0.980000f, 0.960000f);
		HPoint up(-0.408248f, 0.408248f, 0.816497f);

		HC_Set_Camera(&pos, &tar, &up, 3.205059, 3.205059, "orthographic");
	} HC_Close_Segment();

	HBhvBehaviorManager *bm = m_pHView->GetModel()->GetBhvBehaviorManager();

	char segName[64];
	char animationName[64];
	char spathName[64];

	//Currently set at animating 200 cubes 
	for (int i = 0; i < 200; i++) {
		sprintf(segName, "cube_%i", i);
		sprintf(animationName, "move_%i", i);
		sprintf(spathName, "SPATH:MODEL/cube_%i", i);

		double rand_0 = (rand() % 100) / 100.0;
		double rand_1 = (rand() % 100) / 100.0;
		double rand_2 = (rand() % 100) / 100.0;

		insertCube(segName, m_pHView, HPoint(rand_0, rand_1, rand_2));

		HPoint p0(0, 0, 0);
		bm->AddAnimation(animationName, spathName, &p0);

		HPoint rand_start_end((rand() % 100) / 100.0, (rand() % 100) / 100.0, (rand() % 100) / 100.0),
			posB((rand() % 100) / 100.0, (rand() % 100) / 100.0, (rand() % 100) / 100.0),
			posC((rand() % 100) / 100.0, (rand() % 100) / 100.0, (rand() % 100) / 100.0),
			posD((rand() % 100) / 100.0, (rand() % 100) / 100.0, (rand() % 100) / 100.0);

		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->Play();

	m_pHView->Update();
}

```

This code was originally part of a general stress test. The animation creates 200 cubes with each cube having its own segment.

Attached is a video (with audio narration) of the animation in the Part Viewer:

[![](https://us1.discourse-cdn.com/flex020/uploads/techsoft3d/original/1X/9295121043aac4401d9fcb7bb366f188ec3fcadb.jpeg "Animation in 3DF Part Viewer") ](https://www.youtube.com/watch?v=wrtDGMQcZu0)
