public static void ApplyWorksetModifications(Document doc, List <WorksetParameter> worksets, ref TransactionLog log) { using (Transaction t = new Transaction(doc)) { if (t.Start("Add Worksets to Model.") == TransactionStatus.Started) { foreach (var w in worksets) { if (w.IsModified) { if (w.IsExisting && w.Id >= 0) { if (WorksetTable.IsWorksetNameUnique(doc, w.Name)) { WorksetTable.RenameWorkset(doc, new WorksetId(w.Id), w.Name); } var defaultVisibilitySettings = WorksetDefaultVisibilitySettings.GetWorksetDefaultVisibilitySettings(doc); defaultVisibilitySettings.SetWorksetVisibility(new WorksetId(w.Id), w.VisibleInAllViews); log.AddSuccess(w.ToString()); } else { Workset newWorkset = null; if (WorksetTable.IsWorksetNameUnique(doc, w.Name)) { newWorkset = Workset.Create(doc, w.Name); } else { log.AddFailure(w.ToString()); continue; } if (newWorkset != null) { var defaultVisibilitySettings = WorksetDefaultVisibilitySettings.GetWorksetDefaultVisibilitySettings(doc); defaultVisibilitySettings.SetWorksetVisibility(newWorkset.Id, w.VisibleInAllViews); log.AddSuccess(w.ToString()); } } } } t.Commit(); } } }
public static bool SetWorksetName(int WorksetId, string Name) { var doc = DocumentManager.Instance.CurrentDBDocument; if (doc.IsWorkshared) { try { WorksetId id = null; FilteredWorksetCollector worksets = new FilteredWorksetCollector(doc).OfKind(WorksetKind.UserWorkset); foreach (Autodesk.Revit.DB.Workset w in worksets) { if (w.Id.IntegerValue == WorksetId) { id = w.Id; break; } } if (id != null) { using (Autodesk.Revit.DB.Transaction t = new Autodesk.Revit.DB.Transaction(doc, "Dynamo_SetProjectNumber")) { t.Start(); WorksetTable.RenameWorkset(doc, id, Name); t.Commit(); return(true); } } else { throw new Exception("Workset is not available in this document."); } } catch (Exception ex) { throw new Exception(ex.ToString()); } } else { throw new Exception("The current file is not workshared."); } }
public static IDictionary Rename(Workset workset, string name) { //Get Revit Document object revitDoc doc = DocumentManager.Instance.CurrentDBDocument; Workset renamedWorkset = null; bool renamed = false; if (name != null && workset != null) { //Verify that the existing workset is in the document. //If the workset is unique, create it if (WorksetTable.IsWorksetNameUnique(doc, workset.internalWorkset.Name) == false) { // Verify that the new name doesn't already exist if (WorksetTable.IsWorksetNameUnique(doc, name) == true) { //Only rename workset if it's name isn't an empty string if (name != "") { using (Autodesk.Revit.DB.Transaction trans = new Autodesk.Revit.DB.Transaction(doc)) { trans.Start("Rename Workset"); WorksetTable.RenameWorkset(doc, workset.internalId, name); workset.internalName = workset.internalWorkset.Name; renamedWorkset = workset; renamed = true; trans.Commit(); } } } } } return(new Dictionary <string, object> { { "workset", renamedWorkset }, { "renamed", renamed } }); }
private Result CreateWorksets(Document document, List <string> worksets) { if (document.IsWorkshared && !document.IsModifiable) { using (TransactionGroup worksetGroup = new TransactionGroup(document)) { try { worksetGroup.Start("Create Worksets"); IList <Autodesk.Revit.DB.Workset> userWork = UserWorksets(document); foreach (Autodesk.Revit.DB.Workset k in userWork) { if (k.Name == "Workset1") { using (Transaction worksetTransaction = new Transaction(document)) { worksetTransaction.Start("Create Workset: New Name"); WorksetTable.RenameWorkset(document, k.Id, "Your New Name as a String!"); worksetTransaction.Commit(); } } } worksetGroup.Assimilate(); return(Result.Succeeded); } catch (Autodesk.Revit.Exceptions.InvalidOperationException ex) { return(Result.Failed); } } } else { return(Result.Cancelled); } }
public SetupCWSRequest(UIApplication uiApp, String text) { MainUI uiForm = BARevitTools.Application.thisApp.newMainUi; RVTDocument doc = uiApp.ActiveUIDocument.Document; //Make the transaction options TransactWithCentralOptions TWCOptions = new TransactWithCentralOptions(); //Make the relinquish options RelinquishOptions relinquishOptions = new RelinquishOptions(true); //Make the synchronization options SynchronizeWithCentralOptions SWCOptions = new SynchronizeWithCentralOptions(); SWCOptions.Compact = true; SWCOptions.SetRelinquishOptions(relinquishOptions); //Make the worksharing SaveAs options WorksharingSaveAsOptions worksharingSaveOptions = new WorksharingSaveAsOptions(); worksharingSaveOptions.SaveAsCentral = true; worksharingSaveOptions.OpenWorksetsDefault = SimpleWorksetConfiguration.AllWorksets; //Make the save options SaveOptions projectSaveOptions = new SaveOptions(); projectSaveOptions.Compact = true; //Finally, make the SaveAs options SaveAsOptions projectSaveAsOptions = new SaveAsOptions(); projectSaveAsOptions.Compact = true; projectSaveAsOptions.OverwriteExistingFile = true; projectSaveAsOptions.SetWorksharingOptions(worksharingSaveOptions); //The worksetsToAdd list will the names of the worksets to create List <string> worksetsToAdd = new List <string>(); //Collect the names of the worksets from the defaults foreach (string item in uiForm.setupCWSDefaultListBox.Items) { worksetsToAdd.Add(item); } //Collect any names selected in the extended list foreach (string item in uiForm.setupCWSExtendedListBox.CheckedItems) { worksetsToAdd.Add(item); } //Collect the names of any user defined worksets foreach (DataGridViewRow row in uiForm.setupCWSUserDataGridView.Rows) { try { if (row.Cells[0].Value.ToString() != "") { worksetsToAdd.Add(row.Cells[0].Value.ToString()); } } catch { continue; } } //Collect all worksets in the current project List <Workset> worksets = new FilteredWorksetCollector(doc).Cast <Workset>().ToList(); List <string> worksetNames = new List <string>(); //Cycle through the worksets in the project if (worksets.Count > 0) { foreach (Workset workset in worksets) { //If Workset1 exists, rename it Arch if (workset.Name == "Workset1") { try { WorksetTable.RenameWorkset(doc, workset.Id, "Arch"); worksetNames.Add("Arch"); break; } catch { continue; } } else { worksetNames.Add(workset.Name); } } } //If the file has been saved and is workshared, continue if (doc.IsWorkshared && doc.PathName != "") { //Start a transaction Transaction t1 = new Transaction(doc, "CreateWorksets"); t1.Start(); //For each workset name in the list of worksets to add, continue foreach (string worksetName in worksetsToAdd) { //If the list of existing worksets does not contain the workset to make, continue if (!worksetNames.Contains(worksetName)) { //Create the new workset Workset.Create(doc, worksetName); } } t1.Commit(); try { //Save the project with the save options, then synchronize doc.Save(projectSaveOptions); doc.SynchronizeWithCentral(TWCOptions, SWCOptions); } catch { //Else, try using the SaveAs method, then synchronize doc.SaveAs(doc.PathName, projectSaveAsOptions); doc.SynchronizeWithCentral(TWCOptions, SWCOptions); } } //If the project has been saved and the document is not workshared, continue else if (!doc.IsWorkshared && doc.PathName != "") { //Make the document workshared and set the default worksets doc.EnableWorksharing("Shared Levels and Grids", "Arch"); //Add the default worksets to the list of pre-existing worksets worksetNames.Add("Shared Levels and Grids"); worksetNames.Add("Arch"); //Start the transaction Transaction t1 = new Transaction(doc, "MakeWorksets"); t1.Start(); foreach (string worksetName in worksetsToAdd) { //Create the workset if it does not exist in the list of pre-existing worksets if (!worksetNames.Contains(worksetName)) { Workset.Create(doc, worksetName); } } t1.Commit(); //Use the SaveAs method for saving the file, then synchronize doc.SaveAs(doc.PathName, projectSaveAsOptions); doc.SynchronizeWithCentral(TWCOptions, SWCOptions); } else { //If the project has not been saved, let the user know to save it first somewhere MessageBox.Show("Project file needs to be saved somewhere before it can be made a central model"); } }