hh / app.py
sikandarciv101's picture
Create app.py
a41c513 verified
import streamlit as st
from streamlit_drawable_canvas import st_canvas
import networkx as nx
import matplotlib.pyplot as plt
from PyNite import FEModel3D
import numpy as np
def draw_truss_app():
st.title("Truss Drawing & Analysis App")
# Create a drawing canvas
canvas_result = st_canvas(
fill_color="rgba(255, 165, 0, 0.3)",
stroke_width=2,
stroke_color="#000000",
background_color="#FFFFFF",
height=400,
width=600,
drawing_mode="freedraw",
key="canvas",
)
if st.button("Analyze Truss"):
if canvas_result.json_data is not None:
edges = process_canvas_data(canvas_result.json_data)
G = create_graph(edges)
plot_graph(G)
analyze_truss(G)
else:
st.error("Please draw a truss before analyzing!")
def process_canvas_data(json_data):
edges = []
for obj in json_data["objects"]:
if "path" in obj:
path = obj["path"]
for i in range(len(path) - 1):
x1, y1 = path[i]
x2, y2 = path[i + 1]
edges.append(((x1, y1), (x2, y2)))
return edges
def create_graph(edges):
G = nx.Graph()
for (x1, y1), (x2, y2) in edges:
G.add_edge((x1, y1), (x2, y2))
return G
def plot_graph(G):
plt.figure(figsize=(6, 4))
for edge in G.edges():
x_values, y_values = zip(*edge)
plt.plot(x_values, y_values, 'r-', linewidth=2)
plt.gca().invert_yaxis()
plt.show()
st.pyplot(plt)
def analyze_truss(G):
model = FEModel3D()
for node in G.nodes():
x, y = node
model.add_node(str(node), x/100, y/100, 0)
for edge in G.edges():
n1, n2 = edge
model.add_member(str(edge), str(n1), str(n2), "Steel", 0.01, 0.01)
model.analyze()
st.write("### Analysis Results:")
for node in model.Nodes:
st.write(f"Node {node}: DX = {model.Nodes[node].DX}, DY = {model.Nodes[node].DY}")
if __name__ == "__main__":
draw_truss_app()