Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Revit Print pdf error

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
1023 Views, 2 Replies

Revit Print pdf error

Revit批量打印错误.jpg

Has anyone ever encountered such a problem?

I used revit2017 API to write a function of printing  sheets to PDF in batch.But sometimes there's a mistake like this.

Revit2017 crash after clicking close button.

Here is My Code:

Tips:Adobe PDF used.

using (RevitDB.Transaction transaction = new RevitDB.Transaction(revitDoc, "PrintPdf"))
            {
                transaction.Start();

                RevitDB.PrintManager printManager = doc.PrintManager;
                printManager.PrintRange = RevitDB.PrintRange.Select;
                RevitDB.ViewSheetSetting viewSheetSetting = printManager.ViewSheetSetting;
                viewSheetSetting.CurrentViewSheetSet.Views = printableViews;
                viewSheetSetting.SaveAs("MyViewSet");          
                                                                
                if (printManager.IsVirtual == RevitDB.VirtualPrinterType.AdobePDF)
                    printManager.CombinedFile = false;
               
                int IndexFlag = 0;

                foreach (RevitDB.View view in printableViews)
                {
                    printManager.PrintToFile = true;
                    printManager.CombinedFile = false;

                    RevitDB.FilteredElementCollector InnerCollector = new RevitDB.FilteredElementCollector(doc, view.Id);
                    RevitDB.ElementClassFilter classFilter = new RevitDB.ElementClassFilter(typeof(RevitDB.FamilyInstance));
                    InnerCollector.WherePasses(classFilter);
                    RevitDB.Element tukuang = null;
                                                 
                    string curFrameName = string.Empty;
                    foreach (RevitDB.Element ele in InnerCollector)
                    {
                        if (ele.Name.Contains("Cam"))
                        {
                            tukuang = ele;
                            curFrameName = ele.Name.Substring(3);
                            break;
                        }
                    }
                    
                    bool IsFindSetting = false;//是否在打印机中找到对应的设置
                    printManager.PrintSetup.CurrentPrintSetting = printManager.PrintSetup.InSession;
                    RevitDB.PaperSize Ps = printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PaperSize;
                    RevitDB.PaperSizeSet PsSet = printManager.PaperSizes;
                    if (curFrameName.Contains("A4"))
                    {
                        foreach (RevitDB.PaperSize p in PsSet)
                        {
                            if (p.Name.ToString() == "A4")
                            {
                                printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PaperSize = p;
                                printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation = RevitDB.PageOrientationType.Portrait;
                                //printManager.PrintSetup.Save();
                                IsFindSetting = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        foreach (RevitDB.PaperSize p in PsSet)
                        {
                            if (p.Name.ToString() == curFrameName)
                            {
                                printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PaperSize = p;
                                printManager.PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation = RevitDB.PageOrientationType.Landscape;
                                //printManager.PrintSetup.Save();
                                IsFindSetting = true;
                                break;
                            }
                        }
                    }
                    if (!IsFindSetting)
                    {
                        if (tukuang == null)
                            FailedPrintName.Add(view.Name);
                        else
                            FailedPrintName.Add(tukuang.Name);
                        continue;
                    }
                    printManager.PrintSetup.SaveAs((IndexFlag++).ToString());
                    printManager.Apply();
                   
                    printManager.SubmitPrint(view);
                    
                    RevitDB.Element TitleBlock = null;
                    string SheetCode = string.Empty;
                    foreach (RevitDB.Element ele in InnerCollector)
                    {
                        if (ele.Name.Equals("标准"))
                        {
                            TitleBlock = ele;
                            break;
                        }
                    }
                    if (TitleBlock != null)
                    {
                        SheetCode = TitleBlock.GetParameters("图号")[0].AsString();
                        if (!string.IsNullOrEmpty(SheetCode))
                        {
                            SuccessPrintSheetNames.Add(SheetCode);
                        }
                        else
                        {
                            SuccessPrintSheetNames.Add(view.Name);
                        }
                    }
                }
                transaction.RollBack();
                //提示失败的图纸,并且在这里用对话框卡主进程,防止移动图纸时正处于打开状态
                if (FailedPrintName.Count() != 0)
                {
                    string Msg = string.Empty;
                    foreach (string str in FailedPrintName)
                    {
                        Msg += str + ".  ";
                    }
                    RevitUI.TaskDialog Dlg = new RevitUI.TaskDialog("提示");
                    Dlg.MainInstruction = "未打印图纸";
                    Dlg.MainContent = Msg;
                    Dlg.Show();
                }
                else
                {
                    RevitUI.TaskDialog Dlg = new RevitUI.TaskDialog("提示");
                    Dlg.MainInstruction = "已完成所有打印图纸!";
                    //Dlg.MainContent = Msg;
                    Dlg.Show();
                }
                Application.DoEvents();//等待执行,防止系统打印机未完成就开始移动图纸报错
                //移动pdf图纸,并重命名
                MovePdfFile(SuccessPrintSheetNames, SheetNames);
            }
2 REPLIES 2
Message 2 of 3
gopinathrbe
in reply to: Anonymous

Try to print after making view as the Active View

Message 3 of 3
Anonymous
in reply to: gopinathrbe

Thank you very much.It's actually works!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report