Close Active Project/Document in REVIT 2016 using c# API

Close Active Project/Document in REVIT 2016 using c# API

Anonymous
Not applicable
5,100 Views
16 Replies
Message 1 of 17

Close Active Project/Document in REVIT 2016 using c# API

Anonymous
Not applicable

I am trying to close the active project(.rvt)/document in REVIT 2016 using c# API.

It is throwing below errors, I have tried multiple codes but all are throwing different exceptions.

 

I have added my code to open the document and the code I tried, please go through this and revert back with the solution ASAP.

 

The code I have written to open the document:

 

  commandData.Application.OpenAndActivateDocument(@"D:\REVIT\IBG Projects\C12\C12_type C12 for export 12 sep central.rvt");

 

 

I tried below codes:

 

  •   SendKeys.SendWait("^{F4}"); - Not giving any error though not closing the document.
  • var placeholderFile = @"C:\placeholder.rvt";
    var doc = commandData.Application.ActiveUIDocument.Document;
    var file = doc.PathName;
    var docPlaceholder = commandData.Application.OpenAndActivateDocument(placeholderFile);
    doc.Close(false);
    var uidoc = commandData.Application.OpenAndActivateDocument(file);
    docPlaceholder.Document.Close(false);
  • commandData.Application.ActiveUIDocument.SaveAndClose();

I tried code from bellow two links

http://adndevblog.typepad.com/aec/2015/07/revitapi-close-active-document.html

http://thebuildingcoder.typepad.com/blog/2010/10/closing-the-active-document-and-why-not-to.html

 

Attached the error came on my screen after adding below code.

docPlaceholder.Document.Close(false); 

 

Please reply ASAP.

 

Thanks

0 Likes
5,101 Views
16 Replies
Replies (16)
Message 2 of 17

FAIR59
Advisor
Advisor

This code seems fine to me. 

 

var placeholderFile = @"C:\placeholder.rvt";
var doc = commandData.Application.ActiveUIDocument.Document;
var file = doc.PathName;
var docPlaceholder = commandData.Application.OpenAndActivateDocument(placeholderFile);
doc.Close(false);
var uidoc = commandData.Application.OpenAndActivateDocument(file);
docPlaceholder.Document.Close(false);

Getting the error means that the reloading of the document has failed. I suspect you are in a newly created document, that has no PathName assigned yet.

Check the value of the variable  file.

0 Likes
Message 3 of 17

Anonymous
Not applicable

Hi,

 

Thanks for the reply.

I am trying the below code.

Giving the same error can you please help me on that.

 

 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementsArg)
        {
   

            commandData.Application.OpenAndActivateDocument(@"D:\REVIT\IBG Projects\C12S\C12S_type C12s for export 12 sep central_old.rvt");
            


            var placeholderFile = @"D:\REVIT\IBG Projects\C12S\C12S_type C12s for export 12 sep central_old.rvt";
            var doc = commandData.Application.ActiveUIDocument.Document;
            var file = doc.PathName;
            var docPlaceholder = commandData.Application.OpenAndActivateDocument(placeholderFile);
            doc.Close(false);
            var uidoc = commandData.Application.OpenAndActivateDocument(file);
            docPlaceholder.Document.Close(false);

}
0 Likes
Message 4 of 17

FAIR59
Advisor
Advisor

the filename for the first project is equal to the filename for the placeholder project.

You try to open en close the same file.

0 Likes
Message 5 of 17

bvgarbar
Contributor
Contributor

Hi!

Maybe someone has already found this solution to close active document in revit, i don't know)

but I found the following method.

 

RevitCommandId closeDoc = RevitCommandId.LookupPostableCommandId(PostableCommand.Close);

uiapp.PostCommand(closeDoc);

 

Bogdan.

Message 6 of 17

jeremytammik
Autodesk
Autodesk

Shared for posterity by The Building Coder:

 

https://thebuildingcoder.typepad.com/blog/2019/04/close-doc-and-zero-doc-rvtsamples.html#5

 

Many thank to you, Bogdan!

 



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

