Example #1
0
        private void AddCameraActor(
            View3D InView3D,
            CameraInfo InViewCamera
            )
        {
            // Create a new Datasmith camera actor.
            // Hash the Datasmith camera actor name to shorten it.
            string HashedName = FDatasmithFacadeElement.GetStringHash(InView3D.UniqueId);
            FDatasmithFacadeActorCamera CameraActor = new FDatasmithFacadeActorCamera(HashedName);

            CameraActor.SetLabel(InView3D.Name);

            if (InView3D.Category != null)
            {
                // Set the Datasmith camera actor layer to be the 3D view category name.
                CameraActor.SetLayer(InView3D.Category.Name);
            }

            // Gets the current non-saved orientation of the 3D view.
            ViewOrientation3D ViewOrientation = InView3D.GetOrientation();

            // Set the world position (in right-handed Z-up coordinates) of the Datasmith camera actor.
            XYZ CameraPosition = ViewOrientation.EyePosition;

            CameraActor.SetCameraPosition((float)CameraPosition.X, (float)CameraPosition.Y, (float)CameraPosition.Z);

            // Set the world rotation of the Datasmith camera actor with
            // the camera world forward and up vectors (in right-handed Z-up coordinates).
            XYZ CameraForward = ViewOrientation.ForwardDirection;
            XYZ CameraUp      = ViewOrientation.UpDirection;

            CameraActor.SetCameraRotation((float)CameraForward.X, (float)CameraForward.Y, (float)CameraForward.Z, (float)CameraUp.X, (float)CameraUp.Y, (float)CameraUp.Z);

            // When the 3D view camera is not available, an orthographic view should be assumed.
            if (InViewCamera != null)
            {
                // Compute the aspect ratio (width/height) of the Revit 3D view camera, where
                // HorizontalExtent is the distance between left and right planes on the target plane,
                // VerticalExtent is the distance between top and bottom planes on the target plane.
                float AspectRatio = (float)(InViewCamera.HorizontalExtent / InViewCamera.VerticalExtent);

                // Set the aspect ratio of the Datasmith camera.
                CameraActor.SetAspectRatio(AspectRatio);

                if (InView3D.IsPerspective)
                {
                    // Set the sensor width of the Datasmith camera.
                    CameraActor.SetSensorWidth((float)(InViewCamera.HorizontalExtent * /* millimeters per foot */ 304.8));

                    // Get the distance from eye point along view direction to target plane.
                    // This value is appropriate for perspective views only.
                    float TargetDistance = (float)InViewCamera.TargetDistance;

                    // Set the Datasmith camera focus distance.
                    CameraActor.SetFocusDistance(TargetDistance);

                    // Set the Datasmith camera focal length.
                    CameraActor.SetFocalLength(TargetDistance * /* millimeters per foot */ 304.8F);
                }
            }

            // Add the camera actor to the Datasmith scene.
            DatasmithScene.AddActor(CameraActor);

            DirectLink?.MarkForExport(InView3D);
            DirectLink?.CacheElement(RevitDocument, InView3D, new FDocumentData.FBaseElementData(CameraActor, null, DocumentDataStack.Peek()));
        }