public static void ExportSchedules(Document curDoc)
        {
            int counter = 0;

            ViewScheduleExportOptions curOptions = new ViewScheduleExportOptions();

            curOptions.ColumnHeaders = ExportColumnHeaders.MultipleRows;

            //open form
            using (frmForm curForm = new frmForm(curDoc)) {
                //show form
                curForm.ShowDialog();

                if (curForm.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                {
                    //Cancel pressed - do something
                    return;
                }
                else
                {
                    //check if folder path selected
                    if (!string.IsNullOrEmpty(curForm.getSelectedFolder()))
                    {
                        List <string> schedList = curForm.getSelectedSchedules();

                        //loop through each schedule and export
                        foreach (string tmpSched in schedList)
                        {
                            //get selected schedule
                            ViewSchedule curSched   = getScheduleByName(curDoc, tmpSched);
                            string       exportFile = curSched.Name + ".csv";

                            try {
                                curSched.Export(curForm.getSelectedFolder(), exportFile, curOptions);
                                counter = counter + 1;
                            } catch (Exception ex) {
                                TaskDialog.Show("Error", "Could not export schedule " + curSched.Name);
                            }
                        }
                    }
                    else
                    {
                        TaskDialog.Show("Error", "Please select an export folder path.");
                        return;
                    }
                }
            }

            //alert user
            TaskDialog.Show("Complete", "Exported " + counter + " schedules.");
        }
        private static string ExportScheduleCSV(ViewSchedule schedule, string scheduleExportDirectory)
        {
            ViewScheduleExportOptions exportOptions = new ViewScheduleExportOptions()
            {
                ColumnHeaders = ExportColumnHeaders.MultipleRows,
                Title         = false
            };

            var scheduleExportPath = Path.Combine(scheduleExportDirectory, string.Format("{0}.csv", RemoveInvalidCharacters(schedule.Name)));

            schedule.Export(scheduleExportDirectory, Path.GetFileName(scheduleExportPath), exportOptions);

            return(scheduleExportPath);
        }
Esempio n. 3
0
        private bool ExportViewSchedule(ViewSchedule viewSchedule)
        {
            var result = false;

            try
            {
                var options = new ViewScheduleExportOptions
                {
                    ColumnHeaders = ExportColumnHeaders.OneRow,
                    Title         = false
                };

                var centralPath = GetCentralPath(m_doc);
                if (!string.IsNullOrEmpty(centralPath))
                {
                    var directoryName = Path.GetDirectoryName(centralPath);
                    using (var trans = new Transaction(m_doc))
                    {
                        try
                        {
                            trans.Start("Export Schedule");
                            if (directoryName != null)
                            {
                                TextFilePath = Path.Combine(directoryName, viewSchedule.Name + extension);
                                if (File.Exists(TextFilePath))
                                {
                                    File.Delete(TextFilePath);
                                }
                                viewSchedule.Export(directoryName, viewSchedule.Name + extension, options);
                            }
                            trans.Commit();
                        }
                        catch { trans.RollBack(); }
                    }
                }

                if (File.Exists(TextFilePath))
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                Log.AppendLog(LogMessageType.EXCEPTION, ex.Message);
                result = false;
            }
            return(result);
        }
Esempio n. 4
0
        } = 0;                                                      // number of schedules exported
        #endregion

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            // add event handler for when the app does not find the ArpUtilies.dll assembly
            //AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

            #region variables
            UIApplication uiapp = commandData.Application;
            Autodesk.Revit.ApplicationServices.Application app = uiapp.Application;
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document   doc   = uidoc.Document;

            // element selection
            ICollection <ElementId> selIds = uidoc.Selection.GetElementIds();

            // excel export settings
            ViewScheduleExportOptions export_options = new ViewScheduleExportOptions();

            // paths to check Excel install
            string excelPaths_FileName = "OpenInExcel_UserPaths.txt";
            string assemblyPath        = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string excelPaths_FilePath = Path.Combine(assemblyPath, excelPaths_FileName);

            // paths to export schedule
            string temp_folder = Path.GetTempPath();
            #endregion

            // check if file exists
            if (!Data.Helpers.CheckFileExists(excelPaths_FilePath))
            {
                warningMsgMain = "File missing";
                warningMsgBody = string.Format("Could not find the File: {0}{0}{1} in {2}{0}{0}Contact [email protected] for help.",
                                               Environment.NewLine,
                                               excelPaths_FileName,
                                               assemblyPath);
                using (UI.Info.Form_Warning thisForm = new UI.Info.Form_Warning())
                {
                    thisForm.ShowDialog();
                }
                return(Result.Cancelled);
            }

            // check if excel is installed in usual paths
            if (!Data.Helpers.CheckExcelInstall(excelPaths_FilePath))
            {
                warningMsgMain = "Excel not found";
                warningMsgBody = string.Format("Could not find Excel installed in the usual paths.{0}{0}" +
                                               "Please install Excel or add your Excel path to the OpenInExcel_UserPaths.txt " +
                                               "(located in {1}) and try again.{0}{0}" +
                                               "Contact [email protected] for help.", Environment.NewLine, assemblyPath);

                using (UI.Info.Form_Warning thisForm = new UI.Info.Form_Warning())
                {
                    thisForm.ShowDialog();
                }
                return(Result.Cancelled);
            }

            // if there are not schedules selected or open throw a message
            if (!Data.Helpers.GetSelectedSchedules(doc, selIds).Any())
            {
                using (UI.Info.Form_Info1 thisForm = new UI.Info.Form_Info1())
                {
                    thisForm.ShowDialog();
                }
                return(Result.Cancelled);
            }

            // go ahead and export the selected schedules
            if (Data.Helpers.GetSelectedSchedules(doc, selIds).Any())
            {
                // start counters. For this application Use Time and Execution time will be the same.
                useTime.Start();

                Random randomNumber = new Random();

                // create excel name with schedule name + random number
                foreach (View v in Data.Helpers.GetSelectedSchedules(doc, selIds))
                {
                    Ana_NoOfExports += 1;   // count number of schedules exported

                    string scheduleName       = Data.Helpers.ReplaceIllegalCharaters(v.Name);
                    string txtFileName        = scheduleName + "_" + randomNumber.Next(1000) + ".txt";
                    string scheduleExportPath = Path.Combine(temp_folder, txtFileName);

                    // export schedule to .txt
                    ViewSchedule viewSchedule = v as ViewSchedule;
                    viewSchedule.Export(temp_folder, txtFileName, export_options);

                    // open .txt schedule with excel
                    Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
                    excelApp.Visible = true; // make the running excel instance visible
                    Workbook wb = excelApp.Workbooks.Open(scheduleExportPath);
                }
                useTime.Stop();
                UseTimeElapseS  = useTime.Elapsed.Seconds.ToString();
                ExecTimeElapseS = UseTimeElapseS;
            }
            //try
            //{
            //    Utilities.GetAnalyticsCSV(doc, app);
            //    Data.Helpers.ScheduleToExcelAnalytics(doc, app, UseTimeElapseS, ExecTimeElapseS, Ana_NoOfExports);
            //}
            //catch (Exception)
            //{
            //}
            return(Result.Succeeded);
        }
