DWG modify custom properties and save as DWG

Hello,

I load/read i DWG file and then read the custom properties, alter one of the properties and set the custom properties map back on the CadImage object.
When I then convert it to PDF, the custom properties fields that should be displayed in the converted pdf are not updated, instead, it still displays the old values. The reason I save it as a
PDF is that I can not find a way to just update the DWG file (save it again as a DWG).

Could not upload DWG so I took a print screen of it.
DWGPrintScreen.png (46.0 KB)
ResultingPDF.png (15.6 KB)

My test code:

public void testDWG() throws FileNotFoundException {

    //Native lang not supported expetion thrown if not set to English.
    java.util.Locale.setDefault(java.util.Locale.ENGLISH);
    CadImage objImage = (CadImage) CadImage.load(TEST_RESOURCE_PATH.concat("testDwg.dwg"));
    HashMap<String, String> customProperties = objImage.getHeader().getCustomProperties();
    //old value is set to TOLI
    customProperties.put("ORIGINAL_PREPARED_BY_INITIALS", "JT");

    // Prints JT
    System.out.println("updatedPropVal = " + objImage.getHeader().getCustomProperties().get("ORIGINAL_PREPARED_BY_INITIALS"));

    // Export the DWG to PDF
    //Result a pdf, but the custom property value is printed as TOLI.
    objImage.save(TEST_RESOURCE_PATH.concat("testDWGtoPDF.pdf"), new PdfOptions());

    objImage.save(); // Failes with NPE
    objImage.save(new FileOutputStream(TEST_RESOURCE_PATH.concat("testDWGtoDWG.dwg")));    // Failes with NPE.
}

@jthulin,
Hello. Please, attach your test DWG (e.g. inside zip achive), we need it to reproduce the issue on our side.

testDwg.zip (86.5 KB)

Here it is :slight_smile:

By the way we are using aspose cad version 20.4

@jthulin,
Unfortunately, we don’t have support for this feature now.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): CADJAVA-10995

You can obtain Paid Support services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Which future do you mean?

  • Convert to PDF with updated custom properties
  • Update properties and save it as DWG?

@jthulin,
we will look at both problems with applying changes of custom properties correctly (for now we don’t have such relation between them and corresponding entities in the drawing), and saving it to PDF. And we will investigate the issue about saving back to DWG, we have limited support for saving to DWG and working on it constantly.

Thanks for the swipt replies Oleksii. Do you see that we have any other option with the code as is? Thinking for example pre-converting it to anther format with better support currently? I´ve read in some threads that DXF support for editing capabilities. Could that be a way forward? Converting DWG to DXF. Editing the DXF. Converting to PDF.

Thanks in advance!

@nilspersson,
I was thinking about this yesterday, give us some time to check whether it is possible to achieve something like this and I will come back with results.

Thanks Oleksii!

@nilspersson, @jthulin,

We made brief investigation about this problem.

The relation between custom field value (“ORIGINAL_PREPARED_BY_INITIALS”) and corresponding entity in the drawing (MTEXT with related text) is complex and requires analysis of Objects collection for the drawing, there are 4 entities (2 dictionaries and 2 fields) “between” field and MTEXT, some of types are not available in public API (thus, making example of such processing is not available now), and probably, we need to parse additional information about fields for DWG format. After corresponding entity in the drawing is found we need to make changes in it, for some types (e.g. MTEXT) changing of text value may require changing of other properties like size, attachment points, etc. (but at the first look/testing this may be not required, will see later).

We will work with the step-by-step improvements for this case.

@oleksii.gorokhovatskyi

Thanks for this! Is there an implementation plan for the improvements?

@nilspersson,
we work now with reading and writing proper data for DWG format, and will try to make changes to allow public API processing for these properties. After that we will look how to implement export to PDF. Some of these features will probably appear in 23.4 though I can not guarantee this.

@oleksii.gorokhovatskyi When is 23.4 planned to be released? And is there a planned date already for 23.5? Thanks in advance!

