Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,110 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from fastapi.responses import JSONResponse, FileResponse
|
| 4 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
def serve_frontend():
|
| 17 |
-
return FileResponse("index.html")
|
| 18 |
-
|
| 19 |
-
@app.post("/parse")
|
| 20 |
-
async def parse_file(file: UploadFile = File(...)):
|
| 21 |
-
content = await file.read()
|
| 22 |
-
return JSONResponse(content=parse_dcm_assembly(content))
|
| 23 |
-
|
| 24 |
-
def parse_dcm_assembly(file_bytes):
|
| 25 |
-
lines = file_bytes.decode("utf-8").splitlines()
|
| 26 |
-
blocks, current = [], []
|
| 27 |
|
| 28 |
for line in lines:
|
| 29 |
-
|
|
|
|
| 30 |
if current:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
part, members = {}, []
|
| 43 |
-
for line in block:
|
| 44 |
-
if "DCM3_TEXT_LABEL" in line:
|
| 45 |
-
part['name'] = line.split("DCM3_TEXT_LABEL", 1)[1].strip()
|
| 46 |
-
elif "FMEMBER TAGS" in line:
|
| 47 |
-
members = list(map(int, line.split()[2:]))
|
| 48 |
-
if 'name' in part:
|
| 49 |
-
part['member_tags'] = members
|
| 50 |
-
parts.append(part)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
base, norm = None, None
|
| 55 |
-
for line in block:
|
| 56 |
-
if "FBASE_POINT" in line:
|
| 57 |
-
base = tuple(map(float, line.split()[1:]))
|
| 58 |
-
elif "FNORMAL" in line:
|
| 59 |
-
norm = tuple(map(float, line.split()[1:]))
|
| 60 |
-
if base and norm:
|
| 61 |
-
plane_map[pid] = {'base_point': base, 'normal': norm}
|
| 62 |
|
| 63 |
-
|
| 64 |
-
wid = int(head.split()[-1])
|
| 65 |
-
tag, label = None, None
|
| 66 |
-
for line in block:
|
| 67 |
-
if "FMY_PLANE TAG" in line:
|
| 68 |
-
tag = int(line.split()[-1])
|
| 69 |
-
elif "DCM3_TEXT_LABEL" in line:
|
| 70 |
-
label = line.split("DCM3_TEXT_LABEL", 1)[1].strip()
|
| 71 |
-
if tag:
|
| 72 |
-
tag_to_plane[wid] = tag
|
| 73 |
-
label_map[wid] = label
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
con['from'] = int(line.split()[-1])
|
| 80 |
-
elif "FEND_2 TAG" in line:
|
| 81 |
-
con['to'] = int(line.split()[-1])
|
| 82 |
-
elif "DCM3_TEXT_LABEL" in line:
|
| 83 |
-
con['label'] = line.split("DCM3_TEXT_LABEL", 1)[1].strip()
|
| 84 |
-
if con: constraints.append(con)
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
| 95 |
+
parsed_files = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
+
if file1:
|
| 98 |
+
parsed_files.append(("red", "blue", parse_dcm_assembly(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 |
+
)
|