March 19, 2019

When working with NewProjectDocument by Design Automation API

Follow @Xiaodong Liang

When I worked on San Francisco Accelerator the week before last, one requirement was to create an RVT file from a template file (RTE), do some jobs and save this RVT file by Design Automation API (DA). This work item failed. Because of another issue with log file, I was not able to know what happened with my code. With the help of engineer team (Rahul and Lijuan), I finally got the root cause. Thank you Rahul and Lijuan!

In general process of DA, we import a source Revit file, do some jobs and output data or updated file. The activity is defined like (demo of Postman )

{
	"id": "{{activityName}}",
	"commandLine": [ "$(engine.path)\\\\revitcoreconsole.exe /i $(args[rvtFile].path) /al $(appbundles[{{appBudleName}}].path)" ],
	"parameters": {
	  "rvtFile": {
		"zip": false,
		"ondemand": false,
		"verb": "get",
		"description": "Input Revit model",
		"required": true
	  },
	  "result": {
		"zip": false,
		"ondemand": false,
		"verb": "put",
		"description": "Results",
		"required": true,
		"localName": "result.rvt"
	  }
	},
	"engine": "Autodesk.Revit+2019",
	"appbundles": [ "{{dasNickName}}.{{appBudleName}}+{{appBundleAlias}}" ],
	"description": "Demo DA Activity."
}

The code snippet of plugin would be:

public void HandleDesignAutomationReadyEvent(object sender, DesignAutomationReadyEventArgs e){
            e.Succeeded = true;
            Application app = sender as Application; 

            DoJob(e.DesignAutomationData);
 }

public static void DoJob(DesignAutomationData data){
	  Application rvtApp = data.RevitApp;
     if (rvtApp == null) throw new InvalidDataException(nameof(rvtApp)); 

      Document rvtDoc = data.RevitDoc;
     //Do the job with the document	
    //Output… 
}

 Since we set the source file as the parameter for /i, Design Automation of Revit will open this file in the sandbox by default, and do the custom job. The workflow is:

1

If our job is to create a new document from the template file, e.g.

public static void DoJob(DesignAutomationData data){
	  Application rvtApp = data.RevitApp;
            if (rvtApp == null) throw new InvalidDataException(nameof(rvtApp));

      Document rvtDoc = data.RevitDoc;
      Document newDoc = e.DesignAutomationData.RevitApp.NewProjectDocument( rvtDoc.PathName);
     //Do job with newDoc
     //Output…
}

the process of sandbox on cloud will still open the template file, and try to create a new file from the template file. However, this template file has been locked! So NewProjectDocument will throw the exception, which causes the work item fail. 

2

Actually, this problem can be addressed by Debug Tool. If testing with the tool, NewProjectDocument will throw exception. So I strongly recommend try with Debug Tool before you test on DA. It can help to diagnose most problems directly.

So, what is the correct usage to NewProjectDocument ? Easy! define the template file as common parameter, instead of parameter for /i . e.g. the activity can be defined as below: remove /i, instead, define the import file with a localName. 

{
    "id": "{{activityName}}",
    "commandLine": [ "$(engine.path)\\\\revitcoreconsole.exe /al $(appbundles[{{appBudleName}}].path)" ],
    "parameters": {
      "rvtFile": {
        "zip": false,
        "ondemand": false,
        "verb": "get",
        "description": "Input Revit model",
        "required": true,
         "localName": "BaseTemplate.rte"
       },
      "result": {
        "zip": false,
        "ondemand": false,
        "verb": "put",
        "description": "Results",
        "required": true,
        "localName": "result.rvt"
      }
    },
    "engine": "Autodesk.Revit+2019",
    "appbundles": [ "{{dasNickName}}.{{appBudleName}}+{{appBundleAlias}}" ],
    "description": "Demo Activity."
}

In the plugin, use the local name to create the new document. What will happen? When the work item starts, the sandbox of DA will download the template file to working directory, but not opened in the engine of DA. Then, the engine of DA will create new document from the template file by the local name defined in Activity. 

public static void DoJob(DesignAutomationData data){
	  Application rvtApp = data.RevitApp;
       if (rvtApp == null) throw new InvalidDataException(nameof(rvtApp));

     Document rvtDoc = data.RevitDoc;
            Document newDoc = e.DesignAutomationData.RevitApp.NewProjectDocument(“BaseTemplate.rte”);
     //Do job with 	newDoc
      //Output… 
}

3

Posted by

Xiaodong Liang has been with Autodesk since 2007, focusing on providing programming support, consulting, training and evangelism to external developers. He started his career China and now lives in Beijing, China. Xiaodong is currently a developer consultant in the team Developer Technical Services (DevTech), the worldwide team of API gurus providing technical services through the Autodesk Developer Network. He supports...

Related Posts