{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "# 16. Integrated Workflows\n\nGenerate three room designs, choose the one with the least surface area, attach its results as metadata, and export it.\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-16-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-16-01"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Generate and compare\n\nThis combines parametric construction and measurement. Each room has the same volume; only its proportions change.\n",
   "id": "tutorial-16-02"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "from topologicpy.Cell import Cell\nfrom topologicpy.Topology import Topology\nfrom topologicpy.Dictionary import Dictionary\n\nbest_room = None\nbest_area = float(\"inf\")\nbest_width = None\nfor width in [3, 4, 6]:\n    room = Cell.Prism(width=width, length=24 / width, height=3)\n    area = Cell.SurfaceArea(room)\n    print(\"Width:\", width, \"Surface area:\", area)\n    if area < best_area:\n        best_room = room\n        best_area = area\n        best_width = width\nassert best_width == 4\nassert abs(Cell.Volume(best_room) - 72) < 0.0001\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-16-03"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Record the decision and export\n\nThe exported geometry carries the selection criterion and its value, so another workflow can understand why this option was chosen. Widths 4 and 6 tie here; the loop keeps the first best option.\n",
   "id": "tutorial-16-04"
  },
  {
   "cell_type": "code",
   "metadata": {},
   "source": "from pathlib import Path\n\nproperties = Dictionary.ByKeysValues(\n    [\"name\", \"objective\", \"surface_area\"],\n    [\"Selected room\", \"minimum surface area among candidates\", best_area]\n)\nbest_room = Topology.SetDictionary(best_room, properties)\noutput = Path(\"results\")\noutput.mkdir(exist_ok=True)\npath = output / \"16_selected_room.json\"\npath.write_text(Topology.JSONString([best_room]), encoding=\"utf-8\")\nrestored = Topology.ByJSONString(path.read_text(encoding=\"utf-8\"))[0]\nassert Dictionary.ValueAtKey(Topology.Dictionary(restored), \"name\") == \"Selected room\"\nprint(\"Saved:\", path)\n",
   "outputs": [],
   "execution_count": null,
   "id": "tutorial-16-05"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": "## Try it yourself\n\nAdd visualisation from tutorial 05, or store an access graph using tutorial 11. Extend one step at a time.\n",
   "id": "tutorial-16-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
}
