Beispiel #1
0
        public static Bitmap CreateAutoSizedBitmap(
            DxfModel model,
            GDIGraphics3D cadGraphics,
            SmoothingMode smoothingMode,
            Matrix4D transform,
            System.Drawing.Color backColor,
            Size maxSize)
        {
            cadGraphics.CreateDrawables(model);
            Bounds3D bounds = new Bounds3D();

            cadGraphics.BoundingBox(bounds, transform);
            int      num1       = maxSize.Width - 4;
            int      num2       = maxSize.Height - 4;
            Matrix4D transform1 = DxfUtil.GetScaleTransform(bounds.Corner1, bounds.Corner2, new WW.Math.Point3D(bounds.Corner1.X, bounds.Corner2.Y, 0.0), new WW.Math.Point3D(0.0, (double)num2, 0.0), new WW.Math.Point3D((double)num1, 0.0, 0.0), new WW.Math.Point3D(2.0, 2.0, 0.0)) * transform;

            bounds.Reset();
            cadGraphics.BoundingBox(bounds, transform1);
            if (!bounds.Initialized)
            {
                return(ImageExporter.CreateEmptyBitmap(backColor, maxSize.Width, maxSize.Height));
            }
            WW.Math.Vector3D delta = bounds.Delta;
            int width  = System.Math.Min(maxSize.Width, (int)System.Math.Ceiling(delta.X) + 4);
            int height = System.Math.Min(maxSize.Height, (int)System.Math.Ceiling(delta.Y) + 4);

            return(ImageExporter.CreateBitmap(cadGraphics, smoothingMode, transform1, backColor, width, height));
        }
Beispiel #2
0
        public static Bitmap CreateAutoSizedBitmap(
            DxfModel model,
            DxfLayout layout,
            ICollection <DxfViewport> viewports,
            Matrix4D transform,
            GraphicsConfig graphicsConfig,
            SmoothingMode smoothingMode,
            System.Drawing.Color backColor,
            Size maxSize)
        {
            BoundsCalculator boundsCalculator = new BoundsCalculator(graphicsConfig);

            boundsCalculator.GetBounds(model, layout, viewports, transform);
            Bounds3D bounds = boundsCalculator.Bounds;

            if (!bounds.Initialized)
            {
                return(ImageExporter.CreateEmptyBitmap(backColor, maxSize.Width, maxSize.Height));
            }
            int      num1           = maxSize.Width - 4;
            int      num2           = maxSize.Height - 4;
            Matrix4D scaleTransform = DxfUtil.GetScaleTransform(bounds.Corner1, bounds.Corner2, new WW.Math.Point3D(bounds.Corner1.X, bounds.Corner2.Y, 0.0), new WW.Math.Point3D(0.0, (double)num2, 0.0), new WW.Math.Point3D((double)num1, 0.0, 0.0), new WW.Math.Point3D(2.0, 2.0, 0.0));
            Matrix4D to2DTransform  = scaleTransform * transform;

            WW.Math.Vector3D vector3D = scaleTransform.Transform(bounds.Corner2) - scaleTransform.Transform(bounds.Corner1);
            int width  = System.Math.Min(maxSize.Width, (int)System.Math.Ceiling(System.Math.Abs(vector3D.X)) + 4);
            int height = System.Math.Min(maxSize.Height, (int)System.Math.Ceiling(System.Math.Abs(vector3D.Y)) + 4);

            return(ImageExporter.CreateBitmap(model, layout, viewports, graphicsConfig, smoothingMode, backColor, to2DTransform, width, height));
        }
