Copy blocks from another dwg/dxf file

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)

@velotech,
please, review if the attached example is useful.
copyBlock.zip (952 Bytes)

Hello,

The code given works perfectly. Thanks for the same.
Only one issue is that it does not maintain the layer colour and layer line type while copying. Refer d3.dxf which has 3 elements in block with different line type. But when i copy the d3 block to d1.dxf file the block is copied but I see only solid linetype.
Could you please help
asposedrg_with d3 block.7z (66.0 KB)

Regards,
Pranav

@velotech,
you need to extend the example with copying layers and linetypes and enable color export mode:

// copy all missing layers
CadLayersList layers = cadImageBase.getLayers();

for (Object layer: cadImageWithBlock.getLayers())
{
	if (layer instanceof CadLayerTable)
	{
		// TODO prevent adding duplicate of layers
		layers.addItem(layer);
	}
}

cadImageBase.setLayers(layers);


// copy all missing linetypes
CadLineTypesDictionary linetypes = cadImageWithBlock.getLineTypes();
CadLineTypesDictionary linetypesBase = cadImageBase.getLineTypes();

for (String linetype: linetypes.getKeys())
{
	if (!cadImageBase.getLineTypes().containsKey(linetype))
	{
		linetypesBase.addItem(linetype, linetypes.get_Item(linetype));
	}
}

cadImageBase.setLineTypes(linetypesBase);

// color mode
CadRasterizationOptions options = new CadRasterizationOptions();
options.setDrawType(CadDrawTypeMode.UseObjectColor);

PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(options);
cadImageBase.save(outputPdfFilePath, pdfOptions);