Hafsa
Hafsa

Reputation: 29

AutoCAD win32com PDF Export - Custom Scale Issue

I'm trying to convert a DWG file to a PDF using win32com.client in Python, while applying a custom scale of 0.2 . However, the resulting PDF does not have the expected scale.

Here is the code I'm using:

import win32com.client
import time
import os

def dwg_to_pdf_win32(dwg_file_path, pdf_file_path):
    try:
        # Initialize AutoCAD
        autocad = win32com.client.Dispatch("AutoCAD.Application")
        autocad.Visible = False

        # Open the DWG file
        doc = autocad.Documents.Open(dwg_file_path)
        layout = doc.Layouts.Item("Model")

        layout.PlotType = 2  # Extents
        layout.CenterPlot = True
        layout.PlotRotation = 1
        layout.ConfigName = "Microsoft Print to PDF"

        layout.CanonicalMediaName = "A3"
        layout.UseStandardScale = False
        layout.PaperUnits = 1
        layout.SetCustomScale(1, 0.2)  # Scale: 1/200

        layout.PlotWithLineweights = False
        layout.PlotWithPlotStyles = True
        layout.StyleSheet = "style.ctb"

        # Create the PDF
        plot = doc.Plot
        plot.PlotToFile(pdf_file_path)

        # Wait for the PDF to be created
        timeout = 120
        for _ in range(timeout):
            if os.path.exists(pdf_file_path):
                print(f"PDF generated: {pdf_file_path}")
                break
            time.sleep(1)
        else:
            print("PDF file was not created.")
            return None

        # Close document
        doc.Close(False)
        print("Conversion successful!")

    except Exception as e:
        print(f"Error during conversion: {e}")

# Example usage
dwg_to_pdf_win32(
    "C:\\Users\\hp\\P8818-PLAN.dwg",
    "C:\\Users\\hp\\P8818-PLAN.pdf"
)

Any insights or corrections would be greatly appreciated. Thank you!

Upvotes: 2

Views: 29

Answers (1)

Javier Santos
Javier Santos

Reputation: 11

I don't know if I'm missing anything but the SetCustomScale(1, 0.2) doesn't look like a scale of 1:200.

I've tried to look for the official documentation but didn't find it. Maybe you can try SetCustomScale(1, 200) or SetCustomScale(1, 0.005)

Upvotes: 1

Related Questions