Driving a basic AutoCAD plot using .NET

转载 2007年10月15日 14:00:00
I just missed my connecting flight in Chicago, so have 3 hours to pass until the next, and decided to post some code I finally got around to writing on the plane from Zurich.

I've had a few requests for code showing how to plot using the .NET API in AutoCAD. There's an existing ObjectARX (C++) sample on the SDK, under samples/editor/AsdkPlotAPI, but there isn't a publicly posted .NET version right now.

Here's the C# code I put together. Please bear in mind that it was written during less than ideal coding conditions, and I haven't spent a substantial amount of time going through it... it seems to work fine for me, but do post a comment if you have trouble with it.

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Geometry;

using Autodesk.AutoCAD.PlottingServices;


namespace PlottingApplication

{

  public class PlottingCommands

  {

    [CommandMethod("simplot")]

    static public void SimplePlot()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;

      Database db = doc.Database;


      Transaction tr =

        db.TransactionManager.StartTransaction();

      using (tr)

      {

        // We'll be plotting the current layout


        BlockTableRecord btr =

          (BlockTableRecord)tr.GetObject(

            db.CurrentSpaceId,

            OpenMode.ForRead

          );

        Layout lo =

          (Layout)tr.GetObject(

            btr.LayoutId,

            OpenMode.ForRead

          );


        // We need a PlotInfo object

        // linked to the layout


        PlotInfo pi = new PlotInfo();

        pi.Layout = btr.LayoutId;


        // We need a PlotSettings object

        // based on the layout settings

        // which we then customize


        PlotSettings ps =

          new PlotSettings(lo.ModelType);

        ps.CopyFrom(lo);


        // The PlotSettingsValidator helps

        // create a valid PlotSettings object


        PlotSettingsValidator psv =

          PlotSettingsValidator.Current;


        // We'll plot the extents, centered and

        // scaled to fit


        psv.SetPlotType(

          ps,

          Autodesk.AutoCAD.DatabaseServices.PlotType.Extents

        );

        psv.SetUseStandardScale(ps, true);

        psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);

        psv.SetPlotCentered(ps, true);


        // We'll use the standard DWF PC3, as

        // for today we're just plotting to file


        psv.SetPlotConfigurationName(

          ps,

          "DWF6 ePlot.pc3",

          "ANSI_A_(8.50_x_11.00_Inches)"

        );


        // We need to link the PlotInfo to the

        // PlotSettings and then validate it


        pi.OverrideSettings = ps;

        PlotInfoValidator piv =

          new PlotInfoValidator();

        piv.MediaMatchingPolicy =

          MatchingPolicy.MatchEnabled;

        piv.Validate(pi);


        // A PlotEngine does the actual plotting

        // (can also create one for Preview)


        if (PlotFactory.ProcessPlotState ==

            ProcessPlotState.NotPlotting)

        {

          PlotEngine pe =

            PlotFactory.CreatePublishEngine();

          using (pe)

          {

            // Create a Progress Dialog to provide info

            // and allow thej user to cancel


            PlotProgressDialog ppd =

              new PlotProgressDialog(false, 1, true);

            using (ppd)

            {

              ppd.set_PlotMsgString(

                PlotMessageIndex.DialogTitle,

                "Custom Plot Progress"

              );

              ppd.set_PlotMsgString(

                PlotMessageIndex.CancelJobButtonMessage,

                "Cancel Job"

              );

              ppd.set_PlotMsgString(

                PlotMessageIndex.CancelSheetButtonMessage,

                "Cancel Sheet"

              );

              ppd.set_PlotMsgString(

                PlotMessageIndex.SheetSetProgressCaption,

                "Sheet Set Progress"

              );

              ppd.set_PlotMsgString(

                PlotMessageIndex.SheetProgressCaption,

                "Sheet Progress"

              );

              ppd.LowerPlotProgressRange = 0;

              ppd.UpperPlotProgressRange = 100;

              ppd.PlotProgressPos = 0;


              // Let's start the plot, at last


              ppd.OnBeginPlot();

              ppd.IsVisible = true;

              pe.BeginPlot(ppd, null);


              // We'll be plotting a single document


              pe.BeginDocument(

                pi,

                doc.Name,

                null,

                1,

                true, // Let's plot to file

                "c://test-output"

              );


              // Which contains a single sheet


              ppd.OnBeginSheet();


              ppd.LowerSheetProgressRange = 0;

              ppd.UpperSheetProgressRange = 100;

              ppd.SheetProgressPos = 0;


              PlotPageInfo ppi = new PlotPageInfo();

              pe.BeginPage(

                ppi,

                pi,

                true,

                null

              );

              pe.BeginGenerateGraphics(null);

              pe.EndGenerateGraphics(null);


              // Finish the sheet

              pe.EndPage(null);

              ppd.SheetProgressPos = 100;

              ppd.OnEndSheet();


              // Finish the document


              pe.EndDocument(null);


              // And finish the plot


              ppd.PlotProgressPos = 100;

              ppd.OnEndPlot();

              pe.EndPlot(null);

            }

          }

        }

