{ "cells": [ { "cell_type": "markdown", "id": "1008b547-f396-4960-8a80-118370f0fab1", "metadata": {}, "source": [ "# JupyterCAD Python API" ] }, { "cell_type": "markdown", "id": "28c64dd8-6b90-4408-bbeb-e5037add1fbc", "metadata": {}, "source": [ "#### Simple usage" ] }, { "cell_type": "code", "execution_count": null, "id": "050e5936-f9e1-44ff-8bc9-a8556d7643ee", "metadata": {}, "outputs": [], "source": [ "from jupytercad import CadDocument" ] }, { "cell_type": "code", "execution_count": null, "id": "9821d466-6ae7-418f-aea7-9d67153da7d6", "metadata": {}, "outputs": [], "source": [ "doc = CadDocument()" ] }, { "cell_type": "code", "execution_count": null, "id": "e30499f0-6732-4611-a25e-46b27e606b08", "metadata": {}, "outputs": [], "source": [ "doc.add_cone().add_sphere(radius=0.8).cut(color='#ff0000')" ] }, { "cell_type": "code", "execution_count": null, "id": "ab3d7be6", "metadata": {}, "outputs": [], "source": [ "doc.set_color('Cut 1', '#00ff00')" ] }, { "cell_type": "code", "execution_count": null, "id": "aca82e6a-7718-4b1e-b762-9fc62b95fae4", "metadata": {}, "outputs": [], "source": [ "doc.objects" ] }, { "cell_type": "code", "execution_count": null, "id": "b1d3c3f1-b70b-4b42-a165-d3e1434395c5", "metadata": {}, "outputs": [], "source": [ "doc.add_cylinder(radius=0.5, position=[0, 0, 0.8]).fuse();" ] }, { "cell_type": "code", "execution_count": null, "id": "58b50fb3-e95a-409c-b12d-3b630bae7f27", "metadata": {}, "outputs": [], "source": [ "doc = CadDocument()" ] }, { "cell_type": "code", "execution_count": null, "id": "9f4c257e-7093-4724-8474-435fdfaa70fe", "metadata": {}, "outputs": [], "source": [ "doc.add_box().chamfer(edge=11, dist=0.3).fillet(radius=0.2, edge=8)" ] }, { "cell_type": "markdown", "id": "e072569b-21b8-474c-b7b4-5bf042101e9f", "metadata": {}, "source": [ "#### Create a gear wheel" ] }, { "cell_type": "code", "execution_count": null, "id": "802e38ad-ee7c-4c81-8fed-805abf09a4df", "metadata": {}, "outputs": [], "source": [ "# Creating a gear wheel\n", "from jupytercad import CadDocument\n", "import math\n", "\n", "doc = CadDocument()\n", "\n", "radius = 5\n", "num_teeth = 15\n", "tooth_width = 0.5\n", "\n", "# Create the gear wheel body\n", "body_radius = radius - tooth_width / 2\n", "body_height = tooth_width\n", "doc.add_cylinder(radius=body_radius, height=body_height)\n", "\n", "# Create the teeth\n", "tooth_angle = 2 * math.pi / num_teeth\n", "for i in range(num_teeth):\n", " angle = i * tooth_angle\n", " doc.add_box(\n", " length=tooth_width,\n", " width=tooth_width,\n", " height=body_height,\n", " position=[(radius - tooth_width) * math.cos(angle), (radius - tooth_width) * math.sin(angle), 0],\n", " rotation_axis=[0, 0, 1],\n", " rotation_angle=angle * 180/math.pi\n", " ).cut()\n", "\n", "# Create the central hole\n", "hole_radius = radius * 0.3\n", "hole_height = body_height\n", "doc.add_cylinder(radius=hole_radius, height=hole_height).cut()\n", "doc" ] }, { "cell_type": "markdown", "id": "a2874026-07eb-4d92-b660-cc68606c0664", "metadata": {}, "source": [ "#### Create a shape from the Open Cascade Python API" ] }, { "cell_type": "code", "execution_count": null, "id": "9f587854-329d-41b4-8c61-7fc1bf930811", "metadata": {}, "outputs": [], "source": [ "from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox\n", "\n", "from jupytercad import CadDocument\n", "\n", "doc = CadDocument()\n", "\n", "box_shape = BRepPrimAPI_MakeBox(10, 20, 30).Shape()\n", "\n", "doc.add_occ_shape(box_shape)\n", "\n", "doc" ] }, { "cell_type": "code", "execution_count": null, "id": "bb94973f-3b9e-4652-9f46-5070f6bd2583", "metadata": {}, "outputs": [], "source": [ "from OCC.Extend.DataExchange import read_stl_file\n", "\n", "wheel = read_stl_file(\"fan.stl\")\n", "\n", "from jupytercad import CadDocument\n", "\n", "doc = CadDocument()\n", "\n", "doc.add_occ_shape(wheel)\n", "\n", "doc" ] }, { "cell_type": "markdown", "id": "679bd8dd-9bbf-4ecf-a676-32e8137992d1", "metadata": {}, "source": [ "#### Add annotations and modify shape color" ] }, { "cell_type": "code", "execution_count": null, "id": "245baeb7-5df6-45a3-bd75-aa4a33258fce", "metadata": {}, "outputs": [], "source": [ "\n", "from jupytercad_lab import CadDocument\n", "\n", "doc = CadDocument('test.jcad')\n", "doc" ] }, { "cell_type": "code", "execution_count": null, "id": "cf45ee1f-76ee-4973-818f-167ce82ea384", "metadata": {}, "outputs": [], "source": [ "user= {'color':'#0000ff30', 'initials': 'BO','display_name': 'Bot'}\n", "id = doc.add_annotation('box','Added from Python API', user=user)" ] }, { "cell_type": "code", "execution_count": null, "id": "08764533-d8f8-494f-8a59-9dd5360ba17e", "metadata": {}, "outputs": [], "source": [ "doc.remove_annotation(id)" ] } ], "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.12.2" } }, "nbformat": 4, "nbformat_minor": 5 }