Solid-Python-Demo/solidpython.ipynb

382 lines
8.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "23b382e7",
"metadata": {
"tags": []
},
"source": [
"# Solid Python Demo"
]
},
{
"cell_type": "code",
"execution_count": null,
"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": null,
"id": "25a01e9c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"c = cylinder(r=7, h=2)\n",
"r.render(c)"
]
},
{
"cell_type": "markdown",
"id": "6cb47c97-3f36-4b31-baaf-905a399ed7be",
"metadata": {
"tags": []
},
"source": [
"## Create a cuboid"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ecdfd420-ebef-49b1-8d90-c35b1e5ac571",
"metadata": {
"tags": []
},
"outputs": [],
"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": 34,
"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": 35,
"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": 43,
"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": 11,
"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": 48,
"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": 19,
"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": 8,
"id": "9062af4f-d729-4208-abbf-1750b682b482",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Geometries in cache: 4\n",
"Geometry cache size in bytes: 9824\n",
"CGAL Polyhedrons in cache: 1\n",
"CGAL cache size in bytes: 65232\n",
"Total rendering time: 0:00:00.068\n",
" Top level object is a 3D object:\n",
" Simple: yes\n",
" Vertices: 48\n",
" Halfedges: 144\n",
" Edges: 72\n",
" Halffacets: 52\n",
" Facets: 26\n",
" Volumes: 2\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "11a120f2cd4641279ef0d1f1af319eac",
"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((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": 3,
"id": "1810ff24-c0f8-4c80-adc2-6985cd52d7dc",
"metadata": {
"tags": []
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'scad_render_to_file' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mscad_render_to_file\u001b[49m(part, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpart.scad\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
"\u001b[0;31mNameError\u001b[0m: name 'scad_render_to_file' is not defined"
]
}
],
"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": 26,
"id": "44f345ad-417a-4783-b721-847351a13f4b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Geometries in cache: 4\n",
"Geometry cache size in bytes: 3344\n",
"CGAL Polyhedrons in cache: 1\n",
"CGAL cache size in bytes: 24912\n",
"Total rendering time: 0:00:00.018\n",
" Top level object is a 3D object:\n",
" Simple: yes\n",
" Vertices: 18\n",
" Halfedges: 54\n",
" Edges: 27\n",
" Halffacets: 22\n",
" Facets: 11\n",
" Volumes: 2\n"
]
}
],
"source": [
"! openscad -o part.stl part.scad"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ddbc58d3-c9e4-4187-908b-57f65658198a",
"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
}