{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import cadquery as cq\n", "\n", "from jupyter_cadquery import *\n", "\n", "from jupyter_cadquery.replay import replay, enable_replay, disable_replay\n", "\n", "versions()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# CAD Viewer" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Auto show in cells" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "box = cq.Workplane('XY').box(1, 2, 3).edges().fillet(0.1)\n", "cv = show(box)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.center_grid = True\n", "cv.grid = (True, False, False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use `show`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv = open_viewer(\"CadQuery\", anchor=\"right\", cad_width=780, aspect_ratio=1) # sets default viewer" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv2 = open_viewer(\"CadQuery2\", anchor=\"split-bottom\", aspect_ratio=3/4) # sets default viewer" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv3 = open_viewer(\"CadQuery3\", anchor=\"split-left\", aspect_ratio=3/4) # sets default viewer" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "get_default_viewer()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "set_defaults(axes=True, grid=(True, False, False), center_grid=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cq.Workplane().cylinder(1,1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "reset_defaults()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show(box, viewer=\"\", up=\"Y\", debug=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv2 = open_viewer(\"CadQuery2\", anchor=\"split-bottom\") " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cq.Workplane().cylinder(1,1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from enum import Enum\n", "isinstance(Collapse.NONE.value, Enum)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "close_viewers()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv = show(\n", " box,\n", " \n", " viewer=\"CadQuery\",\n", " anchor=\"right\",\n", " cad_width=1000,\n", " tree_width=250,\n", " height=750,\n", " theme=\"dark\",\n", " pinning=True,\n", " \n", " angular_tolerance=0.1,\n", " deviation=1,\n", " edge_accuracy=0.01,\n", " default_color=(23, 176, 36),\n", " default_edgecolor=\"#FF0000\",\n", " render_normals=True,\n", " render_edges=True,\n", " render_mates=True,\n", " helper_scale=2,\n", "\n", " axes=True,\n", " axes0=True,\n", " grid=[True, True,False],\n", " ticks=5,\n", " ortho=False,\n", " transparent=True,\n", " black_edges=False,\n", " position=(12.4, 7.7, -6.9),\n", " quaternion=(0.522, 0.611, 0.582, 0.120),\n", " target=(0.4, -1.6, -2.2),\n", " zoom=0.2,\n", " reset_camera=\"reset\",\n", " zoom_speed=3,\n", " pan_speed=3,\n", " rotate_speed=3,\n", " ambient_intensity=0.5,\n", " direct_intensity=0.05,\n", " show_parent=True,\n", " tools=True,\n", " timeit=False, \n", " debug=True\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Assemblies" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv = open_viewer(\"Assembly\", cad_width=800, height=600)\n", "set_default_viewer(\"Assembly\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "box1 = cq.Workplane('XY').box(10, 20, 30).edges(\">X or X or Z\").fillet(3)\n", "box3.name = \"box3\"\n", "\n", "box4 = box3.mirror(\"XY\").translate((0, -5, 0))\n", "box4.name = \"box4\"\n", "\n", "box1 = box1\\\n", " .cut(box2)\\\n", " .cut(box3)\\\n", " .cut(box4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a1 = (\n", " cq.Assembly(name=\"ensemble\")\n", " .add(box1, name=\"red box\", color=\"#d7191c80\") # transparent alpha = 0x80/0xFF\n", " .add(box3, name=\"green box\", color=\"#abdda4\")\n", " .add(box4, name=\"blue box\", color=(43, 131, 186, 0.3)) # transparent, alpha = 0.3\n", ")\n", "\n", "cv = show(a1, axes=True, grid=[True, False, False], ortho=True, axes0=True, debug=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "workspace_config()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show_all()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show(a1, glass=False, transparent=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from build123d import *\n", "\n", "b123_box1 = Box(10, 20, 30)\n", "edges = b123_box1.edges().group_by(Axis.X)\n", "b123_box1 = chamfer(edges[0] + edges[-1], 2)\n", "b123_box1.label = \"b123d red box\"\n", "b123_box1.color = \"#d7191c80\"\n", "\n", "b123_box3 = Box(8, 18, 28)\n", "edges = b123_box3.edges().group_by(Axis.X)\n", "edges = edges[0] + edges[-1]\n", "b123_box2 = chamfer(edges, 2)\n", "\n", "b123_box3 = Pos(0, 15, 7) * Box(30, 20, 6)\n", "b123_box3 = fillet(b123_box3.edges().group_by()[-1], 3)\n", "b123_box3.label = \"b123d green box\"\n", "b123_box3.color = \"#abdda4\"\n", "\n", "b123_box4 = Pos(0, -5, 0) * mirror(b123_box3, Plane.XY)\n", "b123_box4.label = \"b123d blue box\"\n", "b123_box4.color = Color(43/255, 131/255, 186/255, 0.3)\n", "\n", "b123_box1 = b123_box1 - b123_box2 - b123_box3 - b123_box4" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b123_a1 = Compound(label=\"b123d ensemble\", children=[b123_box1, b123_box3, b123_box4])\n", "show(b123_a1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Faces, Edges, Vertices" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show(box2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "set_defaults(show_parent=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "box1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "box1.faces(\"not(|Z or |X or |Y)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "box1.edges(\"not(|X or |Y or |Z)\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "box1.vertices()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "show(\n", " box1,\n", " box1.faces(\"not(|Z or |X or |Y)\"), \n", " box1.edges(\"not(|X or |Y or |Z)\"), \n", " box1.vertices(),\n", " show_parent=False\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Replay" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "enable_replay(True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "box1 = cq.Workplane('XY').box(10, 20, 30).edges(\">X or X or Z\").fillet(3)\n", "box3.name = \"box3\"\n", "\n", "box4 = box3.mirror(\"XY\").translate((0, -5, 0))\n", "box4.name = \"box4\"\n", "\n", "box1 = box1\\\n", " .cut(box2)\\\n", " .cut(box3)\\\n", " .cut(box4)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "set_defaults(transparent=True)\n", "r = replay(box1, show_result=False, show_bbox=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "disable_replay()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Camera position control" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv = open_viewer(\"Assembly\")\n", "\n", "show(\n", " a1, \n", " orbit_control=True,\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p0 = cv.position\n", "t0 = cv.target\n", "q0 = cv.quaternion\n", "z0 = cv.zoom\n", "p0, q0, t0, z0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.position = (135.3, 79.2, 29.6)\n", "cv.target = (-4.8, 17.1, 7.9)\n", "cv.quaternion = (0.399, 0.518, 0.643, 0.396)\n", "cv.zoom = 1.45" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.position = p0\n", "\n", "if cv.control == \"trackball\":\n", " cv.quaternion = q0\n", "\n", "cv.target = t0\n", "cv.zoom = z0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Widget interaction" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.update_states({\n", " '/ensemble/red box/red box': (1,0),\n", " '/ensemble/blue box/blue box': [0,1],\n", "})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.update_states({\n", " '/ensemble/red box/red box': (1,1),\n", " '/ensemble/blue box/blue box': [1,1],\n", "})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.ambient_intensity = 0.9\n", "cv.direct_intensity = 0.5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.ambient_intensity = 0.5\n", "cv.direct_intensity = 0.3" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ec = cv.default_edgecolor\n", "cv.default_edgecolor = \"#ff0000\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.default_edgecolor = ec" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.grid = [not g for g in cv.widget.grid]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.axes = not cv.axes\n", "cv.axes0 = not cv.axes0\n", "cv.transparent = not cv.transparent\n", "cv.black_edges = not cv.black_edges" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.tools = not cv.tools" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.ortho = not cv.ortho" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.grid = [not g for g in cv.widget.grid]\n", "cv.axes = not cv.axes\n", "cv.axes0 = not cv.axes0\n", "cv.transparent = not cv.transparent\n", "cv.black_edges = not cv.black_edges\n", "cv.tools = not cv.tools\n", "cv.ortho = not cv.ortho" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.glass = not cv.glass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.zoom_speed = 5\n", "cv.pan_speed = 5\n", "cv.rotate_speed = 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.zoom_speed = 1\n", "cv.pan_speed = 1\n", "cv.rotate_speed = 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.last_pick" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pick = get_pick(a1, cv.last_pick)\n", "\n", "if pick is not None:\n", " show(pick, viewer=None)\n", "else:\n", " print(\"Double click on element in the CAD view first\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Clipping handling" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.tab = \"clip\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.clip_intersection = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.clip_planes = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.clip_slider_0 = 0\n", "cv.clip_slider_1 = -2\n", "cv.clip_slider_2 = 12" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.clip_intersection = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.clip_normal_0" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.clip_slider_2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.clip_normal_0 = (-0.35, -0.35, -0.35)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.clip_normal_0 = (-1, 0, 0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.clip_planes = False" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.tab = \"tree\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Rotations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Trackball" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv = show(a1, viewer=\"Cadquery\", orbit_control=False)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import time\n", "\n", "for i in range(10):\n", " cv.rotate_x(1)\n", " cv.rotate_y(3)\n", " cv.rotate_z(5)\n", " time.sleep(0.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(10):\n", " cv.rotate_z(-5)\n", " cv.rotate_y(-3)\n", " cv.rotate_x(-1)\n", " time.sleep(0.1) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Orbit" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv = show(a1, viewer=\"Cadquery\", orbit_control=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(10):\n", " cv.rotate_up(3)\n", " cv.rotate_left(1)\n", " time.sleep(0.1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(10):\n", " cv.rotate_up(-3)\n", " cv.rotate_left(-1)\n", " time.sleep(0.1) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Export PNG" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cv.export_png(\"boxes.png\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 }