ezdxf python - 如何在布局的绘图设置中设置绘图透明度配置?

问题描述 投票:0回答:1

我有一个代码可以绘制使用 ezdxf 创建的布局,但我在设置绘图透明度选项时遇到问题,因为我有一个带有图像的外部参考,我希望其透明度为 40%。

这是我用于绘制整个 dxf 文件的代码,但我不知道如何在绘图选项中将透明度选项设置为 True,我找不到此参考的文档。

如果当我在 ezdxf 中创建布局时,我可以为每个布局设置所有这些绘图选项,那就太好了。

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)

我非常感谢对此的任何帮助。

切洛。

    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()
   
python win32com autocad ezdxf
1个回答
0
投票

对于任何从事这方面工作的人来说,你都可以这样做

def create_and_use_page_setup(doc, setup_name="PyPiper", ctb_file_path=None):
    # 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
    new_setup = doc.PlotConfigurations.Add(setup_name)

    # Configure the new setup
    new_setup.ConfigName = "DWG To PDF.pc3"
    new_setup.CanonicalMediaName = "ISO_full_bleed_A1_(841.00_x_594.00_MM)"
    new_setup.PlotTransparency = True
    new_setup.PlotWithPlotStyles = True
    new_setup.PlotViewportsFirst = False  # This is "Plot paperspace last"
    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 = ctb_file_path
    
    print(f"Created new page setup '{setup_name}'")
    return new_setup



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)

    # Set plot transparency using PostCommand
    doc.PostCommand("-PLOTTRANSPARENCYOVERRIDE 1\n")  # 1 for on, 0 for off
    
    # Create or get the page setup, including the CTB file
    page_setup = create_and_use_page_setup(doc, ctb_file_path=ctb_file_path)

    # Loop through all layouts
    for i in range(doc.Layouts.Count - 1):
        print(i)
        time.sleep(4)
        layout = doc.Layouts.Item(i)  # 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_full_bleed_A1_(841.00_x_594.00_MM)" #debe coincidir exctamente el nombre
        
        # Apply the page setup to the layout
        layout.CopyFrom(page_setup)

        # 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()

    # Quit AutoCAD
    acad.Quit()
© www.soinside.com 2019 - 2024. All rights reserved.