        else

        {

          ed.WriteMessage(

            "/nAnother plot is in progress."

          );

        }

      }

    }

  }

}

A few comments on the code: I chose to plot to a file using the standard DWF plot driver/PC3 file, mainly as it avoids having to second-guess what printers/plotters everyone uses. :-) The SDK sample populates comboboxes in a dialog to allow selection of the device and the media size, but I've just gone with a choice that should work on all systems.

Here's what you should see when you launch the SIMPLOT command:

Plot_progress

The output file should be created in "c:/test-output.dwf".

I added a simple check to fail gracefully when another plot is in progress... as this code will drive either a foreground or a background plot (depending on the value of the BACKGROUNDPLOT system variable), there's clear scope for failure if the code doesn't check via PlotFactory.ProcessPlotState (or handle the exception if another plot is already happening when you call PlotFactory.CreatePublishEngine()).

Now that I'm online I've found there's quite a similar code sample to the ADN site (also converted from the original SDK sample, I suspect):

How to plot using AutoCAD's .NET Managed API?

Driving a multi-sheet AutoCAD plot using .NET

Somewhat symmetrically I’m posting this from Chicago airport, once again, but thankfully I’m now on ...
  • sdphg
  • sdphg
  • 2007-10-15 14:01:00
  • 1256

Visual Basic.NET二次开发AutoCAD(电子书)

  • 2011年11月20日 18:33
  • 8.74MB
  • 下载

Using a modeless .NET dialog to display properties of multiple AutoCAD objects

After a few comments on this previous post I decided that, rather than rushing on to show palettes,...
  • sdphg
  • sdphg
  • 2007-07-13 11:54:00
  • 1247

Using a palette from .NET to display properties of multiple AutoCAD objects

After a brief interlude were back on the series of posts showing how to implement basic user-interf...
  • sdphg
  • sdphg
  • 2007-07-20 16:22:00
  • 1644

Altium Designer-Net has no driving source警告消除的方法

1.其实这个警告原因是: 你图中有一个器件的管脚有属性(如I/O),并且这个管脚设定了驱动源,                     你先从元件库中,找到这个管脚,把管脚的属性 改成下面图片 ...
  • u013385925
  • u013385925
  • 2016-04-16 14:40:24
  • 3506

Autodesk AutoCAD 二次开发视频讲座一键下载 (大量视频课程)

第二栏是语言。 Webcast Date AutoCAD .Net - Session 2 English 13-Sep-12 AutoCAD .Net - Ses...
  • JoeXiongjin
  • JoeXiongjin
  • 2013-05-10 13:37:13
  • 4579

用.NET的面板来显示多个AutoCAD实体的属性

原文:Using a palette from .NET to display properties of multiple AutoCAD objects 本文仅翻译部分内容 在这篇文章中我们将换掉...
  • u011511587
  • u011511587
  • 2013-12-04 00:10:40
  • 3032

《Visual Basic与AutoCAD二次开发》

  • 2010年05月21日 11:40
  • 7.17MB
  • 下载

Altium Designer 消除Net has no driving source警告的方法

在进行原理图编译的时候提示警告:Net has no driving source  如下图: 解决方法:点击Place----Directives-----No ERC(不进行电气规则检查) ...
  • yueniaoshi
  • yueniaoshi
  • 2012-08-01 20:23:25
  • 57968

ISE编译错误记录

1.在Translate过程中出现如下错误:"ERROR:ConstraintSystem:59 - Constraint: NET/INST "xxx" 未找到。Please verify that...
  • tianhen791
  • tianhen791
  • 2014-02-21 15:00:04
  • 9250
收藏助手
不良信息举报
您举报文章: Driving a basic AutoCAD plot using .NET
举报原因:
原因补充:

(最多只允许输入30个字)