Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions CADability.Forms/GdiPrinting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
using CADability.Attribute;
using System.Drawing;
using System.Drawing.Printing;

namespace CADability.Forms
{
/// <summary>
/// Printing with GDI+ (System.Drawing.Printing): prints a <see cref="LayoutView"/> or a <see cref="GDI2DView"/> to a
/// <see cref="PrintDocument"/> and renders a model into a <see cref="Bitmap"/>. These methods replace
/// <see cref="LayoutView.Print(PrintDocument)"/>, <see cref="GDI2DView.PrintSinglePage(PrintDocument)"/> and the public
/// members of <see cref="PrintToGDI"/>, which are obsolete because GDI+ leaves the kernel in CADability 2.0 (issue #412).
/// In CADability 1.x they still use the implementation in the kernel; in 2.0 it moves here.
/// </summary>
public static class GdiPrinting
{
#pragma warning disable CS0618 // the implementation is still in the kernel in CADability 1.x
/// <summary>
/// Prints the layout of <paramref name="layoutView"/> on a single page. Depending on the global setting
/// "Printing.PrintingMode" the patches are either output as vector graphics (mode 1, with hidden line removal and shading)
/// or each patch is rendered into a bitmap at the printer resolution which is placed on the page. The paper size of the
/// layout is assumed to be in mm. Exceptions of the printing system are swallowed.
/// </summary>
/// <param name="printDocument">The print document to print to, with the printer and page settings already set</param>
/// <param name="layoutView">The layout to print</param>
public static void Print(this PrintDocument printDocument, LayoutView layoutView)
{
layoutView.Print(printDocument);
}
/// <summary>
/// Prints the contents of <paramref name="gdiView"/> on a single page. The paper of the view
/// (<see cref="GDI2DView.PaperWidth"/> by <see cref="GDI2DView.PaperHeight"/>) is fitted onto the page, so the paper size
/// should be set. Exceptions of the printing system are swallowed.
/// </summary>
/// <param name="printDocument">The print document to print to, with the printer and page settings already set</param>
/// <param name="gdiView">The view to print</param>
public static void PrintSinglePage(this PrintDocument printDocument, GDI2DView gdiView)
{
gdiView.PrintSinglePage(printDocument);
}
/// <summary>
/// Prints the <see cref="GDI2DView"/> named <paramref name="nameOfView"/> of <paramref name="project"/> on a single page.
/// Nothing is printed if the project has no such view.
/// </summary>
/// <param name="printDocument">The print document to print to, null for the print document of the project</param>
/// <param name="project">The project containing the view</param>
/// <param name="nameOfView">The name of the view</param>
/// <param name="print2D">True to print only the curves, false to print the faces too</param>
public static void PrintSinglePage(this PrintDocument printDocument, Project project, string nameOfView, bool print2D)
{
PrintToGDI.PrintSinglePage(project, nameOfView, print2D, printDocument);
}
/// <summary>
/// Renders <paramref name="model"/> with the <paramref name="projection"/> into <paramref name="bitmap"/>, which is
/// filled white first.
/// </summary>
/// <param name="bitmap">The bitmap to render into</param>
/// <param name="model">The model to render</param>
/// <param name="projection">The projection to use</param>
/// <param name="visibleLayers">The layers to render</param>
/// <param name="shading">True to shade the faces</param>
/// <param name="curvesOnly">Not used: whether only the curves are rendered follows <see cref="Projection.ShowFaces"/> of
/// <paramref name="projection"/>, as in <see cref="PrintToGDI.PrintToBitmap"/></param>
public static void DrawModel(this Bitmap bitmap, Model model, Projection projection, Layer[] visibleLayers, bool shading, bool curvesOnly)
{
new PrintToGDI(bitmap).PrintToBitmap(model, projection, visibleLayers, shading, curvesOnly);
}
#pragma warning restore CS0618
}
}
2 changes: 1 addition & 1 deletion CADability/GDI2DView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ public bool ThinLinesOnly
/// is fitted onto the page, so the paper size should be set. Exceptions of the printing system are swallowed.
/// </summary>
/// <param name="pd">The print document to print to, with the printer and page settings already set</param>
[Obsolete("Printing with GDI+ moves into CADability.Forms in CADability 2.0 (issue #412).")]
[Obsolete("Use the PrintDocument extension PrintSinglePage(GDI2DView) of CADability.Forms.GdiPrinting instead. Printing with GDI+ moves into CADability.Forms in CADability 2.0 (issue #412).")]
public void PrintSinglePage(PrintDocument pd)
{
PrintToGDI pdg = new PrintToGDI(this);
Expand Down
2 changes: 1 addition & 1 deletion CADability/LayoutView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void ZoomTotal(double factor)
/// the printing system are swallowed.
/// </summary>
/// <param name="pd">The print document to print to, with the printer and page settings already set</param>
[Obsolete("Printing with GDI+ moves into CADability.Forms in CADability 2.0 (issue #412).")]
[Obsolete("Use the PrintDocument extension Print(LayoutView) of CADability.Forms.GdiPrinting instead. Printing with GDI+ moves into CADability.Forms in CADability 2.0 (issue #412).")]
public void Print(PrintDocument pd)
{
bool printToGDI = Settings.GlobalSettings.GetIntValue("Printing.PrintingMode", 0) == 1;
Expand Down
2 changes: 1 addition & 1 deletion CADability/PrintToGDI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ public override IGeoObject Debug
/// <see cref="OnPrintPage"/>), to render a model into a <see cref="Bitmap"/> (<see cref="PrintToBitmap"/>) and for the display of
/// the <see cref="GDI2DView"/>. Not available in the WebAssembly build.
/// </summary>
[Obsolete("Printing with GDI+ moves into CADability.Forms in CADability 2.0 (issue #412).")]
[Obsolete("Use the extensions of CADability.Forms.GdiPrinting instead: PrintDocument.Print(LayoutView), PrintDocument.PrintSinglePage(GDI2DView) or PrintSinglePage(Project, string, bool), and Bitmap.DrawModel(...). Printing with GDI+ moves into CADability.Forms in CADability 2.0 (issue #412).")]
public class PrintToGDI : IPaintTo3D, QuadTree<IPrintItemImpl>.IIterateQuadTreeLists
{

Expand Down
54 changes: 54 additions & 0 deletions tests/CADability.Tests/GdiPrintingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#if WINDOWS
using CADability.Attribute;
using CADability.Forms;
using CADability.GeoObject;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Drawing;

namespace CADability.Tests
{
/// <summary>
/// The printing extensions of <see cref="GdiPrinting"/> in CADability.Forms replace the obsolete printing members of the
/// kernel (issue #412). In CADability 1.x they forward to the kernel, so they must produce the same output.
/// </summary>
[TestClass]
public class GdiPrintingTests
{
[TestMethod]
public void DrawModelRendersLikePrintToGdi()
{
Project project = Project.CreateSimpleProject();
Model model = project.GetActiveModel();
Line line = Line.Construct();
line.SetTwoPoints(new GeoPoint(10, 10, 0), new GeoPoint(90, 90, 0));
model.Add(line);
Layer[] layers = project.LayerList.ToArray();

Bitmap viaExtension = Render((bitmap, projection) => bitmap.DrawModel(model, projection, layers, false, true));
#pragma warning disable CS0618 // the obsolete kernel implementation as the reference
Bitmap viaKernel = Render((bitmap, projection) => new PrintToGDI(bitmap).PrintToBitmap(model, projection, layers, false, true));
#pragma warning restore CS0618

int drawn = 0;
for (int x = 0; x < viaKernel.Width; x++)
for (int y = 0; y < viaKernel.Height; y++)
{
Color expected = viaKernel.GetPixel(x, y);
Assert.AreEqual(expected.ToArgb(), viaExtension.GetPixel(x, y).ToArgb(), $"pixel {x}, {y}");
if (expected.ToArgb() != Color.White.ToArgb()) ++drawn;
}
Assert.IsTrue(drawn > 0, "the line is drawn");
}

private static Bitmap Render(System.Action<Bitmap, Projection> draw)
{
Bitmap bitmap = new Bitmap(100, 100);
Projection projection = new Projection(Projection.StandardProjection.FromTop);
projection.ShowFaces = false;
projection.SetPlacement(new Rectangle(0, 0, 100, 100), new BoundingRect(0, 0, 100, 100));
draw(bitmap, projection);
return bitmap;
}
}
}
#endif
Loading