Пример #1
0
 public static void AddFormatToAnyPdfPrinter(string formName, double widthCm, double heightCm)
 {
     foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
     {
         if (printer == "PDFCreator")
         {
             PrinterUtility.AddFormat(printer, formName, widthCm, heightCm);
             return;
         }
         else if (printer.Contains("PDF24"))
         {
             PrinterUtility.AddFormat(printer, formName, widthCm, heightCm);
             return;
         }
         else if (printer == "Adobe PDF")
         {
             PrinterUtility.AddFormat(printer, formName, widthCm, heightCm);
             return;
         }
     }
     PrinterUtility.AddFormat(formName, widthCm, heightCm);
 }
Пример #2
0
        /// <summary>
        /// Ищет и назначает форматы для листов, при необходимости создает форматы в Сервере печати
        /// </summary>
        /// <param name="doc"></param>
        /// <param name="titleBlocks"></param>
        /// <param name="mSheets"></param>
        /// <returns></returns>
        public static string PrintFormatsCheckIn(Document doc, string printerName, List <FamilyInstance> titleBlocks, ref List <MySheet> mSheets)
        {
            PrintManager pManager = doc.PrintManager;

            foreach (MySheet msheet in mSheets)
            {
                Debug.WriteLine(" ");
                Debug.WriteLine(" Проверяется лист " + msheet.sheet.Name);
                double widthMm  = 0;
                double heigthMm = 0;

                List <FamilyInstance> tempTitleBlocks = titleBlocks
                                                        .Where(i => i.get_Parameter(BuiltInParameter.SHEET_NUMBER).AsString() == msheet.sheet.SheetNumber)
                                                        .ToList();

                Debug.WriteLine(" На листе найдено основных надписей: " + tempTitleBlocks.Count.ToString());
                if (tempTitleBlocks.Count == 0)
                {
                    return(" Нет основной надписи на листе " + msheet.sheet.Name);
                }



                msheet.titleBlocks = tempTitleBlocks;
                FamilyInstance titleBlock = tempTitleBlocks.First();
                Debug.WriteLine(" На листе использована основная надпись Id " + titleBlock.Id.IntegerValue.ToString());

                widthMm = titleBlock.get_Parameter(BuiltInParameter.SHEET_WIDTH).AsDouble() * 304.8;
                Debug.WriteLine(" BuiltInParameter.SHEET_WIDTH = " + widthMm.ToString("F3"));

                heigthMm = titleBlock.get_Parameter(BuiltInParameter.SHEET_HEIGHT).AsDouble() * 304.8;
                Debug.WriteLine(" BuiltInParameter.SHEET_HEIGHT = " + heigthMm.ToString("F3"));

                Debug.WriteLine(" Проверяю корректность семейства основной надписи");
                string sizeCheckMessage = SheetSupport.CheckTitleblocSizeCorrects(msheet.sheet, titleBlock);
                if (sizeCheckMessage != "")
                {
                    return(sizeCheckMessage);
                }


                widthMm         = Math.Round(widthMm);
                msheet.widthMm  = widthMm;
                heigthMm        = Math.Round(heigthMm);
                msheet.heigthMm = heigthMm;



                //определяю ориентацию листа
                if (widthMm > heigthMm)
                {
                    Debug.WriteLine(" Это лист вертикальной ориентации");
                    msheet.IsVertical = false;
                }
                else
                {
                    Debug.WriteLine(" Это лист горизонтальной ориентации");
                    msheet.IsVertical = true;
                }

                System.Drawing.Printing.PaperSize winPaperSize = PrinterUtility.GetPaperSize(printerName, widthMm, heigthMm);


                if (winPaperSize != null) //есть подходящий формат
                {
                    Debug.WriteLine(" Найден формат листа Windows: " + winPaperSize.PaperName);
                    pManager = doc.PrintManager;
                    string    paperSizeName  = winPaperSize.PaperName;
                    PaperSize revitPaperSize = PrintSupport.SearchRevitPaperSizeByName(pManager, paperSizeName);


                    if (revitPaperSize == null)
                    {
                        string message = "Не удалось применить формат листа Revit. Попробуйте запустить печать еще раз. Лист: ";
                        message += msheet.sheet.SheetNumber + " : " + msheet.sheet.Name + ". Формат " + paperSizeName;
                        Debug.WriteLine("  " + message);
                        return(message);
                    }

                    Debug.WriteLine(" Найден формат листа Revit: " + revitPaperSize.Name);
                    msheet.revitPaperSize = revitPaperSize;
                }
                else //нет такого формата, нужно добавить в Сервер печати
                {
                    string paperSizeName = widthMm.ToString("F0") + "x" + heigthMm.ToString("F0");
                    Debug.WriteLine("Формат бумаги в Windows не найден! " + paperSizeName);
                    FormCreateCustomFormat formccf = new FormCreateCustomFormat(msheet.sheet.Title, paperSizeName);
                    formccf.ShowDialog();
                    if (formccf.DialogResult != System.Windows.Forms.DialogResult.OK)
                    {
                        return("cancel");
                    }

                    paperSizeName = "UnknownFormat_" + paperSizeName;
                    Debug.WriteLine(" Попытка добавления формата листа в сервер печати " + paperSizeName);

                    try
                    {
                        PrinterUtility.AddFormatToAnyPdfPrinter(paperSizeName, widthMm / 10, heigthMm / 10);
                        Debug.WriteLine(" Формат успешно добавлен!");
                    }
                    catch (Exception ex)
                    {
                        string msg = "Не удалось создать формат " + paperSizeName + ". Возможно, отсутствуют права администратора.\n" + ex.Message;
                        Debug.WriteLine("  " + msg);
                        return(msg);
                    }


                    System.Threading.Thread.Sleep(100);


                    pManager = doc.PrintManager;

                    Debug.WriteLine(" Повторно пытаюсь найти нужный формат листа " + paperSizeName);
                    PaperSize revitPaperSize = PrintSupport.SearchRevitPaperSizeByName(pManager, paperSizeName);
                    if (revitPaperSize == null)
                    {
                        string message = "Обнаружен лист нестандартного формата. Попробуйте запустить печать еще раз. Лист: ";
                        message += msheet.sheet.SheetNumber + " : " + msheet.sheet.Name + ". Формат " + paperSizeName;
                        Debug.WriteLine("  " + message);
                        return(message);
                    }

                    Debug.WriteLine(" Формат найден и успешно применен: " + revitPaperSize.Name);
                    msheet.revitPaperSize = revitPaperSize;
                }
            }

            return(string.Empty);
        }
