├── .gitignore ├── LICENSE ├── README.md ├── __init__.py ├── fp-info-cache ├── round_tracks.png ├── round_tracks_action.py ├── round_tracks_gui.py ├── round_tracks_ui.fbp └── round_tracks_utils.py /.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 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kicad-round-tracks 2 | A plugin to round tracks in PcbNew 3 | 4 | ![Screenshot from 2019-05-10 21-15-53](https://user-images.githubusercontent.com/22014799/57551335-068a5480-7369-11e9-8e1b-37698e389c60.png) 5 | [before](https://user-images.githubusercontent.com/22014799/57551336-08ecae80-7369-11e9-86a9-857060306bef.png) 6 | 7 | ## Installation 8 | Clone or unzip this repository in a KiCad plugin folder : 9 | 10 | - On linux : 11 | - `/usr/share/kicad/scripting/plugins/` 12 | - `~/.kicad/scripting/plugins` 13 | - `~/.kicad_plugins/` 14 | - On macOS : 15 | - `/Applications/kicad/Kicad/Contents/SharedSupport/scripting/plugins` 16 | - `~/Library/Application Support/kicad/scripting/plugins` 17 | 18 | ## Usage 19 | In the app menu `Tools > External plugin` click `Round tracks (new file)`. 20 | ![Screenshot from 2019-05-14 13-08-01](https://user-images.githubusercontent.com/22014799/57694107-2cef0f00-764b-11e9-9bee-effcec4e3e5a.png) 21 | You can tweak settings for each Net Class. 22 | 23 | You can choose to make the modification in place or in a duplicate file (default). 24 | This plugin works by approximating arcs with many small segments, making PcbNew laggy and track modifications extremely difficult after the plugin has been run. I hope this plugin become eventually obsolete with the futurs enhancements of KiCad. 25 | 26 | The new file has the same name as the old except the extension is `.kicad_pcb-rounded`. 27 | You may want to verify that nothing is broken by running the DRC before plotting your board. 28 | All zones are refilled after the process. 29 | 30 | ## History and licence 31 | 32 | Licensed under the Apache License, Version 2.0 (the "License"); 33 | you may not use this file except in compliance with the License. 34 | You may obtain a copy of the License at 35 | http://www.apache.org/licenses/LICENSE-2.0 36 | Unless required by applicable law or agreed to in writing, software 37 | distributed under the License is distributed on an "AS IS" BASIS, 38 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 39 | See the License for the specific language governing permissions and 40 | limitations under the License. 41 | 42 | Original copyright Miles McCoo, 2017 43 | extensively modified by Julian Loiacono, Oct 2018 44 | multi-layer support and repacked as action plugin by Antoine Pintout, May 2019 45 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | from .round_tracks_action import ActionRoundTracks 2 | ActionRoundTracks().register() -------------------------------------------------------------------------------- /fp-info-cache: -------------------------------------------------------------------------------- 1 | 0 2 | -------------------------------------------------------------------------------- /round_tracks.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stimulu/kicad-round-tracks/7c7d5eebb85812683e6722ac12b401faf7e77ab2/round_tracks.png -------------------------------------------------------------------------------- /round_tracks_action.py: -------------------------------------------------------------------------------- 1 | # Original example Copyright [2017] [Miles McCoo] 2 | # Licensed under the Apache License, Version 2.0 (the "License"); 3 | # you may not use this file except in compliance with the License. 4 | # You may obtain a copy of the License at 5 | 6 | # http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | # Unless required by applicable law or agreed to in writing, software 9 | # distributed under the License is distributed on an "AS IS" BASIS, 10 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | # See the License for the specific language governing permissions and 12 | # limitations under the License. 13 | 14 | # extensively modified by Julian Loiacono, Oct 2018 15 | # updated and repacked as action plugin by Antoine Pintout, May 2019 16 | 17 | # This script will round PCBNEW traces by shortening traces at intersections, 18 | # and filling new traces between. This process is repeated four times. 19 | # The resulting board is saved in a new file. 20 | 21 | import pcbnew 22 | import math 23 | import os 24 | import wx 25 | from subprocess import Popen 26 | from .round_tracks_utils import * 27 | from .round_tracks_gui import RoundTracksDialog 28 | from pprint import pprint 29 | 30 | PERCENT_DEFAULT = 0.5 31 | PASSES_DEFAULT = 4 32 | 33 | class RoundTracks(RoundTracksDialog): 34 | 35 | def __init__(self, board, action): 36 | super(RoundTracks, self).__init__(None) 37 | self.board = board 38 | self.configfilepath = ".".join(self.board.GetFileName().split('.')[:-1])+".round-tracks-config" 39 | self.action = action 40 | self.config = {} 41 | self.load_config() 42 | c = self.config['classes'] 43 | if "Default" not in c: 44 | self.netclasslist.AppendItem( ["Default", True, str(PERCENT_DEFAULT), str(PERCENT_DEFAULT), str(PASSES_DEFAULT)]) 45 | else: 46 | self.netclasslist.AppendItem( ["Default", c['Default']['do_round'], str(c['Default']['scaling']), str(c['Default']['max_length']), str(c['Default']['passes'])]) 47 | for class_id in self.board.GetNetClasses().NetClasses(): 48 | classname = str(class_id) 49 | if classname not in c: 50 | self.netclasslist.AppendItem( [classname, True, str(PERCENT_DEFAULT), str(PERCENT_DEFAULT), str(PASSES_DEFAULT)]) 51 | else: 52 | self.netclasslist.AppendItem( [classname, c[classname]['do_round'], str(c[classname]['scaling']), str(c[classname]['max_length']), str(c[classname]['passes'])]) 53 | self.validate_all_data() 54 | 55 | 56 | def run( self, event ): 57 | # save a copy of the board 58 | 59 | self.validate_all_data() 60 | self.save_config() 61 | self.Destroy() 62 | if self.do_create.IsChecked() : 63 | new_name = self.board.GetFileName()+"-rounded" 64 | self.board.Save(new_name) 65 | self.board = pcbnew.LoadBoard(new_name, pcbnew.IO_MGR.KICAD_SEXP) 66 | 67 | classes = self.config['classes'] 68 | for classname in classes: 69 | if classes[classname]['do_round']: 70 | for i in range(classes[classname]['passes']): 71 | self.addIntermediateTracks(self.board, maxlength = classes[classname]['max_length'], scaling = classes[classname]['scaling'], netclass = classname) 72 | 73 | RebuildAllZones(self.board) 74 | 75 | if self.do_create.IsChecked(): 76 | self.board.Save(new_name) 77 | if self.do_create.IsChecked() and self.do_open.IsChecked(): 78 | Popen(['pcbnew', new_name]) 79 | 80 | def on_toggle_create( self, event ): 81 | self.do_open.Enable(self.do_create.IsChecked()) 82 | 83 | def on_close( self, event ): 84 | self.Destroy() 85 | 86 | def on_item_editing( self, event ): 87 | self.validate_all_data() 88 | 89 | def load_config(self): 90 | new_config = {} 91 | if os.path.isfile(self.configfilepath): 92 | with open(self.configfilepath, "r") as configfile: 93 | for line in configfile.readlines(): 94 | params = line[:-1].split("\t") 95 | new_config_line = {} 96 | try: 97 | new_config_line['do_round'] = params[1] == "True" 98 | new_config_line['scaling'] = float(params[2]) 99 | new_config_line['max_length'] = float(params[3]) 100 | new_config_line['passes'] = int(params[4]) 101 | new_config[params[0]] = new_config_line 102 | except Exception as e: 103 | pass 104 | self.config['classes'] = new_config 105 | 106 | def save_config(self): 107 | classes = self.config['classes'] 108 | with open(self.configfilepath, "w") as configfile: 109 | for classname in classes: 110 | configfile.write('%s\t%s\t%s\t%s\t%s\n' % (classname, str(classes[classname]['do_round']), str(classes[classname]['scaling']), str(classes[classname]['max_length']), str(classes[classname]['passes']))) 111 | pass 112 | 113 | def validate_all_data (self): 114 | new_config = {} 115 | for i in range(self.netclasslist.GetItemCount()): 116 | for j in range(5): 117 | if j is 2 or j is 3: 118 | # param should be between 0 and 1 119 | try: 120 | tested_val = float(self.netclasslist.GetTextValue(i, j)) 121 | if tested_val < 0 or tested_val > 1: 122 | self.netclasslist.SetTextValue(str(PERCENT_DEFAULT), i, j) 123 | except Exception as e: 124 | self.netclasslist.SetTextValue(str(PERCENT_DEFAULT), i, j) 125 | if j is 4: 126 | # param should be between int 1 and 5 127 | try: 128 | tested_val = int(self.netclasslist.GetTextValue(i, j)) 129 | if tested_val < 0 or tested_val > 5: 130 | self.netclasslist.SetTextValue(str(PASSES_DEFAULT), i, j) 131 | except Exception as e: 132 | self.netclasslist.SetTextValue(str(PASSES_DEFAULT), i, j) 133 | new_config[self.netclasslist.GetTextValue(i, 0)] = { 134 | 'do_round' : self.netclasslist.GetToggleValue(i, 1), 135 | 'scaling' : float(self.netclasslist.GetTextValue(i, 2)), 136 | 'max_length' : float(self.netclasslist.GetTextValue(i, 3)), 137 | 'passes' : int(self.netclasslist.GetTextValue(i, 4)) 138 | } 139 | self.config['classes'] = new_config 140 | 141 | def addIntermediateTracks( self, board, maxlength = 0.5, scaling = 0.5, netclass = None): 142 | 143 | MAXLENGTH = maxlength # % of length of track used for the arc 144 | SCALING = scaling # radius 145 | SCALE = 1000000.0 146 | 147 | # returns a dictionary netcode:netinfo_item 148 | netcodes = board.GetNetsByNetcode() 149 | 150 | # list off all of the nets in the board. 151 | for netcode, net in netcodes.items(): 152 | 153 | # print(net.GetName(), net.GetClassName()) 154 | 155 | if netclass is not None and netclass == net.GetClassName(): 156 | 157 | #print("netcode {}, name {}".format(netcode, net.GetNetname())) 158 | allTracks = board.TracksInNet(net.GetNet()) # get all the tracks in this net 159 | 160 | tracksPerLayer = {} 161 | # separate track by layer 162 | for t in allTracks: 163 | layer = t.GetLayer() 164 | if t.GetStart() != t.GetEnd(): # ignore vias 165 | if layer not in tracksPerLayer: 166 | tracksPerLayer[layer] = [] 167 | tracksPerLayer[layer].append(t) 168 | 169 | for layer in tracksPerLayer: 170 | tracks = tracksPerLayer[layer] 171 | 172 | #add all the possible intersections to a unique set, for iterating over later 173 | intersections = set(); 174 | endings = set(); 175 | for t1 in range(len(tracks)): 176 | for t2 in range(t1+1, len(tracks)): 177 | #check if these two tracks share an endpoint 178 | # reduce it to a 2-part tuple so there are not multiple objects of the same point in the set 179 | if(tracks[t1].GetLayer() == tracks[t2].GetLayer()): 180 | if(tracks[t1].IsPointOnEnds(tracks[t2].GetStart())): 181 | intersections.add((tracks[t2].GetStart().x, tracks[t2].GetStart().y)) 182 | if(tracks[t1].IsPointOnEnds(tracks[t2].GetEnd())): 183 | intersections.add((tracks[t2].GetEnd().x, tracks[t2].GetEnd().y)) 184 | 185 | #for each remaining intersection, shorten each track by the same amount, and place a track between. 186 | tracksToAdd = [] 187 | tracksToShorten = set() 188 | for ip in intersections: 189 | (newX, newY) = ip; 190 | intersection = pcbnew.wxPoint(newX, newY) 191 | tracksHere = []; 192 | for t1 in tracks: 193 | # it seems vias are treated as tracks ???? this should take care of that 194 | if(t1.GetLength() > 0): 195 | if similarPoints(t1.GetStart(), intersection): 196 | tracksHere.append(t1) 197 | if similarPoints(t1.GetEnd(), intersection): 198 | #flip track such that all tracks start at the IP 199 | reverseTrack(t1) 200 | tracksHere.append(t1) 201 | 202 | 203 | # sometimes tracksHere is empty ??? 204 | if len(tracksHere) == 0: 205 | continue 206 | 207 | #sort tracks by length, just to find the shortest 208 | tracksHere.sort(key = GetTrackLength) 209 | #shorten all tracks by the same length, which is a function of existing shortest path length 210 | shortenLength = min(tracksHere[0].GetLength() * MAXLENGTH, SCALING*SCALE) 211 | 212 | #sort these tracks by angle, so new tracks can be drawn between them 213 | tracksHere.sort(key = getTrackAngle) 214 | #shorten all these tracks 215 | for t1 in range(len(tracksHere)): 216 | shortenTrack(tracksHere[t1], shortenLength) 217 | #connect the new startpoints in a circle around the old center point 218 | for t1 in range(len(tracksHere)): 219 | #dont add 2 new tracks in the 2 track case 220 | if not (len(tracksHere) == 2 and t1 == 1): 221 | newPoint1 = cloneWxPoint(tracksHere[t1].GetStart()) 222 | newPoint2 = cloneWxPoint(tracksHere[(t1+1)%len(tracksHere)].GetStart()) 223 | tracksToAdd.append((newPoint1, newPoint2, tracksHere[t1].GetWidth(), tracksHere[t1].GetLayer(), tracksHere[t1].GetNet())) 224 | 225 | #add all the new tracks in post, so as not to cause problems with set iteration 226 | for trackpoints in tracksToAdd: 227 | (sp, ep, width, layer, net) = trackpoints 228 | 229 | track = pcbnew.TRACK(board) 230 | track.SetStart(sp) 231 | track.SetEnd(ep) 232 | track.SetWidth(width) 233 | track.SetLayer(layer) 234 | board.Add(track) 235 | track.SetNet(net) 236 | 237 | 238 | class ActionRoundTracks( pcbnew.ActionPlugin ): 239 | 240 | def defaults( self ): 241 | self.name = "Round the tracks (new file)" 242 | self.category = "Modify PCB" 243 | self.description = "Will round all tracks on the PCB" 244 | self.icon_file_name = os.path.join(os.path.dirname(__file__), "./round_tracks.png") 245 | 246 | def Run( self ): 247 | board = pcbnew.GetBoard() 248 | rt = RoundTracks(board, self) 249 | rt.ShowModal() 250 | 251 | -------------------------------------------------------------------------------- /round_tracks_gui.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | ########################################################################### 4 | ## Python code generated with wxFormBuilder (version Oct 26 2018) 5 | ## http://www.wxformbuilder.org/ 6 | ## 7 | ## PLEASE DO *NOT* EDIT THIS FILE! 8 | ########################################################################### 9 | 10 | import wx 11 | import wx.xrc 12 | import wx.dataview 13 | 14 | ########################################################################### 15 | ## Class RoundTracksDialog 16 | ########################################################################### 17 | 18 | class RoundTracksDialog ( wx.Dialog ): 19 | 20 | def __init__( self, parent ): 21 | wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = u"Round Tracks", pos = wx.DefaultPosition, size = wx.DefaultSize, style = wx.DEFAULT_DIALOG_STYLE ) 22 | 23 | self.SetSizeHints( wx.Size( 510,300 ), wx.DefaultSize ) 24 | 25 | grid_vert = wx.FlexGridSizer( 2, 1, 0, 0 ) 26 | grid_vert.AddGrowableCol( 0 ) 27 | grid_vert.AddGrowableRow( 0 ) 28 | grid_vert.SetFlexibleDirection( wx.BOTH ) 29 | grid_vert.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED ) 30 | 31 | self.netclasslist = wx.dataview.DataViewListCtrl( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 ) 32 | self.netclasslist.SetMinSize( wx.Size( 100,100 ) ) 33 | 34 | self.net_class = self.netclasslist.AppendTextColumn( u"Net Class", wx.dataview.DATAVIEW_CELL_INERT, 160, wx.ALIGN_LEFT, wx.dataview.DATAVIEW_COL_RESIZABLE|wx.dataview.DATAVIEW_COL_SORTABLE ) 35 | self.do_rounding = self.netclasslist.AppendToggleColumn( u"Rounding", wx.dataview.DATAVIEW_CELL_ACTIVATABLE, 80, wx.ALIGN_CENTER, wx.dataview.DATAVIEW_COL_SORTABLE ) 36 | self.scaling = self.netclasslist.AppendTextColumn( u"Scaling %", wx.dataview.DATAVIEW_CELL_EDITABLE, 80, wx.ALIGN_LEFT, wx.dataview.DATAVIEW_COL_SORTABLE ) 37 | self.max_length = self.netclasslist.AppendTextColumn( u"Length %", wx.dataview.DATAVIEW_CELL_EDITABLE, 80, wx.ALIGN_LEFT, wx.dataview.DATAVIEW_COL_SORTABLE ) 38 | self.passes = self.netclasslist.AppendTextColumn( u"Passes #", wx.dataview.DATAVIEW_CELL_EDITABLE, 80, wx.ALIGN_LEFT, wx.dataview.DATAVIEW_COL_SORTABLE ) 39 | grid_vert.Add( self.netclasslist, 1, wx.EXPAND|wx.TOP|wx.RIGHT|wx.LEFT, 5 ) 40 | 41 | grid_horiz = wx.FlexGridSizer( 0, 4, 0, 0 ) 42 | grid_horiz.AddGrowableCol( 2 ) 43 | grid_horiz.SetFlexibleDirection( wx.BOTH ) 44 | grid_horiz.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_SPECIFIED ) 45 | 46 | self.do_create = wx.CheckBox( self, wx.ID_ANY, u"create a new file", wx.DefaultPosition, wx.DefaultSize, 0 ) 47 | self.do_create.SetValue(True) 48 | grid_horiz.Add( self.do_create, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT, 5 ) 49 | 50 | self.do_open = wx.CheckBox( self, wx.ID_ANY, u"open the newly created file", wx.DefaultPosition, wx.DefaultSize, 0 ) 51 | self.do_open.SetValue(True) 52 | grid_horiz.Add( self.do_open, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT|wx.ALL, 5 ) 53 | 54 | 55 | grid_horiz.Add( ( 0, 0), 1, wx.EXPAND, 5 ) 56 | 57 | self.apply = wx.Button( self, wx.ID_ANY, u"Run", wx.DefaultPosition, wx.Size( 120,-1 ), 0 ) 58 | 59 | self.apply.SetDefault() 60 | grid_horiz.Add( self.apply, 0, wx.ALL|wx.EXPAND, 5 ) 61 | 62 | 63 | grid_vert.Add( grid_horiz, 1, wx.EXPAND, 5 ) 64 | 65 | 66 | self.SetSizer( grid_vert ) 67 | self.Layout() 68 | grid_vert.Fit( self ) 69 | 70 | self.Centre( wx.BOTH ) 71 | 72 | # Connect Events 73 | self.Bind( wx.EVT_CLOSE, self.on_close ) 74 | self.netclasslist.Bind( wx.dataview.EVT_DATAVIEW_ITEM_EDITING_DONE, self.on_item_editing, id = wx.ID_ANY ) 75 | self.do_create.Bind( wx.EVT_CHECKBOX, self.on_toggle_create ) 76 | self.apply.Bind( wx.EVT_BUTTON, self.run ) 77 | 78 | def __del__( self ): 79 | pass 80 | 81 | 82 | # Virtual event handlers, overide them in your derived class 83 | def on_close( self, event ): 84 | event.Skip() 85 | 86 | def on_item_editing( self, event ): 87 | event.Skip() 88 | 89 | def on_toggle_create( self, event ): 90 | event.Skip() 91 | 92 | def run( self, event ): 93 | event.Skip() 94 | 95 | 96 | -------------------------------------------------------------------------------- /round_tracks_ui.fbp: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ; 6 | Python 7 | 1 8 | source_name 9 | 0 10 | 0 11 | res 12 | UTF-8 13 | connect 14 | round_tracks_gui 15 | 1000 16 | none 17 | 0 18 | 0 19 | RoundTracks 20 | 21 | . 22 | 23 | 1 24 | 1 25 | 1 26 | 1 27 | UI 28 | 0 29 | 0 30 | 31 | 0 32 | wxAUI_MGR_DEFAULT 33 | 34 | wxBOTH 35 | 36 | 1 37 | 1 38 | impl_virtual 39 | 40 | 41 | 42 | 0 43 | wxID_ANY 44 | 45 | 510,300 46 | RoundTracksDialog 47 | 48 | 49 | wxDEFAULT_DIALOG_STYLE|wxSTAY_ON_TOP 50 | ; ; forward_declare 51 | Round Tracks 52 | 53 | 54 | 55 | 56 | on_close 57 | 58 | 1 59 | wxBOTH 60 | 0 61 | 0 62 | 0 63 | 64 | grid_vert 65 | wxFLEX_GROWMODE_SPECIFIED 66 | none 67 | 2 68 | 0 69 | 70 | 5 71 | wxEXPAND|wxTOP|wxRIGHT|wxLEFT 72 | 1 73 | 74 | 75 | 76 | 1 77 | 1 78 | 79 | 80 | 0 81 | wxID_ANY 82 | 83 | 100,100 84 | netclasslist 85 | protected 86 | 87 | 88 | 89 | ; ; forward_declare 90 | 91 | 92 | 93 | 94 | on_item_editing 95 | 96 | wxALIGN_LEFT 97 | 98 | wxDATAVIEW_COL_RESIZABLE|wxDATAVIEW_COL_SORTABLE 99 | Net Class 100 | wxDATAVIEW_CELL_INERT 101 | net_class 102 | protected 103 | Text 104 | 160 105 | 106 | 107 | wxALIGN_CENTER 108 | 109 | wxDATAVIEW_COL_SORTABLE 110 | Rounding 111 | wxDATAVIEW_CELL_ACTIVATABLE 112 | do_rounding 113 | protected 114 | Toggle 115 | 80 116 | 117 | 118 | wxALIGN_LEFT 119 | 120 | wxDATAVIEW_COL_SORTABLE 121 | Scaling % 122 | wxDATAVIEW_CELL_EDITABLE 123 | scaling 124 | protected 125 | Text 126 | 80 127 | 128 | 129 | wxALIGN_LEFT 130 | 131 | wxDATAVIEW_COL_SORTABLE 132 | Length % 133 | wxDATAVIEW_CELL_EDITABLE 134 | max_length 135 | protected 136 | Text 137 | 80 138 | 139 | 140 | wxALIGN_LEFT 141 | 142 | wxDATAVIEW_COL_SORTABLE 143 | Passes # 144 | wxDATAVIEW_CELL_EDITABLE 145 | passes 146 | protected 147 | Text 148 | 80 149 | 150 | 151 | 152 | 153 | 5 154 | wxEXPAND 155 | 1 156 | 157 | 4 158 | wxBOTH 159 | 2 160 | 161 | 0 162 | 163 | grid_horiz 164 | wxFLEX_GROWMODE_SPECIFIED 165 | none 166 | 0 167 | 0 168 | 169 | 5 170 | wxALL|wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT 171 | 0 172 | 173 | 1 174 | 1 175 | 1 176 | 1 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 1 185 | 0 186 | 1 187 | 1 188 | 189 | 1 190 | 0 191 | Dock 192 | 0 193 | Left 194 | 1 195 | 196 | 1 197 | 198 | 0 199 | 0 200 | wxID_ANY 201 | create a new file 202 | 203 | 0 204 | 205 | 206 | 0 207 | 208 | 1 209 | do_create 210 | 1 211 | 212 | 213 | protected 214 | 1 215 | 216 | Resizable 217 | 1 218 | 219 | 220 | ; ; forward_declare 221 | 0 222 | 223 | 224 | wxFILTER_NONE 225 | wxDefaultValidator 226 | 227 | 228 | 229 | 230 | on_toggle_create 231 | 232 | 233 | 234 | 5 235 | wxALIGN_CENTER_VERTICAL|wxALIGN_RIGHT|wxALL 236 | 0 237 | 238 | 1 239 | 1 240 | 1 241 | 1 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 1 250 | 0 251 | 1 252 | 1 253 | 254 | 1 255 | 0 256 | Dock 257 | 0 258 | Left 259 | 1 260 | 261 | 1 262 | 263 | 0 264 | 0 265 | wxID_ANY 266 | open the newly created file 267 | 268 | 0 269 | 270 | 271 | 0 272 | 273 | 1 274 | do_open 275 | 1 276 | 277 | 278 | protected 279 | 1 280 | 281 | Resizable 282 | 1 283 | 284 | 285 | ; ; forward_declare 286 | 0 287 | 288 | 289 | wxFILTER_NONE 290 | wxDefaultValidator 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 5 299 | wxEXPAND 300 | 1 301 | 302 | 0 303 | protected 304 | 0 305 | 306 | 307 | 308 | 5 309 | wxALL|wxEXPAND 310 | 0 311 | 312 | 1 313 | 1 314 | 1 315 | 1 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 1 325 | 0 326 | 1 327 | 328 | 1 329 | 330 | 1 331 | 0 332 | 333 | Dock 334 | 0 335 | Left 336 | 1 337 | 338 | 1 339 | 340 | 341 | 0 342 | 0 343 | wxID_ANY 344 | Run 345 | 346 | 0 347 | 348 | 0 349 | 350 | 351 | 0 352 | 353 | 1 354 | apply 355 | 1 356 | 357 | 358 | protected 359 | 1 360 | 361 | 362 | 363 | Resizable 364 | 1 365 | 120,-1 366 | 367 | ; ; forward_declare 368 | 0 369 | 370 | 371 | wxFILTER_NONE 372 | wxDefaultValidator 373 | 374 | 375 | 376 | 377 | run 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | -------------------------------------------------------------------------------- /round_tracks_utils.py: -------------------------------------------------------------------------------- 1 | # some utility functions for dealing with tracks 2 | # Julian Loiacono 2018 3 | 4 | import pcbnew 5 | import math 6 | 7 | def reverseTrack(t1): 8 | #flip the track 9 | sp = cloneWxPoint(t1.GetStart()) 10 | ep = cloneWxPoint(t1.GetEnd()) 11 | t1.SetStart(ep) 12 | t1.SetEnd(sp) 13 | 14 | #determine if there is a stiffener zone on Eco1 which this point lies above 15 | def stiffened(board, point): 16 | # remove all of the intersections which occur over an ECO1 (stiffener) zone 17 | # we are not worried about rounding these traces. 18 | for zone in board.Zones(): 19 | if "Eco1" in board.GetLayerName(zone.GetLayer()): 20 | if zone.HitTestInsideZone(point): 21 | return True 22 | return False; 23 | 24 | def RebuildAllZones(pcb): 25 | filler = pcbnew.ZONE_FILLER(pcb) 26 | filler.Fill(pcb.Zones()) 27 | 28 | # normalizes any angle to between pi/2 and -pi/2 29 | def normalizeAngleHalf(inputAngle): 30 | #normalize the angle to [-pi, pi) 31 | while(inputAngle >= math.pi/2): 32 | inputAngle -= math.pi; 33 | while(inputAngle < -math.pi/2): 34 | inputAngle += math.pi; 35 | 36 | return inputAngle; 37 | 38 | #determines whether 2 points are close enough to be considered identical 39 | def similarPoints(p1, p2): 40 | return (((p1.x > p2.x - 10) and (p1.x < p2.x + 10)) and ((p1.y > p2.y - 10) and (p1.y < p2.y + 10))); 41 | #return ((p1.x == p2.x) and (p1.y == p2.y)); 42 | 43 | # the math for shorten track 44 | def projectInward(t1, amountToShorten): 45 | 46 | #reduce to 0 length if amount to shorten exceeds length 47 | 48 | # find the angle of this track 49 | angle = normalizeAngle(getTrackAngle(t1)); 50 | #print("angle of track: ", angle*180/math.pi) 51 | #print("Shortening from Start") 52 | if amountToShorten >= t1.GetLength(): 53 | newX = t1.GetEnd().x; 54 | newY = t1.GetEnd().y; 55 | 56 | else: 57 | #project amountToShorten as the new startpoint 58 | newX = t1.GetStart().x + math.cos(angle)*amountToShorten; 59 | newY = t1.GetStart().y + math.sin(angle)*amountToShorten; 60 | 61 | return pcbnew.wxPoint(newX, newY) 62 | 63 | # shortens a track by an arbitrary amount, maintaning the angle and the endpoint 64 | def shortenTrack(t1, amountToShorten): 65 | newPoint = projectInward(t1, amountToShorten); 66 | 67 | #always shorten from start 68 | t1.SetStart(newPoint) 69 | 70 | return newPoint 71 | 72 | # normalizes any angle to between pi and -pi 73 | def normalizeAngle(inputAngle): 74 | #normalize the angle to [-pi, pi) 75 | while(inputAngle >= math.pi): 76 | inputAngle -= 2*math.pi; 77 | while(inputAngle < -math.pi): 78 | inputAngle += 2*math.pi; 79 | 80 | return inputAngle; 81 | 82 | #gets the angle of a track (unnormalized) 83 | #startReference of True means take angle from startpoint 84 | #otherwise take angle from endpoint (difference of pi) 85 | def getTrackAngle(t1): 86 | #use atan2 so the correct quadrant is returned 87 | return math.atan2((t1.GetEnd().y - t1.GetStart().y), (t1.GetEnd().x - t1.GetStart().x)); 88 | 89 | 90 | #finds the angle difference between t1 and t2 at intersection ip 91 | def angleBetween(t1, t2, ip): 92 | 93 | #find the angle betwenn 2 tracks 94 | #where start and end point are arranged arbitrarily 95 | angle1 = getTrackAngle(t1) 96 | angle2 = getTrackAngle(t2) 97 | if(similarPoints(t1.GetEnd(), ip)): 98 | angle1 += math.pi; 99 | if(similarPoints(t2.GetEnd(), ip)): 100 | angle2 += math.pi; 101 | 102 | return abs(normalizeAngle(angle1-angle2)) 103 | 104 | #utility function for sorting tracks by length 105 | def GetTrackLength(t1): 106 | return t1.GetLength() 107 | 108 | #utility function to return a new point object with the same coordinates 109 | def cloneWxPoint(wxp): 110 | return pcbnew.wxPoint(wxp.x, wxp.y) --------------------------------------------------------------------------------