How to create a camera to show the entire model when open it with SketchUp?

I use the following code to create a camera and add to a scene, but I could not see the elements when open it with SketchUp and I need use ‘Zoom Extents’ to show the model.

if (SU_ERROR_NONE == SUCameraCreate (&camera) && camera.ptr)
{
SUPoint3D position = ConvertPoint (&eye);
SUPoint3D newTarget = ConvertPoint (&target);
SUVector3D up_vector = ConvertVector (&up);
SU_CALL(SUCameraSetOrientation (camera, &position, &newTarget, &up_vector));
SU_CALL(SUCameraSetPerspective (camera, viewFlags.camera));
if (viewFlags.camera)
SU_CALL(SUCameraSetPerspectiveFrustumFOV (camera, fieldOfView));
else
SU_CALL(SUCameraSetOrthographicFrustumHeight (camera, height));

    if (SU_ERROR_NONE == SUSceneCreate (&scene) && scene.ptr)
        {
        SUSceneSetName (scene, Utf8String (pName).c_str());
        SUSceneSetCamera (scene, camera);
        SUSceneSetUseCamera (scene, true);
        SUModelAddScene (m_pDocument, -1, scene, &out_index);
        }
    }

How do I create a camera to show the entire model when open it with SketchUp? I use the similar data to create a camera with old COM API could show the entire model without ‘Zoom Extents’.

double nearDistance = 1.0;
double farDistance  = 1000.0;
camera.Set(atlast::geometry::CPoint3d (eye.x, eye.y, eye.z),
           atlast::geometry::CPoint3d (target.x, target.y, target.z),
           atlast::geometry::CUnitVector3d (up.x, up.y, up.z),
           nearDistance, farDistance, viewFlags.camera, fieldOfView, height);

if (NULL == pName)
    m_pDocument->GetModel()->SetCamera(camera);
else
    m_pDocument->GetModel()->GetPages()->AddPage(pName)->SetCamera (camera);

This question was asked in the forum 6 years ago, but never answered …


I would think that it would be necessary to write your own “zoom” type function.

It would need to ensure that all the corners of the model’s bounding box was visible. The visibility check would need to calculate the camera’s viewing frustum and test of the bounding corners are within it.

See:

Or … (SketchUp 2020.0, API 8.0 and higher) perhaps check that all top level instances and primitives in the model are visible, if not transform the camera’s eye point farther from the target and repeat the visibility check.

@tt_su , @ene_su Have any ideas or gists available for this ?

BTW, what is the purpose of the SUCameraGetViewTransformation() function ?

  • Ie, the docs mention a “look at matrix” but not what this transformation matrix is.
    There is no equivalent method in the Ruby API.

@cagdcg , You may add feature requests in the official SketchUp API tracker:

Thank you, Dan. I will try with your suggestion.