明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 19795|回复: 32

[打印] 使用NetAPI进行CAD打印,有兴趣的交流交流

    [复制链接]
发表于 2010-12-17 15:48 | 显示全部楼层 |阅读模式
最近在写一个批量分图打印的程序,在研究了kean的打印程序后,总结一下使用NetAPI打印的过程:
1.创建一个plotinfo,然后设置layout属性使其与打印布局空间
2.读取布局的plotsettings对象。先创建一个新的plotsettings,然后使用copyfrom方法由布局空间映射打印设置
3.使用当前的plotsettingsvalidator对plotsettings对象进行自定义设置。plotsettings保存了打印的一些设置,但是plotsettings的属性基本上都是只读的,因此如果我们需要进行自定义设置,必须通过plotsettingsvalidator才能对plotsettings进行有限的操作,其中:
1)  GetPlotDeviceList 读取打印机列表
2)GetCanonicalMediaNameList 读取打印机的图纸列表。一般在读取图纸列表之前,我们先使用SetPlotConfigurationName设置打印机,图纸大小设置为null,然后调用RefreshLists刷新图纸列表,然后再进行读取。
3)GetLocaleMediaName 图纸的本地自定义命名。我们会发现GetCanonicalMediaNameList 返回的图纸列表中,用户自定的图纸会以UserDefinedMetric来命名的。而我们的自定义命名只能通过GetLocaleMediaName来读取
4)GetPlotStyleSheetList 读取打印样式表
5)SetPlotConfigurationName 设置打印机及图纸大小
6)SetPlotPaperUnits 设置打印单位。在kean的程序中,SetPlotConfigurationName是最后设置的。但是我们会发现,设置了SetPlotConfigurationName后,打印单位会自动变成了英寸,因此我们需要在SetPlotConfigurationName后调用SetPlotPaperUnits来进行修正。
7)SetCurrentStyleSheet 设置打印样式
8)SetPlotWindowArea 设置打印窗选范围
9)SetPlotType设置打印范围类型。对于窗选打印,应先设置SetPlotWindowArea ,然后再设置SetPlotType,否则有可能会引发错误
10)SetCustomPrintScale 设置自定义打印比例
11)SetClosestMediaName 设置接近的图幅。这个方法的使用暂时不明确,因为每次测试均引发错误
12)SetPlotOrigin 设置打印偏移
13)SetPlotCentered设置居中打印
14)SetStdScaleType 设置标准打印比例。布满图纸打印在这里设置
15)SetStdScale 设置标准打印比例
16)SetUseStandardScale 使用标准打印比例,对于自定义打印比例不需要设置该项,否则可能引发错误
4.通过plotinfo的overridesettings属性将plotinfo与plotsettings关联
5.创建一个plotinfovalidator,使用validate方法将plotinfo激活
6.由plotfactory建立一个plotengine发布引擎,执行打印任务。
打印顺序:
1)BeginPlot
2)BeginDocument
3)BeginPage
4)BeginGenerateGraphics
5)EndGenerateGraphics
6)EndPage
7)EndDocument
8)EndPlot

评分

参与人数 3威望 +1 金钱 +25 收起 理由
TiStars + 5 很给力!
fsxm + 20 很给力!
雪山飞狐_lzh + 1

查看全部评分

发表于 2017-12-28 16:27 | 显示全部楼层
有意思吗,标题上的内容就不放,与题不合。
回复 支持 0 反对 3

使用道具 举报

发表于 2018-1-18 07:06 | 显示全部楼层
本帖最后由 TiStars 于 2018-1-18 10:12 编辑

我狗尾续貂一下:

  • 如果在模型空间打印,且需要选定范围,根据我自己的经验,选定的坐标不能直接设置给SetPlotWindowArea ,否则打印出来的都是空白,需要按照这里说的作一下转换。

----------------------------------------------------------------
  1. // Gets the current view
  2. ViewTableRecord acView = acDoc.Editor.GetCurrentView();
  3. Extents3d eExtents = new Extents3d(new Point3d(0, 0, 0), new Point3d(630, 445.5, 0));

  4. // Translates WCS coordinates to DCS
  5. Matrix3d matWCS2DCS;
  6. matWCS2DCS = Matrix3d.PlaneToWorld(acView.ViewDirection);
  7. matWCS2DCS = Matrix3d.Displacement(acView.Target - Point3d.Origin) * matWCS2DCS;
  8. matWCS2DCS = Matrix3d.Rotation(-acView.ViewTwist,
  9.                                acView.ViewDirection,
  10.                                acView.Target) * matWCS2DCS;

  11. // Tranforms the extents to DCS
  12. matWCS2DCS = matWCS2DCS.Inverse();
  13. eExtents.TransformBy(matWCS2DCS);

  14. // Defines the area to output
  15. acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
  16. acPlSetVdr.SetPlotWindowArea(acPlSet, new Extents2d(eExtents.MinPoint.X, eExtents.MinPoint.Y,
  17.                                                     eExtents.MaxPoint.X, eExtents.MaxPoint.Y));

