autocad.net-图片打印合成

调用打印程序“PublishToWeb JPG.pc3”进行图片打印,该打印驱动程序中内置了很多的打印方案,在同尺寸的打印方案下,数据范围越大打印出来的清晰度就越差,内置的尺寸不一定都满足,在又要通过我们的插件去完成打印任务,又不能让客户总是做配置的情况下,我总结了一个不是很完美的解决方案,实现思路如下:

1、选定基础打印尺寸方案(本demo选定“UserDefinedRaster (1600.00 x 1200.00Pixels)”),一定是系统自带的,不然就需要人工配置,暂时没有找到通过代码去修改打印方案

2、在一定的精度比例尺下(本demo是一个像素点代表10个坐标点的跨度),根据用户选择的范围计算出要打印的图片数量。

3、将单张打印的图片存储到临时文件夹下,等全部打印完毕,按照顺序合并成一张图图片

主方法,打印合成的入口

   public  bool CutImgAndPutFromLeftUpperCAD(Document doc)
{
Point3d maxPt = Point3d.Origin;
Point3d minPt = Point3d.Origin;
PromptIntegerOptions prInt = new PromptIntegerOptions("\n请选择导出方式:选择方式(1:全图导出,2:拉框导出):");
prInt.DefaultValue = ;
prInt.AllowArbitraryInput = false;
prInt.AllowNegative = false;
prInt.AllowNone = false;
PromptIntegerResult ptrInt = Tools.MdiAcEditor.GetInteger(prInt);
if (ptrInt.Status != PromptStatus.OK)
return false;
switch (ptrInt.Value)
{
case :
maxPt = doc.Database.Extmax;
minPt = doc.Database.Extmin;
break;
case :
PromptPointOptions StartPoint = new PromptPointOptions("\n请选择第一个角点");
PromptPointResult StartPointResult = Tools.MdiAcEditor.GetPoint(StartPoint);
PromptCornerOptions PromptCornerOptions = new PromptCornerOptions("\n请选中第二个角点", StartPointResult.Value);
PromptPointResult EndPointResult = Tools.MdiAcEditor.GetCorner(PromptCornerOptions);
double maxX = StartPointResult.Value.X > EndPointResult.Value.X ? StartPointResult.Value.X : EndPointResult.Value.X;
double minX = StartPointResult.Value.X > EndPointResult.Value.X ? EndPointResult.Value.X : StartPointResult.Value.X;
double maxY = StartPointResult.Value.Y > EndPointResult.Value.Y ? StartPointResult.Value.Y : EndPointResult.Value.Y;
double minY = StartPointResult.Value.Y > EndPointResult.Value.Y ? EndPointResult.Value.Y : StartPointResult.Value.Y; maxPt = new Point3d(maxX, maxY, );
minPt = new Point3d(minX, minY, );
break;
} string JPGSavePath = Const.systemPath + "\\Save";
string dataSavePath = Const.systemPath + "\\Temp";
if (Directory.Exists(dataSavePath))
{
FileOperate.DelectDir(dataSavePath);
}
else
{
Directory.CreateDirectory(dataSavePath);
}
if (Directory.Exists(JPGSavePath))
{
FileOperate.DelectDir(JPGSavePath);
}
else
{
Directory.CreateDirectory(JPGSavePath);
}
Point3d LeftUpP = new Point3d(minPt.X, maxPt.Y, );
Point3d RightDownP = new Point3d(maxPt.X, minPt.Y, );
Point3d LeftDownP = new Point3d(minPt.X, minPt.Y, );
Point3d RightUpP = new Point3d(maxPt.X, maxPt.Y, );
//根据坐标范围计算图片的数量,多于100张不执行打印任务
int rows = Convert.ToInt32(Math.Floor(LeftUpP.Y - RightDownP.Y) / 12000.0) + ;
int cels = Convert.ToInt32(Math.Floor(RightUpP.X - LeftUpP.X) / 16000.0) + ;
if (rows *cels > )
{
return false;
}
int cutTrueCount = ;
int itemImgCount = ;
itemImgCount = ;
int imgCount = ;
string imgPathAndName = string.Empty;
CircularProgress cirProgress = new CircularProgress(, rows * cels, "正在导出图片");
cirProgress.StartProgress(); try
{ for (int row = ; row < rows; row++)
{
for (int cel = ; cel < cels; cel++)
{
try
{
imgCount++;
cirProgress.SendMessageChangeValue("正在切图 ", (imgCount*)/ (rows * cels));
double nowLeftX = LeftUpP.X + (double)(cel * );
double nowLeftY = LeftUpP.Y - (double)(row * );
Point3d leftPoint = new Point3d(nowLeftX, nowLeftY - , );
Point3d rightPoint = new Point3d(nowLeftX + , nowLeftY, );
string m_ImgName = string.Concat(new object[]
{
row,
"_",
cel,
".jpg"
});
imgPathAndName = dataSavePath + "\\";
object obj = imgPathAndName;
imgPathAndName = string.Concat(new object[]
{
obj,
row,
"_",
cel,
".jpg"
});
//单张图片打印
ExportMapToFileCAD(leftPoint, rightPoint, imgPathAndName, doc);
itemImgCount++;
cutTrueCount++;
}
catch
{
break;
}
}
}
string JPGName = doc.Window.Text;
JPGName = JPGName.Split(new char[] { '.' })[];
//开始合成图片
CombinImage(rows, cels, , , dataSavePath, JPGSavePath + "\\" + JPGName + ".jpg", cirProgress);
Tools.MdiAcEditor.WriteMessageWithReturn("图片导出成功:" + JPGSavePath + "\\" + JPGName + ".jpg");
cirProgress.StopProgress();
return true; }
catch (Exception ex)
{
cirProgress.StopProgress();
return false;
}
}
  private  bool ExportMapToFileCAD(Point3d leftPoint, Point3d rigthPoint, string fileName, Document doc)
{
bool result;
try
{
Editor ed = doc.Editor;
Database db = doc.Database;
if (fileName.Trim().Equals(""))
{
result = false;
}
else
{
using (doc.LockDocument())
{
using (Transaction tr = db.TransactionManager.StartTransaction())
{
//一定要记得设置,否则打印将非常之慢
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("BackGroundPlot", );
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite);
Layout lo = (Layout)tr.GetObject(btr.LayoutId, OpenMode.ForWrite);
PlotInfo pi = new PlotInfo();
pi.Layout = btr.LayoutId;
PlotSettings ps = new PlotSettings(lo.ModelType);
ps.CopyFrom(lo);
PlotSettingsValidator psv = PlotSettingsValidator.Current;
//很重要,坐标处理,针对自定义坐标系的,像天正的图纸,不做处理可能会打印出空白的
Extents2d ext2d = Ucs2Dcs(leftPoint, rigthPoint);
psv.SetPlotWindowArea(ps, ext2d);
psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
psv.SetPlotRotation(ps, );
psv.SetStdScaleType(ps, );
psv.SetPlotCentered(ps, true); psv.GetCanonicalMediaNameList(ps);
psv.SetPlotConfigurationName(ps, "PublishToWeb JPG.pc3", "UserDefinedRaster (1600.00 x 1200.00Pixels)");
pi.OverrideSettings = ps;
PlotInfoValidator piv = new PlotInfoValidator();
piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
piv.Validate(pi);
while ((PlotFactory.ProcessPlotState == ProcessPlotState.BackgroundPlotting) || (PlotFactory.ProcessPlotState == ProcessPlotState.ForegroundPlotting))
{
System.Threading.Thread.Sleep();
}
if (PlotFactory.ProcessPlotState == )
{
PlotEngine pe = PlotFactory.CreatePublishEngine();
using (pe)
{
PlotProgressDialog ppd = new PlotProgressDialog(false, , true);
using (ppd)
{
ppd.set_PlotMsgString(, "CAD切图");
ppd.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "取消切图");
ppd.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "取消切图");
ppd.set_PlotMsgString(PlotMessageIndex.SheetSetProgre*tion, "切图");
ppd.set_PlotMsgString(PlotMessageIndex.SheetProgre*tion, "正在切图");
ppd.LowerPlotProgressRange = ;
ppd.UpperPlotProgressRange = ;
ppd.PlotProgressPos = ;
ppd.OnBeginPlot();
ppd.IsVisible = false;
pe.BeginPlot(ppd, null);
pe.BeginDocument(pi, fileName, null, , true, fileName);
ppd.OnBeginSheet();
ppd.LowerSheetProgressRange = ;
ppd.UpperSheetProgressRange = ;
ppd.SheetProgressPos = ;
PlotPageInfo ppi = new PlotPageInfo();
pe.BeginPage(ppi, pi, true, null);
pe.BeginGenerateGraphics(null);
pe.EndGenerateGraphics(null);
pe.EndPage(null);
ppd.SheetProgressPos = ;
ppd.OnEndSheet();
pe.EndDocument(null);
ppd.PlotProgressPos = ;
ppd.OnEndPlot();
pe.EndPlot(null);
}
}
}
else
{
ed.WriteMessage("\n另外一个程序正在运行中。。。");
}
}
}
result = true;
}
}
catch (System.Exception ex)
{
result = false;
}
return result;
}

