def main(): # define where we'll read shapes from sources = [ Path.cwd().parent / "oxford" / "master.dxf", Path.cwd().parent / "oxford" / "derivatives" / "5x5_cluster_master.dxf", ] support_thickness = 0.65 feature_thickness = 0.2 shim_thickness = 0.5 support_color = "GOLDENROD" feature_color = "GRAY28" shim_color = "DARKGREEN" # define how we'll tile things spacing5 = 30 array5 = [(x * spacing5, y * spacing5, 0) for x, y in itertools.product(range(-2, 3), range(-2, 3))] instructions = [] instructions.append({ "name": "active_mask_stack", "layers": [ { "name": "active_support", "color": support_color, "thickness": support_thickness, "drawing_layer_names": [ "glass_extents", "aggressive_support_active", ], }, { "name": "active_feature", "color": feature_color, "thickness": feature_thickness, "drawing_layer_names": [ "glass_extents", "active_layer", ], }, { "name": "spacer_shim", "color": shim_color, "thickness": shim_thickness, "drawing_layer_names": [ "glass_extents", "spacer_shim", ], }, ], }) instructions.append({ "name": "active_mask_stack_5x5", "layers": [ { "name": "active_support", "color": support_color, "thickness": support_thickness, "drawing_layer_names": [ "outline_5x5", "aggressive_support_active", ], "edge_case": "inner_outline_5x5", "array": array5, }, { "name": "active_feature", "color": feature_color, "thickness": feature_thickness, "drawing_layer_names": [ "outline_no_alignment_5x5", "active_layer", ], "edge_case": "inner_outline_5x5", "array": array5, }, { "name": "spacer_shim", "color": shim_color, "thickness": shim_thickness, "drawing_layer_names": [ "outline_no_alignment_5x5", "spacer_shim", ], "edge_case": "inner_outline_5x5", "array": array5, }, ], }) instructions.append({ "name": "metal_mask_stack", "layers": [ { "name": "metal_support", "color": support_color, "thickness": support_thickness, "drawing_layer_names": [ "glass_extents", "aggressive_metal_support_tc_metal", "aggressive_metal_support_small_upper", "aggressive_metal_support_large_lower", ], }, { "name": "metal_feature", "color": feature_color, "thickness": feature_thickness, "drawing_layer_names": [ "glass_extents", "tc_metal", "pixel_electrodes_small_upper", "pixel_electrodes_large_lower", ], }, { "name": "spacer_shim_thin", "color": shim_color, "thickness": shim_thickness, "drawing_layer_names": [ "glass_extents", "spacer_shim_thin", ], }, ], }) instructions.append({ "name": "metal_mask_stack_5x5", "layers": [ { "name": "metal_support", "color": support_color, "thickness": support_thickness, "drawing_layer_names": [ "outline_5x5", "aggressive_metal_support_tc_metal", "aggressive_metal_support_small_upper", "aggressive_metal_support_large_lower", ], "edge_case": "inner_outline_5x5", "array": array5, }, { "name": "metal_feature", "color": feature_color, "thickness": feature_thickness, "drawing_layer_names": [ "outline_no_alignment_5x5", "tc_metal", "pixel_electrodes_small_upper", "pixel_electrodes_large_lower", ], "edge_case": "inner_outline_5x5", "array": array5, }, { "name": "spacer_shim_thin", "color": shim_color, "thickness": shim_thickness, "drawing_layer_names": [ "outline_no_alignment_5x5", "spacer_shim_thin", ], "edge_case": "inner_outline_5x5", "array": array5, }, ], }) ttt = TwoDToThreeD(instructions=instructions, sources=sources) to_build = ["active_mask_stack", "metal_mask_stack"] # to_build = ["active_mask_stack_5x5"] # to_build = [""] # all of them asys = ttt.build(to_build) # asy: cadquery.Assembly = list(asys.values())[0] # TODO:take more than the first value for stack_name, asy in asys.items(): if "show_object" in globals(): # we're in cq-editor assembly_mode = True # at the moment, when true we can't select/deselect subassembly parts if assembly_mode: show_object(asy) else: for key, val in asy.traverse(): shapes = val.shapes if shapes != []: c = cq.Compound.makeCompound(shapes) odict = {} if val.color is not None: co = val.color.wrapped.GetRGB() rgb = (co.Red(), co.Green(), co.Blue()) odict["color"] = rgb show_object(c.locate(val.loc), name=val.name, options=odict) else: # save assembly asy.save( str(Path(__file__).parent / "output" / f"{stack_name}.step")) asy.save( str(Path(__file__).parent / "output" / f"{stack_name}.glb"), "GLTF") # cadquery.exporters.assembly.exportCAF(asy, str(Path(__file__).parent / "output" / f"{stack_name}.std")) # cq.Shape.exportBrep(cq.Compound.makeCompound(itertools.chain.from_iterable([x[1].shapes for x in asy.traverse()])), str(Path(__file__).parent / "output" / f"{stack_name}.brep")) save_individual_stls = False save_individual_steps = False save_individual_breps = False save_individual_dxfs = True # save each shape individually for key, val in asy.traverse(): shapes = val.shapes if shapes != []: c = cq.Compound.makeCompound(shapes) if save_individual_stls == True: cadquery.exporters.export( c.locate(val.loc), str( Path(__file__).parent / "output" / f"{stack_name}-{val.name}.stl")) if save_individual_steps == True: cadquery.exporters.export( c.locate(val.loc), str( Path(__file__).parent / "output" / f"{stack_name}-{val.name}.step")) if save_individual_breps == True: cq.Shape.exportBrep( c.locate(val.loc), str( Path(__file__).parent / "output" / f"{stack_name}-{val.name}.brep")) if save_individual_dxfs == True: cl = c.locate(val.loc) bb = cl.BoundingBox() zmid = (bb.zmin + bb.zmax) / 2 nwp = CQ("XY", origin=(0, 0, zmid)).add(cl) dxface = nwp.section() cadquery.exporters.export( dxface, str( Path(__file__).parent / "output" / f"{stack_name}-{val.name}.dxf"), cadquery.exporters.ExportTypes.DXF)
def outputter(cls, asys, wrk_dir, save_dxfs=False, save_stls=False, save_steps=False, save_breps=False, save_vrmls=False): """do output tasks on a dictionary of assemblies""" for stack_name, asy in asys.items(): if "show_object" in globals(): # we're in cq-editor assembly_mode = True # at the moment, when true we can't select/deselect subassembly parts if assembly_mode: show_object(asy) else: for key, val in asy.traverse(): shapes = val.shapes if shapes != []: c = cq.Compound.makeCompound(shapes) odict = {} if val.color is not None: co = val.color.wrapped.GetRGB() rgb = (co.Red(), co.Green(), co.Blue()) odict["color"] = rgb show_object(c.locate(val.loc), name=val.name, options=odict) else: Path.mkdir(wrk_dir / "output", exist_ok=True) # save assembly asy.save(str(wrk_dir / "output" / f"{stack_name}.step")) # asy.save(str(wrk_dir / "output" / f"{stack_name}.brep")) asy.save(str(wrk_dir / "output" / f"{stack_name}.xml"), "XML") # asy.save(str(wrk_dir / "output" / f"{stack_name}.vtkjs"), "VTKJS") # stupid workaround for gltf export bug: https://github.com/CadQuery/cadquery/issues/993 asy2 = None # for path, child in asy._flatten().items(): for child in asy.children: # if "/" in path: if asy2 is None: asy2 = cadquery.Assembly(child.obj, name=child.name, color=child.color) else: asy2.add(child.obj, name=child.name, color=child.color) asy2.save(str(wrk_dir / "output" / f"{stack_name}.glb"), "GLTF") # cadquery.exporters.assembly.exportCAF(asy, str(wrk_dir / "output" / f"{stack_name}.std")) # cq.Shape.exportBrep(cq.Compound.makeCompound(itertools.chain.from_iterable([x[1].shapes for x in asy.traverse()])), str(wrk_dir / "output" / f"{stack_name}.brep")) # save each shape individually for key, val in asy.traverse(): shapes = val.shapes if shapes != []: c = cq.Compound.makeCompound(shapes) if save_stls == True: cadquery.exporters.export( c.locate(val.loc), str(wrk_dir / "output" / f"{stack_name}-{val.name}.stl"), cadquery.exporters.ExportTypes.STL) if save_steps == True: cadquery.exporters.export( c.locate(val.loc), str(wrk_dir / "output" / f"{stack_name}-{val.name}.step"), cadquery.exporters.ExportTypes.STEP) if save_breps == True: cq.Shape.exportBrep( c.locate(val.loc), str(wrk_dir / "output" / f"{stack_name}-{val.name}.brep")) if save_vrmls == True: cadquery.exporters.export( c.locate(val.loc), str(wrk_dir / "output" / f"{stack_name}-{val.name}.wrl"), cadquery.exporters.ExportTypes.VRML) if save_dxfs == True: cl = c.locate(val.loc) bb = cl.BoundingBox() zmid = (bb.zmin + bb.zmax) / 2 nwp = CQ("XY", origin=(0, 0, zmid)).add(cl) dxface = nwp.section() cadquery.exporters.export( dxface, str(wrk_dir / "output" / f"{stack_name}-{val.name}.dxf"), cadquery.exporters.ExportTypes.DXF)