Exemplo 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);
        }
Exemplo n.º 2
0
        public Result Execute(
            ExternalCommandData commandData,
            ref string message,
            ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument    uidoc = uiapp.ActiveUIDocument;
            Application   app   = uiapp.Application;



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

                string[] filePath = System.IO.Directory.GetFiles(formOpen.filePath);

                foreach (string file in filePath)
                {
                    Document open_file = app.OpenDocumentFile(file);


                    FamilyManager fm = open_file.FamilyManager;


                    using (Transaction t = new Transaction(open_file, "Remove types"))
                    {
                        t.Start();

                        FamilyTypeSet familyTypes = fm.Types;

                        int count = familyTypes.Size;

                        while (count > 1)
                        {
                            fm.DeleteCurrentType();
                            count -= 1;
                        }

                        try
                        {
                            fm.RenameCurrentType("Default");
                        }
                        catch
                        {
                            //Do nothing
                        }


                        ICollection <ElementId> purgeableElements = null;

                        if (PurgeTool.GetPurgeableElements(open_file, ref purgeableElements) & purgeableElements.Count > 0)
                        {
                            open_file.Delete(purgeableElements);
                        }


                        t.Commit();


                        /*
                         * Press.Keys("PU");
                         * winForm.SendKeys.SendWait("{ENTER}");
                         *
                         * String s_commandToDisable = "ID_PURGE_UNUSED";
                         * RevitCommandId s_commandId = RevitCommandId.LookupCommandId(s_commandToDisable);
                         * uiapp.PostCommand(s_commandId);*/
                    }

                    //open_file.Save();
                    open_file.Close();
                }
            }

            return(Result.Succeeded);
        }