How to open and active a new document that is not saved ?

How to open and active a new document that is not saved ?

Anonymous
Not applicable
5,895 Views
7 Replies
Message 1 of 8

How to open and active a new document that is not saved ?

Anonymous
Not applicable

As everyone know, we can create new project in the startup page. Then ,we edit it and save it later to a local path.

 

But i can not open a new document that is not saved.

 

Because the only way API provided is to use the function named below

 

OpenAndActivateDocument(string fileName); 

 

The property named ‘PathName’ of document is empty before the document saved.

 

so i have no fileName to pass to the function OpenAndActivateDocument.

 

is there you guys met the same situation ? could you please share your solution to me ? Thanks in advance!

 

 

0 Likes
5,896 Views
7 Replies
Replies (7)
Message 2 of 8

RPTHOMAS108
Mentor
Mentor

You can't open anything that isn't saved, do you mean activate? i.e. you create a file and then open another meaning the file you created is no longer the active document?

 

You can iterate the list of DB documents in Application.Documents and create a UIDocument from the one you want (using UIDocument.New(Document)). You could try making that active by setting the UIDocument.ActiveView property after going through the list of UIView in UIDocument.GetOpenUIViews.

 

If the created file has no path then you'll have to record the HashCode or something else to identify it later within Application.Documents. Note also that path is also complicated by worksharing. I believe links are still listed in the Application.Documents so you have to be careful not to try and create a UIDocument from one of those (filter out those with Document.IsLinked = true).

0 Likes
Message 3 of 8

Mustafa.Salaheldin
Collaborator
Collaborator

Here is the cure for your pain, You can use the Application.NewDocument method

 

#region "Namespaces"

using System;
using System.Linq;
using System.Collections.Generic;
using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Structure;

using Autodesk.Revit.UI;

//using Autodesk.Revit.Collections;

using RvtApplication = Autodesk.Revit.ApplicationServices.Application;
using RvtDocument = Autodesk.Revit.DB.Document;

#endregion

namespace Toolbox
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class NewProject : IExternalCommand
	{
		#region "Cached Variables"


		private static ExternalCommandData _cachedCmdData;
		public static UIApplication CachedUiApp {
			get { return _cachedCmdData.Application; }
		}

		public static RvtApplication CachedApp {
			get { return CachedUiApp.Application; }
		}

		public static RvtDocument CachedDoc {
			get { return CachedUiApp.ActiveUIDocument.Document; }
		}

		#endregion

		#region "IExternalCommand Members"

		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elemSet)
		{
			_cachedCmdData = cmdData;
			UIDocument _cachedUiDoc = CachedUiApp.ActiveUIDocument;

			try {
                //TODO: add your code below.

                RvtDocument doc = CachedUiApp.Application.NewProjectDocument(UnitSystem.Metric);
                doc.SaveAs(@"E:\bla.rvt");
                CachedUiApp.OpenAndActivateDocument(doc.PathName);
                return Result.Succeeded;
			} catch (Exception ex) {
				msg = ex.ToString();
				return Result.Failed;
			}
		}
        #endregion

    }


}

You have two options, either to create new document with no template, or to determine a template name.

If this reply satisfies your needs please don't forget to mark it as an answer.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

Message 4 of 8

Anonymous
Not applicable

Thanks for your answer. 

 

I think you are not very clear what i want.

In general, we could click the menu named "new" in the Revit startup page. And then,we can see the project has been created and activated, but the current project has not been saved to local until to click the save menu button or press hot-key Ctrl+S.

 

But by Revit API, I can never open/activated a document that is not saved.

0 Likes
Message 5 of 8

Anonymous
Not applicable

Thanks for your answer.

What i need to do is to create a new document and active the document before saving.

Just like the general operation of Revit: 

 

1, New and activate a project

 

2, Edit the project 

 

3, At last to save the project or just exit Revit without saving.

 

How could i to reappear this situation by Revit API ? any idea on it?

 

Thanks in advance.

0 Likes
Message 6 of 8

Mustafa.Salaheldin
Collaborator
Collaborator

Then unfortunately the answer is you can't do this. You have to pass a file path in order to do it so you have to temporarly save the document to get a file path.


¯\_(ツ)_/¯
Let it work like a charm.

Mustafa Salaheldin


EESignature




Digital Integration Manager, DuPod

Facebook | Twitter | LinkedIn

0 Likes
Message 7 of 8

RPTHOMAS108
Mentor
Mentor

When you call UIDocument.ShowElements the active Document will change to the document you are showing elements in, therefore:

 

If you filter for any element in one of the views of the newly created document you can call the above function using that element which will then activate that document.

 

Sometimes you get the dialogue 'No good view found' which is odd considering you know there is at least one view with the element in considering you are filtering for it by view. You can handle the appearance of this dialogue and the ActiveDocument still gets changed. I believe the new document generally has a view with Elevation markers hence there is always something to find and show.

 

I don't know the implications of changing the ActiveDocument this way or why the API has no ability to directly change the ActiveDocument directly but I suspect if it were easy in terms of how the API works, it would have been done by now.

0 Likes
Message 8 of 8

jeremytammik
Autodesk
Autodesk

Edited and saved for posterity here:

 

http://thebuildingcoder.typepad.com/blog/2018/03/switch-view-or-document-by-showing-elements.html

 

Cheers,

 

Jeremy



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

0 Likes