Revit 2015 exception with Autodesk.Revit.DB.CustomExporter

Revit 2015 exception with Autodesk.Revit.DB.CustomExporter

pfk
Enthusiast Enthusiast
2,854 Views
13 Replies
Message 1 of 14

Revit 2015 exception with Autodesk.Revit.DB.CustomExporter

pfk
Enthusiast
Enthusiast

My plugin is causing an exception with Revit 2015 on the following line

 

CustomExporter exporter = new CustomExporter(document, m_ExportContext);

 

The above code works fine with Revit 2014. 

 

The exception raised is:

 

An unhandled exception of type 'Autodesk.Revit.Exceptions.InternalException' occurred in RevitAPI.dll

Additional information: Failed to register a managed object for the currently active external application. A possible cause may be an inactive external application (not being invoked by Revit at present) attempting to assess the Revit API from a modeless dialog or another outside thread.

 

Has something changed in Revit 2015?  The modeless dialog calling the above code is not "inactive".  Does anyone know how I can get around this issue pls?

 

Thanks

 

Paul

0 Likes
2,855 Views
13 Replies
Replies (13)
Message 2 of 14

arnostlobel
Alumni
Alumni

Hello Paul!

 

Although it is a bit odd that the exception is an Internal exception, the reported details suggest what could be wrong. It is indeed illegal making out-of-context calls to Revit API from a modeless dialog. It has always been illegal, but Revit does not always enforces that in all possible situations. More and more checks are added to the API in each release to enforce this rule against illegal invocations of the Revit API. I believe this particular case is one of those that had been addressed in R2015.
 
What I mean by "illegal" is the case when some add-in makes calls to the Revit API at times that is not within a regular and standard API context, that is a process started by Revit, such as an external command, dynamic updater, event, call-back, etc. This is the case, for example, of a modeless dialog invoking the API at any given time (i.e. outside of the contexts mentioned above). That has been always illegal and unsupported in Revit, for Revit does not support external access from multiple threads. There are only two legitimate situations in which a modeless dialog can communicate with Revit, and that is 1) during an Idling event, and 2) via an External event. Both cases are well documented and sampled in the SDK. (Look for Modeless dialog.)
 
So, in your particular case of trying to register a Custom Exporter, this is what is happening: Your app implements an Export Context and instantiates it. Then it tries to invoke a custom export with the context object as an argument. Revit needs to associate the given object with the currently active (i.e. being invoked by Revit) application. However, since the call to export was called from an independent modeless dialog, there is no active application available, hence the exception.  
 
I hope I’ve explained it satisfactorily.
 
Arnošt Löbel
Sr. Principal Engineer
Autodesk, Revit R&D 
Arnošt Löbel
0 Likes
Message 4 of 14

pfk
Enthusiast
Enthusiast

Thanks for the detailed responses.

 

