Hi All,
I am struggling with making changes to a document after i have used open and activate document. I think its something to do with the transaction as this keeps crashing the debugger.
What i am trying to do is register an event so if someone clicks "New Project" in the Revit program rather than the default box they get my document opened and once its opened i want to change the project name from the project information.
Code below
using System; using System.Collections.Generic; using Autodesk.Revit.ApplicationServices; using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using Autodesk.Revit.UI.Events; namespace RevitAddin2 { [Transaction(TransactionMode.Manual)] class App : IExternalApplication { public Result OnStartup(UIControlledApplication a) { // Register dialog box event. a.DialogBoxShowing += new EventHandler<DialogBoxShowingEventArgs>(application_new); return Result.Succeeded; } public Result OnShutdown(UIControlledApplication a) { // remove the dialog box event. a.DialogBoxShowing -= application_new; return Result.Succeeded; } public void application_new(object sender, DialogBoxShowingEventArgs args) { if (args.DialogId == "Dialog_Revit_NewProject") { args.OverrideResult((int)TaskDialogResult.Cancel); UIApplication uiapp = sender as UIApplication; Application app = uiapp.Application; uiapp.OpenAndActivateDocument(@"C:\Users\mike.turpincmw\Desktop\output.rvt"); UIDocument uidoc = uiapp.ActiveUIDocument; Document doc = uidoc.Document; using (Transaction tx = new Transaction(doc)) { tx.Start("ChangeName"); doc.ProjectInformation.Name = "Project1"; tx.Commit(); } } } } }
Any help or pointers greatly appreciated!
Mike
Solved! Go to Solution.
Solved by jeremytammik. Go to Solution.
Hi Mike,
The OpenAndActivateDocument function returns a uiDoc, so try using that instead of assuming that the uiapp is fully synchronised.
If your debugger is failing, go old school by logging between lines of code.
Cheers,
-Matt
Dear Mike and Matt,
Happy New Year to you all!
Nice to see you back again, Matt!
Thank you for your interesting query and helpful suggestion.
I have some further ideas:
What you are attempting to do sounds slightly challenging, and Revit may be thinking so as well.
In effect, you are interfering with its process of creating a new document.
It may well have open transactions going on in the other document.
I would suggest simplifying your code somewhat, in the following sense:
Separate the separate steps into separate steps.
In other words:
As long as Revit is busy creating and opening the new document that has been requested, let it go ahead and do so until full completion.
Once the new document has been completely created and activated and Revit has stopped fussing around with it, jump in and do your stuff, e.g.:
I can imagine that subscribing to a one-off call from the Idling event might help you achieve this.
When the dialogue box showing event appears, subscribe to the idling event.
When the Idling event handler is called, check whether Revit has really and truly and completely finished setting up the new document.
If so, go ahead as described above.
If not, think again, further.
I hope this helps.
Good luck!
Best regards,
Jeremy
Hi Jeremy,
Happy new year to you.
You've been busy over the break!
Thanks once again for taking the 'bigger picture' view.
Cheers,
-Matt
🙂
The two approaches complement each other perfectly!
Here is another related issue and further explanation: