def select(self, indexes): self.debug_output.clear_output() with self.debug_output: self.indexes = indexes cad_objs = [self.stack[i][1] for i in self.indexes] # Add hidden result to start with final size and allow for comparison if not isinstance(self.stack[-1][1].val(), cq.Vector): result = Part(self.stack[-1][1], "Result", show_faces=False, show_edges=False) objs = [result] + cad_objs else: objs = cad_objs with self.debug_output: assembly = to_assembly(*objs) mapping = assembly.to_state() shapes = assembly.collect_mapped_shapes( mapping, quality=self.quality, deviation=self.deviation, angular_tolerance=self.angular_tolerance, edge_accuracy=self.edge_accuracy, render_edges=get_default("render_edges"), render_normals=get_default("render_normals"), ) tree = assembly.to_nav_dict() self.display.add_shapes(shapes=shapes, mapping=mapping, tree=tree, bb=_combined_bb(shapes), reset_camera=False)
def start_viewer(): global VIEWER zmq_port = 5555 if os.environ.get("ZMQ_PORT") is None else os.environ["ZMQ_PORT"] cad_width = get_default("cad_width") if os.environ.get("CAD_WIDTH") is None else int(os.environ["CAD_WIDTH"]) cad_height = get_default("height") if os.environ.get("CAD_HEIGHT") is None else int(os.environ["CAD_HEIGHT"]) theme = get_default("theme") if os.environ.get("THEME") is None else os.environ["THEME"] glass_mode = get_default("glass") if os.environ.get("GLASS_MODE") is None else (os.environ["GLASS_MODE"] == "1") VIEWER = Viewer(zmq_port) VIEWER.start_viewer(cad_width, cad_height, theme, glass_mode)
def _display(self, data, logo=False): mesh_data = data["data"] config = data["config"] info(mesh_data["bb"]) if logo or config.get("cad_width") is None: config["cad_width"] = get_default("cad_width") else: if config.get("cad_width") < 640: warn("cad_width has to be >= 640, setting to 640") config["cad_width"] = 640 if logo or config.get("height") is None: config["height"] = get_default("height") else: if config.get("height") < 400: warn("height has to be >= 400, setting to 400") config["height"] = 400 if logo or config.get("tree_width") is not None: config["tree_width"] = get_default("tree_width") else: if config.get("tree_width") < 200: warn("tree_width has to be >= 200, setting to 200") config["tree_width"] = 200 width = config["cad_width"] + config["tree_width"] + 6 if self.interactive is not None: self.interactive.layout.width = px(width - 30) if self.log_output is not None: self.log_output.layout.width = px(width - 30) self.log_view.layout.width = px(width) # Force reset of camera to not inhereit splash settings for first object if self.cad_display.splash: config["reset_camera"] = True self.cad_display.splash = False self.cad_display.set_size(config.get("tree_width"), config.get("cad_width"), config.get("height")) self.cad_display.init_progress(data.get("count", 1)) create_args, add_shape_args = split_args(config) self.cad_display._update_settings(**create_args) self.cad_display.add_shapes(**mesh_data, **add_shape_args) info(create_args, add_shape_args) self.cad_display.info.ready_msg(self.cad_display.cq_view.grid.step) self.root_group = self.cad_display.root_group
def _convert(*cad_objs, **kwargs): color = kwargs.get("default_color") if color is None: color = get_default("default_color") part_group = to_assembly( *cad_objs, render_mates=kwargs.get("render_mates"), mate_scale=kwargs.get("mate_scale", 1), default_color=color, ) if len(part_group.objects) == 1 and isinstance(part_group.objects[0], PartGroup): part_group = part_group.objects[0] # Do not send defaults for postion, rotation unless they are set in kwargs config = { k: v for k, v in get_defaults().items() if not k in ("position", "rotation") } for k, v in kwargs.items(): if v is not None: config[k] = v mapping = part_group.to_state() shapes = part_group.collect_mapped_shapes( mapping, quality=config.get("quality"), deviation=config.get("deviation"), angular_tolerance=config.get("angular_tolerance"), edge_accuracy=config.get("edge_accuracy"), render_edges=config.get("render_edges"), render_normals=config.get("render_normals"), timeit=config.get("timeit"), progress=Progress(), ) tree = part_group.to_nav_dict() data = { "data": dict(mapping=mapping, shapes=shapes, tree=tree, bb=_combined_bb(shapes)), "type": "data", "config": config, "count": part_group.count_shapes(), } return data
def _display(self, data, logo=False): mesh_data = data["data"] config = data["config"] if logo: config["cad_width"] = get_default("cad_width") config["tree_width"] = get_default("tree_width") config["height"] = get_default("height") if config.get("cad_width") is None: config["cad_width"] = get_default("cad_width") else: if config.get("cad_width") < 640: warn("cad_width has to be >= 640, setting to 640") config["cad_width"] = 640 if config.get("height") is None: config["height"] = get_default("height") else: if config.get("height") < 400: warn("height has to be >= 400, setting to 400") config["height"] = 400 if config.get("tree_width") is None: config["tree_width"] = get_default("tree_width") else: if config.get("tree_width") < 200: warn("tree_width has to be >= 200, setting to 200") config["tree_width"] = 200 if config.get("glass") is None: config["glass"] = get_default("glass") width = config["cad_width"] + 6 if not config["glass"]: width += config["tree_width"] if self.interactive is not None: self.interactive.layout.width = px(width - 30) if self.log_output is not None: self.log_output.layout.width = px(width - 30) self.log_view.layout.width = px(width) # Force reset of camera to not inhereit splash settings for first object if self.splash: config["reset_camera"] = True self.splash = False kwargs = add_shape_args(config) self.viewer.clear_tracks() self.viewer.add_shapes(**mesh_data, **kwargs) info(create_args(config)) info(add_shape_args(config))
def __init__(self, shape, name="Part", color=None, show_faces=True, show_edges=True): super().__init__() self.name = name self.id = None self.color = Color( get_default("default_color") if color is None else color) self.shape = shape self.set_states(show_faces, show_edges) self.renderback = False
def _convert(*cad_objs, **kwargs): color = kwargs.get("default_color") if color is None: color = get_default("default_color") part_group = to_assembly( *cad_objs, render_mates=kwargs.get("render_mates"), mate_scale=kwargs.get("mate_scale", 1), default_color=color, ) if len(part_group.objects) == 1 and isinstance(part_group.objects[0], PartGroup): part_group = part_group.objects[0] # Do not send defaults for postion, rotation and zoom unless they are set in kwargs config = { k: v for k, v in get_defaults().items() if not k in ("position", "rotation", "zoom", "cad_width", "tree_width", "height", "glass") } for k, v in kwargs.items(): if v is not None: config[k] = v shapes, states = _tessellate_group(part_group, kwargs, Progress(), config.get("timeit")) config["normal_len"] = get_normal_len( preset("render_normals", config.get("render_normals")), shapes, preset("deviation", config.get("deviation")), ) bb = _combined_bb(shapes).to_dict() # add global bounding box shapes["bb"] = bb data = { "data": dict(shapes=shapes, states=states), "type": "data", "config": config, "count": part_group.count_shapes(), } return data
def __init__(self): self.color = Color(get_default("default_color"))
def _show(part_group, **kwargs): for k in kwargs: if get_default(k, "n/a") == "n/a": raise KeyError(f"Paramater {k} is not a valid argument for show()") if kwargs.get("cad_width") is not None and kwargs.get("cad_width") < 640: warn("cad_width has to be >= 640, setting to 640") kwargs["cad_width"] = 640 if kwargs.get("height") is not None and kwargs.get("height") < 400: warn("height has to be >= 400, setting to 400") kwargs["height"] = 400 if kwargs.get("tree_width") is not None and kwargs.get("tree_width") < 250: warn("tree_width has to be >= 250, setting to 250") kwargs["tree_width"] = 250 if kwargs.get("quality") is not None: warn( "quality is ignored. Use deviation to control smoothness of edges") del kwargs["quality"] sidecar_backup = None # if kwargs.get("parallel") is not None: # if kwargs["parallel"] and platform.system() != "Linux": # warn("parallel=True only works on Linux. Setting parallel=False") # kwargs["parallel"] = False timeit = preset("timeit", kwargs.get("timeit")) with Timer(timeit, "", "overall"): if part_group is None: import base64 # pylint:disable=import-outside-toplevel import pickle # pylint:disable=import-outside-toplevel from jupyter_cadquery.logo import LOGO_DATA # pylint:disable=import-outside-toplevel logo = pickle.loads(base64.b64decode(LOGO_DATA)) config = add_shape_args(logo["config"]) defaults = get_defaults() config["cad_width"] = defaults["cad_width"] config["tree_width"] = defaults["tree_width"] config["height"] = defaults["height"] config["glass"] = defaults["glass"] config["title"] = defaults["viewer"] for k, v in create_args(kwargs).items(): config[k] = v shapes = logo["data"]["shapes"] states = logo["data"]["states"] bb = _combined_bb(shapes).to_dict() # add global bounding box shapes["bb"] = bb else: config = apply_defaults(**kwargs) if config.get("viewer") == "": # If viewer is "" (the show default), then the default sidecar should be taken into account config["viewer"] = None elif config.get("viewer") is None: # if viewer is None (explicitely set), then ignore the default sidecar, i.e. back it up and set to None sidecar_backup = get_default_sidecar() _set_default_sidecar(None) if config.get("reset_camera") is False: # could be None if config.get("zoom") is not None: del config["zoom"] if config.get("position") is not None: del config["position"] if config.get("quaternion") is not None: del config["quaternion"] parallel = preset("parallel", config.get("parallel")) with Timer(timeit, "", "tessellate", 1): num_shapes = part_group.count_shapes() progress_len = 2 * num_shapes if parallel else num_shapes progress = None if num_shapes < 2 else Progress(progress_len) if parallel: init_pool() keymap.reset() shapes, states = _tessellate_group(part_group, tessellation_args(config), progress, timeit) if parallel: mp_get_results(shapes, progress) close_pool() bb = _combined_bb(shapes).to_dict() # add global bounding box shapes["bb"] = bb if progress is not None: progress.done() # Calculate normal length config["normal_len"] = get_normal_len( preset("render_normals", config.get("render_normals")), shapes, preset("deviation", config.get("deviation")), ) show_bbox = preset("show_bbox", kwargs.get("show_bbox")) if show_bbox: insert_bbox(show_bbox, shapes, states) with Timer(timeit, "", "show shapes", 1): cv = viewer_show(shapes, states, **show_args(config)) # If we forced to ignore the default sidecar, restore it if sidecar_backup is not None: _set_default_sidecar(sidecar_backup) return cv