KiCAD to OpenSCAD for PCB case generation - Page 1
Forum section: electronics / general

author: Pineapple Dan
author_rank: Contributor
timestamp: January 21, 2022, 07:48:24 pm
content: This is very rough and ready but it has saved me a great deal of time trying to manually place mount points for PCB casesBasically I use the following python script to find where the mount holes are in the Kicad_pcbCode:MH = []Valid_MH = ['MountingHole:MountingHole_4.3mm_M4','MountingHole:MountingHole_3.2mm_M3']class MountHole():def __init__(self,raw_text):self.x = 0self.y = 0self.size = 3self.raw = raw_textself.parse()def parse(self):ls = self.raw.splitlines()for a in ls:if(a[:4] == "(at "):loc = a.split(" ")self.x = float(loc)self.y = float(loc[:-1])def OpenSCAD(self):return ("mount(" + str(self.x) + ',' + str(self.y) + "," + str(self.size) + ");")#print("m")#return "mount(" + str(self.x,'utf-8') + "," + str(self.y,'utf-8') + "," + str(self.size,'utf-8') + ")\n"class Line():def __init__(self,raw):self.raw = raw#Start coordsself.s = self.raw.find("(start")self.st = self.raw[self.s:]self.sta = self.st[:self.st.find(")")].split(" ")self.startx = float(self.sta)self.starty = float(self.sta)#End coordsself.e = self.raw.find("(end")self.et = self.raw[self.e:]self.eta = self.et[:self.et.find(")")].split(" ")self.endx = float(self.eta)self.endy = float(self.eta)print(self.endx,self.endy)class KCfile():def __init__(self,path):self.kfile = open(path,'r')self.mounts = []self.lins = []self.mods_text = self.getmods()self.getmounts()def par(self,string):return(string.count("(") - string.count(")"))def getmods(self):mods = []module_text = ""in_module = Falseopen_bracket = 0for a in self.kfile.readlines():a = a.strip()if(a[:8] == "(gr_line"):self.lins.append(Line(a))if(a[:8] != "(module " and not in_module): continuein_module = Truemodule_text = module_text + a + '\n'open_bracket += self.par(a)# print(a,open_bracket)if(open_bracket == 0):mods.append(module_text)module_text = ""in_module = Falsereturn modsdef getmounts(self):mcount = 0for a in self.mods_text:mtype = a.split(" ")if(mtype in Valid_MH):mcount += 1self.mounts.append(MountHole(a))print(mcount, "mount holes found")def OpenSCAD_mounts(self):minusx = 9999999999minusy = 9999999999hy = 0for a in self.mounts:#print(a.x)if(a.x < minusx): minusx = a.xif(a.y < minusy): minusy = a.yprint("mirror([0,90,0])\nunion(){")for a in self.mounts:if(a.y - minusy >hy): hy=a.y - minusyfor a in self.mounts:x = a.x-minusxy = a.y-minusyprint("mount(" + str(x) + ',' + str(y - hy) + "," + str(a.size) + ");")#print(a.OpenSCAD())print("}")def Gen_poly(self):passfl = KCfile('YOUR-FILENAME-HERE.kicad_pcb')#print(fl.mods_text)fl.OpenSCAD_mounts()#print(fl.lins)That script spits out a few lines like for exampleCode:mirror([0,90,0])union(){mount(0.0,0.0,3);mount(65.34076,-9.57002,3);mount(3.4696399999999983,-53.507639999999995,3);mount(66.23075999999999,-52.560019999999994,3);}This I can paste into openscad and once I have my my mount() module defined somewhere ( very simple just one cylinder minus another)Code:module mount(x,y,size = 2.4, support = false) { // For 3mm screwtranslate([x,y,0])difference(){cylinder($mount_height,r=size);cylinder($mount_height + 0.01,r=(size/2));}}Once u have the mount points in the right place its very easy to design a box around it, I do have a script to generate the boxes as well but I really need to clean that up before I even consider posting it anywhere. Still I hope this is useful to someone

author: Doctorandus_P
author_rank: Super Contributor
timestamp: April 06, 2022, 04:58:45 am
content: Are you aware of the KiCadStepUp workbench in FreeCAD?

author: kripton2035
author_rank: Super Contributor
timestamp: April 06, 2022, 06:30:06 am
content: +1. exporting a kicad pcb to freecad is really easy. once you tried it !

author: thinkfat
author_rank: Unknown
timestamp: April 06, 2022, 08:51:59 am
content: You don't even need StepUp for that. The standard STEP export from KiCAD is totally sufficient to get a model into FreeCAD that you can then use for checking collisions with your box etc.However, not everyone is a FreeCAD user and having an automated way to export mounting points to OpenSCAD, which many engineers seem to like, is definitely appreciated.

author: kripton2035
author_rank: Super Contributor
timestamp: April 06, 2022, 12:00:43 pm
content: Quote from: thinkfat on April 06, 2022, 08:51:59 amabsolutely !