Esempio n. 5
0
        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_viewModel.ExportExcelFolderPath))
            {
                _viewModel.ExportExcelFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            }

            #region Code

            List <ViewScheduleExtension> viewScheduleExport = new List <ViewScheduleExtension>();
            foreach (ViewScheduleExtension viewSchedule in _viewModel.AllViewSchedules)
            {
                if (viewSchedule.IsExport)
                {
                    viewScheduleExport.Add(viewSchedule);
                }
            }

            if (viewScheduleExport.Count == 0)
            {
                MessageBox.Show("Please select the Schedules which will be exported!");
                return;
            }

            if (_viewModel.IsMultiWorkbooks)
            {
                foreach (var vse in viewScheduleExport)
                {
                    ViewSchedule selectedSchedule = vse.ViewSchedule;
                    string       filePath         = Path.Combine(_viewModel.ExportExcelFolderPath, selectedSchedule.Name + ".xlsx");

                    try
                    {
                        string filePathTxt = Path.Combine(_viewModel.ExportExcelFolderPath,
                                                          string.Concat(selectedSchedule.Name, ".txt"));

                        List <string> scheduleExportFieldName = selectedSchedule.GetNameOfFields();

                        ViewScheduleExportOptions viewScheduleExportOption = new ViewScheduleExportOptions();
                        selectedSchedule.Export(_viewModel.ExportExcelFolderPath, Path.GetFileName(filePathTxt),
                                                viewScheduleExportOption);

                        DataTable dataTable = ExcelUtils.ReadFile(filePathTxt, scheduleExportFieldName.Count);

                        FileInfo existingFile = new FileInfo(filePath);
                        using (var package = new ExcelPackage(existingFile))
                        {
                            ExcelUtils.ExportDataTableToExcel(package, dataTable,
                                                              selectedSchedule.Name, filePath);
                        }

                        File.Delete(filePathTxt);
                    }
                    catch (Exception)
                    {
                    }
                }
            }
            else
            {
                string title = _viewModel.Doc.Title;
                title = title.Substring(0, title.Length - 4);
                string filePath = Path.Combine(_viewModel.ExportExcelFolderPath,
                                               string.Concat(title, ".xlsx"));
                FileInfo existingFile = new FileInfo(filePath);
                using (var package = new ExcelPackage(existingFile))
                {
                    foreach (var vse in viewScheduleExport)
                    {
                        ViewSchedule selectedSchedule = vse.ViewSchedule;
                        try
                        {
                            string filePathTxt = Path.Combine(_viewModel.ExportExcelFolderPath,
                                                              string.Concat(selectedSchedule.Name, ".txt"));

                            List <string> scheduleExportFieldName = selectedSchedule.GetNameOfFields();

                            ViewScheduleExportOptions viewScheduleExportOption = new ViewScheduleExportOptions();
                            selectedSchedule.Export(_viewModel.ExportExcelFolderPath, Path.GetFileName(filePathTxt),
                                                    viewScheduleExportOption);

                            DataTable dataTable = ExcelUtils.ReadFile(filePathTxt, scheduleExportFieldName.Count);

                            ExcelUtils.ExportDataTableToExcel(package, dataTable,
                                                              selectedSchedule.Name, filePath);

                            File.Delete(filePathTxt);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }

            if (MessageBox.Show("Do you want to open folder that store exported files?", "Open folder",
                                MessageBoxButton.YesNo)
                == MessageBoxResult.Yes)
            {
                try
                {
                    Process.Start(_viewModel.ExportExcelFolderPath);
                }
                catch (Exception)
                {
                }
            }

            #endregion

            DialogResult = true;
            Close();
        }