@nilspersson,
.NET version is released usually at the second part of the month, so 23.4 version will be released close to the end of April, and 23.5 - in the second part of May. Java releases are a bit delayed compared to .NET, sometimes up to the month.

1 Like

Thanks for this!

1 Like

Hi Oleksii!

Just reaching out to hear if any of these requests made it into the 23.4 and/or the 23.5 releases? And if not if they are planned for any future releases?

Thanks in advance!
Regards,
Nils

@nilspersson,
Hello.
We are preparing Aspose.CAD for Java 23.3 to be released at the beginning of the next week, I’m going to test the progress and new possibilities for this feature using it and come back to you with more information.

1 Like

@nilspersson,
Hi.
So, here is a workaround that is available with the latest Aspose.CAD for Java 23.3 release to export DXF to PDF with changing of custom properties:

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

HashMap<String, String> custom = cadImage.getHeader().getCustomProperties();

String customName = "ORIGINAL_PREPARED_BY_INITIALS";
String oldValue = custom.get(customName);
String newValue = "JT";

for (CadBaseObject bo: cadImage.getObjects())
{
	if (bo.getTypeName() != CadObjectTypeName.FIELD)
	{
		continue;
	}

	CadField field = (CadField)bo;

	if (field.getFieldCodeString().contains(customName))
	{
		CadField parentField = (CadField)FindObject(cadImage, field.getSoftOwner());
		CadDictionary parentDict = (CadDictionary)FindObject(cadImage, parentField.getSoftOwner());
		CadDictionary parentDict2 = (CadDictionary)FindObject(cadImage, parentDict.getSoftOwner());

		CadBaseEntity relatedEntity = FindEntity(cadImage.getBlockEntities(), cadImage.getEntities(), parentDict2.getSoftOwner());

		if (relatedEntity.getTypeName() == CadEntityTypeName.MTEXT)
		{
			CadMText mtext = (CadMText)relatedEntity;
			mtext.setText(mtext.getText().replace(oldValue, newValue));
		}
	}
}

custom.put(customName, oldValue);

CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
rasterizationOptions.setPageWidth(1550);
rasterizationOptions.setPageHeight(1550);

// Create an instance of PdfOptions
final PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(rasterizationOptions);
cadImage.save(out, pdfOptions);



private static CadBaseObject FindObject(CadImage cadImage, String handle)
{
	for (CadBaseObject bo : cadImage.getObjects())
	{
		if (bo.getObjectHandle().equals(handle))
		{
			return bo;
		}
	}

	return null;
}

private static CadBaseEntity FindEntity(CadBlockDictionary blockDictionary, CadBaseEntity[] entities, String handle)
{
	for (CadBaseEntity be : entities)
	{
		if (be.getObjectHandle().equals(handle))
		{
			return be;
		}

		// search for entities inside inserts
		if (be.getTypeName() == CadEntityTypeName.INSERT)
		{
			CadInsertObject insert = (CadInsertObject)be;
			CadBlockEntity block = blockDictionary.get_Item(insert.getName());

			CadBaseEntity found = FindEntity(blockDictionary, block.getEntities(), handle);

			if (found != null)
			{
				return found;
			}
		}
	}

	return null;
}

This example does not work for DWG now (only for DXF -> PDF), we expect to deliever it in next 23.6 version this month (for Aspose.CAD for .NET) and a month-two later for Aspose.CAD for Java (and will do our best to deliever it faster). Saving DWG to DWG is also planned for 23.6. Please, note that I can not guarantee these terms though.

Other planned work includes more convenient internal processing of these items (but there is some uncertainty how e.g. AutoCAD stores and updates them), as well as we have issues with export of DWG to DXF for this case, this task is also planned for investigation.

Thanks Oleksii! Fantastic news! We will evaluate the java version then after summer. Thanks for a great job!

Regards,
Nils

1 Like

Hi Oleksii! Hope all is fine!

Just reaching out to hear if the JAVA based version is now supporting the DWG to PDF functionality discussed above?

Thanks in advance!

Regards.
Nils