<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Plot Preview from Database in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/plot-preview-from-database/m-p/10855741#M14207</link>
    <description>&lt;P&gt;I want plot preview not open dwg file, I meet three questions, I need help ,thank you everyone.&amp;nbsp; this is very important for me,help please.&lt;BR /&gt;questions:&lt;BR /&gt;1.first plot not right;&lt;BR /&gt;2.close preview, Layout note the one before preview;&lt;/P&gt;&lt;P&gt;3.preview again ,it's right.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; public class Startup
    {
        [CommandMethod("PlotPreView")]
        public void PlotPreView()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            var acEdit = acDoc.Editor;

            using (DocumentLock acLckDoc = acDoc.LockDocument())
            {
                var acDbSave = HostApplicationServices.WorkingDatabase;

                using (Database acDb = new Database(false, false))
                {
                    var localFile = "E:\\plotprevie.dwg";

                    acDb.ReadDwgFile(localFile, FileOpenMode.OpenForReadAndAllShare, false, null);
                    acDb.CloseInput(true);

                    using (Transaction acTrans = acDb.TransactionManager.StartTransaction())
                    {
                        HostApplicationServices.WorkingDatabase = acDb;

                        LayoutManager acLayoutMgr = LayoutManager.Current;

                        // 1D4 64B
                        string handle = "1D4";
                        long lng = Convert.ToInt64(handle, 16);
                        ObjectId objectId = acDb.GetObjectId(false, new Handle(lng), 0);
                        if (objectId == null) return;
                        BlockReference acBrf = acTrans.GetObject(objectId, OpenMode.ForRead) as BlockReference;
                        if (acBrf == null) return;

                        BlockTableRecord acBlkTblRec = acTrans.GetObject(acBrf.OwnerId, OpenMode.ForRead) as BlockTableRecord;
           
                        Layout acLayout = acBlkTblRec.LayoutId.GetObject(OpenMode.ForWrite) as Layout;

                        acLayoutMgr.CurrentLayout = acLayout.LayoutName;
                        acLayoutMgr.SetCurrentLayoutId(acLayout.ObjectId);

                        acEdit.WriteMessage(acLayoutMgr.CurrentLayout + "3\n");

                        using (PlotInfo acPlInfo = new PlotInfo())
                        {
                            acPlInfo.Layout = acLayout.ObjectId;

                            // Get a copy of the PlotSettings from the layout
                            using (PlotSettings acPlSet = new PlotSettings(acLayout.ModelType))
                            {
                                acPlSet.CopyFrom(acLayout);

                                // Update the PlotSettings object
                                PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;

                                // Set the plot type
                                acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);

                                // Set the plot scale
                                acPlSetVdr.SetUseStandardScale(acPlSet, true);
                                acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);

                                // Center the plot
                                acPlSetVdr.SetPlotCentered(acPlSet, true);

                                // Set the plot device to use
                                acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWF6 ePlot.pc3", "ANSI_A_(8.50_x_11.00_Inches)");

                                // Set the plot info as an override since it will
                                // not be saved back to the layout
                                acPlInfo.OverrideSettings = acPlSet;

                                // Validate the plot info
                                using (PlotInfoValidator acPlInfoVdr = new PlotInfoValidator())
                                {
                                    acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
                                    acPlInfoVdr.Validate(acPlInfo);

                                    // Check to see if a plot is already in progress
                                    if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
                                    {
                                        using (PlotEngine acPlEng = PlotFactory.CreatePreviewEngine((int)PreviewEngineFlags.Plot))
                                        {
                                            // Track the plot progress with a Progress dialog
                                            using (PlotProgressDialog acPlProgDlg = new PlotProgressDialog(true, 1, true))
                                            {
                                                using ((acPlProgDlg))
                                                {
                                                    // Define the status messages to display 
                                                    // when plotting starts
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot Progress");
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");

                                                    // Set the plot progress range
                                                    acPlProgDlg.LowerPlotProgressRange = 0;
                                                    acPlProgDlg.UpperPlotProgressRange = 100;
                                                    acPlProgDlg.PlotProgressPos = 0;

                                                    // Display the Progress dialog
                                                    acPlProgDlg.OnBeginPlot();
                                                    acPlProgDlg.IsVisible = true;

                                                    // Start to plot the layout
                                                    acPlEng.BeginPlot(acPlProgDlg, null);

                                                    // Define the plot output
                                                    acPlEng.BeginDocument(acPlInfo, acDb.Filename, null, 1, false, null);

                                                    // Display information about the current plot
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + acDb.Filename + " - " + acLayout.LayoutName);

                                                    // Set the sheet progress range
                                                    acPlProgDlg.OnBeginSheet();
                                                    acPlProgDlg.LowerSheetProgressRange = 0;
                                                    acPlProgDlg.UpperSheetProgressRange = 100;
                                                    acPlProgDlg.SheetProgressPos = 0;

                                                    // Plot the first sheet/layout
                                                    using (PlotPageInfo acPlPageInfo = new PlotPageInfo())
                                                    {
                                                        acPlEng.BeginPage(acPlPageInfo, acPlInfo, true, null);
                                                    }

                                                    acPlEng.BeginGenerateGraphics(null);
                                                    acPlEng.EndGenerateGraphics(null);

                                                    // Finish plotting the sheet/layout
                                                    acPlEng.EndPage(null);
                                                    acPlProgDlg.SheetProgressPos = 100;
                                                    acPlProgDlg.OnEndSheet();

                                                    // Finish plotting the document
                                                    acPlEng.EndDocument(null);

                                                    // Finish the plot
                                                    acPlProgDlg.PlotProgressPos = 100;
                                                    acPlProgDlg.OnEndPlot();
                                                    acPlEng.EndPlot(null);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        HostApplicationServices.WorkingDatabase = acDbSave;
                    }
                }
            }
        }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="5previewagain.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007010i271D24DB726770B1/image-size/large?v=v2&amp;amp;px=999" role="button" title="5previewagain.png" alt="5previewagain.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="4closefirstpreview.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007009i30C825F3E1C5EDE2/image-size/large?v=v2&amp;amp;px=999" role="button" title="4closefirstpreview.png" alt="4closefirstpreview.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="3firstpreview.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007012iB0137A862B310F49/image-size/large?v=v2&amp;amp;px=999" role="button" title="3firstpreview.png" alt="3firstpreview.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2beforefirstpreview.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007011i4B0865590E575799/image-size/large?v=v2&amp;amp;px=999" role="button" title="2beforefirstpreview.png" alt="2beforefirstpreview.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1dwg.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007013iA49E76428BEAD880/image-size/large?v=v2&amp;amp;px=999" role="button" title="1dwg.png" alt="1dwg.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 03 Jan 2022 19:23:32 GMT</pubDate>
    <dc:creator>lifanvc</dc:creator>
    <dc:date>2022-01-03T19:23:32Z</dc:date>
    <item>
      <title>Plot Preview from Database</title>
      <link>https://forums.autodesk.com/t5/net-forum/plot-preview-from-database/m-p/10855741#M14207</link>
      <description>&lt;P&gt;I want plot preview not open dwg file, I meet three questions, I need help ,thank you everyone.&amp;nbsp; this is very important for me,help please.&lt;BR /&gt;questions:&lt;BR /&gt;1.first plot not right;&lt;BR /&gt;2.close preview, Layout note the one before preview;&lt;/P&gt;&lt;P&gt;3.preview again ,it's right.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt; public class Startup
    {
        [CommandMethod("PlotPreView")]
        public void PlotPreView()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            var acEdit = acDoc.Editor;

            using (DocumentLock acLckDoc = acDoc.LockDocument())
            {
                var acDbSave = HostApplicationServices.WorkingDatabase;

                using (Database acDb = new Database(false, false))
                {
                    var localFile = "E:\\plotprevie.dwg";

                    acDb.ReadDwgFile(localFile, FileOpenMode.OpenForReadAndAllShare, false, null);
                    acDb.CloseInput(true);

                    using (Transaction acTrans = acDb.TransactionManager.StartTransaction())
                    {
                        HostApplicationServices.WorkingDatabase = acDb;

                        LayoutManager acLayoutMgr = LayoutManager.Current;

                        // 1D4 64B
                        string handle = "1D4";
                        long lng = Convert.ToInt64(handle, 16);
                        ObjectId objectId = acDb.GetObjectId(false, new Handle(lng), 0);
                        if (objectId == null) return;
                        BlockReference acBrf = acTrans.GetObject(objectId, OpenMode.ForRead) as BlockReference;
                        if (acBrf == null) return;

                        BlockTableRecord acBlkTblRec = acTrans.GetObject(acBrf.OwnerId, OpenMode.ForRead) as BlockTableRecord;
           
                        Layout acLayout = acBlkTblRec.LayoutId.GetObject(OpenMode.ForWrite) as Layout;

                        acLayoutMgr.CurrentLayout = acLayout.LayoutName;
                        acLayoutMgr.SetCurrentLayoutId(acLayout.ObjectId);

                        acEdit.WriteMessage(acLayoutMgr.CurrentLayout + "3\n");

                        using (PlotInfo acPlInfo = new PlotInfo())
                        {
                            acPlInfo.Layout = acLayout.ObjectId;

                            // Get a copy of the PlotSettings from the layout
                            using (PlotSettings acPlSet = new PlotSettings(acLayout.ModelType))
                            {
                                acPlSet.CopyFrom(acLayout);

                                // Update the PlotSettings object
                                PlotSettingsValidator acPlSetVdr = PlotSettingsValidator.Current;

                                // Set the plot type
                                acPlSetVdr.SetPlotType(acPlSet, Autodesk.AutoCAD.DatabaseServices.PlotType.Extents);

                                // Set the plot scale
                                acPlSetVdr.SetUseStandardScale(acPlSet, true);
                                acPlSetVdr.SetStdScaleType(acPlSet, StdScaleType.ScaleToFit);

                                // Center the plot
                                acPlSetVdr.SetPlotCentered(acPlSet, true);

                                // Set the plot device to use
                                acPlSetVdr.SetPlotConfigurationName(acPlSet, "DWF6 ePlot.pc3", "ANSI_A_(8.50_x_11.00_Inches)");

                                // Set the plot info as an override since it will
                                // not be saved back to the layout
                                acPlInfo.OverrideSettings = acPlSet;

                                // Validate the plot info
                                using (PlotInfoValidator acPlInfoVdr = new PlotInfoValidator())
                                {
                                    acPlInfoVdr.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;
                                    acPlInfoVdr.Validate(acPlInfo);

                                    // Check to see if a plot is already in progress
                                    if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
                                    {
                                        using (PlotEngine acPlEng = PlotFactory.CreatePreviewEngine((int)PreviewEngineFlags.Plot))
                                        {
                                            // Track the plot progress with a Progress dialog
                                            using (PlotProgressDialog acPlProgDlg = new PlotProgressDialog(true, 1, true))
                                            {
                                                using ((acPlProgDlg))
                                                {
                                                    // Define the status messages to display 
                                                    // when plotting starts
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.DialogTitle, "Plot Progress");
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelJobButtonMessage, "Cancel Job");
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.CancelSheetButtonMessage, "Cancel Sheet");
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetSetProgressCaption, "Sheet Set Progress");
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.SheetProgressCaption, "Sheet Progress");

                                                    // Set the plot progress range
                                                    acPlProgDlg.LowerPlotProgressRange = 0;
                                                    acPlProgDlg.UpperPlotProgressRange = 100;
                                                    acPlProgDlg.PlotProgressPos = 0;

                                                    // Display the Progress dialog
                                                    acPlProgDlg.OnBeginPlot();
                                                    acPlProgDlg.IsVisible = true;

                                                    // Start to plot the layout
                                                    acPlEng.BeginPlot(acPlProgDlg, null);

                                                    // Define the plot output
                                                    acPlEng.BeginDocument(acPlInfo, acDb.Filename, null, 1, false, null);

                                                    // Display information about the current plot
                                                    acPlProgDlg.set_PlotMsgString(PlotMessageIndex.Status, "Plotting: " + acDb.Filename + " - " + acLayout.LayoutName);

                                                    // Set the sheet progress range
                                                    acPlProgDlg.OnBeginSheet();
                                                    acPlProgDlg.LowerSheetProgressRange = 0;
                                                    acPlProgDlg.UpperSheetProgressRange = 100;
                                                    acPlProgDlg.SheetProgressPos = 0;

                                                    // Plot the first sheet/layout
                                                    using (PlotPageInfo acPlPageInfo = new PlotPageInfo())
                                                    {
                                                        acPlEng.BeginPage(acPlPageInfo, acPlInfo, true, null);
                                                    }

                                                    acPlEng.BeginGenerateGraphics(null);
                                                    acPlEng.EndGenerateGraphics(null);

                                                    // Finish plotting the sheet/layout
                                                    acPlEng.EndPage(null);
                                                    acPlProgDlg.SheetProgressPos = 100;
                                                    acPlProgDlg.OnEndSheet();

                                                    // Finish plotting the document
                                                    acPlEng.EndDocument(null);

                                                    // Finish the plot
                                                    acPlProgDlg.PlotProgressPos = 100;
                                                    acPlProgDlg.OnEndPlot();
                                                    acPlEng.EndPlot(null);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        HostApplicationServices.WorkingDatabase = acDbSave;
                    }
                }
            }
        }
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="5previewagain.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007010i271D24DB726770B1/image-size/large?v=v2&amp;amp;px=999" role="button" title="5previewagain.png" alt="5previewagain.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="4closefirstpreview.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007009i30C825F3E1C5EDE2/image-size/large?v=v2&amp;amp;px=999" role="button" title="4closefirstpreview.png" alt="4closefirstpreview.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="3firstpreview.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007012iB0137A862B310F49/image-size/large?v=v2&amp;amp;px=999" role="button" title="3firstpreview.png" alt="3firstpreview.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="2beforefirstpreview.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007011i4B0865590E575799/image-size/large?v=v2&amp;amp;px=999" role="button" title="2beforefirstpreview.png" alt="2beforefirstpreview.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="1dwg.png" style="width: 999px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/1007013iA49E76428BEAD880/image-size/large?v=v2&amp;amp;px=999" role="button" title="1dwg.png" alt="1dwg.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Jan 2022 19:23:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/plot-preview-from-database/m-p/10855741#M14207</guid>
      <dc:creator>lifanvc</dc:creator>
      <dc:date>2022-01-03T19:23:32Z</dc:date>
    </item>
  </channel>
</rss>

