Pushpak21 commited on
Commit
fe75c49
·
verified ·
1 Parent(s): 44344df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -79
app.py CHANGED
@@ -1,94 +1,73 @@
1
  import streamlit as st
2
- import json
3
  import os
 
 
 
 
4
 
5
- # ---- Optional: define parser directly here or import from module ----
6
  def parse_dcm_assembly(uploaded_file):
7
- lines = uploaded_file.read().decode("utf-8").splitlines()
 
8
  geometry = {}
9
- constraints = []
10
  parts = []
11
- set_geom_map = {}
12
- current = {}
13
- label = None
 
 
 
 
 
14
 
15
  for line in lines:
16
  line = line.strip()
17
- if line.startswith("CLASS "):
18
- if current:
19
- if current["type"] == "SET":
20
- parts.append({
21
- "name": current.get("label", ""),
22
- "member_tags": current.get("member_tags", [])
23
- })
24
- elif current["type"] == "PLANE":
25
- geometry[current["id"]] = {
26
- "base_point": current.get("base_point"),
27
- "normal": current.get("normal"),
28
- "label": current.get("label", "")
29
- }
30
- elif current["type"] == "WORK_PLANE":
31
- tag = current.get("tag")
32
- if tag in geometry:
33
- geometry[tag]["label"] = current.get("label", "")
34
- elif current["type"] == "CONSTRAINT":
35
- constraints.append({
36
- "from": current.get("end_1"),
37
- "to": current.get("end_2"),
38
- "label": current.get("label", "")
39
- })
40
- parts_of_line = line.split()
41
- current = {
42
- "type": parts_of_line[1],
43
- "id": int(parts_of_line[2])
44
- }
45
  elif line.startswith("FBASE_POINT"):
46
- current["base_point"] = tuple(map(float, line.split()[1:]))
47
  elif line.startswith("FNORMAL"):
48
- current["normal"] = tuple(map(float, line.split()[1:]))
49
- elif line.startswith("FMY_PLANE TAG"):
50
- current["tag"] = int(line.split()[-1])
51
- elif line.startswith("DCM3_TEXT_LABEL"):
52
- current["label"] = line.split("DCM3_TEXT_LABEL", 1)[-1].strip()
53
  elif line.startswith("FMEMBER TAGS"):
54
- current["member_tags"] = list(map(int, line.split()[2:]))
 
 
 
 
 
 
 
 
 
55
  elif line.startswith("FEND_1 TAG"):
56
- current["end_1"] = int(line.split()[-1])
57
  elif line.startswith("FEND_2 TAG"):
58
- current["end_2"] = int(line.split()[-1])
59
- if current:
60
- if current["type"] == "SET":
61
- parts.append({
62
- "name": current.get("label", ""),
63
- "member_tags": current.get("member_tags", [])
64
- })
65
- elif current["type"] == "PLANE":
66
- geometry[current["id"]] = {
67
- "base_point": current.get("base_point"),
68
- "normal": current.get("normal"),
69
- "label": current.get("label", "")
70
- }
71
- elif current["type"] == "WORK_PLANE":
72
- tag = current.get("tag")
73
- if tag in geometry:
74
- geometry[tag]["label"] = current.get("label", "")
75
- elif current["type"] == "CONSTRAINT":
76
- constraints.append({
77
- "from": current.get("end_1"),
78
- "to": current.get("end_2"),
79
- "label": current.get("label", "")
80
- })
81
 
82
  return {
83
  "geometry": geometry,
84
- "constraints": constraints,
85
- "parts": parts
86
  }
87
 
88
- # ---- Streamlit UI and file handling ----
89
- st.set_page_config(layout="wide")
90
- st.title("DCM Assembly Visualizer")
91
-
92
  file1 = st.file_uploader("Upload File A", type=["txt"])
93
  file2 = st.file_uploader("Upload File B", type=["txt"])
94
 
@@ -99,12 +78,14 @@ if file1:
99
  if file2:
100
  parsed_files.append(("purple", "orange", parse_dcm_assembly(file2)))
101
 
 
102
  if parsed_files:
103
- os.makedirs("static", exist_ok=True)
104
- with open("static/parsed_data.json", "w") as f:
105
- json.dump({"files": parsed_files}, f, indent=2)
 
