« Duplicate Mark Values | Main | Revit API Resource and Getting Started Guide »

March 23, 2010

Comments

Hi Jeremy,
This post got me half way to where I need to be. Is there a way to start the revit.exe and then supply the file name? I need a way to deal with multiple versions installed on a machine and have the correct year / flavor open. I tried setting ...StartInfo.Arguments to the file name but Revit returns a "Failed to open document." error. Any ideas?

Dear Chris,

Sorry for the very late answer, I seem to have missed the original notification when you posted your comment.

You should be able to use Process.Start to start up Revit.exe as well, specifying the full desired path name and the project file as an argument at the same time, cf. e.g.

http://dotnetperls.com/process-start-net

Cheers, Jeremy.

Wow wow, GREAT web site!
I can’t tell you how much time is spend reading your Website getting ideas, testing, playing.
I don’t even have any real programming experience and I’m thinking of leaving my Architectural Job and starting my own Software Company.

A friend and I am trying to write an add-on that will loop through a list of families, open each family extract a list of type names and parameters values and close it without saving.

The code I have post below works for a small number of families (less that 100) but Revit crashes when we process a large number (more than 100) it displays a dialog saying it was “memory corruption”. It would happen during the opening of a file “OpenDocumentFile()”(see code below).
Is there a better way to loop through families opening and closing them?
Could we have an external application fire up Revit and run my plugin. The external application could keep track of which file was next.

Thank you very much any help would be much appreciated.

void LoadFamily(Autodesk.Revit.ApplicationServices.Application app,
ExternalCommandData commandData, string filename, StreamWriter writer)
{
try
{
if (string.IsNullOrEmpty(filename))
return;

if (!File.Exists(filename))
return;

Document activeDoc =
commandData.Application.ActiveUIDocument.Document;

if (activeDoc != null)
activeDoc.Close();

activeDoc = app.OpenDocumentFile(filename);

if (activeDoc == null)
return;

//process the document and add data to an external file
}
catch (Exception e)
{
MessageBox.Show(e.Message + "\n" + filename);
}
}

Dear Nicholas,

Thank you very much for your appreciation, and I am very glad that you find it useful!

Congratulations on getting into Revit programming with your friend!

I see several sub-questions in your comment:

1) How can I reduce the memory being used by Revit when I open and close a file?

2) How can I drive Revit from outside to open a specific file, execute some code in it, and close the file again, so that I can keep track from outside of which files have been processed so far?

Regarding the first question, I see you are closing the active document. I do not think that this will work. When you open a document through the Revit API, that does not affect the active foreground document, as far as I know. The API-opened document will always be a background document. I would do something like this instead, ignoring the active document, doing all the processing in the background:

Document doc = app.OpenDocumentFile( filename );
Process( doc );
doc.Close();

Try it out, that might solve your problem.

If not, you can continue with your second idea, which makes good sense to me. I have resorted to similar approaches in the past for batch processing a large number of files with applications which do not release all the data every time the files is closed, so they eventually run out of memory.

You could implement your functionality in an Application.DocumentOpened event handler, and then use a command line application to start up Revit with a journal file to simply open and close each document you wish to process. You could also simply implement your functionality in an external command instead of the DocumentOpened event, and add the launching of your command to the journal file steps. For more info on this approach, please refer to

http://thebuildingcoder.typepad.com/blog/2009/07/journal-file-replay.html

I hope this helps and wish you much success with your project!

Cheers, Jeremy.

Is is possible to open a project and show the specify workset dialog before the file opens?
Thanks.

Dear Ian,

If it is possible for you to include starting up Revit itself into the steps you describe, then yes, that should be possible using a journal file as described in

http://thebuildingcoder.typepad.com/blog/2010/07/ifc-import-and-conversion-journal-script.html

Otherwise, you might be able to use Autohotkey or the Windows API or some other mechanism to simulate the user input to display the desired dialogue.

Cheers, Jeremy.

Hi Jeremy,

Is it possible to open/load a Revit project(.rvt) without Revit.exe instance running? Most of the Revit Sdk samples are running in add-in behavior where the Revit.exe must be startup first.

Let's said i want to open a Revit project and retrieve the elements inside the project and show it in a windows form.(without running in add-in manner)

Dear Bsyap,

Short and clear answer: no, sorry, no way.

Cheers, Jeremy.

Hi Jeremy,

For Revit 2012, is it possible to insert Revit file as linked file using Revit API. I have read and search if there are any method or way to doing it but failed. I only managed to open revit file in a separate window using OpenAndActivateDocument.

Thank you

Abby

Dear Abby,

Sorry, that is currently not supported by the Revit API.

All we have right now is the Document.Link method, which only applies to DWG files.

Cheers, Jeremy.

Is there any way to open revit file with 'Audit' option using Process.Start? Where could I find the full list of command line parameters (if there are any)?
Thank you!

Dear Victor,

Nope, not that I know of.

Your best bet, if you really want to do it anyway, without official API support, might be to try using UI Automation:

http://thebuildingcoder.typepad.com/blog/automation

Cheers, Jeremy.

Hello,
your solution works well thanks a lot

I have just create a small code OpenRevit.exe to open a file in REVIT with filename as Parameter (use "" if filename have any space in name , ex : OpenRevit.exe "C:\temp\My File REVIT.rvt")


Module Module1

Sub Main()
Dim CommandLine As String = My.Application.CommandLineArgs(0)
'Environment.GetCommandLineArgs()
System.Diagnostics.Process.Start(CommandLine)
End Sub

End Module

Dear Xavier,

Cool! Thank you for your appreciation and for sharing your code!

Cheers, Jeremy.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Your Information

(Name and email address are required. Email address will not be displayed with the comment.)

Jeremy Tammik

AboutTopicsIndexSource