DWG to PNG - please provide sample of cadRasterizationOptions settings for Max quality

I have managed to get a very poor quality image exported from a professional ACAD .DWG. It is not fit for purpose. The drawing (i want to print zoom extents) exports perfectly fine in Autodesk trueview to a monchrome PNG , size A1 paper and with the settings outlined below. I wish to implement these settings in aspose python CAD. I find that none of cadRasterizationOptions work for me, and there is no examples of typical settings (and they are poorly documented).
The most basic code from github does work. I have commented out some settings that do not work. Please supply code that replicates the settings from trueview shown here.

my code:

import aspose.cad as cad
from aspose.cad import License
from aspose.cad.imageoptions import PngOptions

# This script converts DWG files to PNG images using Aspose.CAD for Python via .NET

lic = License()
lic.set_license(r"D:\z_Software_Support\pythonscripts\Aspose.CAD.PythonVia.NET.lic")
try:
** image = cad.Image.load(r"D:\z_private_pid_dataset\test\01P01D0030.dwg")**
** cadRasterizationOptions = cad.imageoptions.CadRasterizationOptions()**


** #cadRasterizationOptions.page_height = 594.0 -doesnt work**
** #cadRasterizationOptions.page_width = 841.0 -doesnt work**
** #cadRasterizationOptions.AutomaticLayoutsScaling = True -doesnt work**
** #cadRasterizationOptions.centerDrawing = True -doesnt work**
** #cadRasterizationOptions.ExportAllLayout = True -doesnt work**
** #cadRasterizationOptions.quality = 100.0 -doesnt work**
** #cadRasterizationOptions.layout = “Model” -doesnt work**
** cadRasterizationOptions.zoom = 1.0 # not sure wghat this does but it works**
** #cadRasterizationOptions.layers = [“Model”] -doesnt work**
** # cadRasterizationOptions.background_color = Color.green -doesnt work**

** options = PngOptions()**
** options.vector_rasterization_options = cadRasterizationOptions**

** image.save(r"D:\z_private_pid_dataset\test\01P01D0030.png", options)**

except Exception as e:
** print(“error loading image:”, e)**
** raise e**

typical true settings that i want to replicate in the exported png
TypicalTrueviewSettings.png (23.6 KB)

@ctgarvey

To achieve maximum quality when converting DWG files to PNG using Aspose.CAD for Python, you can configure the CadRasterizationOptions with specific settings. Below is a sample code snippet that demonstrates how to set these options for optimal output quality:

import aspose.cad as cad
from aspose.cad import License
from aspose.cad.imageoptions import PngOptions

# Set the license
lic = License()
lic.set_license(r"D:\path\to\your\license\Aspose.CAD.PythonVia.NET.lic")

try:
    # Load the DWG file
    image = cad.Image.load(r"D:\path\to\your\file\example.dwg")

    # Create CadRasterizationOptions and set properties for maximum quality
    cadRasterizationOptions = cad.imageoptions.CadRasterizationOptions()
    cadRasterizationOptions.page_height = 1684  # A1 size in pixels (for 300 DPI)
    cadRasterizationOptions.page_width = 2384   # A1 size in pixels (for 300 DPI)
    cadRasterizationOptions.quality = 100.0       # Set quality to maximum
    cadRasterizationOptions.zoom = 1.0            # No zoom
    cadRasterizationOptions.centerDrawing = True   # Center the drawing on the page
    cadRasterizationOptions.export_all_layouts = True  # Export all layouts if needed

    # Set up PNG options
    options = PngOptions()
    options.vector_rasterization_options = cadRasterizationOptions

    # Save the image as PNG
    image.save(r"D:\path\to\your\output\example.png", options)

except Exception as e:
    print("Error loading image:", e)
    raise e

Key Settings Explained:

  • Page Height and Width: Set the dimensions according to the desired output size (e.g., A1 size at 300 DPI).
  • Quality: Set to 100 for the highest quality output.
  • Zoom: A value of 1.0 means no zoom; adjust if you want to scale the output.
  • Center Drawing: This centers the drawing on the page, which can be useful for presentations.
  • Export All Layouts: If you want to include all layouts from the DWG file, set this to True.

