Hi everyone, I hope someone can help me with the following issue:I wrote a simple C# addin program that I intend to run automatically, via the MS_DGNAPPS config variable, when Connect is started and a dgn loaded. This addin program just needs to submit a few key in commands such as DMSG CLOSEDIALOG -79 to close the element selector dialog box and db=fakeu/fakepw@fakealias to connect the dgn file to our database.
Since this addin is loaded automatically via MS_DGNAPPS, I first put the session keyin calls in the addin's run method, which is run after the addin is loaded. The key ins didn't seem to take even though there were no errors. The element selector was open and not closed after the dgn file was fully loaded.
I figured that maybe these keyins were being submitted too early in the process of loading the dgn. I knew the addin class has event handlers, so I just assumed I would be able to find one for when the dgn file is fully loaded and I could just add the key ins there. I checked the documentation but was surprised to find out that no such event handler exists, as far as I can tell.
I tried many other event handlers including CmdWindowOpenEventHandler, ModelChangedEvent, BeforeNewDesignFileEvent etc.. But the behavior was no different than when I had the keyins in the run method.
When I used the NewDesignFileEvent event, it showed me that my theory was correct: Unlike the other events that I tried, with this event the element selector is visible on the screen when this event is fired. I could then see that when my code runs the element selector dialog does disappear, but for some reason Connect then shows the element selector dialog after it leaves my event handler while it finishes loading the dgn.
Note as test, I put my key ins into the LevelChangeEvent eventhandler in the addin class and then manually changed the level after the dgn file was fully loaded and my key ins worked perfectly, so I know my code is good. The issue is just the point the the process that my code is running when Connect is loading a dgn that is the issue.
Questions:Am I missing something or is there an eventhandler for the addin class that fires when Connect fully loads a dgn file? This seems like such a basic requirement.
Is there possibly another way to automatically fire a key in command after Connect has fully loaded a dgn file? We do not want our users to have to manually key in commands.
Brenden OReilly said:Am I missing something or is there an eventhandler for the addin class that fires when Connect fully loads a dgn file? This seems like such a basic requirement.
You can simple just send a local command from your Addin's run funtion and place all your real actions in this local command function. Because our local command will be processed after MS fully started.
Brenden OReilly said:Is there possibly another way to automatically fire a key in command after Connect has fully loaded a dgn file? We do not want our users to have to manually key in commands.
microstation.exe -sCmdFile.txt can do this. Please refer my just answer for below post:
Running a keyin at startup
Hi Brenden,
in addition to Yongan's answer:
Brenden OReilly said:I checked the documentation but was surprised to find out that no such event handler exists, as far as I can tell.
Be aware NET API wraps C++ world, but not completely. So when you are interested in detail monitoring of MicroStation engine and related events, in some cases only C++ API provides the right tools. Also, in some situations more solutions / approaches exist.
Brenden OReilly said:This addin program just needs to submit a few key in commands
In my opinion there are two separate topics:
Brenden OReilly said:When I used the NewDesignFileEvent event
I guess this addin event wraps SystemFunc_NewDesignFile system hook. As described in C++ API help (which should always be checked): UserFunction called when a new design file is opened. So it is probably fired when "it happens", not when "it is finished".
I think two solutions can be tried:
To try Session.OnMasterFileStart event. As described in Session class documentation, it is fired when a file become "master file" and the session starts.
If it is not enough (e.g. OnMasterFileStart is still "too early event"), you can register for the same (or any other available, informing that file or model changed) and when it happens, to register for EnterIdleEvent. It is available in Interop (and in C++), but not in new NET API. It is fired when MicroStation processed everything in input queue, so when monitored after the model is opened, it can be interpreted "MicroStation finished the loading process". Of course, application must unregister immediately and wait for next file/model open event.
With regards,
Jan
Bentley Accredited Developer: iTwin Platform - AssociateLabyrinth Technology | dev.notes() | cad.point
Yongan.Fu,
When you say local command, what do you mean by that? Currently, I am calling the keyin method of the session object to send in the keyin commands. How would I call a local command?
Jan,
Unfortunately, the OnMasterFileStart session event happens too early also. I'm trying to subscript to the EnterIdleEvent event via InputCallback_SetEnterIdleFunction in c++. Thus far it is not working, in that my event handler in c# is not firing.
Sorry, I should have also thanked you both for responding
Brenden OReilly said:I'm trying to subscript to the EnterIdleEvent event via InputCallback_SetEnterIdleFunction in c++.
It's hard to say why it does not work without any code shared, but personally I am not surprised, because my experience is that it's complex and tricky thing. Personally I prefer to use Interop event handler (because to use my preferred C++/CLI is overkill in the discussed situation).
Answer Verified By: Brenden OReilly
I was able to get the event to fire in my c# code almost at the exact same time as your response came into my email inbox. The issue was that variable holding the delegate/call back method in my c# code was a local variable, so it was going out of scope before it could be called by the c++ code.
I just had to change it to declare it as an instance variable and now the callback method fires in my c# code. Strangely enough, the callback fires continuously as if Microstations keeps telling me that it is idle, which sort of make sense. I will just have to unsubscribe to the event after the first time it fires, which is what I want.
The event also is clearly being fired after the file is fully loaded, so putting my key ins in this event should work fine.
Thank you for your valuable help!
A command can be your own, this is called local command. In C++ programming's .r file, we use LCMD to denote it. If the command comes from MS itself, we call this kind of command MS command. In C++ programming's .r file, we use MCMD to denote it.