New Project Document

A short and simple

question
from Nadim on the NewProjectDocument method:

Question: I am using


Document doc = app.NewProjectDocument(
string templateFile );

Revit shows that it is loading the template as it does usually when it creates a new document; however, it does not really create the new document.

Any idea why?

Answer: I implemented a sample external command CmdNewProjectDoc to test it for you and it works fine for me:


const string _template_file_path
  = "C:/Documents and Settings/All Users"
  + "/Application Data/Autodesk/RAC 2010"
  + "/Metric Templates/DefaultMetric.rte";
 
public IExternalCommand.Result Execute(
  ExternalCommandData commandData,
  ref string message,
  ElementSet elements )
{
  Application app = commandData.Application;
 
  Document doc = app.NewProjectDocument(
    _template_file_path );
 
  doc.SaveAs( "C:/tmp/new_project.rvt" );
 
  return CmdResult.Failed;
}

After running the command, the new project file is created and saved in the expected location:


2009-11-23  12:26  2,056,192 new_project.rvt

Comments

4 responses to “New Project Document”

  1. Rajeswara Rao Avatar
    Rajeswara Rao

    Hello Jeremy Tammik,
    I wanted to create a new project document in OnStartup method of an IExternalApplication. Is there any way.
    Thanks and Regards,
    Rajeswara Rao

  2. Dear Rajeswara,
    The argument passed in to the OnStartup method is a UIControlledApplication instance. If you look at the description of that class in the Revit API help file RevitAPI.chm, it says:
    UIControlledApplication Class: Represents the Autodesk Revit user interface, providing access to UI customization methods and events.
    Remarks: This class does not provide access to documents because it is provided to you through the ExternalApplication OnStartup and OnShutdown methods. It is not possible to work with Revit documents in the context of these methods. You can work with documents by getting them from the UIApplication class, which is provided from other events and the ExternalCommand call-backs.
    This information is from Revit 2011, but the situation is analogous in Revit 2010 as well.
    So the answer is no, you will have to use some other approach to create your new document. There are many simple alternative approaches. For instance, there are numerous other events that you can subscribe to in the OnStartup method, and then you can create your new document in one of their event handlers.
    One such event may be the Idling event. Or you could get the ControlledApplication from the UIControlledApplication and register to one of its many events.
    An example of accessing the ControlledApplication and registering to one of its events in the OnStartup method of an external application is given by the sequence OnStartup > UIControlledApplication > ControlledApplication > DocumentOpened in the Revit 2011 ChangesMonitor sample:
    public Result OnStartup(
    UIControlledApplication application )
    {
    m_CtrlApp = application.ControlledApplication;
    . . .
    // register the DocumentChanged event
    m_CtrlApp.DocumentChanged
    += new EventHandler(
    CtrlApp_DocumentChanged );
    . . .
    return Result.Succeeded;
    }
    Cheers, Jeremy.

  3. Rajeswara Rao Avatar
    Rajeswara Rao

    Dear Jeremy,
    Thanks for your answer.
    I was added a ApplicationMenuItem “MyProject) to ApplicationButton->New: successfully. I get the “New” menu item from “RevitApplicationMenu.AppMenuContent.Items”.
    Now my aim is to create a new project doc using my template, when I select “MyProject” application menu item. Is there any work around way to achieve this?
    Thanks and Regards,
    Rajeswara Rao

  4. Dear Rajeswara Rao,
    Sure, you can call the Application.NewProjectDocument method and specify a template file name.
    However, I think you should seriously consider whether you really need to implement your application the way you describe.
    What are you trying to achieve, and in what way will the end user have any advantage by using your menu entry to create a new project instead of going through the normal Revit UI?
    Cheers, Jeremy.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading