Example #1
0
        /// <summary>
        /// delete elements depends on the input params.json file
        /// </summary>
        /// <param name="data"></param>
        public static void DeleteAllElements(DesignAutomationData data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            Application rvtApp = data.RevitApp;

            if (rvtApp == null)
            {
                throw new InvalidDataException(nameof(rvtApp));
            }

            string modelPath = data.FilePath;

            if (String.IsNullOrWhiteSpace(modelPath))
            {
                throw new InvalidDataException(nameof(modelPath));
            }

            // If the input revit model passed is a workshared revit file then by default the Design Automation
            // bridge will open the model detached from central, opting DetachAndPreserveWorsets option.
            // Non-worshared revit file will be load as is.
            Document doc = data.RevitDoc;

            if (doc == null)
            {
                throw new InvalidOperationException("Could not open document.");
            }


            // For CountIt workItem: If RvtParameters is null, count all types
            DeleteElementsParams deleteElementsParams = DeleteElementsParams.Parse("params.json");

            using (Transaction transaction = new Transaction(doc))
            {
                transaction.Start("Delete Elements");
                if (deleteElementsParams.walls)
                {
                    FilteredElementCollector col = new FilteredElementCollector(doc).OfClass(typeof(Wall));
                    doc.Delete(col.ToElementIds());
                }
                if (deleteElementsParams.floors)
                {
                    FilteredElementCollector col = new FilteredElementCollector(doc).OfClass(typeof(Floor));
                    doc.Delete(col.ToElementIds());
                }
                if (deleteElementsParams.doors)
                {
                    FilteredElementCollector collector  = new FilteredElementCollector(doc);
                    ICollection <ElementId>  collection = collector.OfClass(typeof(FamilyInstance))
                                                          .OfCategory(BuiltInCategory.OST_Doors)
                                                          .ToElementIds();
                    doc.Delete(collection);
                }
                if (deleteElementsParams.windows)
                {
                    FilteredElementCollector collector  = new FilteredElementCollector(doc);
                    ICollection <ElementId>  collection = collector.OfClass(typeof(FamilyInstance))
                                                          .OfCategory(BuiltInCategory.OST_Windows)
                                                          .ToElementIds();
                    doc.Delete(collection);
                }
                transaction.Commit();
            }

            ModelPath path = ModelPathUtils.ConvertUserVisiblePathToModelPath("result.rvt");
            // If a worshared file is opened as a part of this addin then the new file will be
            // saved as central.
            SaveAsOptions opts = new SaveAsOptions();

            if (doc.IsWorkshared)
            {
                opts.SetWorksharingOptions(new WorksharingSaveAsOptions {
                    SaveAsCentral = true
                });
                WorksharingUtils.RelinquishOwnership(doc, new RelinquishOptions(true), new TransactWithCentralOptions());
            }
            doc.SaveAs(path, new SaveAsOptions());
        }
