diff --git a/Test/Sample/DeteachAndSaveCommand.cs b/Test/Sample/DeteachAndSaveCommand.cs new file mode 100644 index 0000000..a6477ee --- /dev/null +++ b/Test/Sample/DeteachAndSaveCommand.cs @@ -0,0 +1,74 @@ +using System.IO; +using Autodesk.Revit.ApplicationServices; +using Autodesk.Revit.Attributes; +using Autodesk.Revit.DB; +using Autodesk.Revit.UI; + +namespace Test; + +[Transaction(TransactionMode.Manual)] +public class DetachAndSaveCommand : IExternalCommand +{ + public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) + { + var app = commandData.Application.Application; + var uiapp = new UIApplication(app); + var uidoc = uiapp.ActiveUIDocument; + var doc = uidoc.Document; + string dir = @"D:\\Development\\Revit\\Project\\F7H-M"; + var listFiles = Directory.GetFiles(dir, "*.rvt"); + foreach (var file in listFiles) + { + DetachAndSaveAs(app, file); + } + return Result.Succeeded; + } + + public void DetachAndSaveAs(Application app,string path) + { + OpenOptions options = new OpenOptions + { + DetachFromCentralOption = DetachFromCentralOption.ClearTransmittedSaveAsNewCentral, + AllowOpeningLocalByWrongUser = true, + IgnoreExtensibleStorageSchemaConflict = true, + Audit = false, + }; + app.FailuresProcessing += (sender, args) => + { + var failuresAccessor = args.GetFailuresAccessor(); + var failures = failuresAccessor.GetFailureMessages(); + foreach (var failure in failures) + { + if (failure.GetSeverity() == FailureSeverity.Warning) + { + failuresAccessor.DeleteWarning(failure); + } + else + { + failuresAccessor.ResolveFailure(failure); + } + } + args.SetProcessingResult(FailureProcessingResult.Continue); + }; + var modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(path); + var detachedDoc = app.OpenDocumentFile(modelPath, options); + // resolve warnings with detachedDoc + + // if same version with app, continue + if (detachedDoc.Application.VersionNumber == app.VersionNumber) + { + detachedDoc.Close(false); + return; + } + var saveOptions = new SaveAsOptions + { + OverwriteExistingFile = true, + MaximumBackups = 1, + }; + string folder = Path.GetDirectoryName(path); + string fileName = Path.GetFileName(path); + // save over the same file + detachedDoc.SaveAs(Path.Combine(folder, fileName), saveOptions); + detachedDoc.Close(false); + } +} \ No newline at end of file diff --git a/Test/Sample/UnloadAndSaveModelAsCloudCommand.cs b/Test/Sample/UnloadAndSaveModelAsCloudCommand.cs index f23353b..c93ab08 100644 --- a/Test/Sample/UnloadAndSaveModelAsCloudCommand.cs +++ b/Test/Sample/UnloadAndSaveModelAsCloudCommand.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; +using System.Threading; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; @@ -13,9 +15,9 @@ public class UnloadAndSaveModelAsCloudCommand : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { - UIApplication uiApp = commandData.Application; - uiApp.Idling += ApplicationIdling; - string dir = @"D:\_WIP\Download\04-Mechanical_selected_2024-10-21_08-46-43am"; + //UIApplication uiApp = commandData.Application; + //uiApp.Idling += ApplicationIdling; + string dir = @"D:\Development\Revit\Project\F7H-M"; Guid accountId = new Guid("1715cf2b-cc12-46fd-9279-11bbc47e72f6"); Guid projectId = new Guid("58450c0d-394f-41b2-a7e6-7aa53665dfb8"); string folderId = "urn:adsk.wipprod:fs.folder:co.qslf4shtRpOHsZLUmwANiQ"; @@ -25,6 +27,7 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme foreach (string revitPath in revitPaths) { UnloadRevitLinks(revitPath); + continue; string fileName = System.IO.Path.GetFileNameWithoutExtension(revitPath); Document? doc = OpenDocument(commandData.Application.Application, revitPath, report); @@ -32,28 +35,18 @@ public Result Execute(ExternalCommandData commandData, ref string message, Eleme { continue; } - // Start transaction and use custom failure preprocessor - using Transaction transaction = new Transaction(doc, "Save As Cloud"); - transaction.Start(); - - // Set the failure handler to suppress warnings - FailureHandlingOptions failureOptions = transaction.GetFailureHandlingOptions(); - failureOptions.SetFailuresPreprocessor(new IgnoreFailuresPreprocessor()); - transaction.SetFailureHandlingOptions(failureOptions); - try { doc.SaveAsCloudModel(accountId, projectId, folderId, fileName); + // sleep for 5 seconds to allow the cloud model to be created + Thread.Sleep(5000); + doc.Close(false); } catch (Exception e) { - // Handle the error appropriately - report.Add($"Error saving cloud model: {e.Message}"); + Trace.WriteLine(e.Message); } - - transaction.Commit(); } - TaskDialog.Show("Done", "Process complete."); return Result.Succeeded; } @@ -96,7 +89,6 @@ private static string UnloadRevitLinks(string path) { ModelPath mPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(path); TransmissionData tData = TransmissionData.ReadTransmissionData(mPath); - if (tData == null) { return path;