How to intercept OnOpenDocument and block the open command

How to intercept OnOpenDocument and block the open command

massimomgrs13
Contributor Contributor
694 Views
11 Replies
Message 1 of 12

How to intercept OnOpenDocument and block the open command

massimomgrs13
Contributor
Contributor

Hello,

 

I need to prevent some files to be opened, if fullpath match a pattern. Like "*ToolBox*"

 

I tried to intercept the OnOpenDocument 

and set HandlingCode = HandlingCodeEnum.kEventCanceled;

 

But Inventor still open the file.

 

public void AppEvent_OnOpenDocument_Handler(_Document DocumentObject, string FullDocumentName, EventTimingEnum BeforeOrAfter, NameValueMap Context, out HandlingCodeEnum HandlingCode)
{
Debug.Print("OnOpenDocument");
HandlingCode = HandlingCodeEnum.kEventCanceled;

 

How is it possible to avoid the open of file,

for example if the path match a pattern?

 

Thanks for helping.

regards

Massimo

0 Likes
Accepted solutions (1)
695 Views
11 Replies
Replies (11)
Message 2 of 12

WCrihfield
Mentor
Mentor

Hi @massimomgrs13.  The Inventor API online help documentations for the ApplicationEvents.OnOpenDocument says that the 'HandlingCode' is ignored.  That means it will not let us set the HandlingCodeEnum to kEventCanceled, because it will be ignored.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 12

massimomgrs13
Contributor
Contributor

Hi Wesley, maybe there is some other way to do that?  For example, attach to the open command of UI

and open a custom OpenFile dialog?

Thanks. regards Massimo

0 Likes
Message 4 of 12

WCrihfield
Mentor
Mentor
Accepted solution

Hi @massimomgrs13.  If you are only concerned with when someone attempts to open specific files using the normal user interface Open dialog, then there may be another way, using the FileUIEvents.OnFileOpenDialog Event.  However, this event will only be triggered by this specific dialog, and will not get triggered by any other possible ways those files might possibly get opened.  This event can either be 'handled' (forced it to do something other than default/standard behavior), or canceled, and can offer potentially a lot of information.  There is also the similar FileUIEvents.OnFileOpenFromMRU Event, for when attempting to open a file that way, instead of through the Open dialog.  Neither of those events will get triggered if you open a file by code though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 12

massimomgrs13
Contributor
Contributor

Hi @WCrihfield thanks. You are right, what I need is to intercept ALL open events and decide if drop the event and do nothing if some conditions are met. But I did not found a valid solution until now. Thanks for the help. regards Massimo

0 Likes
Message 6 of 12

jjstr8
Collaborator
Collaborator
You can close the document in your OnOpenDocument handler. You'll have to wait for the kAfter event timing, which means the document would appear briefly in Inventor.
0 Likes
Message 7 of 12

massimomgrs13
Contributor
Contributor
Hello @jjstr8, thanks. But I need to discard the operation ASAP. Otherwise file will be loaded and if it's an assembly it takes time, to open and then close it. I will keep searching a way to do it. Thanks, regards
0 Likes
Message 8 of 12

jjstr8
Collaborator
Collaborator
What's the reason for not allowing certain files to be opened?
0 Likes
Message 9 of 12

WCrihfield
Mentor
Mentor

Unfortunately, the ApplicationEvents.OnInitializeDocument Event also ignores its HandlingCodeEnum, so that one can not be canceled either.  Initialization happens automatically when any assembly or drawing is opened that references any other documents...those referenced documents are initialized (partially loaded, but not fully opened yet).

 

Another area of thought comes to mind, but I am not sure how you would be able to use it.  There is a ApplicationEvents.OnUndoOpenDocument Event, so there must be a way to 'abort' opening a document, but it sounds like it has to do with Transactions.  I have use Transactions in my code for years, but only utilizing the basics like StartTransactionAbort, and End.  There is also StartTransactionForDocumentOpen, but it seems like this one has gone mostly unexplored by most.  Multiple other users have inquired about the proper way to use it on this forum, but I suspect it is getting in 'too deep' for most users needs/interest.  Then the Transaction object has the IdentifyForDocumentOpen method.  However, although those things are interesting to think about in this situation, it seems to me like you may need to have established/started the Transaction before the document open process ever started, so that you had total control over it to abort that specific transaction, without undoing any other actions that may have happened during the same transaction.  Plus, it looks like these transactions can have both parent and child transactions, so it sounds relatively complicated to get involved with.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 10 of 12

massimomgrs13
Contributor
Contributor

Hello,

for example a pdm application mist prevent user from opening a file.

 

or for limit external offices access to files

based on tule

 

thanks regards Maaaimo

0 Likes
Message 11 of 12

massimomgrs13
Contributor
Contributor
Hello,
For example pdm addin or limit external office from opening files they do not have access. With other cad software it’s possible. But for sure also inventor must have this possibility. Regards
0 Likes
Message 12 of 12

jjstr8
Collaborator
Collaborator

The only thing I could come up with is the following:

  • In OnOpenDocument (kBefore timing) check if you want to restrict access.
  • If so, open a FileStream to the file.  Use FileMode.Open, FileAccess.Read, FileShare.None for parameters.  This will lock all access to the file.  Your FileStream object should be a field in your addin class (or wherever your OnOpenDocument is handled) so you can access it through multiple firings of OnOpenDocument.
  • Look for the OnOpenDocument kAbort timing event that matches that file, and then close and dispose of your FileStream object at that point.  I would set it to null after disposing.  If the file is in an assembly, it looks like you'll get multiple open attempts before the abort, so you'll need to null check your FileStream object.