Example #1
0
        /// <summary>
        /// Orthogonal View by Perspective Camera.
        /// </summary>
        /// <param name="bcfView"></param>
        /// <param name="camera"></param>
        /// <returns></returns>
        private bool SetOrthogonalView(View3D bcfView, PerspectiveCamera camera)
        {
            var result = false;

            try
            {
                var direction   = RevitUtils.GetRevitXYZ(camera.CameraDirection);
                var upVector    = RevitUtils.GetRevitXYZ(camera.CameraUpVector);
                var viewPoint   = RevitUtils.GetRevitXYZ(camera.CameraViewPoint);
                var orientation = RevitUtils.ConvertBasePoint(ActiveDoc, viewPoint, direction, upVector, true);

                using (var trans = new Transaction(ActiveDoc))
                {
                    trans.Start("Set Orientation");
                    try
                    {
                        bcfView.SetOrientation(orientation);
                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.RollBack();
                    }
                }

                SetViewPointBoundingBox(bcfView);
                result = true;
            }
            catch (Exception)
            {
                // ignored
            }

            return(result);
        }
Example #2
0
        private PerspectiveCamera GetPerspectiveCamera(View3D view, string viewpoint_Guid)
        {
            var camera = new PerspectiveCamera();

            try
            {
                var    viewCenter = view.Origin;
                double zoomValue  = 45;

                var orientation = RevitUtils.ConvertBasePoint(ActiveDoc, viewCenter, view.ViewDirection, view.UpDirection, false);

                var c  = orientation.EyePosition;
                var vi = orientation.ForwardDirection;
                var up = orientation.UpDirection;

                camera.Guid          = Guid.NewGuid().ToString();
                camera.ViewPointGuid = viewpoint_Guid;

                var viewPoint = new Schemas.Point
                {
                    Guid = Guid.NewGuid().ToString(),
                    X    = c.X.ToMeters(),
                    Y    = c.Y.ToMeters(),
                    Z    = c.Z.ToMeters()
                };
                camera.CameraViewPoint = viewPoint;

                var upVector = new Direction
                {
                    Guid = Guid.NewGuid().ToString(),
                    X    = up.X.ToMeters(),
                    Y    = up.Y.ToMeters(),
                    Z    = up.Z.ToMeters()
                };
                camera.CameraUpVector = upVector;

                var direction = new Direction
                {
                    Guid = Guid.NewGuid().ToString(),
                    X    = -(vi.X.ToMeters()),
                    Y    = -(vi.Y.ToMeters()),
                    Z    = -(vi.Z.ToMeters())
                };
                camera.CameraDirection = direction;

                camera.FieldOfView = zoomValue;
            }
            catch (Exception)
            {
                // ignored
            }

            return(camera);
        }
Example #3
0
        private bool SetPerspectiveView(View3D bcfView, PerspectiveCamera camera)
        {
            var result = false;

            try
            {
                var zoom        = camera.FieldOfView;
                var direction   = RevitUtils.GetRevitXYZ(camera.CameraDirection);
                var upVector    = RevitUtils.GetRevitXYZ(camera.CameraUpVector);
                var viewPoint   = RevitUtils.GetRevitXYZ(camera.CameraViewPoint);
                var orientation = RevitUtils.ConvertBasePoint(ActiveDoc, viewPoint, direction, upVector, true);


                using (var trans = new Transaction(ActiveDoc))
                {
                    trans.Start("Set Orientation");
                    try
                    {
                        if (bcfView.CanResetCameraTarget())
                        {
                            bcfView.ResetCameraTarget();
                        }
                        bcfView.SetOrientation(orientation);
                        if (bcfView.get_Parameter(BuiltInParameter.VIEWER_BOUND_ACTIVE_FAR).HasValue)
                        {
                            var m_farClip = bcfView.get_Parameter(BuiltInParameter.VIEWER_BOUND_ACTIVE_FAR);
                            m_farClip.Set(0);
                        }

                        bcfView.CropBoxActive  = true;
                        bcfView.CropBoxVisible = true;

                        trans.Commit();
                    }
                    catch (Exception)
                    {
                        trans.RollBack();
                    }
                }
                result = true;
            }
            catch (Exception)
            {
                // ignored
            }

            return(result);
        }
