loading...

Introduction to Aspose.CAD library

nnevod profile image Dmitry Matuzko ・5 min read

Aspose.CAD is a standalone software library for .Net and Java platforms that reads CAD files such as DWG, DXF, DNG, IFC, STL files and can export their contents to PDF files and raster images. It requires no additional software to work - no need for AutoCAD or ReadDWG, it works by itself.

It also allows partial export of files, exporting only specific entities or layers from DWG and DXF files, substituting colors or fonts specified in CAD files during export process, and export of 3D entities as well.

While Aspose.CAD is a commercial library, it allows free evaluation, which offers all the functionality and can readily be used for experimenting, but only loads files of limited complexity and watermarks exported images. Besides paying upfront, one can use metered, pay-per-use license.

I'm going to write a series of articles covering key points of working with Aspose.CAD using Aspose.CAD for .NET, starting from the most basic things such as loading CAD files and exporting images to more specific aspects.

So, let's begin with the most straightforward case:

Loading CAD file and exporting image

The process would consist of three main steps:
1) Load CAD file.
2) Set up exporting settings.
3) Save the image.

Load CAD file

First step is very straightforward. Just call the static Load method of Image class passing the file path to it:


            string dwgPathToFile =  "files/cad.dwg";
            Image cadImage1 = Image.Load(dwgPathToFile);

The Load method will determine the file format from file extension and create corresponding object of Image's subclass. No hassle there!

Now I'll skip to the third step, as it's equally simple, and then return to the second step.

Save the image

Call the Save method on your loaded Image, passing file path to save and ImageOptionsBase object and it's done.

                cadImage1.Save("cad.pdf", pdfOptions);

The output image format will be determined from particular subclass of ImageOptionsBase, of which pdfOptions is an instance. There are separate classes for all output formats. Let's dive deeper in the second step.

Set up exporting options

This is where the main work is done from library user's standpoint. You select the output format by creating an object of corresponding subclass of ImageOptionsBase.
There are the following subclasses of ImageOptionsBase:
PdfOptions, BmpOptions, GifOptions, JpegOptions,PngOptions, PsdOptions, TiffOptions and some more. Each provides setup for corresponding output file format. Then set it up by setting its properties and then pass it to the third step.
The most important property would be VectorRasterizationOptions property, which we should set with an instance of CadRasterizationOptions class. This class specifies how your CAD image should be rendered - width, height in pixels, wether CAD content should be centered, background color and override color for objects, etc. In its simplest form this step could be done the following way:


                PdfOptions pdfOptions = new PdfOptions();
                CadRasterizationOptions rasterizationOptions= new CadRasterizationOptions();
                pdfOptions.VectorRasterizationOptions = rasterizationOptions;

                rasterizationOptions.PageHeight = 1600;
                rasterizationOptions.PageWidth = 1600;
                rasterizationOptions.DrawType = CadDrawTypeMode.UseObjectColor;

So we create an PdfOptions instance which obviously sets up the output into PDF file format, create new CadRasterizationOptions to set up how the CAD image should be rendered and set PdfOptions's VectorRasterizationOptions property with it, then specify output image height and width (
By default, your CAD image will be stretched/shrunk to fit to specified output page size, maintaining aspect ratio) and let the rasterization use colors specified in the CAD file.
After this setup, we're ready for step three.

Now you know how to export a CAD file to an raster image or PDF file using Aspose.CAD for .NET. Java version of Aspose.CAD is essentially identical as well.

Here's the complete example:

using System;
using System.Collections.Generic;
using Aspose.CAD;
using Aspose.CAD.ImageOptions;

namespace ConsoleExampleAsposeCAD
{
    class Program
    {
        static void Main()
        {

            string dwgPathToFile =  "files/cad.dwg";
            Image cadImage1 = Image.Load(dwgPathToFile);

            PdfOptions pdfOptions = new PdfOptions();
            CadRasterizationOptions rasterizationOptions= new CadRasterizationOptions();
            pdfOptions.VectorRasterizationOptions = rasterizationOptions;

            rasterizationOptions.PageHeight = 1600;
            rasterizationOptions.PageWidth = 1600;
            rasterizationOptions.DrawType = CadDrawTypeMode.UseObjectColor;

            cadImage1.Save("cad.pdf", pdfOptions);            


        }
    }
}

Just create a new console application, copy the example content into Program.cs, install Aspose.CAD for .NET from NuGet and see for yourself.

Rotating and flipping image

With Aspose.CAD it's very easy to export CAD image not only as it is, but in rotated or mirriored form. Just get back to the second step of the first part, and tweak it a little:


                PdfOptions pdfOptions = new PdfOptions();
                //Here it is, the rotation and flip!
                pdfOptions.Rotation = RotateFlipType.Rotate270FlipY;
                //^^^^
                CadRasterizationOptions rasterizationOptions= new CadRasterizationOptions();
                pdfOptions.VectorRasterizationOptions = rasterizationOptions;

                rasterizationOptions.PageHeight = 1600;
                rasterizationOptions.PageWidth = 1600;
                rasterizationOptions.DrawType = CadDrawTypeMode.UseObjectColor;

As seen, the Rotation property accepts an value from RotateFlipType enum. It lists all combinations of rotation in 90 degree steps and mirroring along X and Y axes -either or both at once. The Rotation property is availible for all subclasses of ImageOptionsBase, meaning it is supported for any output format.

That's all for now, stay tuned!

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

Posted on by:

Discussion

pic
Editor guide