Message 7 of 17

rui.frazao.oliveira
Contributor
Contributor

Hi Bogdan,

I have tried your suggestion but nothing happen.

What I am doing wrong?

Thanks & regards,

Rui

0 Likes
Message 8 of 17

bvgarbar
Contributor
Contributor

Hi, Rui!

I checked again my method for Revit versions 15, 17 and 20. Everything works.

Please describe your problem in more detail or send your code to take a look at it.

Thanks,

Bogdan.

0 Likes
Message 9 of 17

rui.frazao.oliveira
Contributor
Contributor

Hi Bogdan,

 

I am using Revit 2017 and I have the following code:

        public bool m_Close()
        {
            string ls_source = "RevitAddinServer::m_Close";
            try
            {
                ib_closedByEvent = false;

                //AddinGlobal.ActiveDocument.Close();
                RevitCommandId closeDoc = RevitCommandId.LookupPostableCommandId(PostableCommand.Close);
                AddinGlobal.uiapp.PostCommand(closeDoc);

                ib_closedByEvent = true;
                return true;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ls_source + "\n" + ex.Message, "e-grou Message", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                return false;
            }
        }

And I call it from a button in my custom ribbon bar, after open a document and the document is not closed.

 

If I click more than once appears the message:

"Revit does not support more than one command are posted."

 

Thanks & Regards,

Rui

0 Likes
Message 10 of 17

bvgarbar
Contributor
Contributor

Hi, Rui!

I find it difficult to understand the part of the code that is taken out of context.
If possible, please post your solution.

 

Bogdan.

0 Likes
Message 11 of 17

rui.frazao.oliveira
Contributor
Contributor

Hi Bogdan,

in attach a sample code project with my code.

Thanks & Regards,

Rui

 

 

0 Likes
Message 12 of 17

bvgarbar
Contributor
Contributor

Hi, Rui.

I looked at your code.

Please tell me what you want to do?

Do you want to make a button on the panel and when clicked, the project should close...or smth else?

Thanks, 

Bogdan.

0 Likes
Message 13 of 17

rui.frazao.oliveira
Contributor
Contributor

Yes, I want to have a button on the panel and when clicked, "do something"... and at the end of that "something" the active document should close.

 

At my sample code I ignored the part "do something" (that I already control) and just tried to close the active document (the part that I didn't know how to do).

 

PS: I have similar addins in AutoCAD and Inventor, and in both I have always ways in API to close the active document by API, but unhappily in Revit the method "ActiveDocument.Close()" don't work as expected.

 

Thanks & Regards,

Rui

0 Likes
Message 14 of 17

bvgarbar
Contributor
Contributor

Hi, Rui!

I tried my solution again using ExternalCommand and IExternalEvent.
Yesterday, I uploaded your solution. At the same time, I noticed a strange effect for myself. When I start debug my solution using, the visual studio changes to your code and revit continues to do something, but all is unsuccessful.

I have an assumption that something went wrong with the addition of the panel and the purpose of the button.
As an alternative, I suggest you try using the VCRevitRibbonUtil library to add a panel and button. Next, try implementing through ExternalCommand.

 

For example, When i've creating my solution, i used IExternalCommand. Smth done and if Result==true, then try post command.

 

Thanks & Regards,

Bogdan.

0 Likes
Message 15 of 17

rui.frazao.oliveira
Contributor
Contributor

Hi Bogdan,

Did you have some sample using the VCRevitRibbonUtil library to add a panel and button?

Thanks & regards,

Rui

0 Likes
Message 16 of 17

bvgarbar
Contributor
Contributor

Hi, Rui!

You can see this sample

https://adn-cis.org/legkoe-sozdanie-knopok-na-lente-dlya-vyizova-vneshnix-komand.html).

Thanks & regards,

Bogdan

0 Likes
Message 17 of 17

rui.frazao.oliveira
Contributor
Contributor

Hi Bogdan,

Creating the ribbon with

Autodesk.Revit.UI

instead of

Autodesk.Windows

works well!

Thanks & regards,

Rui

0 Likes