PDF and DisplayMode

Hi,

I’m taking V7 for a spin and seeing if some of my V6 issues will go away with it.
I do a lot of PDF generating. This is a sample command:

using System;
using System.IO;
using System.Linq;
using Rhino;
using Rhino.Commands;
using Rhino.Display;
using Rhino.DocObjects;
using Rhino.Geometry;

namespace Dev.Common
{
    [
        System.Runtime.InteropServices.Guid("726ced4a-8585-4a66-9a3c-da1964808c58"),
        CommandStyle(Style.ScriptRunner)
    ]
    public class PdfCommand : Command
    {
        public PdfCommand()
        {
            // Rhino only creates one instance of each command class defined in a
            // plug-in, so it is safe to store a refence in a static property.
            Instance = this;
        }

        ///<summary>The only instance of this command.</summary>
        public static PdfCommand Instance { get; private set; }

        public override string EnglishName => "PdfCommand";

        protected override Result RunCommand(Rhino.RhinoDoc doc, RunMode mode)
        {
            try
            {
                GeneratePDF.DoStuff(doc);
                RhinoApp.WriteLine("Done");
                return Result.Success;
            } catch (NullReferenceException exception)
            {
                Console.WriteLine(exception);
                RhinoApp.WriteLine("Failed");
                return Result.Failure;
            }
        }
    }

    public static class GeneratePDF
    {
        public static void DoStuff(RhinoDoc doc)
        {
            // Using RhinoScript since ViewTable.AddPageView also doesn't work in V7
            string createPage = $"_-Layout \"foo\" 297 210 0 _Enter";
            RhinoApp.RunScript(createPage, false);

            var pageView = doc.Views.GetPageViews().Last();

            var detailView = pageView.AddDetailView("bar",
                new Point2d(0, 0),
                new Point2d(100, 100),
                DefinedViewportProjection.Perspective
            );
            // This is the offending line. Comment this out, and it will work.
            // DisplayMode isn't null after this line.
            // Doesn't fail before AddPage(..) further down is called.
            detailView.Viewport.DisplayMode = DisplayModeDescription.FindByName("Technical");

            detailView.Viewport.Rotate(Math.PI, detailView.Viewport.CameraY, new Point3d(1, 1, 1));
            detailView.CommitViewportChanges();
            detailView.CommitChanges();

            var sheetPdf = Rhino.FileIO.FilePdf.Create();
            var sheetCapture = new Rhino.Display.ViewCaptureSettings(pageView, 300);
            // This will fail:
            sheetPdf.AddPage(sheetCapture);

            string partPdfPath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.pdf");
            sheetPdf.Write(partPdfPath);
        }
    }
}

Running that will fail in the AddPage line marked above.

The NullReferenceException looks like this:

"Object reference not set to an instance of an object"
...
"System.Drawing"
...
"  at System.Drawing.Bitmap.InternalSave (System.String path, System.Drawing.Imaging.ImageFormat format) [0x000bc] in <2b335274651f4b938bfc96d92815357e>:0 
  at (wrapper remoting-invoke-with-check) System.Drawing.Bitmap.InternalSave(string,System.Drawing.Imaging.ImageFormat)
  at System.Drawing.Image.Save (System.String filename, System.Drawing.Imaging.ImageFormat format) [0x0000a] in <2b335274651f4b938bfc96d92815357e>:0 
  at System.Drawing.Bitmap.InternalSave (System.String path) [0x00086] in <2b335274651f4b938bfc96d92815357e>:0 
  at (wrapper remoting-invoke-with-check) System.Drawing.Bitmap.InternalSave(string)
  at System.Drawing.Image.Save (System.String filename) [0x0000a] in <2b335274651f4b938bfc96d92815357e>:0 
  at (wrapper remoting-invoke-with-check) System.Drawing.Image.Save(string)
  at ExportPDF.PdfPageWriter.DrawBitmap (System.Drawing.Bitmap bitmap, System.Double m11, System.Double m12, System.Double m21, System.Double m22, System.Double dx, System.Double dy) [0x0005c] in /Users/bozo/TeamCity/buildAgent/work/96e64af5b81c6f85/src4/rhino4/Plug-ins/export_PDF/ExportPDFPlugIn.cs:289 
  at Rhino.Runtime.ViewCaptureWriter.TexturedPlane (System.IntPtr pRhinoDibOrNsImage, System.Double m11, System.Double m12, System.Double m21, System.Double m22, System.Double dx, System.Double dy) [0x000ca] in /Users/bozo/TeamCity/buildAgent/work/96e64af5b81c6f85/src4/DotNetSDK/rhinocommon/dotnet/rhino/rhinosdkprintinfo.cs:1428 
  at (wrapper native-to-managed) Rhino.Runtime.ViewCaptureWriter.TexturedPlane(intptr,double,double,double,double,double,double)
  at (wrapper managed-to-native) UnsafeNativeMethods.CRhinoPrintInfo_VectorCapture(intptr,Rhino.Runtime.ViewCaptureWriter/SetClipRectProc,Rhino.Runtime.ViewCaptureWriter/FillProc,Rhino.Runtime.ViewCaptureWriter/VectorPolylineProc,Rhino.Runtime.ViewCaptureWriter/VectorArcProc,Rhino.Runtime.ViewCaptureWriter/VectorStringProc,Rhino.Runtime.ViewCaptureWriter/VectorPolylineProc,Rhino.Runtime.ViewCaptureWriter/VectorFillPolygonProc,Rhino.Runtime.ViewCaptureWriter/VectorPathProc,Rhino.Runtime.ViewCaptureWriter/VectorPointProc,Rhino.Runtime.ViewCaptureWriter/VectorBitmapProc,Rhino.Runtime.ViewCaptureWriter/VectorRoundedRectProc,Rhino.Runtime.ViewCaptureWriter/VectorClipPathProc,Rhino.Runtime.ViewCaptureWriter/VectorGradientProc,uint)
  at Rhino.Runtime.ViewCaptureWriter.Draw (System.IntPtr constPtrPrintInfo, Rhino.RhinoDoc doc) [0x00177] in /Users/bozo/TeamCity/buildAgent/work/96e64af5b81c6f85/src4/DotNetSDK/rhinocommon/dotnet/rhino/rhinosdkprintinfo.cs:971 
  at ExportPDF.PdfDocumentWriter.AddPage (Rhino.Display.ViewCaptureSettings settings) [0x001aa] in /Users/bozo/TeamCity/buildAgent/work/96e64af5b81c6f85/src4/rhino4/Plug-ins/export_PDF/ExportPDFPlugIn.cs:847 
  at Stykka.Common.Views.ProjLayout.GeneratePDF (Stykka.Common.Part part, Rhino.RhinoDoc doc, Rhino.Display.RhinoPageView& pageView, Rhino.DocObjects.DetailViewObject& orthoDetailView) [0x0004c] in /Users/aske/Projects/Stykka/Stykka.Common/Views/MyLayout.cs:678 

Since I don’t provide a Bitmap at any point I assume this is somewhere in the application code.

System:
Version 7 BETA (7.0.20273.15004, 2020-09-29)
Mac OS X 10.14.6

I didn’t “upgrade” my application code in any way from V6. I just changed target and then it worked. Is there anything else I should do?

Thanks,
Aske

Hi @aske,

Can you zip up and upload a plug-in project that I can run here?

Thanks,

– Dale

I started from scratch with a new solution and chose the V7 WIP option, then added/removed files from that. In the solution there are 2 commands: SampleCSViewCapture which works, and PDFCommand which fails for me. See the comments in the code.

plugin.zip (25.9 KB)

Thanks for taking a look.

I’m going crazy. Can someone confirm if there is a bug?

Hi @aske,

I hope to have time to look at this next week.

Thanks,

– Dale

1 Like

Thank you so much @dale!