PCB-Generator / app.py
syedwaqarumer's picture
Create app.py
ada5868 verified
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)