Esempio n. 1
0
        /// <summary>
        /// Gets the camera of the BCF viewpoint. It returns the value within an optional, which is None, if the BCF
        /// viewpoint has no camera set.
        /// </summary>
        /// <returns>The optional containing the camera.</returns>
        public Option <Camera> GetCamera()
        {
            Camera camera = null;

            if (Viewpoint?.Perspective_camera != null)
            {
                var c = new PerspectiveCamera();
                Perspective_camera bcfPerspective = Viewpoint.Perspective_camera;

                c.FieldOfView = bcfPerspective.Field_of_view.ToDecimal();
                c.Position    = new Position(
                    bcfPerspective.Camera_view_point.ToVector3(),
                    bcfPerspective.Camera_direction.ToVector3(),
                    bcfPerspective.Camera_up_vector.ToVector3());

                camera = c;
            }

            if (Viewpoint?.Orthogonal_camera != null)
            {
                var c = new OrthogonalCamera();
                Orthogonal_camera bcfOrthogonal = Viewpoint.Orthogonal_camera;

                c.ViewToWorldScale = bcfOrthogonal.View_to_world_scale.ToDecimal();
                c.Position         = new Position(
                    bcfOrthogonal.Camera_view_point.ToVector3(),
                    bcfOrthogonal.Camera_direction.ToVector3(),
                    bcfOrthogonal.Camera_up_vector.ToVector3());

                camera = c;
            }

            return(camera.SomeNotNull());
        }
        private static (Orthogonal_camera orthogonalCamera, Perspective_camera perspectiveCamera) GetBcfCameraValues(
            this UIDocument uiDocument)
        {
            if (uiDocument.ActiveView is not View3D view3D)
            {
                return(null, null);
            }

            CameraType cameraType     = view3D.IsPerspective ? CameraType.Perspective : CameraType.Orthogonal;
            Position   cameraPosition = uiDocument.GetCameraPosition(view3D.IsPerspective);
            Vector3    center         = cameraPosition.Center.ToMeters();

            var cameraViewpoint = new iabi.BCF.APIObjects.V21.Point
            {
                X = Convert.ToSingle(center.X),
                Y = Convert.ToSingle(center.Y),
                Z = Convert.ToSingle(center.Z)
            };

            var cameraUpVector = new Direction
            {
                X = Convert.ToSingle(cameraPosition.Up.X),
                Y = Convert.ToSingle(cameraPosition.Up.Y),
                Z = Convert.ToSingle(cameraPosition.Up.Z)
            };

            var cameraDirection = new Direction
            {
                X = Convert.ToSingle(cameraPosition.Forward.X * -1),
                Y = Convert.ToSingle(cameraPosition.Forward.Y * -1),
                Z = Convert.ToSingle(cameraPosition.Forward.Z * -1)
            };

            Orthogonal_camera  ortho       = null;
            Perspective_camera perspective = null;

            switch (cameraType)
            {
            case CameraType.Perspective:
                perspective = new Perspective_camera
                {
                    Field_of_view     = 45, // revit default value
                    Camera_direction  = cameraDirection,
                    Camera_up_vector  = cameraUpVector,
                    Camera_view_point = cameraViewpoint
                };
                break;

            case CameraType.Orthogonal:
                ortho = new Orthogonal_camera
                {
                    View_to_world_scale = uiDocument.GetViewBoxHeight(),
                    Camera_direction    = cameraDirection,
                    Camera_up_vector    = cameraUpVector,
                    Camera_view_point   = cameraViewpoint
                };
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(cameraType), cameraType, "invalid camera type");
            }

            return(ortho, perspective);
        }