根据范围打印单张图片

  private  void CombinImage(int rows, int cels, int width, int height,string url,string outPath , CircularProgress cirProgress)
{
using (Bitmap bg = new Bitmap(cels * width, rows * height))
{
//构建画布
Graphics g = Graphics.FromImage(bg);
//清除画布,背景透明
g.Clear(Color.Transparent);
int leftUpX = ;
int leftUpY = ;
int count = ;
cirProgress.SendMasterChangeMessage("正在进行图片合成", rows * cels);
for (int row = ; row < rows; row++)
{
for (int cel = ; cel < cels; cel++)
{
count++;
cirProgress.SendMessageChangeValue("正在进行图片合成",(count*)/(rows*cels));
//加载小图片
System.Drawing.Image img = System.Drawing.Image.FromFile(url + "\\" + row + "_" + cel + ".jpg");
//确定插入位置
leftUpX = (cel * width);
leftUpY = (row * height);
//插入图片到画布中
g.DrawImage(img, new System.Drawing.Point(leftUpX, leftUpY));
img.Dispose();
}
}
g.Dispose();
bg.Save(outPath);
cirProgress.SendMessageChangeValue("正在进行图片反色", (count * ) / (rows * cels));
// 反色处理并且保存图片
reversePic(bg);
//Bitmap tempBitmap = reversePic(bg);
//tempBitmap.Save(Const.systemPath + "\\Save\\test.jpg");
//tempBitmap.Dispose();
}
}

图片合成

  Extents2d Ucs2Dcs(Point3d objStart, Point3d objEnd)
{
ResultBuffer rbFrom =
new ResultBuffer(new TypedValue(, )),
rbTo =
new ResultBuffer(new TypedValue(, )); double[] firres = new double[] { , , };
double[] secres = new double[] { , , }; CommandTools.acedTrans(
objStart.ToArray(),
rbFrom.UnmanagedObject,
rbTo.UnmanagedObject,
,
firres
); CommandTools.acedTrans(
objEnd.ToArray(),
rbFrom.UnmanagedObject,
rbTo.UnmanagedObject,
,
secres
); Extents2d window =
new Extents2d(
firres[],
firres[],
secres[],
secres[]
);
return window;
}

坐标转换

上一篇:Qt... configure: error: Qt (>= Qt 2.2.2) (headers…


下一篇:Qt QML之不显示标题栏、边框