def ask_add_storage(self, expr: str) -> bool: try: # Put the expression into parser to see if it is legal parse_params(expr) except Exception as error: logger.warn(error) QMessageBox.warning( self, "Loading failed", "Your expression is in an incorrect format." ) return False name, ok = QInputDialog.getText( self, "Storage", "Please input name tag:" ) if not ok: return False name_list = [ self.mechanism_storage.item(i).text() for i in range(self.mechanism_storage.count()) ] i = 0 name = name or f"Prototype_{i}" while name in name_list: name = f"Prototype_{i}" i += 1 self.__add_storage(name, expr) return True
def engine_picker(g: Graph, engine: str, node_mode: bool) -> _Pos: """Generate a position dict.""" if engine == "External Loop": try: layout: _Pos = external_loop_layout(g, node_mode, scale=30) except ValueError as error: logger.warn(error) return {} else: raise ValueError(f"engine {engine} is not exist") inf = float('inf') x_max = -inf x_min = inf y_max = -inf y_min = inf for x, y in layout.values(): if x > x_max: x_max = x if x < x_min: x_min = x if y > y_max: y_max = y if y < y_min: y_min = y x_cen = (x_max + x_min) / 2 y_cen = (y_max + y_min) / 2 pos: _Pos = { node: (x - x_cen, y - y_cen) for node, (x, y) in layout.items() } return pos
def resolve(self) -> None: """Resolve: Using three libraries to solve the system. + Pyslvs + Python-Solvespace + Sketch Solve """ for b, d, a in self.inputs_widget.input_pairs(): if b == d: self.vpoint_list[b].set_offset(a) solve_kernel = self.prefer.planar_solver_option try: if solve_kernel == 0: result = expr_solving( self.get_triangle(), {n: f'P{n}' for n in range(len(self.vpoint_list))}, self.vpoint_list, tuple(a for b, d, a in self.inputs_widget.input_pairs() if b != d)) elif solve_kernel == 1: result, _ = _slvs_solve(self.vpoint_list, { (b, d): a for b, d, a in self.inputs_widget.input_pairs() } if not self.free_move_button.isChecked() else {}) elif solve_kernel == 2: result = SolverSystem( self.vpoint_list, {(b, d): a for b, d, a in self.inputs_widget.input_pairs()}).solve() else: raise ValueError("incorrect kernel") except ValueError as error: # Error: Show warning without update data. if self.prefer.console_error_option: logger.warn(format_exc()) error_text = f"Error: {error}" self.conflict.setToolTip(error_text) self.conflict.setStatusTip(error_text) self.conflict.setVisible(True) self.dof_view.setVisible(False) else: self.entities_point.update_current_position(result) for i, c in enumerate(result): if type(c[0]) is float: self.vpoint_list[i].move(cast(_Coord, c)) else: c1, c2 = cast(Tuple[_Coord, _Coord], c) self.vpoint_list[i].move(c1, c2) self.__dof = vpoint_dof(self.vpoint_list) self.dof_view.setText( f"{self.__dof} ({self.inputs_widget.input_count()})") self.conflict.setVisible(False) self.dof_view.setVisible(True) self.reload_canvas()
def resolve(self) -> None: """Resolve: Using three libraries to solve the system. + Pyslvs + Python-Solvespace + Sketch Solve """ for b, d, a in self.inputs_widget.input_pairs(): if b == d: self.vpoint_list[b].set_offset(a) solve_kernel = self.prefer.planar_solver_option input_pair = {(b, d): a for b, d, a in self.inputs_widget.input_pairs()} try: if solve_kernel == Kernel.PYSLVS: result = expr_solving( self.get_triangle(), self.vpoint_list, input_pair ) elif solve_kernel == Kernel.SOLVESPACE: result, _ = _slvs_solve( self.vpoint_list, input_pair if not self.free_move_button.isChecked() else {} ) elif solve_kernel == Kernel.SKETCH_SOLVE: result = SolverSystem(self.vpoint_list, input_pair).solve() else: raise ValueError("incorrect kernel") except ValueError as error: # Error: Show warning without update data. if self.prefer.console_error_option: logger.warn(format_exc()) error_text = f"Error: {error}" self.conflict.setToolTip(error_text) self.conflict.setStatusTip(error_text) self.conflict.setVisible(True) self.dof_view.setVisible(False) else: self.entities_point.update_current_position(result) for i, c in enumerate(result): if isinstance(c[0], float): self.vpoint_list[i].move(c) else: c1, c2 = c self.vpoint_list[i].move(c1, c2) self.__dof = vpoint_dof(self.vpoint_list) self.dof_view.setText( f"{self.__dof} ({self.inputs_widget.input_count()})") self.conflict.setVisible(False) self.dof_view.setVisible(True) self.reload_canvas()