Преглед изворни кода

added a sketch yaml format and reader

Gilbert Brault пре 5 година
родитељ
комит
5fdc909f90

BIN
doc/pub/tutorial/.ipynb_checkpoints/pysketcher-checkpoint.pdf


BIN
notebooks/.ipynb_checkpoints/fig1-checkpoint.png


+ 8 - 5
notebooks/.ipynb_checkpoints/wheelonInclinedPlane-checkpoint.ipynb

@@ -94,7 +94,7 @@
     "\n",
     "\n",
     "    B = point(a+L, 0)\n",
     "    B = point(a+L, 0)\n",
     "    A = point(a, tan(radians(theta))*L)\n",
     "    A = point(a, tan(radians(theta))*L)\n",
-    "\n",
+    "    \n",
     "    wall = Wall(x=[A[0], B[0]], y=[A[1], B[1]], thickness=-0.25,\n",
     "    wall = Wall(x=[A[0], B[0]], y=[A[1], B[1]], thickness=-0.25,\n",
     "                transparent=False)\n",
     "                transparent=False)\n",
     "\n",
     "\n",
@@ -115,6 +115,9 @@
     "    contact = point(x, y)\n",
     "    contact = point(x, y)\n",
     "    normal_vec = point(sin(radians(theta)), cos(radians(theta)))\n",
     "    normal_vec = point(sin(radians(theta)), cos(radians(theta)))\n",
     "    tangent_vec = point(cos(radians(theta)), -sin(radians(theta)))\n",
     "    tangent_vec = point(cos(radians(theta)), -sin(radians(theta)))\n",
+    "    \n",
+    "    length = Distance_wText(A+normal_vec*2.5,B+normal_vec*2.5, '$L$')\n",
+    "    \n",
     "    c = contact + r*normal_vec\n",
     "    c = contact + r*normal_vec\n",
     "    outer_wheel = Circle(c, r)\n",
     "    outer_wheel = Circle(c, r)\n",
     "    outer_wheel.set_linecolor('blue')\n",
     "    outer_wheel.set_linecolor('blue')\n",
@@ -134,11 +137,11 @@
     "    x_const.set_linestyle('dotted')\n",
     "    x_const.set_linestyle('dotted')\n",
     "    x_const.rotate(-theta, contact)\n",
     "    x_const.rotate(-theta, contact)\n",
     "    # or x_const = Line(contact-2*r*normal_vec, contact+4*r*normal_vec).set_linestyle('dotted')\n",
     "    # or x_const = Line(contact-2*r*normal_vec, contact+4*r*normal_vec).set_linestyle('dotted')\n",
-    "    x_axis = Axis(start=contact+ 3*r*normal_vec, length=4*r,\n",
+    "    x_axis = Axis(start=contact+ 3.5*r*normal_vec, length=4*r,\n",
     "                  label='$x$', rotation_angle=-theta)\n",
     "                  label='$x$', rotation_angle=-theta)\n",
     "\n",
     "\n",
     "    body  = Composition({'wheel': wheel, 'N': N, 'mg': mg})\n",
     "    body  = Composition({'wheel': wheel, 'N': N, 'mg': mg})\n",
-    "    fixed = Composition({'angle': angle, 'inclined wall': wall,\n",
+    "    fixed = Composition({'length': length, 'angle': angle, 'inclined wall': wall,\n",
     "                         'ground': ground,\n",
     "                         'ground': ground,\n",
     "                         'x start': x_const, 'x axis': x_axis})\n",
     "                         'x start': x_const, 'x axis': x_axis})\n",
     "\n",
     "\n",
@@ -210,7 +213,7 @@
     {
     {
      "data": {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "d62ee3eb721b4dfb8e5ed6cb5f0b506b",
+       "model_id": "e875370227c444868676903f4485bc7b",
        "version_major": 2,
        "version_major": 2,
        "version_minor": 0
        "version_minor": 0
       },
       },
@@ -268,7 +271,7 @@
    "metadata": {},
    "metadata": {},
    "outputs": [],
    "outputs": [],
    "source": [
    "source": [
-    "fig"
+    "fig.show_hierarchy"
    ]
    ]
   },
   },
   {
   {

Разлика између датотеке није приказан због своје велике величине
+ 541 - 0
notebooks/.ipynb_checkpoints/yaml-checkpoint.ipynb


+ 36 - 0
notebooks/fig.dot

@@ -0,0 +1,36 @@
+digraph G {
+"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";
+}

BIN
notebooks/fig.png


BIN
notebooks/fig1.png


Разлика између датотеке није приказан због своје велике величине
+ 172 - 0
notebooks/svg_filter_pie.svg


Разлика између датотеке није приказан због своје велике величине
+ 109 - 0
notebooks/test.ipynb


+ 9 - 51
notebooks/wheelonInclinedPlane.ipynb

@@ -94,7 +94,7 @@
     "\n",
     "\n",
     "    B = point(a+L, 0)\n",
     "    B = point(a+L, 0)\n",
     "    A = point(a, tan(radians(theta))*L)\n",
     "    A = point(a, tan(radians(theta))*L)\n",
-    "\n",
+    "    \n",
     "    wall = Wall(x=[A[0], B[0]], y=[A[1], B[1]], thickness=-0.25,\n",
     "    wall = Wall(x=[A[0], B[0]], y=[A[1], B[1]], thickness=-0.25,\n",
     "                transparent=False)\n",
     "                transparent=False)\n",
     "\n",
     "\n",
@@ -115,6 +115,9 @@
     "    contact = point(x, y)\n",
     "    contact = point(x, y)\n",
     "    normal_vec = point(sin(radians(theta)), cos(radians(theta)))\n",
     "    normal_vec = point(sin(radians(theta)), cos(radians(theta)))\n",
     "    tangent_vec = point(cos(radians(theta)), -sin(radians(theta)))\n",
     "    tangent_vec = point(cos(radians(theta)), -sin(radians(theta)))\n",
+    "    \n",
+    "    length = Distance_wText(A+normal_vec*2.5,B+normal_vec*2.5, '$L$')\n",
+    "    \n",
     "    c = contact + r*normal_vec\n",
     "    c = contact + r*normal_vec\n",
     "    outer_wheel = Circle(c, r)\n",
     "    outer_wheel = Circle(c, r)\n",
     "    outer_wheel.set_linecolor('blue')\n",
     "    outer_wheel.set_linecolor('blue')\n",
@@ -134,11 +137,11 @@
     "    x_const.set_linestyle('dotted')\n",
     "    x_const.set_linestyle('dotted')\n",
     "    x_const.rotate(-theta, contact)\n",
     "    x_const.rotate(-theta, contact)\n",
     "    # or x_const = Line(contact-2*r*normal_vec, contact+4*r*normal_vec).set_linestyle('dotted')\n",
     "    # or x_const = Line(contact-2*r*normal_vec, contact+4*r*normal_vec).set_linestyle('dotted')\n",
-    "    x_axis = Axis(start=contact+ 3*r*normal_vec, length=4*r,\n",
+    "    x_axis = Axis(start=contact+ 3.5*r*normal_vec, length=4*r,\n",
     "                  label='$x$', rotation_angle=-theta)\n",
     "                  label='$x$', rotation_angle=-theta)\n",
     "\n",
     "\n",
     "    body  = Composition({'wheel': wheel, 'N': N, 'mg': mg})\n",
     "    body  = Composition({'wheel': wheel, 'N': N, 'mg': mg})\n",
-    "    fixed = Composition({'angle': angle, 'inclined wall': wall,\n",
+    "    fixed = Composition({'length': length, 'angle': angle, 'inclined wall': wall,\n",
     "                         'ground': ground,\n",
     "                         'ground': ground,\n",
     "                         'x start': x_const, 'x axis': x_axis})\n",
     "                         'x start': x_const, 'x axis': x_axis})\n",
     "\n",
     "\n",
@@ -210,7 +213,7 @@
     {
     {
      "data": {
      "data": {
       "application/vnd.jupyter.widget-view+json": {
       "application/vnd.jupyter.widget-view+json": {
-       "model_id": "d62ee3eb721b4dfb8e5ed6cb5f0b506b",
+       "model_id": "0a666eee05c24db584fc9920da91b8af",
        "version_major": 2,
        "version_major": 2,
        "version_minor": 0
        "version_minor": 0
       },
       },
@@ -264,54 +267,9 @@
   },
   },
   {
   {
    "cell_type": "code",
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": null,
    "metadata": {},
    "metadata": {},
-   "outputs": [
-    {
-     "data": {
-      "text/plain": [
-       "<bound method Shape.show_hierarchy of {\n",
-       "'body': {\n",
-       "    'wheel': {\n",
-       "        'outer': {\n",
-       "            'arc': \"181 (x,y) coords linecolor='b' fillcolor='b' fillpattern=''\",},\n",
-       "        'inner': {\n",
-       "            'arc': \"181 (x,y) coords linecolor='b' fillcolor='w' fillpattern=''\",},},\n",
-       "    'N': {\n",
-       "        'arrow': {\n",
-       "            'line': \"2 (x,y) coords arrow='->'\",},\n",
-       "        'text': \"Text at (2.7875,1.94134)\",},\n",
-       "    'mg': {\n",
-       "        'arrow': {\n",
-       "            'line': \"2 (x,y) coords linecolor='k' arrow='->'\",},\n",
-       "        'text': \"Text at (4.5,1.48248)\",},},\n",
-       "'fixed elements': {\n",
-       "    'angle': {\n",
-       "        'arc': {\n",
-       "            'arc': \"181 (x,y) coords linecolor='k' linewidth=1\",},\n",
-       "        'text': \"Text at (7.86074,0.841162)\",},\n",
-       "    'inclined wall': {\n",
-       "        'wall': \"4 (x,y) coords fillcolor='white' fillpattern='/'\",},\n",
-       "    'ground': {\n",
-       "        'line': \"2 (x,y) coords linecolor='k' linewidth=1 linestyle='dashed'\",},\n",
-       "    'x start': {\n",
-       "        'line': \"2 (x,y) coords linestyle='dotted'\",},\n",
-       "    'x axis': {\n",
-       "        'arrow': {\n",
-       "            'line': {\n",
-       "                'line': \"2 (x,y) coords\",},\n",
-       "            'head left': {\n",
-       "                'line': \"2 (x,y) coords linestyle='solid'\",},\n",
-       "            'head right': {\n",
-       "                'line': \"2 (x,y) coords linestyle='solid'\",},},\n",
-       "        'label': \"Text at (9.25278,4.47286)\",},},}>"
-      ]
-     },
-     "execution_count": 14,
-     "metadata": {},
-     "output_type": "execute_result"
-    }
-   ],
+   "outputs": [],
    "source": [
    "source": [
     "fig.show_hierarchy"
     "fig.show_hierarchy"
    ]
    ]

Разлика између датотеке није приказан због своје велике величине
+ 541 - 0
notebooks/yaml.ipynb


+ 61 - 0
pysketcher/shapes.py

@@ -13,10 +13,71 @@ from builtins import object
 from numpy import linspace, sin, cos, pi, array, asarray, ndarray, sqrt, abs
 from numpy import linspace, sin, cos, pi, array, asarray, ndarray, sqrt, abs
 import pprint, copy, glob, os
 import pprint, copy, glob, os
 from math import radians
 from math import radians
+from io import BytesIO
+from ruamel.yaml import YAML
 
 
 from .MatplotlibDraw import MatplotlibDraw
 from .MatplotlibDraw import MatplotlibDraw
 drawing_tool = MatplotlibDraw()
 drawing_tool = MatplotlibDraw()
 
 
+def toSVG():
+    f = BytesIO()
+    drawing_tool.mpl.savefig(f, format="svg")
+    return f.getvalue()
+
+def loadSketch(sketch, container):
+    yaml = YAML()
+    gwd = yaml.load(sketch)
+    for _k in list(gwd.keys()):
+        if _k == "stop":
+            break
+        _c = gwd[_k]
+        _t = str(type(_c))
+        if _k == "libraries":
+            for l in _c:
+                exec(l,container)
+        #print(_k, _c, _t)
+        if _t == "<class 'ruamel.yaml.scalarfloat.ScalarFloat'>" or \
+        _t == "<class 'str'>" or _t == "<class 'int'>":
+            _formula = f"{_k} = {_c}".replace("<bslash>","\\") 
+            #print(_formula)
+            exec(_formula,container)
+        elif _t == "<class 'ruamel.yaml.comments.CommentedMap'>":
+            #print(_c)
+            _keys = list(_c.keys())
+            #print(_keys)
+            if 'formula' in _keys:
+                _formula = f"{_k} = {_c['formula']}".replace("<bslash>","\\")
+                #print(_formula)
+                exec(_formula,container)
+            if 'style' in _keys:
+                for _style in _c["style"]:
+                    #  x_const.set_linestyle('dotted')
+                    _param = _c["style"][_style]
+                    __t = str(type(_param))
+                    #print(__t)
+                    if __t == "<class 'int'>":
+                        _style = f"{_k}.set_{_style}({_param})"
+                    else:
+                        _style = f"{_k}.set_{_style}('{_param}')"
+                    #print(_style)
+                    exec(_style,container)
+            if 'transform' in _keys:
+                #print(_c['transform'])
+                if str(type(_c['transform'])) == "<class 'str'>":
+                    _t = f"{_k}.{_c['transform']}"
+                    #print(_t)
+                    exec(_t,container)
+                else:
+                    for _transform in _c["transform"]:
+                    #  x_const.rotate(-theta, contact)
+                        _t = f"{_k}.{_c['transform']}"
+                        #print(_t)
+                        exec(_t,container)
+            if "action" in _keys:
+                _action = _c["action"]
+                #print(_action)
+                exec(_action,container)
+
 def point(x, y, check_inside=False):
 def point(x, y, check_inside=False):
     for obj, name in zip([x, y], ['x', 'y']):
     for obj, name in zip([x, y], ['x', 'y']):
         if isinstance(obj, (float,int)):
         if isinstance(obj, (float,int)):