Search code examples
pythonmathprintinggeometry

User input based circle drawing, then printing to paper with fixed sizes


How to create a program, that places circles in a coordinate system based on user inputs (x,y, and circle diameter (mm)), and after that, the user can print the graph to a paper, which then can be used for positioning and drilling.

Now, the work flow process is this: 1-Create an Excel table with fixed mm cell steps. 2-Draw fix position and diameter circles into that table. 3-Print the excel table with the circles to a A4 paper. 4-Use the circle positions and the graph for work.

My task is to create a program, which places the circles automatically based on user input parameters, so that we can eliminate the human error factor.

My first idea was that to create a python Tkinter GUI, which does the work, then print a generated pdf from it. The problem was that Tkinter doesn't have mm/cm units, and with pixels I can't guarantee, that the printed graph will have the standard metric sizes.

Any ideas?


Solution

  • i think something like this

    import matplotlib.pyplot as plt
    from reportlab.pdfgen import canvas
    from matplotlib.backends.backend_pdf import PdfPages
    
    def draw_circles_and_save_pdf(circle_data, filename="output.pdf"):
        # Create a figure with DPI that reflects the print size in millimeters
        fig, ax = plt.subplots(figsize=(210/25.4, 297/25.4), dpi=100) # A4 size in inches: 210mm x 297mm
        ax.set_xlim(0, 210) # Assuming the coordinate system is in millimeters
        ax.set_ylim(0, 297)
        
        for x, y, diameter in circle_data:
            circle = plt.Circle((x, y), diameter/2, fill=False)
            ax.add_artist(circle)
        
        ax.set_aspect('equal')
        plt.grid(True, which='both', linestyle='--', linewidth=0.5)
        plt.axis('off')
    
        # Save the plot to a PDF
        pp = PdfPages(filename)
        pp.savefig(fig)
        pp.close()
    
    # Example usage
    circle_data = [
        (50, 50, 20), # Each tuple is (x, y, diameter)
        (100, 150, 30),
        # Add more circles as needed
    ]
    draw_circles_and_save_pdf(circle_data)