------------------------------------------------


而且,按照上述操作进行Extents3d转换时,要注意第一个参数的坐标值要小于第二个参数的坐标值。

不过我只知其然而不知其所以然,请高手帮忙解释一下,谢谢。

  • 另外还想起来一个,如果把后台打印设为前台,则会大大提高打印速度。

  1. //保存App的原参数
  2. short bgPlot =(short)Application.GetSystemVariable("BACKGROUNDPLOT");
  3. //设定为前台打印,加快打印速度
  4. Application.SetSystemVariable("BACKGROUNDPLOT", 0);



  • 刚刚又遇到一个问题,看了楼主的这一条解决了,不过我没有RefreshLists就可以获取到图纸列表。


2)GetCanonicalMediaNameList 读取打印机的图纸列表。一般在读取图纸列表之前,我们先使用SetPlotConfigurationName设置打印机,图纸大小设置为null,然后调用RefreshLists刷新图纸列表,然后再进行读取。



回复 支持 1 反对 0

使用道具 举报

发表于 2010-12-17 15:56 | 显示全部楼层
打印这部分因为自己不需要所以一直懒得看,呵呵
欢迎水手多发相关的文章和代码
发表于 2010-12-17 19:17 | 显示全部楼层
我用VBA做过,NET没试过
发表于 2010-12-19 18:11 | 显示全部楼层
期待楼主杰作...
 楼主| 发表于 2011-2-17 15:06 | 显示全部楼层

使用NetAPI进行CAD打印(二)-首尾相连的线段连接成多段线

