laceypants 发表于 2011-5-27 11:33:50

简单的 dwg 到 pdf 绘图

大家好,
我只是想在不打开AutoCAD或任何其他外部dwg查看器的情况下将dwg文件转换为pdf格式。我遵循了.NET开发指南中的概要(http://docs.autodesk.com/ACD/2010/ENU/AutoCAD%20.NET%20Developer这是%20Guide/index.html吗?url=WS1a9193826455f5ff2566ffd511ff6f8c7ca-33b0.htm,topicNumber=d0e50852)以及其他开发人员的帮助(
http://through-the-interface.typepad.com/through_the_interface/2007/10/previewing-and-.html
)但我已经被无数的错误所困扰
首先,打开外部文件的最佳格式是什么?文档管理员。Open()命令或数据库。readDWGFile()
其次,DocumentManager也会这样做。MdiActiveDocument=以前打开的文件,是否建立“当前”文件?每个解决方案都需要访问“当前”文档,这是否可行?
那么有什么意义吗?如果是这样,我将使用什么来打印/转换单个DWG文件?‘Simplot?
最后,表单“Autodesk.AutoCAD.ApplicationServices”出现错误的原因是什么。XXXXX是否没有实施?我确保所有引用的“复制本地”功能设置为False
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.ComponentModel;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.PlottingServices;
//using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Geometry;
using CAD_AS = Autodesk.AutoCAD.ApplicationServices;
namespace ......(ommitted)
{
    class AutoCADToPDF : (abstract class ommitted)
    {
      
      public AutoCADToPDF()
      { }
      
      public override string Convert(string filein)
      {
         
         //**FILE IN STRING ERROR CHECK HERE
         //**Make a try/catch statement
         //Open the drawing; read-only
            Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.Open(filein, true);
            

         //Establish as current, active document
         CAD_AS.Application.DocumentManager.MdiActiveDocument = dwg;
         //**Probably unecessary, but setting document to Active
         CAD_AS.Document doc = CAD_AS.Application.DocumentManager.MdiActiveDocument;
         
         //Initalize the editor
         Editor edit = doc.Editor;
         //Initalize the database by loading the drawing into it
         Database db = doc.Database;
            
            //Start a new transaction against the open drawing
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                //Reference the Layout Manager
                LayoutManager layman = LayoutManager.Current;
               
                //Open layout as read-only
                Layout layout= (Layout)tr.GetObject(layman.GetLayoutId(layman.CurrentLayout),OpenMode.ForRead);
                //Create plot info for layout
                PlotInfo plotInfo = new PlotInfo();
                plotInfo.Layout = layout.ObjectId;
                //Copy plot settings from layout
                PlotSettings settings = new PlotSettings(layout.ModelType);
                settings.CopyFrom(layout);
                //Retrieve the current plot settings validator
                PlotSettingsValidator setValidator = PlotSettingsValidator.Current;
                //Set Plot type
                setValidator.SetPlotType(settings, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
               
                //Set the Plot Scale and center the plot
                setValidator.SetUseStandardScale(settings, true);
                setValidator.SetStdScaleType(settings, StdScaleType.ScaleToFit);
                setValidator.SetPlotCentered(settings, true);
                //Set the plotter and media
                setValidator.SetPlotConfigurationName(settings, "DWG to PDF.pc3", "ANSI_A_(8.50 x 11.00 Inches)");
                //Link plot settings to plot info
                //**Override not needed?
                plotInfo.OverrideSettings = settings;
                PlotInfoValidator infoValidator = new PlotInfoValidator();
                infoValidator.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
                infoValidator.Validate(plotInfo);
                //Check to see if a plot is already in progress
                if(PlotFactory.ProcessPlotState != ProcessPlotState.NotPlotting)
                {
                  throw new ConvertException("Cannot proceed, another plot is in progress !");
                }
                //Plot the document
                using (PlotEngine engine = PlotFactory.CreatePublishEngine())
                {
                  //Silence the plot dialogue
                  PlotProgressDialog dlg = new PlotProgressDialog(false, 1, true);
                  dlg.OnBeginPlot();
                  dlg.IsVisible = false;
                  
                  //SPecify plot destination
                  engine.BeginPlot(dlg, null);
                  engine.BeginDocument(plotInfo, filein, null, 1, true, @"D:\");
                  //Plot the first sheet
                  PlotPageInfo pageInfo = new PlotPageInfo();
                  engine.BeginPage(pageInfo, plotInfo, true, null);
                  engine.BeginGenerateGraphics(null);
                  engine.EndGenerateGraphics(null);
                  //End plotting sheet
                  engine.EndPage(null);
                  engine.EndDocument(null);
                  //Finish plot
                  dlg.OnEndPlot();
                  engine.EndPlot(null);
                }
            }
            return @"D:\";
            return null;
      } //End Convert
    }
}

**** Hidden Message *****

owenwengerd 发表于 2011-5-27 16:45:16

。NET API将无法在正在运行的AutoCAD实例之外工作。您需要许可AutoCAD OEM,以便您可以构建自己的主机应用程序,或使用许多其他DWG库之一来读取和处理DWG文件。
页: [1]
查看完整版本: 简单的 dwg 到 pdf 绘图