Beispiel #3
0
        private static Image <Bgra32> RenderToBitmap(DxfModel model)
        {
            var graphicsConfig = (GraphicsConfig)GraphicsConfig.AcadLikeWithWhiteBackground.Clone();

            BoundsCalculator boundsCalculator = new BoundsCalculator();

            boundsCalculator.GetBounds(model);
            Bounds3D bounds = boundsCalculator.Bounds;

            int      size      = 2000;
            Matrix4D transform = DxfUtil.GetScaleTransform(
                bounds.Min,
                bounds.Max,
                bounds.Center,
                new Point3D(0, size, 0),
                new Point3D(size, 0, 0),
                new Point3D(0.5d * size, 0.5d * size, 0)
                );

            Memory <Bgra32> memory = new Memory <Bgra32>();

            Image.WrapMemory <Bgra32>(memory, 10, 10);
            Image <Bgra32> bitmap = ImageExporter.CreateBitmap <Bgra32>(
                model, transform,
                graphicsConfig,
                //new GraphicsOptions(false),
                new Size(size, size));

            return(bitmap);
        }
        private unsafe WriteableBitmap CreateBitmap()
        {
            WriteableBitmap  writeableBitmap  = null;
            FrameworkElement frameworkElement = mainGrid;

            if (
                model != null &&
                !double.IsNaN(frameworkElement.ActualWidth) &&
                !double.IsNaN(frameworkElement.ActualHeight) &&
                frameworkElement.ActualWidth != 0 &&
                frameworkElement.ActualHeight != 0
                )
            {
                int pixelWidth  = (int)frameworkElement.ActualWidth;
                int pixelHeight = (int)frameworkElement.ActualHeight;

                SixLabors.Primitives.Size size = new SixLabors.Primitives.Size(pixelWidth, pixelHeight);
                Matrix4D transform             = DxfUtil.GetScaleTransform(
                    bounds.Min,
                    bounds.Max,
                    bounds.Center,
                    new Point3D(0, size.Height, 0),
                    new Point3D(size.Width, 0, 0),
                    new Point3D(0.5d * size.Width, 0.5d * size.Height, 0)
                    );

                writeableBitmap = new WriteableBitmap(pixelWidth, pixelHeight);
                cadGraphics.Draw(writeableBitmap, new Rectangle(0, 0, size.Width, size.Height), transform);
            }
            return(writeableBitmap);
        }
Beispiel #5
0
        public Matrix4D CalculateTo2DTransform1()
        {
            Matrix4D to2DTransform = Matrix4D.Identity;

            if (model != null && bounds != null)
            {
                double halfHeight = ImageControl.ClientSize.Height / 2;
                double halfWidth  = ImageControl.ClientSize.Width / 2;
                double margin     = 5d; // 5 pixels margin on each size.
                to2DTransform =
                    Transformation4D.Translation(translation) *
                    Transformation4D.Translation(halfWidth, halfHeight, 0) *
                    Transformation4D.Scaling(scaleFactor) *
                    DxfUtil.GetScaleTransform(
                        bounds.Corner1,
                        bounds.Corner2,
                        bounds.Center,
                        new Point3D(margin, ImageControl.ClientSize.Height - margin, 0d),
                        new Point3D(ImageControl.ClientSize.Width - margin, margin, 0d),
                        Point3D.Zero
                        );
            }
            gdiGraphics3D.To2DTransform = to2DTransform * modelTransform;
            from2DTransform             = gdiGraphics3D.To2DTransform.GetInverse();
            return(to2DTransform);
        }
Beispiel #6
0
 public void CalculateTo2DTransform()
 {
     if (bounds != null)
     {
         Matrix4D to2DTransform = DxfUtil.GetScaleTransform(
             bounds.Corner1,
             bounds.Corner2,
             bounds.Center,
             new Point3D(0d, ImageControl.ClientSize.Height, 0d),
             new Point3D(ImageControl.ClientSize.Width, 0d, 0d),
             new Point3D(ImageControl.ClientSize.Width / 2, ImageControl.ClientSize.Height / 2, 0d)
             );
         gdiGraphics3D.To2DTransform = to2DTransform * modelTransform;
     }
 }
Beispiel #7
0
        } // End Sub WriteDefaultLayoutToPdf

        /// <summary>
        /// Write a complete model to a PDF file.
        /// </summary>
        /// <remarks>
        /// The model is scaled to fit the paper inside the margin.
        /// This code will center the model on top of the page.
        /// </remarks>
        /// <param name="model">DXF model to write</param>
        /// <param name="paperSize">targeted paper size</param>
        /// <param name="margin">margin around model print in inches</param>
        /// <param name="outfile">path of PDF output file</param>
        /// <param name="embedFonts">embed fonts into PDF?</param>
        /// <param name="lineWeight">default line weight in 100th of mm</param>
        private static void WriteModelToPdf(DxfModel model, System.Drawing.Printing.PaperSize paperSize, float margin, string outfile, bool embedFonts, short lineWeight)
        {
            WW.Cad.Drawing.BoundsCalculator boundsCalculator = new WW.Cad.Drawing.BoundsCalculator();
            boundsCalculator.GetBounds(model);
            Bounds3D bounds = boundsCalculator.Bounds;

            // Lengths in inches.
            float pageWidth  = paperSize.Width / 100f;
            float pageHeight = paperSize.Height / 100f;

            // Scale and transform such that its fits max width/height
            // and the top middle of the cad drawing will match the
            // top middle of the pdf page.
            // The transform transforms to pdf pixels.
            double   scaling;
            Matrix4D to2DTransform = DxfUtil.GetScaleTransform(
                bounds.Corner1,
                bounds.Corner2,
                new Point3D(bounds.Center.X, bounds.Corner2.Y, 0d),
                new Point3D(new Vector3D(margin, margin, 0d) * PdfExporter.InchToPixel),
                new Point3D(new Vector3D(pageWidth - margin, pageHeight - margin, 0d) * PdfExporter.InchToPixel),
                new Point3D(new Vector3D(pageWidth / 2d, pageHeight - margin, 0d) * PdfExporter.InchToPixel),
                out scaling
                );

            using (System.IO.Stream stream = System.IO.File.Create(outfile))
            {
                PdfExporter pdfGraphics = new PdfExporter(stream);
                pdfGraphics.ExportLayers      = true;
                pdfGraphics.UseMultipleLayers = false;
                pdfGraphics.EmbedFonts        = embedFonts;
                WW.Cad.Drawing.GraphicsConfig config = (WW.Cad.Drawing.GraphicsConfig)WW.Cad.Drawing.GraphicsConfig.AcadLikeWithWhiteBackground.Clone();
                config.TryDrawingTextAsText = embedFonts;
                config.DefaultLineWeight    = lineWeight;

                pdfGraphics.DrawPage(
                    model,
                    config,
                    to2DTransform,
                    scaling,
                    null,
                    null,
                    paperSize
                    );

                pdfGraphics.EndDocument();
            } // End Using stream
        }     // End Sub WriteModelToPdf
