Spaces:
Sleeping
Sleeping
| # === app.py (FastAPI backend) === | |
| from fastapi import FastAPI, UploadFile, File | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.responses import JSONResponse | |
| import uvicorn | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| def parse_dcm_assembly_from_bytes(file_bytes): | |
| lines = file_bytes.decode("utf-8").splitlines() | |
| blocks = [] | |
| current_block = [] | |
| for line in lines: | |
| if line.startswith("CLASS"): | |
| if current_block: | |
| blocks.append(current_block) | |
| current_block = [] | |
| current_block.append(line.strip()) | |
| if current_block: | |
| blocks.append(current_block) | |
| parts = [] | |
| geometry = {} | |
| constraints = [] | |
| plane_map = {} | |
| tag_to_plane = {} | |
| label_map = {} | |
| for block in blocks: | |
| header = block[0] | |
| if header.startswith("CLASS SET "): | |
| part = {} | |
| member_tags = [] | |
| for line in block: | |
| if line.startswith("DCM3_TEXT_LABEL"): | |
| part['name'] = line.split("DCM3_TEXT_LABEL", 1)[1].strip() | |
| elif line.startswith("FMEMBER TAGS"): | |
| member_tags = list(map(int, line.split()[2:])) | |
| if 'name' in part: | |
| part['member_tags'] = member_tags | |
| parts.append(part) | |
| elif header.startswith("CLASS PLANE "): | |
| plane_id = int(header.split()[-1]) | |
| base_point = None | |
| normal = None | |
| for line in block: | |
| if line.startswith("FBASE_POINT"): | |
| base_point = tuple(map(float, line.split()[1:])) | |
| elif line.startswith("FNORMAL"): | |
| normal = tuple(map(float, line.split()[1:])) | |
| if base_point and normal: | |
| plane_map[plane_id] = {'base_point': base_point, 'normal': normal} | |
| elif header.startswith("CLASS WORK_PLANE "): | |
| work_plane_id = int(header.split()[-1]) | |
| plane_tag = None | |
| label = None | |
| for line in block: | |
| if line.startswith("FMY_PLANE TAG"): | |
| plane_tag = int(line.split()[-1]) | |
| elif line.startswith("DCM3_TEXT_LABEL"): | |
| label = line.split("DCM3_TEXT_LABEL", 1)[1].strip() | |
| if plane_tag: | |
| tag_to_plane[work_plane_id] = plane_tag | |
| label_map[work_plane_id] = label | |
| elif header.startswith("CLASS CONSTRAINT "): | |
| constraint = {} | |
| for line in block: | |
| if line.startswith("FEND_1 TAG"): | |
| constraint['from'] = int(line.split()[-1]) | |
| elif line.startswith("FEND_2 TAG"): | |
| constraint['to'] = int(line.split()[-1]) | |
| elif line.startswith("DCM3_TEXT_LABEL"): | |
| constraint['label'] = line.split("DCM3_TEXT_LABEL", 1)[1].strip() | |
| if constraint: | |
| constraints.append(constraint) | |
| for wp_id, plane_id in tag_to_plane.items(): | |
| if plane_id in plane_map: | |
| geometry[wp_id] = { | |
| **plane_map[plane_id], | |
| 'label': label_map.get(wp_id) | |
| } | |
| return {"parts": parts, "geometry": geometry, "constraints": constraints} | |
| async def parse_file(file: UploadFile = File(...)): | |
| content = await file.read() | |
| parsed_data = parse_dcm_assembly_from_bytes(content) | |
| return JSONResponse(content=parsed_data) | |
| if __name__ == "__main__": | |
| uvicorn.run(app, host="0.0.0.0", port=7860) | |