Example #4
0
        private bool SetOrthogonalView(View3D bcfView, OrthogonalCamera camera)
        {
            var result = false;

            try
            {
                var zoom        = camera.ViewToWorldScale.ToFeet();
                var direction   = RevitUtils.GetRevitXYZ(camera.CameraDirection);
                var upVector    = RevitUtils.GetRevitXYZ(camera.CameraUpVector);
                var viewPoint   = RevitUtils.GetRevitXYZ(camera.CameraViewPoint);
                var orientation = RevitUtils.ConvertBasePoint(ActiveDoc, viewPoint, direction, upVector, true);

                using (var trans = new Transaction(ActiveDoc))
                {
                    trans.Start("Set Orientation");
                    try
                    {
                        bcfView.SetOrientation(orientation);
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.RollBack();
                        var message = ex.Message;
                    }
                }

                var m_xyzTl = bcfView.Origin.Add(bcfView.UpDirection.Multiply(zoom)).Subtract(bcfView.RightDirection.Multiply(zoom));
                var m_xyzBr = bcfView.Origin.Subtract(bcfView.UpDirection.Multiply(zoom)).Add(bcfView.RightDirection.Multiply(zoom));
                BCFUIView.ZoomAndCenterRectangle(m_xyzTl, m_xyzBr);

                result = true;
            }
            catch (Exception ex)
            {
                var message = ex.Message;
            }
            return(result);
        }
Example #5
0
        //<summary>
        //Generate a VisualizationInfo of the current view
        //</summary>
        //<returns></returns>
        private VisualizationInfo GenerateViewpoint()
        {
            try
            {
                var uidoc = uiapp.ActiveUIDocument;
                var doc   = uidoc.Document;

                var v = new VisualizationInfo();

                //Corners of the active UI view
                var topLeft     = uidoc.GetOpenUIViews()[0].GetZoomCorners()[0];
                var bottomRight = uidoc.GetOpenUIViews()[0].GetZoomCorners()[1];

                //It's a 2D view
                //not supported by BCF, but I store it under a custom
                //fields using 2D coordinates and sheet id
                if (uidoc.ActiveView.ViewType != ViewType.ThreeD)
                {
                    v.SheetCamera = new SheetCamera
                    {
                        SheetID = uidoc.ActiveView.Id.IntegerValue,
                        TopLeft = new Point {
                            X = topLeft.X, Y = topLeft.Y, Z = topLeft.Z
                        },
                        BottomRight = new Point {
                            X = bottomRight.X, Y = bottomRight.Y, Z = bottomRight.Z
                        }
                    };
                }
                //It's a 3d view
                else
                {
                    var    viewCenter = new XYZ();
                    var    view3D     = (View3D)uidoc.ActiveView;
                    double zoomValue  = 1;
                    // it is a orthogonal view
                    if (!view3D.IsPerspective)
                    {
                        double x = (topLeft.X + bottomRight.X) / 2;
                        double y = (topLeft.Y + bottomRight.Y) / 2;
                        double z = (topLeft.Z + bottomRight.Z) / 2;
                        //center of the UI view
                        viewCenter = new XYZ(x, y, z);

                        //vector going from BR to TL
                        XYZ diagVector = topLeft.Subtract(bottomRight);
                        //lenght of the vector
                        double dist = topLeft.DistanceTo(bottomRight) / 2;

                        //ViewToWorldScale value
                        zoomValue = dist * Math.Sin(diagVector.AngleTo(view3D.RightDirection)).ToMeters();

                        // **** CUSTOM VALUE FOR TEKLA **** //
                        // calculated sperimentally, not sure why but it works
                        //if (UserSettings.Get("optTekla") == "1")
                        //  zoomValue = zoomValue * 2.5;
                        // **** CUSTOM VALUE FOR TEKLA **** //

                        ViewOrientation3D t = RevitUtils.ConvertBasePoint(doc, viewCenter, uidoc.ActiveView.ViewDirection,
                                                                          uidoc.ActiveView.UpDirection, false);

                        XYZ c  = t.EyePosition;
                        XYZ vi = t.ForwardDirection;
                        XYZ up = t.UpDirection;


                        v.OrthogonalCamera = new OrthogonalCamera
                        {
                            CameraViewPoint =
                            {
                                X = c.X.ToMeters(),
                                Y = c.Y.ToMeters(),
                                Z = c.Z.ToMeters()
                            },
                            CameraUpVector =
                            {
                                X = up.X.ToMeters(),
                                Y = up.Y.ToMeters(),
                                Z = up.Z.ToMeters()
                            },
                            CameraDirection =
                            {
                                X = vi.X.ToMeters() * -1,
                                Y = vi.Y.ToMeters() * -1,
                                Z = vi.Z.ToMeters() * -1
                            },
                            ViewToWorldScale = zoomValue
                        };
                    }
                    // it is a perspective view
                    else
                    {
                        viewCenter = uidoc.ActiveView.Origin;
                        //revit default value
                        zoomValue = 45;

                        ViewOrientation3D t = RevitUtils.ConvertBasePoint(doc, viewCenter, uidoc.ActiveView.ViewDirection,
                                                                          uidoc.ActiveView.UpDirection, false);

                        XYZ c  = t.EyePosition;
                        XYZ vi = t.ForwardDirection;
                        XYZ up = t.UpDirection;

                        v.PerspectiveCamera = new PerspectiveCamera
                        {
                            CameraViewPoint =
                            {
                                X = c.X.ToMeters(),
                                Y = c.Y.ToMeters(),
                                Z = c.Z.ToMeters()
                            },
                            CameraUpVector =
                            {
                                X = up.X.ToMeters(),
                                Y = up.Y.ToMeters(),
                                Z = up.Z.ToMeters()
                            },
                            CameraDirection =
                            {
                                X = vi.X.ToMeters() * -1,
                                Y = vi.Y.ToMeters() * -1,
                                Z = vi.Z.ToMeters() * -1
                            },
                            FieldOfView = zoomValue
                        };
                    }
                }
                //COMPONENTS PART
                string versionName = doc.Application.VersionName;
                v.Components = new List <Component>();

                var visibleElems = new FilteredElementCollector(doc, doc.ActiveView.Id)
                                   .WhereElementIsNotElementType()
                                   .WhereElementIsViewIndependent()
                                   .ToElementIds();
                var hiddenElems = new FilteredElementCollector(doc)
                                  .WhereElementIsNotElementType()
                                  .WhereElementIsViewIndependent()
                                  .Where(x => x.IsHidden(doc.ActiveView) ||
                                         !doc.ActiveView.IsElementVisibleInTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate, x.Id)).ToList();//would need to check how much this is affecting performance

                var selectedElems = uidoc.Selection.GetElementIds();


                //include only hidden elements and selected in the BCF
                if (visibleElems.Count() > hiddenElems.Count())
                {
                    foreach (var elem in hiddenElems)
                    {
                        v.Components.Add(new Component
                        {
                            OriginatingSystem = versionName,
                            IfcGuid           = IfcGuid.ToIfcGuid(ExportUtils.GetExportId(doc, elem.Id)),
                            Visible           = false,
                            Selected          = false,
                            AuthoringToolId   = elem.Id.IntegerValue.ToString()
                        });
                    }
                    foreach (var elem in selectedElems)
                    {
                        v.Components.Add(new Component
                        {
                            OriginatingSystem = versionName,
                            IfcGuid           = IfcGuid.ToIfcGuid(ExportUtils.GetExportId(doc, elem)),
                            Visible           = true,
                            Selected          = true,
                            AuthoringToolId   = elem.IntegerValue.ToString()
                        });
                    }
                }
                //include only visigle elements
                //all the others are hidden
                else
                {
                    foreach (var elem in visibleElems)
                    {
                        v.Components.Add(new Component
                        {
                            OriginatingSystem = versionName,
                            IfcGuid           = IfcGuid.ToIfcGuid(ExportUtils.GetExportId(doc, elem)),
                            Visible           = true,
                            Selected          = selectedElems.Contains(elem),
                            AuthoringToolId   = elem.IntegerValue.ToString()
                        });
                    }
                }
                return(v);
            }
            catch (System.Exception ex1)
            {
                TaskDialog.Show("Error generating viewpoint", "exception: " + ex1);
            }
            return(null);
        }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="view"></param>
        /// <param name="viewpoint_Guid"></param>
        /// <returns></returns>
        private OrthogonalCamera GetOrthogonalCamera(View3D view, string viewpoint_Guid)
        {
            var camera = new OrthogonalCamera();

            try
            {
                var uiView = FindDefaultUIView(view); if (null == uiView)
                {
                    return(camera);
                }
                var topLeft     = uiView.GetZoomCorners()[0];
                var bottomRight = uiView.GetZoomCorners()[1];

                var x = (topLeft.X + bottomRight.X) / 2;
                var y = (topLeft.Y + bottomRight.Y) / 2;
                var z = (topLeft.Z + bottomRight.Z) / 2;

                var viewCenter = new XYZ(x, y, z);

                var diagVector = topLeft.Subtract(bottomRight);
                var dist       = topLeft.DistanceTo(bottomRight) / 2;

                var zoomValue = dist * Math.Sin(diagVector.AngleTo(view.RightDirection)).ToMeters();

                var orientation = RevitUtils.ConvertBasePoint(ActiveDoc, viewCenter, view.ViewDirection, view.UpDirection, false);

                var c  = orientation.EyePosition;
                var vi = orientation.ForwardDirection;
                var up = orientation.UpDirection;

                camera.Guid          = Guid.NewGuid().ToString();
                camera.ViewPointGuid = viewpoint_Guid;
                var viewPoint = new Schemas.Point
                {
                    Guid = Guid.NewGuid().ToString(),
                    X    = c.X.ToMeters(),
                    Y    = c.Y.ToMeters(),
                    Z    = c.Z.ToMeters()
                };
                camera.CameraViewPoint = viewPoint;

                var upVector = new Direction
                {
                    Guid = Guid.NewGuid().ToString(),
                    X    = up.X.ToMeters(),
                    Y    = up.Y.ToMeters(),
                    Z    = up.Z.ToMeters()
                };
                camera.CameraUpVector = upVector;

                var direction = new Direction
                {
                    Guid = Guid.NewGuid().ToString(),
                    X    = -(vi.X.ToMeters()),
                    Y    = -(vi.Y.ToMeters()),
                    Z    = -(vi.Z.ToMeters())
                };
                camera.CameraDirection = direction;

                camera.ViewToWorldScale = zoomValue;
            }
            catch (Exception)
            {
                // ignored
            }

            return(camera);
        }
