public static void InitiateClashDetection(string fileName) { if (_app == null) { StartNavisworksInstance(); } _appAuto.OpenFile(fileName); }
private async void Run_Plugin(object sender, RoutedEventArgs e) { NavisworksApplication navisworksApplication = null; try { // Reset values of form ExecutionTime_label.Content = ""; proBar_label.Content = ""; if (txtFile_hasChoosed && document_hasChoosed) { // Open File Dialog to choose the directory of the output file SaveFileDialog saveFileDialog = new SaveFileDialog(); if (Dir_SavedFile == "c:\\") { saveFileDialog.InitialDirectory = Dir_Document; } else { saveFileDialog.InitialDirectory = Dir_SavedFile; } saveFileDialog.Filter = "Navisworks files (*.nwd)|*.nwd|All files (*.*)|*.*"; saveFileDialog.FilterIndex = 1; int returned_status = 0; if (saveFileDialog.ShowDialog() == true) { // Start timer to calculate the execution time var watch = System.Diagnostics.Stopwatch.StartNew(); // Get extension of the entered filename string ext = Path.GetExtension(saveFileDialog.FileName); // Check if user entered the extension of filename or not if (ext == ".nwd") { // Update the Form to inform the user that the Plugin has started proBar.Value = 1; proBar_label.Foreground = Brushes.Red; proBar_label.Content = "Файл обрабатывается"; fullpath_SavedFile = saveFileDialog.FileName; } else if (ext == "") // If the user didn't enter the extension of the output file, add it { // Update the Form to inform the user that the Plugin has started proBar.Value = 1; proBar_label.Foreground = Brushes.Red; proBar_label.Content = "Файл обрабатывается"; fullpath_SavedFile = saveFileDialog.FileName + ".nwd"; } else { // Update the Form to inform the user that the Plugin has started proBar.Value = 0; proBar_label.Foreground = Brushes.Red; proBar_label.Content = "Выберите правильный формат файла"; throw new System.ArgumentException(" Формат файла должен быть '.nwd' \n " + " The extension of selected file has to be '.nwd' "); } // The path of directory where to save outputfile Dir_SavedFile = Path.GetDirectoryName(saveFileDialog.FileName); // Start another thread to run the Plugin await Task.Run(async() => { try { // Start Navisworks navisworksApplication = new NavisworksApplication(); //disable progress whilst we do this procedure navisworksApplication.DisableProgress(); // Enter Document to Navisworks navisworksApplication.OpenFile(fullPath_Document); // Run Plugin returned_status = navisworksApplication.ExecuteAddInPlugin("CustomTab_main.Speech", fullPath_TextFile, "Yes"); // Save the output file navisworksApplication.SaveFile(fullpath_SavedFile); //Re-enable progress navisworksApplication.EnableProgress(); } catch (AutomationException exp) { //An error occurred, display it to the user MessageBox.Show(exp.Message, "Error "); } catch (AutomationDocumentFileException exp) { //An error occurred, display it to the user MessageBox.Show(exp.Message, "Error "); } catch (Exception exp) { MessageBox.Show(exp.Message, "Error "); } finally { // Close Navisworks if (navisworksApplication != null) { navisworksApplication.Dispose(); } } }); //MessageBox.Show("Returned Value :" + return_status); if (returned_status == 1) { // Update the Form to inform the user that the Plugin has finished proBar.Value = 3; // Execution time of Plugin watch.Stop(); long t_seconds = watch.ElapsedMilliseconds / 1000; long time_minutes = t_seconds / 60; long time_seconds = t_seconds - (time_minutes * 60); ExecutionTime_label.Content = $"Время Выполнения: {time_minutes} m : {time_seconds} s"; proBar_label.Foreground = Brushes.Green; proBar_label.Content = "Файл готов!"; } else { watch.Stop(); // Update the Form to inform the user that the Plugin has failed proBar.Value = 0; proBar_label.Foreground = Brushes.Red; proBar_label.Content = "Ошибка... Процесс не удался"; } } } else { MessageBox.Show(" First you have to choose Naviswork document and paramters file \n" + " Cначала вам нужно выбрать файл Naviswork и текстовый файл с параметрами", "Error"); } } catch (InvalidOperationException exp) { MessageBox.Show(exp.Message, "Invalid Operation Exception"); } catch (ArgumentException exp) { MessageBox.Show(exp.Message, "Argument Exception"); } catch (Exception exp) { MessageBox.Show(exp.Message, "Exception"); } finally { // Close Navisworks if (navisworksApplication != null) { navisworksApplication.Dispose(); } } }