I want to make my plotting code faster.

I want to make my plotting code faster.

goforlee
Participant Participant
661 Views
3 Replies
Message 1 of 4

I want to make my plotting code faster.

goforlee
Participant
Participant

 

Hi everyone,

I've been receiving amazing help from reading other people's posts on this forum. Thank you!

 

I've been working on capturing (or taking a screenshot of) specific coordinates using the attached function.

The code works fine, but it's extremely SLOW!!.

I expected it to take less than a second, but it takes at least 10 seconds. It executes all the way down to the "this.Show()" function almost immediately, but generating the actual file takes a long time.

 

Could you please help me out with this issue?

Thank you in advance!

 

private void CaptureAndSaveImage(Point3d startPoint, Point3d endPoint, string outputPath)
{
    
    Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
    Database db = doc.Database;
    using (DocumentLock docLock = doc.LockDocument())

    {
        // Start a transaction
        using (Transaction trans = db.TransactionManager.StartTransaction())
        {

            // Get the current layout
            //Layout layout = trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as Layout;
            BlockTableRecord btr = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead);

            Layout layout = (Layout)trans.GetObject(btr.LayoutId, OpenMode.ForRead);
            //ObjectId modelSpaceId = SymbolUtilityServices.GetBlockModelSpaceId(db);
            //Layout layout = trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead) as Layout;
            // Create a new PlotInfo object

            PlotSettings plotSettings = new PlotSettings(layout.ModelType);
            plotSettings.CopyFrom(layout);

            // Get the PlotSettingsValidator
            PlotSettingsValidator plotSettingsValidator = PlotSettingsValidator.Current;

            // Calculate the extents from the two points
            Extents2d plotWindowExtents = new Extents2d(startPoint.X, startPoint.Y, endPoint.X, endPoint.Y);

            plotSettings.ScaleLineweights = true;


            // Set the plot area
            plotSettingsValidator.SetPlotWindowArea(plotSettings, plotWindowExtents);
            plotSettingsValidator.SetPlotType(plotSettings, Autodesk.AutoCAD.DatabaseServices.PlotType.Window);
            plotSettingsValidator.SetUseStandardScale(plotSettings, true);

            plotSettingsValidator.SetStdScaleType(plotSettings, StdScaleType.ScaleToFit);
            plotSettingsValidator.SetPlotRotation(plotSettings, PlotRotation.Degrees180);
            
            plotSettingsValidator.SetPlotCentered(plotSettings, false);
            // Set the plot configuration name
            plotSettingsValidator.SetPlotConfigurationName(plotSettings, "PublishToWeb PNG.pc3", "VGA_(640.00_x_480.00_Pixels)");

            //plotSettingsValidator.SetPlotConfigurationName(plotSettings, "AutoCAD PDF (Web and Mobile).pc3", "ISO_A4_(210.00_x_297.00_MM)");
            // Update the PlotInfo object with the new settings
            PlotInfo plotInfo = new PlotInfo();
            plotInfo.Layout = btr.LayoutId;
            plotInfo.OverrideSettings = plotSettings;

            PlotInfoValidator piv = new PlotInfoValidator();

            piv.MediaMatchingPolicy = MatchingPolicy.MatchEnabled;

            piv.Validate(plotInfo);

            //// Commit the changes to the transaction
            trans.Commit();

            // Plot the image
            using (PlotEngine plotEngine = PlotFactory.CreatePublishEngine())
            {
                plotEngine.BeginPlot(null, null);

                plotEngine.BeginDocument(plotInfo, doc.Name, null, 1, true, Path.Combine("C:\\Users\\YR_VM\\Desktop\\cad backup", Guid.NewGuid() + ".png"));

                // Define the plot output
                PlotPageInfo pageInfo = new PlotPageInfo();
                try
                {
                    plotEngine.BeginPage(pageInfo, plotInfo, true, null);

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                plotEngine.BeginGenerateGraphics(null);
                plotEngine.EndGenerateGraphics(null);


                plotEngine.EndPage(null);
                plotEngine.EndDocument(null);

                // End the plot
                plotEngine.EndPlot(null);
            }
        }
    }


    this.Show();
}

 

0 Likes
Accepted solutions (1)
662 Views
3 Replies
Replies (3)
Message 2 of 4

ActivistInvestor
Mentor
Mentor

Plotting would obviously take longer than a graphics dump of the current view.

 

While output to PDF may not be possible using other means, the code at this link will generate a screenshot.

0 Likes
Message 3 of 4

goforlee
Participant
Participant

I am not really great at coding... but the link doesn't seem like I can specify coordinates of objects as my code. Did I understand it wrong? or I can implement that kind of function with the code in the link ??

0 Likes
Message 4 of 4

ActivistInvestor
Mentor
Mentor
Accepted solution

The code at that link really doesn't specify what to take a snapshot of it just shows how to take a snapshot. So, you would have to first set the view before taking the snapshot through code.

0 Likes