I have moved all the code to run via an IExternalEvent, which resolves the above problem.  However I now have an issue where Revit is crashing when the plugin is obtaining a list of linked documents (which is occurring in the IExportContext.Finish() method.

I am using some code I think Jeremy posted some time ago to do this.  The code is:

        public static IEnumerable<Document> GetLinkedDocuments( Document _document )
        {
            var linkedfiles = GetLinkedFileReferences( _document );
            var linkedFileNames = linkedfiles.Select( x => ModelPathUtils.ConvertModelPathToUserVisiblePath( x.GetAbsolutePath() ) ).ToList();
            return _document.Application.Documents.Cast<Document>().Where( doc => linkedFileNames.Any( fileName => doc.PathName.Equals( fileName ) ) );
        }

The crash occurs on the return statement line.  This function is working fine on Revit 2014.

The specific exception leading to the crash is:

An unhandled exception of type 'System.AccessViolationException' occurred in RevitAPI.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

 

Thanks again for your quick and detailed responses.  Much appreciated.

 

Paul

0 Likes
Message 5 of 14

arnostlobel
Alumni
Alumni
I recommend converting the LINQ statements into more manageable, albeit longer C# code in order to find which particular file from the collection of linked files gives you trouble. I assume the crash occurs on the last line of your snippet, correct?

Arnošt Löbel
Arnošt Löbel
0 Likes
Message 6 of 14

pfk
Enthusiast
Enthusiast

I have spent a few more days on this and am now at a dead-end.  It appears to me that there is a bug in Revit 2015, such that referening linked files is crashing Revit.

 

For example, load a revit scene which includes linked content, then the CustomeExport.Export function crashes Revit immediately after the Start method is called, and prior any other callback methods being called.

 

In Revit 2014 this all runs fine.

 

This looks like a critical bug in Revit 2015,

 

Paul

0 Likes
Message 7 of 14

arnostlobel
Alumni
Alumni

Paul:

 

So, it looks like this is a different issue than as originally posted, correct? Should I assume that once you re-implemented your custom exporter using the External Event mechanism the exporting itself does not give you the originally mentioned exception anymore? (Instead, now you see a crash when accessing linked files.)
 
If so, I suggest you start a new thread (maybe something like – Cannot access linked files via the API). In there, please post a snippet of the code that crashes in your app. If it can be a version less compact than what you posted originally it would help a lot, for then we could focus on one single line of code at a time. Also, please describe whether it is any linked file whatsoever that gives you this problem, or only some files. If the latter is the case, please describe what is different about those files.
 
Posting the code snippet in as simple a form as possible (one simple command per line, no LINQ, no special language features if possible) is very helpful for us to better understand what may be going on. I am a bit intrigued by you experimenting “System.AccessViolationException” exception, for I believe Revit does not normally causes that exception from its internal code, because most of Revit is native and if there is an access violation it would propagate differently. It is, however (to my best knowledge) possible such a managed exception coming from Revit Server, since major parts of that framework are managed. Which leads me to a question: Are the linked files you try to access local or are there on a server?
 
Thank you
 
Arnošt Löbel
Sr. Principal Engineer
Autodesk, Revit R&D
Arnošt Löbel
0 Likes
Message 8 of 14

arnostlobel
Alumni
Alumni
One more thing: I will try to play with it too to see if I can reproduce it. Paul, you stated Export crashes just after the Start method was invoked. Do you have anything in the Start method or is is empty? Do you try to access the linked documents from there?

Thank you

Arnošt Löbel
Sr. Principal Engineer
Autodesk, Revit R&D
Arnošt Löbel
0 Likes
Message 9 of 14

pfk
Enthusiast
Enthusiast
My start method is....

public bool Start()
{
Debug.WriteLine("Start");
return true;
}

I will try to build a sample plugin which reproduces the problem when I get
a chance over the next few days, and post on a new thread.

Paul
0 Likes
Message 10 of 14

arnostlobel
Alumni
Alumni
Interesting. I'll try the same.
And you said that the linked files are regular local Revit documents stored on your hardrive?

Arnošt
Arnošt Löbel
0 Likes
Message 11 of 14

arnostlobel
Alumni
Alumni

Paul,

 

I have run a couple of tests exporting  documents with links and must report that it all went smoothly without a hiccup. I tried it in two different builds: my current local build (R2016 in progress) and R2015. In both cases I used my own sample application for testing custom export, each built for the respective Revit target. The sample application exports a selected 3D view and saves all incoming data in tidy XML format. It does not do anything else though. (Jeremy Tammik had posted a version of my sample on his blog a time ago – search for Custom Export if you are interested.) The export is invoked as an external command (to keep is simple). 

 

The documents I tested the export with were trivial. A master document with nothing but three walls and a linked document with also three walls and a window on one of the walls. I used different materials for the walls, but that’s about it. Both the master and linked files were saved locally in one folder on my hard drive.

 

So, exporting itself seems to be working for master and linked geometry. Thus I suspect the problem you are experiencing may be caused by something in your linked files. Therefore I suggest you put aside the export testing code for a while and spend some time with investigation of the linked files. A few posts back you showed a snippet of code in which you iterated through a set of linked files; you reported that the code would crash for you. That is of course very interesting and in my opinion it deservers looking into.

 

You also mentioned once that you played with External Events. I am not sure how it is all related together with the export and with linked files, but I suggest you start with as simple version of your application as possible. If you suspect the problem is with the export, remove everything else and try a simple export as an external command (no Iding, no External Event). Try to export a simple document first. If it works, add a simple link file and export again. If it works, go on with adding complexity.

 

Thank you

 

Arnošt Löbel

Autodesk, Revit R&D

Arnošt Löbel
0 Likes
Message 12 of 14

pfk
Enthusiast
Enthusiast

New thread with code posted at http://forums.autodesk.com/t5/Revit-API/Revit-2015-InternalException-on-CustomExporter-with-Linked-F....

 

Forgot to mention - crash doesn't occur if it's not in a separate thread.

 

Paul

0 Likes
Message 13 of 14

pfk
Enthusiast
Enthusiast

Additional info....The linked files are trivial (a single wall in a scene in the same folder as the host scene).  At a guess - I would say you are unable to reproduce the issue because you are doing it in the main UI thread.

 

Paul

0 Likes
Message 14 of 14

arnostlobel
Alumni
Alumni

Paul,

 

This is important: What do you mean by separate thread? We (in Revit) do not allow the API to be accessed from other that the main thread, thus there may not be a separate thread involved. Can you please explain what you meant?

 

Thanks

 

Arnošt Löbel

Sr. Principal Engineer

Autodesk, Revit R&A

Arnošt Löbel
0 Likes