{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%matplotlib widget" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from pysketcher import *" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "from ipywidgets import FloatSlider, AppLayout, Label, HBox" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from IPython.display import SVG, display" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "myfig={}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "gw = \"\"\"\\\n", "libraries: [\"from math import tan, radians, sin, cos\",\"from pysketcher import *\"]\n", "fontsize: 18\n", "g: 9.81 # constant gravity\n", "theta: 30.0 # inclined plane angle\n", "L: 10.0 # sketch sizing parameter\n", "a: 1.0 #\n", "xmin: 0.0 # sketech min Abscissa\n", "ymin: -3.0 # sketech min Ordinate \n", "r: 1.0 # radius of the wheel\n", "setframe: # sketch setup\n", " action: \"drawing_tool.set_coordinate_system(xmin=xmin, xmax=xmin+1.5*L,ymin=ymin, ymax=ymin+L+1,instruction_file='tmp_mpl.py')\"\n", "setblackline: # default frame values and actions\n", " action: \"drawing_tool.set_linecolor('black')\"\n", "B: point(a+L,0) # wall right end\n", "A: point(a,tan(radians(theta))*L) # wall left end\n", "normal_vec: point(sin(radians(theta)),cos(radians(theta))) # Vector normal to wall\n", "tangent_vec: point(cos(radians(theta)),-sin(radians(theta))) # Vector tangent to wall\n", "help_line: Line(A,B) # wall line\n", "x: a + 3*L/10.\n", "y: help_line(x=x) \n", "contact: point(x, y) \n", "c: contact + r*normal_vec\n", "outer_wheel: \n", " formula: Circle(c, r)\n", " style:\n", " linecolor: blue\n", " filled_curves: blue\n", "hole: \n", " formula: Circle(c, r/2.)\n", " style:\n", " linecolor: blue\n", " filled_curves: white\n", "N: \n", " formula: Force(contact - 2*r*normal_vec, contact, r'$N$', text_pos='start')\n", " style:\n", " linecolor: black\n", "mg: \n", " formula: Gravity(c, 3*r, text='$Mg$')\n", " style:\n", " linecolor: black\n", "wheel: \n", " formula: \"Composition({'outer': outer_wheel, 'inner': hole})\" \n", " style:\n", " shadow: 4\n", "body: \n", " formula: \"Composition({'wheel': wheel, 'N': N, 'mg': mg})\"\n", " style:\n", " linecolor: black\n", "mB:\n", " formula: Text(r'$B$',B)\n", "mA:\n", " formula: Text(r'$A$', A)\n", "wall: \n", " formula: Wall(x=[A[0], B[0]], y=[A[1], B[1]], thickness=-0.25,transparent=False)\n", " style:\n", " linecolor: black \n", "angle: \n", " formula: \"Arc_wText(r'$theta$', center=B, radius=3, start_angle=180-theta, arc_angle=theta, fontsize=fontsize)\"\n", " style:\n", " linecolor: black\n", " linewidth: 1\n", "ground: \n", " formula: Line((B[0]-L/10., 0), (B[0]-L/2.,0))\n", " stlye:\n", " linecolor: black\n", " linestyle: dashed\n", " linewidth: 1\n", "x_const: \n", " formula: Line(contact, contact + point(0,4))\n", " style:\n", " linestyle: dotted\n", " transform: rotate(-theta, contact)\n", "x_axis: \n", " formula: \"Axis(start=contact+ 3*r*normal_vec, length=4*r,label='$x$', rotation_angle=-theta)\"\n", "fixed: \n", " formula: \"Composition({'angle': angle, 'inclined wall': wall,'ground': ground,'x start': x_const, 'x axis': x_axis, 'mA': mA, 'mB': mB})\"\n", "fig: \n", " formula: \"Composition({'body': body, 'fixed elements': fixed})\"\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "sketchParse(gw,myfig)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "d = myfig['fig'].draw()" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "drawing_tool.display()" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def position(t):\n", " \"\"\"Position of center point of wheel.\"\"\"\n", " global tangent_vec,c,myfig\n", " return myfig['c'] + 0.5*myfig['g']*t**2*myfig['tangent_vec']\n", "t = 0\n", "def move(change):\n", " global fig,t,label,x,x0,dt\n", " dt = change.new - t \n", " t = change.new\n", " drawing_tool.erase()\n", " x = position(t)\n", " x0 = position(t-dt)\n", " displacement = x - x0 \n", " myfig['fig']['body'].translate(displacement)\n", " myfig['fig'].draw()\n", " label.value = f\"p:{x}\"" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "slider = FloatSlider(\n", " orientation='horizontal',\n", " description='Time:',\n", " value=0.0,\n", " min=0.0,\n", " max=1.0,\n", " step = 1.0 / 30\n", ")\n", "slider.observe(move, 'value')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "label = Label(value=\"\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "0c3ebf44543e4375a87ae6de51d4ec3e", "version_major": 2, "version_minor": 0 }, "text/plain": [ "AppLayout(children=(HBox(children=(FloatSlider(value=0.0, description='Time:', max=1.0, step=0.033333333333333…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "AppLayout(\n", " center=drawing_tool.mpl.gcf().canvas,\n", " footer=HBox([slider,label]),\n", " pane_heights=[0, 6, 1]\n", ")\n", "#drawing_tool.mpl.ion()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "drawing_tool.mpl.ion()" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "graphviz [('Composition:\\\\nfig', 'Composition:\\\\nbody'), ('Composition:\\\\nbody', 'Composition:\\\\nwheel'), ('Composition:\\\\nwheel', 'Circle:\\\\nouter'), ('Circle:\\\\nouter', 'Curve:\\\\narc'), ('Composition:\\\\nwheel', 'Circle:\\\\ninner'), ('Circle:\\\\ninner', 'Curve:\\\\narc'), ('Composition:\\\\nbody', 'Force:\\\\nN'), ('Force:\\\\nN', 'Line:\\\\narrow'), ('Line:\\\\narrow', 'Curve:\\\\nline'), ('Force:\\\\nN', 'Text:\\\\ntext'), ('Composition:\\\\nbody', 'Gravity:\\\\nmg'), ('Gravity:\\\\nmg', 'Line:\\\\narrow'), ('Line:\\\\narrow', 'Curve:\\\\nline'), ('Gravity:\\\\nmg', 'Text:\\\\ntext'), ('Composition:\\\\nfig', 'Composition:\\\\nfixed elements'), ('Composition:\\\\nfixed elements', 'Arc_wText:\\\\nangle'), ('Arc_wText:\\\\nangle', 'Arc:\\\\narc'), ('Arc:\\\\narc', 'Curve:\\\\narc'), ('Arc_wText:\\\\nangle', 'Text:\\\\ntext'), ('Composition:\\\\nfixed elements', 'Wall:\\\\ninclined wall'), ('Wall:\\\\ninclined wall', 'Curve:\\\\nwall'), ('Composition:\\\\nfixed elements', 'Line:\\\\nground'), ('Line:\\\\nground', 'Curve:\\\\nline'), ('Composition:\\\\nfixed elements', 'Line:\\\\nx start'), ('Line:\\\\nx start', 'Curve:\\\\nline'), ('Composition:\\\\nfixed elements', 'Axis:\\\\nx axis'), ('Axis:\\\\nx axis', 'Arrow3:\\\\narrow'), ('Arrow3:\\\\narrow', 'Line:\\\\nline'), ('Line:\\\\nline', 'Curve:\\\\nline'), ('Arrow3:\\\\narrow', 'Line:\\\\nhead left'), ('Line:\\\\nhead left', 'Curve:\\\\nline'), ('Arrow3:\\\\narrow', 'Line:\\\\nhead right'), ('Line:\\\\nhead right', 'Curve:\\\\nline'), ('Axis:\\\\nx axis', 'Text:\\\\nlabel'), ('Composition:\\\\nfixed elements', 'Text:\\\\nmA'), ('Composition:\\\\nfixed elements', 'Text:\\\\nmB')] defaultdict(. at 0x0000024C689EE3A0>, {'Composition:\\\\nbody': 1, 'Composition:\\\\nfig': 0, 'Composition:\\\\nwheel': 1, 'Circle:\\\\nouter': 1, 'Curve:\\\\narc': 3, 'Circle:\\\\ninner': 1, 'Force:\\\\nN': 1, 'Line:\\\\narrow': 2, 'Curve:\\\\nline': 7, 'Text:\\\\ntext': 3, 'Gravity:\\\\nmg': 1, 'Composition:\\\\nfixed elements': 1, 'Arc_wText:\\\\nangle': 1, 'Arc:\\\\narc': 1, 'Wall:\\\\ninclined wall': 1, 'Curve:\\\\nwall': 1, 'Line:\\\\nground': 1, 'Line:\\\\nx start': 1, 'Axis:\\\\nx axis': 1, 'Arrow3:\\\\narrow': 1, 'Line:\\\\nline': 1, 'Line:\\\\nhead left': 1, 'Line:\\\\nhead right': 1, 'Text:\\\\nlabel': 1, 'Text:\\\\nmA': 1, 'Text:\\\\nmB': 1})\n", "[('Composition:\\\\nfig', 'Composition:\\\\nbody'), ('Composition:\\\\nbody', 'Composition:\\\\nwheel'), ('Composition:\\\\nwheel', 'Circle:\\\\nouter'), ('Circle:\\\\nouter', 'Curve:\\\\narc (1)'), ('Composition:\\\\nwheel', 'Circle:\\\\ninner'), ('Circle:\\\\ninner', 'Curve:\\\\narc (2)'), ('Composition:\\\\nbody', 'Force:\\\\nN'), ('Force:\\\\nN', 'Line:\\\\narrow (1)'), ('Line:\\\\narrow (1)', 'Curve:\\\\nline (1)'), ('Force:\\\\nN', 'Text:\\\\ntext (1)'), ('Composition:\\\\nbody', 'Gravity:\\\\nmg'), ('Gravity:\\\\nmg', 'Line:\\\\narrow (2)'), ('Line:\\\\narrow (2)', 'Curve:\\\\nline (2)'), ('Gravity:\\\\nmg', 'Text:\\\\ntext (2)'), ('Composition:\\\\nfig', 'Composition:\\\\nfixed elements'), ('Composition:\\\\nfixed elements', 'Arc_wText:\\\\nangle'), ('Arc_wText:\\\\nangle', 'Arc:\\\\narc'), ('Arc:\\\\narc', 'Curve:\\\\narc (3)'), ('Arc_wText:\\\\nangle', 'Text:\\\\ntext (3)'), ('Composition:\\\\nfixed elements', 'Wall:\\\\ninclined wall'), ('Wall:\\\\ninclined wall', 'Curve:\\\\nwall'), ('Composition:\\\\nfixed elements', 'Line:\\\\nground'), ('Line:\\\\nground', 'Curve:\\\\nline (3)'), ('Composition:\\\\nfixed elements', 'Line:\\\\nx start'), ('Line:\\\\nx start', 'Curve:\\\\nline (4)'), ('Composition:\\\\nfixed elements', 'Axis:\\\\nx axis'), ('Axis:\\\\nx axis', 'Arrow3:\\\\narrow'), ('Arrow3:\\\\narrow', 'Line:\\\\nline'), ('Line:\\\\nline', 'Curve:\\\\nline (5)'), ('Arrow3:\\\\narrow', 'Line:\\\\nhead left'), ('Line:\\\\nhead left', 'Curve:\\\\nline (6)'), ('Arrow3:\\\\narrow', 'Line:\\\\nhead right'), ('Line:\\\\nhead right', 'Curve:\\\\nline (7)'), ('Axis:\\\\nx axis', 'Text:\\\\nlabel'), ('Composition:\\\\nfixed elements', 'Text:\\\\nmA'), ('Composition:\\\\nfixed elements', 'Text:\\\\nmB')]\n", "Run dot -Tpng -o fig.png fig.dot\n" ] } ], "source": [ "myfig['fig'].graphviz_dot('fig')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!dot -Tpng -o fig.png fig.dot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](fig.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "drawing_tool.mpl.gcf().canvas.print_png(\"fig1.png\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "![](fig1.png)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "display(SVG(sketch2SVG()))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }