|
| 1 | +import json |
| 2 | +import os |
| 3 | +import xml.etree.ElementTree as ET |
| 4 | +import flask |
| 5 | +from ... import geode_functions, utils_functions |
| 6 | + |
| 7 | +routes = flask.Blueprint("models", __name__, url_prefix="/models") |
| 8 | + |
| 9 | + |
| 10 | +schemas = os.path.join(os.path.dirname(__file__), "schemas") |
| 11 | + |
| 12 | +with open(os.path.join(schemas, "vtm_component_indices.json"), "r") as file: |
| 13 | + vtm_component_indices_json = json.load(file) |
| 14 | + |
| 15 | + |
| 16 | +@routes.route( |
| 17 | + vtm_component_indices_json["route"], methods=vtm_component_indices_json["methods"] |
| 18 | +) |
| 19 | +def uuid_to_flat_index(): |
| 20 | + |
| 21 | + print(f"uuid_to_flat_index : {flask.request=}", flush=True) |
| 22 | + utils_functions.validate_request(flask.request, vtm_component_indices_json) |
| 23 | + vtm_file_path = os.path.join( |
| 24 | + flask.current_app.config["DATA_FOLDER_PATH"], flask.request.json["id"] + ".vtm" |
| 25 | + ) |
| 26 | + |
| 27 | + tree = ET.parse(vtm_file_path) |
| 28 | + root = tree.getroot() |
| 29 | + uuid_to_flat_index = {} |
| 30 | + current_index = 1 |
| 31 | + |
| 32 | + for elem in root.iter(): |
| 33 | + if "uuid" in elem.attrib and elem.tag == "DataSet": |
| 34 | + uuid_to_flat_index[elem.attrib["uuid"]] = current_index |
| 35 | + current_index += 1 |
| 36 | + |
| 37 | + return flask.make_response( |
| 38 | + {"uuid_to_flat_index": uuid_to_flat_index}, |
| 39 | + 200, |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def extract_model_uuids(geode_object, file_path): |
| 44 | + model = geode_functions.load(geode_object, file_path) |
| 45 | + mesh_components = model.mesh_components() |
| 46 | + |
| 47 | + uuid_dict = {} |
| 48 | + |
| 49 | + for mesh_component, ids in mesh_components.items(): |
| 50 | + component_name = mesh_component.get() |
| 51 | + uuid_dict[component_name] = [id.string() for id in ids] |
| 52 | + |
| 53 | + return uuid_dict |
| 54 | + |
| 55 | + |
| 56 | +with open(os.path.join(schemas, "mesh_components.json"), "r") as file: |
| 57 | + mesh_components_json = json.load(file) |
| 58 | + |
| 59 | + |
| 60 | +@routes.route(mesh_components_json["route"], methods=mesh_components_json["methods"]) |
| 61 | +def extract_uuids_endpoint(): |
| 62 | + print(f"extract_uuids_endpoint : {flask.request=}", flush=True) |
| 63 | + |
| 64 | + utils_functions.validate_request(flask.request, mesh_components_json) |
| 65 | + |
| 66 | + file_path = os.path.join( |
| 67 | + flask.current_app.config["DATA_FOLDER_PATH"], flask.request.json["filename"] |
| 68 | + ) |
| 69 | + |
| 70 | + if not os.path.exists(file_path): |
| 71 | + return flask.make_response({"error": "File not found"}, 404) |
| 72 | + |
| 73 | + uuid_dict = extract_model_uuids(flask.request.json["geode_object"], file_path) |
| 74 | + return flask.make_response({"uuid_dict": uuid_dict}, 200) |
0 commit comments