private static PrintSetting LoadRevitPrintSetting( Document doc, string size, PrintManager pm, string printerName, bool forceRaster, ExportLog log) { log.AddMessage(Resources.MessageAttemptingToLoadRevitPrintSettings + size); PrintSetting ps = GetPrintSettingByName(doc, size, forceRaster); if (ps == null) { log.AddError(null, Resources.ErrorRetrievingRevitPrintSettingsFAILED); return(null); } log.AddMessage(Resources.MessageUsingPrinter + printerName); if (!SetPrinterByName(doc, printerName, pm)) { log.AddError(null, Resources.MessageCannotSetPrinter + printerName); return(null); } return(ps); }
////[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] public static bool PrintToDevice( Document doc, string size, PrintManager pm, string printerName, bool forceRaster, ExportLog log) { if (pm == null) { return(false); } PrintSetting ps = LoadRevitPrintSetting(doc, size, pm, printerName, forceRaster, log); if (ps == null) { return(false); } var t = new Transaction(doc, Resources.ApplyPrintSettings); t.Start(); try { if (ps.IsValidObject) { pm.PrintSetup.CurrentPrintSetting = ps; } else { if (log != null) { log.AddWarning(null, Resources.WarningPrintSetupReadOnly); } } pm.PrintRange = PrintRange.Current; pm.PrintSetup.CurrentPrintSetting.PrintParameters.PaperPlacement = PaperPlacementType.Margins; pm.PrintSetup.InSession.PrintParameters.PaperPlacement = PaperPlacementType.Margins; pm.PrintSetup.CurrentPrintSetting.PrintParameters.MarginType = MarginType.UserDefined; pm.PrintSetup.InSession.PrintParameters.MarginType = MarginType.UserDefined; pm.PrintSetup.CurrentPrintSetting.PrintParameters.UserDefinedMarginX = 0; pm.PrintSetup.InSession.PrintParameters.UserDefinedMarginX = 0; pm.PrintSetup.CurrentPrintSetting.PrintParameters.UserDefinedMarginY = 0; pm.PrintSetup.InSession.PrintParameters.UserDefinedMarginY = 0; pm.PrintToFile = false; pm.Apply(); t.Commit(); return(true); } catch (Exception ex) { System.Diagnostics.Debug.Print(ex.Message); if (log != null) { log.AddError(null, ex.ToString()); } t.RollBack(); return(false); } }
public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements) { if (commandData == null) { return(Result.Failed); } if (!System.IO.Directory.Exists(Constants.DefaultExportDirectory)) { System.IO.Directory.CreateDirectory(Constants.DefaultExportDirectory); } if (string.IsNullOrEmpty(FileUtilities.GetCentralFileName(commandData.Application.ActiveUIDocument.Document))) { WindowManager.ShowMessageBox("FAIL", "Please save the file before continuing"); return(Result.Failed); } var uidoc = commandData.Application.ActiveUIDocument; if (uidoc == null) { return(Result.Failed); } var views = new List <ViewSheet>(); if (uidoc.Document.ActiveView.ViewType == ViewType.ProjectBrowser) { var s = uidoc.Selection.GetElementIds(); foreach (var id in s) { var projectBrowserView = uidoc.Document.GetElement(id); if (projectBrowserView is View) { var v = (View)projectBrowserView; if (v.ViewType == ViewType.ProjectBrowser) { continue; } if (v is ViewSheet) { views.Add((ViewSheet)v); continue; } } } } // Deselect all elements before continuing so they don't appear incorrectly uidoc.Selection.SetElementIds(new List <ElementId>()); var manager = new Manager(uidoc); var log = new ExportLog(); if (views == null) { views = new List <ViewSheet>(); } var vm = new ViewModels.SCexportViewModel(manager, views); var wm = WindowManager; wm.ShowDialog(vm, null, ViewModels.SCexportViewModel.DefaultWindowSettings); if (vm.CloseStatus != ViewModels.SCexportViewModel.CloseMode.Exit) { string exportType = string.Empty; switch (vm.CloseStatus) { case ViewModels.SCexportViewModel.CloseMode.Export: exportType = "Exporting"; break; case ViewModels.SCexportViewModel.CloseMode.Print: case ViewModels.SCexportViewModel.CloseMode.PrintA3: case ViewModels.SCexportViewModel.CloseMode.PrintA2: exportType = "Printing"; break; } var progressVm = new ViewModels.ProgressMonitorViewModel { MaximumValue = vm.SelectedSheets.Count, Value = 0 }; log.Clear(); log.Start(exportType + " Started."); WindowManager.ShowWindow(progressVm, null, ViewModels.ProgressMonitorViewModel.DefaultWindowSettings); if (manager.SaveHistory) { RecentExport.Save(manager, vm.SelectedSheets); } foreach (var sheet in vm.SelectedSheets) { progressVm.ProgressSummary += @" --> " + exportType + @" " + sheet.FullExportName + "..."; switch (vm.CloseStatus) { case ViewModels.SCexportViewModel.CloseMode.Export: manager.ExportSheet(sheet, log); break; case ViewModels.SCexportViewModel.CloseMode.Print: manager.Print(sheet, manager.PrinterNameLargeFormat, 1, log); break; case ViewModels.SCexportViewModel.CloseMode.PrintA3: manager.Print(sheet, manager.PrinterNameA3, 3, log); break; case ViewModels.SCexportViewModel.CloseMode.PrintA2: manager.Print(sheet, manager.PrinterNameLargeFormat, 2, log); break; default: return(Result.Succeeded); } progressVm.Value++; string niceTime = string.Format( System.Globalization.CultureInfo.CurrentCulture, "OK [ time {0:hh\\.mm\\:ss} total {1:hh\\.mm\\:ss} ~remaining {2:hh\\.mm\\:ss}]", log.LastItemElapsedTime, log.TimeSinceStart, System.TimeSpan.FromTicks(log.TimeSinceStart.Ticks / progressVm.Value * (progressVm.MaximumValue - progressVm.Value))); progressVm.ProgressSummary += niceTime + System.Environment.NewLine; if (progressVm.CancelPressed) { break; } } log.Stop("Finished"); progressVm.Stop(log); progressVm.ProcessComplete = true; } if (manager.ShowExportLog || log.Errors > 0) { var exportLogViewModel = new ViewModels.ExportLogViewModel(log); WindowManager.ShowDialog(exportLogViewModel, null, ViewModels.ExportLogViewModel.DefaultWindowSettings); } return(Result.Succeeded); }