Make sure to adjust the paths and settings according to your specific requirements. This setup should help you achieve high-quality PNG exports similar to those generated by Autodesk TrueView.

1 Like

Thanks for the worlds fastest response! :grinning:

I have tried the code as shown above, but with greater DPI. I am using aspose-cad 25.3 I get the following failure trying to run this script. are there DPI limitatations? I will try with the exact numbers you have above but its not enough for the app.
highdpi_fail.png (18.9 KB)

one thing to add. I added .0 to height& width (so now cadRasterizationOptions.page_height = 1684.0)…
but it now fails on the quality setting. Is there a type i can cast to before setting this?

The failure is
cadRasterizationOptions.quality = 100.0
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: can’t build RasterizationQuality value from ‘float’

@ctgarvey,
we are sorry for the confusion in the example above, quality is not a numerical value but another object to set up, please look at this example:

cadRasterizationOptions.quality.arc = cad.imageoptions.RasterizationQualityValue.HIGH
cadRasterizationOptions.quality.text_thickness_normalization = True
1 Like

Thanks for the response. Where do i even find info on these classes? For example theer is a problem with the centerdrawing type also:

Untitled.png (21.8 KB)

I look here in the .NET ACAD documentation for clues (as there is no equivalent python documentation at all)…by alas i find nothing . Or the search does not work…
Untitled2.png (31.7 KB)

@ctgarvey,
Here is the documentation for the Aspose.CAD for Python. But you are right - there is no such property as “centerDrawing”, and as I guess, the next one “export_all_layouts” is missing too :slight_smile: The example above is produced by bot, so these are a sort of hallucinations :slight_smile: Don’t use both, the drawing will be centered automatically anyway.

1 Like

Tanks Oleksii, how about layers? Is this supported? for example:

cadRasterizationOptions.layers = “Layout1”

I find that layers does not work for me at all. I want to export the layout image/view/layer (not the model). here is screenshot from drawing.
Layout1.png (22.1 KB)

If i use Trueview to plot PDF, i note that there is no mention of layers. here is screenshot. How can i print the layout?

Many thanks
Cormac
LayoutName.png (21.1 KB)

I should add that currently, only the .dwg model page appears to be printed out. No other layout (with for example the drawing name and information block) is printed out. This is different to trueview plot whereby a ‘layout’ (it looks like all the layers are flattened together) is plotted

@ctgarvey,
could you pelase test how these options work:

cadRasterizationOptions.layouts = ["Layout1"]
cadRasterizationOptions.layers = ["Layer1"]
1 Like

Thanks Oleksii, the line
cadRasterizationOptions.layouts = [“Layout1”] …
solved the issue .
One final Q. Is
MyDWG Text = image.get_strings()
the best / easiest way to get all the text on the drawing? Or is there a better method that formats the text based on the object it was associated with? Ive noticed that some items that look like text (but may not be) are not exported.

Thanks again

I have seen some chatter on the board and some stuff on the .NET API around the AnnotationEntity class. But the python object appears to only have an annotation_service() method. Could i be using this to get entities that have a text property? How does it fit in to the image instance? -Could you perhaps provide example again?

Many thanks
Cormac

@ctgarvey,

The answer about GetStrings is drawing-dependent. This method provides quick way to get strings from drawings. But if your drawing contains a lot of nested blocks and the text is in the deepest one, it could be missed, and this is not a single reason. This method also does not map strings with related objects. Additionaly, text could be stored in different entities like TEXT, MTEXT, ATTRIB. You can iterate over cadImage.Entities collection, analyze each entity and grab text in a way suitable for the particular drawing. Please note that to get full access to the Entities collection you need to apply at least free evaluation license.

No, AnnotationEntity has no relation to this, it is another engine to create annotations. If you need the answer how to get particular text please attach the test drawing so we can refer to it and create example.

1 Like

