Plotting files with api doesn't include paper space objects OR viewport

Plotting files with api doesn't include paper space objects OR viewport

ottosson_mathias
Advocate Advocate
904 Views
3 Replies
Message 1 of 4

Plotting files with api doesn't include paper space objects OR viewport

ottosson_mathias
Advocate
Advocate

Hi

 

I'm trying to implement a plot method and have problems getting correct results. Sometimes I get paperspace objects only, and sometimes I get viewports only. I haven't managed to get both at the same time for some reason... and I don't know what I've done to get different. Seemingly nothing!

 

plot-empty-viewport.pngplot-no-paperspace-objects.png

 

Here is my code:

public static void Plot(string filePath, string layoutName, string plotName, string canonicalMediaName, string plotStyleTable)
{
    var fileName = Path.GetFileNameWithoutExtension(filePath);
    var path = Path.GetDirectoryName(filePath);
    var outFilePath = Path.Combine(path, fileName + ".pdf");

    var doc = Application.DocumentManager.MdiActiveDocument;
    var db = doc.Database;

    using (var trans = db.TransactionManager.StartTransaction())
    {
        HostApplicationServices.WorkingDatabase = db;
        LayoutManager.Current.CurrentLayout = layoutName;

        var blockTableRecord = (BlockTableRecord)trans.GetObject(db.CurrentSpaceId, OpenMode.ForRead);
        // Getting the layout object from elided method
        Autodesk.AutoCAD.DatabaseServices.Layout currentLayout = LayoutHandler.GetLayout(layoutName);

        var plotInfo = new PlotInfo
        {
            Layout = blockTableRecord.LayoutId
        };

        var plotSettings = new Autodesk.AutoCAD.DatabaseServices.PlotSettings(currentLayout.ModelType);
        plotSettings.CopyFrom(currentLayout);

        var plotSettingsValidator = PlotSettingsValidator.Current;

        plotSettingsValidator.RefreshLists(plotSettings);
        plotSettingsValidator.SetUseStandardScale(plotSettings, true);
        plotSettingsValidator.SetStdScaleType(plotSettings, StdScaleType.ScaleToFit);
        plotSettingsValidator.SetPlotConfigurationName(plotSettings, plotName, canonicalMediaName);
        plotSettingsValidator.SetPlotPaperUnits(plotSettings, PlotPaperUnit.Millimeters);
        plotSettingsValidator.SetPlotRotation(plotSettings, PlotRotation.Degrees000);
        plotSettingsValidator.SetPlotType(plotSettings, Autodesk.AutoCAD.DatabaseServices.PlotType.Layout);
        plotSettingsValidator.SetCustomPrintScale(plotSettings, new CustomScale(1, 1));
        plotSettingsValidator.SetCurrentStyleSheet(plotSettings, plotStyleTable);

        plotSettings.DrawViewportsFirst = true;
        plotSettings.PrintLineweights = true;
        plotSettings.ScaleLineweights = true;

        var plotInfoValidator = new PlotInfoValidator
        {
            MediaMatchingPolicy = MatchingPolicy.MatchEnabled
        };

        plotInfoValidator.Validate(plotInfo);

        if (PlotFactory.ProcessPlotState == ProcessPlotState.NotPlotting)
        {
            using (var plotEngine = PlotFactory.CreatePublishEngine())
            {
                // Start plot
                plotEngine.BeginPlot(null, null);
                plotEngine.BeginDocument(plotInfo, doc.Name, null, 1, true, outFilePath);
                plotEngine.BeginPage(new PlotPageInfo(), plotInfo, true, null);
                plotEngine.BeginGenerateGraphics(null);

                // Finish plot
                plotEngine.EndGenerateGraphics(null);
                plotEngine.EndPage(null);
                plotEngine.EndDocument(null);
                plotEngine.EndPlot(null);
            }
        }
    }
}

 

I have tried to change plotSettings.DrawViewportsFirst  to false, but nothing changes...

I don't know what I can do or why I sometimes get the viewport content and sometime only the paperspace content!

 

Anyone recognizes the problem or has any suggestions?

Thanks!

0 Likes
905 Views
3 Replies
Replies (3)
Message 2 of 4

SENL1362
Advisor
Advisor

Might have something to do with you're layoutId assignment to PlotInfo.

 

Try this:

 

...
var lm = LayoutManager.Current;
lm.CurrentLayout = layoutName;
var layoutId = lm.GetLayoutId(layoutName);
plotInfo.Layout = layoutId;
...

 

Simplified copy of Layout2Pdf -- without the PlotSettings changes:

 

public static void Layout2Pdf(ObjectId layoutId, string pdfPathname)
{
Document doc = null;
LayoutManager lm = null;
try
{
if (layoutId.IsNull)
throw new System.Exception($"Invalid Argument: {nameof(layoutId)} IS NULL");

if (string.IsNullOrWhiteSpace(pdfPathname))
throw new System.Exception($"Invalid Argument: {nameof(pdfPathname)} IS NULL");

var db = layoutId.Database;
if (db == null)
throw new System.Exception($"Not in DB: {nameof(layoutId)} ");

doc = AcadApp.DocumentManager.GetDocument(db);
lm = LayoutManager.Current;
lm.SetCurrentLayoutId(layoutId);

if (File.Exists(pdfPathname))
File.Delete(pdfPathname);

var psv = PlotSettingsValidator.Current;
using (var plotInfo = new PlotInfo())
{
using (var plotEngine = PlotFactory.CreatePublishEngine())
{
plotEngine.BeginPlot(null, null);
var validator = new PlotInfoValidator()
{
MediaMatchingPolicy = Autodesk.AutoCAD.PlottingServices.MatchingPolicy.MatchEnabled
};
plotInfo.Layout = layoutId;
validator.Validate(plotInfo);
plotEngine.BeginDocument(plotInfo, doc.Name, null, 1, true, pdfPathname);
using (var pageInfo = new PlotPageInfo())
{
plotEngine.BeginPage(pageInfo, plotInfo, true, null);
}
plotEngine.BeginGenerateGraphics(null);
plotEngine.EndGenerateGraphics(null);
plotEngine.EndPage(null);
plotEngine.EndDocument(null);
plotEngine.EndPlot(null);
}
}
if (!File.Exists(pdfPathname))
{
throw new System.Exception($"Create PDF failed ");
}
}
catch (System.Exception ex)
{
throw;
}
}
0 Likes
Message 3 of 4

ottosson_mathias
Advocate
Advocate

Thanks, but unfortunately

LayoutManager.Current.CurrentLayout = layoutName;

vs

LayoutManager.Current.SetCurrentLayoutId(layoutId);

did not make any difference...

0 Likes
Message 4 of 4

SENL1362
Advisor
Advisor

It's not the Set Layout but the LayoutId assignment to PlotInfo what looks suspicions to me.

My advise:

0. Create the Layout with the required PlotSettings manually.

1. Then create the PDF without the PlotSettings modifications and use the first four assignments.

2. Then when a valid PDF has been made, Add The PlotSettings modifications.

 

 

0 Likes