Pushpak21 commited on
Commit
84f8cf1
·
verified ·
1 Parent(s): 94220d7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -57
app.py CHANGED
@@ -1,82 +1,107 @@
 
1
  from fastapi import FastAPI, UploadFile, File
2
- from fastapi.responses import FileResponse, JSONResponse
3
- from fastapi.staticfiles import StaticFiles
4
- import os
5
 
6
  app = FastAPI()
7
 
8
- app.mount("/", StaticFiles(directory="static", html=True), name="static")
 
 
 
 
 
 
9
 
10
- @app.post("/parse")
11
- async def parse_file(file: UploadFile = File(...)):
12
- content = await file.read()
13
- return JSONResponse(content=parse_dcm_assembly(content))
14
-
15
-
16
- def parse_dcm_assembly(file_bytes):
17
  lines = file_bytes.decode("utf-8").splitlines()
18
- blocks, current = [], []
 
 
19
 
20
  for line in lines:
21
  if line.startswith("CLASS"):
22
- if current:
23
- blocks.append(current)
24
- current = []
25
- current.append(line.strip())
26
- if current: blocks.append(current)
 
27
 
28
- parts, geometry, constraints = [], {}, []
29
- plane_map, tag_to_plane, label_map = {}, {}, {}
 
 
 
 
 
30
 
31
  for block in blocks:
32
- head = block[0]
33
- if "CLASS SET " in head:
34
- part, members = {}, []
 
35
  for line in block:
36
- if "DCM3_TEXT_LABEL" in line:
37
  part['name'] = line.split("DCM3_TEXT_LABEL", 1)[1].strip()
38
- elif "FMEMBER TAGS" in line:
39
- members = list(map(int, line.split()[2:]))
40
  if 'name' in part:
41
- part['member_tags'] = members
42
  parts.append(part)
43
 
44
- elif "CLASS PLANE " in head:
45
- pid = int(head.split()[-1])
46
- base, norm = None, None
 
47
  for line in block:
48
- if "FBASE_POINT" in line:
49
- base = tuple(map(float, line.split()[1:]))
50
- elif "FNORMAL" in line:
51
- norm = tuple(map(float, line.split()[1:]))
52
- if base and norm:
53
- plane_map[pid] = {'base_point': base, 'normal': norm}
54
 
55
- elif "CLASS WORK_PLANE " in head:
56
- wid = int(head.split()[-1])
57
- tag, label = None, None
 
58
  for line in block:
59
- if "FMY_PLANE TAG" in line:
60
- tag = int(line.split()[-1])
61
- elif "DCM3_TEXT_LABEL" in line:
62
  label = line.split("DCM3_TEXT_LABEL", 1)[1].strip()
63
- if tag:
64
- tag_to_plane[wid] = tag
65
- label_map[wid] = label
66
 
67
- elif "CLASS CONSTRAINT " in head:
68
- con = {}
69
  for line in block:
70
- if "FEND_1 TAG" in line:
71
- con['from'] = int(line.split()[-1])
72
- elif "FEND_2 TAG" in line:
73
- con['to'] = int(line.split()[-1])
74
- elif "DCM3_TEXT_LABEL" in line:
75
- con['label'] = line.split("DCM3_TEXT_LABEL", 1)[1].strip()
76
- if con: constraints.append(con)
 
 
 
 
 
 
 
 
 
 
77
 
78
- for wid, pid in tag_to_plane.items():
79
- if pid in plane_map:
80
- geometry[wid] = {**plane_map[pid], 'label': label_map.get(wid)}
 
 
81
 
82
- return { "parts": parts, "geometry": geometry, "constraints": constraints }
 
 
1
+ # === app.py (FastAPI backend) ===
2
  from fastapi import FastAPI, UploadFile, File
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+ from fastapi.responses import JSONResponse
5
+ import uvicorn
6
 
7
  app = FastAPI()
8
 
