Unspecified error occured on document.Close() method

Unspecified error occured on document.Close() method

karolis.s
Advocate Advocate
253 Views
3 Replies
Message 1 of 4

Unspecified error occured on document.Close() method

karolis.s
Advocate
Advocate

Hello.

I am having an issue with opening and closing files in Autodesk Inventor. 

The task is to update iProperties on currently edited part, but to do that I must open it, get specific data from view cube orientation and according to that data update iProperties.

Attached is an image of workspace and error message. I think the problem is that I need to do that from an assembly document when I am returning to it (from activated part).

I wanted to do that with onEnvironmentChange event, but unfortunately I wasn't successful. Any ideas or workarounds please?

  private void UserInterfaceEvents_OnEnvironmentChange(Inventor.Environment environment, EnvironmentStateEnum environmentState, EventTimingEnum beforeOrAfter, NameValueMap context, out HandlingCodeEnum handlingCode)
        {
            try
            {
                string envInternalName = environment.InternalName;

                // Check if we are in the part environment
                if (envInternalName == "PMxPartEnvironment")
                {
                    // When resuming or activating the part environment, store the part document reference
                    if (environmentState == EnvironmentStateEnum.kActivateEnvironmentState || environmentState == EnvironmentStateEnum.kResumeEnvironmentState)
                    {
                        activeEditedDocument = m_inventorApp.ActiveEditDocument;
                    }

                    // When suspending or terminating the part environment, open and close the stored part document
                    if (environmentState == EnvironmentStateEnum.kTerminateEnvironmentState || environmentState == EnvironmentStateEnum.kSuspendEnvironmentState && activeEditedDocument != null)
                    {
                        // Open and immediately close the stored part document
                        // Add your logic here for opening and closing the document
                        // For example:                        
                        if (beforeOrAfter == EventTimingEnum.kBefore)
                        {
                            m_inventorApp.Documents.Open(activeEditedDocument.FullFileName, true);
                            activeEditedDocument.Close();
                        }
                        else if (beforeOrAfter == EventTimingEnum.kAfter)
                        {
                            m_inventorApp.Documents.Open(activeEditedDocument.FullFileName, true);
                            activeEditedDocument.Close();
                        }

                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }

            handlingCode = HandlingCodeEnum.kEventNotHandled;
        }



0 Likes
254 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @karolis.s.  Your first image shows that the error is happening when it tries to close a document, and the second image shows what looks like you being in 'edit mode' of a part type assembly component, while an assembly is actually the 'active' document.  When you enter and exit the 'edit mode' of that part type component while an assembly is 'active', that will change the environment, triggering event handler to run.  If that part is still being referenced by the assembly, you will not be able to truly close it.  If you open the document visibly, by specifying True for Visible when opening it, then using Close on that document would normally just turn its visibility back off, but it would remain open (or at least initiated) in the background, because of it still being actively referenced by the assembly.  Closing whatever document was the 'active edit document' every time the environment changes away from a part editing one does not sound like a good idea to me, but I do not know what your design intent is, so maybe with a bit of further development it would not be bad at all.

 

Edit:  Also, reviewing what a Document object is may help in areas like this.  Below is a link to the online help page for the basic Document Inventor API object.  It says that it represents something that is already 'in memory'.  Then, if you look at its Open property, which is a ReadOnly Boolean, you will see that the primary two possible states of a Document object are 'Initiated' and 'Open'.  And if just initiated, then accessing any of its properties will cause it to Open.  And again, open does not always mean visible on your screen, or that there will be a Document tab for it showing.  Any time you open either an assembly, or a drawing, that action also initializes all of their referenced documents.  You will see those numbers at the right end of the Inventor 'status bar' (usually at the bottom right of the screen).

https://help.autodesk.com/view/INVNTOR/2024/ENU/?guid=GUID-Document 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

karolis.s
Advocate
Advocate

Thank you for a detailed reply.

Unfortunately I have to open the document with the visibility property set as True. Link below provides the reason why I have to do that.

https://forums.autodesk.com/t5/inventor-programming-ilogic/retrieve-view-cube-directions-from-an-ass...

I am able to close the document from an assembly (visible) if it wasn't in 'edit mode'. So my command works fine in assembly environment without 'edit mode' and part environment. I am looking for a solution to make it work in when 'edit mode' is active.


0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor

OK.  So, from what I have gathered from that other forum topic, the code you have posted here is not complete yet, and you will be using this code above to run the code routine shown within that other forum post when this event it triggered, by running it between opening and closing the 'active edit document'?  It's a bit confusing, but OK.  But just so you know, if you have opened a document as not visible, then you can use Document.ReleaseReference instead of Document.Close, then at some point, it may also be good to use ThisApplication.Documents.CloseAll(True), where True specifies to only close all unreferenced documents being held in memory, as a clean-up tool.  Good luck.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)