Example #7
0
        /// <summary>
        /// External Event Implementation
        /// </summary>
        /// <param name="app"></param>
        public void Execute(UIApplication app)
        {
            try
            {
                UIDocument uidoc      = app.ActiveUIDocument;
                Document   doc        = uidoc.Document;
                var        uniqueView = UserSettings.GetBool("alwaysNewView");

                // IS ORTHOGONAL
                if (v.OrthogonalCamera != null)
                {
                    if (v.OrthogonalCamera.CameraViewPoint == null || v.OrthogonalCamera.CameraUpVector == null || v.OrthogonalCamera.CameraDirection == null)
                    {
                        return;
                    }
                    //type = "OrthogonalCamera";
                    var zoom            = v.OrthogonalCamera.ViewToWorldScale.ToFeet();
                    var cameraDirection = RevitUtils.GetRevitXYZ(v.OrthogonalCamera.CameraDirection);
                    var cameraUpVector  = RevitUtils.GetRevitXYZ(v.OrthogonalCamera.CameraUpVector);
                    var cameraViewPoint = RevitUtils.GetRevitXYZ(v.OrthogonalCamera.CameraViewPoint);
                    var orient3D        = RevitUtils.ConvertBasePoint(doc, cameraViewPoint, cameraDirection, cameraUpVector, true);

                    View3D orthoView = null;
                    //if active view is 3d ortho use it
                    if (doc.ActiveView.ViewType == ViewType.ThreeD)
                    {
                        var activeView3D = doc.ActiveView as View3D;
                        if (!activeView3D.IsPerspective)
                        {
                            orthoView = activeView3D;
                        }
                    }
                    if (orthoView == null)
                    {
                        //try to use an existing 3D view
                        IEnumerable <View3D> viewcollector3D = get3DViews(doc);
                        if (viewcollector3D.Any(o => o.Name == "{3D}" || o.Name == "BCFortho"))
                        {
                            orthoView = viewcollector3D.First(o => o.Name == "{3D}" || o.Name == "BCFortho");
                        }
                    }
                    using (var trans = new Transaction(uidoc.Document))
                    {
                        if (trans.Start("Open orthogonal view") == TransactionStatus.Started)
                        {
                            //create a new 3d ortho view

                            if (orthoView == null || uniqueView)
                            {
                                orthoView      = View3D.CreateIsometric(doc, getFamilyViews(doc).First().Id);
                                orthoView.Name = (uniqueView) ? "BCFortho" + DateTime.Now.ToString("yyyyMMddTHHmmss") : "BCFortho";
                            }
                            else
                            {
                                //reusing an existing view, I net to reset the visibility
                                //placed this here because if set afterwards it doesn't work
                                orthoView.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
                            }
                            orthoView.SetOrientation(orient3D);
                            trans.Commit();
                        }
                    }
                    uidoc.ActiveView = orthoView;
                    //adjust view rectangle

                    // **** CUSTOM VALUE FOR TEKLA **** //
                    // double x = zoom / 2.5;
                    // **** CUSTOM VALUE FOR TEKLA **** //

                    double x = zoom;
                    //if(MySettings.Get("optTekla")=="1")
                    //    x = zoom / 2.5;

                    //set UI view position and zoom
                    XYZ m_xyzTl = uidoc.ActiveView.Origin.Add(uidoc.ActiveView.UpDirection.Multiply(x)).Subtract(uidoc.ActiveView.RightDirection.Multiply(x));
                    XYZ m_xyzBr = uidoc.ActiveView.Origin.Subtract(uidoc.ActiveView.UpDirection.Multiply(x)).Add(uidoc.ActiveView.RightDirection.Multiply(x));
                    uidoc.GetOpenUIViews().First().ZoomAndCenterRectangle(m_xyzTl, m_xyzBr);
                }
                //perspective
                else if (v.PerspectiveCamera != null)
                {
                    if (v.PerspectiveCamera.CameraViewPoint == null || v.PerspectiveCamera.CameraUpVector == null || v.PerspectiveCamera.CameraDirection == null)
                    {
                        return;
                    }

                    //not used since the fov cannot be changed in Revit
                    var zoom = v.PerspectiveCamera.FieldOfView;
                    //FOV - not used
                    //double z1 = 18 / Math.Tan(zoom / 2 * Math.PI / 180);
                    //double z = 18 / Math.Tan(25 / 2 * Math.PI / 180);
                    //double factor = z1 - z;

                    var cameraDirection = RevitUtils.GetRevitXYZ(v.PerspectiveCamera.CameraDirection);
                    var cameraUpVector  = RevitUtils.GetRevitXYZ(v.PerspectiveCamera.CameraUpVector);
                    var cameraViewPoint = RevitUtils.GetRevitXYZ(v.PerspectiveCamera.CameraViewPoint);
                    var orient3D        = RevitUtils.ConvertBasePoint(doc, cameraViewPoint, cameraDirection, cameraUpVector, true);



                    View3D perspView = null;
                    //try to use an existing 3D view
                    IEnumerable <View3D> viewcollector3D = get3DViews(doc);
                    if (viewcollector3D.Any(o => o.Name == "BCFpersp"))
                    {
                        perspView = viewcollector3D.First(o => o.Name == "BCFpersp");
                    }

                    using (var trans = new Transaction(uidoc.Document))
                    {
                        if (trans.Start("Open perspective view") == TransactionStatus.Started)
                        {
                            if (null == perspView || uniqueView)
                            {
                                perspView      = View3D.CreatePerspective(doc, getFamilyViews(doc).First().Id);
                                perspView.Name = (uniqueView) ? "BCFpersp" + DateTime.Now.ToString("yyyyMMddTHHmmss") : "BCFpersp";
                            }
                            else
                            {
                                //reusing an existing view, I net to reset the visibility
                                //placed this here because if set afterwards it doesn't work
                                perspView.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
                            }

                            perspView.SetOrientation(orient3D);

                            // turn off the far clip plane
                            if (perspView.get_Parameter(BuiltInParameter.VIEWER_BOUND_ACTIVE_FAR).HasValue)
                            {
                                Parameter m_farClip = perspView.get_Parameter(BuiltInParameter.VIEWER_BOUND_ACTIVE_FAR);
                                m_farClip.Set(0);
                            }
                            perspView.CropBoxActive  = true;
                            perspView.CropBoxVisible = true;

                            trans.Commit();
                        }
                    }
                    uidoc.ActiveView = perspView;
                }
                //sheet
                else if (v.SheetCamera != null)
                {
                    IEnumerable <View> viewcollectorSheet = getSheets(doc, v.SheetCamera.SheetID);
                    if (!viewcollectorSheet.Any())
                    {
                        MessageBox.Show("View " + v.SheetCamera.SheetName + " with Id=" + v.SheetCamera.SheetID + " not found.");
                        return;
                    }
                    uidoc.ActiveView = viewcollectorSheet.First();
                    uidoc.RefreshActiveView();

                    XYZ m_xyzTl = new XYZ(v.SheetCamera.TopLeft.X, v.SheetCamera.TopLeft.Y, v.SheetCamera.TopLeft.Z);
                    XYZ m_xyzBr = new XYZ(v.SheetCamera.BottomRight.X, v.SheetCamera.BottomRight.Y, v.SheetCamera.BottomRight.Z);
                    uidoc.GetOpenUIViews().First().ZoomAndCenterRectangle(m_xyzTl, m_xyzBr);
                }
                //no view included
                else
                {
                    return;
                }

                if (v.Components == null)
                {
                    return;
                }


                var elementsToSelect = new List <ElementId>();
                var elementsToHide   = new List <ElementId>();
                var elementsToShow   = new List <ElementId>();

                var visibleElems = new FilteredElementCollector(doc, doc.ActiveView.Id)
                                   .WhereElementIsNotElementType()
                                   .WhereElementIsViewIndependent()
                                   .ToElementIds()
                                   .Where(e => doc.GetElement(e).CanBeHidden(doc.ActiveView)); //might affect performance, but it's necessary


                bool canSetVisibility = (v.Components.Visibility != null &&
                                         v.Components.Visibility.DefaultVisibilitySpecified &&
                                         v.Components.Visibility.Exceptions.Any())
                ;
                bool canSetSelection = (v.Components.Selection != null && v.Components.Selection.Any());



                //loop elements
                foreach (var e in visibleElems)
                {
                    var guid = IfcGuid.ToIfcGuid(ExportUtils.GetExportId(doc, e));

                    if (canSetVisibility)
                    {
                        if (v.Components.Visibility.DefaultVisibility)
                        {
                            if (v.Components.Visibility.Exceptions.Any(x => x.IfcGuid == guid))
                            {
                                elementsToHide.Add(e);
                            }
                        }
                        else
                        {
                            if (v.Components.Visibility.Exceptions.Any(x => x.IfcGuid == guid))
                            {
                                elementsToShow.Add(e);
                            }
                        }
                    }

                    if (canSetSelection)
                    {
                        if (v.Components.Selection.Any(x => x.IfcGuid == guid))
                        {
                            elementsToSelect.Add(e);
                        }
                    }
                }



                using (var trans = new Transaction(uidoc.Document))
                {
                    if (trans.Start("Apply BCF visibility and selection") == TransactionStatus.Started)
                    {
                        if (elementsToHide.Any())
                        {
                            doc.ActiveView.HideElementsTemporary(elementsToHide);
                        }
                        //there are no items to hide, therefore hide everything and just show the visible ones
                        else if (elementsToShow.Any())
                        {
                            doc.ActiveView.IsolateElementsTemporary(elementsToShow);
                        }

                        if (elementsToSelect.Any())
                        {
                            uidoc.Selection.SetElementIds(elementsToSelect);
                        }
                    }
                    trans.Commit();
                }


                uidoc.RefreshActiveView();
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Error!", "exception: " + ex);
            }
        }