Sample Code: Animation in 3DF Part Viewer

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:
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: