BIM World
A Professional BIM Learning Platform


Revit Secondary Development: Managing Project File Separation

When rushing to meet project deadlines, I found importing drawings and separating models to be overly complicated. Handling a large number of drawings was not only tedious but also prone to errors. To address this, I developed a solution that integrates importing CAD and PDF files and model separation into a single batch export process, automatically saving files in their respective directories.

While working on the model separation, I encountered some API limitations. For example, the project cannot be separated directly from the current document. The current document must first be closed before separation by reopening it. However, the API does not support closing the current document programmatically, nor does it provide a way to switch between documents directly.

My workaround was to create a temporary project, open it as the active document using UIApplication, perform the separation, and then delete the temporary project once complete. This approach feels cumbersome, and I am unsure if improvements have been made to the API since 2016.

Here is the key code snippet:

UIApplication uiapp = commandData.Application;
Document doc = uiapp.ActiveUIDocument.Document;
String filePath = @"C:UsersimfourDesktopNew Folder (2)New Folder (2)" + doc.Title;

// Check if worksharing is enabled
if (doc.IsWorkshared)
{
    // Synchronize with central model
    doc.SynchronizeWithCentral(new TransactWithCentralOptions(), new SynchronizeWithCentralOptions());

    // Create a temporary document and set it as the active document
    Document temDoc = uiapp.Application.NewProjectDocument(@"C:ProgramDataAutodeskRVT 2016TemplatesChinaConstruction-DefaultCHSCHS.rte");
    string temFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"temFile.rvt";
    temDoc.SaveAs(temFilePath);
    temDoc.Close(false);
    uiapp.OpenAndActivateDocument(temFilePath);
    temDoc = uiapp.ActiveUIDocument.Document;

    // Close local files
    string docPathName = doc.PathName;
    ModelPath modelPath = doc.GetWorksharingCentralModelPath();
    doc.Close(false);

    // Separate model with specific open options
    OpenOptions openOptions = new OpenOptions
    {
        DetachFromCentralOption = DetachFromCentralOption.DetachAndDiscardWorksets
    };
    Document detachDoc = uiapp.Application.OpenDocumentFile(modelPath, openOptions);
    detachDoc.SaveAs(filePath);
    detachDoc.Close(false);

    // Reopen the original local file
    uiapp.OpenAndActivateDocument(docPathName);

    // Clean up temporary document
    temDoc.Close(false);
    File.Delete(temFilePath);
}
else
{
    // Save project and copy to new path
    doc.Save();
    File.Copy(doc.PathName, filePath);
}
xuebim
Follow the latest BIM developments in the architecture industry, explore innovative building technologies, and discover cutting-edge industry insights.
← Scan with WeChat
Like(0) 打赏
BIM WORLD » Revit Secondary Development: Managing Project File Separation

Comment Get first!

Must log in before commenting!

 

BIM World, A Professional BIM Learning Platform

Stay updated on the latest architecture trends and share new building technologies.

Contact UsAbout Us

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫

微信扫一扫

Account Login

By signing in, you agree toUser Agreement

Sign Up