{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# 10. Data Exchange and Interoperability\n\nExport a labelled room to JSON, then import it and check its geometry and attributes.\n\n**Goal:** Complete one small workflow. Run the cells from top to bottom in a fresh kernel.\n\n**Extra dependencies:** None beyond the base environment.\n\nAll examples use metres unless stated otherwise.\n",
   "id": "tutorial-10-00"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "import os\nos.environ[\"TOPOLOGICPY_CORE_BACKEND\"] = \"pythonocc\"\n\nimport topologicpy\nfrom topologicpy.Core import Core\n\nassert topologicpy.__version__ == \"0.9.86\", \"Use TopologicPy 0.9.86 for these tutorials.\"\nassert type(Core.Backend()).__name__ == \"PythonOCCBackend\", \"Restart with the pythonocc backend.\"\nprint(\"TopologicPy\", topologicpy.__version__, \"—\", type(Core.Backend()).__name__)\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-10-01"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Write a small model\n\nUse a relative output folder. The JSON representation is a convenient TopologicPy round trip, not a universal exchange standard.\n",
   "id": "tutorial-10-02"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "from pathlib import Path\nfrom topologicpy.Cell import Cell\nfrom topologicpy.Topology import Topology\nfrom topologicpy.Dictionary import Dictionary\n\nroom = Cell.Prism(width=4, length=3, height=2)\nroom = Topology.SetDictionary(room, Dictionary.ByKeysValues([\"name\"], [\"Studio\"]))\ntext = Topology.JSONString([room])\noutput = Path(\"results\")\noutput.mkdir(exist_ok=True)\npath = output / \"10_room.json\"\npath.write_text(text, encoding=\"utf-8\")\nprint(\"Saved:\", path)\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-10-03"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Read and verify\n\nImport returns a list of topologies. Check both the volume and the semantic data after the round trip.\n",
   "id": "tutorial-10-04"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "restored = Topology.ByJSONString(path.read_text(encoding=\"utf-8\"))\nrestored_room = restored[0]\nname = Dictionary.ValueAtKey(Topology.Dictionary(restored_room), \"name\")\nprint(\"Restored:\", name, Cell.Volume(restored_room))\nassert abs(Cell.Volume(restored_room) - 24) < 0.0001\nassert name == \"Studio\"\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-10-05"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Try it yourself\n\nAdd an occupancy property and verify it survives the round trip.\n",
   "id": "tutorial-10-06"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (TopologicPy tutorials)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
