sikandarciv101 commited on
Commit
a41c513
·
verified ·
1 Parent(s): 8047a6a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from streamlit_drawable_canvas import st_canvas
3
+ import networkx as nx
4
+ import matplotlib.pyplot as plt
5
+ from PyNite import FEModel3D
6
+ import numpy as np
7
+
8
+ def draw_truss_app():
9
+ st.title("Truss Drawing & Analysis App")
10
+
11
+ # Create a drawing canvas
12
+ canvas_result = st_canvas(
13
+ fill_color="rgba(255, 165, 0, 0.3)",
14
+ stroke_width=2,
15
+ stroke_color="#000000",
16
+ background_color="#FFFFFF",
17
+ height=400,
18
+ width=600,
19
+ drawing_mode="freedraw",
20
+ key="canvas",
21
+ )
22
+
23
+ if st.button("Analyze Truss"):
24
+ if canvas_result.json_data is not None:
25
+ edges = process_canvas_data(canvas_result.json_data)
26
+ G = create_graph(edges)
27
+ plot_graph(G)
28
+ analyze_truss(G)
29
+ else:
30
+ st.error("Please draw a truss before analyzing!")
31
+
32
+ def process_canvas_data(json_data):
33
+ edges = []
34
+ for obj in json_data["objects"]:
35
+ if "path" in obj:
36
+ path = obj["path"]
37
+ for i in range(len(path) - 1):
38
+ x1, y1 = path[i]
39
+ x2, y2 = path[i + 1]
40
+ edges.append(((x1, y1), (x2, y2)))
41
+ return edges
42
+
43
+ def create_graph(edges):
44
+ G = nx.Graph()
45
+ for (x1, y1), (x2, y2) in edges:
46
+ G.add_edge((x1, y1), (x2, y2))
47
+ return G
48
+
49
+ def plot_graph(G):
50
+ plt.figure(figsize=(6, 4))
51
+ for edge in G.edges():
52
+ x_values, y_values = zip(*edge)
53
+ plt.plot(x_values, y_values, 'r-', linewidth=2)
54
+ plt.gca().invert_yaxis()
55
+ plt.show()
56
+ st.pyplot(plt)
57
+
58
+ def analyze_truss(G):
59
+ model = FEModel3D()
60
+ for node in G.nodes():
61
+ x, y = node
62
+ model.add_node(str(node), x/100, y/100, 0)
63
+ for edge in G.edges():
64
+ n1, n2 = edge
65
+ model.add_member(str(edge), str(n1), str(n2), "Steel", 0.01, 0.01)
66
+ model.analyze()
67
+
68
+ st.write("### Analysis Results:")
69
+ for node in model.Nodes:
70
+ st.write(f"Node {node}: DX = {model.Nodes[node].DX}, DY = {model.Nodes[node].DY}")
71
+
72
+ if __name__ == "__main__":
73
+ draw_truss_app()