Name 'CadDrawTypeMode' is not defined error when converting from DWG to PDF using python

I was trying to convert some dwg files to pdf using the basic convertion code using this code:

import os
import aspose.cad as cad

  # Iterate through all files in the directory and its subdirectories
  for foldername, subfolders, filenames in os.walk(root_directory):
      for filename in filenames:
          if filename.endswith(".dwg"):
              # Construct the full path to the DWG file
              dwg_path = os.path.join(foldername, filename)
  
              # Construct the output PDF file path
              pdf_output_path = os.path.join(foldername, filename[:-4] + ".pdf")
  
              # Check if a PDF file with the same name already exists
              if not os.path.exists(pdf_output_path):
                  # Load an existing DWG file
                  image = cad.Image.load(dwg_path)
  
                  # Initialize and specify CAD options
                  rasterizationOptions = cad.imageoptions.CadRasterizationOptions()
                  rasterizationOptions.page_width = 1200.0
                  rasterizationOptions.page_height = 1200.0
                  rasterizationOptions.DrawType = cad.CadDrawTypeMode.UseObjectColor
  
                  # Specify PDF Options
                  pdfOptions = cad.imageoptions.PdfOptions()
                  pdfOptions.vector_rasterization_options = rasterizationOptions
  
  
                  # Save as PDF
                  image.save(pdf_output_path, pdfOptions)
  
                  print(f"Conversion complete for: {dwg_path}. Output saved to: {pdf_output_path}")
              else:
                  print(f"PDF file already exists for: {dwg_path}. Skipping conversion.")

This however returns the error: module ‘aspose.cad’ has no attribute ‘CadDrawTypeMode’
Note that when removing this line of code, it seems to work fine.

@xyzzzz,
Hi.
Could you please try this line instead:

rasterizationOptions.draw_type = cad.fileformats.cad.CadDrawTypeMode.USE_OBJECT_COLOR

This seems to work…Thank you so much.

1 Like

@xyzzzz,
we were happy to help :slight_smile: