Prevent Double Clicking in Windows to Open Project Files

Prevent Double Clicking in Windows to Open Project Files

smichaudN26PG
Enthusiast Enthusiast
1,308 Views
8 Replies
Message 1 of 9

Prevent Double Clicking in Windows to Open Project Files

smichaudN26PG
Enthusiast
Enthusiast

I'm trying to help our BIM Support Staff who deal with tickets about issues rooted in users opening Revit Project Files with poor practices.

 

Examples:

  • Double Clicking a Local or Central model from windows file explorer causing either file opening with the wrong arguments or unintended version upgrades. 
  • Opening C:\Revit Projects instead of opening a new local copy from the central model on the network drive.

My first thought was to see if I could use the process cmd line inputs to see if the DocumentOpeningEvent was triggered by a file explorer double click. This unfortunately doesn't work as /dde is triggered on all opening events via the Revit UI. That being said the DocumentOpeningEvent Cancel work well to stop the documents form upgrading and or opening. So my guess is using the Event Handler for the event is the right thought. I just am not full aware of how I might be able to identify how the opening information and program startup is occurring. Curious if anyone has gone down this road or has any ideas on how this might be possible.  

 public Result OnStartup(UIControlledApplication a)
        {

            a.ControlledApplication.DocumentOpening += new EventHandler<DocumentOpeningEventArgs>(FileExplorerLaunchLockOut);
             return Result.Succeeded;
        }
 
 private void FileExplorerLaunchLockOut(object sender, DocumentOpeningEventArgs arga)
        {
            
            Process process = Process.GetCurrentProcess();
            try
            {
                using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
                using (ManagementObjectCollection objects = searcher.Get())
                { cmdline = objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString(); }
            }
            catch { }
            bool a2 = cmdline.Contains("/dde");
         
            if (a2 == true)
            {
                arga.Cancel();
                TaskDialog.Show("Invald Opening Document Method", "Document File for Revit Should not be opened via Windows Explorer/UI methods\nAKA: Double Clicking!!!!\nOpen Command Cancelled\nUse Reivt User Interface to open desired file");
            }
		}
                // SendKeys.Send("^D");
                //Process.GetCurrentProcess().CloseMainWindow();
            }

 

0 Likes
Accepted solutions (1)
1,309 Views
8 Replies
Replies (8)
Message 2 of 9

jeremy_tammik
Alumni
Alumni

I am pretty sure that Revit itself cannot differentiate how it has been launched by the Windows OS, so there is little chance of you being able to address this in your add-in.

 

I think your best chance to address this will be via the pure native Windows API, if it is possible at all. It probably is. Maybe you can implement some kind of Windows or an Explorer extension that detects a double click on RVT and RFA files.

  

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

smichaudN26PG
Enthusiast
Enthusiast

Thanks for the response, the more I think on it, windows & explorer are probably the best way to handle it then if Revit has no idea how the files are opening. I've see stuff similar in the past.  Where right clicking an .rvt or .rfa gives a "Revit Properties" dialog with an "Open With" drop down to select the Revit Year to open the file with. 

 

 

 

0 Likes
Message 4 of 9

RPTHOMAS108
Mentor
Mentor
Accepted solution

Via command bindings you can check for various sanctioned opening methods and then allow the continuation when document opening handler is encountered.

 

The below code will prevent:

Opening via double click

Opening via file drop onto Revit

 

It will also prevent opening of documents on the home page which isn't desirable but there is no fathomable command id for that (so can't be checked). This test was conducted with Revit open sometimes the double click doesn't lead to the expected version of Revit opening.

 

Private Shared IntOpenWasViaCommand As Boolean = False
Private Sub DocOpening(s As Application, e As DocumentOpeningEventArgs)
	If IntOpenWasViaCommand = False Then
		e.Cancel()
	End If
	IntOpenWasViaCommand = False
End Sub
Private Sub ProjectOpenBeforeExecute(s As UIApplication, e As BeforeExecutedEventArgs)
	IntOpenWasViaCommand = True
End Sub