Beispiel #8
0
        private DxfLayout.PlotInfo GetPlotInfo(
            bool invertY,
            DxfLayout.BasicLayoutRenderInfo layoutRenderInfo)
        {
            Size2D plotPaperSize = this.PlotPaperSize;

            if (plotPaperSize.X == 0.0 || plotPaperSize.Y == 0.0)
            {
                return((DxfLayout.PlotInfo)null);
            }
            double y = invertY ? this.UnprintableMarginBottom : plotPaperSize.Y - this.UnprintableMarginTop;

            WW.Math.Point3D toPoint1         = new WW.Math.Point3D(this.UnprintableMarginLeft, invertY ? plotPaperSize.Y - this.UnprintableMarginTop : this.UnprintableMarginBottom, 0.0);
            WW.Math.Point3D toPoint2         = new WW.Math.Point3D(plotPaperSize.X - this.UnprintableMarginRight, y, 0.0);
            WW.Math.Point3D toReferencePoint = new WW.Math.Point3D(0.5 * (toPoint1.X + toPoint2.X), 0.5 * (toPoint1.Y + toPoint2.Y), 0.0);
            Matrix4D        matrix4D1        = Matrix4D.Identity;

            switch (this.PlotRotation)
            {
            case PlotRotation.QuarterCounterClockwise:
                matrix4D1 = Transformation4D.RotateZ(System.Math.PI / 2.0);
                break;

            case PlotRotation.Half:
                matrix4D1 = Transformation4D.RotateZ(System.Math.PI);
                break;

            case PlotRotation.QuarterClockwise:
                matrix4D1 = Transformation4D.RotateZ(-1.0 * System.Math.PI / 2.0);
                break;
            }
            if (invertY)
            {
                matrix4D1 *= Transformation4D.Scaling(1.0, -1.0, 1.0);
            }
            WW.Math.Point3D point3D1           = new WW.Math.Point3D(layoutRenderInfo.LayoutViewport.Min, 0.0);
            WW.Math.Point3D point3D2           = new WW.Math.Point3D(layoutRenderInfo.LayoutViewport.Max, 0.0);
            WW.Math.Point3D fromReferencePoint = new WW.Math.Point3D(layoutRenderInfo.LayoutViewport.Center, 0.0);
            Matrix4D        matrix4D2          = Transformation4D.Translation((WW.Math.Vector3D)fromReferencePoint) * matrix4D1 * Transformation4D.Translation(-(WW.Math.Vector3D)fromReferencePoint);

            WW.Math.Point3D point3D3 = matrix4D2 * point3D1;
            point3D2 = matrix4D2 * point3D2;
            double   scaling;
            Matrix4D toPaperTransform = DxfUtil.GetScaleTransform(new WW.Math.Point3D(System.Math.Min(point3D3.X, point3D2.X), System.Math.Min(point3D3.Y, point3D2.Y), 0.0), new WW.Math.Point3D(System.Math.Max(point3D3.X, point3D2.X), System.Math.Max(point3D3.Y, point3D2.Y), 0.0), fromReferencePoint, toPoint1, toPoint2, toReferencePoint, out scaling) * matrix4D2;

            return(new DxfLayout.PlotInfo(this, plotPaperSize, new Rectangle2D(toPoint1.X, System.Math.Min(toPoint1.Y, toPoint2.Y), toPoint2.X, System.Math.Max(toPoint1.Y, toPoint2.Y)), toPaperTransform, scaling));
        }
Beispiel #9
0
        public static Bitmap CreateAutoSizedBitmap(
            GDIGraphics3D cadGraphics,
            SmoothingMode smoothingMode,
            Matrix4D transform,
            System.Drawing.Color backColor)
        {
            transform = Transformation4D.Translation(2.0, 2.0, 0.0) * transform;
            Bounds3D bounds = new Bounds3D();

            cadGraphics.BoundingBox(bounds, transform);
            if (!bounds.Initialized)
            {
                return(ImageExporter.CreateEmptyBitmap(backColor, 1, 1));
            }
            WW.Math.Vector3D delta      = bounds.Delta;
            Matrix4D         transform1 = DxfUtil.GetScaleTransform(bounds.Min, bounds.Max, new WW.Math.Point3D(2.0, delta.Y + 2.0, 0.0), new WW.Math.Point3D(delta.X + 2.0, 2.0, 0.0)) * transform;
            int width  = (int)System.Math.Ceiling(delta.X) + 4;
            int height = (int)System.Math.Ceiling(delta.Y) + 4;

            return(ImageExporter.CreateBitmap(cadGraphics, smoothingMode, transform1, backColor, width, height));
        }
Beispiel #10
0
        public static Bitmap CreateAutoSizedBitmap(
            DxfModel model,
            Matrix4D transform,
            GraphicsConfig graphicsConfig,
            SmoothingMode smoothingMode)
        {
            BoundsCalculator boundsCalculator = new BoundsCalculator(graphicsConfig);

            boundsCalculator.GetBounds(model, transform);
            Bounds3D bounds = boundsCalculator.Bounds;

            if (!bounds.Initialized)
            {
                return(ImageExporter.CreateEmptyBitmap((System.Drawing.Color)graphicsConfig.BackColor, 1, 1));
            }
            WW.Math.Vector3D delta      = bounds.Delta;
            Matrix4D         transform1 = DxfUtil.GetScaleTransform(bounds.Min, bounds.Max, new WW.Math.Point3D(2.0, delta.Y + 2.0, 0.0), new WW.Math.Point3D(delta.X + 2.0, 2.0, 0.0)) * transform;
            int width  = (int)System.Math.Ceiling(delta.X) + 4;
            int height = (int)System.Math.Ceiling(delta.Y) + 4;

            return(ImageExporter.CreateBitmap(model, transform1, graphicsConfig, smoothingMode, width, height));
        }
