Modify dxfs using Java

I see this is mentioned on the website using the CAD API, but I’m unsure if that is what I’m looking for. For example, say I have a DXF of a door and I want to alter the size of the door and then export the dxf with the new dimensions.

@whydoihavetosignuptoaskabuyerquestion

Can you please provide more details about the specific modifications you want to make to the DXF file, such as the new dimensions for the door?

Does the new dimensions matter…? e.g door dxf starts off 800mm wide 2000mm high. I want to change the dimension to 820mm wide 2010mm high then export the dxf with the new dimensions.

@whydoihavetosignuptoaskabuyerquestion,
Hello.
The exact new dimension values don’t matter, but other details matter :slight_smile: Could you please attach the sample file with door and dimensions (it is perfect if you can provide the desired result in DXF too). The most important question is how the “door” is stored in the file, is it a block, or is it created from lines, arcs, etc., and what is dimension - is it DIMENSION entity, or just TEXT, or MTEXT for instance. The drawing contains the list of all entities, you can modify them. Here is the example how to modify the values of MTEXT entities, and other entities could be modified similarly.

CadImage cadImage = (CadImage)Image.load(inputFile);

for (CadEntityBase baseEntity: cadImage.getEntities())
{
	if (baseEntity.getTypeName() == CadEntityTypeName.MTEXT)
	{
		CadMText mtext = (CadMText)baseEntity;
		mtext.setText("New value");
	}
}

cadImage.save(newFileName);

Thanks for the response.
The door is stored as lines, arcs, etc. I’ve attached a simple example of what the inital dxf would be and an example of the modified dxf. The initial dxf would be the master, a user will enter the size of door that they need, a new dxf is created from the master with the new dimensions and then the new dxf is output.
Single door simple initial.jpg (67.6 KB)

Single door simple with change.jpg (68.9 KB)

dxfs attached
Simple doors.zip (10.4 KB)

@whydoihavetosignuptoaskabuyerquestion,
thank you for all details, please give us some time for investigation, I will return with more information.

@whydoihavetosignuptoaskabuyerquestion,
So, the situation seems to be the following.

In order to modify lines and dimensions in the DXF we need to iterate Entities collection and map the relation between lines and corresponding dimensions ourselves. Modifying lines is not so complex, the example below extends the horizontal lines to the left. But the situation with dimensions is complex, because its structure is stored not only as DIMENSION entity, but also as a separate set of entities - LINES, MTEXT, SOLID for arrows and so on. So, when you modify the dimension in AutoCAD, it forms all these entities separately and writes them into the file. There is no choice except of modifying all these lines and text items properly when we need to change a dimension.

The example below shows how to start modifying separate items for the particular dimension object (with the length 896).

CadImage cadImage = (CadImage)Image.load(in);

int masterWidth1 = 896;

// differences are known from master dimensions and new ones
int differenceWidth1 = masterWidth1 - 926;

// iterate over all entities in the drawing
for (CadEntityBase baseEntity: cadImage.getEntities())
{
	// this shows how to modify regular lines in the drawing
	if (baseEntity.getTypeName() == CadEntityTypeName.LINE)
	{
		CadLine line = (CadLine) baseEntity;
		Cad3DPoint first = line.getFirstPoint();
		Cad3DPoint second = line.getSecondPoint();

		// if Y coordinate is the same - it is horizontal line
		if (Math.abs(first.getY() - second.getY()) < 1)
		{
			// move its first point to the left
			first.setX(first.getX() + differenceWidth1);
		}
	}
	
	// if current entity is dimension
	if (baseEntity.getTypeName() == CadEntityTypeName.DIMENSION)
	{
		// cast to rotated because only such dimensions are in the drawing
		CadRotatedDimension dimensionBase = (CadRotatedDimension) baseEntity;

		// copy first definitioan point
		Cad3DPoint defPoint1 = new Cad3DPoint(dimensionBase.getDefinitionPoint1().getX(),
				dimensionBase.getDefinitionPoint1().getY(),
				dimensionBase.getDefinitionPoint1().getZ());

		Cad3DPoint defPoint2 = dimensionBase.getDefinitionPoint2();

		// get measurement of the dimension
		double measurement = Math.sqrt(Math.pow(defPoint1.getX() - defPoint2.getX(), 2) +
				Math.pow(defPoint1.getY() - defPoint2.getY(), 2));

		// if the measurement corresponds to the master one - we have found the required dimension
		if (Math.abs(measurement - masterWidth1) < 1)
		{
			// get the collection of entities that form the dimension
			CadBlockEntity dimensionBlock = cadImage.getBlockEntities().get_Item(dimensionBase.getBlockName());

			// move the definition point 1 to increase the dimension size
			dimensionBase.getDefinitionPoint1().setX(dimensionBase.getDefinitionPoint1().getX() + differenceWidth1);

			// look at all entities that form the dimension
			for (CadEntityBase blockBaseEntity: dimensionBlock.getEntities())
			{
				// MTEXT stores its text
				if (blockBaseEntity.getTypeName() == CadEntityTypeName.MTEXT)
				{
					CadMText mtext = (CadMText)blockBaseEntity;
					mtext.setText(Double.toString(measurement - differenceWidth1));
				}

				// move horizontal and vertical lines of the dimensions properly
				if (blockBaseEntity.getTypeName() == CadEntityTypeName.LINE)
				{
					CadLine line = (CadLine) blockBaseEntity;
					Cad3DPoint first = line.getFirstPoint();
					Cad3DPoint second = line.getSecondPoint();

					if (Math.abs(defPoint1.getX() - first.getX()) < 1)
					{
						// move vertical line
						if (Math.abs(first.getX() - second.getX()) < 1)
						{
							first.setX(first.getX() + differenceWidth1);
							second.setX(second.getX() + differenceWidth1);
						}

						// extend horizontal line
						if (Math.abs(first.getY() - second.getY()) < 1)
						{
							first.setX(first.getX() + differenceWidth1);
						}
					}
				}
			}
		}
	}
}

cadImage.save(out);

But the main issue at the moment relates to the dimension itself: after we just resave the initial file without any modifications, the dimensions in output DXF file are not possible to change anymore, the text of dimension becomes unreadable. We have created CADJAVA-11696 to cover this issue, and after that will return to the example above again, perhaps, it will require some additional changes.

Thank you very much for these details. Are you asying that if you make a change to the dimension, it can’t be changed for a second time. i.e. The master still remains readable, but the copy dimension is unreadable? Is it unreadable in terms of access by the code or unreadable in terms of output. i.e. unreadable on the drawing by a person?

@whydoihavetosignuptoaskabuyerquestion,
the “master” file remains the same, it is not modified. The file after export is readable, but when you modify the dimension in AutoCAD it loses its text value. I have attached this file here so you can check this.
Single door simple initial.dxf_java_.zip (6.3 KB)