pythonwin32comautocadezdxf

ezdxf python - How can I set the plot transparency configuration in the plotsettings of a layout?


I have a code that plots a layout created with ezdxf, but I have trouble setting my plot transparency option, because I have a external reference with an image that I want to be 40% transparent.

This is the code I use for plotting the whole dxf file, but I dont know how to set the transparency option to True in the plot options, I cant find the docs for this references.

Also it would be very nice if when I create my layout in the ezdxf, I could set all this plotting options for each layout.

doc = ezdxf.readfile(dxf_path)
layout = doc.layouts.new(name='layout_name')
layout.applyPlotOptions(papersize="ISO_full_bleed_A1_(841.00_x_594.00_MM)", plotsytle table='style.ctb', plottransparency=0.6, etc)

I would very much appreciatte any help on this.

Chelo.

    def plot_layouts(dwg_path, ctb_file_path, output_folder):
    
    if not os.path.exists(output_folder):
        os.mkdir(output_folder)
        
    
   # Initialize AutoCAD
    acad = win32com.client.Dispatch("AutoCAD.Application")
    acad.Visible = False  # Set to False if you don't want to show AutoCAD

    # Open the DWG file
    doc = acad.Documents.Open(dwg_path)

    # Loop through all layouts
    for i in range(doc.Layouts.Count):
        print(i)
        time.sleep(5)
        layout = doc.Layouts.Item(i + 1)  # Indexing starts from 1 in COM

        # Set the layout as active
        doc.ActiveLayout = layout

        # Configure the plot settings
        doc.ActiveLayout.ConfigName = "DWG To PDF.pc3"  #se puede cambiar a cualquier pc3 configurado
        doc.ActiveLayout.StyleSheet = ctb_file_path
        # doc.ActiveLayout.CanonicalMediaName = "ISO_expand_A4_(210.00_x_297.00_MM)" #debe coincidir exctamente el nombre
        doc.ActiveLayout.CanonicalMediaName = "ISO_full_bleed_A1_(841.00_x_594.00_MM)" #debe coincidir exctamente el nombre


        # Set the output file path
        output_file_path = f"{output_folder}/{layout.Name}.pdf"

        # Plot the layout
        doc.Plot.PlotToFile(output_file_path)
        time.sleep(5)

    # Close the document without saving changes
    doc.Close(SaveChanges=True)

    # Quit AutoCAD
    acad.Quit()
   

Solution

  • it turns out is pretty simple, this is a way to setup the page to plot.

    def create_and_use_page_setup(self, doc, setup_name="NewPageSetUp", ctb_file_path=None):
        """
        Create or retrieve a page setup configuration for plotting.
        
        Args:
            doc (object): Active AutoCAD document object
            setup_name (str, optional): Name for the page setup. Defaults to "NewSetUp".
            ctb_file_path (str, optional): Path to CTB plot style file. Defaults to None.
        
        Returns:
            object: AutoCAD page setup configuration object
        """
        # Set plot transparency using PostCommand 
        doc.PostCommand("-PLOTTRANSPARENCYOVERRIDE 2\n")
        
        # Disable background plotting and PDF launch
        doc.PostCommand("_BACKGROUNDPLOT 0\n")
        
        # Check if the page setup already exists
        try:
            existing_setup = doc.PlotConfigurations.Item(setup_name)
            print(f"Page setup '{setup_name}' already exists. Using existing setup.")
            return existing_setup
        except:
            pass  # Setup doesn't exist, we'll create a new one
        
        # Create a new page setup with standard A1 PDF configuration
        new_setup = doc.PlotConfigurations.Add(setup_name)
        new_setup.ConfigName = "DWG To PDF.pc3"
        new_setup.CanonicalMediaName = "ISO_full_bleed_A1_(841.00_x_594.00_MM)"
        new_setup.PlotWithPlotStyles = True
        new_setup.PlotWithLineweights = True
        new_setup.PaperUnits = 1  # 1 for mm
        new_setup.PlotRotation = 2  # 2 for landscape
        
        # Set the CTB file if provided
        if ctb_file_path:
            new_setup.StyleSheet = os.path.basename(ctb_file_path)
        
        print(f"Created new page setup '{setup_name}'")
        return new_setup