Search code examples
c#revit-apirevit

OpenAndActivateDocument generates ArgumentException


I have to call a function that needs my active document to be close. Since I can't close an active document I create an empty project and use OpenAndActivateDocument to set the empty project to active. Than I close the first document and call that function, after that I call OpenAndActivateDocument to reopen the first document. When I call OpenAndActivateDocument an ArgumentException is generated: The file is not a Revit document.

I have checked the string filePath before the exception and it is a valid path to a project.

Here is my code:

try{
 string filePath = mainDoc.PathName;
      string fileName = Path.GetFileNameWithoutExtension(filePath);
      try { mainDoc.Save(); } catch (Exception) { }
      Document placeHolder = uiapp.Application.NewProjectDocument(UnitSystem.Metric);     //Creo un progetto temporaneo vuoto
      if (File.Exists(@"C:\Users\Public\placeholder.rvt")) {
        File.Delete(@"C:\Users\Public\placeholder.rvt");
      }
      placeHolder.SaveAs(@"C:\Users\Public\placeholder.rvt");
      placeHolder = uiapp.OpenAndActivateDocument(placeHolder.PathName).Document;                                //Apro il progetto temporaneo e lo attivo
      mainDoc.Close(false);                                                               //Chiudo il progetto precedente

      if (!Documenta.messageBroker.SCheckInDoc(fileName, DcmRPC.DcmEnum.enumCheckinType.AutoIn, 0)) {   //Faccio il checkin del progetto
        TaskDialog.Show("Errore", "Impossibile effettuare il check in, codice documento: " + fileName);
        return Result.Failed;
      }

      mainDoc = uiapp.OpenAndActivateDocument(filePath).Document;                                            //Riapro il progetto principale
      placeHolder.Close(false);                                                           //Chiudo il progetto temporaneo
      if (File.Exists(@"C:\Users\Public\placeholder.rvt")) {
        File.Delete(@"C:\Users\Public\placeholder.rvt");
      }
    }
    catch (Exception exc) {
      TaskDialog.Show("Errore", exc.ToString());
      message = exc.Message;
      return Result.Failed;
    }

Solution

  • Solved it. There simply wasn't enough space on the disk.