Author Topic: Ex: Plot  (Read 4204 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Ex: Plot
« on: September 29, 2010, 06:58:12 AM »
I have seen this question asked at different forums so here is a simple example plotting to pdf.
It should plot a pdf file in the same folder your drawing is in. Use a saved drawing.
I got an error a couple of times but I believe it was having windows explorer open and had the pdf selected which shows the preview in the preview box and it was unable to generate the graphics.

It is pretty much how it is in the Developers guide but for some reason he does things out of order which for me generates an error.
You can look at the Developers Guide if you want to add a plot dialog box.
If you look in the docs it will tell what order and what you need set.

All you should have to do is add a reference to System.Windows.Forms

I did not feel like typing it out in VB so I used http://www.developerfusion.com/tools/convert/csharp-to-vb/ which did a good job and usually you have to do a little cleanup but in this case I did not.

C# Code

Code: [Select]
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.PlottingServices;
using System.IO;


[assembly: CommandClass(typeof(PlotExample.MyCommands))]

namespace PlotExample
{

   
    public class MyCommands
    {
       
        [CommandMethod("PlotEx")]
        public void PlotEx()
        {           
                Application.SetSystemVariable("BACKGROUNDPLOT", 0);
                using (Transaction tr = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction())
                {               
                try
                {                   
                    LayoutManager layMgr = LayoutManager.Current;
                    ObjectId loObjId = layMgr.GetLayoutId(layMgr.CurrentLayout);
                    Layout lo = (Layout)tr.GetObject(loObjId, OpenMode.ForRead);
                    PlotSettings ps = new PlotSettings(lo.ModelType);
                    ps.CopyFrom(lo);                   
                    PlotInfo pi = new PlotInfo();
                    pi.Layout = loObjId;
                    PlotSettingsValidator psv = Autodesk.AutoCAD.DatabaseServices.PlotSettingsValidator.Current;
                    psv.SetPlotConfigurationName(ps, "DWG To PDF.pc3", "ANSI_B_(11.00_x_17.00_Inches)");
                    psv.RefreshLists(ps);
                    psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);
                    psv.SetUseStandardScale(ps, true);
                    psv.SetStdScaleType(ps, StdScaleType.ScaleToFit);
                    pi.OverrideSettings = ps;                     
                    PlotEngine pe = PlotFactory.CreatePublishEngine();
                    try
                    {                       
                        pe.BeginPlot(null, null);
                        PlotInfoValidator validator = new PlotInfoValidator();
                        validator.MediaMatchingPolicy = Autodesk.AutoCAD.PlottingServices.MatchingPolicy.MatchEnabled;
                        validator.Validate(pi);                       
                        string plotFile = Path.GetFullPath(Application.DocumentManager.MdiActiveDocument.Database.Filename);
                        plotFile = plotFile.Remove(plotFile.Length - 4);
                        pe.BeginDocument(pi, Application.DocumentManager.MdiActiveDocument.Database.Filename, null, 1, true, plotFile + ".pdf");                     
                        PlotPageInfo pageInfo = new PlotPageInfo();
                        pe.BeginPage(pageInfo, pi, true, null);
                        pe.BeginGenerateGraphics(null);
                        pe.EndGenerateGraphics(null);
                        pe.EndPage(null);                       
                        pe.EndDocument(null);
                        pe.EndPlot(null);
                    }
                    catch (System.Exception ex)
                    {
                        System.Windows.Forms.MessageBox.Show(ex.Message);
                    }                   
                    pe.Destroy();
                    tr.Commit();
                }
                catch (System.Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }           
            }
        }


    }

}


VB code

Code: [Select]
Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.PlottingServices
Imports System.IO
<Assembly: CommandClass(GetType(PlotExampleVB.MyCommands))>

Namespace PlotExampleVB
    Public Class MyCommands

        <CommandMethod("PlotExVB")> _
        Public Sub PlotExVB()
            Application.SetSystemVariable("BACKGROUNDPLOT", 0)
            Using tr As Transaction = Application.DocumentManager.MdiActiveDocument.TransactionManager.StartTransaction()
                Try
                    Dim layMgr As LayoutManager = LayoutManager.Current
                    Dim loObjId As ObjectId = layMgr.GetLayoutId(layMgr.CurrentLayout)
                    Dim lo As Layout = DirectCast(tr.GetObject(loObjId, OpenMode.ForRead), Layout)
                    Dim ps As New PlotSettings(lo.ModelType)
                    ps.CopyFrom(lo)
                    Dim pi As New PlotInfo()
                    pi.Layout = loObjId
                    Dim psv As PlotSettingsValidator = Autodesk.AutoCAD.DatabaseServices.PlotSettingsValidator.Current
                    psv.SetPlotConfigurationName(ps, "DWG To PDF.pc3", "ANSI_B_(11.00_x_17.00_Inches)")
                    psv.RefreshLists(ps)
                    psv.SetPlotType(ps, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents)
                    psv.SetUseStandardScale(ps, True)
                    psv.SetStdScaleType(ps, StdScaleType.ScaleToFit)
                    pi.OverrideSettings = ps
                    Dim pe As PlotEngine = PlotFactory.CreatePublishEngine()
                    Try
                        pe.BeginPlot(Nothing, Nothing)
                        Dim validator As New PlotInfoValidator()
                        validator.MediaMatchingPolicy = Autodesk.AutoCAD.PlottingServices.MatchingPolicy.MatchEnabled
                        validator.Validate(pi)
                        Dim plotFile As String = Path.GetFullPath(Application.DocumentManager.MdiActiveDocument.Database.Filename)
                        plotFile = plotFile.Remove(plotFile.Length - 4)
                        pe.BeginDocument(pi, Application.DocumentManager.MdiActiveDocument.Database.Filename, Nothing, 1, True, plotFile & ".pdf")
                        Dim pageInfo As New PlotPageInfo()
                        pe.BeginPage(pageInfo, pi, True, Nothing)
                        pe.BeginGenerateGraphics(Nothing)
                        pe.EndGenerateGraphics(Nothing)
                        pe.EndPage(Nothing)
                        pe.EndDocument(Nothing)
                        pe.EndPlot(Nothing)
                    Catch ex As System.Exception
                        System.Windows.Forms.MessageBox.Show(ex.Message)
                    End Try
                    pe.Destroy()
                    tr.Commit()
                Catch ex As System.Exception
                    System.Windows.Forms.MessageBox.Show(ex.Message)
                End Try
            End Using
        End Sub

    End Class

End Namespace

fixo

  • Guest
Re: Ex: Plot
« Reply #1 on: September 29, 2010, 08:13:15 AM »
Thanks for the posting a solution
Working nice on A2009

Regards,

~'J'~

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Ex: Plot
« Reply #2 on: September 29, 2010, 10:06:33 AM »
No problem I tested on 2011