Commit a8d3a419 authored by Julian Stirling's avatar Julian Stirling 🐧

Added IPython display functions and examples

parent 6d09776f
*.egg-info
*.pyc
__pycache__
.ipynb_checkpoints
*.gcode
This diff is collapsed.
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from swomppy.ipyview import showpath,showlayer\n",
"import pyx\n",
"pyx.unit.set(defaultunit='mm')\n",
"from copy import deepcopy\n",
"import swomppy as sp\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"none\" version=\"1.1\" viewBox=\"-1 -171.079 89.874 172.079\" x=\"-1pt\" y=\"-171.079pt\" width=\"89.874pt\" height=\"172.079pt\" stroke-width=\"0.566929\" stroke-miterlimit=\"10.000000\">\n",
" <g stroke-width=\"0.850394\">\n",
" <path d=\"M0 -0L0 -170.079L28.3465 -170.079L28.3465 -93.5433L59.5276 -93.5433L59.5276 -170.079L87.874 -170.079L87.874 -0L59.5276 -0L59.5276 -76.5354L28.3465 -76.5354L28.3465 -0Z\" stroke=\"#00f\"/>\n",
" </g>\n",
"</svg>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Generate a H-shape perimenter, and the parts of two flexures\n",
"\n",
"l=0.0\n",
"bw = 10.0 # beam width\n",
"b = 0.0 #bottom\n",
"t = 60.0 #top\n",
"fr = 3.0 #flex radius\n",
"fl = 5.0 # flex length\n",
"\n",
"li = l+bw #left inside\n",
"r=l+2*bw+2*fr+fl # right\n",
"ri = r-bw # left\n",
"\n",
"fpos = [l+bw+fr,t/2.0]\n",
"fpos2 = [l+bw+fr+fl,t/2.0]\n",
"\n",
"fpos_u = [fpos[0],fpos[1]]\n",
"fpos2_u = [fpos2[0],fpos2[1]]\n",
"fpos_l = [fpos[0],fpos[1]]\n",
"fpos2_l = [fpos2[0],fpos2[1]]\n",
"\n",
"ufs = fpos_u[1]+fr #upper flex start (y)\n",
"lfs = fpos_l[1]-fr #lower flex start (y)\n",
"\n",
"asf = 20.0 #angle to skip for flexure\n",
"\n",
"\n",
"perimeter = sp.spath([(l,b),(l,t),(bw,t),(li,ufs),(ri,ufs),\n",
" (ri,t),(r,t),(r,b),(ri,b),(ri,lfs),(li,lfs),\n",
" (li,b),(l,b)],True)\n",
"\n",
"flex1 = sp.spath([(li,ufs),\n",
" [[li,ufs-fr/2],[li+fr/2,fpos_u[1]],fpos_u],fpos2_u,\n",
" [(ri-fr/2,fpos_u[1]),(ri,ufs-fr/2),(ri,ufs)]])\n",
" \n",
"flex2 = sp.spath([(ri,lfs),\n",
" [(ri,lfs+fr/2),(ri-fr/2,fpos_l[1]),fpos2_l],fpos_l,\n",
" [(li+fr/2,fpos_l[1]),(li,lfs+fr/2),(li,lfs)]])\n",
"\n",
"\n",
"showpath(perimeter)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
"<svg xmlns=\"http://www.w3.org/2000/svg\" fill=\"none\" version=\"1.1\" viewBox=\"-1 -171.079 89.874 172.079\" x=\"-1pt\" y=\"-171.079pt\" width=\"89.874pt\" height=\"172.079pt\" stroke-width=\"0.566929\" stroke-miterlimit=\"10.000000\">\n",
" <g stroke-width=\"0.850394\">\n",
" <path d=\"M0 -0L28.3465 -0L28.3465 -76.5354C28.3465 -80.7874 32.5984 -85.0394 36.8504 -85.0394L51.0236 -85.0394C55.2756 -85.0394 59.5276 -80.7874 59.5276 -76.5354L59.5276 -0L87.874 -0L87.874 -170.079L59.5276 -170.079L59.5276 -93.5433C59.5276 -89.2913 55.2756 -85.0394 51.0236 -85.0394L36.8504 -85.0394C32.5984 -85.0394 28.3465 -89.2913 28.3465 -93.5433L28.3465 -170.079L0 -170.079ZM0.992126 -0.992126L27.3543 -0.992126L27.3543 -76.5354C27.3543 -80.001 29.7987 -83.4101 32.9643 -85.0394C29.7987 -86.6687 27.3543 -90.0777 27.3543 -93.5433L27.3543 -169.087L0.992126 -169.087ZM54.9097 -85.0394C58.0753 -83.4101 60.5197 -80.001 60.5197 -76.5354L60.5197 -0.992126L86.8819 -0.992126L86.8819 -169.087L60.5197 -169.087L60.5197 -93.5433C60.5197 -90.0777 58.0753 -86.6687 54.9097 -85.0394ZM1.98425 -1.98425L26.3622 -1.98425L26.3622 -76.5354C26.3622 -79.8396 28.3206 -83.0584 31.073 -85.0394C28.3206 -87.0203 26.3622 -90.2391 26.3622 -93.5433L26.3622 -168.094L1.98425 -168.094ZM56.801 -85.0394C59.5534 -83.0584 61.5118 -79.8396 61.5118 -76.5354L61.5118 -1.98425L85.8898 -1.98425L85.8898 -168.094L61.5118 -168.094L61.5118 -93.5433C61.5118 -90.2391 59.5534 -87.0203 56.801 -85.0394ZM2.97638 -2.97638L25.3701 -2.97638L25.3701 -76.5354C25.3701 -79.7505 27.0276 -82.8671 29.4854 -85.0394C27.0276 -87.2116 25.3701 -90.3282 25.3701 -93.5433L25.3701 -167.102L2.97638 -167.102ZM58.3886 -85.0394C60.8464 -82.8671 62.5039 -79.7505 62.5039 -76.5354L62.5039 -2.97638L84.8976 -2.97638L84.8976 -167.102L62.5039 -167.102L62.5039 -93.5433C62.5039 -90.3282 60.8464 -87.2116 58.3886 -85.0394Z\" stroke=\"#0f0\"/>\n",
" </g>\n",
" <g stroke-width=\"0.850394\">\n",
" <path d=\"M3.9685 -3.72047L3.9685 -166.358M4.96063 -3.72047L4.96063 -166.358M5.95276 -3.72047L5.95276 -166.358M6.94488 -3.72047L6.94488 -166.358M7.93701 -3.72047L7.93701 -166.358M8.92913 -3.72047L8.92913 -166.358M9.92126 -3.72047L9.92126 -166.358M10.9134 -3.72047L10.9134 -166.358M11.9055 -3.72047L11.9055 -166.358M12.8976 -3.72047L12.8976 -166.358M13.8898 -3.72047L13.8898 -166.358M14.8819 -3.72047L14.8819 -166.358M15.874 -3.72047L15.874 -166.358M16.8661 -3.72047L16.8661 -166.358M17.8583 -3.72047L17.8583 -166.358M18.8504 -3.72047L18.8504 -166.358M19.8425 -3.72047L19.8425 -166.358M20.8346 -3.72047L20.8346 -166.358M21.8268 -3.72047L21.8268 -166.358M22.8189 -3.72047L22.8189 -166.358M23.811 -3.72047L23.811 -166.358M24.8031 -78.4395L24.8031 -91.6393M25.7953 -81.419L25.7953 -88.6597M26.7874 -83.1141L26.7874 -86.9646M27.7795 -84.3825L27.7795 -85.6963M59.5276 -84.9877L59.5276 -85.0911M60.5197 -83.8763L60.5197 -86.2025M61.5118 -82.4579L61.5118 -87.6208M62.5039 -80.4403L62.5039 -89.6385M63.4961 -3.72047L63.4961 -166.358M64.4882 -3.72047L64.4882 -166.358M65.4803 -3.72047L65.4803 -166.358M66.4724 -3.72047L66.4724 -166.358M67.4646 -3.72047L67.4646 -166.358M68.4567 -3.72047L68.4567 -166.358M69.4488 -3.72047L69.4488 -166.358M70.4409 -3.72047L70.4409 -166.358M71.4331 -3.72047L71.4331 -166.358M72.4252 -3.72047L72.4252 -166.358M73.4173 -3.72047L73.4173 -166.358M74.4094 -3.72047L74.4094 -166.358M75.4016 -3.72047L75.4016 -166.358M76.3937 -3.72047L76.3937 -166.358M77.3858 -3.72047L77.3858 -166.358M78.378 -3.72047L78.378 -166.358M79.3701 -3.72047L79.3701 -166.358M80.3622 -3.72047L80.3622 -166.358M81.3543 -3.72047L81.3543 -166.358M82.3465 -3.72047L82.3465 -166.358M83.3386 -3.72047L83.3386 -166.358\" stroke=\"#f00\"/>\n",
" </g>\n",
"</svg>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"offset=.35\n",
"height = 12\n",
"h0=.125\n",
"h=.25\n",
"N_lay = int((height-h0)//h)\n",
"layers = []\n",
"\n",
"brim=False\n",
"\n",
"for n in range(N_lay):\n",
" if n==0:\n",
" if brim:\n",
" brim_layer=True\n",
" else:\n",
" brim_layer=False\n",
" else:\n",
" brim_layer=False\n",
" \n",
" if n==0 or n==N_lay-1:\n",
" infillType=sp.Params.VertInfill\n",
" infillSpacing=offset\n",
" elif n==1 or n==N_lay-2:\n",
" infillType=sp.Params.HorInfill\n",
" infillSpacing=offset\n",
" else:\n",
" infillType=sp.Params.CrossInfill\n",
" infillSpacing=3\n",
" \n",
" \n",
" f1_c = deepcopy(flex1)\n",
" f2_c = deepcopy(flex2) \n",
" if n%2 == 0:\n",
" f1_c[1].attrs['NoPrint'] = True\n",
" else:\n",
" f2_c[1].attrs['NoPrint'] = True\n",
" shell=deepcopy(perimeter)\n",
" shell.replaceSubpath(f1_c)\n",
" shell.replaceSubpath(f2_c)\n",
" \n",
" layer = sp.Layer(shell,offset=offset,infillSpacing=infillSpacing,infillType=infillType,brim=brim_layer)\n",
" layer.fillLayer()\n",
" layers.append(layer)\n",
"\n",
" \n",
"showlayer(layers[0])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"ps = sp.PrintSettings(flavor='UltiGCode')\n",
"sp.generateGcode(layers,'Fibre',printsettings=ps)"
]
}
],
"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.6.8"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
from IPython.display import HTML,display
import pyx
pyx.unit.set(defaultunit='mm')
def showsvg(canvas):
canvas.writeSVGfile('/tmp/1.svg')
display(HTML(open(f'/tmp/1.svg').read()))
def showpath(path):
print_plot = pyx.canvas.canvas()
print_plot.stroke(path.np,[pyx.color.rgb.blue, pyx.style.linewidth(0.3)])
showsvg(print_plot)
def showlayer(layer):
print_plot = pyx.canvas.canvas()
if layer.brimPaths is not None:
print_plot.stroke(layer.brimPaths.as_normpath(),[pyx.color.rgb.blue, pyx.style.linewidth(0.3)])
print_plot.stroke(layer.allShells.as_normpath(),[pyx.color.rgb.green, pyx.style.linewidth(0.3)])
print_plot.stroke(layer.fillPaths.as_normpath(),[pyx.color.rgb.red, pyx.style.linewidth(0.3)])
showsvg(print_plot)
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment