Sample Code

1
Marcellus Zebra
11/20/2013 9:33 AM

Hi,

I work with WoutWare products from a pair of years.

Obviously I tested also a lot of cad libraries alternatives but I think the rigorous mathematical approach produces really the best results.

The only real problem is that not all developers can work for months for understand completly all required matrix computations for obtain a (simple) result.

In example, I want to generate a good Pdf file from my application that contains the current control view. I requested help in forum without success.

I understand that the customer care is expensive and requires a lot of time.

So my suggestion is this.
Why you don't allow developers to access your Sharp Viewer code instead of the really poor samples installed with libraries?

I think Sharp Viewer implements the 90% of a developer's requested features.
A good license agreement can protect you from malicious users.

If not, please increment samples capabilities and/or forum support.

Thank's very much and sorry for my terrible english,
marc.

Wout
11/26/2013 5:06 PM

Hi Marc,

For unexperienced users the matrix transformations can be a little bit intimidating but you don't need to understand all the details to use it. The DWG DXF Sharp Viewer also does not contain export to PDF using a clip rectangle. In the topic that Rammi answered using a layout/viewport, that is one option. You could also use the PDF clipping, but the PDF would be bigger than necessary ofcourse, especially if you are clipping most of the drawing away. For clipping use PdfPageConfiguration.ClipRectangle in the PdfExporter.DrawPage. In general you'll need to do the following:

  • Transform the clip rectangle from the display coordinate system (DCS) to the world coordinate system (WCS). Use the inverse of the projection transform (GDIGraphics2D.To2DTransform.GetInverse()).
  • Now you know the clip rectangle in WCS. You now can calculate the matrix to fit this rectangle onto your PDF page rectangle. Use DxfUtil.GetScaleTransform like demonstrated in the PdfExporter example code. From now on the code is mostly similar to the PdfExporter example.
  • With the WCS to PDF transform, you can also calculate the clip rectangle in PDF space.
Below is an example of how to do this:

C# Code:
        /// <summary>
        /// Exports the current view to a PDF file.
        /// </summary>
        public void ExportViewToPdf() {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.Filter = "PDF file (*.pdf)|*.pdf";
            if (dialog.ShowDialog() == DialogResult.OK) {
                Matrix4D dcsToWcsTransform = gdiGraphics3D.To2DTransform.GetInverse();
                Rectangle2D clipRectangleWcs = new Rectangle2D(
                    dcsToWcsTransform.Transform(new Point2D(ClientRectangle.Left, ClientRectangle.Bottom)),
                    dcsToWcsTransform.Transform(new Point2D(ClientRectangle.Right, ClientRectangle.Top))
                    );
                PaperSize paperSize = PaperSizes.GetPaperSize(PaperKind.A4);
                Size2D paperSizePdf = new Size2D(paperSize.Width / 100d * PdfExporter.InchToPixel, paperSize.Height / 100d * PdfExporter.InchToPixel);
                double margin = 0.5d /* inch*/ * PdfExporter.InchToPixel;
                Matrix4D wcsToPdfTransform =
                    DxfUtil.GetScaleTransform(
                        (Point3D)clipRectangleWcs.Corner1,
                        (Point3D)clipRectangleWcs.Corner2,
                        (Point3D)clipRectangleWcs.Center,
                        new Point3D(margin, margin, 0),
                        new Point3D(paperSizePdf.X - margin, paperSizePdf.Y - margin, 0),
                        new Point3D(0.5d * paperSizePdf.X, 0.5d * paperSizePdf.Y, 0)
                    );
                // The Pdf clip rectangle specified in hundredth of inches.
                Matrix4D wcsToPdfInchesTransform = Transformation4D.Scaling(100d / PdfExporter.InchToPixel) * wcsToPdfTransform;
                PdfPageConfiguration pdfPageConfiguration = new PdfPageConfiguration(model,
                    GraphicsConfig.AcadLikeWithWhiteBackground, wcsToPdfTransform, paperSize) {
                        ClipRectangle = new Rectangle2D(
                            wcsToPdfInchesTransform.Transform(clipRectangleWcs.Corner1),
                            wcsToPdfInchesTransform.Transform(clipRectangleWcs.Corner2)
                        )
                    };
                //pdfPageConfiguration.ClipRectangle = null;
                using (Stream stream = File.Create(dialog.FileName)) {
                    PdfExporter pdfExporter = new PdfExporter(stream);
                    pdfExporter.DrawPage(pdfPageConfiguration);
                    pdfExporter.EndDocument();
                }
            }
        }

- Wout

1