Пример #3
0
        public FormPrint(Dictionary <string, List <MySheet> > SheetsBase, YayPrintSettings printSettings)
        {
            InitializeComponent();
            //labelVersion.Text = System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
            this.AcceptButton = btnOk;
            this.CancelButton = btnCancel;

            sheetsBaseToPrint = SheetsBase;

            //заполняю treeView
            foreach (var docWithSheets in sheetsBaseToPrint)
            {
                TreeNode docNode     = new TreeNode(docWithSheets.Key);
                bool     haveChecked = false;
                foreach (MySheet sheet in docWithSheets.Value)
                {
                    string   sheetTitle = sheet.ToString();
                    TreeNode sheetNode  = new TreeNode(sheetTitle);
                    sheetNode.Checked = sheet.IsPrintable;
                    if (sheet.IsPrintable)
                    {
                        haveChecked = true;
                    }
                    docNode.Nodes.Add(sheetNode);
                }
                if (haveChecked)
                {
                    docNode.Expand();
                }
                treeView1.Nodes.Add(docNode);
            }

            //заполняю параметры печати
            _printSettings = printSettings;
            textBoxNameConstructor.Text = printSettings.nameConstructor;
            txtBoxOutputFolder.Text     = printSettings.outputFolder;
            checkBoxMergePdfs.Checked   = printSettings.mergePdfs;


            List <string> printers = new List <string>();

            foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
            {
                printers.Add(printer);
            }
            if (printers.Count == 0)
            {
                throw new Exception("Cant find any installed printers");
            }
            comboBoxPrinters.DataSource = printers;

            if (printers.Contains(_printSettings.printerName))
            {
                comboBoxPrinters.SelectedItem = printers.Where(i => i.Equals(_printSettings.printerName)).First();
            }
            else
            {
                string selectedPrinterName = PrinterUtility.GetDefaultPrinter();
                if (!printers.Contains(selectedPrinterName))
                {
                    throw new Exception("Cant find printer " + selectedPrinterName);
                }
                comboBoxPrinters.SelectedItem = printers.Where(i => i.Equals(selectedPrinterName)).First();
            }



            radioButtonPDF.Checked   = comboBoxPrinters.SelectedItem.ToString().Contains("PDF");
            radioButtonPaper.Checked = !radioButtonPDF.Checked;


            if (_printSettings.hiddenLineProcessing == Autodesk.Revit.DB.HiddenLineViewsType.VectorProcessing)
            {
                radioButtonVector.Checked = true;
                radioButtonRastr.Checked  = false;
            }
            else
            {
                radioButtonVector.Checked = false;
                radioButtonRastr.Checked  = true;
            }

            List <Autodesk.Revit.DB.RasterQualityType> rasterTypes =
                Enum.GetValues(typeof(Autodesk.Revit.DB.RasterQualityType))
                .Cast <Autodesk.Revit.DB.RasterQualityType>()
                .ToList();

            comboBoxRasterQuality.DataSource = rasterTypes;
            try
            {
                comboBoxRasterQuality.SelectedItem = _printSettings.rasterQuality;
                //rasterQualityTypes.Where(i => Enum.GetName(typeof(Autodesk.Revit.DB.RasterQualityType), i).Equals()).First();
            }
            catch { }

            List <ColorType> colorTypes = Enum.GetValues(typeof(ColorType))
                                          .Cast <ColorType>()
                                          .ToList();

            comboBoxColors.DataSource        = colorTypes;
            comboBoxColors.SelectedItem      = _printSettings.colorsType;
            textBoxAlwaysColorParamName.Text = _printSettings.alwaysColorParamName;

            checkBoxExportDwg.Checked      = _printSettings.exportToDwg;
            textBoxDwgNameConstructor.Text = _printSettings.dwgNameConstructor;
            List <string> dwgExportSettingsList = _printSettings.dwgProfiles.Select(i => i.Name).ToList();

            comboBoxDwgProfiles.DataSource = dwgExportSettingsList;
            if (dwgExportSettingsList.Contains(_printSettings.selectedDwgExportProfileName))
            {
                comboBoxDwgProfiles.SelectedItem = _printSettings.selectedDwgExportProfileName;
            }
        }