File size: 2,952 Bytes
ada5868
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import streamlit as st
import matplotlib.pyplot as plt
import matplotlib.patches as patches

# Function to generate a basic PCB design
def generate_pcb_design(board_width, board_height, components):
    fig, ax = plt.subplots(figsize=(8, 8))

    # Set the dimensions of the PCB board
    ax.set_xlim(0, board_width)
    ax.set_ylim(0, board_height)
    
    # Configure the plot to represent a PCB (green board with components)
    ax.set_facecolor("#2E8B57")  # PCB typical green color
    boundary_color = 'white'
    boundary_thickness = 2
    
    # Loop through components and place them on the PCB
    for component in components:
        component_type, x_pos, y_pos, width, height, label = component
        
        # Draw the component as a rectangle
        ax.add_patch(patches.Rectangle((x_pos, y_pos), width, height,
                                       edgecolor=boundary_color, facecolor='gray', lw=boundary_thickness))
        
        # Add label to the component
        ax.text(x_pos + width / 2, y_pos + height / 2, label, 
                ha='center', va='center', fontsize=10, color='white', weight='bold')
        
    # Draw traces (lines between components)
    for i in range(len(components) - 1):
        start_component = components[i]
        end_component = components[i + 1]
        start_x = start_component[1] + start_component[3] / 2  # Midpoint of width
        start_y = start_component[2] + start_component[4] / 2  # Midpoint of height
        end_x = end_component[1] + end_component[3] / 2
        end_y = end_component[2] + end_component[4] / 2
        
        # Draw a line (trace) between the two components
        ax.plot([start_x, end_x], [start_y, end_y], color='yellow', lw=2)

    ax.set_aspect('equal')
    ax.set_axis_off()  # Hide axes for cleaner appearance
    
    st.pyplot(fig)

# Streamlit app title
st.title("PCB Design Generator")

# Get input from the user for PCB dimensions
board_width = st.number_input("PCB Board Width (mm)", value=100)
board_height = st.number_input("PCB Board Height (mm)", value=80)

num_components = st.slider("Number of Components", 1, 10, 4)
components = []

# Input component details
for i in range(num_components):
    st.subheader(f"Component {i + 1} Details")
    component_type = st.selectbox(f"Component {i + 1} Type", ["Resistor", "Capacitor", "IC", "Diode", "Transistor"])
    x_pos = st.number_input(f"Component {i + 1} X Position (mm)", value=10)
    y_pos = st.number_input(f"Component {i + 1} Y Position (mm)", value=10)
    width = st.number_input(f"Component {i + 1} Width (mm)", value=10)
    height = st.number_input(f"Component {i + 1} Height (mm)", value=5)
    label = st.text_input(f"Component {i + 1} Label", value=f"{component_type} {i + 1}")
    components.append((component_type, x_pos, y_pos, width, height, label))

# Generate button
if st.button("Generate PCB Layout"):
    generate_pcb_design(board_width, board_height, components)