Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Modifying an OpenAndActivate document

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
miketurpin_innovatingfutures
578 Views, 5 Replies

Modifying an OpenAndActivate document

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

5 REPLIES 5
Message 2 of 6

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


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
Message 3 of 6

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.:

 

  • Switch to the other document that you wish to set up yourself.
  • Discard the newly created document that Revit just finished setting up.

 

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



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 4 of 6

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


Cheers,

-Matt
_______________________________________________________________________________
Marking a post as a 'solution' helps the community. Giving a post 'Kudos' is as good as saying thanks. Why not do both?
Message 5 of 6

🙂

 

The two approaches complement each other perfectly!



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 6 of 6

Here is another related issue and further explanation:

  

https://forums.autodesk.com/t5/revit-api-forum/element-collector-inaccurate-results-when-using/m-p/9...

  

Jeremy Tammik, Developer Advocacy and Support, The Building Coder, Autodesk Developer Network, ADN Open

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Rail Community