How-To Compute a default cameras for a CAD Model

To programmatically create a “Fit visible” camera, you will compute its value using the model bounding box. In this sample code below you can see a simple way to compute a fit world view

“Default” camera

  1. A3DMiscComputeBoundingBox will compute a Bounding Box for the model.
  2. Then we compute the center of the bounding box. It will be the camera center.
  3. Finally we make a position for the camera. Here I computed a vector from the center position, but you may adjust the values to find the best fitting for your usage.
  4. We use the camera in the HOOPS Publish sample UserDefinedViews.
A3DBoundingBoxData pboxdata;	
A3D_INITIALIZE_DATA(A3DBoundingBoxData, pboxdata);
A3DMiscComputeBoundingBox(pModelFile, 0, &pboxdata);
double centerx, centery, centerz;
centerx = pboxdata.m_sMax.m_dX - pboxdata.m_sMin.m_dX;
centery = pboxdata.m_sMax.m_dY - pboxdata.m_sMin.m_dY;
centerz = pboxdata.m_sMax.m_dZ - pboxdata.m_sMin.m_dZ;

double positionx, positiony, positionz;
positionx = centerx + 0.5 * centerx;
positiony = centery + 0.5 * centery;
positionz = centerz + 0.5 * centerz;

// Default view: no button for this default view
CHECK_RET(CreateView(pDoc, p3DArtwork,
	positionx, positiony,positionz,	// camera position
	centerx, centery, centerz,		// target position
	0,0,0,	// up vector unused - roll is used instead
	0,	// roll
	kA3DPDFPerspectiveMode, 0, 30,
	true, viewnameDef, &pViewDef));