首页 > 代码库 > 各种PDF转换问题(三).将AutoCAD图纸转为PDF文档

各种PDF转换问题(三).将AutoCAD图纸转为PDF文档

        目前国内企业生产的自动化程度并不是很高,大多数都无法使用三维图直接进行加工生产,所以二维图对于大多数企业来说,仍然是最根本的生产依据。那么,在企业中,二维图转PDF就是一个很重要的小步骤,因为后续的打印以及企业之间的交流,都离不开由二维图转出来的PDF文档。

        在三维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库:

        /// <summary>
        /// 通过自定义打印参数获取ACAD内部打印机对象参数
        /// </summary>
        /// <param name="document">图纸对象</param>
        /// <param name="plotSetting">自定义打印参数</param>
        /// <returns>内置打印机参数</returns>
        public static AcadPlotConfiguration 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(new double[] { pageSize.LowerLeftX, pageSize.LowerLeftY, 0 }, AcCoordinateSystem.acWorld, AcCoordinateSystem.acDisplayDCS, 0, Type.Missing) as double[];
            double[] UpperRight = document.Utility.TranslateCoordinates(new double[] { pageSize.UpperRightX, pageSize.UpperRightY, 0 }, AcCoordinateSystem.acWorld, AcCoordinateSystem.acDisplayDCS, 0, Type.Missing) as double[];
            plotConfiguration.SetWindowToPlot(new double[] { LowerLeft[0], LowerLeft[1] }, new double[] { 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;   //隐藏图元
            return plotConfiguration;
        }

        /// <summary>
        /// 打印图纸的模型空间到PDF文件
        /// </summary>
        /// <param name="document">图纸对象</param>
        /// <param name="plotSetting">PDF打印机设置</param>
        public static void goPlotting(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库:

        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);
        }

COM库的特点,主要是可以用一个外部程序来启动并操作AutoCAD,便于和其它软件集成;而.NET库的特点,主要是它有事务之类的东西,相对COM库,某些操作(比如遍历模型库)会快很多,只可惜它一般只能在AutoCAD内部进程使用。

各种PDF转换问题(三).将AutoCAD图纸转为PDF文档