Hello,
How do I copy a block from another file to my existing file
Say my base drawing file is b1.dwg. I want to copy “b2” block with insertion point from b2.dwg to b1.dwg at 0,0 point and print to pdf
I tried but not able to do so. Could you please help
public class CopyBlocks {
public static void main(String[] args) throws IOException {
try {
com.aspose.cad.License license = new com.aspose.cad.License();
Random rand = new Random();
System.out.println("start time:" + new Date());
// ExStart:ExportDXFDrawingToPDF
String srcPath = "e:/asposedrg/";
String srcFile = "b1.dwg";
String srcFile2 = "b2.dwg";
String destFile2 = "output.pdf";
Image image = Image.load(srcPath + srcFile);
CadImage cadImage = (CadImage) image;
Image b2Image = Image.load(srcPath + srcFile2);
CadImage cadImageB2 = (CadImage) b2Image;
// Create an instance of CadRasterizationOptions and set its various
// properties
CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
setPage(rasterizationOptions, "Portrait", cadImage.getUnitType());
rasterizationOptions.setAutomaticLayoutsScaling(true);
rasterizationOptions.setBackgroundColor(Color.getWhite());
rasterizationOptions.setLayouts(new String[] { "Model" });
// Added to reduced pdf size
RasterizationQuality q = new RasterizationQuality();
q.setObjectsPrecision(RasterizationQualityValue.Low);
q.setText(RasterizationQualityValue.Low);
rasterizationOptions.setQuality(q);
for (CadBaseEntity entity : cadImageB2.getEntities()) {
if (entity instanceof CadInsertObject) {
CadInsertObject cib = (CadInsertObject) entity;
if (cib.getName().equals("b2")) {
cadImage.getBlockEntities().get_Item("*Model_Space").addEntity(cib);
}
}
}
// Create an instance of PdfOptions
PdfOptions pdfOptions = new PdfOptions();
// Set the VectorRasterizationOptions property
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);
// Export the DXF to PDF
image.save(srcPath + rand.nextInt() + destFile2, pdfOptions);
// ExEnd:ExportDXFDrawingToPDF
System.out.println("end time:" + new Date());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void setPage(CadRasterizationOptions rasterizationOptions, String layout, int cadImageUnitType) {
// reference
// https://apireference.aspose.com/cad/java/com.aspose.cad/Image
Boolean currentUnitIsMetric = false;
double currentUnitCoefficient = 1.0;
PageSize pageSize = new PageSize(cadImageUnitType);
currentUnitIsMetric = pageSize.getIsMetric();
currentUnitCoefficient = pageSize.getCoefficient();
if (currentUnitIsMetric) {
double metersCoeff = 1 / 1000.0;
double scaleFactor = metersCoeff / currentUnitCoefficient;
rasterizationOptions.setUnitType(UnitType.Millimeter);
if (layout.equals("Portrait")) {
rasterizationOptions.setPageWidth((float) (210 * scaleFactor));
rasterizationOptions.setPageHeight((float) (297 * scaleFactor));
} else {
rasterizationOptions.setPageWidth((float) (297 * scaleFactor));
rasterizationOptions.setPageHeight((float) (210 * scaleFactor));
}
} else {
rasterizationOptions.setUnitType(UnitType.Inch);
if (layout.equals("Portrait")) {
rasterizationOptions.setPageWidth((float) (8.27f / currentUnitCoefficient));
rasterizationOptions.setPageHeight((float) (11.69f / currentUnitCoefficient));
} else {
rasterizationOptions.setPageWidth((float) (11.69f / currentUnitCoefficient));
rasterizationOptions.setPageHeight((float) (8.27f / currentUnitCoefficient));
}
}
}
}
Regards,
Pranav
asposedrg.7z (44.2 KB)