{ "cells": [ { "cell_type": "markdown", "id": "23b382e7", "metadata": { "tags": [] }, "source": [ "# Solid Python Demo" ] }, { "cell_type": "code", "execution_count": 3, "id": "a75c573b", "metadata": { "tags": [] }, "outputs": [], "source": [ "from solid import *\n", "import viewscad\n", "r = viewscad.Renderer()" ] }, { "cell_type": "markdown", "id": "6a77043a", "metadata": {}, "source": [ "## Create a cylinder" ] }, { "cell_type": "code", "execution_count": 4, "id": "25a01e9c", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Geometries in cache: 1\n", "Geometry cache size in bytes: 3032\n", "CGAL Polyhedrons in cache: 0\n", "CGAL cache size in bytes: 0\n", "Total rendering time: 0:00:00.000\n", " Top level object is a 3D object:\n", " Facets: 22\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "2fb4234896484f178a27830ab615f08b", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value=''), Renderer(background='#cccc88', background_opacity=0.0, camera=PerspectiveCamera…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "cyl = cylinder(r=7, h=2)\n", "r.render(cyl)" ] }, { "cell_type": "markdown", "id": "6cb47c97-3f36-4b31-baaf-905a399ed7be", "metadata": { "tags": [] }, "source": [ "## Create a cuboid" ] }, { "cell_type": "code", "execution_count": 5, "id": "ecdfd420-ebef-49b1-8d90-c35b1e5ac571", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Geometries in cache: 1\n", "Geometry cache size in bytes: 728\n", "CGAL Polyhedrons in cache: 0\n", "CGAL cache size in bytes: 0\n", "Total rendering time: 0:00:00.000\n", " Top level object is a 3D object:\n", " Facets: 6\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "f3131bd71d4b44ffa01e86efeb64a525", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value=''), Renderer(background='#cccc88', background_opacity=0.0, camera=PerspectiveCamera…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "c = cube((10,4,6)) #pass a tuple x, y, z\n", "r.render(c)" ] }, { "cell_type": "markdown", "id": "c77c2196-ccf6-4be8-94ea-8ad870ccdd23", "metadata": {}, "source": [ "## Translating" ] }, { "cell_type": "code", "execution_count": null, "id": "2ae63616-4044-4c6a-bf11-9ebf83f8f22a", "metadata": { "tags": [] }, "outputs": [], "source": [ "from solid.utils import *" ] }, { "cell_type": "code", "execution_count": null, "id": "77e99d01-1aa4-43ce-93bf-c71c6f1325c8", "metadata": { "tags": [] }, "outputs": [], "source": [ "x, y, z = 1, 1, 1\n", "c = translate([x, y, z]) (\n", " cylinder(r=1, h=5)\n", ")\n", "r.render(c)" ] }, { "cell_type": "code", "execution_count": null, "id": "bb5ad126-5476-43a8-9c8f-75aebce0c47e", "metadata": { "tags": [] }, "outputs": [], "source": [ "c = left(x)(\n", " forward(y)(\n", " up(z)(\n", " cylinder(r=1, h=5)\n", " )\n", " )\n", " )\n", "r.render(c)" ] }, { "cell_type": "markdown", "id": "1bc5ffad-a5d3-4d61-8d38-5ee2c015c3cc", "metadata": {}, "source": [ "## Rotating" ] }, { "cell_type": "code", "execution_count": null, "id": "86cb4b1a-03cf-449c-bc08-2ea81512c34a", "metadata": { "tags": [] }, "outputs": [], "source": [ "c = cylinder(r=2, h=4)\n", "rotated = rotate((90, 0 , 0))(c)\n", "r.render(rotated)" ] }, { "cell_type": "markdown", "id": "153bc0ab-212a-417c-aefd-0693b2c6a704", "metadata": { "tags": [] }, "source": [ "## Adding objects" ] }, { "cell_type": "code", "execution_count": null, "id": "a5769eb4-13d4-425a-a22b-8e14123f8908", "metadata": { "tags": [] }, "outputs": [], "source": [ "base = cube((5,5,1)) #pass a tuple x, y, z\n", "top = cylinder(r=1, h=5)\n", "part = union()(\n", " base,\n", " translate((2.5, 2.5, 1))(\n", " top\n", " )\n", ")\n", "r.render(part)" ] }, { "cell_type": "code", "execution_count": null, "id": "6107bce9-ff24-4f94-974b-637445c707f5", "metadata": { "tags": [] }, "outputs": [], "source": [ "base = cube((5,5,1)) #pass a tuple x, y, z\n", "top = cylinder(r=1, h=5)\n", "part = base + translate((2.5, 2.5, 1))(\n", " rotate((60, 0, 0))(\n", " top\n", " )\n", ")\n", "\n", "r.render(part)" ] }, { "cell_type": "markdown", "id": "c1413bc8-05a6-458c-b308-07e8af2b1ebe", "metadata": {}, "source": [ "## Subtracting objects" ] }, { "cell_type": "code", "execution_count": null, "id": "c425603f-8c79-4e7b-b204-3c128768b6ab", "metadata": { "tags": [] }, "outputs": [], "source": [ "base = cube((5,5,1)) #pass a tuple x, y, z\n", "top = cylinder(r=1, h=5)\n", "part = difference()(\n", " base,\n", " translate((2.5, 2.5, 0))(\n", " top\n", " )\n", ")\n", "r.render(part)" ] }, { "cell_type": "code", "execution_count": null, "id": "9062af4f-d729-4208-abbf-1750b682b482", "metadata": { "tags": [] }, "outputs": [], "source": [ "base = cube((5,5,2))\n", "inner = cylinder(r=0.5, h=5)\n", "part = base - translate((2.5, 0, 1))(\n", " rotate((270,0,0))(\n", " inner\n", " )\n", ")\n", "\n", "\n", "r.render(part)" ] }, { "cell_type": "markdown", "id": "856d57d9-c292-48ba-8871-050ea53c0110", "metadata": {}, "source": [ "## Save to SCAD" ] }, { "cell_type": "code", "execution_count": null, "id": "1810ff24-c0f8-4c80-adc2-6985cd52d7dc", "metadata": { "tags": [] }, "outputs": [], "source": [ "scad_render_to_file(part, \"part.scad\")" ] }, { "cell_type": "markdown", "id": "98302da6-a3ac-4386-9398-2afa67b2856c", "metadata": {}, "source": [ "## Convert to stl" ] }, { "cell_type": "code", "execution_count": null, "id": "44f345ad-417a-4783-b721-847351a13f4b", "metadata": {}, "outputs": [], "source": [ "! openscad -o part.stl part.scad" ] }, { "cell_type": "markdown", "id": "c0c11a99-e3f6-4479-81d6-28715f84199f", "metadata": {}, "source": [ "## Polyhedron" ] }, { "cell_type": "code", "execution_count": 36, "id": "9866b676-247c-4a9b-a67d-0122d2932819", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Geometries in cache: 1\n", "Geometry cache size in bytes: 584\n", "CGAL Polyhedrons in cache: 0\n", "CGAL cache size in bytes: 0\n", "Total rendering time: 0:00:00.000\n", " Top level object is a 3D object:\n", " Facets: 6\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "95a13583cb54495f85e760a9be96547c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value=''), Renderer(background='#cccc88', background_opacity=0.0, camera=PerspectiveCamera…" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "'/home/jimmy/projects/Solid-Python-Demo/polyhedron.scad'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = polyhedron(points = [\n", " [2, 2, 2],\n", " [2, 1, 2],\n", " [2, 2 ,1],\n", " [1, 2, 1],\n", " [1, 3, 2]\n", " ], \n", " faces = [\n", " [0 ,1, 2],\n", " [0, 1, 3],\n", " [1, 2, 3],\n", " [2, 3, 4],\n", " [0, 3, 4],\n", " [0, 2, 4]\n", " ]\n", ")\n", "r.render(p)\n", "scad_render_to_file(p, \"polyhedron.scad\")" ] }, { "cell_type": "code", "execution_count": 42, "id": "cf0508f0-cd53-4161-aa5d-47ebd3a5af52", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Geometries in cache: 27\n", "Geometry cache size in bytes: 79560\n", "CGAL Polyhedrons in cache: 1\n", "CGAL cache size in bytes: 1358544\n", "Total rendering time: 0:00:03.888\n", " Top level object is a 3D object:\n", " Simple: yes\n", " Vertices: 1008\n", " Halfedges: 3024\n", " Edges: 1512\n", " Halffacets: 1012\n", " Facets: 506\n", " Volumes: 2\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "ea6ccc6896874bb4a4c3ba5ce93ea251", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value=''), Renderer(background='#cccc88', background_opacity=0.0, camera=PerspectiveCamera…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "base = cube((10, 10, 1))\n", "for y in range(5):\n", " for x in range(5):\n", " base -= translate([x*2+1, y*2+1, 0])(cylinder(r=0.5, h=1))\n", "r.render(base)" ] }, { "cell_type": "code", "execution_count": 46, "id": "ddd5c9a6-61e9-48d6-9755-354d48628ac0", "metadata": { "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Geometries in cache: 111\n", "Geometry cache size in bytes: 80808\n", "CGAL Polyhedrons in cache: 1\n", "CGAL cache size in bytes: 482320\n", "Total rendering time: 0:00:01.601\n", " Top level object is a 3D object:\n", " Simple: no\n", " Vertices: 324\n", " Halfedges: 1050\n", " Edges: 525\n", " Halffacets: 406\n", " Facets: 203\n", " Volumes: 2\n", "WARNING: Object may not be a valid 2-manifold and may need repair!\n", "EXPORT-WARNING: Exported object may not be a valid 2-manifold and may need repair\n" ] }, { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "cd1ce9b432e2425d989cf116cd0d61b8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "VBox(children=(HTML(value=''), Renderer(background='#cccc88', background_opacity=0.0, camera=PerspectiveCamera…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from random import randint\n", "base = cube((10, 10, 1))\n", "for y in range(10):\n", " for x in range(10):\n", " base += translate([x, y, 0])(cube((1,1,randint(1,10))))\n", "r.render(base)\n", " " ] }, { "cell_type": "code", "execution_count": null, "id": "173c5f9c-e99e-461d-a421-cb68a1ad6783", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" } }, "nbformat": 4, "nbformat_minor": 5 }