mkhsj928
发表于 2017-10-20 14:41:37
本帖最后由 mkhsj928 于 2017-10-20 15:13 编辑
/// <summary>
/// 多页打印/预览函数
/// </summary>
/// <param name="pe"></param>
/// <param name="isPreview"></param>
/// <param name="plotList"></param>
/// <param name="sheetNumIfPreview"></param>
/// <param name="layoutCount"></param>
/// <param name="plotDevice"></param>
/// <param name="plotCanonicalMeida"></param>
/// <param name="plotStyle"></param>
/// <param name="saveFileName"></param>
/// <returns></returns>
private static PreviewEndPlotStatus MultiPlotOrPreview(
PlotEngine pe,
bool isPreview,
Dictionary<Extents2d, ObjectId> plotList,
int sheetNumIfPreview,
int layoutCount,//布局总数
string plotDevice,
string plotCanonicalMeida,
string plotStyle,
string saveFileName
)
{
PreviewEndPlotStatus ret = PreviewEndPlotStatus.Cancel;
string DocName = mCommands.Doc.Name;
DocName = DocName.Substring(DocName.LastIndexOf("\\") + 1);
DocName = DocName.Substring(0, DocName.LastIndexOf("."));
#region 准备打印区域列表plotAreaList
Dictionary<Extents2d, ObjectId> plotAreaList = new Dictionary<Extents2d, ObjectId>();
if (isPreview && sheetNumIfPreview >= 0)
{
KeyValuePair<Extents2d, ObjectId> kv = plotList.ElementAt(sheetNumIfPreview);
plotAreaList.Add(kv.Key, kv.Value);//预览只能一个区域
}
else
{
plotAreaList = plotList;//打印全部区域
}
#endregion
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
try
{
using (PlotProgressDialog ppd = new PlotProgressDialog(isPreview, plotAreaList.Count, false))
{
#region 设置进度条显示信息
ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "转换进度");
ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "本布局转换进度:__/__");
ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "总转换进度: __/__");
ppd.LowerPlotProgressRange = 0;
ppd.UpperPlotProgressRange = plotAreaList.Count;
//显示进度条对话框
ppd.OnBeginPlot();
ppd.IsVisible = true;
#endregion
int pageNum = 0;//布局打印页计数
int layoutPageNum = 0;//当前布局总打印页数(区域数)
int sheetNum = 0;//所有打印页计数(打印总区域数)
ObjectId layoutId = ObjectId.Null;//当前布局Id
Layout lo = null;
foreach (KeyValuePair<Extents2d, ObjectId> kv in plotAreaList)
{
if (kv.Value != layoutId)
{
layoutId = kv.Value;
lo = mFun.m_OpenEntity(layoutId) as Layout;
LayoutManager.Current.CurrentLayout = lo.LayoutName;//切换为当前布局,是否必须?!!
pageNum=0;//重置布局页计数
layoutPageNum = plotAreaList.Count(a => a.Value == layoutId);
ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = layoutPageNum;
}
pageNum++;//布局页计数+1
sheetNum++;//总打印区域计数+1
ppd.set_PlotMsgString(PlotMessageIndex.SheetName, $"{DocName}-{lo.LayoutName}");//打印文件名-布局名
//设置打印配置参数
PlotInfo pi = SetPlotInfo(lo, kv.Key, plotDevice, plotCanonicalMeida, plotStyle,isPreview);
#region 启动打印
if (sheetNum == 1)
{
pe.BeginPlot(ppd, null);
pe.BeginDocument(
pi, //打印信息
mCommands.Doc.Name, //当前图纸名
null,
1, //打印份数
!isPreview, //是否打印至文件
isPreview ? "" : saveFileName //保存文件名
);
}
#endregion
#region 开始打印
ppd.OnBeginSheet();
ppd.SheetProgressPos = pageNum;
ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, $"本布局转换进度:{pageNum}/{layoutPageNum}");
ppd.PlotProgressPos = sheetNum;
ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, $"总转换进度:sheetNum}/{plotAreaList.Count}");
pe.BeginPage(
new PlotPageInfo(),
pi, //打印信息
sheetNum == plotAreaList.Count, //是否最后一页
null
);
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
PreviewEndPlotInfo pepi = new PreviewEndPlotInfo();
pe.EndPage(pepi);//结束本页打印,返回预览状态
ret = pepi.Status;
#endregion
}
#region 结束打印
ppd.OnEndSheet();
pe.EndDocument(null);
ppd.OnEndPlot();
pe.EndPlot(null);
#endregion
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
MessageBox.Show($"转换为单个PDF文档出错: {e.Message}!\n请选择【单幅分别保存】进行转换。", "转换出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
ret = PreviewEndPlotStatus.Cancel;
}
}
return ret;
}
mkhsj928
发表于 2017-10-20 14:42:02
本帖最后由 mkhsj928 于 2017-10-20 15:10 编辑
/// <summary>
/// 单页预览
/// </summary>
/// <param name="layoutId"></param>
/// <param name="plotArea"></param>
/// <param name="plotDevice"></param>
/// <param name="plotCanonicalMeida"></param>
/// <param name="plotStyle"></param>
public static void PreviewSinglePage(
ObjectId layoutId,
Extents2d plotArea,
string plotDevice,
string plotCanonicalMeida,
string plotStyle
)
{
if (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
{
MessageBox.Show($"当前不能预览,另一个打印任务正在进行中!");
return;
}
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
Layout lo = mFun.m_OpenEntity(layoutId) as Layout;
if (LayoutManager.Current.CurrentLayout != lo.LayoutName)
{
LayoutManager.Current.CurrentLayout = lo.LayoutName;
}
using (PlotProgressDialog ppd = new PlotProgressDialog(true, 1, false))
{
#region 设置进度条信息
#endregion
using (PlotEngine pe = PlotFactory.CreatePreviewEngine(0))
{
//设置打印配置参数
PlotInfo pi = SetPlotInfo(lo, plotArea, plotDevice, plotCanonicalMeida, plotStyle,true);
pe.BeginPlot(ppd, null);
pe.BeginDocument(pi, mCommands.Doc.Name, null, 1, false, "");
pe.BeginPage(new PlotPageInfo(), pi, true, null);
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
PreviewEndPlotInfo pepi = new PreviewEndPlotInfo();
pe.EndPage(pepi);//结束本页打印,返回预览状态
pe.EndDocument(null);
pe.EndPlot(null);
}
}
}
}
mkhsj928
发表于 2017-10-20 14:42:30
本帖最后由 mkhsj928 于 2017-10-20 15:09 编辑
/// <summary>
/// 单页打印
/// </summary>
/// <param name="layoutId"></param>
/// <param name="plotArea"></param>
/// <param name="plotDevice"></param>
/// <param name="plotCanonicalMeida"></param>
/// <param name="plotStyle"></param>
/// <param name="saveFilName"></param>
public static void PlotSinglePage(
ObjectId layoutId,
Extents2d plotArea,
string plotDevice,
string plotCanonicalMeida,
string plotStyle,
string saveFilName)
{
if (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
{
MessageBox.Show($"当前不能打印,另一个打印任务正在进行中!");
return;
}
using (Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument())
{
Layout lo = mFun.m_OpenEntity(layoutId) as Layout;
if (LayoutManager.Current.CurrentLayout != lo.LayoutName)
{
LayoutManager.Current.CurrentLayout = lo.LayoutName;
}
using (PlotProgressDialog ppd = new PlotProgressDialog(false, 1, false))
{
#region 设置进度条信息
#endregion
using (PlotEngine pe = PlotFactory.CreatePublishEngine())
{
//设置打印配置参数
PlotInfo pi = SetPlotInfo(lo, plotArea, plotDevice, plotCanonicalMeida, plotStyle,true);
pe.BeginPlot(ppd, null);
pe.BeginDocument(pi, mCommands.Doc.Name, null, 1, true, saveFilName);
pe.BeginPage(new PlotPageInfo(), pi, true, null);
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
pe.EndPage(null);
pe.EndDocument(null);
pe.EndPlot(null);
}
}
}
}
mkhsj928
发表于 2017-10-20 14:44:03
以上就是打印至PDF的主要函数代码,可以打印成每张一个PDF,也可以打印成一个PDF速度没你说的那么慢
atrixs
发表于 2017-11-27 17:35:00
试试楼上的代码
Leo1980
发表于 2017-12-5 09:17:06
我觉得主要是要设置为前台打印
SetSystemVariable("BACKGROUNDPLOT", 0);
menethil
发表于 2017-12-6 15:28:31
mkhsj928 发表于 2017-10-20 14:40
你的代码是一个布局一个图,能不能只在一个布局中打很多张在一个PDF里,我试了很多次,每次都在beginpage中发生eValidePlotInfo异常,把角度设置成一样的也没用
menethil
发表于 2017-12-6 19:37:32
有用吗大家?我怎么设置了前景还是一分钟一张图
menethil
发表于 2017-12-6 19:43:05
menethil 发表于 2017-12-6 19:37
有用吗大家?我怎么设置了前景还是一分钟一张图
是我设置错了,设置前景是有用的,一秒一张
zjh2785
发表于 2017-12-17 19:35:15
好贴留名。。。