Last active
December 4, 2024 21:26
-
-
Save scottyob/e8576c060ffe9018c50846e1fe934590 to your computer and use it in GitHub Desktop.
Script to import into FreeCAD for my keyboard project.
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
from itertools import chain | |
spec = {"width": 76.9, "height": 117.7, "support_stems": [[19.2, -19.15], [-19.45, -19.1], [-19.35, 19.0]], "switches": [[9.53, 28.0], [-19.05, -48.2], [9.53, -48.2], [-9.53, 8.95], [-28.58, -10.1], [-9.53, -29.15], [-9.53, -10.1], [28.57, 28.0], [28.57, -38.67], [28.57, -0.57], [-28.58, 28.0], [9.53, -10.1], [-28.58, -29.15], [9.53, 8.95], [9.53, -29.15], [-28.58, 8.95], [-9.53, 28.0]], "diodes": [[-19.4, -56.6, 180.0], [10.2, -56.6, 180.0], [-27.5, 0.5, 180.0], [19.6, 4.3, 90.0], [-27.9, 19.65, 180.0], [-8.45, 0.5, 180.0], [10.0, -18.6, 180.0], [10.6, 0.5, 180.0], [-9.0, 19.65, 180.0], [-8.8, -18.6, 180.0], [10.2, 19.65, 180.0], [29.3, 19.7, 180.0], [-8.55, -37.65, 180.0], [19.6, -33.85, 90.0], [-27.6, -37.65, 180.0], [-28.0, -18.6, 180.0], [10.0, -37.65, 180.0]], "smd_cutouts": [[19.8, 19.8, 6.4, 8.0], [30.7, 17.8, 9.8, 6.0], [2.1, -38.5, 8.2, 4.2], [0.0, -32.0, 5.6, 8.8], [30.1, -19.6, 15.0, 7.6]]} | |
DIODE_LENGTH = 5 | |
DIODE_WIDTH = 2 | |
SMD_HEIGHT = 1.3 | |
document = FreeCAD.ActiveDocument | |
# Remove all of bottom cutout objects | |
stems_container = document.getObjectsByLabel("BottomSupportStems")[0] | |
case_bottom_cutouts = document.getObjectsByLabel("CaseBottomCutouts")[0] | |
top_plate_cutouts = document.getObjectsByLabel("TopPlateCutouts")[0] | |
top_plate_smd_cutouts = document.getObjectsByLabel("TopPlateSMDCutouts")[0] | |
for container in chain((stems_container, case_bottom_cutouts, top_plate_cutouts, top_plate_smd_cutouts)): | |
for child in container.getSubObjects(): | |
name = child.rstrip(".") | |
document.removeObject(name) | |
# Create links for the stems | |
parent_stem = document.getObjectsByLabel("SupportStem")[0] | |
parent_stem_cutout = document.getObjectsByLabel("SupportStemCutout")[0] | |
parent_top_stem_cutout = document.getObjectsByLabel("SupportTopStemCutout")[0] | |
for i, stem in enumerate(spec['support_stems']): | |
name = f'StemLink{i:03}' | |
cutout_name = f'StemCutoutLink{i:03}' | |
cutout_top_name = f'StemTopPlateCutoutLink{i:03}' | |
x, y = stem | |
# Create a new link for this stem object | |
link = document.addObject('App::Link', name) | |
link.setLink(parent_stem) | |
link.Label = name | |
link.Placement = App.Placement(App.Vector(x,y,0),App.Rotation(App.Vector(0,0,1),0)) | |
stems_container.addObject(link) | |
# Create a new link for this stem cutout | |
link = document.addObject('App::Link', cutout_name) | |
link.setLink(parent_stem_cutout) | |
link.Label = cutout_name | |
link.Placement = App.Placement(App.Vector(x,y,0),App.Rotation(App.Vector(0,0,1),0)) | |
case_bottom_cutouts.addObject(link) | |
# For the top plate cutout | |
link = document.addObject('App::Link', cutout_top_name) | |
link.setLink(parent_top_stem_cutout) | |
link.Label = cutout_top_name | |
link.Placement = App.Placement(App.Vector(x,y,0),App.Rotation(App.Vector(0,0,1),0)) | |
top_plate_cutouts.addObject(link) | |
# Create links for the switch cutouts | |
parent_switch = document.getObjectsByLabel("KeyCutout")[0] | |
parent_top_plate_cutout = document.getObjectsByLabel("TopPlateKeyCutout")[0] | |
for i, switch in enumerate(spec['switches']): | |
name = f'SwitchLink{i:03}' | |
top_plate_name = f'TopPlateLink{i:03}' | |
x, y = switch | |
# Create a new link for this switch cutout on the bottom plate | |
link = document.addObject('App::Link', name) | |
link.setLink(parent_switch) | |
link.Label = name | |
link.Placement = App.Placement(App.Vector(x,y,0),App.Rotation(App.Vector(0,0,1),0)) | |
case_bottom_cutouts.addObject(link) | |
# And for the top plate | |
link = document.addObject('App::Link', top_plate_name) | |
link.setLink(parent_top_plate_cutout) | |
link.Label = top_plate_name | |
link.Placement = App.Placement(App.Vector(x,y,0),App.Rotation(App.Vector(0,0,1),0)) | |
top_plate_cutouts.addObject(link) | |
# Diode cubes | |
for i, diode in enumerate(spec['diodes']): | |
name = f'DiodeTopPlateCutout{i:03}' | |
x, y, rotation = diode | |
cube = document.addObject("Part::Box", name) | |
cube.Length = DIODE_LENGTH | |
cube.Width = DIODE_WIDTH | |
cube.Height = SMD_HEIGHT | |
cube.Label = name | |
cube.Placement = App.Placement(App.Vector(x,y,0),App.Rotation(App.Vector(0,0,1),rotation)) | |
top_plate_cutouts.addObject(cube) | |
# Surface mount cutout cubes | |
for i, cutout in enumerate(spec['smd_cutouts']): | |
name = f'SmdTopPlateCutout{i:03}' | |
x, y, length, width = cutout | |
x -= length / 2 | |
y -= width / 2 | |
cube = document.addObject("Part::Box", name) | |
cube.Length = length | |
cube.Width = width | |
cube.Height = SMD_HEIGHT | |
cube.Label = name | |
cube.Placement = App.Placement(App.Vector(x,y,0),App.Rotation(App.Vector(0,0,1),0)) | |
top_plate_smd_cutouts.addObject(cube) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment