C#打印程序出错,是在AUTOCAD开发手册上操的。
C#打印程序出错,是在AUTOCAD开发手册上操的。这是我做的一个批量打印程序,打印那一块全是在手册上操的。编译没问题,运行的时候有三行代码发生异常,且这三行代码都是原封不动从手册上操的。改来改去也没搞懂什么原因。请各位大侠指教!万分感谢!using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.PlottingServices;
namespace AllBlock
{
public class Class1
{
//public void Init()
//{
public static Document doc = Application.DocumentManager.MdiActiveDocument;
public static Database db = doc.Database;
public static Editor ed = doc.Editor;
//}
public void ba()
{
//Init();
TypedValue[] tv = new TypedValue;
tv.SetValue(new TypedValue((int)DxfCode.BlockName, "a4"),0);
// 赋值过滤条件给 SelectionFilter 对象 Assign the filter criteria to a SelectionFilter object
SelectionFilter acSelFtr = new SelectionFilter(tv);
// 要求在图形区域中选择对象 Request for objects to be selected in the drawing area
PromptSelectionResult acSSPrompt = ed.SelectAll(acSelFtr);
// 如果提示状态是 OK,对象就被选择了 If the prompt status is OK, objects were selected
if (acSSPrompt.Status == PromptStatus.OK)
{
SelectionSet acSSet = acSSPrompt.Value;
Application.ShowAlertDialog("Number of objects selected: " +acSSet.Count.ToString());
using (Transaction tr = db.TransactionManager.StartTransaction())
{
foreach (SelectedObject sobj in acSSet)
{
BlockReference bref = tr.GetObject(sobj.ObjectId, OpenMode.ForRead) as BlockReference;
ed.WriteMessage("\n块的名称是: " + bref.Name);
ed.WriteMessage("\n块的插入位置是:"+ bref.Position.ToString());
Extents3d exSize = bref.GeometricExtents;
Point2d pStart = Point3dToPoint2d(exSize.MinPoint);
Point2d pEnd = Point3dToPoint2d(exSize.MaxPoint);
ed.WriteMessage("\n块的大小是: 长{0},宽{1}", pEnd.X-pStart.X,pEnd.Y-pStart.Y);
ed.WriteMessage("\n块的旋转角度是: " + bref.Rotation * 180 / 3.1415926);
int a = Plot(pStart, pEnd, 0, 3);
}
}
}
else
{
Application.ShowAlertDialog("Number of objects selected: 0");
}
}
private int Plot(Point2d pMin, Point2d pMax, int angle, int size)
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//设置布局管理器为当前布局管理器
LayoutManager layoutMan = LayoutManager.Current;
//取得当前布局
Layout currentLayout = tr.GetObject(layoutMan.GetLayoutId(layoutMan.CurrentLayout),
OpenMode.ForRead) as Layout;
//从布局获取打印信息
PlotInfo pi = new PlotInfo();
pi.Layout = currentLayout.ObjectId;
//从布局获取一个PlotSetting的副本
PlotSettings ps = new PlotSettings(currentLayout.ModelType);
ps.CopyFrom(currentLayout);
//升级PlotSetting对象
PlotSettingsValidator psv = PlotSettingsValidator.Current;
//设置打印范围
Extents2d exWin = new Extents2d(pMin, pMax);
psv.SetPlotWindowArea(ps, exWin);
psv.SetPlotType(ps,Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
//设置打印比例,设置为布满图纸
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
//设置居中打印
psv.SetPlotCentered(ps,true);
//设置打印设备
ed.WriteMessage("\\\\work14\\canon_ip1100_series");
psv.SetPlotConfigurationName(ps, "\\\\work14\\canon ip1100 series", "A4");
//重写覆盖打印信息
pi.OverrideSettings = ps;
//确认打印信息
PlotInfoValidator piVdr = new PlotInfoValidator();
piVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled; //允许匹配颜色
piVdr.Validate(pi);
//检查打印状态
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
//开启打印引擎
using (PlotEngine pe = PlotFactory.CreatePublishEngine())
{
//通过一个进程对话框追踪打印进程
PlotProgressDialog plDialog = new PlotProgressDialog(false, 1, true);
using (plDialog)
{
// Define the status messages to display when plotting starts
//定义打印开始时显示的状态信息
plDialog.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot Progress");
plDialog.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
plDialog.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
plDialog.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
plDialog.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
// Set the plot progress range
//设置打印进程等级
plDialog.LowerPlotProgressRange = 0; //最低等级为0级
plDialog.UpperPlotProgressRange = 100; //最高等级为100级
plDialog.PlotProgressPos = 0;
// Display the Progress dialog //显示进程对话框
plDialog.OnBeginPlot();
plDialog.IsVisible = true;
// Start to plot the layout //开始打印布局
pe.BeginPlot(plDialog, null);
// Define the plot output
//acPlEng.BeginDocument(pi, acDoc.Name, null, 1, true, "c:\\myplot");
// Display information about the current plot
//显示当前打印相关信息
plDialog.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + doc.Name + " - " + currentLayout.LayoutName);
// Set the sheet progress range
//设置图纸进程等级
plDialog.OnBeginSheet();
plDialog.LowerSheetProgressRange = 0;
plDialog.UpperSheetProgressRange = 100;
plDialog.SheetProgressPos = 0;
// Plot the first sheet/layout
//打印第一个视口
try
{
PlotPageInfo ppInfo = new PlotPageInfo();
pe.BeginPage(ppInfo, pi, false, "ADASF");////这行出问题
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
}
catch (Autodesk.AutoCAD.Runtime.Exception ex)
{
Application.ShowAlertDialog(ex.Message);
}
// Finish plotting the sheet/layout
//完成打印视口
pe.EndPage(null); //这行也出问题
plDialog.SheetProgressPos = 100;
plDialog.OnEndSheet();
// Finish plotting the document
//完成文档打印
pe.EndDocument(null);
// Finish the plot
//完成打印操作
plDialog.PlotProgressPos = 100;
plDialog.OnEndPlot();
pe.EndPlot(null); //这行也出错
}
}
}
}
return 0;
}
public Point2d Point3dToPoint2d(Point3d p3d)
{
Point2d p2d = new Point2d(p3d.X, p3d.Y);
return p2d;
}
}
}
没有人愿意看看吗?
页:
[1]