Implementing Aspose.Cad for JAVA in pumpsearch.com

Velotech Solutions Lunavat
2 min readOct 22, 2020

Introduction

One our projects requirements was to show the dxf drawing in pdf. Also the text of dxf drawings was supposed to be changed based on the product selected

Problem Statement

We had developed our own library in 2012, but it did not support all entities. Moreover the conversion from dxf to pdf was taking more time and this impacted the overall performance of the product

Sometimes the entity did not print or print wrongly. It used to take lot of time to find out the real issue. This impacted our deliverables to the customer

Aspose.Cad

We looked for many tools, but finally settled for Aspose Cad for java.

Aspose configuration was easy as we just need to include the jar in our library and follow the api

It took almost 1 month for us to get acquainted with the api. Our most challenging job was to modify the drawing before converting to pdf.

Some of our requirements where as follows

a. Modify text

b. Show or Hide layers

c. Modify images

During testing we were not clear about how to resize the image and posted the same on support forum. Their feedback helped us to solve the issue

Sample code for switching Layer On/Off

private void setLayersOnOff(CadImage cadImage){CadLayersList lay=cadImage.getLayers();List<String> layerName=lay.getLayersNames();CadBoolParameter cbFalse = new CadBoolParameter();cbFalse.setValue(false);CadBoolParameter cbTrue = new CadBoolParameter();cbTrue.setValue(true);for (String string : layerName) {if(layersList.containsKey(string)){if(layersList.get(string)==true)lay.getLayer(string).setPlotFlag(cbTrue);elselay.getLayer(string).setPlotFlag(cbFalse);}}}

This is the sample code we used to change the entity

private void IterateCADNodeEntities(CadBaseEntity obj) throws IOException {switch (obj.getTypeName()) {case CadEntityTypeName.TEXT:CadText childObjectText = (CadText) obj;if (blockValues.containsKey(childObjectText.getDefaultValue())&& !(childObjectText.getLayerName().equals("0")||childObjectText.getLayerName().equals("Sheet")))childObjectText.setDefaultValue(blockValues.get(childObjectText.getDefaultValue()));break;case CadEntityTypeName.MTEXT:CadMText childObjectMText = (CadMText) obj;if (blockValues.containsKey(childObjectMText.getText())) {childObjectMText.setText(blockValues.get(childObjectMText.getText()));}break;case CadEntityTypeName.ATTDEF:CadAttDef attDef = (CadAttDef) obj;if (blockValues.containsKey(attDef.getDefaultString())) {attDef.setDefaultString(blockValues.get(attDef.getDefaultString()));attDef.setAlignmentPoint(attDef.getAlignmentPoint());}break;case CadEntityTypeName.ATTRIB:CadAttrib attAttrib = (CadAttrib) obj;if (blockValues.containsKey(attAttrib.getDefaultText())) {attAttrib.setDefaultText(blockValues.get(attAttrib.getDefaultText()));attAttrib.setAlignmentPoint(attAttrib.getAlignmentPoint());attAttrib.setAttribAlignmentPoint(attAttrib.getAttribAlignmentPoint());attAttrib.setTextStartPoint(attAttrib.getTextStartPoint());}break;case CadEntityTypeName.IMAGE:CadRasterImage ans = (CadRasterImage) obj;if (handleId.containsKey(obj.getObjectHandle())) {CadRasterImage entity2 = (CadRasterImage) obj;BufferedImage bimg = ImageIO.read(new File(blockValues.get(handleId.get(obj.getObjectHandle()))));int width = bimg.getWidth();int height = bimg.getHeight();Double xscale = entity2.getImageSizeU() / width;Double yscale = entity2.getImageSizeV() / height;entity2.getUVector().setX(entity2.getUVector().getX() * Math.min(xscale, yscale));entity2.getVVector().setY(entity2.getVVector().getY() * Math.min(xscale, yscale));}break;case CadEntityTypeName.INSERT:CadInsertObject childInsertObject = (CadInsertObject) obj;for (CadBaseEntity tempobj : childInsertObject.getChildObjects()) {IterateCADNodeEntities(tempobj);}break;}}

After through trials and testing, we finalised to go for Aspose.Cad for Java for our product. This reduced our pdf generation time. Also we can get benefitted by using Autocad drawings files directly rather than converting to dxf

For more details visit www.pumpsearch.com, www.velotechsolutions.com

--

--