Exception Message: Switching active documents is not allowed during API event handling.

Exception Message: Switching active documents is not allowed during API event handling.

archana.sapkal
Participant Participant
804 Views
4 Replies
Message 1 of 5

Exception Message: Switching active documents is not allowed during API event handling.

archana.sapkal
Participant
Participant

Exception Message: Switching active documents is not allowed during API event handling. 

foreach (var filePath in filePaths)
{
try
{
UIDocument uiDocument = uiApp.OpenAndActivateDocument(filePath);
Document currentDoc = uiApp.ActiveUIDocument.Document;

if (previousDoc != null && previousDoc.IsValidObject)
{
// previousDoc.Close(false);
previousDoc = null;
}
RunExportProcess(currentDoc);
previousDoc = currentDoc;
}
catch (Exception ex)
{
MessageBox.Show(ex + "");
LogException(ex, $"Error processing file {filePath}");
}

Here How can load multiple revit models in revit?

0 Likes
805 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

You can easily load as many documents into Revit as you like (within reason, of course) by doing so when Revit is in an idle state and ready to accept new requests. This is not the case during API event handling.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 5

archana.sapkal
Participant
Participant

private void OnIdling(object sender, IdlingEventArgs e)
{
_cachedUiCtrApp.Idling -= OnIdling;
UIApplication uiApp = sender as UIApplication;
try
{
var filename = siemensRevitFIleListLogFilePath;
if (System.IO.File.Exists(filename))
{
string logContent = System.IO.File.ReadAllText(filename);
System.IO.File.WriteAllText(filename, string.Empty);

string[] logLines = logContent.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
ProcessDocuments(uiApp, logLines);
}
}
catch (Exception ex)
{
LogException(ex, "Error in OnIdling method");
}
}

private void ProcessDocuments(UIApplication uiApp, string[] filePaths)
{
foreach (var filePath in filePaths)
{
Document previousDoc = null;
try
{
UIDocument uiDocument = uiApp.OpenAndActivateDocument(filePath);
Document currentDoc = uiApp.ActiveUIDocument.Document;
if (currentDoc != null && currentDoc.IsValidObject)
{
RunExportProcess(currentDoc);
}
}
catch (Exception ex)
{
MessageBox.Show(ex + "");
LogException(ex, $"Error processing file {filePath}");
}
}

 

 

In this code not loading second Revit document. for first document it complete work and for next document throw exception Switching active documents is not allowed during API event handling. how can solve this?

 

0 Likes
Message 4 of 5

ricaun
Advisor
Advisor

The correct way would be not using Idling event and use ExternalEvent instead.

 

https://help.autodesk.com/view/RVT/2024/ENU/?guid=Revit_API_Revit_API_Developers_Guide_Advanced_Topi...

 

ExternalEvent will basically run when Revit is in idle mode and gonna run only one time, similar what you are doing with the Idling.

 

ExternalEvent is the best choice, and you should never run large processes in the Idling event.

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 5 of 5

archana.sapkal
Participant
Participant

for (int i = 0; i < logLines.Length; i++)
{
string docPath = logLines[i];
if (i == 0)
{
try
{
uiDocument = uiApp.OpenAndActivateDocument(docPath);
currentDoc = uiApp.ActiveUIDocument.Document;
RunExportProcess(currentDoc);
MessageBox.Show("Export process finished");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
try
{
MessageBox.Show("Document opened");
uiDocument.Dispose();
uiDocument = null;
Document doc2 = uiApp.Application.OpenDocumentFile(docPath);
RunExportProcess(doc2);
MessageBox.Show("Export process finished");
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
finally
{
if (uiDocument != null)
{
uiDocument.Dispose();
}
}
}

 

How to set doc2 as active document using Revit API.

0 Likes