9
+ app.add_middleware(
10
+ CORSMiddleware,
11
+ allow_origins=["*"],
12
+ allow_credentials=True,
13
+ allow_methods=["*"],
14
+ allow_headers=["*"],
15
+ )
16
 
17
+ def parse_dcm_assembly_from_bytes(file_bytes):
 
 
 
 
 
 
18
  lines = file_bytes.decode("utf-8").splitlines()
19
+
20
+ blocks = []
21
+ current_block = []
22
 
23
  for line in lines:
24
  if line.startswith("CLASS"):
25
+ if current_block:
26
+ blocks.append(current_block)
27
+ current_block = []
28
+ current_block.append(line.strip())
29
+ if current_block:
30
+ blocks.append(current_block)
31
 
32
+ parts = []
33
+ geometry = {}
34
+ constraints = []
35
+
36
+ plane_map = {}
37
+ tag_to_plane = {}
38
+ label_map = {}
39
 
40
  for block in blocks:
41
+ header = block[0]
42
+ if header.startswith("CLASS SET "):
43
+ part = {}
44
+ member_tags = []
45
  for line in block:
46
+ if line.startswith("DCM3_TEXT_LABEL"):
47
  part['name'] = line.split("DCM3_TEXT_LABEL", 1)[1].strip()
48
+ elif line.startswith("FMEMBER TAGS"):
49
+ member_tags = list(map(int, line.split()[2:]))
50
  if 'name' in part:
51
+ part['member_tags'] = member_tags
52
  parts.append(part)
53
 
54
+ elif header.startswith("CLASS PLANE "):
55
+ plane_id = int(header.split()[-1])
56
+ base_point = None
57
+ normal = None
58
  for line in block:
59
+ if line.startswith("FBASE_POINT"):
60
+ base_point = tuple(map(float, line.split()[1:]))
61
+ elif line.startswith("FNORMAL"):
62
+ normal = tuple(map(float, line.split()[1:]))
63
+ if base_point and normal:
64
+ plane_map[plane_id] = {'base_point': base_point, 'normal': normal}
65
 
66
+ elif header.startswith("CLASS WORK_PLANE "):
67
+ work_plane_id = int(header.split()[-1])
68
+ plane_tag = None
69
+ label = None
70
  for line in block:
71
+ if line.startswith("FMY_PLANE TAG"):
72
+ plane_tag = int(line.split()[-1])
73
+ elif line.startswith("DCM3_TEXT_LABEL"):
74
  label = line.split("DCM3_TEXT_LABEL", 1)[1].strip()
75
+ if plane_tag:
76
+ tag_to_plane[work_plane_id] = plane_tag
77
+ label_map[work_plane_id] = label
78
 
79
+ elif header.startswith("CLASS CONSTRAINT "):
80
+ constraint = {}
81
  for line in block:
82
+ if line.startswith("FEND_1 TAG"):
83
+ constraint['from'] = int(line.split()[-1])
84
+ elif line.startswith("FEND_2 TAG"):
85
+ constraint['to'] = int(line.split()[-1])
86
+ elif line.startswith("DCM3_TEXT_LABEL"):
87
+ constraint['label'] = line.split("DCM3_TEXT_LABEL", 1)[1].strip()
88
+ if constraint:
89
+ constraints.append(constraint)
90
+
91
+ for wp_id, plane_id in tag_to_plane.items():
92
+ if plane_id in plane_map:
93
+ geometry[wp_id] = {
94
+ **plane_map[plane_id],
95
+ 'label': label_map.get(wp_id)
96
+ }
97
+
98
+ return {"parts": parts, "geometry": geometry, "constraints": constraints}
99
 
100
+ @app.post("/parse")
101
+ async def parse_file(file: UploadFile = File(...)):
102
+ content = await file.read()
103
+ parsed_data = parse_dcm_assembly_from_bytes(content)
104
+ return JSONResponse(content=parsed_data)
105
 
106
+ if __name__ == "__main__":
107
+ uvicorn.run(app, host="0.0.0.0", port=7860)