Beispiel #11
0
        public static Bitmap CreatePlotLayoutBitmap(
            Size maxSize,
            DxfModel model,
            DxfLayout layout,
            DxfViewportCollection viewports,
            GraphicsConfig config,
            SmoothingMode smoothingMode)
        {
            DxfLayout.PlotInfo plotInfo = layout.GetPlotInfo((double)maxSize.Width, (double)maxSize.Height, new Rectangle2D(0.0, 0.0, (double)(maxSize.Width - 1), (double)(maxSize.Height - 1)), false, true);
            Size     size;
            Matrix4D to2DTransform;

            if (plotInfo != null)
            {
                Rectangle2D printableArea = plotInfo.PrintableArea;
                size = new Size((int)System.Math.Ceiling(printableArea.Width), (int)System.Math.Ceiling(printableArea.Height));
                WW.Math.Point2D center = printableArea.Center;
                to2DTransform = Transformation4D.Translation((WW.Math.Vector3D)(new WW.Math.Point2D(0.5 * (double)(size.Width - 1), 0.5 * (double)(size.Height - 1)) - center)) * plotInfo.ToPaperTransform;
            }
            else
            {
                BoundsCalculator boundsCalculator = new BoundsCalculator();
                boundsCalculator.GetBounds(model, layout);
                Bounds3D bounds = boundsCalculator.Bounds;
                if (!bounds.Initialized)
                {
                    return((Bitmap)null);
                }
                WW.Math.Point3D  min      = bounds.Min;
                WW.Math.Point3D  max      = bounds.Max;
                WW.Math.Vector3D vector3D = max - min;
                WW.Math.Point3D  point3D  = new WW.Math.Point3D(min.X, min.Y, 0.0);
                double           scaling;
                to2DTransform = DxfUtil.GetScaleTransform(point3D, new WW.Math.Point3D(max.X, min.Y, 0.0), point3D, WW.Math.Point3D.Zero, new WW.Math.Point3D((double)(maxSize.Width - 1), (double)(maxSize.Height - 1), 0.0), WW.Math.Point3D.Zero, out scaling);
                size          = new Size((int)System.Math.Ceiling(vector3D.X * scaling), (int)System.Math.Ceiling(vector3D.Y * scaling));
            }
            return(ImageExporter.CreateBitmap(model, layout, (ICollection <DxfViewport>)viewports, config, smoothingMode, (System.Drawing.Color)config.BackColor, to2DTransform, size.Width, size.Height));
        }
        public void getDXFFormat(DxfModel model, string filename, string outfile)
        {
            try
            {
                string extension = System.IO.Path.GetExtension(filename);
                if (string.Compare(extension, ".dwg", true) == 0)
                {
                    model = DwgReader.Read(filename);
                }
                else
                {
                    model = DxfReader.Read(filename);
                }
            }
            catch (Exception e)
            {
                //Console.Error.WriteLine("Error occurred: " + e.Message);
                //Environment.Exit(1);
            }

            GDIGraphics3D graphics = new GDIGraphics3D(GraphicsConfig.BlackBackgroundCorrectForBackColor);
            Size          maxSize  = new Size(500, 500);
            Bitmap        bitmap   =
                ImageExporter.CreateAutoSizedBitmap(
                    model,
                    graphics,
                    Matrix4D.Identity,
                    System.Drawing.Color.Black,
                    maxSize
                    );
            //string outfile = Path.GetFileNameWithoutExtension(Path.GetFullPath(filename));
            Stream stream = null;
            //string outfile = AppDomain.CurrentDomain.BaseDirectory + "Drill\\rockHistogram\\"+filename;

            BoundsCalculator boundsCalculator = new BoundsCalculator();

            boundsCalculator.GetBounds(model);
            Bounds3D  bounds    = boundsCalculator.Bounds;
            PaperSize paperSize = PaperSizes.GetPaperSize(PaperKind.A4);//设置为A4纸的大小
            // Lengths in inches.
            float pageWidth  = (float)paperSize.Width / 100f;
            float pageHeight = (float)paperSize.Height / 100f;
            float margin     = 0.2f; //值越小 model相对于pdf图幅越大
            // Scale and transform such that its fits max width/height
            // and the top left middle of the cad drawing will match the
            // top middle of the pdf page.
            // The transform transforms to pdf pixels.
            Matrix4D to2DTransform = DxfUtil.GetScaleTransform(
                bounds.Corner1,
                bounds.Corner2,
                new Point3D(bounds.Center.X, bounds.Corner2.Y, 0d),
                new Point3D(new Vector3D(margin, margin, 0d) * PdfExporter.InchToPixel),
                new Point3D(new Vector3D(pageWidth - margin, pageHeight - margin, 0d) * PdfExporter.InchToPixel),
                new Point3D(new Vector3D(pageWidth / 2d, pageHeight - margin, 0d) * PdfExporter.InchToPixel)
                );

            using (stream = File.Create(outfile + ".pdf"))
            {
                PdfExporter pdfGraphics = new PdfExporter(stream);
                pdfGraphics.DrawPage(
                    model,
                    GraphicsConfig.WhiteBackgroundCorrectForBackColor,
                    to2DTransform,
                    paperSize
                    );
                pdfGraphics.EndDocument();
            }

            /*
             * 可选的图片格式
             * stream = File.Create(outfile + ".png");
             * ImageExporter.EncodeImageToPng(bitmap, stream);
             *
             * stream = File.Create(outfile + ".tiff");
             * ImageExporter.EncodeImageToTiff(bitmap, stream);
             *
             * stream = File.Create(outfile + ".jpg");
             * ImageExporter.EncodeImageToJpeg(bitmap, stream);
             *
             * stream = File.Create(outfile + ".gif");
             * ImageExporter.EncodeImageToGif(bitmap, stream);
             *
             * stream = File.Create(outfile + ".bmp");
             * ImageExporter.EncodeImageToBmp(bitmap, stream);
             */
        }
