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);
}













Must log in before commenting!
Sign Up