Skip to content

Commit

Permalink
detach save model
Browse files Browse the repository at this point in the history
  • Loading branch information
chuongmep committed Oct 30, 2024
1 parent 584fe04 commit 785f28e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 18 deletions.
74 changes: 74 additions & 0 deletions Test/Sample/DeteachAndSaveCommand.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
28 changes: 10 additions & 18 deletions Test/Sample/UnloadAndSaveModelAsCloudCommand.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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";
Expand All @@ -25,35 +27,26 @@ 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);
if (doc == null)
{
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;
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 785f28e

Please sign in to comment.