You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

459 lines
24 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using connor_zwcadm.model;
using connor_zwcadm.util;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZwSoft.ZwCAD.ApplicationServices;
using ZwSoft.ZwCAD.DatabaseServices;
using ZwSoft.ZwCAD.Geometry;
using ZwSoft.ZwCAD.Internal;
using ZwSoft.ZwCAD.PlottingServices;
namespace connor_zwcadm.commands
{
public class ConvertPDFCommand
{
public static string ConvertPDF(Document doc, Dictionary<string, PlotData> plotInfos, string path)
{
if (doc == null)
{
throw new Exception("图纸不能为null");
}
string cadPath = doc.Name;
KUtil.Log("转PDF " + cadPath);
KUtil.Log("PDF路径" + path);
if (path.Contains("/"))
{
path = path.Replace("/", "");
}
string pdfTempPath = System.IO.Path.GetTempPath() + "\\" + path;
//string pdfTempPath = cadPath.Substring(0, cadPath.LastIndexOf('\\') + 1) + path;
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
List<BlockReference> blocks;
List<string> blockRefRegexs;
KCADUtil.GetBlocksByRegex(tr, db, bt, OpenMode.ForRead, out blocks, out blockRefRegexs, plotInfos.Keys.ToArray());
int blockCnt = blocks == null ? 0 : blocks.Count;
KUtil.Log("找到块数量:" + blockCnt);
if (blockCnt == 0)
{
KUtil.Log("无需转换pdf");
//throw new Exception("图纸中没有找到需要打印的块");
return "OFF";
}
PlotInfo pi = new PlotInfo();
PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
if (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
{
throw new Exception("其他打印正在执行");
}
short bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT");
Application.SetSystemVariable("BACKGROUNDPLOT", 0);
using (PlotEngine pe = PlotFactory.CreatePublishEngine())
{
using (PlotProgressDialog ppd = new PlotProgressDialog(false, blockCnt, true))
{
PlotSettingsValidator psv = PlotSettingsValidator.Current;
StringCollection cols = psv.GetPlotStyleSheetList();
bool StyleSheetCheck = false;
foreach (string s in cols)
{
if (string.Compare(s, "monochrome.ctb", true) == 0)
{
StyleSheetCheck = true;
}
}
BlockTableRecord acBlkTblRec = tr.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
Layout lo = (Layout)tr.GetObject(acBlkTblRec.LayoutId, OpenMode.ForRead);
PlotSettings ps = new PlotSettings(lo.ModelType);
ps.CopyFrom(lo);
ps.PrintLineweights = true;
for (int i = 0; i < blockCnt; i++)
{
BlockReference blockRef = blocks[i];
PlotData plotData = plotInfos[blockRefRegexs[i]];
//psv.SetPlotConfigurationName(ps, "DWG To PDF.pc5", plotData.MediaName);
try
{
KUtil.Log("plotData.TitleBlockName====" + plotData.TitleBlockName);
if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("portrait", StringComparison.OrdinalIgnoreCase) >= 0)
{
psv.SetPlotConfigurationName(ps, "DWG to PDF.pc5", null);
}
else
{
psv.SetPlotConfigurationName(ps, "DWG to PDF横向.pc5", null);
}
psv.RefreshLists(ps);
StringCollection medlist = psv.GetCanonicalMediaNameList(ps);
KUtil.Log(plotData.MediaName);
Boolean flag = true;
for (int j = 0; j < medlist.Count; j++)
{
KUtil.Log("medlist[j]===="+medlist[j]);
if (plotData.MediaName.Equals(medlist[j]))
{
KUtil.Log("1111111111111111111");
if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("portrait", StringComparison.OrdinalIgnoreCase) >= 0)
{
psv.SetPlotConfigurationName(ps, "DWG To PDF.pc5", medlist[j]);
}
else
{
psv.SetPlotConfigurationName(ps, "DWG to PDF横向.pc5", medlist[j]);
}
flag = false;
break;
}
//KUtil.Log(medlist[j]);
}
if (flag)
{
if (!string.IsNullOrEmpty(plotData.TitleBlockName) && plotData.TitleBlockName.IndexOf("portrait", StringComparison.OrdinalIgnoreCase) >= 0)
{
psv.SetPlotConfigurationName(ps, "DWG To PDF.pc5", plotData.MediaName);
}
else
{
psv.SetPlotConfigurationName(ps, "DWG to PDF横向.pc5", plotData.MediaName);
}
}
}
catch (SystemException ex)
{
KUtil.Log("错误行开始:");
KUtil.Log("错误状态:" + ex.InnerException.ToString());
KUtil.Log("异常消息:" + ex.Message.ToString());
KUtil.Log("Source" + ex.Source.ToString());
KUtil.Log("StackTrace" + ex.StackTrace.ToString());
KUtil.Log("TargetSite" + ex.TargetSite.ToString());
}
Point3d minPoint = blockRef.Bounds.Value.MinPoint;
Point3d maxPoint = blockRef.Bounds.Value.MaxPoint; //
psv.SetPlotWindowArea(ps, new Extents2d(minPoint.X, minPoint.Y, maxPoint.X, maxPoint.Y));
psv.SetPlotType(ps, ZwSoft.ZwCAD.DatabaseServices.PlotType.Window);
if (!StyleSheetCheck)
psv.SetCurrentStyleSheet(ps, "monochrome.stb");
else
psv.SetCurrentStyleSheet(ps, "monochrome.ctb");
psv.SetPlotCentered(ps, true);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
psv.SetPlotRotation(ps, plotData.Rotate ? PlotRotation.Degrees090 : PlotRotation.Degrees000);
pi.Layout = acBlkTblRec.LayoutId;
LayoutManager.Current.CurrentLayout = lo.LayoutName;
pi.OverrideSettings = ps;
piv.Validate(pi);
if (i == 0)
{
ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "转PDF");
//ppd.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "取消打印");
//ppd.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "取消当前页打印");
//ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
//ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
ppd.LowerPlotProgressRange = 0;
ppd.UpperPlotProgressRange = blockCnt;
ppd.PlotProgressPos = 0;
ppd.OnBeginPlot();
ppd.IsVisible = true;
pe.BeginPlot(ppd, null);
pe.BeginDocument(pi, cadPath, null, 1, true, pdfTempPath);
}
ppd.StatusMsgString = "打印 " + System.IO.Path.GetFileName(cadPath) + " - 页 ( " + (i + 1) + " / " + blockCnt + " )";
ppd.PlotProgressPos = i;
ppd.OnBeginSheet();
ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = 100;
ppd.SheetProgressPos = 0;
PlotPageInfo ppi = new PlotPageInfo();
pe.BeginPage(ppi, pi, i == blockCnt - 1, null);
pe.BeginGenerateGraphics(null);
ppd.SheetProgressPos = 50;
pe.EndGenerateGraphics(null);
pe.EndPage(null);
ppd.SheetProgressPos = 100;
ppd.OnEndSheet();
}
pe.EndDocument(null);
ppd.PlotProgressPos = 100;
ppd.OnEndPlot();
pe.EndPlot(null);
ppd.Dispose();
}
}
Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
tr.Commit();
}
KUtil.Log("pdfTempPath:" + pdfTempPath);
KUtil.Log("转换PDF完成");
return pdfTempPath;
}
public static string ConvertPDF(Document doc, Dictionary<string, PlotData> plotInfos)
{
if (doc == null)
{
throw new Exception("图纸不能为null");
}
string cadPath = doc.Name;
KUtil.Log("转PDF " + cadPath);
string path = cadPath.Substring(0, cadPath.Length - 4) + ".pdf";
KUtil.Log("PDF路径" + path);
Database db = doc.Database;
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
List<BlockReference> blocks;
List<string> blockRefRegexs;
KCADUtil.GetBlocksByRegex(tr, db, bt, OpenMode.ForRead, out blocks, out blockRefRegexs, plotInfos.Keys.ToArray());
int blockCnt = blocks == null ? 0 : blocks.Count;
KUtil.Log("找到块数量:" + blockCnt);
if (blockCnt == 0)
{
KUtil.Log("无需转换pdf");
//throw new Exception("图纸中没有找到需要打印的块");
return "OFF";
}
PlotInfo pi = new PlotInfo();
PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
if (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
{
throw new Exception("其他打印正在执行");
}
short bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT");
Application.SetSystemVariable("BACKGROUNDPLOT", 0);
using (PlotEngine pe = PlotFactory.CreatePublishEngine())
{
using (PlotProgressDialog ppd = new PlotProgressDialog(false, blockCnt, true))
{
PlotSettingsValidator psv = PlotSettingsValidator.Current;
StringCollection cols = psv.GetPlotStyleSheetList();
bool StyleSheetCheck = false;
foreach (string s in cols)
{
if (string.Compare(s, "monochrome.ctb", true) == 0)
{
StyleSheetCheck = true;
}
}
BlockTableRecord acBlkTblRec = tr.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
Layout lo = (Layout)tr.GetObject(acBlkTblRec.LayoutId, OpenMode.ForRead);
PlotSettings ps = new PlotSettings(lo.ModelType);
ps.CopyFrom(lo);
ps.PrintLineweights = true;
for (int i = 0; i < blockCnt; i++)
{
BlockReference blockRef = blocks[i];
PlotData plotData = plotInfos[blockRefRegexs[i]];
psv.SetPlotConfigurationName(ps, "DWG To PDF.pc5", plotData.MediaName);
Point3d minPoint = blockRef.Bounds.Value.MinPoint;
Point3d maxPoint = blockRef.Bounds.Value.MaxPoint; //
psv.SetPlotWindowArea(ps, new Extents2d(minPoint.X, minPoint.Y, maxPoint.X, maxPoint.Y));
psv.SetPlotType(ps, ZwSoft.ZwCAD.DatabaseServices.PlotType.Window);
if (!StyleSheetCheck)
psv.SetCurrentStyleSheet(ps, "monochrome.stb");
else
psv.SetCurrentStyleSheet(ps, "monochrome.ctb");
psv.SetPlotCentered(ps, true);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
psv.SetPlotRotation(ps, plotData.Rotate ? PlotRotation.Degrees090 : PlotRotation.Degrees000);
pi.Layout = acBlkTblRec.LayoutId;
LayoutManager.Current.CurrentLayout = lo.LayoutName;
pi.OverrideSettings = ps;
piv.Validate(pi);
if (i == 0)
{
ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "转PDF");
//ppd.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "取消打印");
//ppd.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "取消当前页打印");
//ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
//ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
ppd.LowerPlotProgressRange = 0;
ppd.UpperPlotProgressRange = blockCnt;
ppd.PlotProgressPos = 0;
ppd.OnBeginPlot();
ppd.IsVisible = true;
pe.BeginPlot(ppd, null);
pe.BeginDocument(pi, cadPath, null, 1, true, path);
}
ppd.StatusMsgString = "打印 " + System.IO.Path.GetFileName(cadPath) + " - 页 ( " + (i + 1) + " / " + blockCnt + " )";
ppd.PlotProgressPos = i;
ppd.OnBeginSheet();
ppd.LowerSheetProgressRange = 0;
ppd.UpperSheetProgressRange = 100;
ppd.SheetProgressPos = 0;
PlotPageInfo ppi = new PlotPageInfo();
pe.BeginPage(ppi, pi, i == blockCnt - 1, null);
pe.BeginGenerateGraphics(null);
ppd.SheetProgressPos = 50;
pe.EndGenerateGraphics(null);
pe.EndPage(null);
ppd.SheetProgressPos = 100;
ppd.OnEndSheet();
}
pe.EndDocument(null);
ppd.PlotProgressPos = 100;
ppd.OnEndPlot();
pe.EndPlot(null);
ppd.Dispose();
}
}
Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
tr.Commit();
}
KUtil.Log("转换PDF完成");
return path;
}
public static string ConvertPDF(Database db, string cadPath, Dictionary<string, PlotData> plotInfos)
{
if (db == null)
{
throw new Exception("图纸DataBase不能为null");
}
KUtil.Log("转PDF " + cadPath);
string path = cadPath.Substring(0, cadPath.Length - 4) + ".pdf";
KUtil.Log("PDF路径" + path);
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
List<BlockReference> blocks;
List<string> blockRefRegexs;
KCADUtil.GetBlocksByRegex(tr, db, bt, OpenMode.ForRead, out blocks, out blockRefRegexs, plotInfos.Keys.ToArray());
int blockCnt = blocks == null ? 0 : blocks.Count;
KUtil.Log("找到块数量:" + blockCnt);
if (blockCnt == 0)
{
//throw new Exception("图纸中没有找到需要打印的块");
return path;
}
PlotInfo pi = new PlotInfo();
PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
if (PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
{
throw new Exception("其他打印正在执行");
}
short bgPlot = (short)Application.GetSystemVariable("BACKGROUNDPLOT");
Application.SetSystemVariable("BACKGROUNDPLOT", 0);
using (PlotEngine pe = PlotFactory.CreatePublishEngine())
{
//using (PlotProgressDialog ppd = new PlotProgressDialog(false, blockCnt, true)) {
PlotSettingsValidator psv = PlotSettingsValidator.Current;
StringCollection cols = psv.GetPlotStyleSheetList();
bool StyleSheetCheck = false;
foreach (string s in cols)
{
if (string.Compare(s, "monochrome.ctb", true) == 0)
{
StyleSheetCheck = true;
}
}
BlockTableRecord acBlkTblRec = tr.GetObject(bt[BlockTableRecord.ModelSpace],
OpenMode.ForRead) as BlockTableRecord;
Layout lo = (Layout)tr.GetObject(acBlkTblRec.LayoutId, OpenMode.ForRead);
PlotSettings ps = new PlotSettings(lo.ModelType);
ps.CopyFrom(lo);
ps.PrintLineweights = true;
for (int i = 0; i < blockCnt; i++)
{
BlockReference blockRef = blocks[i];
PlotData plotData = plotInfos[blockRefRegexs[i]];
psv.SetPlotConfigurationName(ps, "DWG To PDF.pc5", plotData.MediaName);
Point3d minPoint = blockRef.Bounds.Value.MinPoint;
Point3d maxPoint = blockRef.Bounds.Value.MaxPoint; //
psv.SetPlotWindowArea(ps, new Extents2d(minPoint.X, minPoint.Y, maxPoint.X, maxPoint.Y));
psv.SetPlotType(ps, ZwSoft.ZwCAD.DatabaseServices.PlotType.Window);
if (!StyleSheetCheck)
psv.SetCurrentStyleSheet(ps, "monochrome.stb");
else
psv.SetCurrentStyleSheet(ps, "monochrome.ctb");
psv.SetPlotCentered(ps, true);
psv.SetUseStandardScale(ps, true);
psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
psv.SetPlotRotation(ps, plotData.Rotate ? PlotRotation.Degrees090 : PlotRotation.Degrees000);
pi.Layout = acBlkTblRec.LayoutId;
LayoutManager.Current.CurrentLayout = lo.LayoutName;
pi.OverrideSettings = ps;
piv.Validate(pi);
if (i == 0)
{
//ppd.set_PlotMsgString(PlotMessageIndex.DialogTitle, "转PDF");
////ppd.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "取消打印");
////ppd.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "取消当前页打印");
////ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
////ppd.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");
//ppd.LowerPlotProgressRange = 0;
//ppd.UpperPlotProgressRange = blockCnt;
//ppd.PlotProgressPos = 0;
//ppd.OnBeginPlot();
//ppd.IsVisible = true;
pe.BeginPlot(null, null);
pe.BeginDocument(pi, cadPath, null, 1, true, path);
}
//ppd.StatusMsgString = "打印 " + System.IO.Path.GetFileName(cadPath) + " - 页 ( " + (i + 1) + " / " + blockCnt + " )";
//ppd.PlotProgressPos = i;
//ppd.OnBeginSheet();
//ppd.LowerSheetProgressRange = 0;
//ppd.UpperSheetProgressRange = 100;
//ppd.SheetProgressPos = 0;
PlotPageInfo ppi = new PlotPageInfo();
pe.BeginPage(ppi, pi, i == blockCnt - 1, null);
pe.BeginGenerateGraphics(null);
//ppd.SheetProgressPos = 50;
pe.EndGenerateGraphics(null);
pe.EndPage(null);
// ppd.SheetProgressPos = 100;
// ppd.OnEndSheet();
}
pe.EndDocument(null);
// ppd.PlotProgressPos = 100;
//ppd.OnEndPlot();
pe.EndPlot(null);
// ppd.Dispose();
}
//}
Application.SetSystemVariable("BACKGROUNDPLOT", bgPlot);
tr.Commit();
}
KUtil.Log("转换PDF完成");
return path;
}
}
}