Ejemplo n.º 1
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            Application   app   = uiapp.Application;

            int check             = 0;
            int sheetsNumber      = 0;
            int viewsNumber       = 0;
            int schedulesNumber   = 0;
            int furnitureElements = 0;



            using (var formOpen = new FormOpenFile())
            {
                formOpen.ShowDialog();

                string fileName = formOpen.filePath;

                ModelPath modelP = ModelPathUtils.ConvertUserVisiblePathToModelPath(fileName);


                if (formOpen.DialogResult == winForms.DialogResult.OK)
                {
                    OpenOptions optionDetach = new OpenOptions();

                    optionDetach.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets;

                    optionDetach.Audit = true;

                    openDoc = app.OpenDocumentFile(modelP, optionDetach);

                    IEnumerable <ViewFamilyType> viewFamilyTypes = from elem in new FilteredElementCollector(openDoc).OfClass(typeof(ViewFamilyType))
                                                                   let type = elem as ViewFamilyType
                                                                              where type.ViewFamily == ViewFamily.ThreeDimensional
                                                                              select type;

                    IEnumerable <ElementId> sheetIds = from elem in new FilteredElementCollector(openDoc).OfCategory(BuiltInCategory.OST_Sheets) select elem.Id;

                    IEnumerable <ElementId> viewsIds = from elem in new FilteredElementCollector(openDoc).OfCategory(BuiltInCategory.OST_Views) where elem.Name != "Clarity_IFC _3D" select elem.Id;

#if REVIT2019
                    IEnumerable <ElementId> schedulesIds = from elem in new FilteredElementCollector(openDoc).OfCategory(BuiltInCategory.OST_Schedules) select elem.Id;
#elif REVIT2017
#endif

                    List <BuiltInCategory> builtInCats = new List <BuiltInCategory>();

                    builtInCats.Add(BuiltInCategory.OST_Furniture);
                    builtInCats.Add(BuiltInCategory.OST_Casework);
                    builtInCats.Add(BuiltInCategory.OST_Planting);
                    builtInCats.Add(BuiltInCategory.OST_Entourage);
                    builtInCats.Add(BuiltInCategory.OST_Railings);
                    builtInCats.Add(BuiltInCategory.OST_StairsRailing);

                    ElementMulticategoryFilter filter1 = new ElementMulticategoryFilter(builtInCats);


                    View3D view3d = null;

                    using (Transaction tran = new Transaction(openDoc))
                    {
                        tran.Start("Clarity Setup");

                        FilteredWorksetCollector worksetCollector = new FilteredWorksetCollector(openDoc).OfKind(WorksetKind.UserWorkset);

                        try
                        {
                            view3d = View3D.CreateIsometric(openDoc, viewFamilyTypes.First().Id);

                            view3d.Name = "Clarity_IFC _3D";

                            //uiapp.ActiveUIDocument.ActiveView = view3d;

                            foreach (Workset e in worksetCollector)
                            {
                                view3d.SetWorksetVisibility(e.Id, WorksetVisibility.Visible);
                            }
                        }
                        catch (Exception ex)
                        {
                            TaskDialog.Show("Error", "Name already taken" + "\n" + ex.Message);
                        }


                        try
                        {
                            sheetsNumber += sheetIds.Count();

                            if (sheetIds.Count() > 0)
                            {
                                openDoc.Delete(sheetIds.ToList());
                            }
                        }
                        catch (Exception ex)
                        {
                            TaskDialog.Show("Sheets Error", ex.Message);
                        }
                        try
                        {
                            viewsNumber += viewsIds.Count();

                            if (viewsIds.Count() > 0)
                            {
                                openDoc.Delete(viewsIds.ToList());
                            }
                        }
                        catch (Exception ex)
                        {
                            TaskDialog.Show("Views Error", ex.Message);
                        }
#if REVIT2019
                        try
                        {
                            if (schedulesIds.Count() > 0)
                            {
                                openDoc.Delete(schedulesIds.ToList());
                                schedulesNumber += schedulesIds.Count();
                            }
                        }
                        catch (Exception ex)
                        {
                            TaskDialog.Show("Schedule Error", ex.Message);
                        }
#elif REVIT2017
#endif

                        if (formOpen.cleanArchModel)
                        {
                            int furnitureError = 0;

                            ICollection <ElementId> toDelete = new FilteredElementCollector(openDoc).WherePasses(filter1).ToElementIds();


                            if (toDelete.Count() > 0)
                            {
                                string lastEx = "";
                                foreach (ElementId id in toDelete)
                                {
                                    try
                                    {
                                        openDoc.Delete(id);
                                        furnitureElements += 1;
                                    }
                                    catch (Exception ex)
                                    {
                                        lastEx = $"{ex.Message}\n";
                                    }
                                }
                                //Debug.WriteLine(lastEx.Message);
                                TaskDialog.Show("Error", $"{furnitureElements} elements deleted. {furnitureError} cannot be deleted. Errors:\n{lastEx}");
                            }
                        }

                        if (formOpen.purgeModel)
                        {
                            ICollection <ElementId> purgeableElements = null;

                            PurgeTool.GetPurgeableElements(openDoc, ref purgeableElements);
                            try
                            {
                                while (purgeableElements.Count > 0)
                                {
                                    //TaskDialog.Show("Purge Count", purgeableElements.Count().ToString());
                                    PurgeTool.GetPurgeableElements(openDoc, ref purgeableElements);
                                    openDoc.Delete(purgeableElements);
                                }
                            }
                            catch (Exception ex)
                            {
                                TaskDialog.Show("Purge Error", ex.Message);
                            }
                        }

                        tran.Commit();
                    }

                    SaveAsOptions            saveOpt = new SaveAsOptions();
                    WorksharingSaveAsOptions wos     = new WorksharingSaveAsOptions();
                    wos.SaveAsCentral = true;
                    saveOpt.Compact   = true;
                    saveOpt.SetWorksharingOptions(wos);
                    saveOpt.OverwriteExistingFile = true;

                    openDoc.SaveAs(modelP, saveOpt);


                    check += 1;
                }
                else
                {
                    TaskDialog.Show("Result", "Command aborted.");
                }

                if (check > 0)
                {
                    ICollection <ElementId> viewsId = new FilteredElementCollector(openDoc).OfCategory(BuiltInCategory.OST_Views).ToElementIds();

                    string viewsNames = "";

                    foreach (ElementId eid in viewsId)
                    {
                        viewsNames += openDoc.GetElement(eid).Name + Environment.NewLine;
                    }

                    TaskDialog.Show("Result", String.Format("Sheets deleted {0} \nViews deleted {1} \nSchedules deleted {2} \nViews in the model {3}",

                                                            sheetsNumber, viewsNumber, furnitureElements, viewsNames));
                }

                //PurgeMaterials(openDoc); too slooooooow
            }//close using

            return(Result.Succeeded);
        }
Ejemplo n.º 2
0
        public static View3D CreateWorkset3DView(Document doc, ItemInfo itemInfo, ViewFamilyType view3dFamilyType, bool overwrite)
        {
            View3D view3D = null;

            try
            {
                string viewName = "WS - 3D - " + itemInfo.ItemName;
                using (TransactionGroup tg = new TransactionGroup(doc))
                {
                    tg.Start("Create 3D View");
                    try
                    {
                        FilteredElementCollector collector = new FilteredElementCollector(doc);
                        List <View3D>            view3ds   = collector.OfClass(typeof(View3D)).ToElements().Cast <View3D>().ToList();
                        var views = from view in view3ds where view.Name == viewName select view;
                        if (views.Count() > 0)
                        {
                            if (overwrite)
                            {
                                view3D = views.First();
                            }
                            else
                            {
                                return(view3D);
                            }
                        }
                        if (null == view3D)
                        {
                            using (Transaction trans = new Transaction(doc))
                            {
                                trans.Start("Create Isometric");
                                try
                                {
                                    view3D      = View3D.CreateIsometric(doc, view3dFamilyType.Id);
                                    view3D.Name = viewName;
                                    if (view3D.CanModifyViewDiscipline())
                                    {
                                        view3D.Discipline = ViewDiscipline.Coordination;
                                    }
                                    trans.Commit();
                                }
                                catch (Exception ex)
                                {
                                    trans.RollBack();
                                    MessageBox.Show("Failed to create Isometric.\n" + ex.Message, "Create Isometric", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                            }
                        }

                        using (Transaction trans = new Transaction(doc))
                        {
                            trans.Start("Set Visibility");
                            try
                            {
                                FilteredWorksetCollector worksetCollector = new FilteredWorksetCollector(doc);
                                IList <Workset>          worksetList      = worksetCollector.ToWorksets();
                                var worksets = from workset in worksetList where workset.Kind == WorksetKind.UserWorkset select workset;
                                foreach (Workset ws in worksets)
                                {
                                    if (ws.Kind == WorksetKind.UserWorkset)
                                    {
                                        if (ws.Id.IntegerValue == itemInfo.ItemId)
                                        {
                                            view3D.SetWorksetVisibility(ws.Id, WorksetVisibility.Visible);
                                        }
                                        else
                                        {
                                            view3D.SetWorksetVisibility(ws.Id, WorksetVisibility.Hidden);
                                        }
                                    }
                                }
                                trans.Commit();
                            }
                            catch (Exception ex)
                            {
                                trans.RollBack();
                                MessageBox.Show("Failed to set visibility.\n" + ex.Message, "Set Visibility", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }

                        using (Transaction trans = new Transaction(doc))
                        {
                            trans.Start("Set SectionBox");
                            try
                            {
                                collector = new FilteredElementCollector(doc, view3D.Id);
                                List <Element> elements = collector.ToElements().ToList();
                                if (elements.Count > 0)
                                {
                                    BoundingBoxXYZ boundingBox = GetBoundingBox(elements);
                                    if (null != boundingBox)
                                    {
                                        view3D.SetSectionBox(boundingBox);
                                    }
                                }
                                trans.Commit();
                            }
                            catch (Exception ex)
                            {
                                trans.RollBack();
                                MessageBox.Show("Failed to set sectionbox.\n" + ex.Message, "Set Sectionbox", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Failed to create 3d views by worksets.\n" + ex.Message, "Create 3D Views by Worksets", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        tg.RollBack();
                    }
                    tg.Assimilate();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to create workset views.\n" + ex.Message, "Create Workset 3D View", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            return(view3D);
        }
Ejemplo n.º 3
0
        private bool DuplicateCameraView(ModelInfo sModel, ModelInfo rModel, CameraViewInfo cameraInfo, ViewFamilyType vFamilyType, out CameraViewInfo createdViewInfo)
        {
            bool duplicated = false;

            createdViewInfo = null;
            try
            {
                Document sourceDoc    = sModel.ModelDoc;
                Document recipientDoc = rModel.ModelDoc;

                using (Transaction trans = new Transaction(recipientDoc))
                {
                    trans.Start("Create Camera View");
                    try
                    {
                        View3D createdView = View3D.CreatePerspective(recipientDoc, vFamilyType.Id);
                        if (CanHaveViewName(rModel, cameraInfo.ViewName))
                        {
                            createdView.Name = cameraInfo.ViewName;
                        }
                        createdView.SetOrientation(cameraInfo.Orientation);
                        createdView.CropBoxActive      = cameraInfo.IsCropBoxOn;
                        createdView.CropBox            = cameraInfo.CropBox;
                        createdView.IsSectionBoxActive = cameraInfo.IsSectionBoxOn;
                        createdView.SetSectionBox(cameraInfo.SectionBox);
                        createdView.DisplayStyle = cameraInfo.Display;
                        //createdView.SetRenderingSettings(cameraInfo.Rendering);

                        foreach (string paramName in cameraInfo.ViewParameters.Keys)
                        {
                            Parameter sourceParam    = cameraInfo.ViewParameters[paramName];
                            Parameter recipientParam = createdView.LookupParameter(paramName);
                            if (parametersToSkip.Contains(sourceParam.Id.IntegerValue))
                            {
                                continue;
                            }

                            if (null != recipientParam && sourceParam.HasValue)
                            {
                                if (!recipientParam.IsReadOnly)
                                {
                                    switch (sourceParam.StorageType)
                                    {
                                    case StorageType.Double:
                                        try { recipientParam.Set(sourceParam.AsDouble()); }
                                        catch { }
                                        break;

                                    case StorageType.ElementId:
                                        /*
                                         * try { recipientParam.Set(sourceParam.AsElementId()); }
                                         * catch { }
                                         */
                                        break;

                                    case StorageType.Integer:
                                        try { recipientParam.Set(sourceParam.AsInteger()); }
                                        catch { }
                                        break;

                                    case StorageType.String:
                                        try { recipientParam.Set(sourceParam.AsString()); }
                                        catch { }
                                        break;
                                    }
                                }
                            }
                        }

                        if (cameraInfo.ViewTemplateId != ElementId.InvalidElementId)
                        {
                            ElementId templateId = GetLinkedItem(sModel, rModel, MapType.ViewTemplate, cameraInfo.ViewTemplateId);
                            if (templateId != ElementId.InvalidElementId && createdView.IsValidViewTemplate(templateId))
                            {
                                createdView.ViewTemplateId = templateId;
                            }
                            else
                            {
                                MissingItem missingItem = new MissingItem(cameraInfo.ViewName, "View Template", "");
                                missingItems.Add(missingItem);
                            }
                        }

                        if (cameraInfo.PhaseId != ElementId.InvalidElementId)
                        {
                            ElementId phaseId = GetLinkedItem(sModel, rModel, MapType.Phase, cameraInfo.PhaseId);
                            if (phaseId != ElementId.InvalidElementId)
                            {
                                Parameter param = createdView.get_Parameter(BuiltInParameter.VIEW_PHASE);
                                if (null != param)
                                {
                                    param.Set(phaseId);
                                }
                            }
                            else
                            {
                                MissingItem missingItem = new MissingItem(cameraInfo.ViewName, "Phase", "");
                                missingItems.Add(missingItem);
                            }
                        }

                        if (viewConfig.ApplyWorksetVisibility && cameraInfo.WorksetVisibilities.Count > 0)
                        {
                            bool worksetFound = true;
                            foreach (WorksetId wsId in cameraInfo.WorksetVisibilities.Keys)
                            {
                                WorksetVisibility wsVisibility = cameraInfo.WorksetVisibilities[wsId];
                                WorksetId         worksetId    = GetLinkedWorkset(sModel, rModel, wsId);
                                if (worksetId != WorksetId.InvalidWorksetId)
                                {
                                    createdView.SetWorksetVisibility(worksetId, wsVisibility);
                                }
                                else
                                {
                                    worksetFound = false;
                                }
                            }
                            if (!worksetFound)
                            {
                                MissingItem missingItem = new MissingItem(cameraInfo.ViewName, "Workset", "");
                                missingItems.Add(missingItem);
                            }
                        }

                        createdViewInfo = new CameraViewInfo(createdView);
                        createdViewInfo.LinkedViewId = cameraInfo.ViewId;
                        createdViewInfo.Linked       = true;
                        duplicated = true;
                        trans.Commit();
                    }
                    catch (Exception ex)
                    {
                        trans.RollBack();
                        string message = ex.Message;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to duplicate camera views.\n" + ex.Message, "Duplicate Camera Views", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
            return(duplicated);
        }