Work with CAD formats including DWG and DXF using Aspose.CAD for Java

Share on FacebookTweet about this on TwitterShare on LinkedIn

We are pleased to announce the release of Aspose.CAD for Java 1.0.0. This is a new addition to Aspose for Java APIs and allows you to work with CAD formats including DWG and DXF formats. Aspose.Imaging for Java also supports CAD formats but these formats will be removed from Aspose.Imaging for Java in next three months and all features related to CAD formats will be the part of Aspose.CAD only.

This release of Aspose.CAD for Java supports converting DWG and DXF formats to PDF and raster image formats. Converting CAD layers and layouts to PDF and raster image formats has also been supported by this release. You can also remove entities from DWG and DXF documents.

Convert CAD formats to PDF

Aspose.CAD for Java allows you to convert CAD drawings to PDF format. com.aspose.cad.Image.load method can be used to load CAD drawings, com.aspose.cad.imageoptions.CadRasterizationOptions can be used to set vector rasterization options, com.aspose.cad.imageoptions.PdfOptions can be used to set PDF options and com.aspose.cad.Image.save method can be used to save output PDF e.g.


String dataDir = Utils.getDataDir(ConvertDWGFileToPDF.class) + "DWGDrawings/";
String srcFile = dataDir + "Bottom_plate.dwg";

Image image = Image.load(srcFile);

// Create an instance of CadRasterizationOptions and set its various properties
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setBackgroundColor(Color.getWhite());
rasterizationOptions.setPageWidth(1600);
rasterizationOptions.setPageHeight(1600);

// Create an instance of PdfOptions
PdfOptions pdfOptions = new PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);

// Export the DWG to PDF
image.save(dataDir + "Bottom_plate_out_.pdf", pdfOptions);

Convert CAD formats to raster image formats

Aspose.CAD for Java allows you to convert CAD drawings to raster image formats. com.aspose.cad.Image.load method can be used to load CAD drawings, com.aspose.cad.imageoptions.CadRasterizationOptions can be used to set vector rasterization options, com.aspose.cad.ImageOptionsBase can be used to set image options and com.aspose.cad.Image.save method can be used to save output image e.g.


String dataDir = Utils.getDataDir(ConvertCADDrawingToRasterImageFormat.class) + "CADConversion/";
String srcFile = dataDir + "conic_pyramid.dxf";

Image image = Image.load(srcFile); 

// Create an instance of CadRasterizationOptions
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
// Set page width & height
rasterizationOptions.setPageWidth(1200);
rasterizationOptions.setPageHeight(1200);

// Create an instance of PngOptions for the resultant image
ImageOptionsBase options = new PngOptions();
//Set rasterization options
options.setVectorRasterizationOptions(rasterizationOptions);

// Save resultant image
image.save(dataDir + "conic_pyramid_raster_image_out_.png", options);

Remove entities from CAD documents

Aspose.CAD for Java allows you to remove specific entities from CAD drawings. com.aspose.cad.Image.load method can be used to load CAD drawings, com.aspose.cad.fileformats.cad.CadImage.getEntities() can be used to loop through each entity and com.aspose.cad.fileformats.cad.CadImage. removeEntity can be used to remove any entity e.g.


com.aspose.cad.fileformats.cad.CadImage image = (com.aspose.cad.fileformats.cad.CadImage) com.aspose.cad.Image
		.load("Input.dwg");

for (int i = 0; i < image.getEntities().length; i++) {
	CadBaseEntity baseEntity = image.getEntities()[i];
	if (baseEntity.getClass() == com.aspose.cad.fileformats.cad.cadobjects.CadArc.class) {
		com.aspose.cad.fileformats.cad.cadobjects.CadArc arc = (com.aspose.cad.fileformats.cad.cadobjects.CadArc) baseEntity;
		if (arc.getCenterPoint().getX() > 1e10) {
			// cadImage.RemoveEntityAt(i);
			image.removeEntity(baseEntity);
			i--;
		}
	}
}

image.updateSize();

// Create an instance of CadRasterizationOptions and set its various
// properties
com.aspose.cad.imageoptions.CadRasterizationOptions rasterizationOptions = new com.aspose.cad.imageoptions.CadRasterizationOptions();
rasterizationOptions.setBackgroundColor(com.aspose.cad.Color
		.getWhite());
rasterizationOptions.setLayouts(new String[] { "Model" });

rasterizationOptions.setPageWidth(1600);
rasterizationOptions.setPageHeight(1600);

rasterizationOptions.setCenterDrawing(true);
// Create an instance of PdfOptions
com.aspose.cad.imageoptions.PdfOptions pdfOptions = new com.aspose.cad.imageoptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);

image.save("Output.pdf", pdfOptions);

Other Important Features

Following is a list of some important features included in this release.

To view a complete list of new features and to download the latest release, please visit Aspose.CAD for Java 1.0.0 page in downloads section.

Aspose.CAD for Java Resources

The resources, you may need to accomplish your tasks: