DWG Drawings

Exporting DWG Drawings to PDF

Aspose.CAD for Java API can load AutoCAD drawings in DWG format, and convert it to PDF. This topic explains the usage of Aspose.CAD API to achieve the conversion of DWG to PDF format through simple steps as defined ahead.

DWG File Format

A DWG is a binary file that contains vector image data and metadata. The vector image data provides instructions to the CAD application about how to display the DWG; the metadata may contain a variety of information about the file including location-specific data and also client data. Open Specifications for DWG file format can be found in this PDF

Convert DWG Files to PDF

The following simple steps are required to convert DWG to PDF.

  1. Load the DWG file into an instance of Image.
  2. Create an object of the CadRasterizationOptions class and set the PageHeight & PageWidth properties.
  3. Create an object of the PdfOptions class and set the VectorRasterizationOptions property.
  4. Call the Image.save method and pass the object of PdfOptions as the second parameter.

The code sample below shows how to export DWG Drawings to PDF.

String srcFile = dataDir + "Bottom_plate.dwg";
com.aspose.cad.Image objImage = com.aspose.cad.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
objImage.save(dataDir + "Bottom_plate_out_.pdf", pdfOptions);

Create Single PDF with Different Layout Sizes

Aspose.CAD for Java allows you to convert the DWG file to a single PDF with different layout sizes. This approach works as follows:

  1. Load a DWG file using the Aspose.CAD.Image.load factory method.
  2. Create an instance of the CadRasterizationOptions class and set the resultant page height and width.
  3. Add required LayoutPageSizes for the CadRasterizationOptions object.
  4. Create an instance of PdfOptions class and set its VectorRasterizationOptions property.
  5. Export the image to PDF using the Image.save method.

The code sample below shows how to create a single PDF with different layouts.

String dataDir = Utils.getDataDir(SinglePDFWithDifferentLayouts.class) + "DWGDrawings/";
CadImage cadImage = (CadImage)Image.load(dataDir + "City skyway map.dwg");
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setPageWidth(1000);
rasterizationOptions.setPageHeight(1000);
//custom sizes for several layouts
rasterizationOptions.getLayoutPageSizes().addItem("ANSI C Plot", new SizeF(500, 1000));
rasterizationOptions.getLayoutPageSizes().addItem("8.5 x 11 Plot", new SizeF(1000, 100));
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);
cadImage.save(dataDir + "singlePDF_out.pdf", pdfOptions);

Exporting DWG to PDF/A and PDF/E

The following simple steps are required to convert DWG to PDF.

  1. Load the DWG file into an instance of Image.
  2. Create an object of the CadRasterizationOptions class and set the PageHeight & PageWidth properties.
  3. Create an object of the PdfOptions class and set the VectorRasterizationOptions property.
  4. Call the Image.save method and pass the object of PdfOptions as the second parameter.

The code sample below shows how to export DWG Drawings to PDF.

String srcFile = dataDir + "Bottom_plate.dwg";
com.aspose.cad.Image objImage = com.aspose.cad.Image.load(srcFile);
// Create an instance of PdfOptions
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(new CadRasterizationOptions());
pdfOptions.setCorePdfOptions(new PdfDocumentOptions());
pdfOptions.getCorePdfOptions().setCompliance(PdfCompliance.PdfA1a);
objImage.save(dataDir+"Saved1.pdf", pdfOptions);
pdfOptions.getCorePdfOptions().setCompliance(PdfCompliance.PdfA1b);
objImage.save(dataDir+"Saved.pdf", pdfOptions);

Supported AutoCAD Primitives

The following AutoCAD primitives are supported.

  • TEXT
  • MTEXT
  • ATTDEF
  • ATTRIB
  • ARC
  • ELLIPSE
  • HATCH
  • LEADER
  • POINT
  • VERTEX 2D
  • VERTEX 3D
  • POLYLINE 2D
  • LWPOLYLINE
  • RAY
  • CIRCLE
  • DIMENSION ORDINATE
  • DIMENSION LINEAR
  • DIMENSION ALIGNED
  • DIMENSION ANG 3Pt
  • DIMENSION ANG 2Ln
  • DIMENSION RADIUS
  • DIMENSION DIAMETER
  • SHAPE
  • SOLID
  • SPLINE
  • MLINE
  • LINE
  • XLINE
  • STYLE
  • DIMSTYLE
  • LTYPE
  • MLINESTYLE
  • LAYER
  • VIEWPORT
  • LAYOUT

Exporting Specific DWG Layout to PDF

This approach works as follows:

  1. Load a DWG file using the Image.load factory method.
  2. Create an instance of the CadRasterizationOptions class and set the resultant page height and width.
  3. Set the Layouts property for the CadRasterizationOptions object.
  4. Create an instance of PdfOptions class and set its VectorRasterizationOptions property.
  5. Export the image to PDF using the Image.save() method.

The code sample below shows how to convert a specific layout of DWG to PDF.

String srcFile = dataDir + "visualization_-_conference_room.dwg";
Image image = Image.load(srcFile);
// Create an instance of CadRasterizationOptions and set its various properties
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setPageWidth(1600);
rasterizationOptions.setPageHeight(1600);
// Specify desired layout name
rasterizationOptions.setLayouts(new String[] {"Layout1"});
// Create an instance of PdfOptions
PdfOptions pdfOptions = new PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);
// Export the DWG to PDF
image.save(dataDir + "ExportSpecificLayoutToPDF_out_.pdf", pdfOptions);

Exporting DWG to Raster or PDF using Fixed Size

Aspose.CAD for Java API can load AutoCAD drawings in DWG format, and convert it to PDF or Raster using fixed size.

The code sample below shows how to implement this feature.

public static void main(String[] args)
{
// The path to the resource directory.
String dataDir = Utils.getDataDir(ExportDWGToPDFOrRaster.class) + "DWGDrawings/";
String srcFile = dataDir + "Bottom_plate.dwg";
com.aspose.cad.Image objImage = com.aspose.cad.Image.load(srcFile);
Boolean currentUnitIsMetric = IsMetric(objImage.getUnitType());
int currentUnitCoefficient = objImage.getUnitType();
if (currentUnitIsMetric)
{
double metersCoeff = 1 / 1000.0;
double scaleFactor = metersCoeff / currentUnitCoefficient;
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setPageWidth((float)(210 * scaleFactor));
rasterizationOptions.setPageHeight((float)(297 * scaleFactor));
rasterizationOptions.setUnitType(UnitType.Millimeter);
}
else
{
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setPageWidth((float)(8.27f / currentUnitCoefficient));
rasterizationOptions.setPageHeight((float)(11.69f / currentUnitCoefficient));
rasterizationOptions.setUnitType(UnitType.Inch);
}
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(new CadRasterizationOptions());
objImage.save(dataDir+"Saved.pdf", pdfOptions);
}
public static Boolean IsMetric(int initial)
{
Boolean isMetric = true;
switch (initial)
{
case UnitType.Inch:
case UnitType.MicroInch:
case UnitType.Mil:
case UnitType.Foot:
case UnitType.Yard:
case UnitType.Mile:
case UnitType.Unitless:
isMetric = false;
}
return isMetric;
}
private Double Coefficient(int unitType)
{
Double coefficient = 1.0;
switch (unitType)
{
case UnitType.Parsec:
coefficient = 3.0857 * 10000000000000000.0;
break;
case UnitType.LightYear:
coefficient = 9.4607 * 1000000000000000.0;
break;
case UnitType.AstronomicalUnit:
coefficient = 1.4960 * 100000000000.0;
break;
case UnitType.Gigameter:
coefficient = 1000000000.0;
break;
case UnitType.Kilometer:
coefficient = 1000.0;
break;
case UnitType.Decameter:
coefficient = 10.0;
break;
case UnitType.Hectometer:
coefficient = 100.0;
break;
case UnitType.Meter:
coefficient = 1.0;
break;
case UnitType.Centimenter:
coefficient = 0.01;
break;
case UnitType.Decimeter:
coefficient = 0.1;
break;
case UnitType.Millimeter:
coefficient = 0.001;
break;
case UnitType.Micrometer:
coefficient = 0.000001;
break;
case UnitType.Nanometer:
coefficient = 0.000000001;
break;
case UnitType.Angstrom:
coefficient = 0.0000000001;
break;
case UnitType.Inch:
coefficient = 1.0;
break;
case UnitType.MicroInch:
coefficient = 0.000001;
break;
case UnitType.Mil:
coefficient = 0.001;
break;
case UnitType.Foot:
coefficient = 12.0;
break;
case UnitType.Yard:
coefficient = 36.0;
break;
case UnitType.Mile:
coefficient = 63360.0;
break;
}
return coefficient;
}

Support for rounded hidden lines when exporting DWG/DXF to BMP and PDF

This approach works as follows:

  1. Load a DWG file using the Aspose.CAD.Image.load factory method.
  2. Create an instance of the CadRasterizationOptions class.
  3. Create an instance of the CadRasterizationOptions class and set the resultant page height and width.
  4. Create an instance of PdfOptions class and set its VectorRasterizationOptions property.
  5. Export the image to PDF using the Image.save() method.

The code sample below shows how to implement this feature.

// The path to the resource directory.
String dataDir = Utils.getDataDir(SupportForHiddenLines.class) + "DWGDrawings/";
String sourceFilePath = dataDir + "Bottom_plate.dwg";
String outPath = dataDir + "Bottom_plate.pdf";
CadImage cadImage = (CadImage)Image.load(sourceFilePath);
List<String> list = Arrays.asList("Print","L1_RegMark","L2_RegMark");
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setPageHeight(cadImage.getHeight());
rasterizationOptions.setPageWidth(cadImage.getWidth()) ;
rasterizationOptions.setLayers(list);
PdfOptions pdfOptions = new PdfOptions();
rasterizationOptions.setLayouts(new String[] { "Model" });
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);
cadImage.save(outPath, pdfOptions);
System.out.println("\nThe DWG file exported successfully to PDF.\nFile saved at " + dataDir);

Support for setting print area for DWG image in exported PDF

Aspose.CAD for Java API can load AutoCAD drawings in DWG format, and convert it to PDF. This topic explains the usage of Aspose.CAD API to achieve the support for setting a print area for DWG image in exported PDF.

The code sample below shows how to implement this feature.

// The path to the documents directory.
string MyDir = RunExamples.GetDataDir_DWGDrawings();
string sourceFilePath = MyDir + "visualization_-_conference_room.dwg";
using (Aspose.CAD.Image image = Aspose.CAD.Image.Load(sourceFilePath))
{
// 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" };
// Create an instance of PdfOptions
Aspose.CAD.ImageOptions.PdfOptions pdfOptions = new Aspose.CAD.ImageOptions.PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.VectorRasterizationOptions = rasterizationOptions;
MyDir = MyDir + "ExportSpecificLayoutToPDF_out.pdf";
//Export the DWG to PDF
image.Save(MyDir, pdfOptions);
}

Support for reading XREF metadata for DWG file

  1. Load a DWG file using the Aspose.CAD.Image.load factory method.
  2. Traverse through every image entity.
  3. If the entity is CadUnderlay than XREF entity with metadata.

The code sample below shows how to achieve this feature.

CadImage image = (CadImage)Image.load(dataDir+"Bottom_plate.dwg");
{
for (CadBaseEntity entity : image.getEntities())
{
if (entity instanceof CadUnderlay)
{
//XREF entity with metadata
Cad3DPoint insertionPoint = ((CadUnderlay) entity).getInsertionPoint();
String path = ((CadUnderlay) entity).getUnderlayPath();
}
}
}

DWG document by specifying coordinates

Aspose.CAD for Java API now provides a feature to render DWG documents by providing coordinates of the width and height of the document.

The code sample below shows how to render a DWG document.

String srcFile = dataDir + "visualization_-_conference_room.dwg";
Image image = Image.load(srcFile);
// Create an instance of CadRasterizationOptions and set its various properties
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setPageWidth(1600);
rasterizationOptions.setPageHeight(1600);
// Specify desired layout name
rasterizationOptions.setLayouts(new String[] {"Layout1"});
// Create an instance of PdfOptions
PdfOptions pdfOptions = new PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);
// Export the DWG to PDF
image.save(dataDir + "ExportSpecificLayoutToPDF_out_.pdf", pdfOptions);

The property CadRasterizationOptions.Layouts is of type string array so you may specify more than one layouts at a time for possible conversion to PDF format. While specifying multiple layouts for the CadRasterizationOptions.Layouts property, the resultant PDF would have multiple pages, where each page represents an individual AutoCAD layout.

List All Layouts

You may list all the layouts present in an AutoCAD drawing using the following code snippet.

String srcFile = dataDir + "conic_pyramid.dxf";
Image image = Image.load(srcFile);
CadImage cadImage = (CadImage)image;
CadLayoutDictionary layouts = cadImage.getLayouts();
for (CadLayout layout : layouts.getValues())
{
System.out.println("Layout " + layout.getLayoutName());
}

Get Block Attribute Value Of External Reference

Aspose.CAD for Java API allows you to get an external reference of a block attribute. Aspose.CAD API exposes the getXRefPathName() property to get the external reference of a block attribute in a CadBlockDictionary collection.

The following code demonstrates how to get an external reference of a block attribute.

// Load an existing DWG file as CadImage.
CadImage cadImage = (CadImage) Image.load(dataDir + "sample.dwg");
// Access the external path name property
CadStringParameter sXternalRef = cadImage.getBlockEntities().get_Item("*MODEL_SPACE").getXRefPathName();
System.out.println(sXternalRef);

Search Text In DWG AutoCAD File

Aspose.CAD for Java API allows you to search text in a DWG AutoCAD file. Aspose.CAD API exposes CadText class that represents text entities in the DWG AutoCAD file. Class CadMText is also included in the Aspose.CAD API because some other entities may also contain text.

The following code demonstrates how to search text in the DWG AutoCAD file.

private static final String dataDir = Utils.getDataDir(SearchTextInDWGAutoCADFile.class) + "DWGDrawings/";
public static void main(String[] args) {
// Search Text In DWG AutoCAD File
searchTextInDWGAutoCADFile();
// Search For Text In Specific Layout
}
public static void searchTextInDWGAutoCADFile()
{
//String dataDir="Test_Apsose.CAD\\";
// Load an existing DWG file as CadImage.
CadImage cadImage = (CadImage) CadImage.load(dataDir + "sample_file.dwg");
for (CadBaseEntity entity : cadImage.getEntities())
{
IterateCADNodeEntities(entity);
}
// Search for text in the block section
for (CadBlockEntity blockEntity : cadImage.getBlockEntities().getValues())
{
for (CadBaseEntity entity : blockEntity.getEntities())
{
IterateCADNodeEntities(entity);
}
}
}
//Recursive function to iterate nodes inside nodes
private static void IterateCADNodeEntities(CadBaseEntity obj)
{
switch (obj.getTypeName())
{
case CadEntityTypeName.TEXT:
CadText childObjectText = (CadText) obj;
System.out.println(childObjectText.getDefaultValue());
break;
case CadEntityTypeName.MTEXT:
CadMText childObjectMText = (CadMText) obj;
System.out.println(childObjectMText.getText());
break;
case CadEntityTypeName.INSERT:
CadInsertObject childInsertObject = (CadInsertObject) obj;
for (CadBaseEntity tempobj : childInsertObject.getChildObjects())
{
IterateCADNodeEntities(tempobj);
}
break;
case CadEntityTypeName.ATTDEF:
CadAttDef attDef = (CadAttDef) obj;
System.out.println(attDef.getDefaultString());
break;
case CadEntityTypeName.ATTRIB:
CadAttrib attAttrib = (CadAttrib) obj;
System.out.println(attAttrib.getDefaultText());
break;
}
}

Search For Text In Specific Layout

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Java
// Load an existing DWG file as CadImage.
CadImage cadImage = (CadImage) CadImage.load(dataDir + "sample_file.dwg");
// get all layout names and link each layout with corresponding block with entities
CadLayoutDictionary layouts = cadImage.getLayouts();
String[] layoutNames = new String[layouts.size()];
int i = 0;
for (com.aspose.cad.fileformats.cad.cadobjects.CadLayout layout : layouts.getValues()) {
layoutNames[i++] = layout.getLayoutName();
System.out.println("Layout " + layout.getLayoutName() + " is found");
// find block, applicable for DWG only
CadBlockTableObject blockTableObjectReference = null;
for (CadBlockTableObject tableObject : cadImage.getBlocksTables()) {
if (String.CASE_INSENSITIVE_ORDER.compare(tableObject.getHardPointerToLayout(), layout.getObjectHandle()) == 0) {
blockTableObjectReference = tableObject;
break;
}
}
// Collection cadBlockEntity.Entities contains information about all entities on specific layout
// if this collection has no elements it means layout is a copy of Model layout and contains the same entities
// Below line of code is for reference
CadBlockEntity cadBlockEntity = cadImage.getBlockEntities().get_Item(blockTableObjectReference.getBlockName());
}

Accessing underlay Flags for DWG Format

Aspose.CAD for Java has implemented the underlay flags for DWG format and allows developers to access them. Following is the simple code demonstration.

String dataDir = Utils.getDataDir(AccessingUnderlayFlagsofDWG.class) + "DWGDrawings/";
String fileName = dataDir + "BlockRefDgn.dwg";
// Load an existing DWG file and convert it into CadImage
com.aspose.cad.fileformats.cad.CadImage image = (com.aspose.cad.fileformats.cad.CadImage) com.aspose.cad.Image.load(fileName);
// Go through each entity inside the DWG file
for (com.aspose.cad.fileformats.cad.cadobjects.CadBaseEntity entity : image.getEntities())
{
// Check if entity is of CadDgnUnderlay type
if (entity instanceof com.aspose.cad.fileformats.cad.cadobjects.CadDgnUnderlay)
{
// Access different underlay flags
com.aspose.cad.fileformats.cad.cadobjects.CadUnderlay underlay = (com.aspose.cad.fileformats.cad.cadobjects.CadUnderlay) entity;
System.out.println(underlay.getUnderlayPath());
System.out.println(underlay.getUnderlayName());
System.out.println(underlay.getInsertionPoint().getX());
System.out.println(underlay.getInsertionPoint().getY());
System.out.println(underlay.getRotationAngle());
System.out.println(underlay.getScaleX());
System.out.println(underlay.getScaleY());
System.out.println(underlay.getScaleZ());
System.out.println((underlay.getFlags() & com.aspose.cad.fileformats.cad.cadobjects.UnderlayFlags.UnderlayIsOn) == com.aspose.cad.fileformats.cad.cadobjects.UnderlayFlags.UnderlayIsOn);
System.out.println((underlay.getFlags() & com.aspose.cad.fileformats.cad.cadobjects.UnderlayFlags.ClippingIsOn) == com.aspose.cad.fileformats.cad.cadobjects.UnderlayFlags.ClippingIsOn);
System.out.println((underlay.getFlags() & com.aspose.cad.fileformats.cad.cadobjects.UnderlayFlags.Monochrome) != com.aspose.cad.fileformats.cad.cadobjects.UnderlayFlags.Monochrome);
break;
}
}

Support for DWG Entities

Aspose.CAD for Java API can load AutoCAD drawings in DWG format and support different entities to work with.

Support for MLeader Entity

A DWG is a binary file that contains vector image data and metadata. There are different entities in a DWG file. The following section describes an example of working with MLeader entity inside the DWG file.

String file = dataDir + "Multileaders.dwg";
Image image = Image.load(file);
// Test
CadImage cadImage = (CadImage)image;
Assert.areNotEqual(cadImage.getEntities().length, 0);
CadMLeader cadMLeader = (CadMLeader)cadImage.getEntities()[2];
Assert.areEqual(cadMLeader.getStyleDescription(), "Standard");
Assert.areEqual(cadMLeader.getLeaderStyleId(), "12E");
Assert.areEqual(cadMLeader.getArrowHeadId1(), "639");
Assert.areEqual(cadMLeader.getLeaderLineTypeID(), "14");
CadMLeaderContextData context = cadMLeader.getContextData();
Assert.areEqual(context.getArrowHeadSize(), 30.0, 0.1);
Assert.areEqual(context.getBasePoint().getX(), 481, 1);
Assert.areEqual(context.getContentScale(), 1.0, 0.01);
Assert.areEqual(context.getDefaultText().getValue(), "This is multileader with huge text\\P{\\H1.5x;6666666666666666666666666666\\P}bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
Assert.areEqual(context.hasMText(), true);
CadMLeaderNode mleaderNode = context.getLeaderNode();
Assert.areEqual(mleaderNode.getLastLeaderLinePoint().getX(), 473, 1);
CadMLeaderLine leaderLine = mleaderNode.getLeaderLine();
Assert.areEqual(leaderLine.getBreakEndPoint().toString(), null);
Assert.areEqual(Integer.toString(leaderLine.getBreakPointIndex().getValue()), Integer.toString(0));
Assert.areEqual(leaderLine.getBreakStartPoint().toString(), null);
Assert.areEqual(Integer.toString(leaderLine.getLeaderLineIndex().getValue()), Integer.toString(0));
Assert.areEqual(Integer.toString(leaderLine.getLeaderPoints().size()), Integer.toString(4));
Assert.areEqual(Integer.toString(mleaderNode.getBranchIndex()), Integer.toString(0));
Assert.areEqual(mleaderNode.getDogLegLength(), 8.0, 0.1);
Assert.areEqual(context.hasMText(), true);
// Assert.IsTrue((context.hasMText()==true , "Invalid Z value");
Assert.areEqual(context.getTextAttachmentType().getValue(), (short)1);
Assert.areEqual(context.getTextBackgroundColor().getValue(), 18);
Assert.areEqual(context.getTextHeight(), 20.0, 0.1);
Assert.areEqual(context.getTextStyleID().getValue(), "11");
Assert.areEqual(context.getTextRotation().getValue(), 0.0, 0.01);
Assert.areEqual(cadMLeader.getArrowHeadId1(), "639");
Assert.areEqual(cadMLeader.getLeaderType(), 1);
Assert.areEqual(cadMLeader.getBlockContentColor(), 0);
Assert.areEqual(cadMLeader.getLeaderLineColor(), 0);
Assert.areEqual(cadMLeader.getTextHeight(), 1.0, 0.01);

Support for Exporting DWG Entity to Image

A DWG is a binary file that contains vector image data and metadata. There are different entities in a DWG file. The following section describes an example of exporting a particular DWG entity to the image.

// The path to the resource directory.
String dataDir = Utils.getDataDir(ParticularDWGToImage.class) + "DWGDrawings/";
String sourceFilePath = dataDir + "visualization_-_conference_room.dwg";
CadImage cadImage = ((CadImage)(Image.load(sourceFilePath)));
CadBaseEntity[] entities = cadImage.getEntities();
List<CadBaseEntity> filteredEntities = new ArrayList<CadBaseEntity>();
for (CadBaseEntity baseEntity : entities) {
// selection or filtration of entities
if ((baseEntity.getTypeName() == CadEntityTypeName.TEXT)) {
filteredEntities.add(baseEntity);
}
}
CadBaseEntity[] arr = new CadBaseEntity[filteredEntities.size()];
cadImage.setEntities(filteredEntities.toArray(arr));
// Create an instance of CadRasterizationOptions and set its various properties
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setPageWidth(1600);
rasterizationOptions.setPageHeight(1600);
// Set Auto Layout Scaling
rasterizationOptions.setAutomaticLayoutsScaling(true);
// Create an instance of PdfOptions
PdfOptions pdfOptions = new PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);
String outFile = dataDir + "result_out_generated.pdf";
// Export the CAD to PDF
cadImage.save(outFile, pdfOptions);

Support for Import Image to DWG File

Aspose.CAD for Java API can load AutoCAD drawings in DWG format and support different entities to work with. A DWG is a binary file that contains vector image data and metadata. There are different entities in a DWG file. The following section describes how can we import images inside the DWG file.

Support for Adding text in DWG

Aspose.CAD for Java API can load AutoCAD drawings in DWG format and support different entities to work with. A DWG is a binary file that contains vector image data and metadata. There are different entities in a DWG file. The following section describes how can we add text inside the DWG file.

String dataDir = Utils.getDataDir(AddTextInDWG.class) + "DWGDrawings/";
String dwgPathToFile = dataDir + "SimpleEntites.dwg";
Image image = Image.load(dwgPathToFile);
CadText cadText = new CadText();
cadText.setStyleType("Standard");
cadText.setDefaultValue("Some custom text");
cadText.setColorId(256);
cadText.setLayerName("0");
cadText.getFirstAlignment().setX(47.9);
cadText.getFirstAlignment().setY(5.56);
cadText.setTextHeight(0.8);
cadText.setScaleX(0);
CadImage cadImage = ((CadImage)(image));
cadImage.getBlockEntities().get_Item("*Model_Space").addEntity(cadText);
PdfOptions pdfOptions = new PdfOptions();
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
cadRasterizationOptions.setPageHeight(1600);
cadRasterizationOptions.setPageWidth(1600);
cadRasterizationOptions.setLayouts(new String[] {"Model"});
image.save(dataDir+"SimpleEntites_generated.dwg.pdf", pdfOptions);

Support for Mesh in DWG

Aspose.CAD for Java API can now access the entities supporting mesh which include CadPolyFaceMesh and CadPolygonMesh types. The following example describes how can we access the mesh types.

Convert DWG Drawings to DXF

Aspose.CAD provides the feature to load AutoCAD DWG file and export it to DXF format. DWG to DXF conversion approach works as follows:

  1. Load DWG drawing file using the Image.load factory method.
  2. Export the DWG drawing to DXF using the Image.save() method.

The code sample below shows how to convert a DWG file to a DXF format.

// The path to the resource directory.
String dataDir = Utils.getDataDir(DWGToDXF.class) + "DWGDrawings/";
String inputFile = dataDir+ "Line.dwg";
String outFile = dataDir+ "Line.dxf";
CadImage cadImage = (CadImage)Image.load(inputFile);
cadImage.save(outFile);

Convert DWG Drawings to SVG

Aspose.CAD API can load AutoCAD drawings in DWG format, and convert it to SVG. This topic explains the usage of Aspose.CAD API to achieve the conversion of DWG to SVG format through simple steps as defined below.

  1. Load the DWG file into an instance of Image.
  2. Create an object of the SvgOptions class and set the required properties.
  3. Export the DWG drawing to SVG using the Image.save() method.

The code sample below shows how to convert a DWG file to SVG format.

// The path to the resource directory.
String dataDir = Utils.getDataDir(ExportToSVG.class) + "DWGDrawings/";
Image image = Image.load(dataDir + "meshes.dwg");
{
SvgOptions options = new SvgOptions();
options.setColorType(SvgColorMode.Grayscale);
options.setTextAsShapes(true);
image.save(dataDir + "meshes.svg");
}