Driving a multi-sheet AutoCAD plot using .NET

20 篇文章 0 订阅
Somewhat symmetrically I’m posting this from Chicago airport, once again, but thankfully I’m now on my way home. It was a busy week of meetings, but I did get the chance to put together some code that extended the last post into the realm of multi-sheet plot jobs.

The following code took some work, but I finally managed to iron out the obvious wrinkles and put together an approach to plot multiple sheets into a single document. The standard DWF6 driver doesn’t appear to support multiple sheet jobs (directly, at least), so I chose to use the DWFx driver that I probably downloaded and installed from here.

I haven’t “diffed” and colour-coded the changed lines with the previous post, as there ended up being quite a lot of swapping around etc., but you should be able to perform that comparison yourself, if you so wish.

Here’s the C# code:

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("mplot")]

    static public void MultiSheetPlot()

    {

      Document doc =

        Application.DocumentManager.MdiActiveDocument;

      Editor ed = doc.Editor;

      Database db = doc.Database;


      Transaction tr =

        db.TransactionManager.StartTransaction();

      using (tr)

      {

        BlockTable bt =

          (BlockTable)tr.GetObject(

            db.BlockTableId,

            OpenMode.ForRead

          );


        PlotInfo pi = new PlotInfo();

        PlotInfoValidator piv =

          new PlotInfoValidator();

        piv.MediaMatchingPolicy =

          MatchingPolicy.MatchEnabled;


        // 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)

            {

              ObjectIdCollection layoutsToPlot =

                new ObjectIdCollection();


              foreach (ObjectId btrId in bt)

              {

                BlockTableRecord btr =

                  (BlockTableRecord)tr.GetObject(

                    btrId,

                    OpenMode.ForRead

                  );

                if (btr.IsLayout &&

                    btr.Name.ToUpper() !=

                      BlockTableRecord.ModelSpace.ToUpper())

                {

                  layoutsToPlot.Add(btrId);

                }

              }


              int numSheet = 1;


              foreach (ObjectId btrId in layoutsToPlot)

              {

                BlockTableRecord btr =

                  (BlockTableRecord)tr.GetObject(

                    btrId,

                    OpenMode.ForRead

                  );

                Layout lo =

                  (Layout)tr.GetObject(

                    btr.LayoutId,

                    OpenMode.ForRead

                  );


                // 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 DWFx PC3, as

                // this supports multiple sheets


                psv.SetPlotConfigurationName(

                  ps,

                  "DWFx ePlot (XPS Compatible).pc3",

                  "ANSI_A_(8.50_x_11.00_Inches)"

                );


                // We need a PlotInfo object

                // linked to the layout


                pi.Layout = btr.LayoutId;


                // Make the layout we're plotting current


                LayoutManager.Current.CurrentLayout =

                  lo.LayoutName;


                // We need to link the PlotInfo to the

                // PlotSettings and then validate it


                pi.OverrideSettings = ps;

                piv.Validate(pi);


                if (numSheet == 1)

                {

                  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-multi-sheet"

                  );

                }


                // Which may contain multiple sheets


                ppd.StatusMsgString =

                  "Plotting " +

                  doc.Name.Substring(

                    doc.Name.LastIndexOf("//") + 1

                  ) +

                  " - sheet " + numSheet.ToString() +

                  " of " + layoutsToPlot.Count.ToString();


                ppd.OnBeginSheet();


                ppd.LowerSheetProgressRange = 0;

                ppd.UpperSheetProgressRange = 100;

                ppd.SheetProgressPos = 0;


                PlotPageInfo ppi = new PlotPageInfo();

                pe.BeginPage(

                  ppi,

                  pi,

                  (numSheet == layoutsToPlot.Count),

                  null

                );

                pe.BeginGenerateGraphics(null);

                ppd.SheetProgressPos = 50;

                pe.EndGenerateGraphics(null);


                // Finish the sheet

                pe.EndPage(null);

                ppd.SheetProgressPos = 100;

                ppd.OnEndSheet();

                numSheet++;

              }


              // 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."

          );

        }

      }

    }

  }

}

The output of the MPLOT command will be created in “c:/test-multi-sheet.dwfx”, which can then be viewed using Autodesk Design Review 2008 or the XPS viewer that ships with Windows Vista or from here for Windows XP.

Update

I spent some more time looking at this code and noticed a minor issue... We need to tell the plot dialog that we're working with multiple sheets in its constructor. So we first need to count the sheets and then create the dialog. Here's the modified section of code:

          PlotEngine pe =

            PlotFactory.CreatePublishEngine();

          using (pe)

          {

            // Collect all the paperspace layouts

            // for plotting

            ObjectIdCollection layoutsToPlot =

              new ObjectIdCollection();

            foreach (ObjectId btrId in bt)

            {

              BlockTableRecord btr =

                (BlockTableRecord)tr.GetObject(

                  btrId,

                  OpenMode.ForRead

                );

              if (btr.IsLayout &&

                  btr.Name.ToUpper() !=

                    BlockTableRecord.ModelSpace.ToUpper())

              {

                layoutsToPlot.Add(btrId);

              }

            }

            // Create a Progress Dialog to provide info

            // and allow thej user to cancel

            PlotProgressDialog ppd =

              new PlotProgressDialog(

                false,

                layoutsToPlot.Count,

                true

              );

            using (ppd)

            {

This now leads to the plot progress dialog showing multiple progress bars:

Multisheet_plot_progress

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值