DEV Community

Dmitry Matuzko
Dmitry Matuzko

Posted on

Introduction to Aspose.CAD, Part 3

Part 1
Part 2

Most cases when Aspose.CAD has to be used are probably associated with AutoCAD's files, such as DWG and DXF format. As such, I'm going to describe how can Aspose.CAD work with DWG and DXF formats.

Let's start from a simple and common thing: selection of particular layout or layer. Aspose.CAD supports enumeration of both layers and layouts and selection of one or several particular layers or layouts for export. Let's see!

Getting a list of layout names

Take look at this snippet:

            using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
            {
                Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)image;

                Aspose.CAD.FileFormats.Cad.CadLayoutDictionary layouts = cadImage.Layouts;
                foreach (Aspose.CAD.FileFormats.Cad.CadObjects.CadLayout layout in layouts.Values)
                {
                    Console.WriteLine("Layout " + layout.LayoutName);
                }
            }
Enter fullscreen mode Exit fullscreen mode

What's happening there? When you load an DXF or DWG file, it is represented as an instance of a subclass of CadImage, which has specific properties related to these file formats. As such, you cast your image to CadImage to get access to them.

Then there's Layouts property in CadImage, which is a specific dictionary for CadLayout objects. Extract the CadLayout objects from it as from any IDictionary, and here you have access to layout's properties, including its name.

Selection of layout for export

This, like any setup of export process, is done via setting up an instance of ImageOptionsBase's descendants, such as BmpOptions, PngOptions, PdfOptions etc. - let's just call them image export options, more specifically in this case, by setting up an instance of CadRasterizationOptions which is assigned to image export options's VectorRasterizationOptions property.

            using (Aspose.CAD.Image image = Aspose.CAD.Image.Load("in.dwg"))
            {


                // Create an instance of PdfOptions
                Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();

                // Create an instance of CadRasterizationOptions and set its various properties
                Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
                rasterizationOptions.PageWidth = 1600;
                rasterizationOptions.PageHeight = 1600;
                // Specify desired layout name
                rasterizationOptions.Layouts = new string[] { "Layout1" };


                // Set the VectorRasterizationOptions property
                pdfOptions.VectorRasterizationOptions = rasterizationOptions;


                image.Save("out.pdf", pdfOptions);                
            }
Enter fullscreen mode Exit fullscreen mode

As you can see, the process is the same that is outlined in Part 1 of this article series, the only change is use of Layouts property. Setting it will result in export of only these layouts which names are specified in Layouts property. By default it will use Model layout, so if you wish to revert to that layout, just set the property to null.

Getting a list of layer names

This is very similar to how it's done for layouts with only a small difference:

            using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
            {
                Aspose.CAD.FileFormats.Cad.CadImage cadImage = (Aspose.CAD.FileFormats.Cad.CadImage)image;

                Aspose.CAD.FileFormats.Cad.CadLayersList layers = cadImage.Layers;
                foreach (string layerName in layers.GetLayersNames())
                {
                    Console.WriteLine("Layer " + layerName);
                }
            }
Enter fullscreen mode Exit fullscreen mode

As you can see, the only difference is that Layers property is not a Dictionary, but an CadLayersList and it allows getting a List of layer name as strings directly.

Selection of particular layer for export

Again, this is very similar to working with layouts.



                // Create an instance of PdfOptions
                Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();

                // Create an instance of CadRasterizationOptions and set its various properties
                Aspose.CAD.ImageOptions.CadRasterizationOptions rasterizationOptions = new Aspose.CAD.ImageOptions.CadRasterizationOptions();
                rasterizationOptions.PageWidth = 1600;
                rasterizationOptions.PageHeight = 1600;
                // Specify desired layout name
                // Most of the CAD drawings have a layer by name 0, you may specify any name
                rasterizationOptions.Layers.Add("0");


                // Set the VectorRasterizationOptions property
                pdfOptions.VectorRasterizationOptions = rasterizationOptions;


                image.Save("out.pdf", pdfOptions);   
Enter fullscreen mode Exit fullscreen mode

The difference is that Layers property of CadRasterizationOptions is a List, and by default, when the list is empty, all layers will be exported.

For now, that's all, stay tuned!

For more examples please visit the Aspose.CAD GitHub page. There's also Twitter and Facebook pages for news on Aspose.CAD.

Top comments (0)