在三维CAD设计软件中,二维图转PDF一般是一个软件的内置功能,所以这些软件一般都会直接提供一个API,来实现转PDF的功能。如PROE的TOOLKIT库,NX的NXOpen库(注意不是C库,而是C++库。NX的C库中似乎是没有转PDF的功能的)。但是AutoCAD这个大名鼎鼎,领导世界二维图设计标准的软件,却没有内置的一个转PDF功能。
早期版本的AutoCAD本身是完全不支持PDF转换的,那时候若要实现DWG图纸转PDF,要么使用第三方的库OpenDWG(早期名叫DWG DirectX)转PDF,要么就使用Adobe Acrobat打印机将图纸打印成PDF。后来AutoCAD终于内置了……一个将图纸打印成PDF的打印机。OpenDWG库转PDF以及Acrobat打印机打印PDF我都没研究过,我主要用的是AutoCAD自带的DWG TO PDF打印机。
使用DWG TO PDF打印机打印图纸,大致有两种方式:基于AutoCAD COM库的调用(C++中使用ObjectARX来做这个事情我没研究过)和基于AutoCAD .NET库的调用。
COM库:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455/// <summary>/// 通过自定义打印参数获取ACAD内部打印机对象参数/// </summary>/// <param name="document">图纸对象/// <param name="plotSetting">自定义打印参数/// <returns>内置打印机参数</returns>publicstaticAcadPlotConfiguration getPlotConfiguration(AcadDocument document, PlotSetting plotSetting){PageSize pageSize = plotSetting.PageSize;AcadPlotConfiguration plotConfiguration = document.PlotConfigurations.Add(Guid.NewGuid().ToString("N"), document.ActiveLayout.ModelType);plotConfiguration.ConfigName = plotSetting.Printer;//打印机名plotConfiguration.StyleSheet = plotSetting.StyleSheet;//样式表名plotConfiguration.PlotWithLineweights =true;//线宽比例plotConfiguration.PlotWithPlotStyles =true;//使用样式plotConfiguration.CanonicalMediaName = pageSize.Name;//图纸尺寸double[] LowerLeft = document.Utility.TranslateCoordinates(newdouble[] { pageSize.LowerLeftX, pageSize.LowerLeftY,0}, AcCoordinateSystem.acWorld, AcCoordinateSystem.acDisplayDCS,0, Type.Missing) asdouble[];double[] UpperRight = document.Utility.TranslateCoordinates(newdouble[] { pageSize.UpperRightX, pageSize.UpperRightY,0}, AcCoordinateSystem.acWorld, AcCoordinateSystem.acDisplayDCS,0, Type.Missing) asdouble[];plotConfiguration.SetWindowToPlot(newdouble[] { LowerLeft[0], LowerLeft[1] },newdouble[] { UpperRight[0], UpperRight[1] });plotConfiguration.PlotType = AcPlotType.acWindow;//打印模式plotConfiguration.StandardScale = AcPlotScale.acScaleToFit;//标准比例,需要UseStandardScale = true;plotConfiguration.UseStandardScale =true;//configuration.UseStandardScale = false;//configuration.SetCustomScale(1, 30);plotConfiguration.CenterPlot =true;//居中打印plotConfiguration.PaperUnits = AcPlotPaperUnits.acMillimeters;//单位毫米plotConfiguration.PlotRotation = (pageSize.Rotation ? AcPlotRotation.ac90degrees : AcPlotRotation.ac0degrees);//横向纵向//plotConfiguration.PlotHidden = false; //隐藏图元returnplotConfiguration;}/// <summary>/// 打印图纸的模型空间到PDF文件/// </summary>/// <param name="document">图纸对象/// <param name="plotSetting">PDF打印机设置publicstaticvoidgoPlotting(AcadDocument document, PlotSetting plotSetting){if(AcActiveSpace.acPaperSpace == document.ActiveSpace){document.MSpace =true;document.ActiveSpace = AcActiveSpace.acModelSpace;}document.Regen(AcRegenType.acAllViewports);object oIsBackground = document.GetVariable("BACKGROUNDPLOT");AcadPlotConfiguration plotConfiguration = getPlotConfiguration(document, plotSetting);AcadLayout layout = document.ActiveLayout;layout.CopyFrom(plotConfiguration);layout.RefreshPlotDeviceInfo();document.SetVariable("BACKGROUNDPLOT",0);//前台打印document.Plot.QuietErrorMode =true;document.Plot.NumberOfCopies =1;document.Plot.PlotToFile(plotSetting.TargetFile, plotSetting.Printer);plotConfiguration.Delete();document.SetVariable("BACKGROUNDPLOT", oIsBackground);}
.net库:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static void goPlotting(PlotEngine engine, PlotInfo plotInfo, PlotPageInfo pageInfo, string sName, string sFile){ PlotInfoValidator validator = new PlotInfoValidator(); validator.MediaMatchingPolicy = MatchingPolicy.MatchEnabled; validator.Validate(plotInfo); engine.BeginPlot( null , null ); engine.BeginDocument(plotInfo, sName, null , 1 , true , sFile); engine.BeginPage(pageInfo, plotInfo, true , null ); engine.BeginGenerateGraphics( null ); engine.EndGenerateGraphics( null ); engine.EndPage( null ); engine.EndDocument( null ); engine.EndPlot( null );} |
版权声明:本文为Larry_666原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。