We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Create template based DWG/DGN file via Aspose.Total Java

Dear Team,

I am looking for feature which allowes us to create DWG/DGN files from predefined template.
I see that this feature is available for docx file (https://products.aspose.com/words/java/create/template/docx/). I am wondring if this is possible for CAD files (DWG/DGN). If not possible now then is there a plan to implement this feature in future release of Aspose.Total (It could be part of Aspose.CAD).
This is quite populer feature for our engineering clients which we currently support with another paied library. Our plan is to replace that library with Aspose in near future.

Looking forward to hearing from you.

Thanks & Regards,

Hello. We need to understand your requirements better. What functionaly do you expect to see exactly? DWG templates are usually stored in DWT files, we have support for them. You can also load some DWG file as template and modify it. But at the moment we don’t have support to write DGN and have partial support for DWG writing.

Thanks for your reply.
We are looking for following feature.
I have attached template dwg file called: ‘template.dwg’.
This file contains some text tags which should be replaced with dynamic data. for example: {{COMPANY_NAME}}, {{PROJECT}}, {{TITAL}} etc.

We created another dwg called: ‘Generated-from-template.dwg’ using some other 3rd party library. This file is created using above template. I have attached images of those files here:
Generated-dwg-from-template.png (6.4 KB)
template.png (5.2 KB)

We want to know if this feature is supported by Aspose.Total? If yes, then could you please guide me with some sample examples?
Please let me know if you need more information.


From this point of view I guess you can do it. You can find proper text objects in the DWG file (something like this: https://docs.aspose.com/cad/net/search-text-in-dwg-autocad-file/) and replace them with other values.
I can try to create the example for this but I need your DWG template (you can remove all other content except of required tags or switch this branch to private for safety), because the example depends on how exactly you store the labels in file (e.g. MTEXT or TEXT, inside block or not, etc.).


Thanks for your quick response and sorry for late reply as I was on vacation.
I have uploaded sample template file and also expected result file after replacing text tags.
dwg template.7z (78.0 KB)

Could you please provide an example?


OK, give me couple of days, I will return with the results.

Hello. You can use this example to find and replace values:

    String in = basePath + filename;
    String out = basePath + "outpath.pdf";

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

    Map<String, String> newValues = new HashMap<String, String>();
    newValues.put("{{COMPANY_NAME}}", "Assai Software Services B.V");
    newValues.put("DISCIPLINE: {{DISCIPLINE}}", "DISCIPLINE: Electrical");
    newValues.put("UNIT NO. {{UNIT}}", "UNIT NO. British");
    newValues.put("PROJECT: {{PROJECT}}", "Demo");
    newValues.put("TITLE: {{TITLE}}", "Title");

    // iterate over all entities
    for (CadBaseEntity entity : cadImage.getEntities())
        // identify inserts
        if (CadEntityTypeName.getName(CadEntityTypeName.class, entity.getTypeName()).equals("INSERT"))
            CadInsertObject insert = (CadInsertObject)entity;

            // we need insert with "A3_POTRAIT_TITLE_BLOCK" name
            if (insert.getName().equals("A3_POTRAIT_TITLE_BLOCK"))
                CadBlockEntity block = cadImage.getBlockEntities().get_Item(insert.getName());

                // look inside items in block
                for (CadBaseEntity blockEntity: block.getEntities())
                    // all objects to replace values inside are TEXT entities
                    if (CadEntityTypeName.getName(CadEntityTypeName.class, blockEntity.getTypeName()).equals("TEXT"))
                        CadText text = (CadText)blockEntity;

                        // replacing the current value with new one
                        if (newValues.containsKey(text.getDefaultValue()))

    CadRasterizationOptions rasterizationOptions = new CadRasterizationOptions();
    rasterizationOptions.setLayouts(new String[] { "Model" });
    PdfOptions pdfOptions = new PdfOptions();

    cadImage.save(out, pdfOptions);

But unfortunately, this doesn’t work when saving to DWG. The example above saves to PDF. I created CADJAVA-10664 to investigate and implement saving to DWG.