106
 
107
- st.components.v1.html(
108
- '<iframe src="static/index.html" width="100%" height="800" style="border:none;"></iframe>',
109
- height=800,
110
- )
 
1
  import streamlit as st
 
2
  import os
3
+ import json
4
+
5
+ st.set_page_config(page_title="DCM Assembly Visualizer", layout="wide")
6
+ st.title("DCM Assembly Visualizer")
7
 
8
+ # === Parser Function ===
9
  def parse_dcm_assembly(uploaded_file):
10
+ lines = uploaded_file.read().decode().splitlines()
11
+
12
  geometry = {}
 
13
  parts = []
14
+ constraints = []
15
+ current_class = None
16
+ current_id = None
17
+ tag_to_label = {}
18
+
19
+ temp_geom = {}
20
+ temp_part = {}
21
+ temp_constraint = {}
22
 
23
  for line in lines:
24
  line = line.strip()
25
+ if line.startswith("CLASS"):
26
+ parts_line = line.split()
27
+ current_class = parts_line[1]
28
+ current_id = int(parts_line[2])
29
+ if current_class == "PLANE":
30
+ temp_geom = {"tag": current_id}
31
+ elif current_class == "WORK_PLANE":
32
+ temp_geom["tag"] = current_id
33
+ elif current_class == "SET":
34
+ temp_part = {"member_tags": []}
35
+ elif current_class == "CONSTRAINT":
36
+ temp_constraint = {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  elif line.startswith("FBASE_POINT"):
38
+ temp_geom["base_point"] = tuple(map(float, line.split()[1:]))
39
  elif line.startswith("FNORMAL"):
40
+ temp_geom["normal"] = tuple(map(float, line.split()[1:]))
41
+ elif line.startswith("DCM3_TEXT_LABEL") and current_class == "WORK_PLANE":
42
+ temp_geom["label"] = line.split(" ", 1)[1].strip()
 
 
43
  elif line.startswith("FMEMBER TAGS"):
44
+ tags = list(map(int, line.split()[2:]))
45
+ temp_part["member_tags"].extend(tags)
46
+ elif line.startswith("DCM3_TEXT_LABEL") and current_class == "SET":
47
+ temp_part["name"] = line.split(" ", 1)[1].strip()
48
+ elif line.startswith("FSET_GEOM TAG"):
49
+ temp_part["geom_tag"] = int(line.split()[-1])
50
+ elif line.startswith("FMY_PLANE TAG"):
51
+ plane_tag = int(line.split()[-1])
52
+ geometry[temp_geom["tag"]] = temp_geom
53
+ tag_to_label[plane_tag] = temp_geom["label"]
54
  elif line.startswith("FEND_1 TAG"):
55
+ temp_constraint["from"] = int(line.split()[-1])
56
  elif line.startswith("FEND_2 TAG"):
57
+ temp_constraint["to"] = int(line.split()[-1])
58
+ elif line.startswith("DCM3_TEXT_LABEL") and current_class == "CONSTRAINT":
59
+ temp_constraint["label"] = line.split(" ", 1)[1].strip()
60
+ constraints.append(temp_constraint)
61
+ elif line == "#" and current_class == "SET":
62
+ parts.append(temp_part)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  return {
65
  "geometry": geometry,
66
+ "parts": parts,
67
+ "constraints": constraints
68
  }
69
 
70
+ # === File Upload ===
 
 
 
71
  file1 = st.file_uploader("Upload File A", type=["txt"])
72
  file2 = st.file_uploader("Upload File B", type=["txt"])
73
 
 
78
  if file2:
79
  parsed_files.append(("purple", "orange", parse_dcm_assembly(file2)))
80
 
81
+ # === Draw Button + Iframe Viewer ===
82
  if parsed_files:
83
+ if st.button("🎯 Visualize"):
84
+ os.makedirs("static", exist_ok=True)
85
+ with open("static/parsed_data.json", "w") as f:
86
+ json.dump({"files": parsed_files}, f, indent=2)
87
 
88
+ st.components.v1.html(
89
+ '<iframe src="static/index.html" width="100%" height="800" style="border:none;"></iframe>',
90
+ height=800,
91
+ )