简单C#打印程序并注释
private PlotInfo SetPlotInfo(Document doc, Extents2d exArea, string strPrinter, int iSize)
{
Database db = doc.Database;
Editor ed = doc.Editor;
using (Transaction tr = db.TransactionManager.StartTransaction()) //开启事务
{
//设置布局管理器为当前布局管理器
LayoutManager layoutMan = LayoutManager.Current;
//获取当前布局,用GetObject的方式
Layout currentLayout = tr.GetObject(layoutMan.GetLayoutId(layoutMan.CurrentLayout), OpenMode.ForRead) as Layout;
//创建一个PlotInfo类,从布局获取信息
PlotInfo pi = new PlotInfo();
pi.Layout = currentLayout.ObjectId;
//从布局获取一个PlotSettings对象的附本
PlotSettings ps = new PlotSettings(currentLayout.ModelType);
ps.CopyFrom(currentLayout);
//创建PlotSettingsValidator对象,通过它来改变PlotSettings.
PlotSettingsValidator psv = PlotSettingsValidator.Current;
//设置打印窗口范围
psv.SetPlotWindowArea(ps, exArea);
psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
//设置打印比例为布满图纸
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
//设置居中打印
psv.SetPlotCentered(ps, true);
//设置横向打印还是纵向打印
if ((exArea.MaxPoint.X - exArea.MinPoint.X) > (exArea.MaxPoint.Y - exArea.MinPoint.Y))
{
psv.SetPlotRotation(ps, PlotRotation.Degrees090); //横向
}
else
{
psv.SetPlotRotation(ps, PlotRotation.Degrees000); //纵向
}
//设置打印设备
if (iSize == 3)
{
psv.SetPlotConfigurationName(ps, strPrinter, null); //打印A3
StringCollection sMediasA3 = psv.GetCanonicalMediaNameList(ps);
bool isA3 = false;
int i = 0;
for(i=0;i<sMediasA3.Count;i++)
{
if (sMediasA3.ToLower().Contains("a3") == true)
{
isA3 = true;
break;
}
}
if (isA3 == true)
{
psv.SetPlotConfigurationName(ps, strPrinter, sMediasA3);
}
else
{
MessageBox.Show("您选择的打印机不支持A3!请重新选择A3打印机");
return null;
}
}
else
{
psv.SetPlotConfigurationName(ps, strPrinter, null); //打印A4
StringCollection sMediasA4 = psv.GetCanonicalMediaNameList(ps);
bool isA4 = false;
int j = 0;
for (j = 0; j < sMediasA4.Count; j++)
{
if (sMediasA4.ToLower().Contains("a4") == true)
{
isA4 = true;
break;
}
}
if (isA4 == true)
{
psv.SetPlotConfigurationName(ps, strPrinter, sMediasA4);
}
else
{
MessageBox.Show("您选择的A4打印机不支持A4,请重新选择A4打印机!");
}
}
//重写打印信息
pi.OverrideSettings = ps;
//确认打印信息
PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
piv.Validate(pi);
return pi;
}
}
private int Plot(Document doc,Extents2d exArea, string strPrinter, int iSize)//打印单张图纸
{
PlotInfo pi = SetPlotInfo(doc,exArea,strPrinter,iSize);
if (pi == null)
{
return 1;
}
//设置系统环境变量
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("BACKGROUNDPLOT", 0);
try
{
//检查打印状态
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
//开户引擎
using (PlotEngine pe = PlotFactory.CreatePublishEngine())
{
PlotProgressDialog ppDialog = new PlotProgressDialog(false, 1, true);
using (ppDialog)
{
//显示进程对话框
ppDialog.OnBeginPlot();
ppDialog.IsVisible = true;
//开始打印布局
pe.BeginPlot(ppDialog, null);
//sw.WriteLine(DateTime.Now + " pe.BeginPlot()开始执行.");
//显示当前打印相关信息
pe.BeginDocument(pi, doc.Name, null, 1, false, null);
//打印第一个视口
PlotPageInfo ppInfo = new PlotPageInfo();
//sw.WriteLine(DateTime.Now + " 创建一个PlotPageInfo类");
pe.BeginPage(ppInfo, pi, true, null); //此处第三个参数至为关键,如果设为false则会不能执行.
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
//完成打印视口
pe.EndPage(null);
ppDialog.OnEndSheet();
//完成文档打印
pe.EndDocument(null);
//完成打印操作
ppDialog.OnEndPlot();
pe.EndPlot(null);
}
}
}
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
MessageBox.Show(ex.ToString());
}
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("BACKGROUNDPLOT", 2);
//sw.WriteLine(DateTime.Now + " 设置环境变量BACKGROUNDPLOT = 2.\n\n\n");
//sw.Close();
return 0;
}
页:
[1]