gsteven 发表于 2009-10-2 15:00:00

如何批量打印图纸?已解决!如何不打开图形打印?

有一个文件夹下1000张图,需要把他们用adobe pdf打印机打印到另外一个文件夹中,
打印范围是(0,0)(420,297),A3纸,横向打印,打印后pdf名字取dwg的名字,去掉dwg,改成pdf,如1.dwg,打印完了以后就是1.pdf。
主要是如何打印一张图纸,那么1000张也就好办了。最后能够不用打开图纸,用database读取dwg或dxf,然后打印。
PlottingServices里面的东西太多了,不知道怎么用。
谢谢!
下面的是VBA代码,不知道C#怎么用
Public Sub PlotWindow()
    ' 确保当前布局是模型空间
    ThisDrawing.ActiveLayout = ThisDrawing.Layouts.Item("Model")
   
    ' 设置打印设备
    ThisDrawing.ActiveLayout.ConfigName = "DWF6 ePlot.pc3"
   
    ' 设置打印比例为"布满图纸"
    ThisDrawing.ActiveLayout.StandardScale = acScaleToFit
   
    ' 设置图纸类型
    ThisDrawing.ActiveLayout.CanonicalMediaName = "ISO_A4_(210.00_x_297.00_MM)"
   
    ' 让在前台进行打印
    ThisDrawing.SetVariable "BACKGROUNDPLOT", 0
   
    Dim objPlot As AcadPlot
    Set objPlot = ThisDrawing.Plot
   
    ' 设置打印窗口
    Dim minPoint(0 To 1) As Double, maxPoint(0 To 1) As Double
    SetPoint2d minPoint, 0, 0
    SetPoint2d maxPoint, 800, 600
    ThisDrawing.ActiveLayout.SetWindowToPlot minPoint, maxPoint
   
    ' 设置打印类型
    ThisDrawing.ActiveLayout.PlotType = acWindow
   
    objPlot.PlotToFile "C:\test.dwf"
   
    ' 恢复系统变量的值
    ThisDrawing.SetVariable "BACKGROUNDPLOT", 2
End Sub
以下是解决的代码,速度不算慢,100张图7分钟,每张图都是先打开,再打印完,再关闭的。
能否这样,不打开图形,采用database.readdwg或者database.dxfin的方法进行打印?怎么实现?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.IO;
using Excel;// =Microsoft.Office.Interop.Excel ;
using VB = Microsoft.VisualBasic;//用于激活窗口
using System.Runtime.InteropServices;//使用Marshal获得进程
using System.Reflection;//使用Missing.value
using FS = Scripting;//文件系统
using Autodesk.AutoCAD.PlottingServices;
using System.Collections.Specialized;
namespace GWDwgFrame
{
    public partial class DwgFrame : Form
    {
      
      private Excel.Application ExcelApp;//excel工程
      string excelPath;//Excel路径
      Excel.Workbook operBook;//操作book
      Excel.Worksheet operSheet;//操作sheet
      int iRow;//Excel循环数
      int finish;//完成数1-100
      string folderPath="";//文件夹路径
      string framePath="";//图框路径
      string operFilePath;//当前操作的文件路径
      string frameBlkRecName = "LoopNo";
      int iDwg;//操作文件的循环计数
      FS.FileSystemObject fso=new FS.FileSystemObject();
      FS.Folder dwgFolder;
      FS.Files dwgFileGroup;
      FS.File dwgFile;
      public System.Collections.ArrayList dwgFilesRoute=new System.Collections.ArrayList();//所有文件的路径
      public int dwgCount;//文件数计算
      string fileType="DXF";//文件类型,DXF或者DWG
      ObjectIdCollection frameIDCol;//复制块定义所需ID集合
      IdMapping acIdMap;// //复制数据库函数成员
      Autodesk.AutoCAD.DatabaseServices.Database operDb;//操作数据库
      Autodesk.AutoCAD.DatabaseServices.Database frameDb;//图框数据库
      //Document operDwgDoc;//操作图形文档
      string frameName;//图框名字,带后缀.dwg
      string dwgRefName;//外部参照名字
      string[] loopHead = new string[] { "文件名","回路名", "EPC图号", "业主图号" };
      string[] loopInfor = new string;//分别是回路名,回路名,EPC图号,业主图号
      Autodesk.AutoCAD.DatabaseServices.AttributeCollection attCollec;//快参照属性集合
      Autodesk.AutoCAD.DatabaseServices.AttributeReference attOper;//属性参照
      DwgVersion dwgSaveVer=DwgVersion.AC1015;//保存的版本2000
      public string devName;//打印设备
      public string devPaper;//打印图纸
      public bool plotToFile = false;//是否打印到图纸
      public PlotRotation rotAng;//旋转角度
//获得可用的打印机
private void DwgFrameLoad(object sender, EventArgs e)
      {
            //comboBoxFileType.SelectedIndex = 0;
            //comboBoxFileType.Text = "DXF";
            //comboBoxDwgVer.SelectedIndex = 1;//默认版本2000
            Document doc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PlotSettingsValidator psv =PlotSettingsValidator.Current;
            StringCollection devlist =psv.GetPlotDeviceList();
            for (int i = 0; i
      }
//获得该打印机图纸
      private void getPaperSize(object sender, EventArgs e)
      {
            comboBoxPaperSize.Items.Clear();
            devName = comboBoxPrintDev.Text;
            Document doc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;
            PlotSettingsValidator psv =PlotSettingsValidator.Current;
            PlotSettings ps = new PlotSettings(true);
            psv.SetPlotConfigurationName(ps, devName, null);
            psv.RefreshLists(ps);
            StringCollection medlist =psv.GetCanonicalMediaNameList(ps);
            for (int i = 0; i
      }
      //打印图纸大小
      private void setPaperSize(object sender, EventArgs e)
      {
            devPaper = comboBoxPaperSize.Text;
      }
      //打印角度
      private void rotateAngle(object sender, EventArgs e)
      {
         string rot=comboBoxRotate.Text;
            if (rot=="0" )
            {
                rotAng = PlotRotation.Degrees000;
            }
            if (rot=="90" )
            {
                rotAng = PlotRotation.Degrees090;
            }
            if (rot=="180" )
            {
                rotAng = PlotRotation.Degrees180;
            }
            if (rot=="270" )
            {
                rotAng = PlotRotation.Degrees270;
            }
      }
      //是否打印到文件
      private void plottoFile(object sender, EventArgs e)
      {
            plotToFile=checkBoxPrintFile.Checked;
            if (plotToFile == true)
            {
                folderBrowserDialog1.SelectedPath = "";
                folderBrowserDialog1.Description = "请选择打印文件的存放文件夹";
                folderBrowserDialog1.ShowDialog();
               
                if (fso.FolderExists(folderBrowserDialog1.SelectedPath) == false)
                {
                  return;
                }
                folderPath = folderBrowserDialog1.SelectedPath;
                textBoxPintFolder.Text = "文件夹路径:" + folderPath;
                textBoxPintFolder.Visible = true;
                //dwgFolder = fso.GetFolder(folderPath);
                if (folderPath.Substring(folderPath.Length - 1, 1) == "\\")
                {
                  folderPath = folderPath ;
                }
                else
                {
                  folderPath = folderPath + "\\";
                }
            }
      }
      //开始打印
      private void buttonPrint_Click(object sender, EventArgs e)
      {
            string printFile;
            //PlotProgressDialog ppd=new   PlotProgressDialog(false, 1, true);
            //PlotEngine pe =                        PlotFactory.CreatePublishEngine();
            for (iDwg = 0; iDwg
                Transaction tr =
                  db.TransactionManager.StartTransaction();
                using (tr)
                {
                  // We'll be plotting the current layout
                  BlockTableRecord btr =
                      (BlockTableRecord)tr.GetObject(
                        db.CurrentSpaceId,
                        OpenMode.ForRead
                      );
                  Layout lo =
                      (Layout)tr.GetObject(
                        btr.LayoutId,
                        OpenMode.ForRead
                      );
                  // We need a PlotInfo object
                  // linked to the layout
                  PlotInfo pi = new PlotInfo();
                  pi.Layout = btr.LayoutId;
                  // We need a PlotSettings object
                  // based on the layout settings
                  // which we then customize
                  PlotSettings ps =
                      new PlotSettings(lo.ModelType);
                  ps.CopyFrom(lo);
                  // The PlotSettingsValidator helps
                  // create a valid PlotSettings object
                  PlotSettingsValidator psv =
                      PlotSettingsValidator.Current;
                  // We'll plot the extents, centered and
                  // scaled to fit
                  psv.SetPlotRotation(ps, rotAng);
                  psv.SetPlotType(
                      ps,
                      Autodesk.AutoCAD.DatabaseServices.PlotType.Extents
                  );
                  psv.SetUseStandardScale(ps, true);
                  psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
                  psv.SetPlotCentered(ps, true);
                  // We'll use the standard DWF PC3, as
                  // for today we're just plotting to file
                  psv.SetPlotConfigurationName(
                      ps,
                      devName,//"Adobe PDF",
                      devPaper//"A3"
                  );
                  // We need to link the PlotInfo to the
                  // PlotSettings and then validate it
                  pi.OverrideSettings = ps;
                  PlotInfoValidator piv =
                      new PlotInfoValidator();
                  piv.MediaMatchingPolicy =
                      MatchingPolicy.MatchEnabled;
                  piv.Validate(pi);
                  // A PlotEngine does the actual plotting
                  // (can also create one for Preview)
                  //if (PlotFactory.ProcessPlotState ==
                  //    ProcessPlotState.NotPlotting)
                  PlotEngine pe =
                      PlotFactory.CreatePublishEngine();
                  using (pe)
                  {
                            // Create a Progress Dialog to provide info
                            // and allow thej user to cancel
                        PlotProgressDialog ppd = new PlotProgressDialog(false, 1, true);
                            using (ppd)
                            {
                              ppd.set_PlotMsgString(
                                  PlotMessageIndex.DialogTitle,
                                  "正在打印"+operFilePath//"Custom Plot Progress"
                              );
                              ppd.set_PlotMsgString(
                                  PlotMessageIndex.CancelJobButtonMessage,
                                  "取消当前打印"//"Cancel Job"
                              );
                              ppd.set_PlotMsgString(
                                  PlotMessageIndex.CancelSheetButtonMessage,
                                  "取消本页打印"//"Cancel Sheet"
                              );
                              ppd.set_PlotMsgString(
                                  PlotMessageIndex.SheetSetProgressCaption,
                                  "打印集合进度"//"Sheet Set Progress"
                              );
                              ppd.set_PlotMsgString(
                                  PlotMessageIndex.SheetProgressCaption,
                                  "打印页进度"//"Sheet Progress"
                              );
                              ppd.LowerPlotProgressRange = 0;
                              ppd.UpperPlotProgressRange = 100;
                              ppd.PlotProgressPos = 0;
                              // Let's start the plot, at last
                              ppd.OnBeginPlot();
                              ppd.IsVisible = true;
                              pe.BeginPlot(ppd, null);
                              // We'll be plotting a single document
                              pe.BeginDocument(
                                  pi,
                                  doc.Name,
                                  null,
                                  1,
                                  plotToFile,//true, // Let's plot to file
                                  printFile//"c:\\test-output"
                              );
                              // Which contains a single sheet
                              ppd.OnBeginSheet();
                              ppd.LowerSheetProgressRange = 0;
                              ppd.UpperSheetProgressRange = 100;
                              ppd.SheetProgressPos = 0;
                              PlotPageInfo ppi = new PlotPageInfo();
                              pe.BeginPage(
                                  ppi,
                                  pi,
                                  true,
                                  null
                              );
                              pe.BeginGenerateGraphics(null);
                              pe.EndGenerateGraphics(null);
                              // Finish the sheet
                              pe.EndPage(null);
                              ppd.SheetProgressPos = 100;
                              ppd.OnEndSheet();
                              // Finish the document
                              pe.EndDocument(null);
                              // And finish the plot
                              ppd.PlotProgressPos = 100;
                              ppd.OnEndPlot();
                              pe.EndPlot(null);
                              
                            }
                        }//using pe
                  
                  
                }
                for (; ; )
                {
                  this.Update();
                  //if (ppd.IsDisposed)
                  if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
                  {
                        break;
                  }
                }
                //for (int i = 0; i
            
      }


雪山飞狐_lzh 发表于 2009-10-2 15:08:00

先看看这里吧:

gsteven 发表于 2009-10-5 18:47:00

我看了那些kean的文章,实在是太复杂了,就这么一个打印功能,搞的如此复杂,真实有点怕了!

游天居士 发表于 2009-10-9 23:52:00

我也想搞一个.以前用VBA搞了一个.想改成C#的.可是就是不知怎么改.

menethil 发表于 2017-12-5 14:03:00

有办法把所有PDF合并成一个吗

Leo1980 发表于 2017-12-6 09:50:00


两个方法:
1、pdfFactory pro
2、用第三方pdf操作库
页: [1]
查看完整版本: 如何批量打印图纸?已解决!如何不打开图形打印?