Public Function OnStartup(ByVal application As UIControlledApplication) As Result Implements IExternalApplication.OnStartup

	AddHandler application.ControlledApplication.DocumentOpening, AddressOf DocOpening

	Dim Bx As AddInCommandBinding = application.CreateAddInCommandBinding(
		RevitCommandId.LookupPostableCommandId(PostableCommand.OpenRevitFile))
	Dim By As AddInCommandBinding = application.CreateAddInCommandBinding(
		RevitCommandId.LookupPostableCommandId(PostableCommand.OpenProject))
	Dim Bz As AddInCommandBinding = application.CreateAddInCommandBinding(
		RevitCommandId.LookupPostableCommandId(PostableCommand.OpenFamily))
	'Most recent user files list or the files of Mr U perhaps.
	Dim Bu As AddInCommandBinding = application.CreateAddInCommandBinding(
		RevitCommandId.LookupCommandId("ID_FILE_MRU_FILE5"))

	AddHandler Bx.BeforeExecuted, AddressOf ProjectOpenBeforeExecute
	AddHandler By.BeforeExecuted, AddressOf ProjectOpenBeforeExecute
	AddHandler Bz.BeforeExecuted, AddressOf ProjectOpenBeforeExecute
	AddHandler Bu.BeforeExecuted, AddressOf ProjectOpenBeforeExecute

	Return Autodesk.Revit.UI.Result.Succeeded
End Function

 

 

 

 

Message 5 of 9

jeremy_tammik
Alumni
Alumni

Thank you for the great suggestion, Richard!

 

That looks very clever to me, using the combination of the cancellable DocumentOpening event and BeforeExecuted event handlers on individual flavours of the open command, using them to set a flag, and detecting the double click event by the absence and non-execution of the other open commands. Super cool. 

 

So, my statement above was completely false. 

  

Revit can and does differentiate how it was launched, and the add-in can determine that by using an appropriate and clever combination of event handlers.

  

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

smichaudN26PG
Enthusiast
Enthusiast

Thanks Richard, 

 

I gave this a shot last night in my C# add-in and had some questions.

  • I'm not seeing PostableCommand.OpenProject. Is this a item that was removed from the API? I looked at 2017-22 and am working in 21'
  • I'm unclear if "ID_FILE_MRU_FILES" is actual grabbing the most recently used models from the Model Browser. 
  • I found that the loop setup caused every other open command to fail even if it was an approved method. I'm going to chat diagram on my white board what's happening to try to understand why. 

Thanks for your help with this topic!!! I fell like just asking help launch me much further in this goal

0 Likes
Message 7 of 9

RPTHOMAS108
Mentor
Mentor

Interesting

 

I tested with 2022 so  all postable commands are present in that. They may have differing names in earlier versions You can check the journal and may be able to find them.

 

ID_FILE_MRU_FILES was a journal entry that is added after selecting a file from the most recent files list as below. Clicking on the tiles in the model browser seemed to raise a non-API event so code posted could not detect those.

 

210916a.png

 

Code seemed to work ok in the original VB.Net version. Note that I reset the IntOpenWasViaCommand to false at the end of the document opening handler. So in theory the state should be reset to false no matter how the document was opened i.e. the default situation should be that the open is cancelled. If it is opening I can think of only three logical explanations:

1) The IntOpenWasViaCommand was left as true after last open (check position within handler)

2) e.Cancel had no effect.

3) The Document Opening handler wasn't raised.

 

I don't think 2 and 3 are possible but testing may prove otherwise.

 

I didn't test dropping multiple files but I assume again since the default is not to open that it would still prevent it.

 

By 'tiles in model browser' I mean the below. In the original version open was erroneously prevented from these but I couldn't change that since there is no API way to detect them being clicked.

 

210916b.PNG

 

 

Message 8 of 9

smichaudN26PG
Enthusiast
Enthusiast

Okay so I did not place a return to false at the end of OnStarup, I defined it and then did not reset it to be in a state that made sense at the end of startup. Appears to now operate correctly in that regard.  Basically it was flip flopping the default state due to my error in implementing it in C#. My bad. 

As for the tile in the model browser bit, I snoop the yeah I'm not seeing an Revit API event. I decided to use a WPF snooping tool to see what's going on and it appears to be just a Container within the RevitHomeBrowser - Model Browser Container is what's being tripped when a "tile" is pressed. 

 

I'm somewhere between calling this good enough vs trying to find a way to allow the "Tiles" (Model Browser Container) to function as well during for a Document Opening event. 

 

snoopTiles.png

 

 

0 Likes
Message 9 of 9

mhannonQ65N2
Collaborator
Collaborator

I'd recommend tackling this from the Windows side by changing what program opens .rfa and .rvt files. These settings are stored in the windows registry and once you figure out what you want to set them to you could write a script or program that automatically changes them.