171 lines
4.5 KiB
Python
Executable File
171 lines
4.5 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""
|
|
This script creates a motherboard plate and allows for various exports.
|
|
|
|
Usage: python3 main.py --display --stl output.stl --dxf output.dxf --svg output.svg
|
|
"""
|
|
import logging
|
|
import argparse
|
|
from bd_cruft import immersion_lid
|
|
from build123d import *
|
|
from ocp_vscode import *
|
|
|
|
|
|
def display_object(obj: BuildPart):
|
|
"""
|
|
Display the given object in the IDE with specified color and transparency.
|
|
|
|
:param obj: The object to be displayed.
|
|
:type obj: Any
|
|
"""
|
|
show_object(
|
|
obj,
|
|
name="Immersion Motherboard Plate",
|
|
black_edges=True,
|
|
)
|
|
|
|
|
|
def export_stl(obj: BuildPart, filename):
|
|
"""
|
|
Export the given object as an STL file.
|
|
|
|
:param obj: The object to be exported.
|
|
:type obj: BuildPart
|
|
:param filename: The path of the output file.
|
|
:type filename: str
|
|
"""
|
|
try:
|
|
print("exporting STL to ", filename)
|
|
obj.part.export_stl(filename)
|
|
except Exception as e:
|
|
logging.error(f"Error exporting STL to {filename}: {e}")
|
|
|
|
|
|
def export_dxf(obj: BuildPart, filename):
|
|
"""
|
|
Export the given object as a DXF file.
|
|
|
|
:param obj: The object to be exported.
|
|
:type obj: BuildPart
|
|
:param filename: The path of the output file.
|
|
:type filename: str
|
|
"""
|
|
try:
|
|
print("exporting DXF to ", filename)
|
|
view_port_origin = (0, 0, 1)
|
|
viewport_up = (0, 1, 0)
|
|
look_at = (0, 0, 0)
|
|
visible, hidden = obj.part.project_to_viewport(
|
|
view_port_origin, viewport_up=viewport_up, look_at=look_at
|
|
)
|
|
max_dimension = max(*Compound(children=visible).bounding_box().size)
|
|
exporter = ExportDXF(unit=Unit.MM, line_weight=0.5)
|
|
exporter.add_layer("Visible")
|
|
exporter.add_shape(visible, layer="Visible")
|
|
exporter.write(filename)
|
|
except Exception as e:
|
|
logging.error(f"Error exporting DXF to {filename}: {e}")
|
|
|
|
|
|
def export_svg(obj: BuildPart, filename):
|
|
"""
|
|
Export the given object as a SVG file.
|
|
|
|
:param obj: The object to be exported.
|
|
:type obj: BuildPart
|
|
:param filename: The path of the output file.
|
|
:type filename: str
|
|
"""
|
|
try:
|
|
print("exporting SVG to ", filename)
|
|
view_port_origin = (0, 0, 1)
|
|
viewport_up = (0, 1, 0)
|
|
look_at = (0, 0, 0)
|
|
visible, hidden = obj.part.project_to_viewport(
|
|
view_port_origin, viewport_up=viewport_up, look_at=look_at
|
|
)
|
|
max_dimension = max(*Compound(children=visible).bounding_box().size)
|
|
exporter = ExportSVG(scale=100 / max_dimension)
|
|
exporter.add_layer("Visible")
|
|
exporter.add_shape(visible, layer="Visible")
|
|
exporter.write(filename)
|
|
|
|
except Exception as e:
|
|
logging.error(f"Error exporting SVG to {filename}: {e}")
|
|
|
|
|
|
def parse_args(**kwargs):
|
|
"""
|
|
Parse the command line arguments.
|
|
|
|
:param kwargs: Additional arguments to pass to ArgumentParser.parse_args()
|
|
:return: The parsed command line arguments.
|
|
:rtype: Any
|
|
"""
|
|
parser = argparse.ArgumentParser(
|
|
description="Create and manipulate a motherboard plate."
|
|
)
|
|
parser.add_argument(
|
|
"--display", action="store_false", help="Display the generated plate in IDE"
|
|
)
|
|
parser.add_argument(
|
|
"--stl",
|
|
dest="stl_filename",
|
|
type=str,
|
|
metavar="FILENAME",
|
|
help="Export the plate as an STL file",
|
|
)
|
|
parser.add_argument(
|
|
"--dxf",
|
|
dest="dxf_filename",
|
|
type=str,
|
|
metavar="FILENAME",
|
|
help="Export the plate as a DXF file",
|
|
)
|
|
parser.add_argument(
|
|
"--svg",
|
|
dest="svg_filename",
|
|
type=str,
|
|
metavar="FILENAME",
|
|
help="Export the plate as a SVG file",
|
|
)
|
|
|
|
config = {
|
|
"display": True,
|
|
"stl_filename": None,
|
|
"dxf_filename": None,
|
|
"svg_filename": None,
|
|
}
|
|
|
|
parser.set_defaults(**config)
|
|
|
|
args = parser.parse_args(**kwargs)
|
|
|
|
return args
|
|
|
|
|
|
def main():
|
|
args = parse_args()
|
|
|
|
plate_config = immersion_lid.ImmersionLidConfig()
|
|
plate = immersion_lid.create_plate(**plate_config.model_dump())
|
|
|
|
if any([args.stl_filename, args.dxf_filename, args.svg_filename]):
|
|
args.display = False
|
|
|
|
if args.display:
|
|
display_object(plate)
|
|
|
|
if args.stl_filename is not None:
|
|
export_stl(plate, args.stl_filename)
|
|
|
|
if args.dxf_filename is not None:
|
|
export_dxf(plate, args.dxf_filename)
|
|
|
|
if args.svg_filename is not None:
|
|
export_svg(plate, args.svg_filename)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|