AutoCAD按坐标打印图纸
前几天公司要求按坐标打印DWG文件,中间走了不少弯路,好在已经搞定了,整理一下分享给大家,希望后来人少走弯路。
1. 设计需求:
公司的图纸用AutoCAD2010做成,通常一个项目的所有图纸都存放在一个DWG文件内,根据具体的子项不同,放在不同的块引用里,我要做的是找到每一个块引用,并把他打印到bmp文件里。
2.实现思路:
利用AutoCAD的.net API,找到符合条件的快引用,得到块引用左下角和右上角的点的坐标,把两点坐标框选的矩形区域发给打印机打印
3.遇到的问题
有的图纸打印没有问题,有的图纸打印出一张空白图。
4.解决问题(在这里向业界大牛Kean同志表示强烈的感谢,感谢他无私的帮助)
之所以有些图纸打印出白图是因为坐标体系的问题,AutoCAD中有很多种坐标体系,例如UCS世界坐标,DCS显示设备坐标,UCS用户坐标等等,除了UCS坐标其他各坐标体系都是UCS坐标推衍而来,UCS坐标是永远不变的。.net API取出的块引用坐标是UCS,而要向打印机输出坐标打印命令需要用DCS坐标。也就是说不把UCS转换为DCS,图纸就会打印出白图,碰巧打印出来的是因为UCS和DCS重合。
很遗憾,AutoCAD .net API并没有转换坐标的函数,幸运的是ObjectARX 中有acedTrans()函数,于是先要把此函数引入我们自己的工程,加入如下代码
public partial class BlockSelectForm : Form
{
[DllImport("acad.exe",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "acedTrans")
]
static extern int acedTrans(
double[] point,
IntPtr fromRb,
IntPtr toRb,
int disp,
double[] result
);
....
private Extents2d Ucs2Dcs(Point3d objStart,Point3d objEnd)
{
ResultBuffer rbFrom =
new ResultBuffer(new TypedValue(5003, 1)),
rbTo =
new ResultBuffer(new TypedValue(5003, 2)); double[] firres = new double[] { 0, 0, 0 };
double[] secres = new double[] { 0, 0, 0 };
acedTrans(
objStart.ToArray(),
rbFrom.UnmanagedObject,
rbTo.UnmanagedObject,
0,
firres
); acedTrans(
objEnd.ToArray(),
rbFrom.UnmanagedObject,
rbTo.UnmanagedObject,
0,
secres
); Extents2d window =
new Extents2d(
firres[0],
firres[1],
secres[0],
secres[1]
);
return window;
}
下面是打印函数代码
private string PrintBMP(Point3d objStart, Point3d objEnd, string strPrintName, string strPaperName, string strStyleName,
string strRotation)
{
// 打开文档数据库
Document acDoc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
Extents2d objPoint = Ucs2Dcs(objStart, objEnd);
string strFileName = string.Empty; using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTableRecord btr =
(BlockTableRecord)acTrans.GetObject(
acCurDb.CurrentSpaceId,
OpenMode.ForRead
); Layout acLayout =
(Layout)acTrans.GetObject(
btr.LayoutId,
OpenMode.ForRead
); // Get the PlotInfo from the layout
PlotInfo acPlInfo = new PlotInfo();
acPlInfo.Layout = btr.LayoutId; // Get a copy of the PlotSettings from the layout
PlotSettings acPlSet = new PlotSettings(acLayout.ModelType);
acPlSet.CopyFrom(acLayout); // Update the PlotSettings object
PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current; acPlSetVdr.SetPlotWindowArea(acPlSet, objPoint);
// Set the plot type
acPlSetVdr.SetPlotType(acPlSet,
Autodesk.AutoCAD.DatabaseServices.PlotType.Window); // Set the plot scale
acPlSetVdr.SetUseStandardScale(acPlSet, true);
acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit); // Center the plot
acPlSetVdr.SetPlotCentered(acPlSet, true); // Set the plot device to use
acPlSetVdr.SetPlotConfigurationName(acPlSet, strPrintName,
strPaperName); acPlSetVdr.RefreshLists(acPlSet);
acPlSetVdr.SetCurrentStyleSheet(acPlSet, strStyleName); switch (strRotation)
{
case "0":
acPlSetVdr.SetPlotRotation(acPlSet,PlotRotation.Degrees000);
break;
case "90":
acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees090);
break;
case "180":
acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees180);
break;
case "270":
acPlSetVdr.SetPlotRotation(acPlSet, PlotRotation.Degrees270);
break;
} // Set the plot info as an override since it will
// not be saved back to the layout
acPlInfo.OverrideSettings = acPlSet; // Validate the plot info
PlotInfoValidator acPlInfoVdr = new PlotInfoValidator();
acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
acPlInfoVdr.Validate(acPlInfo); // Check to see if a plot is already in progress
if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
{
using (PlotEngine acPlEng = PlotFactory.CreatePublishEngine())
{
// Track the plot progress with a Progress dialog
PlotProgressDialog acPlProgDlg = new PlotProgressDialog(false,
1,
false); using (acPlProgDlg)
{
// Define the status messages to display when plotting starts
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle,
"我们公司名,隐去了"); acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage,
"Cancel Job"); acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage,
"Cancel Sheet"); acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption,
"Sheet Set Progress"); acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption,
"正在生成" + acDoc.Name); // Set the plot progress range
acPlProgDlg.LowerPlotProgressRange = 0;
acPlProgDlg.UpperPlotProgressRange = 100;
acPlProgDlg.PlotProgressPos = 0; // Display the Progress dialog
acPlProgDlg.OnBeginPlot();
acPlProgDlg.IsVisible = true; // Start to plot the layout
acPlEng.BeginPlot(acPlProgDlg, null); string strTempPath = System.IO.Path.GetTempPath();
strFileName = Path.Combine(strTempPath,
acDoc.Name.Substring(acDoc.Name.LastIndexOf("\\")+1).Replace("dwg","")
+ DateTime.Now.ToString("yyyyMMddhhmmssfff") + "Compare" + ".bmp"); // Define the plot output
acPlEng.BeginDocument(acPlInfo,
acDoc.Name,
null,
1,
true,
strFileName); // Display information about the current plot
acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status,
"Plotting: " + acDoc.Name + " - " +
acLayout.LayoutName); // Set the sheet progress range
acPlProgDlg.OnBeginSheet();
acPlProgDlg.LowerSheetProgressRange = 0;
acPlProgDlg.UpperSheetProgressRange = 100;
acPlProgDlg.SheetProgressPos = 0; // Plot the first sheet/layout
PlotPageInfo acPlPageInfo = new PlotPageInfo();
acPlEng.BeginPage(acPlPageInfo,
acPlInfo,
true,
null); acPlEng.BeginGenerateGraphics(null);
acPlEng.EndGenerateGraphics(null); // Finish plotting the sheet/layout
acPlEng.EndPage(null);
acPlProgDlg.SheetProgressPos = 100;
acPlProgDlg.OnEndSheet(); // Finish plotting the document
acPlEng.EndDocument(null); // Finish the plot
acPlProgDlg.PlotProgressPos = 100;
acPlProgDlg.OnEndPlot();
acPlEng.EndPlot(null);
}
}
}
}
return strFileName;
}
先写这么多了,欢迎交流吐槽,
AutoCAD按坐标打印图纸的更多相关文章
- 如何在CAD中批量打印图纸?这种方法你要知道
CAD图纸都是使用CAD制图软件进行设计出来的,图纸的格式均为dwg格式的,不方便进行使用.就需要将图纸进行打印出来.多张CAD图纸如果一张一张进行打印速度就会非常的慢,那就可以使用CAD中的批量打印 ...
- CAD打印图纸要怎么操作?简单方法分享给你
大家日常生活中多多少少的都接触到过CAD文件,CAD图是借助CAD制图软件来进行绘制完成的.唯一的困惑就是CAD图纸的格式大多数均为dwg格式的,查看起来不是那么的方便?所以很多设计师们都会选择将图纸 ...
- autocad.net-图片打印合成
调用打印程序“PublishToWeb JPG.pc3”进行图片打印,该打印驱动程序中内置了很多的打印方案,在同尺寸的打印方案下,数据范围越大打印出来的清晰度就越差,内置的尺寸不一定都满足,在又要通过 ...
- Excel控制AutoCad进行坐标标注
做过工程测绘,平面设计,使用过Autocad制图的朋友们,都经常要在CAD上标注点或者线的坐标,CAD自身的标注功能,并不能同时标注X和Y坐标,,要同时标注X和Y坐标,可以使用南方CASS软件,或者一 ...
- AutoCAD如何设置A0A1图纸
可以从网上下载相应的图纸模板,下载之后可以发现有相应的文字和模板文件 随后我们新建并找到这个dwt文件模板(比如要做一个A1的模板) 随后即可发现模板的样式,包括每种颜色的粗细,颜色和明细栏等 ...
- CAD中解决打印图纸模糊而且有的字体深浅不一的方法
按圈圈中选择打印样式
- AutoCAD图形打印出图片 C#
这几天搞cad二次开发,用的是C#语言,目前在网上找到的资料比较少.弄了两天,才做出怎样实现打印出图片.首先得在AutoCAD软件界面下,设置打印机的页面设置和打印机设备名称一样(以防打印不出来).即 ...
- objectARX2010及其以上版本使用publish打印(发布)图纸,后台布局打印图纸例子浅析
AutoCAD 2010版本开始新增了一个发布图纸的功能,可以后台打印图纸,以下是ADN官方博客例子浅析 原文地址 https://adndevblog.typepad.com/autocad/201 ...
- 用Python来控制Autocad的打印------以Pycomcad为例
from pycomcad import * #以pycomcad作为接口库为例 import win32com acad=Autocad() 打印最重要的设置都在上面的界面中,下面对这些个界面,用P ...
随机推荐
- Linux 下C++编写
今天搞了一天Linux下C++编程,还没有什么成效.好烦躁好心焦,想砸电脑的冲动.抽根烟理下思路一定要把它拿下!! ===搞了两天,真是搞到生无可恋,试了共享文件, 试了网络配置,各种博客就是各种行不 ...
- 在JSP中使用CKEditor网页编辑器
为了在我的一个项目使用CKEditor网页编辑器,我开始了寻找应用之法. 我下载了ckeditor_4.3.3_standard和ckeditor-java-core-3.5.3. 之前的版本和现在版 ...
- RSS阅读器&BT sync
①RSS阅读器? 答:RSS阅读器是一种软件或是说一个程序,这种软件可以自由读取RSS和Atom两种规范格式的文档,且这种读取RSS和Atom文档的软件有多个版本,由不同的人或公司开发,有着不同的名字 ...
- C_functions
1.C常用函数分为如下几大类!! 1,字符测试函数. 2,字符串操作 3,内存管理函数 4,日期与时间函数 5,数学函数 6,文件操作函数 7,进程管理函数 8,文件权限控制 9,信号处理 10,接口 ...
- Struts2文件下载
1). Struts2 中使用 type="stream" 的 result 进行下载 2). 可以为 stream 的 result 设定如下参数 contentType: 结果 ...
- Message Forwarding
[Preprocess] 在使用forwarding机制前,会先经历2个步骤,只有当这2个步骤均失败的情况下,才会激活forwarding. 1.+(BOOL)resolveInstanceMetho ...
- reentrant可重入函数
在多任务操作系统环境中,应用程序的各个任务是并发运行的,所以会经常出现多个任务“同时”调用同一个函数的情况.这里之所以在“同时” 这个词上使用了引号,是因为这个歌”同时“的含义与我们平时所说的同时不是 ...
- postconf 命令常用参数
postfix的main.cf配置文件一般不直接编辑,而多使用postconf命令来配置‘ postconf -d:查看默认配置: postconf -n:查看当前配置(即当前生效的配置): post ...
- Windows 2003 服务器安全设置-批处理 (附参考链接)
长期维护windows服务器终结出来的安全设置批处理与大家分享,复制以下全部内容用记事本另存为bat或者cmd执行 ===================分隔符号=================== ...
- unigui unidbgrid显示列的合计值
procedure TfrmClient.UniDBGrid1ColumnSummaryResult(Column: TUniDBGridColumn; GroupFieldValue: Varian ...