How to adapt DXF to DXF image to fit the window


#1

My current practice is as follows, but some of the maps are wrong.

using (CadImage cadImage = (CadImage)Image.Load(sourceFilePath))
{
#region
Aspose.CAD.FileFormats.Cad.CadTables.CadViewTableObject ss = new Aspose.CAD.FileFormats.Cad.CadTables.CadViewTableObject();
double Xmin = 0;
double Xmax = 0;
double Ymin = 0;
double Ymax = 0;

                foreach (CadBaseEntity entity in cadImage.Entities)
                {
                    if (entity is Aspose.CAD.FileFormats.Cad.CadObjects.Polylines.CadPolyline3D)
                    {
                        foreach (Cad3DVertex ttt in entity.ChildObjects)
                        {
                            if (ttt.LocationPoint.X < Xmin || Xmin == 0)
                            {
                                Xmin = ttt.LocationPoint.X;
                            }
                            if (ttt.LocationPoint.X > Xmax || Xmax == 0)
                            {
                                Xmax = ttt.LocationPoint.X;
                            }
                            if (ttt.LocationPoint.Y < Ymin || Ymin == 0)
                            {
                                Ymin = ttt.LocationPoint.Y;
                            }
                            if (ttt.LocationPoint.Y > Ymax || Ymax == 0)
                            {
                                Ymax = ttt.LocationPoint.Y;
                            }
                        }
                    }



                }

                double Dx = Xmax - Xmin;//
                double Dy = Ymax - Ymin;
                #endregion
                
                Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
                double ImageWidth = cadImage.Width;//
                double ImageHeight = cadImage.Height;

                double ZommW = ImageWidth / Dx;//
                double ZommH = ImageHeight / Dy;
                double Zoom;
                double PngW = width / ImageWidth;//
                double PngH = height / ImageHeight;

                if (ZommW > ZommH)
                {
                    rasterizationOptions.Zoom = 1f * (float)ZommH;
                    Zoom = ZommH;
                }
                else
                {
                    rasterizationOptions.Zoom = 1f * (float)ZommW;
                    Zoom = ZommW;
                }
                if (PngW > PngH)
                {
                   
                    rasterizationOptions.PageWidth = (float)ImageWidth * (float)PngH;
                    rasterizationOptions.PageHeight = (float)ImageHeight * (float)PngH;
                }
                else
                {
                    rasterizationOptions.PageWidth = (float)ImageWidth * (float)PngW;
                    rasterizationOptions.PageHeight = (float)ImageHeight * (float)PngW;
                }

                
                ImageOptionsBase options = new Aspose.CAD.ImageOptions.PngOptions();
                
                options.VectorRasterizationOptions = rasterizationOptions;

                MyDir = MyDir + FileName + ".png";
                
                cadImage.Save(MyDir, options);

#2

@zziimmss,

I have observed your requirements and have not been able to completely understand them. Can you please elaborate your requirements in the form of sample DXF and DXF to image file. Please also provide the required output and complete working example as well that you are using on your end.


#3

DXF file to image, I need to get the minimum XY and maximum XY value, to make the DXF image fill the entire picture, but the above code can not do all types.


#4

@zziimmss,

I have observed your comments. Can you please confirm that you need to fill the drawing with only polylines. Please explain your requirements in form of sample so that we may further investigate to help you out.


#5

The version I am using now is 16.12.0. There is no object to adjust the Zoom. How to adjust the zoom ratio in this version?


#6

@zziimmss,

You are using fairly old version on your end. Please first try using latest Aspose.CAD for .NET 18.8.1 on your end.


#7

thank you very much! Now I use Zoom to control the DXF to fit the canvas so that the picture fits the window. Is there a better way than my current solution or can I get the Zoom that currently loads DXF? My current solution does not solve all the objects, here is my code. C#

Blockquote
MinMaxXY ret = new MinMaxXY();

        foreach (CadBaseEntity entity in Entities)
        {
            #region CadPolyline3D
            if (entity is Aspose.CAD.FileFormats.Cad.CadObjects.Polylines.CadPolyline3D)
            {
                foreach (Cad3DVertex ttt in entity.ChildObjects)
                {
                    if (ttt.LocationPoint.X < ret.Xmin )
                    {
                        ret.Xmin = ttt.LocationPoint.X;
                    }
                    if (ttt.LocationPoint.X > ret.Xmax )
                    {
                        ret.Xmax = ttt.LocationPoint.X;
                    }
                    if (ttt.LocationPoint.Y < ret.Ymin )
                    {
                        ret.Ymin = ttt.LocationPoint.Y;
                    }
                    if (ttt.LocationPoint.Y > ret.Ymax )
                    {
                        ret.Ymax = ttt.LocationPoint.Y;
                    }
                }
            }
            #endregion

            #region CadLine 直线段
            if (entity is CadLine)
            {
                CadLine cl = (CadLine)entity;
                var x = cl.FirstPoint.X;
                var x1 = cl.SecondPoint.X;
                var y = cl.FirstPoint.Y;
                var y1 = cl.SecondPoint.Y;
                
                if (x < ret.Xmin )
                {
                    ret.Xmin = x;
                }
                if (x > ret.Xmax )
                {
                    ret.Xmax = x;
                }
                if (x1 < ret.Xmin )
                {
                    ret.Xmin = x1;
                }
                if (x1 > ret.Xmax )
                {
                    ret.Xmax = x1;
                }
                if (y < ret.Ymin )
                {
                    ret.Ymin = y;
                }
                if (y > ret.Ymax )
                {
                    ret.Ymax = y;
                }
                if (y1 < ret.Ymin)
                {
                    ret.Ymin = y1;
                }
                if (y1 > ret.Ymax)
                {
                    ret.Ymax = y1;
                }

            }
            #endregion

            #region CadArc 圆弧
            if (entity is CadArc)
            {
                CadArc cl = (CadArc)entity;
                var x = cl.CenterPoint.X + cl.Radius * Math.Cos(cl.StartAngle * Math.PI / 180);
                var x1 = cl.CenterPoint.X + cl.Radius * Math.Cos(cl.EndAngle * Math.PI / 180);
                var y = cl.CenterPoint.Y + cl.Radius * Math.Sin(cl.StartAngle * Math.PI / 180);
                var y1 = cl.CenterPoint.Y + cl.Radius * Math.Sin(cl.EndAngle * Math.PI / 180);
                if (x < ret.Xmin)
                {
                    ret.Xmin = x;
                }
                if (x > ret.Xmax)
                {
                    ret.Xmax = x;
                }
                if (x1 < ret.Xmin)
                {
                    ret.Xmin = x1;
                }
                if (x1 > ret.Xmax)
                {
                    ret.Xmax = x1;
                }
                if (y < ret.Ymin)
                {
                    ret.Ymin = y;
                }
                if (y > ret.Ymax)
                {
                    ret.Ymax = y;
                }
                if (y1 < ret.Ymin)
                {
                    ret.Ymin = y1;
                }
                if (y1 > ret.Ymax)
                {
                    ret.Ymax = y1;
                }
            }
            #endregion

            #region CadCircle 圆
            if (entity is CadCircle && !(entity is CadArc))
            {
                CadCircle cl = (CadCircle)entity;
                var x = cl.CenterPoint.X + cl.Radius;
                var x1 = cl.CenterPoint.X - cl.Radius;
                var y = cl.CenterPoint.Y + cl.Radius;
                var y1 = cl.CenterPoint.Y - cl.Radius;
                if (x < ret.Xmin)
                {
                    ret.Xmin = x;
                }
                if (x > ret.Xmax)
                {
                    ret.Xmax = x;
                }
                if (x1 < ret.Xmin)
                {
                    ret.Xmin = x1;
                }
                if (x1 > ret.Xmax)
                {
                    ret.Xmax = x1;
                }
                if (y < ret.Ymin)
                {
                    ret.Ymin = y;
                }
                if (y > ret.Ymax)
                {
                    ret.Ymax = y;
                }
                if (y1 < ret.Ymin)
                {
                    ret.Ymin = y1;
                }
                if (y1 > ret.Ymax)
                {
                    ret.Ymax = y1;
                }
            }
            #endregion
        }

        return ret;

Blockquote using
chicun = chicun.RetMinMaxXY(cadImage.Entities);
double Dx = chicun.Xmax - chicun.Xmin;//DXF image actual size
double Dy = chicun.Ymax - chicun.Ymin;
double ZommW = ImageWidth / Dx;//DXF image drawing size ratio
double ZommH = ImageHeight / Dy;


#8

PS:Some DXF images are not filled with canvas
For example, the picture jpg in the file is not satisfied, PNG is satisfied.
Causes me to parse the minimum XY and maximum XY to control their Zoom,S301.png (5.9 KB)
S301.jpg (24.5 KB)


#9

@zziimmss,

I have tried understanding your requirements and have not been able to completely understand the requirements. Can you please elaborate your requirements along with source files and desired output that you want to generate using Aspose.CAD. Please also provide the working sample project as well.