Hi I am calling my .Net dll via RhinoPython.
In my dll I am refering to the Rhino Document via Rhino.RhinoDoc.ActiveDoc this works only as long as I am in the same file. When I open a new file this refrence breaks. how can I fix this?
If you store the reference to the active document somewhere in a variable then that reference will be outdated once a new document is loaded.
I think it is better not to use ActiveDoc (apparently it will not work in Rhino on the Mac). An alternative is to subscribe to the RhinoDoc.EndOpenDocument event and renew the reference to the document when the event is fired.
In C# code you would do this (sorry, I don’t know how to do this in Python, but I hope you can understand the meaning of the code).
// variable to store active document
RhinoDoc _currentDocument;
{
// write this where you activate the code:
// subscribe to the event
RhinoDoc.EndOpenDocument += OnEndOpenDocument;
}
void OnEnDopenDocument(object sender, DocumentOpenEventArgs args)
{
_currentDocument = args.Document;
}
Now each time as a document is opened, the _currentDocument variable will have a reference to the loaded document.
thanks for your advice, Menno. I wrote this in F#:
module RhinoScriptSyntax =
open Rhino
let mutable Doc = RhinoDoc.ActiveDoc
let updateDoc (args:DocumentOpenEventArgs) =
Doc <- args.Document
RhinoDoc.EndOpenDocument.Add updateDoc
Why don’t you just have the document as an argument to your functions? That way you know you are using the correct document at the time that the function is being executed.
Don’t use ActiveDoc if you don’t have to.
yes I could do that too. Is the use of ActiveDoc only problematic on a mac ?
No - when you load a new document on Windows, your ActiveDoc pointers will go stale. It’s just much worse on the Mac, because the active document can change any time.
The simple answer is - never store pointers to documents. A slightly better approach - if you really have to store a reference to a specific document at run time - is to use document run-time serial numbers.
“Under the hood” in RhinoCommon, the document runtime serial number is what is actually stored in the RhinoDoc class which means that if you store a document in Rhino, it will be using the mechanism that Andy is describing. The problem is that with Mac there are potentially many documents and it is very difficult to figure out which one is “active”. Using the document that is passed to you in your RunCommand function is the recommended approach to working with documents.