本帖最后由 雪山飞狐_lzh 于 2011-2-17 18:16 编辑


  1.         /// <summary>
  2.         ///首尾相连的线段连接成多段线
  3.         /// V1.0 by WeltionChen @2011.02.17
  4.         /// 实现原理:
  5.         /// 1.选择图面上所有直线段
  6.         /// 2.选取选集第一条直线作为起始线段,向线段的两个方向搜索与之相连的直线段
  7.         /// 3.搜索方式采用Editor的SelectCrossingWindow方法通过线段的端点创建选集
  8.         /// 正常情况下会选到1到2个线段(本程序暂不处理3个线段相交的情况),剔除本身,得到与之相连的直线段
  9.         /// 4.处理过的直线段将不再作为起始线段,由集合中剔除
  10.         /// 4.通过递归循环依次搜索,直到末端。
  11.         /// 5.删除原线段,根据创建多段线
  12.         /// 6.循环处理所有的线段
  13.         /// </summary>
  14.         [CommandMethod ("tt5")]
  15.         public void JionLinesToPline()
  16.         {
  17.             //选择图面上所有直线段
  18.             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  19.             SelectionFilter sf = new SelectionFilter(new TypedValue[] { new TypedValue(0, "Line") });
  20.             PromptSelectionResult selectLinesResult = ed.SelectAll(sf);
  21.             if (selectLinesResult.Status != PromptStatus.OK)
  22.                 return;
  23.             //需要处理的直线段集合
  24.             List<ObjectId> lineObjectIds = new List<ObjectId>(selectLinesResult.Value.GetObjectIds());
  25.             using (Transaction tr = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
  26.             {
  27.                 Database db = HostApplicationServices.WorkingDatabase;
  28.                 BlockTableRecord currentSpace = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
  29.                 while (true)
  30.                 {
  31.                     //选取选集第一条直线作为起始线段
  32.                     ObjectId currentLineId = lineObjectIds[0];
  33.                     //处理过的直线段将不再作为起始线段,由集合中剔除
  34.                     lineObjectIds.RemoveAt(0);
  35.                     Line currentLine = tr.GetObject(currentLineId, OpenMode.ForWrite) as Line;
  36.                     //多段线的顶点集合,由各段相连的直线段的端点组成,初始值为起始线段的端点
  37.                     List<Point3d> plinePoints = new List<Point3d> { currentLine.StartPoint, currentLine.EndPoint };
  38.                     //每个直线段有两个方向,由起点向终点方向搜索
  39.                     JionLinesToPline(ref lineObjectIds, tr, ref plinePoints, currentLineId, currentLineId);
  40.                     //翻转点集
  41.                     plinePoints.Reverse();
  42.                     //由终点向起点方向搜索
  43.                     JionLinesToPline(ref lineObjectIds, tr, ref plinePoints, currentLineId, currentLineId);
  44.                     //本程序为将相连的直线段转成多段线,所以对孤立的直线段不做处理
  45.                     if (plinePoints.Count > 2)
  46.                     {
  47.                         //创建多段线
  48.                         Autodesk.AutoCAD.DatabaseServices.Polyline resultPline = new Autodesk.AutoCAD.DatabaseServices.Polyline();                        
  49.                         for (int i = 0; i < plinePoints.Count-1; i++)
  50.                         {
  51.                             resultPline.AddVertexAt(i, new Point2d(plinePoints.X, plinePoints.Y), 0, 0, 0);
  52.                         }
  53.                         if (plinePoints[0] == plinePoints[plinePoints.Count - 1])
  54.                         {
  55.                             resultPline.Closed = true;
  56.                         }
  57.                         else
  58.                         {
  59.                             resultPline.AddVertexAt(plinePoints.Count - 1, new Point2d(plinePoints[plinePoints.Count - 1].X, plinePoints[plinePoints.Count - 1].Y), 0, 0, 0);
  60.                         }
  61.                         resultPline.Layer = currentLine.Layer;
  62.                         resultPline.Linetype = currentLine.Linetype;
  63.                         resultPline.LinetypeScale = currentLine.LinetypeScale;
  64.                         currentSpace.AppendEntity(resultPline);
  65.                         tr.AddNewlyCreatedDBObject(resultPline, true);
  66.                         //删除起始直线段
  67.                         currentLine.Erase();
  68.                     }
  69.                     //处理完毕,跳出循环
  70.                     if (lineObjectIds.Count == 0)
  71.                         break;
  72.                 }
  73.                 tr.Commit();
  74.             }
  75.         }
  76.         /// <summary>
  77.         /// 线段连接成多段线递归循环部分
  78.         /// V1.0 by WeltionChen @2011.02.17
  79.         /// </summary>
  80.         /// <param name="lineObjectIds">线段的objectid集合</param>
  81.         /// <param name="tr">transaction</param>
  82.         /// <param name="plinePoints">多段线顶点坐标,也是各线段的端点坐标集合</param>
  83.         /// <param name="currentLineId">当前线段的objectid</param>
  84.         void JionLinesToPline(ref List<ObjectId> lineObjectIds, Transaction tr, ref List<Point3d> plinePoints, ObjectId currentLineId, ObjectId startLineId)
  85.         {
  86.             //提取端点
  87.             Point3d lastPoint = plinePoints[plinePoints.Count - 1];
  88.             Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
  89.             SelectionFilter sf = new SelectionFilter(new TypedValue[] { new TypedValue(0, "Line") });
  90.             //通过点创建选集
  91.             PromptSelectionResult selectLinesResult = ed.SelectCrossingWindow(lastPoint, lastPoint, sf);
  92.             if (selectLinesResult.Status == PromptStatus.OK)
  93.             {
  94.                 List<ObjectId> selectedLinesId = new List<ObjectId>(selectLinesResult.Value.GetObjectIds());
  95.                 //剔除本身
  96.                 selectedLinesId.Remove(currentLineId);
  97.                 //处理相连的直线段
  98.                 if (selectedLinesId.Count == 1)
  99.                 {
  100.                     ObjectId selectedLineId = selectedLinesId[0];
  101.                     //处理过的直线段将不再作为起始线段,由集合中剔除
  102.                     if (selectedLineId != startLineId)
  103.                     {
  104.                         lineObjectIds.Remove(selectedLineId);
  105.                         Line selectedLine = tr.GetObject(selectedLineId, OpenMode.ForWrite) as Line;
  106.                         //添加顶点
  107.                         if (selectedLine.StartPoint == lastPoint)
  108.                         {
  109.                             plinePoints.Add(selectedLine.EndPoint);
  110.                         }
  111.                         else
  112.                         {
  113.                             plinePoints.Add(selectedLine.StartPoint);
  114.                         }
  115.                         //递归继续搜索
  116.                         JionLinesToPline(ref lineObjectIds, tr, ref plinePoints, selectedLineId, startLineId);
  117.                         //删除中间线段
  118.                         selectedLine.Erase();
  119.                     }                    
  120.                 }
  121.             }
  122.         }

评分

参与人数 1威望 +1 明经币 +1 收起 理由
雪山飞狐_lzh + 1 + 1

查看全部评分

发表于 2011-2-17 18:17 | 显示全部楼层
好像和打印无关?
ps:WeltionChen既是水手?
 楼主| 发表于 2011-2-17 18:19 | 显示全部楼层
呵呵,这是打印前期处理,到后面就清楚了
 楼主| 发表于 2011-2-17 18:23 | 显示全部楼层
添加格式后看起来很乱,我一开始也是添加了格式,后来取消的
发表于 2011-2-18 08:04 | 显示全部楼层
图框识别呀,赞一个俺先慢慢消化,期待楼主下文~~~
发表于 2011-2-21 11:48 | 显示全部楼层
太好了!这个方法有C#版的了,谢谢水老哥!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-8 04:41 , Processed in 0.308988 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表