Beispiel #13
0
        public static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                args = new string[2];
                //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test1.dwg";
                //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test2.dwg";
                //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test3.dwg";
                //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test4.dwg";
                args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test5Insert.dwg";
                //args[1] = "D:\\C项目\\CadLCmd\\CadLCmd\\dwg\\test6.dwg";
            }
            string format   = args[0];
            string filename = args[1];

            DxfModel model = null;

            try
            {
                string extension = Path.GetExtension(filename);
                if (string.Compare(extension, ".dwg", true) == 0)
                {
                    model = DwgReader.Read(filename);
                }
                else
                {
                    model = DxfReader.Read(filename);
                }
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error occurred: " + e.Message);
                Environment.Exit(1);
            }

            //foreach (var entityGroups in model.Entities.GroupBy(a => a.GetType()))
            //{

            //    Console.WriteLine(entityGroups.GetType());

            //    //if (typeof(DxfLine) == entityGroups.Key)
            //    //{
            //    //    foreach (var item in entityGroups)
            //    //    {
            //    //        Console.WriteLine(item.Color);

            //    //    }
            //    //}

            //}
            //FileStream fs = new FileStream("D:\\C项目\\CadLCmd\\CadLCmd\\txt\\test1.txt", FileMode.Create);



            FindEntities(model.Entities);



            string json = JsonConvert.SerializeObject(cadEntities);

            File.WriteAllText("D:\\C项目\\CadLCmd\\CadLCmd\\txt\\model.json", json);
            File.WriteAllText("D:\\Project\\cadtest\\node_modules\\@cadTestUbim\\res\\data\\dxfdata.json", json);

            //清空缓冲区
            sw.Flush();
            //关闭流
            sw.Close();
            fs.Close();

            KTCodes = ktls.ToArray();

            KTest = ktest.ToArray();

            DxfType = dxfType.ToArray();

            KInsert = kins.ToArray();

            KEC = kco.ToArray();

            KCIRCLE = kcircle.ToArray();

            //Console.ReadKey();
            string outfile = Path.GetDirectoryName(Path.GetFullPath(filename)) + "\\12";
            Stream stream;

            if (format == "pdf")
            {
                BoundsCalculator boundsCalculator = new BoundsCalculator();
                boundsCalculator.GetBounds(model);
                Bounds3D  bounds    = boundsCalculator.Bounds;
                PaperSize paperSize = PaperSizes.GetPaperSize(PaperKind.Letter);
                // Lengths in inches.
                float pageWidth  = (float)paperSize.Width / 100f;
                float pageHeight = (float)paperSize.Height / 100f;
                float margin     = 0.5f;
                // Scale and transform such that its fits max width/height
                // and the top left middle of the cad drawing will match the
                // top middle of the pdf page.
                // The transform transforms to pdf pixels.
                Matrix4D to2DTransform = DxfUtil.GetScaleTransform(
                    bounds.Corner1,
                    bounds.Corner2,
                    new Point3D(bounds.Center.X, bounds.Corner2.Y, 0d),
                    new Point3D(new Vector3D(margin, margin, 0d) * PdfExporter.InchToPixel),
                    new Point3D(new Vector3D(pageWidth - margin, pageHeight - margin, 0d) * PdfExporter.InchToPixel),
                    new Point3D(new Vector3D(pageWidth / 2d, pageHeight - margin, 0d) * PdfExporter.InchToPixel)
                    );
                using (stream = File.Create(outfile + ".pdf"))
                {
                    PdfExporter pdfGraphics = new PdfExporter(stream);
                    pdfGraphics.DrawPage(
                        model,
                        GraphicsConfig.WhiteBackgroundCorrectForBackColor,
                        to2DTransform,
                        paperSize
                        );
                    pdfGraphics.EndDocument();
                }
            }
            else
            {
                GDIGraphics3D graphics = new GDIGraphics3D(GraphicsConfig.BlackBackgroundCorrectForBackColor);
                Size          maxSize  = new Size(500, 500);
                Bitmap        bitmap   =
                    ImageExporter.CreateAutoSizedBitmap(
                        model,
                        graphics,
                        Matrix4D.Identity,
                        System.Drawing.Color.Black,
                        maxSize
                        );
                switch (format)
                {
                case "bmp":
                    using (stream = File.Create(outfile + ".bmp"))
                    {
                        ImageExporter.EncodeImageToBmp(bitmap, stream);
                    }
                    break;

                case "gif":
                    using (stream = File.Create(outfile + ".gif"))
                    {
                        ImageExporter.EncodeImageToGif(bitmap, stream);
                    }
                    break;

                case "tiff":
                    using (stream = File.Create(outfile + ".tiff"))
                    {
                        ImageExporter.EncodeImageToTiff(bitmap, stream);
                    }
                    break;

                case "png":
                    using (stream = File.Create(outfile + ".png"))
                    {
                        ImageExporter.EncodeImageToPng(bitmap, stream);
                    }
                    break;

                case "jpg":
                    using (stream = File.Create(outfile + ".jpg"))
                    {
                        ImageExporter.EncodeImageToJpeg(bitmap, stream);
                    }
                    break;

                default:
                    Console.WriteLine("Unknown format " + format + ".");
                    break;
                }
            }
        }