Thanks Oleksii,
I will send you the DWG but need to strip out some confidential text first. On a related note, i cannot find this entity, in Python. In the script , i set the license…

import aspose.cad as cad
from aspose.cad import License
from aspose.cad.imageoptions import PngOptions, ImageOptionsBase
import pythonnet # needed for interop with .NET libraries
lic = License()
lic.set_license(r"D:\z_Software_Support\pythonscripts\Aspose.CAD.PythonVia.NET.lic")
try:
image = cad.Image.load(r"D:\z_private_pid_dataset\DWGbatch1\test\P21-100.dwg")
…etc…

Check if the image object has an Entities collection

if hasattr(image, "Entities"):
    # Iterate over image.Entities collection, analyze each TEXT, MTEXT, ATTRIB entity and get text
    for entity in image.Entities:
        iterate_cad_nodes(entity)

This code doesnt work. How do i access the entities collection via Python?

Thanks again
C

@ctgarvey,
please test if this is helpful:

from aspose.pycore import cast
import aspose.cad as cad

image = cad.Image.load(file_name)

with cast(cad.fileformats.cad.CadImage, image) as cad_image:
       for entity in cad_image.entities:
              if entity.type_name == cad.fileformats.cad.cadconsts.CadEntityTypeName.TEXT:
                     text = cast(cad.fileformats.cad.cadobjects.CadText,entity)
                     print(text.default_value)
              if entity.type_name == cad.fileformats.cad.cadconsts.CadEntityTypeName.MTEXT:
                     mtext = cast(cad.fileformats.cad.cadobjects.CadMText,entity)
                     print(mtext.text)

1 Like

Hi Oleksii,
this worked well thank you so much. It exported all the text from the ‘model’ layout.
Is there any way to change it to export the text from the ‘Layout1’ layout instead or as well?

The image object doesnt have a layouts property (unlike cadRasterizationOptions.layouts)
Thanks again
C

@ctgarvey,
Hello,
please find if these ideas are useful:

with cast(cad.fileformats.cad.CadImage, image) as cad_image:
    objects = list(cad_image.objects)
    layouts_handles = {}
    for object in objects:
        if object.type_name == cad.fileformats.cad.cadconsts.CadObjectTypeName.LAYOUT:
            layout = cast(cad.fileformats.cad.cadobjects.CadLayout, object)
            layouts_handles[layout.layout_name] = layout.object_handle

    print(layouts_handles)

    blocks_tables = cast(cad.fileformats.cad.CadBlockRecordList, cad_image.blocks_tables)
    block_entities = cast(cad.fileformats.cad.CadBlockDictionary, cad_image.block_entities)
    block_keys_list = list(block_entities.keys)
    block_values_list = list(block_entities.values)

    for layout_name in layouts_handles:
        for block in blocks_tables:
            block_table = cast(cad.fileformats.cad.cadtables.CadBlockTableObject, block)
            if block_table.hard_pointer_to_layout == layouts_handles[layout_name]:
                if block_table.block_name in block_entities.keys:
                    position = block_keys_list.index(block_table.block_name)
                    block_entity = block_values_list[position]

                    print(f"Layout {layout_name} has such entities: ")

                    for block_entity in block_entity.entities:
                        print(block_entity.type_name)
1 Like

Hi Oleksii, i just get a string of numbers and the output : Layout Layout1 has such entities:
Layout Layout1 has such entities:
47
47
49
41
…etc
Is that what is supposed to happen? Am i then supposed to go through each object for its text property?
If i capture to a file, i get a dictionary of:

{‘Model’: ‘22’, ‘Layout1’: ‘1E’}[‘Model’][<CadEntityTypeName.INSERT: 15>][<CadEntityTypeName.INSERT: 15>][<CadEntityTypeName.INSERT: 15>]

Many thanks
C

@ctgarvey,
It looks like block_entity.type_name is printed as number in your case, 41 is text entity, 47 is viewport and so on. Yes, you can analyze these types and get proper information from text entities as in the previous example.