Example #2
0
            public void Execute(UIApplication app)
            {
                if (App.docdict.Keys.Count == 0)
                {
                    return;
                }

                TransactWithCentralOptions transact      = new TransactWithCentralOptions();
                SynchLockCallback          transCallBack = new SynchLockCallback();

                transact.SetLockCallback(transCallBack);
                SynchronizeWithCentralOptions syncset           = new SynchronizeWithCentralOptions();
                RelinquishOptions             relinquishoptions = new RelinquishOptions(true)
                {
                    CheckedOutElements = true
                };

                syncset.SetRelinquishOptions(relinquishoptions);

                App.running = true;

                if (relinquish)
                {
                    foreach (Document doc in App.docdict.Keys)
                    {
                        try
                        {
                            WorksharingUtils.RelinquishOwnership(doc, relinquishoptions, transact);
                        }
                        catch { }
                    }

                    relinquish = false;
                }
                if (sync)
                {
                    if (App.Dismiss())
                    {
                        sync        = false;
                        App.running = false;
                        return;
                    }

                    app.Application.FailuresProcessing += FailureProcessor;

                    bool syncfailed = false;

                    foreach (Document doc in App.docdict.Keys)
                    {
                        try
                        {
                            if (docdict[doc])
                            {
                                doc.SynchronizeWithCentral(transact, syncset);
                                app.Application.WriteJournalComment("AutoSync", true);
                            }
                        }
                        catch
                        {
                            syncfailed = true;
                        }
                    }

                    app.Application.FailuresProcessing -= FailureProcessor;

                    if (syncfailed)
                    {
                        countdown -= retrysync;
                    }

                    sync = false;
                }
                if (close)
                {
                    if (App.Dismiss())
                    {
                        App.running = false;
                        close       = false;
                        return;
                    }

                    bool closelast = false;

                    string activepathname = app.ActiveUIDocument.Document.PathName;

                    List <Document> docsdeletelist = new List <Document>();

                    foreach (Document doc in App.docdict.Keys)
                    {
                        if (activepathname == doc.PathName)
                        {
                            closelast = true;
                        }
                        else
                        {
                            docsdeletelist.Add(doc);
                        }
                    }
                    foreach (Document doc in docsdeletelist)
                    {
                        try
                        {
                            doc.Close(false);
                        }
                        catch { }
                    }

                    if (closelast)
                    {
                        RevitCommandId closeDoc = RevitCommandId.LookupPostableCommandId(PostableCommand.Close);
                        app.PostCommand(closeDoc);
                    }

                    close     = false;
                    countdown = 0;
                }

                App.running = false;

                transact.Dispose();
                syncset.Dispose();
                relinquishoptions.Dispose();
            }
        public bool UpgradeRevitProject(Document document, string revitFileName)
        {
            bool upgraded = false;

            try
            {
                BasicFileInfo     basicFileInfo     = BasicFileInfo.Extract(revitFileName);
                FileSaveAsOptions fileSaveAsOptions = projectSettings.UpgradeOptions.UpgradeVersionSaveAsOptions;

                LogFileManager.AppendLog("Upgrade Revit Project: " + revitFileName);
                LogFileManager.AppendLog("The Original Revit file was saved in " + basicFileInfo.SavedInVersion);

                SaveAsOptions saveAsOptions = new SaveAsOptions();
                saveAsOptions.OverwriteExistingFile = true;

                if (basicFileInfo.IsWorkshared)
                {
                    WorksharingSaveAsOptions worksharingSaveAsOptions = new WorksharingSaveAsOptions();
                    worksharingSaveAsOptions.OpenWorksetsDefault = FindWorksetOption(fileSaveAsOptions.WorksetConfiguration);
                    worksharingSaveAsOptions.SaveAsCentral       = fileSaveAsOptions.MakeCentral;

                    saveAsOptions.MaximumBackups = fileSaveAsOptions.NumOfBackups;
                    saveAsOptions.SetWorksharingOptions(worksharingSaveAsOptions);
                }

                bool isFinalUpgrade = projectSettings.UpgradeOptions.IsFinalUpgrade;
                if (isFinalUpgrade)
                {
                    string backupDirectory = FindBackupDirectory(revitFileName);
                    if (!string.IsNullOrEmpty(backupDirectory))
                    {
                        string fileName    = Path.GetFileName(revitFileName);
                        string newFilePath = Path.Combine(backupDirectory, fileName);
                        File.Copy(revitFileName, newFilePath, true);
                        if (File.Exists(newFilePath))
                        {
                            document.SaveAs(revitFileName, saveAsOptions);
                            LogFileManager.AppendLog("Backup Saved: " + newFilePath);
                            if (fileSaveAsOptions.Relinquish)
                            {
                                RelinquishOptions roptions = new RelinquishOptions(false);
                                roptions.UserWorksets = true;
                                TransactWithCentralOptions coptions = new TransactWithCentralOptions();
                                WorksharingUtils.RelinquishOwnership(document, roptions, coptions);
                                LogFileManager.AppendLog("Relinquish all worksets created by the current user.");
                            }
                            upgraded = true;
                        }
                    }
                    else
                    {
                        LogFileManager.AppendLog("File Not Saved", "The backup directory cannot be found.");
                        upgraded = false;
                    }
                }
                else
                {
                    string reviewDirectory = FindReviewDirectory(revitFileName);
                    if (string.IsNullOrEmpty(reviewDirectory))
                    {
                        reviewDirectory = fileSaveAsOptions.ReviewLocation;
                    }
                    string fileName = Path.GetFileName(revitFileName);
                    if (!string.IsNullOrEmpty(reviewDirectory))
                    {
                        revitFileName = Path.Combine(reviewDirectory, fileName);
                        document.SaveAs(revitFileName, saveAsOptions);
                        LogFileManager.AppendLog("File Saved: " + revitFileName);
                        if (fileSaveAsOptions.Relinquish)
                        {
                            RelinquishOptions roptions = new RelinquishOptions(false);
                            roptions.UserWorksets = true;
                            TransactWithCentralOptions coptions = new TransactWithCentralOptions();
                            WorksharingUtils.RelinquishOwnership(document, roptions, coptions);
                            LogFileManager.AppendLog("Relinquish all worksets created by the current user.");
                        }
                        upgraded = true;
                    }
                    else
                    {
                        LogFileManager.AppendLog("File Not Saved", "The review directory cannot be found.");
                        upgraded = false;
                    }
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
                LogFileManager.AppendLog("[Error] Upgrade Revit Project", message);
            }
            return(upgraded);
        }