Error opening model - The local file is not owned by the current user, who therefore is not allowed to modify it.

Error opening model - The local file is not owned by the current user, who therefore is not allowed to modify it.

FrankHolidaytoiling
Advocate Advocate
311 Views
4 Replies
Message 1 of 5

Error opening model - The local file is not owned by the current user, who therefore is not allowed to modify it.

FrankHolidaytoiling
Advocate
Advocate

How do i skip this error or just close the file i am opening cleanly, when i get the error on opening a Revit model? 

"The local file is not owned by the current user, who therefore is not allowed to modify it. "

I need to process many revit models automatically so I want to just skip this if i can however I realise you cannto skip errors in general, so do i roll back the transaction or do i need a try catch and then skip. I will try both approaches.

 

0 Likes
312 Views
4 Replies
Replies (4)
Message 2 of 5

jeremy_tammik
Alumni
Alumni

It sounds as if you are in an add-in code and repeatedly calling the OpenDocumentFile method for batch processing. Can you confirm, and maybe provide some further context? Thank you.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 5

FrankHolidaytoiling
Advocate
Advocate

 

        private static readonly string LogFilePath = @"C:\temp\processing_log.txt";

        //Missing
        //warnings swallow
        //error skipping
        //taskdialog clicking, need to use Documentopened event

        internal static void GetModelsFromDirectory(UIApplication uiApp)
        {
            using(FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
            {
                folderBrowserDialog.Description = "Select Folder Containing Revit Models";
            
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    string[] rvtFiles = Directory.GetFiles(folderBrowserDialog.SelectedPath, "*.rvt", SearchOption.AllDirectories);

                    using (FormProg form = new FormProg(rvtFiles.Length))
                    {
                        form.TopLevel = true;
                        form.AutoScroll = false;
                        form.FormBorderStyle = FormBorderStyle.None;
                        form.Show();

                        foreach (string rvtPath in rvtFiles)
                        {
                            File.AppendAllText(LogFilePath, $"+++: {rvtPath} \n");
                            if (!IsRvtFile(rvtPath)) continue;
                            if (!IsCurrentVersion(uiApp, rvtPath)) continue;
                            if (IsBackupFiles(rvtPath)) continue;

                            form.StepProgress(rvtPath);
                            ProcessModel(uiApp, rvtPath);                           
                        }
                    }
                }
            }
        }
        private static void ProcessModel(UIApplication uiApp, string rvtPath)
        {
            string fileName = Path.GetFileName(rvtPath);

            try
            {
                using (var modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(rvtPath))
                using (var openOptions = new OpenOptions { Audit = true, DetachFromCentralOption = DetachFromCentralOption.DoNotDetach })
                {
                    Document doc = null;

                    try
                    {
                        doc = uiApp.Application.OpenDocumentFile(modelPath, openOptions);
                    }
                    catch (Autodesk.Revit.Exceptions.WrongUserException)
                    {
                        return;
                    }
                    catch (Exception)
                    {
                        return;
                    }

                    ProcessAndSaveDocument(doc);

                    doc.Close(true);
                }
            }
            catch (Exception) { }
        }
        private static void ProcessAndSaveDocument(Document doc)
        {
            using (var transaction = new Transaction(doc, "Process Model"))
            {
                transaction.Start();

                var failureHandlingOptions = transaction.GetFailureHandlingOptions();
                failureHandlingOptions.SetFailuresPreprocessor(new CustomFailureProcessor());
                transaction.SetFailureHandlingOptions(failureHandlingOptions);
                failureHandlingOptions.SetClearAfterRollback(true);

                using (var saveOptions = new SaveOptions { Compact = true })
                {
                    doc.Save(saveOptions);
                }

                transaction.Commit();
            }
        }

        private static bool IsRvtFile(string filePath)
        {
            bool isRvt = Path.GetExtension(filePath).Equals(".rvt", StringComparison.OrdinalIgnoreCase);

            if (isRvt)
            {
                File.AppendAllText(LogFilePath, $"Is Extension .Rvt?: {isRvt} \n");
            }
            else
            {
                File.AppendAllText(LogFilePath, $"Is Extension .Rvt?: {isRvt} \n");
            }

            return isRvt;
        }
        private static bool IsCurrentVersion(UIApplication uiApp, string rvtPath)
        {
            BasicFileInfo fileInfo = BasicFileInfo.Extract(rvtPath);
            string fileRevitVersion = fileInfo.Format;
            string currentRevitVersion = uiApp.Application.VersionNumber;
            bool isCurrentVersion = fileRevitVersion == currentRevitVersion;

            if (isCurrentVersion)
            {
                File.AppendAllText(LogFilePath, $"Is Revit Version Current?: {isCurrentVersion} \n");
            }
            else
            {
                File.AppendAllText(LogFilePath, $"Is Revit Version Current?: {isCurrentVersion} \n");
            }

            return isCurrentVersion;
        }
        private static bool IsBackupFiles(string filePath)
        {
            string fileName = Path.GetFileName(filePath);
            Regex pattern = new Regex(@"\.\d{4}\.rvt$");
            bool isBackup = pattern.IsMatch(fileName);

            if (isBackup)
            {
                File.AppendAllText(LogFilePath, $"Skip if Is Backup File?: {isBackup} \n");
            }
            else
            {
                File.AppendAllText(LogFilePath, $"Skip if Is Backup File?: {isBackup} \n");
            }

            return isBackup;
        }

    }//cl

    internal class CustomFailureProcessor : IFailuresPreprocessor
    {
        private static readonly string LogFilePath = @"C:\temp\processing_log.txt";

        public FailureProcessingResult PreprocessFailures(FailuresAccessor failuresAccessor)
        {

            IList<FailureMessageAccessor> failures = failuresAccessor?.GetFailureMessages();
            if (failures != null)
            {
                foreach (FailureMessageAccessor failure in failures)
                {
                    if (failure.GetSeverity() == FailureSeverity.Error)
                    {
                        File.AppendAllText(LogFilePath, "Internal model errors: True \n");
                        return FailureProcessingResult.ProceedWithRollBack;
                    }
                    else if (failure.GetSeverity() == FailureSeverity.Warning)
                    {
                        failuresAccessor.DeleteWarning(failure);
                        FailureHandlingOptions handlingOptions;
                    }  
                }
            }
            return FailureProcessingResult.Continue;
        }
    }

 

0 Likes
Message 4 of 5

jeremy_tammik
Alumni
Alumni

Tsk, tsk, tsk... you are totally mixing up your user interface with the actual model processing. That is pretty tricky and fraught with peril. Remember that the Revit API is single-threaded, and the user interface is often a mixture of modal and modeless. I would strongly recommend that you separate your UI from the actual processing. In fact, it is best to separate your UI from all Revit API calls, if possible. So, if your UI needs to present some information obtained from the Revit API, obtain it first, terminate all Revit API interaction, and present your UI. In your UI, obtain all required user input,. store it, and close the UI and all its forms. After that, you can start processing the models in the required manner again, using the Revit API. Isolate. KISS.  I hope this helps. Good luck.

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 5 of 5

FrankHolidaytoiling
Advocate
Advocate

Thanx Jeremy,

I Think that I need to use the on document opened event to create the transaction as opening a document file does not invoke a Transaction. I do not want to delete the errors as sometimes this involves deleting elements from the model that users still require for Prints, my strategy would be to rollback after logging the specific error and ask the model manager to replace error elements. I would be running on many models of the same Revit year only. Another Idea I had was to drive the opening of the Revit model from a windows task scheduler, the on document opened event should be driven from the on opened event. 

0 Likes