Beispiel #14
0
        // For each layout, add a page to the PDF file.
        // Optionally specify a modelView (for model space only).
        // Optionally specify a layout.
        public static void AddLayoutToPdfExporter(
            PdfExporter pdfExporter, GraphicsConfig config, DxfModel model, DxfView modelView, DxfLayout layout
            )
        {
            Bounds3D    bounds;
            const float defaultMargin = 0.5f;
            float       margin        = 0f;
            PaperSize   paperSize     = null;
            bool        useModelView  = false;
            bool        emptyLayout   = false;

            if (layout == null || !layout.PaperSpace)
            {
                // Model space.
                BoundsCalculator boundsCalculator = new BoundsCalculator();
                boundsCalculator.GetBounds(model);
                bounds = boundsCalculator.Bounds;
                if (bounds.Initialized)
                {
                    if (bounds.Delta.X > bounds.Delta.Y)
                    {
                        paperSize = PaperSizes.GetPaperSize(PaperKind.A4Rotated);
                    }
                    else
                    {
                        paperSize = PaperSizes.GetPaperSize(PaperKind.A4);
                    }
                }
                else
                {
                    emptyLayout = true;
                }
                margin       = defaultMargin;
                useModelView = modelView != null;
            }
            else
            {
                // Paper space layout.
                Bounds2D plotAreaBounds = layout.GetPlotAreaBounds();
                bounds      = new Bounds3D();
                emptyLayout = !plotAreaBounds.Initialized;
                if (plotAreaBounds.Initialized)
                {
                    bounds.Update((Point3D)plotAreaBounds.Min);
                    bounds.Update((Point3D)plotAreaBounds.Max);

                    if (layout.PlotArea == PlotArea.LayoutInformation)
                    {
                        switch (layout.PlotPaperUnits)
                        {
                        case PlotPaperUnits.Millimeters:
                            paperSize = new PaperSize(Guid.NewGuid().ToString(), (int)(plotAreaBounds.Delta.X * 100d / 25.4d), (int)(plotAreaBounds.Delta.Y * 100d / 25.4d));
                            break;

                        case PlotPaperUnits.Inches:
                            paperSize = new PaperSize(Guid.NewGuid().ToString(), (int)(plotAreaBounds.Delta.X * 100d), (int)(plotAreaBounds.Delta.Y * 100d));
                            break;

                        case PlotPaperUnits.Pixels:
                            // No physical paper units. Fall back to fitting layout into a known paper size.
                            break;
                        }
                    }

                    if (paperSize == null)
                    {
                        if (bounds.Delta.X > bounds.Delta.Y)
                        {
                            paperSize = PaperSizes.GetPaperSize(PaperKind.A4Rotated);
                        }
                        else
                        {
                            paperSize = PaperSizes.GetPaperSize(PaperKind.A4);
                        }
                        margin = defaultMargin;
                    }
                }
            }

            if (!emptyLayout)
            {
                // Lengths in inches.
                float pageWidthInInches  = paperSize.Width / 100f;
                float pageHeightInInches = paperSize.Height / 100f;

                double   scaleFactor;
                Matrix4D to2DTransform;
                if (useModelView)
                {
                    to2DTransform = modelView.GetMappingTransform(
                        new Rectangle2D(
                            margin * PdfExporter.InchToPixel,
                            margin * PdfExporter.InchToPixel,
                            (pageWidthInInches - margin) * PdfExporter.InchToPixel,
                            (pageHeightInInches - margin) * PdfExporter.InchToPixel),
                        false);
                    scaleFactor = double.NaN; // Not needed for model space.
                }
                else
                {
                    to2DTransform = DxfUtil.GetScaleTransform(
                        bounds.Corner1,
                        bounds.Corner2,
                        new Point3D(bounds.Center.X, bounds.Corner2.Y, 0d),
                        new Point3D(new Vector3D(margin, margin, 0d) * PdfExporter.InchToPixel),
                        new Point3D(new Vector3D(pageWidthInInches - margin, pageHeightInInches - margin, 0d) * PdfExporter.InchToPixel),
                        new Point3D(new Vector3D(pageWidthInInches / 2d, pageHeightInInches - margin, 0d) * PdfExporter.InchToPixel),
                        out scaleFactor
                        );
                }
                if (layout == null || !layout.PaperSpace)
                {
                    pdfExporter.DrawPage(model, config, to2DTransform, paperSize);
                }
                else
                {
                    pdfExporter.DrawPage(model, config, to2DTransform, scaleFactor, layout, null, paperSize);
                }
            }
        }