├── wsgi.py ├── requirements.txt ├── favicon ├── favicon.ico ├── favicon-16x16.png ├── favicon-32x32.png ├── mstile-150x150.png ├── apple-touch-icon.png ├── android-chrome-192x192.png ├── android-chrome-512x512.png ├── browserconfig.xml ├── site.webmanifest └── safari-pinned-tab.svg ├── img ├── navbar │ ├── logo.png │ └── dropdown.png └── icons │ └── iconmonstr-github-4.svg ├── requirements-web.txt ├── test-data ├── test-numpad ├── test-numpad-rs-flag ├── test-tkl ├── test-rotated-keys ├── rotated-blocks ├── test-full104 └── test-ergo ├── test.py ├── .plate.dxf.swp ├── .gitignore ├── README.md ├── web.py ├── templates └── base.html ├── plategen2.py ├── LICENSE └── plategen.py /wsgi.py: -------------------------------------------------------------------------------- 1 | from web import app 2 | 3 | if __name__ == "__main__": 4 | app.run() -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | ezdxf==0.8.8 2 | pyparsing==2.2.2 3 | json5==0.6.1 4 | mpmath==1.0.0 5 | -------------------------------------------------------------------------------- /favicon/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fcoury/another-keyboard-builder/master/favicon/favicon.ico -------------------------------------------------------------------------------- /img/navbar/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fcoury/another-keyboard-builder/master/img/navbar/logo.png -------------------------------------------------------------------------------- /img/navbar/dropdown.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fcoury/another-keyboard-builder/master/img/navbar/dropdown.png -------------------------------------------------------------------------------- /favicon/favicon-16x16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fcoury/another-keyboard-builder/master/favicon/favicon-16x16.png -------------------------------------------------------------------------------- /favicon/favicon-32x32.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fcoury/another-keyboard-builder/master/favicon/favicon-32x32.png -------------------------------------------------------------------------------- /favicon/mstile-150x150.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fcoury/another-keyboard-builder/master/favicon/mstile-150x150.png -------------------------------------------------------------------------------- /favicon/apple-touch-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fcoury/another-keyboard-builder/master/favicon/apple-touch-icon.png -------------------------------------------------------------------------------- /favicon/android-chrome-192x192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fcoury/another-keyboard-builder/master/favicon/android-chrome-192x192.png -------------------------------------------------------------------------------- /favicon/android-chrome-512x512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fcoury/another-keyboard-builder/master/favicon/android-chrome-512x512.png -------------------------------------------------------------------------------- /requirements-web.txt: -------------------------------------------------------------------------------- 1 | Click==7.0 2 | ezdxf==0.8.8 3 | Flask==1.0.2 4 | Jinja2==2.10 5 | json5==0.6.1 6 | MarkupSafe==1.0 7 | mpmath==1.0.0 8 | pyparsing==2.2.2 9 | Werkzeug==0.14.1 10 | -------------------------------------------------------------------------------- /test-data/test-numpad: -------------------------------------------------------------------------------- 1 | ["Num Lock","/","*","-"], 2 | ["7\nHome","8\n↑","9\nPgUp",{h:2},"+"], 3 | ["4\n←","5","6\n→"], 4 | ["1\nEnd","2\n↓","3\nPgDn",{h:2},"Enter"], 5 | [{w:2},"0\nIns",".\nDel"] 6 | -------------------------------------------------------------------------------- /test-data/test-numpad-rs-flag: -------------------------------------------------------------------------------- 1 | ["Num Lock","/","*","-"], 2 | ["7\nHome","8\n↑","9\nPgUp",{h:2},"+"], 3 | ["4\n←","5","6\n→"], 4 | ["1\nEnd","2\n↓","3\nPgDn",{h:2},"Enter"], 5 | [{w:2,_rs:23},"0\nIns",".\nDel"] 6 | -------------------------------------------------------------------------------- /favicon/browserconfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | #ffffff 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import plategen 4 | 5 | filename = 'test-data/test-full104' 6 | gen = plategen.PlateGenerator('mx', '0.5', 'mx-simple', '0.5', 'none', '0.5', '19.05', '19.05', 'stdout', 'plate.dxf', False) 7 | with open(filename, 'r') as input_file: 8 | input_data = input_file.read() 9 | gen.generate_plate(input_data) 10 | -------------------------------------------------------------------------------- /test-data/test-tkl: -------------------------------------------------------------------------------- 1 | [{a:7},"",{x:1},"","","","",{x:0.5},"","","","",{x:0.5},"","","","",{x:0.25},"","",""], 2 | [{y:0.25},"","","","","","","","","","","","","",{w:2},"",{x:0.25},"","",""], 3 | [{w:1.5},"","","","","","","","","","","","","",{w:1.5},"",{x:0.25},"","",""], 4 | [{w:1.75},"","","","","","","","","","","","",{w:2.25},""], 5 | [{w:2.25},"","","","","","","","","","","",{w:2.75},"",{x:1.25},""], 6 | [{w:1.25},"",{w:1.25},"",{w:1.25},"",{w:6.25},"",{w:1.25},"",{w:1.25},"",{w:1.25},"",{w:1.25},"",{x:0.25},"","",""] 7 | -------------------------------------------------------------------------------- /favicon/site.webmanifest: -------------------------------------------------------------------------------- 1 | { 2 | "name": "", 3 | "short_name": "", 4 | "icons": [ 5 | { 6 | "src": "/favicon/android-chrome-192x192.png", 7 | "sizes": "192x192", 8 | "type": "image/png" 9 | }, 10 | { 11 | "src": "/favicon/android-chrome-512x512.png", 12 | "sizes": "512x512", 13 | "type": "image/png" 14 | } 15 | ], 16 | "theme_color": "#ffffff", 17 | "background_color": "#ffffff", 18 | "display": "standalone" 19 | } 20 | -------------------------------------------------------------------------------- /test-data/test-rotated-keys: -------------------------------------------------------------------------------- 1 | ["~\n`","!\n1","@\n2","#\n3","$\n4","%\n5","^\n6","&\n7","*\n8","(\n9",")\n0","_\n-","+\n=",{w:2},"Backspace"], 2 | [{w:1.5},"Tab","Q","W","E","R","T","Y","U","I","O","P","{\n[","}\n]",{w:1.5},"|\n\\"], 3 | [{w:1.75},"Caps Lock","A","S",{x:1},"F","G","H","J","K","L",":\n;","\"\n'",{w:2.25},"Enter"], 4 | [{w:2.25},"Shift","Z","X","C","V","B","N","M","<\n,",">\n.","?\n/",{w:2.75},"Shift"], 5 | [{w:1.25},"Ctrl",{w:1.25},"Win",{w:1.25},"Alt",{a:7,w:6.25},"",{a:4,w:1.25},"Alt",{w:1.25},"Win",{w:1.25},"Menu",{w:1.25},"Ctrl"], 6 | [{r:15,rx:4.25,ry:2.5,y:-0.5,x:-0.5},"D"] 7 | -------------------------------------------------------------------------------- /test-data/rotated-blocks: -------------------------------------------------------------------------------- 1 | ["~\n`","!\n1","@\n2","#\n3"], 2 | [{y:-0.5,x:9.5},"*\n8","(\n9",")\n0","_\n-","+\n=",{w:2},"Backspace"], 3 | [{y:-0.5,w:1.5},"Tab","Q","W"], 4 | [{y:-0.5,x:10},"I","O","P","{\n[","}\n]",{w:1.5},"|\n\\"], 5 | [{y:-0.5,w:1.75},"Caps Lock","A","S"], 6 | [{y:-0.5,x:9.25},"J","K","L",":\n;","\"\n'",{w:2.25},"Enter"], 7 | [{y:-0.5,w:2.25},"Shift","Z"], 8 | [{y:-0.5,x:9.75},"M","<\n,",">\n.","?\n/",{w:2.75},"Shift"], 9 | [{y:-0.5,w:1.25},"Ctrl",{w:1.25},"Win",{w:1.25},"Alt"], 10 | [{y:-0.5,x:11.5,w:1.25},"Alt",{w:1.25},"Win",{w:1.25},"Menu",{w:1.25},"Ctrl"], 11 | [{r:15,rx:5,ry:3,y:-3,x:-1},"$\n4","%\n5","^\n6","&\n7"], 12 | [{x:-1.5},"E","R","T","Y","U"], 13 | [{x:-1.25},"D","F","G","H"], 14 | [{x:-1.75},"X","C","V","B","N"], 15 | [{x:-1.25,a:7,w:6.25},""] 16 | -------------------------------------------------------------------------------- /img/icons/iconmonstr-github-4.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /test-data/test-full104: -------------------------------------------------------------------------------- 1 | ["Esc",{x:1},"F1","F2","F3","F4",{x:0.5},"F5","F6","F7","F8",{x:0.5},"F9","F10","F11","F12",{x:0.25},"PrtSc","Scroll Lock","Pause\nBreak"], 2 | [{y:0.5},"~\n`","!\n1","@\n2","#\n3","$\n4","%\n5","^\n6","&\n7","*\n8","(\n9",")\n0","_\n-","+\n=",{w:2},"Backspace",{x:0.25},"Insert","Home","PgUp",{x:0.25},"Num Lock","/","*","-"], 3 | [{w:1.5},"Tab","Q","W","E","R","T","Y","U","I","O","P","{\n[","}\n]",{w:1.5},"|\n\\",{x:0.25},"Delete","End","PgDn",{x:0.25},"7\nHome","8\n↑","9\nPgUp",{h:2},"+"], 4 | [{w:1.75},"Caps Lock","A","S","D","F","G","H","J","K","L",":\n;","\"\n'",{w:2.25},"Enter",{x:3.5},"4\n←","5","6\n→"], 5 | [{w:2.25},"Shift","Z","X","C","V","B","N","M","<\n,",">\n.","?\n/",{w:2.75},"Shift",{x:1.25},"↑",{x:1.25},"1\nEnd","2\n↓","3\nPgDn",{h:2},"Enter"], 6 | [{w:1.25},"Ctrl",{w:1.25},"Win",{w:1.25},"Alt",{a:7,w:6.25},"",{a:4,w:1.25},"Alt",{w:1.25},"Win",{w:1.25},"Menu",{w:1.25},"Ctrl",{x:0.25},"←","↓","→",{x:0.25,w:2},"0\nIns",".\nDel"] 7 | -------------------------------------------------------------------------------- /.plate.dxf.swp: -------------------------------------------------------------------------------- 1 | b0nano 2.5.3ryotaWINDOWS-O1SDBU0plate.dxf -------------------------------------------------------------------------------- /test-data/test-ergo: -------------------------------------------------------------------------------- 1 | [{x:4},"#\n3",{x:6.5},"*\n8"], 2 | [{y:-0.87,x:3},"@\n2",{x:1},"$\n4",{x:4.5},"&\n7",{x:1},"(\n9"], 3 | [{y:-0.88,x:6},"%\n5",{x:2.5},"^\n6"], 4 | [{y:-0.87,x:1},"Esc","!\n1",{x:10.5},")\n0","{\n["], 5 | [{y:-0.75,a:7,h:2},"Layer Up",{x:14.5,a:4},"Back Space"], 6 | [{y:-0.63,x:4},"E",{x:6.5},"I"], 7 | [{y:-0.87,x:3},"W",{x:1},"R",{x:4.5},"U",{x:1},"O"], 8 | [{y:-0.88,x:6},"T",{x:2.5},"Y"], 9 | [{y:-0.87,x:1},"Tab","Q",{x:10.5},"P","}\n]"], 10 | [{y:-0.75,x:15.5},"|\n\\"], 11 | [{y:-0.63,x:4},"D",{x:6.5},"K"], 12 | [{y:-0.87,x:3},"S",{x:1},"F",{x:4.5},"J",{x:1},"L"], 13 | [{y:-0.88,x:6},"G",{x:2.5},"H"], 14 | [{y:-0.87,x:1},"Caps","A",{x:10.5},":\n;","\"\n'"], 15 | [{y:-0.75,a:7,h:2},"Layer Down",{x:14.5,a:4},"Enter"], 16 | [{y:-0.63,x:4},"C",{x:6.5},"<\n,"], 17 | [{y:-0.87,x:3},"X",{x:1},"V",{x:4.5},"M",{x:1},">\n."], 18 | [{y:-0.88,x:6},"B",{x:2.5},"N"], 19 | [{y:-0.87,x:1},"Shift","Z",{x:10.5},"?\n/","Print Screen"], 20 | [{y:-0.75,x:15.5},"Delete"], 21 | [{y:-0.63,x:4},"Arrow Keys Toggle",{x:6.5},"~\n`"], 22 | [{y:-0.62,x:1,w:2},"Control",{x:10.5},"_\n-","+\n="], 23 | [{y:0.22,w:3.75,d:true},"2U key positions can be replaced with 2x 1U keys on both halves."], 24 | [{rx:3,ry:5,y:-0.87},"Super"], 25 | [{rx:13.75,y:-0.87,x:-1.25},"Menu"], 26 | [{r:18,rx:5.95,ry:4.8,y:-0.5,x:-0.5},"Alt"], 27 | [{r:60,rx:9.45,ry:5.1,y:-0.5,x:-0.75,a:7,w:1.5},""], 28 | [{r:-60,rx:7.05,y:-0.5,x:-0.75,w:1.5},""], 29 | [{r:-18,rx:10.55,ry:4.8,y:-0.5,x:-0.5,a:4},"Alt"] 30 | -------------------------------------------------------------------------------- /favicon/safari-pinned-tab.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | Created by potrace 1.11, written by Peter Selinger 2001-2013 9 | 10 | 12 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .nox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | .hypothesis/ 49 | .pytest_cache/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | local_settings.py 58 | db.sqlite3 59 | 60 | # Flask stuff: 61 | instance/ 62 | .webassets-cache 63 | 64 | # Scrapy stuff: 65 | .scrapy 66 | 67 | # Sphinx documentation 68 | docs/_build/ 69 | 70 | # PyBuilder 71 | target/ 72 | 73 | # Jupyter Notebook 74 | .ipynb_checkpoints 75 | 76 | # IPython 77 | profile_default/ 78 | ipython_config.py 79 | 80 | # pyenv 81 | .python-version 82 | 83 | # celery beat schedule file 84 | celerybeat-schedule 85 | 86 | # SageMath parsed files 87 | *.sage.py 88 | 89 | # Environments 90 | .env 91 | .venv 92 | env/ 93 | venv/ 94 | ENV/ 95 | env.bak/ 96 | venv.bak/ 97 | 98 | # Spyder project settings 99 | .spyderproject 100 | .spyproject 101 | 102 | # Rope project settings 103 | .ropeproject 104 | 105 | # mkdocs documentation 106 | /site 107 | 108 | # mypy 109 | .mypy_cache/ 110 | .dmypy.json 111 | dmypy.json 112 | 113 | # Pyre type checker 114 | .pyre/ 115 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Another Keyboard Builder 2 | An exact mechanical keyboard plate creator that doesn't result in 13.99999mm cutouts 3 | 4 | ## Use it here: 5 | [kbplate.ai03.me](http://kbplate.ai03.me/) 6 | 7 | ## Why? 8 | - Alternate generators don't have either the features or exactness that designers need. 9 | - This plate generator is designed to save keyboard designers significant amounts of time, while also providing top-notch accuracy required for designs costing hundreds of dollars. 10 | 11 | ## Features 12 | - Filleting. Fillet all corners during generation so Fusion 360 doesn't have to crash 50 times later attempting it. 13 | - Exactness. No more 13.99999mm or 14.00001mm cutouts; this generator will always produce exact, accurate dimensions. 14 | - A variety of cutout options based on actual production-use dimensions. 15 | 16 | ## Usage 17 | 18 | There are two methods of use: As a CLI tool, or as a web app. 19 | 20 | #### Plategen as a standalone tool: 21 | ``` 22 | plategen.py [-h] [-ct CUTOUT_TYPE] [-cr CUTOUT_RADIUS] [-st STAB_TYPE] 23 | [-sr STAB_RADIUS] [-at ACOUSTICS_TYPE] 24 | [-ar ACOUSTICS_RADIUS] [-uw UNIT_WIDTH] [-uh UNIT_HEIGHT] 25 | [--debug-log] 26 | ``` 27 | Run `python plategen.py -h` to see detailed information on each argument. 28 | 29 | An example of generating based on raw data in a file: 30 | ``` 31 | cat kle-raw | python plategen.py > plate.dxf 32 | ``` 33 | To use the CLI tool, requirements from requirements.txt must be installed. 34 | 35 | #### Hosting: 36 | Simply run web.py with requirements from requirements-web.txt installed. 37 | 38 | ## Additional Options 39 | 40 | In addition to feeding in typical Keyboard-Layout-Editor data, custom fields may be added to fine-tune the outcomes: 41 | - `_rs:` Rotate the stabilizer cutout independently from the key. (Idea from [SwillKB Builder](https://github.com/swill/kad)) 42 | 43 | For example, for bottom row flipped spacebars, a 6.25U spacebar may have the data field `{w:6.25,_rs:180}`. 44 | 45 | - `_rc:` Similar, but for rotating the switch cutout independently of the key. 46 | 47 | ## Requirements: 48 | - Python 3.7 49 | - Everything in requirements.txt (or requirements-web.txt if hosting web service) 50 | 51 | ## Todo 52 | - More stabilizer cutout options. 53 | - More switch cutout options. 54 | - Fix certain rotated switches dying. However, this is mostly due to awful KLE syntax, so I don't plan to repair it soon. 55 | -------------------------------------------------------------------------------- /web.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, flash, request, send_from_directory, send_file 2 | 3 | import datetime 4 | import plategen 5 | import io 6 | 7 | # App config. 8 | DEBUG = True 9 | app = Flask(__name__) 10 | app.config.from_object(__name__) 11 | app.config['SECRET_KEY'] = 'change me'.encode('utf8') 12 | 13 | @app.route('/img/') 14 | def static_img(path): 15 | return send_from_directory('img', path) 16 | 17 | @app.route('/favicon/') 18 | def static_favicon(path): 19 | return send_from_directory('favicon', path) 20 | 21 | @app.route('/favicon.ico') 22 | def static_favicon_default(path): 23 | return send_from_directory('favicon/favicon.ico', path) 24 | 25 | @app.route("/", methods=['GET']) 26 | def default_route(): 27 | 28 | return render_template('base.html') 29 | 30 | @app.route("/plategen", methods=['POST']) 31 | def receive_data(): 32 | 33 | cutout_type = request.form['cutout-type'] 34 | cutout_radius = request.form['cutout-radius'] 35 | stab_type = request.form['stab-type'] 36 | stab_radius = request.form['stab-radius'] 37 | acoustic_type = request.form['acoustic-type'] 38 | acoustic_radius = request.form['acoustic-radius'] 39 | unit_width = request.form['unit-width'] 40 | unit_height = request.form['unit-height'] 41 | kle_input = request.form['kle-data'] 42 | 43 | output_data = io.StringIO() 44 | 45 | try: 46 | gen = plategen.PlateGenerator(cutout_type, cutout_radius, stab_type, stab_radius, acoustic_type, acoustic_radius, 47 | unit_width, unit_height, False) 48 | except(ValueError): 49 | flash("Enter valid integer arguments.") 50 | return render_template('base.html') 51 | 52 | out_code = gen.generate_plate(output_data, kle_input) 53 | if (out_code == 1): 54 | flash("Invalid KLE data.") 55 | return render_template('base.html') 56 | elif (out_code == 2): 57 | flash("Unsupported stabilizer cutout type.") 58 | return render_template('base.html') 59 | elif (out_code == 3): 60 | flash("Unsupported switch cutout type.") 61 | return render_template('base.html') 62 | elif (out_code == 4): 63 | flash("Switch fillet radius must be between 0 and half the cutout width/height.") 64 | return render_template('base.html') 65 | elif (out_code == 5): 66 | flash("Unit size must be between 0 and 1000mm.") 67 | return render_template('base.html') 68 | elif (out_code == 6): 69 | flash("Stablizer fillet radius must be between 0 and 5.") 70 | return render_template('base.html') 71 | elif (out_code == 7): 72 | flash("Acoustic cutout fillet radius must be between 0 and 5.") 73 | return render_template('base.html') 74 | elif (out_code == 8): 75 | flash("Unsupported stabilizer type.") 76 | return render_template('base.html') 77 | elif (out_code != 0): 78 | flash("Unspecified error.") 79 | return render_template('base.html') 80 | 81 | # Convert StringIO to BytesIO 82 | output_file = io.BytesIO() 83 | output_file.write(output_data.getvalue().encode('utf-8')) 84 | # seeking was necessary. Python 3.5.2, Flask 0.12.2 85 | output_file.seek(0) 86 | output_data.close() 87 | 88 | # Generate filename 89 | date_time = datetime.datetime.now() 90 | plate_name = 'plate-' + date_time.strftime("%Y%m%d-%H%M%S") + '.dxf' 91 | 92 | return send_file( 93 | output_file, 94 | as_attachment=True, 95 | attachment_filename=plate_name, 96 | mimetype='application/dxf' 97 | ) 98 | 99 | if (__name__ == "__main__"): 100 | app.run() -------------------------------------------------------------------------------- /templates/base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | ai03 - Keyboard Plate Generator 36 | 37 | 38 | 39 | 40 | 41 | 77 | 78 | 79 | 80 |
81 | 82 | 83 |
84 |

ai03 Plate Generator

85 |

An advanced plate generator which doesn't result in 13.99999mm cutouts.

86 |

Also fillets the cutouts so you don't have to waste hours later. Enjoy.

87 | 88 | {% with messages = get_flashed_messages(with_categories=true) %} 89 | {% if messages %} 90 | {% for message in messages %} 91 |
92 |

Error: {{ message[1] }}

93 |
94 | {% endfor %} 95 | {% endif %} 96 | {% endwith %} 97 | 98 |
99 | 100 |
101 | 102 |
103 | 104 |
105 |

KLE Raw Data

106 | 109 |
110 |
111 | 112 |
113 | 114 |
115 | 116 |
117 |

Cutout Selection

118 |

The most important part.

119 | 120 | 125 | 130 | 131 | 136 | 142 | 143 | 148 | 153 |
154 | 155 |
156 |

Filleting

157 |

Recommended 0.5~1mm for CNC, 1mm for laser-cutting.

158 |
159 | 160 | 161 |
162 |
163 | 164 | 165 |
166 |
167 | 168 | 169 |
170 |
171 | 172 |
173 |

Advanced

174 |

Best leave these alone unless you know what you are doing.

175 |
176 | 177 | 178 |
179 |
180 | 181 | 182 |
183 |
184 |
185 | 186 |
187 | 188 |
189 |
190 | 191 | 214 | 215 | 239 | 240 | 262 | 263 | 284 | 285 | 299 | 300 | 301 | 302 | 319 | 320 | 321 | -------------------------------------------------------------------------------- /plategen2.py: -------------------------------------------------------------------------------- 1 | #=================================# 2 | # Plate Generator 2 # 3 | #=================================# 4 | 5 | # By ai03 6 | # Credits to 7 | # Amtra5, Mxblue, Bakingpy, 8 | # Senter, Pwner, Kevinplus, 9 | # Peioris, Deskthority Wiki, 10 | # and any others I may have missed 11 | 12 | # Automated production of high-end mechanical keyboard plate data 13 | # No float rounding issues, pre-filleted corners, ready for production. 14 | 15 | # EXTRA SUPPORTED JSON FIELDS: 16 | # In addition to the KLE fields such as w: for width, 17 | # _rs: Rotate the stabilizers 18 | # _rc: Rotate switch cutout 19 | 20 | #=================================# 21 | # # 22 | #=================================# 23 | 24 | # Import necessities 25 | import ezdxf 26 | import sys 27 | import json5 28 | import argparse 29 | 30 | from mpmath import * 31 | from decimal import * 32 | 33 | class PlateGenerator(object): 34 | 35 | #init 36 | def __init__(self, arg_ct, arg_cr, arg_st, arg_sr, arg_at, arg_ar, arg_uw, arg_uh, arg_db): 37 | 38 | # Set up decimal and mpmath 39 | getcontext().prec = 50 40 | mp.dps = 50 41 | mp.pretty = True 42 | 43 | # Create blank dxf workspace 44 | self.plate = ezdxf.new(dxfversion='AC1024') 45 | self.modelspace = self.plate.modelspace() 46 | 47 | # Cutout type: mx, alps 48 | self.cutout_type = arg_ct 49 | 50 | # Cutout radius: The fillet radius ( 0 <= x <= 1/2 cutout width or height ) 51 | try: 52 | self.cutout_radius = Decimal(arg_cr) 53 | except: 54 | raise ValueError 55 | 56 | # Stab type: mx-simple, large-cuts, alps-aek, alps-at101 57 | self.stab_type = arg_st 58 | 59 | # Stab radius: The fillet radius for stab cutouts ( 0 <= x <= 1 ) 60 | try: 61 | self.stab_radius = Decimal(arg_sr) 62 | except: 63 | raise ValueError 64 | 65 | # Acoustic cuts: The cutouts typically found on high end plates beside the switches. 66 | # This script only handles the thin short cuts vertically beside each switch cut, not the large ones, i.e. between fn row and alphas. 67 | # none = disabled, typical = 1.5-1.75U only, extreme = On 1.5-2.75U 68 | self.acoustics_type = arg_at 69 | 70 | # Acoustic radius: Fillet radius for cuts mentioned above. 71 | try: 72 | self.acoustics_radius = Decimal(arg_ar) 73 | except: 74 | raise ValueError 75 | 76 | # Unit size (i.e. 1U = 19.05mm). ( 0 <= x <= inf, cap at 1000 for now ) 77 | try: 78 | self.unit_width = Decimal(arg_uw) 79 | except: 80 | raise ValueError 81 | try: 82 | self.unit_height = Decimal(arg_uh) 83 | except: 84 | raise ValueError 85 | 86 | 87 | #== Debug parameters ==# 88 | 89 | # The debug_log flag makes the script spit out debug info and NOT write out the plate file. 90 | self.debug_log = arg_db 91 | 92 | # Other variables 93 | self.cutout_width = Decimal("0"); 94 | self.cutout_height = Decimal("0"); 95 | 96 | #=================================# 97 | # Classes # 98 | #=================================# 99 | 100 | class Switch: 101 | 102 | def __init__(self): 103 | # These fields correspond to the respective kle data 104 | 105 | # Universal fields 106 | self.width = 1 # Width 107 | self.height = 1 # Height 108 | self.width2 = 1 # Width2: For oddly sized keys such as ISO, stepped 109 | self.height2 = 1 # Height2: For oddly sized keys such as ISO, stepped 110 | self.rotated_zone = False # Rotation zone for rotation zone (coords handled differently) 111 | 112 | # Coord fields (in mm) generated based on variables 113 | self.coord_x = 0; 114 | self.coord_y = 0; 115 | 116 | # Notice about offset fields (x: and y: fields): 117 | # They are calculated into the x/y_var or x_y_offset respectively during switch creation 118 | 119 | # Non-rotated fields 120 | self.pos_x = x_var # Implied coord X (Does not have a field in KLE) 121 | self.pos_y = y_var # Implied coord Y (Does not have a field in KLE) 122 | 123 | # Rotated fields 124 | self.rot_anchor_x = 0 # RotationX: Rotation anchor 125 | self.rot_anchor_y = 0 # RotationY: Rotation anchor 126 | self.rot_angle = 0 # Rotation: Angle 127 | self.rot_x_offset = 0 # x: X offset from anchor 128 | self.rot_y_offset = 0 # y: Y offset from anchor 129 | 130 | # Custom fields unique to plategen 131 | self.cutout_angle = 0 # Custom field for switch-independent cutout rotation 132 | self.stab_angle = 0 # Custom field for switch-independent stabilizer rotation 133 | 134 | #=================================# 135 | # DXF Blocks # 136 | #=================================# 137 | 138 | # These basically act as stamps. 139 | # Saves from having to write each and every entity individually, as was done before. 140 | 141 | def build_blocks(self): 142 | 143 | # Switch cutout 144 | block_switch_cutout = self.plate.blocks.new(name='SWITCH_CUTOUT') 145 | switch_half_width = self.cutout_width / 2 146 | switch_half_height = self.cutout_height / 2 147 | block_switch_cutout.add_line((-switch_half_width + self.cutout_radius, -switch_half_height), (switch_half_width - self.cutout_radius, -switch_half_height)) # Top 148 | block_switch_cutout.add_line((-switch_half_width + self.cutout_radius, switch_half_height), (switch_half_width - self.cutout_radius, switch_half_height)) # Bottom 149 | block_switch_cutout.add_line((-switch_half_width, -switch_half_height + self.cutout_radius), (-switch_half_width, switch_half_height - self.cutout_radius)) # Left 150 | block_switch_cutout.add_line((switch_half_width, -switch_half_height + self.cutout_radius), (switch_half_width, switch_half_height - self.cutout_radius)) # Right 151 | block_switch_cutout.add_arc((switch_half_width - self.cutout_radius, -switch_half_height + self.cutout_radius), self.cutout_radius, 0, 90) # Top right 152 | block_switch_cutout.add_arc((-switch_half_width + self.cutout_radius, -switch_half_height + self.cutout_radius), self.cutout_radius, 90, 180) # Top left 153 | block_switch_cutout.add_arc((-switch_half_width + self.cutout_radius, switch_half_height - self.cutout_radius), self.cutout_radius, 180, 270) # Bottom left 154 | block_switch_cutout.add_arc((switch_half_width - self.cutout_radius, switch_half_height - self.cutout_radius), self.cutout_radius, 270, 360) # Bottom right 155 | 156 | # Stab cutout for mx-simple 157 | # Fairly tight rectangles with width based on official spec 158 | stab_mx_simple_cutout = self.plate.blocks.new(name='STAB_MX_SIMPLE_CUTOUT') 159 | stab_mx_simple_cutout.add_line((Decimal('-3.375') + self.stab_radius, Decimal('6')), (Decimal('3.375') - self.stab_radius, Decimal('6'))) 160 | stab_mx_simple_cutout.add_line((Decimal('-3.375') + self.stab_radius, Decimal('-8')), (Decimal('3.375') - self.stab_radius, Decimal('-8'))) 161 | stab_mx_simple_cutout.add_line((Decimal('-3.375'), Decimal('6') - self.stab_radius), (Decimal('-3.375'), Decimal('-8') + self.stab_radius)) 162 | stab_mx_simple_cutout.add_line((Decimal('3.375'), Decimal('6') - self.stab_radius), (Decimal('3.375'), Decimal('-8') + self.stab_radius)) 163 | stab_mx_simple_cutout.add_arc((Decimal('-3.375') + self.stab_radius, Decimal('6') - self.stab_radius), self.stab_radius, 90, 180) 164 | stab_mx_simple_cutout.add_arc((Decimal('3.375') - self.stab_radius, Decimal('6') - self.stab_radius), self.stab_radius, 0, 90) 165 | stab_mx_simple_cutout.add_arc((Decimal('-3.375') + self.stab_radius, Decimal('-8') + self.stab_radius), self.stab_radius, 180, 270) 166 | stab_mx_simple_cutout.add_arc((Decimal('3.375') - self.stab_radius, Decimal('-8') + self.stab_radius), self.stab_radius, 270, 360) 167 | 168 | # Stab cutout for large-cuts 169 | # Large, spacious 15x7 cutouts, the recommended standard 170 | stab_large_cuts_cutout = self.plate.blocks.new(name='STAB_LARGE_CUTS_CUTOUT') 171 | stab_large_cuts_cutout.add_line((Decimal('-3.5') + self.stab_radius, Decimal('6')), (Decimal('3.5') - self.stab_radius, Decimal('6'))) 172 | stab_large_cuts_cutout.add_line((Decimal('-3.5') + self.stab_radius, Decimal('-9')), (Decimal('3.5') - self.stab_radius, Decimal('-9'))) 173 | stab_large_cuts_cutout.add_line((Decimal('-3.5'), Decimal('6') - self.stab_radius), (Decimal('-3.5'), Decimal('-9') + self.stab_radius)) 174 | stab_large_cuts_cutout.add_line((Decimal('3.5'), Decimal('6') - self.stab_radius), (Decimal('3.5'), Decimal('-9') + self.stab_radius)) 175 | stab_large_cuts_cutout.add_arc((Decimal('-3.5') + self.stab_radius, Decimal('6') - self.stab_radius), self.stab_radius, 90, 180) 176 | stab_large_cuts_cutout.add_arc((Decimal('3.5') - self.stab_radius, Decimal('6') - self.stab_radius), self.stab_radius, 0, 90) 177 | stab_large_cuts_cutout.add_arc((Decimal('-3.5') + self.stab_radius, Decimal('-9') + self.stab_radius), self.stab_radius, 180, 270) 178 | stab_large_cuts_cutout.add_arc((Decimal('3.5') - self.stab_radius, Decimal('-9') + self.stab_radius), self.stab_radius, 270, 360) 179 | 180 | # Alps cutouts 181 | # Rectangles 2.67 wide, 5.21 high. 182 | stab_alps_cutout = self.plate.blocks.new(name='STAB_ALPS_CUTOUT') 183 | stab_alps_cutout.add_line((Decimal('-1.335') + self.stab_radius, Decimal('-3.875')), (Decimal('1.335') - self.stab_radius, Decimal('-3.875'))) 184 | stab_alps_cutout.add_line((Decimal('-1.335') + self.stab_radius, Decimal('-9.085')), (Decimal('1.335') - self.stab_radius, Decimal('-9.085'))) 185 | stab_alps_cutout.add_line((Decimal('-1.335'), Decimal('-3.875') - self.stab_radius), (Decimal('-1.335'), Decimal('-9.085') + self.stab_radius)) 186 | stab_alps_cutout.add_line((Decimal('1.335'), Decimal('-3.875') - self.stab_radius), (Decimal('1.335'), Decimal('-9.085') + self.stab_radius)) 187 | stab_alps_cutout.add_arc((Decimal('-1.335') + self.stab_radius, Decimal('-3.875') - self.stab_radius), self.stab_radius, 90, 180) 188 | stab_alps_cutout.add_arc((Decimal('1.335') - self.stab_radius, Decimal('-3.875') - self.stab_radius), self.stab_radius, 0, 90) 189 | stab_alps_cutout.add_arc((Decimal('-1.335') + self.stab_radius, Decimal('-9.085') + self.stab_radius), self.stab_radius, 180, 270) 190 | stab_alps_cutout.add_arc((Decimal('1.335') - self.stab_radius, Decimal('-9.085') + self.stab_radius), self.stab_radius, 270, 360) 191 | 192 | # Acoustic cutouts 193 | acoustic_cutout = self.plate.blocks.new(name='ACOUSTIC_CUTOUT') 194 | acoustic_cutout.add_line((Decimal('-1') + self.acoustics_radius, (self.cutout_height / Decimal('2'))), (Decimal('1') - self.acoustics_radius, (self.cutout_height / Decimal('2')))) 195 | acoustic_cutout.add_line((Decimal('-1') + self.acoustics_radius, (self.cutout_height / -Decimal('2'))), (Decimal('1') - self.acoustics_radius, (self.cutout_height / -Decimal('2')))) 196 | acoustic_cutout.add_line((Decimal('-1'), (self.cutout_height / Decimal('2')) - self.acoustics_radius), (Decimal('-1'), (self.cutout_height / -Decimal('2')) + self.acoustics_radius)) 197 | acoustic_cutout.add_line((Decimal('1'), (self.cutout_height / Decimal('2')) - self.acoustics_radius), (Decimal('1'), (self.cutout_height / -Decimal('2')) + self.acoustics_radius)) 198 | acoustic_cutout.add_arc((Decimal('-1') + self.acoustics_radius, (self.cutout_height / Decimal('2')) - self.acoustics_radius), self.stab_radius, 90, 180) 199 | acoustic_cutout.add_arc((Decimal('1') - self.acoustics_radius, (self.cutout_height / Decimal('2')) - self.acoustics_radius), self.stab_radius, 0, 90) 200 | acoustic_cutout.add_arc((Decimal('-1') + self.acoustics_radius, (self.cutout_height / -Decimal('2')) + self.acoustics_radius), self.stab_radius, 180, 270) 201 | acoustic_cutout.add_arc((Decimal('1') - self.acoustics_radius, (self.cutout_height / -Decimal('2')) + self.acoustics_radius), self.stab_radius, 270, 360) 202 | 203 | 204 | #=================================# 205 | # Functions # 206 | #=================================# 207 | 208 | # Check if string is valid number 209 | # Credits to https://stackoverflow.com/questions/4138202/using-isdigit-for-floats 210 | def is_a_number(self, s): 211 | return_value = True 212 | try: 213 | test_float = float(s) 214 | except ValueError: 215 | return_value = False 216 | return return_value 217 | 218 | # Modifies a point with rotation 219 | def rotate_point_around_anchor(self, x, y, anchor_x, anchor_y, angle): 220 | radius_squared = ((x - anchor_x) ** Decimal('2')) + ((y-anchor_y) ** Decimal('2')) 221 | radius = Decimal.sqrt(radius_squared) 222 | anglefrac = angle.as_integer_ratio() 223 | radian_qty = radians(anglefrac[0]/anglefrac[1]) 224 | cos_result = Decimal(str(cos(radian_qty))) 225 | sin_result = Decimal(str(sin(radian_qty))) 226 | 227 | old_x = x - anchor_x 228 | old_y = y - anchor_y 229 | 230 | coord = matrix([float(old_x), float(old_y)]) 231 | transform = matrix([[cos(radian_qty), -sin(radian_qty)], [sin(radian_qty), cos(radian_qty)]]) 232 | result = transform * coord 233 | 234 | new_x = Decimal(str(result[0])) 235 | new_y = Decimal(str(result[1])) 236 | 237 | new_x += anchor_x 238 | new_y += anchor_y 239 | 240 | return (new_x, new_y) 241 | 242 | # Stab cutout maker 243 | # The x and y are center, like this: 244 | # 245 | # ------- 246 | # | | 247 | # | X | - - - Center Y of switch 248 | # | | 249 | # |_ _| 250 | # |_| 251 | 252 | 253 | # Acoustics cuts maker 254 | 255 | def make_acoustic_cutout(self, x, y, anchor_x, anchor_y, angle): 256 | 257 | 258 | 259 | # Calls make stab cutout based on unit width and style 260 | def generate_stabs(self, center_x, center_y, angle, unitwidth): 261 | 262 | if (self.stab_type == "mx-simple" or self.stab_type == "large-cuts"): 263 | # Switch based on unit width 264 | # These spacings are based on official mx datasheets and deskthority measurements 265 | if (unitwidth >= 8): 266 | # self.make_stab_cutout(x, y, anchor_x, anchor_y, angle) 267 | self.make_stab_cutout(center_x + Decimal('66.675'), center_y, center_x, center_y, angle) 268 | self.make_stab_cutout(center_x - Decimal('66.675'), center_y, center_x, center_y, angle) 269 | elif (unitwidth >= 7): 270 | self.make_stab_cutout(center_x + Decimal('57.15'), center_y, center_x, center_y, angle) 271 | self.make_stab_cutout(center_x - Decimal('57.15'), center_y, center_x, center_y, angle) 272 | elif (unitwidth == 6.25): 273 | self.make_stab_cutout(center_x + Decimal('50'), center_y, center_x, center_y, angle) 274 | self.make_stab_cutout(center_x - Decimal('50'), center_y, center_x, center_y, angle) 275 | elif (unitwidth == 6): 276 | #self.make_stab_cutout(center_x + Decimal('38.1'), center_y, center_x, center_y, angle) 277 | #self.make_stab_cutout(center_x - Decimal('57.15'), center_y, center_x, center_y, angle) 278 | self.make_stab_cutout(center_x - Decimal('47.625'), center_y, center_x, center_y, angle) 279 | self.make_stab_cutout(center_x - Decimal('47.625'), center_y, center_x, center_y, angle) 280 | 281 | elif (unitwidth >= 3): 282 | self.make_stab_cutout(center_x + Decimal('19.05'), center_y, center_x, center_y, angle) 283 | self.make_stab_cutout(center_x - Decimal('19.05'), center_y, center_x, center_y, angle) 284 | elif (unitwidth >= 2): 285 | self.make_stab_cutout(center_x + Decimal('11.938'), center_y, center_x, center_y, angle) 286 | self.make_stab_cutout(center_x - Decimal('11.938'), center_y, center_x, center_y, angle) 287 | if (self.acoustics_type == "extreme"): 288 | self.make_acoustic_cutout(center_x + Decimal('18.25'), center_y, center_x, center_y, angle) 289 | self.make_acoustic_cutout(center_x - Decimal('18.25'), center_y, center_x, center_y, angle) 290 | elif (unitwidth >= 1.5): 291 | if (self.acoustics_type == "typical" or (self.acoustics_type == "extreme")): 292 | self.make_acoustic_cutout(center_x + Decimal('11.6'), center_y, center_x, center_y, angle) 293 | self.make_acoustic_cutout(center_x - Decimal('11.6'), center_y, center_x, center_y, angle) 294 | elif (self.stab_type == "alps-aek"): 295 | # These are mostly based on measurements. 296 | # If someone has datasheets, please let me know 297 | if (unitwidth >= 6.5): 298 | self.make_stab_cutout(center_x + Decimal('45.3'), center_y, center_x, center_y, angle) 299 | self.make_stab_cutout(center_x - Decimal('45.3'), center_y, center_x, center_y, angle) 300 | elif (unitwidth >= 6.25): 301 | self.make_stab_cutout(center_x + Decimal('41.86'), center_y, center_x, center_y, angle) 302 | self.make_stab_cutout(center_x - Decimal('41.86'), center_y, center_x, center_y, angle) 303 | elif (unitwidth >= 2): 304 | self.make_stab_cutout(center_x + Decimal('14'), center_y, center_x, center_y, angle) 305 | self.make_stab_cutout(center_x - Decimal('14'), center_y, center_x, center_y, angle) 306 | elif (unitwidth >= 1.75): 307 | self.make_stab_cutout(center_x + Decimal('12'), center_y, center_x, center_y, angle) 308 | self.make_stab_cutout(center_x - Decimal('12'), center_y, center_x, center_y, angle) 309 | elif (self.stab_type == "alps-at101"): 310 | # These are mostly based on measurements. 311 | # If someone has datasheets, please let me know 312 | if (unitwidth >= 6.5): 313 | self.make_stab_cutout(center_x + Decimal('45.3'), center_y, center_x, center_y, angle) 314 | self.make_stab_cutout(center_x - Decimal('45.3'), center_y, center_x, center_y, angle) 315 | elif (unitwidth >= 6.25): 316 | self.make_stab_cutout(center_x + Decimal('41.86'), center_y, center_x, center_y, angle) 317 | self.make_stab_cutout(center_x - Decimal('41.86'), center_y, center_x, center_y, angle) 318 | elif (unitwidth >= 2.75): 319 | self.make_stab_cutout(center_x + Decimal('20.5'), center_y, center_x, center_y, angle) 320 | self.make_stab_cutout(center_x - Decimal('20.5'), center_y, center_x, center_y, angle) 321 | elif (unitwidth >= 2): 322 | self.make_stab_cutout(center_x + Decimal('14'), center_y, center_x, center_y, angle) 323 | self.make_stab_cutout(center_x - Decimal('14'), center_y, center_x, center_y, angle) 324 | elif (unitwidth >= 1.75): 325 | self.make_stab_cutout(center_x + Decimal('12'), center_y, center_x, center_y, angle) 326 | self.make_stab_cutout(center_x - Decimal('12'), center_y, center_x, center_y, angle) 327 | 328 | def place_switch_cutouts(self, switch): 329 | 330 | # Handle 6U 331 | if (switch.width == 6): 332 | coord = self.rotate_point_around_anchor(switch.coord_x + Decimal("9.525"), switch.coord_y, switch.coord_x, switch.coord_y, switch.rot_angle) 333 | 334 | # Place switch object 335 | self.modelspace.add_blockref('SWITCH_CUTOUT', (switch.coord_x, switch.coord_y), dxfattribs={ 336 | 'xscale': 1, 337 | 'yscale': 1, 338 | 'rotation': switch.rot_angle 339 | }) 340 | 341 | 342 | # Adjust width for vertically tall keys, and generate stabs 343 | apparent_width = switch.width; 344 | if (switch.width < switch.height): 345 | apparent_width = switch.height; 346 | switch.stab_angle += Decimal("90"); 347 | 348 | self.generate_stabs(mm_center_x, mm_center_y, switch.angle + switch.stab_angle, apparent_width) 349 | 350 | def render_switch(self, switch): 351 | 352 | # First, generate the switch's coord variables based on other fields 353 | 354 | # If non-rotated, use regular x/y_var 355 | if (switch.rotated_zone == False): 356 | 357 | switch.coord_x = self.unit_width * switch.x_var; 358 | switch.coord_y = self.unit_height * switch.y_var; 359 | 360 | # Otherwise, do some magic with rotation fields 361 | else: 362 | 363 | coords = rotate_point_around_anchor(switch.rot_x_offset, switch.rot_y_offset, switch.rot_anchor_x, switch.rot_anchor_y, switch.rot_angle) 364 | switch.coord_x = coords[0] * self.unit_width; 365 | switch.corod_y = coords[1] * self.unit_height; 366 | 367 | # TODO: self.max_width and self.max_height 368 | 369 | # Draw cutouts 370 | self.place_switch_cutouts(switch) 371 | 372 | 373 | # Generate switch cutout sizes 374 | def initialize_variables(self): 375 | if (self.cutout_type == "mx"): 376 | self.cutout_width = Decimal('14'); 377 | self.cutout_height = Decimal('14'); 378 | elif (self.cutout_type == "alps"): 379 | self.cutout_width = Decimal('15.50'); 380 | self.cutout_height = Decimal('12.80'); 381 | elif (self.cutout_type == "omron"): 382 | self.cutout_width = Decimal('13.50'); 383 | self.cutout_height = Decimal('13.50'); 384 | else: 385 | print("Unsupported cutout type.", file=sys.stderr) 386 | print("Supported: mx, alps, omron", file=sys.stderr) 387 | #exit(1) 388 | return 3 389 | 390 | # Check if values legal 391 | 392 | # Cutout radius: The fillet radius ( 0 <= x <= 1/2 width or height) 393 | if ((self.cutout_radius < 0) or (self.cutout_radius > (self.cutout_width/2)) or (self.cutout_radius > (self.cutout_height/2))) : 394 | print("Radius must be between 0 and half the cutout width/height.", file=sys.stderr) 395 | #exit(1) 396 | return 4 397 | 398 | # Unit size ( 0 <= x <= inf, cap at 1000 for now ) 399 | if (self.unit_width < 0 or self.unit_width > 1000): 400 | print("Unit size must be between 0 and 1000", file=sys.stderr) 401 | #exit(1) 402 | return 5 403 | 404 | if (self.unit_height < 0 or self.unit_height > 1000): 405 | print("Unit size must be between 0 and 1000", file=sys.stderr) 406 | #exit(1) 407 | return 5 408 | 409 | if (self.stab_radius < 0 or self.stab_radius > 5): 410 | return 6 411 | if (self.acoustics_radius < 0 or self.acoustics_radius > 5): 412 | return 7 413 | if (self.stab_type != "mx-simple" and self.stab_type != "large-cuts" and self.stab_type != "alps-aek" and self.stab_type != "alps-at101"): 414 | print("Unsupported stab type.", file=sys.stderr) 415 | print("Stab types: mx-simple, large-cuts, alps-aek, alps-at101", file=sys.stderr) 416 | #exit(1) 417 | return(8) 418 | 419 | 420 | return 0 421 | 422 | def generate_plate(self, file, input_data=None): 423 | 424 | # Init vars 425 | init_code = self.initialize_variables() 426 | if (init_code != 0): 427 | return init_code 428 | 429 | # Sanitize by removing \" (KLE's literal " for a label) 430 | #input_data = input_data.replace('\n', '') 431 | #input_data = input_data.replace(r'\"', '') 432 | 433 | # TODO: Filter out improper quotes from " being in a label! 434 | 435 | if (self.debug_log): 436 | print("Filtered input data:") 437 | print(input_data) 438 | print("") 439 | 440 | # Parse KLE data 441 | all_switches = [] 442 | 443 | # Switch to rotation parse mode once KLE enters the rotation syntax zone 444 | rotate_zone = False 445 | 446 | try: 447 | json_data = json5.loads('[' + input_data + ']') 448 | except(ValueError): 449 | #print("Invalid KLE data", file=sys.stderr) 450 | return(1) 451 | 452 | for row in json_data: 453 | if (self.debug_log): 454 | print (">>> ROW BEGIN") 455 | print (str(row)) 456 | 457 | # KLE standard supports first row being metadata. 458 | # If it is, ignore. 459 | if isinstance(row, dict): 460 | if (self.debug_log): 461 | print ("!!! Row is metadata. Skip.") 462 | continue 463 | for key in row: 464 | # The "key" can either be a legend (actual key) or dictionary of data (for succeeding key). 465 | 466 | # If it's just a string, it's just a key. Create one and add to list 467 | if isinstance(key, str): 468 | 469 | if (self.current_deco): 470 | self.reset_key_parameters() 471 | continue 472 | 473 | # First, we simply make the switch 474 | current_switch = self.Switch(self.current_x, self.current_y) 475 | 476 | # TODO: Write the key creation code 477 | 478 | all_switches.append(current_switch) 479 | 480 | # Reset the fields to their defaults 481 | self.reset_key_parameters() 482 | 483 | # Otherwise, it's a data dictionary. We must parse it properly 484 | else: 485 | for i in key: 486 | # i = The dictionary key. Not the keyboard kind of key 487 | # j = The corresponding value. 488 | j = key[i] 489 | 490 | # Large if-else chain to set params 491 | if (str(i) == "w"): 492 | # w = Width 493 | self.current_width = Decimal(str(j)) 494 | 495 | elif (str(i) == "h"): 496 | # h = Height 497 | self.current_height = Decimal(str(j)) 498 | 499 | elif (str(i) == "w2"): 500 | # w2 = Secondary width 501 | self.current_width_secondary = Decimal(str(j)) 502 | 503 | elif (str(i) == "h2"): 504 | # h2 = Secondary height 505 | self.current_height_secondary = Decimal(str(j)) 506 | 507 | elif (str(i) == "rx"): 508 | # rx = Rotation anchor x 509 | self.current_rotx = Decimal(str(j)) 510 | 511 | elif (str(i) == "ry"): 512 | # ry = Rotation anchor y 513 | self.current_roty = Decimal(str(j)) 514 | 515 | elif (str(i) == "r"): 516 | # r = Rotation angle OPPOSITE OF typical counterclockwise-from-xpositive 517 | self.current_angle = -Decimal(str(j)) 518 | 519 | elif (str(i) == "_rs"): 520 | # _rs = Rotation angle offset for stabilizer OPPOSITE OF typical counterclockwise-from-xpositive 521 | self.current_stab_angle = -Decimal(str(j)) 522 | 523 | elif (str(i) == "_rc"): 524 | # _rs = Switch cutout angle offset for stabilizer OPPOSITE OF typical counterclockwise-from-xpositive 525 | self.current_cutout_angle = -Decimal(str(j)) 526 | 527 | elif (str(i) == "x"): 528 | # x = X offset for next keys OR offset from rotation anchor (seriously kle?) 529 | self.current_offset_x = Decimal(str(j)) 530 | 531 | elif (str(i) == "y"): 532 | # y = Y offset for next keys OR offset from rotation anchor (seriously kle?) 533 | self.current_offset_y = Decimal(str(j)) 534 | 535 | elif (str(i) == "d"): 536 | # Key is decoration. 537 | self.current_deco = True 538 | 539 | # Finished row 540 | self.current_y -= Decimal('1') 541 | self.current_x = Decimal('0') 542 | 543 | 544 | # At this point, the keys are built. 545 | 546 | # Adjust max width/height from units to mm 547 | 548 | self.max_width = self.max_width * self.unit_width 549 | self.max_height = self.max_height * self.unit_height 550 | 551 | # Render each one by one. 552 | for switch in all_switches: 553 | self.render_switch(switch) 554 | 555 | # Draw outer bounds - top, bottom, left, right 556 | self.modelspace.add_line((0, 0), (self.max_width, 0)) 557 | self.modelspace.add_line((0, self.max_height), (self.max_width, self.max_height)) 558 | self.modelspace.add_line((0, 0), (0, self.max_height)) 559 | self.modelspace.add_line((self.max_width, 0), (self.max_width, self.max_height)) 560 | 561 | if (self.debug_log): 562 | print("Complete! Saving plate to specified output") 563 | 564 | if (file == "stdout"): 565 | self.plate.write(sys.stdout) 566 | else: 567 | self.plate.write(file) 568 | return 0 569 | 570 | 571 | 572 | if __name__ == "__main__": 573 | 574 | parser = argparse.ArgumentParser(description='Create a plate DXF based on KLE raw data.') 575 | 576 | # Note: The args will be fed into Decimal(), which takes strings 577 | 578 | parser.add_argument("-ct", "--cutout-type", help="Switch cutout type. Supported: mx, alps, omron; Default: mx", type=str, default='mx') 579 | parser.add_argument("-cr", "--cutout-radius", help="Switch cutout fillet radius. Default: 0.5", type=str, default='0.5') 580 | parser.add_argument("-st", "--stab-type", help="Stabilizer type. Supported: mx-simple, large-cuts, alps-aek, alps-at101; Default: mx-simple", type=str, default='mx-simple') 581 | parser.add_argument("-sr", "--stab-radius", help="Stabilizer cutout fillet radius. Default: 0.5", type=str, default='0.5') 582 | parser.add_argument("-at", "--acoustics-type", help="Acoustic cutouts type. Supported: none, typical, extreme; Default: none", type=str, default='none') 583 | parser.add_argument("-ar", "--acoustics-radius", help="Acoustic cutouts fillet radius. Default: 0.5", type=str, default='0.5') 584 | parser.add_argument("-uw", "--unit-width", help="Key unit width. Default: 19.05", type=str, default='19.05') 585 | parser.add_argument("-uh", "--unit-height", help="Key unit height. Default: 19.05", type=str, default='19.05') 586 | #parser.add_argument("-om", "--output-method", help="The save method for data. Supported: stdout, file; Default: stdout", type=str, default='stdout') 587 | #parser.add_argument("-of", "--output-file", help="Output file name if using file output-method. Default: plate.dxf", type=str, default='plate.dxf') 588 | parser.add_argument("--debug-log", help="Spam output with useless info.", action="store_true", default = False) 589 | 590 | args = parser.parse_args() 591 | 592 | gen = PlateGenerator(args.cutout_type, args.cutout_radius, args.stab_type, args.stab_radius, args.acoustics_type, args.acoustics_radius, 593 | args.unit_width, args.unit_height, args.debug_log) 594 | 595 | input_data = sys.stdin.read() 596 | gen.generate_plate("stdout", input_data) 597 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU AFFERO GENERAL PUBLIC LICENSE 2 | Version 3, 19 November 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU Affero General Public License is a free, copyleft license for 11 | software and other kinds of works, specifically designed to ensure 12 | cooperation with the community in the case of network server software. 13 | 14 | The licenses for most software and other practical works are designed 15 | to take away your freedom to share and change the works. By contrast, 16 | our General Public Licenses are intended to guarantee your freedom to 17 | share and change all versions of a program--to make sure it remains free 18 | software for all its users. 19 | 20 | When we speak of free software, we are referring to freedom, not 21 | price. Our General Public Licenses are designed to make sure that you 22 | have the freedom to distribute copies of free software (and charge for 23 | them if you wish), that you receive source code or can get it if you 24 | want it, that you can change the software or use pieces of it in new 25 | free programs, and that you know you can do these things. 26 | 27 | Developers that use our General Public Licenses protect your rights 28 | with two steps: (1) assert copyright on the software, and (2) offer 29 | you this License which gives you legal permission to copy, distribute 30 | and/or modify the software. 31 | 32 | A secondary benefit of defending all users' freedom is that 33 | improvements made in alternate versions of the program, if they 34 | receive widespread use, become available for other developers to 35 | incorporate. Many developers of free software are heartened and 36 | encouraged by the resulting cooperation. However, in the case of 37 | software used on network servers, this result may fail to come about. 38 | The GNU General Public License permits making a modified version and 39 | letting the public access it on a server without ever releasing its 40 | source code to the public. 41 | 42 | The GNU Affero General Public License is designed specifically to 43 | ensure that, in such cases, the modified source code becomes available 44 | to the community. It requires the operator of a network server to 45 | provide the source code of the modified version running there to the 46 | users of that server. Therefore, public use of a modified version, on 47 | a publicly accessible server, gives the public access to the source 48 | code of the modified version. 49 | 50 | An older license, called the Affero General Public License and 51 | published by Affero, was designed to accomplish similar goals. This is 52 | a different license, not a version of the Affero GPL, but Affero has 53 | released a new version of the Affero GPL which permits relicensing under 54 | this license. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS 60 | 61 | 0. Definitions. 62 | 63 | "This License" refers to version 3 of the GNU Affero General Public License. 64 | 65 | "Copyright" also means copyright-like laws that apply to other kinds of 66 | works, such as semiconductor masks. 67 | 68 | "The Program" refers to any copyrightable work licensed under this 69 | License. Each licensee is addressed as "you". "Licensees" and 70 | "recipients" may be individuals or organizations. 71 | 72 | To "modify" a work means to copy from or adapt all or part of the work 73 | in a fashion requiring copyright permission, other than the making of an 74 | exact copy. The resulting work is called a "modified version" of the 75 | earlier work or a work "based on" the earlier work. 76 | 77 | A "covered work" means either the unmodified Program or a work based 78 | on the Program. 79 | 80 | To "propagate" a work means to do anything with it that, without 81 | permission, would make you directly or secondarily liable for 82 | infringement under applicable copyright law, except executing it on a 83 | computer or modifying a private copy. Propagation includes copying, 84 | distribution (with or without modification), making available to the 85 | public, and in some countries other activities as well. 86 | 87 | To "convey" a work means any kind of propagation that enables other 88 | parties to make or receive copies. Mere interaction with a user through 89 | a computer network, with no transfer of a copy, is not conveying. 90 | 91 | An interactive user interface displays "Appropriate Legal Notices" 92 | to the extent that it includes a convenient and prominently visible 93 | feature that (1) displays an appropriate copyright notice, and (2) 94 | tells the user that there is no warranty for the work (except to the 95 | extent that warranties are provided), that licensees may convey the 96 | work under this License, and how to view a copy of this License. If 97 | the interface presents a list of user commands or options, such as a 98 | menu, a prominent item in the list meets this criterion. 99 | 100 | 1. Source Code. 101 | 102 | The "source code" for a work means the preferred form of the work 103 | for making modifications to it. "Object code" means any non-source 104 | form of a work. 105 | 106 | A "Standard Interface" means an interface that either is an official 107 | standard defined by a recognized standards body, or, in the case of 108 | interfaces specified for a particular programming language, one that 109 | is widely used among developers working in that language. 110 | 111 | The "System Libraries" of an executable work include anything, other 112 | than the work as a whole, that (a) is included in the normal form of 113 | packaging a Major Component, but which is not part of that Major 114 | Component, and (b) serves only to enable use of the work with that 115 | Major Component, or to implement a Standard Interface for which an 116 | implementation is available to the public in source code form. A 117 | "Major Component", in this context, means a major essential component 118 | (kernel, window system, and so on) of the specific operating system 119 | (if any) on which the executable work runs, or a compiler used to 120 | produce the work, or an object code interpreter used to run it. 121 | 122 | The "Corresponding Source" for a work in object code form means all 123 | the source code needed to generate, install, and (for an executable 124 | work) run the object code and to modify the work, including scripts to 125 | control those activities. However, it does not include the work's 126 | System Libraries, or general-purpose tools or generally available free 127 | programs which are used unmodified in performing those activities but 128 | which are not part of the work. For example, Corresponding Source 129 | includes interface definition files associated with source files for 130 | the work, and the source code for shared libraries and dynamically 131 | linked subprograms that the work is specifically designed to require, 132 | such as by intimate data communication or control flow between those 133 | subprograms and other parts of the work. 134 | 135 | The Corresponding Source need not include anything that users 136 | can regenerate automatically from other parts of the Corresponding 137 | Source. 138 | 139 | The Corresponding Source for a work in source code form is that 140 | same work. 141 | 142 | 2. Basic Permissions. 143 | 144 | All rights granted under this License are granted for the term of 145 | copyright on the Program, and are irrevocable provided the stated 146 | conditions are met. This License explicitly affirms your unlimited 147 | permission to run the unmodified Program. The output from running a 148 | covered work is covered by this License only if the output, given its 149 | content, constitutes a covered work. This License acknowledges your 150 | rights of fair use or other equivalent, as provided by copyright law. 151 | 152 | You may make, run and propagate covered works that you do not 153 | convey, without conditions so long as your license otherwise remains 154 | in force. You may convey covered works to others for the sole purpose 155 | of having them make modifications exclusively for you, or provide you 156 | with facilities for running those works, provided that you comply with 157 | the terms of this License in conveying all material for which you do 158 | not control copyright. Those thus making or running the covered works 159 | for you must do so exclusively on your behalf, under your direction 160 | and control, on terms that prohibit them from making any copies of 161 | your copyrighted material outside their relationship with you. 162 | 163 | Conveying under any other circumstances is permitted solely under 164 | the conditions stated below. Sublicensing is not allowed; section 10 165 | makes it unnecessary. 166 | 167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 168 | 169 | No covered work shall be deemed part of an effective technological 170 | measure under any applicable law fulfilling obligations under article 171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 172 | similar laws prohibiting or restricting circumvention of such 173 | measures. 174 | 175 | When you convey a covered work, you waive any legal power to forbid 176 | circumvention of technological measures to the extent such circumvention 177 | is effected by exercising rights under this License with respect to 178 | the covered work, and you disclaim any intention to limit operation or 179 | modification of the work as a means of enforcing, against the work's 180 | users, your or third parties' legal rights to forbid circumvention of 181 | technological measures. 182 | 183 | 4. Conveying Verbatim Copies. 184 | 185 | You may convey verbatim copies of the Program's source code as you 186 | receive it, in any medium, provided that you conspicuously and 187 | appropriately publish on each copy an appropriate copyright notice; 188 | keep intact all notices stating that this License and any 189 | non-permissive terms added in accord with section 7 apply to the code; 190 | keep intact all notices of the absence of any warranty; and give all 191 | recipients a copy of this License along with the Program. 192 | 193 | You may charge any price or no price for each copy that you convey, 194 | and you may offer support or warranty protection for a fee. 195 | 196 | 5. Conveying Modified Source Versions. 197 | 198 | You may convey a work based on the Program, or the modifications to 199 | produce it from the Program, in the form of source code under the 200 | terms of section 4, provided that you also meet all of these conditions: 201 | 202 | a) The work must carry prominent notices stating that you modified 203 | it, and giving a relevant date. 204 | 205 | b) The work must carry prominent notices stating that it is 206 | released under this License and any conditions added under section 207 | 7. This requirement modifies the requirement in section 4 to 208 | "keep intact all notices". 209 | 210 | c) You must license the entire work, as a whole, under this 211 | License to anyone who comes into possession of a copy. This 212 | License will therefore apply, along with any applicable section 7 213 | additional terms, to the whole of the work, and all its parts, 214 | regardless of how they are packaged. This License gives no 215 | permission to license the work in any other way, but it does not 216 | invalidate such permission if you have separately received it. 217 | 218 | d) If the work has interactive user interfaces, each must display 219 | Appropriate Legal Notices; however, if the Program has interactive 220 | interfaces that do not display Appropriate Legal Notices, your 221 | work need not make them do so. 222 | 223 | A compilation of a covered work with other separate and independent 224 | works, which are not by their nature extensions of the covered work, 225 | and which are not combined with it such as to form a larger program, 226 | in or on a volume of a storage or distribution medium, is called an 227 | "aggregate" if the compilation and its resulting copyright are not 228 | used to limit the access or legal rights of the compilation's users 229 | beyond what the individual works permit. Inclusion of a covered work 230 | in an aggregate does not cause this License to apply to the other 231 | parts of the aggregate. 232 | 233 | 6. Conveying Non-Source Forms. 234 | 235 | You may convey a covered work in object code form under the terms 236 | of sections 4 and 5, provided that you also convey the 237 | machine-readable Corresponding Source under the terms of this License, 238 | in one of these ways: 239 | 240 | a) Convey the object code in, or embodied in, a physical product 241 | (including a physical distribution medium), accompanied by the 242 | Corresponding Source fixed on a durable physical medium 243 | customarily used for software interchange. 244 | 245 | b) Convey the object code in, or embodied in, a physical product 246 | (including a physical distribution medium), accompanied by a 247 | written offer, valid for at least three years and valid for as 248 | long as you offer spare parts or customer support for that product 249 | model, to give anyone who possesses the object code either (1) a 250 | copy of the Corresponding Source for all the software in the 251 | product that is covered by this License, on a durable physical 252 | medium customarily used for software interchange, for a price no 253 | more than your reasonable cost of physically performing this 254 | conveying of source, or (2) access to copy the 255 | Corresponding Source from a network server at no charge. 256 | 257 | c) Convey individual copies of the object code with a copy of the 258 | written offer to provide the Corresponding Source. This 259 | alternative is allowed only occasionally and noncommercially, and 260 | only if you received the object code with such an offer, in accord 261 | with subsection 6b. 262 | 263 | d) Convey the object code by offering access from a designated 264 | place (gratis or for a charge), and offer equivalent access to the 265 | Corresponding Source in the same way through the same place at no 266 | further charge. You need not require recipients to copy the 267 | Corresponding Source along with the object code. If the place to 268 | copy the object code is a network server, the Corresponding Source 269 | may be on a different server (operated by you or a third party) 270 | that supports equivalent copying facilities, provided you maintain 271 | clear directions next to the object code saying where to find the 272 | Corresponding Source. Regardless of what server hosts the 273 | Corresponding Source, you remain obligated to ensure that it is 274 | available for as long as needed to satisfy these requirements. 275 | 276 | e) Convey the object code using peer-to-peer transmission, provided 277 | you inform other peers where the object code and Corresponding 278 | Source of the work are being offered to the general public at no 279 | charge under subsection 6d. 280 | 281 | A separable portion of the object code, whose source code is excluded 282 | from the Corresponding Source as a System Library, need not be 283 | included in conveying the object code work. 284 | 285 | A "User Product" is either (1) a "consumer product", which means any 286 | tangible personal property which is normally used for personal, family, 287 | or household purposes, or (2) anything designed or sold for incorporation 288 | into a dwelling. In determining whether a product is a consumer product, 289 | doubtful cases shall be resolved in favor of coverage. For a particular 290 | product received by a particular user, "normally used" refers to a 291 | typical or common use of that class of product, regardless of the status 292 | of the particular user or of the way in which the particular user 293 | actually uses, or expects or is expected to use, the product. A product 294 | is a consumer product regardless of whether the product has substantial 295 | commercial, industrial or non-consumer uses, unless such uses represent 296 | the only significant mode of use of the product. 297 | 298 | "Installation Information" for a User Product means any methods, 299 | procedures, authorization keys, or other information required to install 300 | and execute modified versions of a covered work in that User Product from 301 | a modified version of its Corresponding Source. The information must 302 | suffice to ensure that the continued functioning of the modified object 303 | code is in no case prevented or interfered with solely because 304 | modification has been made. 305 | 306 | If you convey an object code work under this section in, or with, or 307 | specifically for use in, a User Product, and the conveying occurs as 308 | part of a transaction in which the right of possession and use of the 309 | User Product is transferred to the recipient in perpetuity or for a 310 | fixed term (regardless of how the transaction is characterized), the 311 | Corresponding Source conveyed under this section must be accompanied 312 | by the Installation Information. But this requirement does not apply 313 | if neither you nor any third party retains the ability to install 314 | modified object code on the User Product (for example, the work has 315 | been installed in ROM). 316 | 317 | The requirement to provide Installation Information does not include a 318 | requirement to continue to provide support service, warranty, or updates 319 | for a work that has been modified or installed by the recipient, or for 320 | the User Product in which it has been modified or installed. Access to a 321 | network may be denied when the modification itself materially and 322 | adversely affects the operation of the network or violates the rules and 323 | protocols for communication across the network. 324 | 325 | Corresponding Source conveyed, and Installation Information provided, 326 | in accord with this section must be in a format that is publicly 327 | documented (and with an implementation available to the public in 328 | source code form), and must require no special password or key for 329 | unpacking, reading or copying. 330 | 331 | 7. Additional Terms. 332 | 333 | "Additional permissions" are terms that supplement the terms of this 334 | License by making exceptions from one or more of its conditions. 335 | Additional permissions that are applicable to the entire Program shall 336 | be treated as though they were included in this License, to the extent 337 | that they are valid under applicable law. If additional permissions 338 | apply only to part of the Program, that part may be used separately 339 | under those permissions, but the entire Program remains governed by 340 | this License without regard to the additional permissions. 341 | 342 | When you convey a copy of a covered work, you may at your option 343 | remove any additional permissions from that copy, or from any part of 344 | it. (Additional permissions may be written to require their own 345 | removal in certain cases when you modify the work.) You may place 346 | additional permissions on material, added by you to a covered work, 347 | for which you have or can give appropriate copyright permission. 348 | 349 | Notwithstanding any other provision of this License, for material you 350 | add to a covered work, you may (if authorized by the copyright holders of 351 | that material) supplement the terms of this License with terms: 352 | 353 | a) Disclaiming warranty or limiting liability differently from the 354 | terms of sections 15 and 16 of this License; or 355 | 356 | b) Requiring preservation of specified reasonable legal notices or 357 | author attributions in that material or in the Appropriate Legal 358 | Notices displayed by works containing it; or 359 | 360 | c) Prohibiting misrepresentation of the origin of that material, or 361 | requiring that modified versions of such material be marked in 362 | reasonable ways as different from the original version; or 363 | 364 | d) Limiting the use for publicity purposes of names of licensors or 365 | authors of the material; or 366 | 367 | e) Declining to grant rights under trademark law for use of some 368 | trade names, trademarks, or service marks; or 369 | 370 | f) Requiring indemnification of licensors and authors of that 371 | material by anyone who conveys the material (or modified versions of 372 | it) with contractual assumptions of liability to the recipient, for 373 | any liability that these contractual assumptions directly impose on 374 | those licensors and authors. 375 | 376 | All other non-permissive additional terms are considered "further 377 | restrictions" within the meaning of section 10. If the Program as you 378 | received it, or any part of it, contains a notice stating that it is 379 | governed by this License along with a term that is a further 380 | restriction, you may remove that term. If a license document contains 381 | a further restriction but permits relicensing or conveying under this 382 | License, you may add to a covered work material governed by the terms 383 | of that license document, provided that the further restriction does 384 | not survive such relicensing or conveying. 385 | 386 | If you add terms to a covered work in accord with this section, you 387 | must place, in the relevant source files, a statement of the 388 | additional terms that apply to those files, or a notice indicating 389 | where to find the applicable terms. 390 | 391 | Additional terms, permissive or non-permissive, may be stated in the 392 | form of a separately written license, or stated as exceptions; 393 | the above requirements apply either way. 394 | 395 | 8. Termination. 396 | 397 | You may not propagate or modify a covered work except as expressly 398 | provided under this License. Any attempt otherwise to propagate or 399 | modify it is void, and will automatically terminate your rights under 400 | this License (including any patent licenses granted under the third 401 | paragraph of section 11). 402 | 403 | However, if you cease all violation of this License, then your 404 | license from a particular copyright holder is reinstated (a) 405 | provisionally, unless and until the copyright holder explicitly and 406 | finally terminates your license, and (b) permanently, if the copyright 407 | holder fails to notify you of the violation by some reasonable means 408 | prior to 60 days after the cessation. 409 | 410 | Moreover, your license from a particular copyright holder is 411 | reinstated permanently if the copyright holder notifies you of the 412 | violation by some reasonable means, this is the first time you have 413 | received notice of violation of this License (for any work) from that 414 | copyright holder, and you cure the violation prior to 30 days after 415 | your receipt of the notice. 416 | 417 | Termination of your rights under this section does not terminate the 418 | licenses of parties who have received copies or rights from you under 419 | this License. If your rights have been terminated and not permanently 420 | reinstated, you do not qualify to receive new licenses for the same 421 | material under section 10. 422 | 423 | 9. Acceptance Not Required for Having Copies. 424 | 425 | You are not required to accept this License in order to receive or 426 | run a copy of the Program. Ancillary propagation of a covered work 427 | occurring solely as a consequence of using peer-to-peer transmission 428 | to receive a copy likewise does not require acceptance. However, 429 | nothing other than this License grants you permission to propagate or 430 | modify any covered work. These actions infringe copyright if you do 431 | not accept this License. Therefore, by modifying or propagating a 432 | covered work, you indicate your acceptance of this License to do so. 433 | 434 | 10. Automatic Licensing of Downstream Recipients. 435 | 436 | Each time you convey a covered work, the recipient automatically 437 | receives a license from the original licensors, to run, modify and 438 | propagate that work, subject to this License. You are not responsible 439 | for enforcing compliance by third parties with this License. 440 | 441 | An "entity transaction" is a transaction transferring control of an 442 | organization, or substantially all assets of one, or subdividing an 443 | organization, or merging organizations. If propagation of a covered 444 | work results from an entity transaction, each party to that 445 | transaction who receives a copy of the work also receives whatever 446 | licenses to the work the party's predecessor in interest had or could 447 | give under the previous paragraph, plus a right to possession of the 448 | Corresponding Source of the work from the predecessor in interest, if 449 | the predecessor has it or can get it with reasonable efforts. 450 | 451 | You may not impose any further restrictions on the exercise of the 452 | rights granted or affirmed under this License. For example, you may 453 | not impose a license fee, royalty, or other charge for exercise of 454 | rights granted under this License, and you may not initiate litigation 455 | (including a cross-claim or counterclaim in a lawsuit) alleging that 456 | any patent claim is infringed by making, using, selling, offering for 457 | sale, or importing the Program or any portion of it. 458 | 459 | 11. Patents. 460 | 461 | A "contributor" is a copyright holder who authorizes use under this 462 | License of the Program or a work on which the Program is based. The 463 | work thus licensed is called the contributor's "contributor version". 464 | 465 | A contributor's "essential patent claims" are all patent claims 466 | owned or controlled by the contributor, whether already acquired or 467 | hereafter acquired, that would be infringed by some manner, permitted 468 | by this License, of making, using, or selling its contributor version, 469 | but do not include claims that would be infringed only as a 470 | consequence of further modification of the contributor version. For 471 | purposes of this definition, "control" includes the right to grant 472 | patent sublicenses in a manner consistent with the requirements of 473 | this License. 474 | 475 | Each contributor grants you a non-exclusive, worldwide, royalty-free 476 | patent license under the contributor's essential patent claims, to 477 | make, use, sell, offer for sale, import and otherwise run, modify and 478 | propagate the contents of its contributor version. 479 | 480 | In the following three paragraphs, a "patent license" is any express 481 | agreement or commitment, however denominated, not to enforce a patent 482 | (such as an express permission to practice a patent or covenant not to 483 | sue for patent infringement). To "grant" such a patent license to a 484 | party means to make such an agreement or commitment not to enforce a 485 | patent against the party. 486 | 487 | If you convey a covered work, knowingly relying on a patent license, 488 | and the Corresponding Source of the work is not available for anyone 489 | to copy, free of charge and under the terms of this License, through a 490 | publicly available network server or other readily accessible means, 491 | then you must either (1) cause the Corresponding Source to be so 492 | available, or (2) arrange to deprive yourself of the benefit of the 493 | patent license for this particular work, or (3) arrange, in a manner 494 | consistent with the requirements of this License, to extend the patent 495 | license to downstream recipients. "Knowingly relying" means you have 496 | actual knowledge that, but for the patent license, your conveying the 497 | covered work in a country, or your recipient's use of the covered work 498 | in a country, would infringe one or more identifiable patents in that 499 | country that you have reason to believe are valid. 500 | 501 | If, pursuant to or in connection with a single transaction or 502 | arrangement, you convey, or propagate by procuring conveyance of, a 503 | covered work, and grant a patent license to some of the parties 504 | receiving the covered work authorizing them to use, propagate, modify 505 | or convey a specific copy of the covered work, then the patent license 506 | you grant is automatically extended to all recipients of the covered 507 | work and works based on it. 508 | 509 | A patent license is "discriminatory" if it does not include within 510 | the scope of its coverage, prohibits the exercise of, or is 511 | conditioned on the non-exercise of one or more of the rights that are 512 | specifically granted under this License. You may not convey a covered 513 | work if you are a party to an arrangement with a third party that is 514 | in the business of distributing software, under which you make payment 515 | to the third party based on the extent of your activity of conveying 516 | the work, and under which the third party grants, to any of the 517 | parties who would receive the covered work from you, a discriminatory 518 | patent license (a) in connection with copies of the covered work 519 | conveyed by you (or copies made from those copies), or (b) primarily 520 | for and in connection with specific products or compilations that 521 | contain the covered work, unless you entered into that arrangement, 522 | or that patent license was granted, prior to 28 March 2007. 523 | 524 | Nothing in this License shall be construed as excluding or limiting 525 | any implied license or other defenses to infringement that may 526 | otherwise be available to you under applicable patent law. 527 | 528 | 12. No Surrender of Others' Freedom. 529 | 530 | If conditions are imposed on you (whether by court order, agreement or 531 | otherwise) that contradict the conditions of this License, they do not 532 | excuse you from the conditions of this License. If you cannot convey a 533 | covered work so as to satisfy simultaneously your obligations under this 534 | License and any other pertinent obligations, then as a consequence you may 535 | not convey it at all. For example, if you agree to terms that obligate you 536 | to collect a royalty for further conveying from those to whom you convey 537 | the Program, the only way you could satisfy both those terms and this 538 | License would be to refrain entirely from conveying the Program. 539 | 540 | 13. Remote Network Interaction; Use with the GNU General Public License. 541 | 542 | Notwithstanding any other provision of this License, if you modify the 543 | Program, your modified version must prominently offer all users 544 | interacting with it remotely through a computer network (if your version 545 | supports such interaction) an opportunity to receive the Corresponding 546 | Source of your version by providing access to the Corresponding Source 547 | from a network server at no charge, through some standard or customary 548 | means of facilitating copying of software. This Corresponding Source 549 | shall include the Corresponding Source for any work covered by version 3 550 | of the GNU General Public License that is incorporated pursuant to the 551 | following paragraph. 552 | 553 | Notwithstanding any other provision of this License, you have 554 | permission to link or combine any covered work with a work licensed 555 | under version 3 of the GNU General Public License into a single 556 | combined work, and to convey the resulting work. The terms of this 557 | License will continue to apply to the part which is the covered work, 558 | but the work with which it is combined will remain governed by version 559 | 3 of the GNU General Public License. 560 | 561 | 14. Revised Versions of this License. 562 | 563 | The Free Software Foundation may publish revised and/or new versions of 564 | the GNU Affero General Public License from time to time. Such new versions 565 | will be similar in spirit to the present version, but may differ in detail to 566 | address new problems or concerns. 567 | 568 | Each version is given a distinguishing version number. If the 569 | Program specifies that a certain numbered version of the GNU Affero General 570 | Public License "or any later version" applies to it, you have the 571 | option of following the terms and conditions either of that numbered 572 | version or of any later version published by the Free Software 573 | Foundation. If the Program does not specify a version number of the 574 | GNU Affero General Public License, you may choose any version ever published 575 | by the Free Software Foundation. 576 | 577 | If the Program specifies that a proxy can decide which future 578 | versions of the GNU Affero General Public License can be used, that proxy's 579 | public statement of acceptance of a version permanently authorizes you 580 | to choose that version for the Program. 581 | 582 | Later license versions may give you additional or different 583 | permissions. However, no additional obligations are imposed on any 584 | author or copyright holder as a result of your choosing to follow a 585 | later version. 586 | 587 | 15. Disclaimer of Warranty. 588 | 589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 597 | 598 | 16. Limitation of Liability. 599 | 600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 608 | SUCH DAMAGES. 609 | 610 | 17. Interpretation of Sections 15 and 16. 611 | 612 | If the disclaimer of warranty and limitation of liability provided 613 | above cannot be given local legal effect according to their terms, 614 | reviewing courts shall apply local law that most closely approximates 615 | an absolute waiver of all civil liability in connection with the 616 | Program, unless a warranty or assumption of liability accompanies a 617 | copy of the Program in return for a fee. 618 | 619 | END OF TERMS AND CONDITIONS 620 | 621 | How to Apply These Terms to Your New Programs 622 | 623 | If you develop a new program, and you want it to be of the greatest 624 | possible use to the public, the best way to achieve this is to make it 625 | free software which everyone can redistribute and change under these terms. 626 | 627 | To do so, attach the following notices to the program. It is safest 628 | to attach them to the start of each source file to most effectively 629 | state the exclusion of warranty; and each file should have at least 630 | the "copyright" line and a pointer to where the full notice is found. 631 | 632 | 633 | Copyright (C) 634 | 635 | This program is free software: you can redistribute it and/or modify 636 | it under the terms of the GNU Affero General Public License as published 637 | by the Free Software Foundation, either version 3 of the License, or 638 | (at your option) any later version. 639 | 640 | This program is distributed in the hope that it will be useful, 641 | but WITHOUT ANY WARRANTY; without even the implied warranty of 642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 643 | GNU Affero General Public License for more details. 644 | 645 | You should have received a copy of the GNU Affero General Public License 646 | along with this program. If not, see . 647 | 648 | Also add information on how to contact you by electronic and paper mail. 649 | 650 | If your software can interact with users remotely through a computer 651 | network, you should also make sure that it provides a way for users to 652 | get its source. For example, if your program is a web application, its 653 | interface could display a "Source" link that leads users to an archive 654 | of the code. There are many ways you could offer source, and different 655 | solutions will be better for different programs; see section 13 for the 656 | specific requirements. 657 | 658 | You should also get your employer (if you work as a programmer) or school, 659 | if any, to sign a "copyright disclaimer" for the program, if necessary. 660 | For more information on this, and how to apply and follow the GNU AGPL, see 661 | . 662 | -------------------------------------------------------------------------------- /plategen.py: -------------------------------------------------------------------------------- 1 | #=================================# 2 | # Plate Generator # 3 | #=================================# 4 | 5 | # By ai03 6 | # Credits to 7 | # Amtra5, Mxblue, Bakingpy, 8 | # Senter, Pwner, Kevinplus, Deskthority Wiki, 9 | # and any others I may have missed 10 | 11 | # Automated production of high-end mechanical keyboard plate data 12 | # No float rounding issues, pre-filleted corners, ready for production. 13 | 14 | # EXTRA SUPPORTED JSON FIELDS: 15 | # In addition to the KLE fields such as w: for width, 16 | # _rs: Rotate the stabilizers 17 | # _rc: Rotate switch cutout 18 | 19 | #=================================# 20 | # # 21 | #=================================# 22 | 23 | # Import necessities 24 | import ezdxf 25 | import sys 26 | import json5 27 | import argparse 28 | 29 | from mpmath import * 30 | from decimal import * 31 | 32 | class PlateGenerator(object): 33 | 34 | #init 35 | def __init__(self, arg_ct, arg_cr, arg_st, arg_sr, arg_at, arg_ar, arg_uw, arg_uh, arg_db): 36 | 37 | # Set up decimal and mpmath 38 | getcontext().prec = 50 39 | mp.dps = 50 40 | mp.pretty = True 41 | 42 | # Create blank dxf workspace 43 | self.plate = ezdxf.new(dxfversion='AC1024') 44 | self.modelspace = self.plate.modelspace() 45 | 46 | # Cutout type: mx, alps 47 | self.cutout_type = arg_ct 48 | 49 | # Cutout radius: The fillet radius ( 0 <= x <= 1/2 cutout width or height ) 50 | try: 51 | self.cutout_radius = Decimal(arg_cr) 52 | except: 53 | raise ValueError 54 | 55 | # Stab type: mx-simple, large-cuts, alps-aek, alps-at101 56 | self.stab_type = arg_st 57 | 58 | # Stab radius: The fillet radius for stab cutouts ( 0 <= x <= 1 ) 59 | try: 60 | self.stab_radius = Decimal(arg_sr) 61 | except: 62 | raise ValueError 63 | 64 | # Acoustic cuts: The cutouts typically found on high end plates beside the switches. 65 | # This script only handles the thin short cuts vertically beside each switch cut, not the large ones, i.e. between fn row and alphas. 66 | # none = disabled, typical = 1.5-1.75U only, extreme = On 1.5-2.75U 67 | self.acoustics_type = arg_at 68 | 69 | # Acoustic radius: Fillet radius for cuts mentioned above. 70 | try: 71 | self.acoustics_radius = Decimal(arg_ar) 72 | except: 73 | raise ValueError 74 | 75 | # Unit size (i.e. 1U = 19.05mm). ( 0 <= x <= inf, cap at 1000 for now ) 76 | try: 77 | self.unit_width = Decimal(arg_uw) 78 | except: 79 | raise ValueError 80 | try: 81 | self.unit_height = Decimal(arg_uh) 82 | except: 83 | raise ValueError 84 | 85 | 86 | #== Debug parameters ==# 87 | 88 | # Tell user everything about what's going on and spam the console? 89 | self.debug_log = arg_db 90 | 91 | # Runtime vars that are often systematically changed or reset 92 | 93 | # Current x/y coordinates 94 | self.current_x = Decimal('0') 95 | self.current_y = Decimal('0') 96 | self.max_width = Decimal('0') 97 | self.max_height = Decimal('0') 98 | 99 | # Cutout sizes 100 | self.cutout_width = Decimal('0') 101 | self.cutout_height = Decimal('0') 102 | 103 | # Used for parsing 104 | self.reset_key_parameters() 105 | self.current_rotx = "NONE" 106 | self.current_roty = "NONE" 107 | self.current_angle = "NONE" 108 | 109 | #=================================# 110 | # Classes # 111 | #=================================# 112 | 113 | class Switch: 114 | 115 | def __init__(self, x_var, y_var): 116 | # These fields correspond to the respective kle data 117 | self.x = x_var 118 | self.y = y_var 119 | self.width = 1 120 | self.height = 1 121 | self.width_secondary = 1 122 | self.height_secondary = 1 123 | self.rotx = 0 124 | self.roty = 0 125 | self.angle = 0 126 | self.cutout_angle = 0 127 | self.stab_angle = 0 128 | self.offset_x = 0 129 | self.offset_y = 0 130 | 131 | #=================================# 132 | # Functions # 133 | #=================================# 134 | 135 | # Check if string is valid number 136 | # Credits to https://stackoverflow.com/questions/4138202/using-isdigit-for-floats 137 | def is_a_number(self, s): 138 | return_value = True 139 | try: 140 | test_float = float(s) 141 | except ValueError: 142 | return_value = False 143 | return return_value 144 | 145 | # Reset key default parameters 146 | def reset_key_parameters(self): 147 | 148 | self.current_width = Decimal('1') 149 | self.current_height = Decimal('1') 150 | self.current_width_secondary = Decimal('1') 151 | self.current_height_secondary = Decimal('1') 152 | self.current_stab_angle = Decimal('0') 153 | self.current_cutout_angle = Decimal('0') 154 | self.current_offset_x = Decimal('0') 155 | self.current_offset_y = Decimal('0') 156 | self.current_deco = False 157 | 158 | # Reset key default parameters for rotated zone 159 | def reset_rotated_key_parameters(self): 160 | 161 | self.current_width = Decimal('1') 162 | self.current_height = Decimal('1') 163 | self.current_width_secondary = Decimal('1') 164 | self.current_height_secondary = Decimal('1') 165 | self.current_stab_angle = Decimal('0') 166 | self.current_cutout_angle = Decimal('0') 167 | self.current_deco = False 168 | self.current_rotx = "UNCHANGED" 169 | self.current_roty = "UNCHANGED" 170 | self.current_angle = "UNCHANGED" 171 | 172 | # Modifies a point with rotation 173 | def rotate_point_around_anchor(self, x, y, anchor_x, anchor_y, angle): 174 | radius_squared = ((x - anchor_x) ** Decimal('2')) + ((y-anchor_y) ** Decimal('2')) 175 | radius = Decimal.sqrt(radius_squared) 176 | anglefrac = angle.as_integer_ratio() 177 | radian_qty = radians(anglefrac[0]/anglefrac[1]) 178 | cos_result = Decimal(str(cos(radian_qty))) 179 | sin_result = Decimal(str(sin(radian_qty))) 180 | 181 | old_x = x - anchor_x 182 | old_y = y - anchor_y 183 | 184 | coord = matrix([float(old_x), float(old_y)]) 185 | transform = matrix([[cos(radian_qty), -sin(radian_qty)], [sin(radian_qty), cos(radian_qty)]]) 186 | result = transform * coord 187 | 188 | new_x = Decimal(str(result[0])) 189 | new_y = Decimal(str(result[1])) 190 | 191 | new_x += anchor_x 192 | new_y += anchor_y 193 | 194 | return (new_x, new_y) 195 | 196 | # Draw line segment rotated with respect to an anchor 197 | def draw_rotated_line(self, x1, y1, x2, y2, anchor_x, anchor_y, angle): 198 | coords_1 = self.rotate_point_around_anchor(x1, y1, anchor_x, anchor_y, angle) 199 | coords_2 = self.rotate_point_around_anchor(x2, y2, anchor_x, anchor_y, angle) 200 | 201 | self.modelspace.add_line((coords_1[0], coords_1[1]), (coords_2[0], coords_2[1])) 202 | 203 | # Draw arc rotated with respect to an anchor 204 | def draw_rotated_arc(self, x, y, anchor_x, anchor_y, radius, angle_start, angle_end, rotation): 205 | coords = self.rotate_point_around_anchor(x, y, anchor_x, anchor_y, rotation) 206 | self.modelspace.add_arc((coords[0], coords[1]), radius, float(angle_start + rotation), float(angle_end + rotation)) 207 | 208 | # Stab cutout maker 209 | # The x and y are center, like this: 210 | # 211 | # ------- 212 | # | | 213 | # | X | - - - Center Y of switch 214 | # | | 215 | # |_ _| 216 | # |_| 217 | 218 | def make_stab_cutout(self, x, y, anchor_x, anchor_y, angle): 219 | 220 | line_segments = [] 221 | corners = [] 222 | 223 | if (self.stab_type == "mx-simple"): 224 | # Rectangular simplified mx cutout. 225 | # A bit larger than stock to account for fillets. 226 | 227 | line_segments.append((Decimal('-3.375') + self.stab_radius, Decimal('6'), Decimal('3.375') - self.stab_radius, Decimal('6'))) 228 | line_segments.append((Decimal('-3.375') + self.stab_radius, Decimal('-8'), Decimal('3.375') - self.stab_radius, Decimal('-8'))) 229 | line_segments.append((Decimal('-3.375'), Decimal('6') - self.stab_radius, Decimal('-3.375'), Decimal('-8') + self.stab_radius)) 230 | line_segments.append((Decimal('3.375'), Decimal('6') - self.stab_radius, Decimal('3.375'), Decimal('-8') + self.stab_radius)) 231 | 232 | corners.append((Decimal('-3.375') + self.stab_radius, Decimal('6') - self.stab_radius, 90, 180)) 233 | corners.append((Decimal('3.375') - self.stab_radius, Decimal('6') - self.stab_radius, 0, 90)) 234 | corners.append((Decimal('-3.375') + self.stab_radius, Decimal('-8') + self.stab_radius, 180, 270)) 235 | corners.append((Decimal('3.375') - self.stab_radius, Decimal('-8') + self.stab_radius, 270, 360)) 236 | 237 | elif (self.stab_type == "large-cuts"): 238 | # Large, spacious 15x7 cutouts; 1mm from mx switch cutout top 239 | 240 | line_segments.append((Decimal('-3.5') + self.stab_radius, Decimal('6'), Decimal('3.5') - self.stab_radius, Decimal('6'))) 241 | line_segments.append((Decimal('-3.5') + self.stab_radius, Decimal('-9'), Decimal('3.5') - self.stab_radius, Decimal('-9'))) 242 | line_segments.append((Decimal('-3.5'), Decimal('6') - self.stab_radius, Decimal('-3.5'), Decimal('-9') + self.stab_radius)) 243 | line_segments.append((Decimal('3.5'), Decimal('6') - self.stab_radius, Decimal('3.5'), Decimal('-9') + self.stab_radius)) 244 | 245 | corners.append((Decimal('-3.5') + self.stab_radius, Decimal('6') - self.stab_radius, 90, 180)) 246 | corners.append((Decimal('3.5') - self.stab_radius, Decimal('6') - self.stab_radius, 0, 90)) 247 | corners.append((Decimal('-3.5') + self.stab_radius, Decimal('-9') + self.stab_radius, 180, 270)) 248 | corners.append((Decimal('3.5') - self.stab_radius, Decimal('-9') + self.stab_radius, 270, 360)) 249 | 250 | elif (self.stab_type == "alps-aek" or self.stab_type == "alps-at101"): 251 | # Rectangles 2.67 wide, 5.21 high. 252 | 253 | line_segments.append((Decimal('-1.335') + self.stab_radius, Decimal('-3.875'), Decimal('1.335') - self.stab_radius, Decimal('-3.875'))) 254 | line_segments.append((Decimal('-1.335') + self.stab_radius, Decimal('-9.085'), Decimal('1.335') - self.stab_radius, Decimal('-9.085'))) 255 | line_segments.append((Decimal('-1.335'), Decimal('-3.875') - self.stab_radius, Decimal('-1.335'), Decimal('-9.085') + self.stab_radius)) 256 | line_segments.append((Decimal('1.335'), Decimal('-3.875') - self.stab_radius, Decimal('1.335'), Decimal('-9.085') + self.stab_radius)) 257 | 258 | corners.append((Decimal('-1.335') + self.stab_radius, Decimal('-3.875') - self.stab_radius, 90, 180)) 259 | corners.append((Decimal('1.335') - self.stab_radius, Decimal('-3.875') - self.stab_radius, 0, 90)) 260 | corners.append((Decimal('-1.335') + self.stab_radius, Decimal('-9.085') + self.stab_radius, 180, 270)) 261 | corners.append((Decimal('1.335') - self.stab_radius, Decimal('-9.085') + self.stab_radius, 270, 360)) 262 | 263 | else: 264 | print("Unsupported stab type.", file=sys.stderr) 265 | print("Stab types: mx-simple, large-cuts, alps-aek, alps-at101", file=sys.stderr) 266 | #exit(1) 267 | return(2) 268 | 269 | for line in line_segments: 270 | self.draw_rotated_line(x + Decimal(str(line[0])), y + Decimal(str(line[1])), x + Decimal(str(line[2])), y + Decimal(str(line[3])), anchor_x, anchor_y, angle) 271 | 272 | for arc in corners: 273 | self.draw_rotated_arc(x + Decimal(str(arc[0])), y + Decimal(str(arc[1])), anchor_x, anchor_y, self.stab_radius, arc[2], arc[3], angle) 274 | 275 | # Acoustics cuts maker 276 | 277 | def make_acoustic_cutout(self, x, y, anchor_x, anchor_y, angle): 278 | 279 | line_segments = [] 280 | corners = [] 281 | 282 | if (self.cutout_type == "mx" or self.cutout_type == "alps"): 283 | 284 | line_segments.append((Decimal('-1') + self.acoustics_radius, (self.cutout_height / Decimal('2')), Decimal('1') - self.acoustics_radius, (self.cutout_height / Decimal('2')))) 285 | line_segments.append((Decimal('-1') + self.acoustics_radius, (self.cutout_height / -Decimal('2')), Decimal('1') - self.acoustics_radius, (self.cutout_height / -Decimal('2')))) 286 | line_segments.append((Decimal('-1'), (self.cutout_height / Decimal('2')) - self.acoustics_radius, Decimal('-1'), (self.cutout_height / -Decimal('2')) + self.acoustics_radius)) 287 | line_segments.append((Decimal('1'), (self.cutout_height / Decimal('2')) - self.acoustics_radius, Decimal('1'), (self.cutout_height / -Decimal('2')) + self.acoustics_radius)) 288 | 289 | corners.append((Decimal('-1') + self.acoustics_radius, (self.cutout_height / Decimal('2')) - self.acoustics_radius, 90, 180)) 290 | corners.append((Decimal('1') - self.acoustics_radius, (self.cutout_height / Decimal('2')) - self.acoustics_radius, 0, 90)) 291 | corners.append((Decimal('-1') + self.acoustics_radius, (self.cutout_height / -Decimal('2')) + self.acoustics_radius, 180, 270)) 292 | corners.append((Decimal('1') - self.acoustics_radius, (self.cutout_height / -Decimal('2')) + self.acoustics_radius, 270, 360)) 293 | 294 | for line in line_segments: 295 | self.draw_rotated_line(x + Decimal(str(line[0])), y + Decimal(str(line[1])), x + Decimal(str(line[2])), y + Decimal(str(line[3])), anchor_x, anchor_y, angle) 296 | 297 | for arc in corners: 298 | self.draw_rotated_arc(x + Decimal(str(arc[0])), y + Decimal(str(arc[1])), anchor_x, anchor_y, self.acoustics_radius, arc[2], arc[3], angle) 299 | 300 | 301 | # Calls make stab cutout based on unit width and style 302 | def generate_stabs(self, center_x, center_y, angle, unitwidth): 303 | 304 | if (self.stab_type == "mx-simple" or self.stab_type == "large-cuts"): 305 | # Switch based on unit width 306 | # These spacings are based on official mx datasheets and deskthority measurements 307 | if (unitwidth >= 8): 308 | # self.make_stab_cutout(x, y, anchor_x, anchor_y, angle) 309 | self.make_stab_cutout(center_x + Decimal('66.675'), center_y, center_x, center_y, angle) 310 | self.make_stab_cutout(center_x - Decimal('66.675'), center_y, center_x, center_y, angle) 311 | elif (unitwidth >= 7): 312 | self.make_stab_cutout(center_x + Decimal('57.15'), center_y, center_x, center_y, angle) 313 | self.make_stab_cutout(center_x - Decimal('57.15'), center_y, center_x, center_y, angle) 314 | elif (unitwidth == 6.25): 315 | self.make_stab_cutout(center_x + Decimal('50'), center_y, center_x, center_y, angle) 316 | self.make_stab_cutout(center_x - Decimal('50'), center_y, center_x, center_y, angle) 317 | elif (unitwidth == 6): 318 | self.make_stab_cutout(center_x + Decimal('38.1'), center_y, center_x, center_y, angle) 319 | self.make_stab_cutout(center_x - Decimal('57.15'), center_y, center_x, center_y, angle) 320 | elif (unitwidth >= 3): 321 | self.make_stab_cutout(center_x + Decimal('19.05'), center_y, center_x, center_y, angle) 322 | self.make_stab_cutout(center_x - Decimal('19.05'), center_y, center_x, center_y, angle) 323 | elif (unitwidth >= 2): 324 | self.make_stab_cutout(center_x + Decimal('11.938'), center_y, center_x, center_y, angle) 325 | self.make_stab_cutout(center_x - Decimal('11.938'), center_y, center_x, center_y, angle) 326 | if (self.acoustics_type == "extreme"): 327 | self.make_acoustic_cutout(center_x + Decimal('18.25'), center_y, center_x, center_y, angle) 328 | self.make_acoustic_cutout(center_x - Decimal('18.25'), center_y, center_x, center_y, angle) 329 | elif (unitwidth >= 1.5): 330 | if (self.acoustics_type == "typical" or (self.acoustics_type == "extreme")): 331 | self.make_acoustic_cutout(center_x + Decimal('11.6'), center_y, center_x, center_y, angle) 332 | self.make_acoustic_cutout(center_x - Decimal('11.6'), center_y, center_x, center_y, angle) 333 | elif (self.stab_type == "alps-aek"): 334 | # These are mostly based on measurements. 335 | # If someone has datasheets, please let me know 336 | if (unitwidth >= 6.5): 337 | self.make_stab_cutout(center_x + Decimal('45.3'), center_y, center_x, center_y, angle) 338 | self.make_stab_cutout(center_x - Decimal('45.3'), center_y, center_x, center_y, angle) 339 | elif (unitwidth >= 6.25): 340 | self.make_stab_cutout(center_x + Decimal('41.86'), center_y, center_x, center_y, angle) 341 | self.make_stab_cutout(center_x - Decimal('41.86'), center_y, center_x, center_y, angle) 342 | elif (unitwidth >= 2): 343 | self.make_stab_cutout(center_x + Decimal('14'), center_y, center_x, center_y, angle) 344 | self.make_stab_cutout(center_x - Decimal('14'), center_y, center_x, center_y, angle) 345 | elif (unitwidth >= 1.75): 346 | self.make_stab_cutout(center_x + Decimal('12'), center_y, center_x, center_y, angle) 347 | self.make_stab_cutout(center_x - Decimal('12'), center_y, center_x, center_y, angle) 348 | elif (self.stab_type == "alps-at101"): 349 | # These are mostly based on measurements. 350 | # If someone has datasheets, please let me know 351 | if (unitwidth >= 6.5): 352 | self.make_stab_cutout(center_x + Decimal('45.3'), center_y, center_x, center_y, angle) 353 | self.make_stab_cutout(center_x - Decimal('45.3'), center_y, center_x, center_y, angle) 354 | elif (unitwidth >= 6.25): 355 | self.make_stab_cutout(center_x + Decimal('41.86'), center_y, center_x, center_y, angle) 356 | self.make_stab_cutout(center_x - Decimal('41.86'), center_y, center_x, center_y, angle) 357 | elif (unitwidth >= 2.75): 358 | self.make_stab_cutout(center_x + Decimal('20.5'), center_y, center_x, center_y, angle) 359 | self.make_stab_cutout(center_x - Decimal('20.5'), center_y, center_x, center_y, angle) 360 | elif (unitwidth >= 2): 361 | self.make_stab_cutout(center_x + Decimal('14'), center_y, center_x, center_y, angle) 362 | self.make_stab_cutout(center_x - Decimal('14'), center_y, center_x, center_y, angle) 363 | elif (unitwidth >= 1.75): 364 | self.make_stab_cutout(center_x + Decimal('12'), center_y, center_x, center_y, angle) 365 | self.make_stab_cutout(center_x - Decimal('12'), center_y, center_x, center_y, angle) 366 | 367 | 368 | # Draw switch cutout 369 | def draw_switch_cutout(self, x, y, angle): 370 | 371 | line_segments = [] 372 | corners = [] 373 | 374 | anchor_x = x; 375 | anchor_y = y; 376 | 377 | if (self.cutout_type == "mx" or self.cutout_type == "alps" or self.cutout_type == "omron"): 378 | 379 | line_segments.append(((self.cutout_width / -Decimal('2')) + self.cutout_radius, (self.cutout_height / Decimal('2')), (self.cutout_width / Decimal('2')) - self.cutout_radius, (self.cutout_height / Decimal('2')))) 380 | line_segments.append(((self.cutout_width / -Decimal('2')) + self.cutout_radius, (self.cutout_height / -Decimal('2')), (self.cutout_width / Decimal('2')) - self.cutout_radius, (self.cutout_height / -Decimal('2')))) 381 | line_segments.append(((self.cutout_width / -Decimal('2')), (self.cutout_height / Decimal('2')) - self.cutout_radius, (self.cutout_width / -Decimal('2')), (self.cutout_height / -Decimal('2')) + self.cutout_radius)) 382 | line_segments.append(((self.cutout_width / Decimal('2')), (self.cutout_height / Decimal('2')) - self.cutout_radius, (self.cutout_width / Decimal('2')), (self.cutout_height / -Decimal('2')) + self.cutout_radius)) 383 | 384 | corners.append(((self.cutout_width / -Decimal('2')) + self.cutout_radius, (self.cutout_height / Decimal('2')) - self.cutout_radius, 90, 180)) 385 | corners.append(((self.cutout_width / Decimal('2')) - self.cutout_radius, (self.cutout_height / Decimal('2')) - self.cutout_radius, 0, 90)) 386 | corners.append(((self.cutout_width / -Decimal('2')) + self.cutout_radius, (self.cutout_height / -Decimal('2')) + self.cutout_radius, 180, 270)) 387 | corners.append(((self.cutout_width / Decimal('2')) - self.cutout_radius, (self.cutout_height / -Decimal('2')) + self.cutout_radius, 270, 360)) 388 | 389 | for line in line_segments: 390 | self.draw_rotated_line(x + Decimal(str(line[0])), y + Decimal(str(line[1])), x + Decimal(str(line[2])), y + Decimal(str(line[3])), anchor_x, anchor_y, angle) 391 | 392 | for arc in corners: 393 | self.draw_rotated_arc(x + Decimal(str(arc[0])), y + Decimal(str(arc[1])), anchor_x, anchor_y, self.cutout_radius, arc[2], arc[3], angle) 394 | 395 | # TODO: Add switchtop removal cutouts, hardcoded radius to 0.5 396 | #elif (self.cutout_type == "mx-topremoval-simple"): 397 | # line_segments.append((-Decimal('7.80') + self.cutout_radius, -Decimal('7')), (Decimal('7.80') - self.cutout_radius, -Decimal('7'))) 398 | 399 | # Use the functions above to render an entire switch - Cutout, stabs, and all 400 | def render_switch(self, switch): 401 | 402 | mm_x = Decimal('0') 403 | mm_y = Decimal('0') 404 | 405 | if(self.debug_log): 406 | print("RX: " + str(switch.rotx)) 407 | print("RY: " + str(switch.roty)) 408 | print("Angle: " + str(switch.angle)) 409 | print("Offset X: " + str(switch.offset_x)) 410 | print("Offset Y: " + str(switch.offset_y)) 411 | print("===") 412 | 413 | # Coord differs for regular vs rotated 414 | if ((switch.rotx != "NONE") and (switch.roty != "NONE") or switch.angle != "NONE"): 415 | # rotx and roty are the raw base coords for anchor 416 | # Then, upper left is offset from there 417 | mm_x = (switch.rotx + switch.offset_x) * self.unit_width 418 | mm_y = (-switch.roty - switch.offset_y) * self.unit_height 419 | 420 | # Confirmed coords are correct at this point 421 | # Something going haywire after this 422 | 423 | else: 424 | # Otherwise, derive mm based on x and y in units 425 | mm_x = switch.x * self.unit_width 426 | mm_y = switch.y * self.unit_height 427 | switch.angle = Decimal("0") 428 | 429 | # Then, derive the center of the switch based on width and height 430 | mm_center_x = mm_x + ((switch.width / Decimal('2')) * self.unit_width) 431 | mm_center_y = mm_y - ((switch.height / Decimal('2')) * self.unit_height) 432 | 433 | # Then, rotate the points if angle != 0 434 | if (switch.angle != Decimal('0')): 435 | 436 | # This part is the issue 437 | 438 | rotated_upper_left_coords = self.rotate_point_around_anchor(mm_x, mm_y, (switch.rotx * self.unit_width), -(switch.roty * self.unit_height), switch.angle) 439 | rotated_central_coords = self.rotate_point_around_anchor(mm_center_x, mm_center_y, (switch.rotx * self.unit_width), -(switch.roty * self.unit_height), switch.angle) 440 | 441 | mm_x = rotated_upper_left_coords[0] 442 | mm_y = rotated_upper_left_coords[1] 443 | 444 | mm_center_x = rotated_central_coords[0] 445 | mm_center_y = rotated_central_coords[1] 446 | 447 | # Do some calculations to see if a rotated switch exceeds current max boundaries 448 | 449 | unrotated_x = (switch.rotx + switch.offset_x) * self.unit_width 450 | unrotated_y = (-switch.roty - switch.offset_y) * self.unit_height 451 | 452 | corners = [] 453 | corners.append((unrotated_x, unrotated_y)) 454 | corners.append((unrotated_x + (switch.width * self.unit_width), unrotated_y)) 455 | corners.append((unrotated_x, unrotated_y - (switch.height * self.unit_height))) 456 | corners.append((unrotated_x + (switch.width * self.unit_width), unrotated_y - (switch.height * self.unit_height))) 457 | 458 | for corner in corners: 459 | rotated_corner = self.rotate_point_around_anchor(corner[0], corner[1], mm_center_x, mm_center_y, switch.angle) 460 | 461 | if (rotated_corner[0] > self.max_width): 462 | self.max_width = rotated_corner[0]; 463 | if (rotated_corner[1] < self.max_height): 464 | self.max_height = rotated_corner[1]; 465 | 466 | # Draw main switch cutout 467 | self.draw_switch_cutout(mm_center_x, mm_center_y, switch.angle + switch.cutout_angle) 468 | 469 | # Adjust width for vertically tall keys, and generate stabs 470 | apparent_width = switch.width; 471 | if (switch.width < switch.height): 472 | apparent_width = switch.height; 473 | 474 | self.generate_stabs(mm_center_x, mm_center_y, switch.angle + switch.stab_angle, apparent_width) 475 | 476 | 477 | # Generate switch cutout sizes 478 | def initialize_variables(self): 479 | if (self.cutout_type == "mx"): 480 | self.cutout_width = Decimal('14'); 481 | self.cutout_height = Decimal('14'); 482 | elif (self.cutout_type == "alps"): 483 | self.cutout_width = Decimal('15.50'); 484 | self.cutout_height = Decimal('12.80'); 485 | elif (self.cutout_type == "omron"): 486 | self.cutout_width = Decimal('13.50'); 487 | self.cutout_height = Decimal('13.50'); 488 | else: 489 | print("Unsupported cutout type.", file=sys.stderr) 490 | print("Supported: mx, alps, omron", file=sys.stderr) 491 | #exit(1) 492 | return 3 493 | 494 | # Check if values legal 495 | 496 | # Cutout radius: The fillet radius ( 0 <= x <= 1/2 width or height) 497 | if ((self.cutout_radius < 0) or (self.cutout_radius > (self.cutout_width/2)) or (self.cutout_radius > (self.cutout_height/2))) : 498 | print("Radius must be between 0 and half the cutout width/height.", file=sys.stderr) 499 | #exit(1) 500 | return 4 501 | 502 | # Unit size ( 0 <= x <= inf, cap at 1000 for now ) 503 | if (self.unit_width < 0 or self.unit_width > 1000): 504 | print("Unit size must be between 0 and 1000", file=sys.stderr) 505 | #exit(1) 506 | return 5 507 | 508 | if (self.unit_height < 0 or self.unit_height > 1000): 509 | print("Unit size must be between 0 and 1000", file=sys.stderr) 510 | #exit(1) 511 | return 5 512 | 513 | if (self.stab_radius < 0 or self.stab_radius > 5): 514 | return 6 515 | if (self.acoustics_radius < 0 or self.acoustics_radius > 5): 516 | return 7 517 | 518 | 519 | return 0 520 | 521 | def generate_plate(self, file, input_data=None): 522 | 523 | # Init vars 524 | init_code = self.initialize_variables() 525 | if (init_code != 0): 526 | return init_code 527 | 528 | # If debug matrix is on, make sth generic 529 | if not input_data: 530 | input_data = self.debug_matrix_data 531 | 532 | # Sanitize by removing \" (KLE's literal " for a label) 533 | #input_data = input_data.replace('\n', '') 534 | #input_data = input_data.replace(r'\"', '') 535 | 536 | # TODO: Filter out improper quotes from " being in a label! 537 | 538 | if (self.debug_log): 539 | print("Filtered input data:") 540 | print(input_data) 541 | print("") 542 | 543 | # Parse KLE data 544 | all_switches = [] 545 | rotation_zone = False 546 | 547 | try: 548 | json_data = json5.loads('[' + input_data + ']') 549 | except(ValueError): 550 | #print("Invalid KLE data", file=sys.stderr) 551 | return(1) 552 | 553 | for row in json_data: 554 | if (self.debug_log): 555 | print (">>> ROW BEGIN") 556 | print (str(row)) 557 | 558 | # KLE standard supports first row being metadata. 559 | # If it is, ignore. 560 | if isinstance(row, dict): 561 | if (self.debug_log): 562 | print ("!!! Row is metadata. Skip.") 563 | continue 564 | for key in row: 565 | # The "key" can either be a legend (actual key) or dictionary of data (for succeeding key). 566 | 567 | # If it's just a string, it's just a key. Create one and add to list 568 | if isinstance(key, str): 569 | 570 | if (self.current_deco): 571 | self.reset_key_parameters() 572 | continue 573 | 574 | # First, we simply make the switch 575 | current_switch = self.Switch(self.current_x, self.current_y) 576 | 577 | # For x and y offset, check if any rotation spec is set. 578 | if (rotation_zone or self.current_rotx != "NONE" or self.current_roty != "NONE" or self.current_angle != "NONE"): 579 | 580 | if (not rotation_zone): 581 | # If first time entering rotated syntax, init values for rotation vars 582 | if (self.current_rotx == "NONE"): 583 | self.current_rotx = Decimal("0") 584 | if (self.current_roty == "NONE"): 585 | self.current_roty = Decimal("0") 586 | if (self.current_angle == "NONE"): 587 | self.current_angle = Decimal("0") 588 | rotation_zone = True 589 | 590 | # This means we RETAIN rx or ry from previous. How awful of a syntax. Seriously KLE? 591 | 592 | # Credits to Peioris to reverse engineering the syntax: 593 | 594 | # when parsing properties, you have to check the r, rx, ry values wrt to the previous values 595 | 596 | # did rx and ry change? current_x = rx; current_y = ry 597 | # did rx change but not ry? current_x = rx; current_y = 0 598 | # did r change but rx, ry did not? current_x = current_rx 599 | 600 | # It appears that in rotation syntax, the following terrible decisions are made: 601 | 602 | # - If a y: is present, it is added to whatever existing value is present (i.e. y:0.5 drops the key and any successors down 0.5U.) 603 | # This effectively signifies the beginning of a row, since all successor keys will be placed with this y as a guideline. 604 | # Also, a y: will reset the current x offset to 0. 605 | # - If a x: is present without a y:, it is appended to the previous key's position (i.e. x:0.5 skips 0.5u before placing the next key in same rotated row 606 | # - If a rx: or ry: is updated, all previous x: and y: references are ignored. 607 | # > If rx: is updated and ry is not given, ry = 0 by default. 608 | # > Similarly, if ry: is updated and rx is not given, rx = 0 by default. 609 | # - If r: is updated, rx: and ry: are presumed 0; however, the previous x: is reset, y: offset value is not discarded (i.e. if y was at 5 before, it will be 6 now) 610 | 611 | # Check for rx or ry changes 612 | if (self.current_rotx != "UNCHANGED"): 613 | self.current_x = Decimal("0") 614 | self.current_offset_y = Decimal("0") 615 | 616 | if (self.current_roty == "UNCHANGED"): 617 | self.current_roty = Decimal("0") 618 | else: 619 | self.current_rotx = all_switches[-1].rotx 620 | 621 | if (self.current_roty != "UNCHANGED"): 622 | self.current_x = Decimal("0") 623 | self.current_offset_y = Decimal("0") 624 | 625 | if (self.current_rotx == "UNCHANGED"): 626 | self.current_rotx = Decimal("0") 627 | else: 628 | self.current_roty = all_switches[-1].roty 629 | 630 | # Check for r changes 631 | if (self.current_angle != "UNCHANGED"): 632 | self.current_offset_y -= Decimal("1") 633 | self.current_offset_x = Decimal("0") 634 | else: 635 | self.current_angle = all_switches[-1].angle 636 | 637 | # - If a y: is present, reset x offset 638 | if (self.current_offset_y != 0): 639 | self.current_offset_x = Decimal("0") 640 | self.current_offset_y -= self.current_offset_y 641 | current_switch.offset_y -= self.current_offset_y 642 | # Otherwise, obtain existing offset from previous switch 643 | else: 644 | current_switch.offset_x = all_switches[-1].offset_x + Decimal("1") 645 | 646 | # Append data for x offset for current switch 647 | # self.current_offset_x += self.current_offset_x 648 | current_switch.offset_x += self.current_offset_x 649 | 650 | # Check and see if it's a y record 651 | if (self.max_height > -self.current_roty - self.current_offset_y): 652 | self.max_height = -self.current_roty - self.current_offset_y 653 | 654 | # Then, adjust the x coord for next switch 655 | self.current_offset_x += self.current_width 656 | 657 | else: 658 | # Otherwise, append 659 | self.current_x += self.current_offset_x 660 | self.current_y -= self.current_offset_y 661 | current_switch.x += self.current_offset_x 662 | current_switch.y -= self.current_offset_y 663 | self.current_offset_x = Decimal('0') 664 | self.current_offset_y = Decimal('0') 665 | 666 | # Check and see if it's a y record 667 | if (self.max_height > self.current_y - self.current_height): 668 | self.max_height = self.current_y - self.current_height 669 | 670 | # Then, adjust the x coord for next switch 671 | self.current_x += self.current_width 672 | 673 | # If this is a x record, update properly 674 | if (self.max_width < self.current_x): 675 | self.max_width = self.current_x 676 | 677 | 678 | # And we adjust the fields as necessary. 679 | # These default to 1, 0, etc unless edited by a data field preceding 680 | current_switch.width = self.current_width 681 | current_switch.height = self.current_height 682 | current_switch.width_secondary = self.current_width_secondary 683 | current_switch.height_secondary = self.current_height_secondary 684 | current_switch.stab_angle = self.current_stab_angle 685 | current_switch.cutout_angle = self.current_cutout_angle 686 | current_switch.rotx = self.current_rotx 687 | current_switch.roty = self.current_roty 688 | current_switch.angle = self.current_angle 689 | 690 | 691 | # Deal with some certain cases 692 | 693 | # For example, vertical keys created by stretching height to be larger than width 694 | # The key's cutout angle and stab angle should be offset by 90 degrees to compensate. 695 | # This effectively transforms the key to a vertical 696 | # This also handles ISO 697 | if (self.current_width < self.current_height and self.current_height >= 1.75): 698 | current_switch.cutout_angle -= Decimal('90') 699 | current_switch.stab_angle -= Decimal('90') 700 | 701 | all_switches.append(current_switch) 702 | 703 | # Reset the fields to their defaults 704 | if (rotation_zone): 705 | self.reset_rotated_key_parameters() 706 | else: 707 | self.reset_key_parameters() 708 | 709 | # Otherwise, it's a data dictionary. We must parse it properly 710 | else: 711 | for i in key: 712 | # i = The dictionary key. Not the keyboard kind of key 713 | # j = The corresponding value. 714 | j = key[i] 715 | 716 | # Large if-else chain to set params 717 | if (str(i) == "w"): 718 | # w = Width 719 | self.current_width = Decimal(str(j)) 720 | 721 | elif (str(i) == "h"): 722 | # h = Height 723 | self.current_height = Decimal(str(j)) 724 | 725 | elif (str(i) == "w2"): 726 | # w2 = Secondary width 727 | self.current_width_secondary = Decimal(str(j)) 728 | 729 | elif (str(i) == "h2"): 730 | # h2 = Secondary height 731 | self.current_height_secondary = Decimal(str(j)) 732 | 733 | elif (str(i) == "rx"): 734 | # rx = Rotation anchor x 735 | self.current_rotx = Decimal(str(j)) 736 | 737 | elif (str(i) == "ry"): 738 | # ry = Rotation anchor y 739 | self.current_roty = Decimal(str(j)) 740 | 741 | elif (str(i) == "r"): 742 | # r = Rotation angle OPPOSITE OF typical counterclockwise-from-xpositive 743 | self.current_angle = -Decimal(str(j)) 744 | 745 | elif (str(i) == "_rs"): 746 | # _rs = Rotation angle offset for stabilizer OPPOSITE OF typical counterclockwise-from-xpositive 747 | self.current_stab_angle = -Decimal(str(j)) 748 | 749 | elif (str(i) == "_rc"): 750 | # _rs = Switch cutout angle offset for stabilizer OPPOSITE OF typical counterclockwise-from-xpositive 751 | self.current_cutout_angle = -Decimal(str(j)) 752 | 753 | elif (str(i) == "x"): 754 | # x = X offset for next keys OR offset from rotation anchor (seriously kle?) 755 | self.current_offset_x = Decimal(str(j)) 756 | 757 | elif (str(i) == "y"): 758 | # y = Y offset for next keys OR offset from rotation anchor (seriously kle?) 759 | self.current_offset_y = Decimal(str(j)) 760 | 761 | elif (str(i) == "d"): 762 | # Key is decoration. 763 | self.current_deco = True 764 | 765 | # Finished row 766 | if (rotation_zone): 767 | self.current_offset_y -= Decimal("1") 768 | self.current_offset_x = Decimal("0") 769 | else: 770 | self.current_y -= Decimal('1') 771 | self.current_x = Decimal('0') 772 | 773 | 774 | # At this point, the keys are built. 775 | 776 | # Adjust max width/height from units to mm 777 | 778 | self.max_width = self.max_width * self.unit_width 779 | self.max_height = self.max_height * self.unit_height 780 | 781 | # Render each one by one. 782 | for switch in all_switches: 783 | self.render_switch(switch) 784 | 785 | # Draw outer bounds - top, bottom, left, right 786 | self.modelspace.add_line((0, 0), (self.max_width, 0)) 787 | self.modelspace.add_line((0, self.max_height), (self.max_width, self.max_height)) 788 | self.modelspace.add_line((0, 0), (0, self.max_height)) 789 | self.modelspace.add_line((self.max_width, 0), (self.max_width, self.max_height)) 790 | 791 | if (self.debug_log): 792 | print("Complete!") 793 | return 0 794 | 795 | 796 | if (file == "stdout"): 797 | self.plate.write(sys.stdout) 798 | else: 799 | self.plate.write(file) 800 | return 0 801 | 802 | 803 | 804 | if __name__ == "__main__": 805 | 806 | parser = argparse.ArgumentParser(description='Create a plate DXF based on KLE raw data.') 807 | 808 | # Note: The args will be fed into Decimal(), which takes strings 809 | 810 | parser.add_argument("-ct", "--cutout-type", help="Switch cutout type. Supported: mx, alps, omron; Default: mx", type=str, default='mx') 811 | parser.add_argument("-cr", "--cutout-radius", help="Switch cutout fillet radius. Default: 0.5", type=str, default='0.5') 812 | parser.add_argument("-st", "--stab-type", help="Stabilizer type. Supported: mx-simple, large-cuts, alps-aek, alps-at101; Default: mx-simple", type=str, default='mx-simple') 813 | parser.add_argument("-sr", "--stab-radius", help="Stabilizer cutout fillet radius. Default: 0.5", type=str, default='0.5') 814 | parser.add_argument("-at", "--acoustics-type", help="Acoustic cutouts type. Supported: none, typical, extreme; Default: none", type=str, default='none') 815 | parser.add_argument("-ar", "--acoustics-radius", help="Acoustic cutouts fillet radius. Default: 0.5", type=str, default='0.5') 816 | parser.add_argument("-uw", "--unit-width", help="Key unit width. Default: 19.05", type=str, default='19.05') 817 | parser.add_argument("-uh", "--unit-height", help="Key unit height. Default: 19.05", type=str, default='19.05') 818 | #parser.add_argument("-om", "--output-method", help="The save method for data. Supported: stdout, file; Default: stdout", type=str, default='stdout') 819 | #parser.add_argument("-of", "--output-file", help="Output file name if using file output-method. Default: plate.dxf", type=str, default='plate.dxf') 820 | parser.add_argument("--debug-log", help="Spam output with useless info.", action="store_true", default = False) 821 | 822 | args = parser.parse_args() 823 | 824 | gen = PlateGenerator(args.cutout_type, args.cutout_radius, args.stab_type, args.stab_radius, args.acoustics_type, args.acoustics_radius, 825 | args.unit_width, args.unit_height, args.debug_log) 826 | 827 | input_data = sys.stdin.read() 828 | gen.generate_plate("stdout", input_data) 829 | --------------------------------------------------------------------------------