Example #1
0
        /// <summary>
        /// External Event Implementation
        /// </summary>
        /// <param name="app"></param>
        public void Execute(UIApplication app)
        {
            try
            {
                UIDocument uidoc = app.ActiveUIDocument;
                Document   doc   = uidoc.Document;
                //Selection m_elementsToHide = uidoc.Selection; //SelElementSet.Create();

                BlockingCollection <ElementId> elementsToBeIsolated = new BlockingCollection <ElementId>();
                BlockingCollection <ElementId> elementsToBeHidden   = new BlockingCollection <ElementId>();
                BlockingCollection <ElementId> elementsToBeSelected = new BlockingCollection <ElementId>();

                // IS ORTHOGONAL
                if (v.OrthogonalCamera != null)
                {
                    if (v.OrthogonalCamera.ViewToWorldScale == null || v.OrthogonalCamera.CameraViewPoint == null || v.OrthogonalCamera.CameraUpVector == null || v.OrthogonalCamera.CameraDirection == null)
                    {
                        return;
                    }
                    //type = "OrthogonalCamera";
                    var zoom            = UnitUtils.ConvertToInternalUnits(v.OrthogonalCamera.ViewToWorldScale, DisplayUnitType.DUT_METERS);
                    var CameraDirection = ARUP.IssueTracker.Revit.Classes.Utils.GetInternalXYZ(v.OrthogonalCamera.CameraDirection.X, v.OrthogonalCamera.CameraDirection.Y, v.OrthogonalCamera.CameraDirection.Z);
                    var CameraUpVector  = ARUP.IssueTracker.Revit.Classes.Utils.GetInternalXYZ(v.OrthogonalCamera.CameraUpVector.X, v.OrthogonalCamera.CameraUpVector.Y, v.OrthogonalCamera.CameraUpVector.Z);
                    var CameraViewPoint = ARUP.IssueTracker.Revit.Classes.Utils.GetInternalXYZ(v.OrthogonalCamera.CameraViewPoint.X, v.OrthogonalCamera.CameraViewPoint.Y, v.OrthogonalCamera.CameraViewPoint.Z);
                    var orient3d        = ARUP.IssueTracker.Revit.Classes.Utils.ConvertBasePoint(doc, CameraViewPoint, CameraDirection, CameraUpVector, true);


                    View3D orthoView = null;
                    //if active view is 3d ortho use it
                    if (doc.ActiveView.ViewType == ViewType.ThreeD)
                    {
                        View3D ActiveView3D = doc.ActiveView as View3D;
                        if (!ActiveView3D.IsPerspective)
                        {
                            orthoView = ActiveView3D;
                        }
                    }
                    string userDefault3dViewName = "{3D - " + app.Application.Username + "}";
                    string default3dViewName     = "{3D}";
                    string userBCForthoViewName  = string.Format("BCFortho_{0}", app.Application.Username);
                    if (orthoView == null)
                    {
                        IEnumerable <View3D> viewcollector3D = get3DViews(doc);
                        //find a orthographic 3D view
                        if (viewcollector3D.Any())
                        {
                            if (viewcollector3D.Where(o => o.Name == userDefault3dViewName).Any()) // 1) try to find user specific 3D view in workshared environment
                            {
                                orthoView = viewcollector3D.Where(o => o.Name == userDefault3dViewName).First();
                            }
                            else if (viewcollector3D.Where(o => o.Name == default3dViewName).Any()) // 2) try to find the default 3D view if 1) not found
                            {
                                orthoView = viewcollector3D.Where(o => o.Name == default3dViewName).First();
                            }
                            else if (viewcollector3D.Where(o => o.Name == userBCForthoViewName).Any()) // 3) try to find BCFortho 3D view generated by AIT previously if 2) is not found
                            {
                                orthoView = viewcollector3D.Where(o => o.Name == userBCForthoViewName).First();
                            }
                        }
                    }
                    using (Transaction trans = new Transaction(uidoc.Document))
                    {
                        if (trans.Start("Open orthogonal view") == TransactionStatus.Started)
                        {
                            //create a new user-specific 3d ortho view
                            if (orthoView == null)
                            {
                                orthoView      = View3D.CreateIsometric(doc, getFamilyViews(doc).First().Id);
                                orthoView.Name = userBCForthoViewName;
                            }

                            orthoView.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
                            orthoView.SetOrientation(orient3d);
                            trans.Commit();
                        }
                    }
                    uidoc.ActiveView = orthoView;
                    //adjust view rectangle

                    // **** CUSTOM VALUE FOR TEKLA **** //
                    // double x = touple.Item2
                    // **** CUSTOM VALUE FOR TEKLA **** //
                    double customZoomValue = (MyProjectSettings.Get("useDefaultZoom", doc.PathName) == "1") ? 1 : 2.5;
                    double x       = zoom / customZoomValue;
                    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);
                }

                else if (v.PerspectiveCamera != null)
                {
                    if (v.PerspectiveCamera.FieldOfView == null || v.PerspectiveCamera.CameraViewPoint == null || v.PerspectiveCamera.CameraUpVector == null || v.PerspectiveCamera.CameraDirection == null)
                    {
                        return;
                    }

                    var    zoom   = v.PerspectiveCamera.FieldOfView;
                    double z1     = 18 / Math.Tan(zoom / 2 * Math.PI / 180); //focale 1
                    double z      = 18 / Math.Tan(25 / 2 * Math.PI / 180);   //focale, da controllare il 18, vedi PDF
                    double factor = z1 - z;

                    var CameraDirection = ARUP.IssueTracker.Revit.Classes.Utils.GetInternalXYZ(v.PerspectiveCamera.CameraDirection.X, v.PerspectiveCamera.CameraDirection.Y, v.PerspectiveCamera.CameraDirection.Z);
                    var CameraUpVector  = ARUP.IssueTracker.Revit.Classes.Utils.GetInternalXYZ(v.PerspectiveCamera.CameraUpVector.X, v.PerspectiveCamera.CameraUpVector.Y, v.PerspectiveCamera.CameraUpVector.Z);
                    XYZ oldO            = ARUP.IssueTracker.Revit.Classes.Utils.GetInternalXYZ(v.PerspectiveCamera.CameraViewPoint.X, v.PerspectiveCamera.CameraViewPoint.Y, v.PerspectiveCamera.CameraViewPoint.Z);
                    var CameraViewPoint = (oldO.Subtract(CameraDirection.Divide(factor)));
                    var orient3d        = ARUP.IssueTracker.Revit.Classes.Utils.ConvertBasePoint(doc, CameraViewPoint, CameraDirection, CameraUpVector, true);

                    View3D perspView = null;

                    IEnumerable <View3D> viewcollector3D = get3DViews(doc);
                    string userPersp3dViewName           = string.Format("BCFperps_{0}", app.Application.Username);
                    // find a perspective view
                    if (viewcollector3D.Any() && viewcollector3D.Where(o => o.Name == userPersp3dViewName).Any())
                    {
                        perspView = viewcollector3D.Where(o => o.Name == userPersp3dViewName).First();
                    }
                    using (Transaction trans = new Transaction(uidoc.Document))
                    {
                        if (trans.Start("Open perspective view") == TransactionStatus.Started)
                        {
                            if (null == perspView)
                            {
                                perspView      = View3D.CreatePerspective(doc, getFamilyViews(doc).First().Id);
                                perspView.Name = userPersp3dViewName;
                            }

                            perspView.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
                            perspView.SetOrientation(orient3d);

                            // turn on the far clip plane with standard parameter API
                            if (perspView.get_Parameter(BuiltInParameter.VIEWER_BOUND_ACTIVE_FAR).HasValue)
                            {
                                Parameter m_farClip = perspView.get_Parameter(BuiltInParameter.VIEWER_BOUND_ACTIVE_FAR);
                                m_farClip.Set(1);
                            }
                            // reset far clip offset
                            if (perspView.get_Parameter(BuiltInParameter.VIEWER_BOUND_OFFSET_FAR).HasValue)
                            {
                                Parameter m_farClipOffset = perspView.get_Parameter(BuiltInParameter.VIEWER_BOUND_OFFSET_FAR);
                                m_farClipOffset.SetValueString("35");
                            }
                            // turn off the far clip plane with standard parameter API
                            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;
                }
                else if (v.SheetCamera != null)//sheet
                {
                    //using (Transaction trans = new Transaction(uidoc.Document))
                    //{
                    //    if (trans.Start("Open sheet view") == TransactionStatus.Started)
                    //    {
                    IEnumerable <View> viewcollectorSheet = getSheets(doc, v.SheetCamera.SheetID);
                    if (!viewcollectorSheet.Any())
                    {
                        MessageBox.Show("No Sheet with Id=" + v.SheetCamera.SheetID + " found.");
                        return;
                    }
                    uidoc.ActiveView = viewcollectorSheet.First();
                    uidoc.ActiveView.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
                    uidoc.RefreshActiveView();

                    //        trans.Commit();
                    //    }
                    //}
                    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);
                }
                else
                {
                    return;
                }

                //apply BCF clipping planes to Revit section box
                View3D view3D = doc.ActiveView as View3D;
                if (view3D != null)
                {
                    // resume section box state
                    using (Transaction trans = new Transaction(uidoc.Document))
                    {
                        if (trans.Start("Resume Section Box") == TransactionStatus.Started)
                        {
                            view3D.IsSectionBoxActive = false;
                        }
                        trans.Commit();
                    }

                    if (v.ClippingPlanes != null)
                    {
                        if (v.ClippingPlanes.Count() > 0)
                        {
                            var result = getBoundingBoxFromClippingPlanes(doc, v.ClippingPlanes);

                            if (result != null)
                            {
                                BoundingBoxXYZ computedBox = result.Item1;
                                Transform      rotate      = result.Item2;

                                using (Transaction trans = new Transaction(uidoc.Document))
                                {
                                    if (trans.Start("Apply Section Box") == TransactionStatus.Started)
                                    {
                                        view3D.IsSectionBoxActive = true;
                                        view3D.SetSectionBox(computedBox);

                                        if (rotate != null)
                                        {
                                            // Transform the View3D's section box with the rotation transform
                                            computedBox.Transform = computedBox.Transform.Multiply(rotate);

                                            // Set the section box back to the view (requires an open transaction)
                                            view3D.SetSectionBox(computedBox);
                                        }
                                    }
                                    trans.Commit();
                                }
                            }
                        }
                    }
                }

                // resume visibility and selection
                using (Transaction trans = new Transaction(uidoc.Document))
                {
                    if (trans.Start("Resume Visibility/Selection") == TransactionStatus.Started)
                    {
#if REVIT2014
                        uidoc.ActiveView.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
                        uidoc.Selection.Elements.Clear();
#elif REVIT2015
                        uidoc.ActiveView.DisableTemporaryViewMode(TemporaryViewMode.TemporaryHideIsolate);
                        uidoc.Selection.SetElementIds(new List <ElementId>());
#else
                        uidoc.ActiveView.TemporaryViewModes.DeactivateAllModes();
                        uidoc.Selection.SetElementIds(new List <ElementId>());
#endif
                    }
                    trans.Commit();
                }

                //select/hide elements
                if (v.Components != null && v.Components.Any())
                {
                    //if (v.Components.Count > 100)
                    //{
                    //    var result = MessageBox.Show("Too many elements attached. It may take for a while to isolate/select them. Do you want to continue?", "Warning", MessageBoxButton.YesNo, MessageBoxImage.Warning);
                    //    if (result == MessageBoxResult.No)
                    //    {
                    //        uidoc.RefreshActiveView();
                    //        return;
                    //    }
                    //}

                    revitWindow.initializeProgressWin(v.Components.Count);

                    FilteredElementCollector collector = new FilteredElementCollector(doc, doc.ActiveView.Id);
                    var allElementIds = collector.ToElementIds();
                    Dictionary <Guid, ElementId> exportIdElementIdDic = new Dictionary <Guid, ElementId>();
                    Dictionary <int, ElementId>  uniqueIdElementIdDic = new Dictionary <int, ElementId>();
                    foreach (ElementId eId in allElementIds)
                    {
                        Guid guid             = ExportUtils.GetExportId(doc, eId);
                        int  elementIdInteger = Convert.ToInt32(doc.GetElement(eId).UniqueId.Substring(37), 16);
                        if (!exportIdElementIdDic.ContainsKey(guid))
                        {
                            exportIdElementIdDic.Add(guid, eId);
                        }
                        if (!uniqueIdElementIdDic.ContainsKey(elementIdInteger))
                        {
                            uniqueIdElementIdDic.Add(elementIdInteger, eId);
                        }
                    }
                    //System.Threading.Tasks.Parallel.For(0, v.Components.Count, i => {
                    for (int i = 0; i < v.Components.Count; i++)
                    {
                        double percentage = ((double)i / (double)v.Components.Count) * 100;
                        revitWindow.updateProgressWin((int)percentage, i, v.Components.Count);

                        ARUP.IssueTracker.Classes.BCF2.Component e = v.Components[i];
                        ElementId currentElementId = null;

                        // find by ElementId first if OriginatingSystem is Revit
                        if (e.OriginatingSystem != null)
                        {
                            if (e.OriginatingSystem.Contains("Revit") && !string.IsNullOrEmpty(e.AuthoringToolId))
                            {
                                int elementId = -1;
                                int.TryParse(e.AuthoringToolId, out elementId);
                                if (elementId != -1)
                                {
                                    Element ele = doc.GetElement(new ElementId(elementId));
                                    if (ele != null)
                                    {
                                        currentElementId = ele.Id;
                                    }
                                }
                            }
                        }

                        // find by IfcGuid if ElementId not found
                        if (currentElementId == null)
                        {
                            var bcfguid = IfcGuid.FromIfcGUID(e.IfcGuid);
                            if (exportIdElementIdDic.ContainsKey(bcfguid))
                            {
                                currentElementId = exportIdElementIdDic[bcfguid];
                            }
                        }

                        // find by UniqueId if IfcGuid not found
                        if (currentElementId == null)
                        {
                            int authoringToolId = -1;
                            int.TryParse(e.AuthoringToolId, out authoringToolId);
                            if (uniqueIdElementIdDic.ContainsKey(authoringToolId))
                            {
                                currentElementId = uniqueIdElementIdDic[authoringToolId];
                            }
                        }

                        if (currentElementId != null)
                        {
                            // handle visibility
                            if (e.Visible)
                            {
                                elementsToBeIsolated.Add(currentElementId);
                            }
                            else
                            {
                                elementsToBeHidden.Add(currentElementId);
                            }

                            // handle selection
                            if (e.Selected)
                            {
                                elementsToBeSelected.Add(currentElementId);
                            }
                        }
                    }
                    ;

                    if (elementsToBeHidden.Count > 0)
                    {
                        using (Transaction trans = new Transaction(uidoc.Document))
                        {
                            if (trans.Start("Hide Elements") == TransactionStatus.Started)
                            {
                                uidoc.ActiveView.HideElementsTemporary(elementsToBeHidden.ToList());
                            }
                            trans.Commit();
                        }
                    }
                    else if (elementsToBeIsolated.Count > 0)
                    {
                        using (Transaction trans = new Transaction(uidoc.Document))
                        {
                            if (trans.Start("Isolate Elements") == TransactionStatus.Started)
                            {
                                uidoc.ActiveView.IsolateElementsTemporary(elementsToBeIsolated.ToList());
                            }
                            trans.Commit();
                        }
                    }

                    if (elementsToBeSelected.Count > 0)
                    {
                        using (Transaction trans = new Transaction(uidoc.Document))
                        {
                            if (trans.Start("Select Elements") == TransactionStatus.Started)
                            {
#if REVIT2014
                                SelElementSet selectedElements = SelElementSet.Create();
                                elementsToBeSelected.ToList().ForEach(id =>
                                {
                                    selectedElements.Add(doc.GetElement(id));
                                });

                                uidoc.Selection.Elements = selectedElements;
#else
                                uidoc.Selection.SetElementIds(elementsToBeSelected.ToList());
#endif
                            }
                            trans.Commit();
                        }
                    }

                    revitWindow.disposeProgressWin();
                }

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