Created
April 30, 2020 23:37
-
-
Save TobleMiner/6aead8688fe2107cca2bc29781a9a826 to your computer and use it in GitHub Desktop.
pcbnew plugin to convert graphical arc to traces
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import pcbnew | |
import os | |
import math | |
RESOLUTION = 1 | |
EPSILON = 1e-4 | |
def arc_to_trace(board, arc): | |
mid = arc.GetPosition() | |
radius = arc.GetRadius() | |
start_angle = arc.GetArcAngleStart() / 10 | |
sweep = arc.GetAngle() / 10 | |
width = arc.GetWidth() | |
direction = -1 if sweep < 0 else 1 | |
def point(angle): | |
return mid + pcbnew.wxPoint(round(math.cos(math.radians(angle)) * radius), round(math.sin(math.radians(angle)) * radius)) | |
angle = 0 | |
while abs(sweep) - angle > EPSILON: | |
pos = point(start_angle + angle * direction) | |
angle += min(RESOLUTION, abs(sweep) - angle) | |
next_pos = point(start_angle + angle * direction) | |
track = pcbnew.TRACK(None) | |
track.SetStart(pos) | |
track.SetEnd(next_pos) | |
track.SetWidth(width) | |
board.Add(track) | |
arc.ClearSelected() | |
board.Remove(arc) | |
class SimplePlugin(pcbnew.ActionPlugin): | |
def defaults(self): | |
self.name = "Arc to trace" | |
self.category = "Trace tools" | |
self.description = "Converts the selected arc to a trace" | |
self.show_toolbar_button = True | |
self.icon_file_name = os.path.join(os.path.dirname(__file__), 'arc_to_trace.png') # Optional, defaults to "" | |
def Run(self): | |
board = pcbnew.GetBoard() | |
for draw in board.GetDrawings(): | |
if draw.IsSelected() and draw.GetShape() == pcbnew.S_ARC: | |
arc_to_trace(board, draw) | |
if __name__ == '__main__': | |
board = pcbnew.LoadBoard('/home/tsys/work/pcb/PowerPack/PowerPack-Brain/PowerPack-Brain.kicad_pcb') | |
for draw in board.GetDrawings(): | |
print(draw.GetShapeStr()) | |
if draw.GetShapeStr() == 'Arc': | |
arc_to_trace(board, draw) | |
else: | |
SimplePlugin().register() # Instantiate and register to Pcbnew |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment