def __getPath(self, row: int): """Using result data to generate paths of mechanism.""" Result = self.mechanism_data[row] point_index = sorted( int(tag.replace('P', '')) for tag in Result if tag.replace('P', '').isdigit()) exprs = [] for expr in Result['Expression'].split(';'): func = strbefore(expr, '[') params = strbetween(expr, '[', ']').split(',') target = strbetween(expr, '(', ')') params.insert(0, func) params.append(target) exprs.append(tuple(params)) pos = {} for name in Result['pos']: try: pos[name] = Result['P{}'.format(name)] except KeyError: pos[name] = Result['pos'][name] vpoints = graph2vpoints(Graph(Result['Graph']), pos, Result['cus'], Result['same']) return [ path for i, path in enumerate( expr_path(tuple(exprs), {n: 'P{}'.format(n) for n in range(len(vpoints))}, vpoints, 3)) if (i in point_index) ]
def mergeResult(self, row, path): """Merge result function of dimensional synthesis.""" Result = self.DimensionalSynthesis.mechanism_data[row] #exp_symbol = ['A', 'B', 'C', 'D', 'E'] exp_symbol = [] for exp in Result['Link_Expression'].split(';'): for name in strbetween(exp, '[', ']').split(','): if name not in exp_symbol: exp_symbol.append(name) self.CommandStack.beginMacro( "Merge mechanism kit from {Dimensional Synthesis}") tmp_dict = {} for tag in sorted(exp_symbol): tmp_dict[tag] = self.addPoint( Result[tag][0], Result[tag][1], color=("Dark-Orange" if (tag in Result['Target']) else None)) for i, exp in enumerate(Result['Link_Expression'].split(';')): self.addNormalLink( tmp_dict[name] for name in strbetween(exp, '[', ']').split(',')) if i == 0: self.constrainLink(self.EntitiesLink.rowCount() - 1) self.CommandStack.endMacro() #Add the path. i = 0 while "Algorithm_path_{}".format(i) in self.InputsWidget.pathData: i += 1 self.InputsWidget.addPath("Algorithm_path_{}".format(i), path) self.MainCanvas.zoomToFit()
def __symbols(self) -> Set[List[str]]: """Return all symbols.""" expr_list = set([]) for expr in self.expr_show.text().split(';'): param_list = strbetween(expr, '[', ']').split(',') param_list.append(strbetween(expr, '(', ')')) expr_list.update(param_list) return expr_list
def outputTo(self, formatName: str, formatChoose: List[str]) -> str: """Simple to support mutiple format.""" suffix0 = strbetween(formatChoose[0], '(', ')').split('*')[-1] file_name, suffix = QFileDialog.getSaveFileName( self, "Save to {}...".format(formatName), self.env + '/' + self.FileWidget.file_name.baseName() + suffix0, ';;'.join(formatChoose)) if file_name: suffix = strbetween(suffix, '(', ')').split('*')[-1] print("Format: {}".format(suffix)) if QFileInfo(file_name).suffix() != suffix[1:]: file_name += suffix self.setLocate(QFileInfo(file_name).absolutePath()) return file_name
def __init__(self, mechanism, Path, parent=None): super(DynamicCanvas, self).__init__(parent) self.mechanism = mechanism self.Path.path = Path self.length = 0 for path in self.Path.path: l = len(path) if l > self.length: self.length = l self.targetPath = self.mechanism['Target'] self.index = 0 #exp_symbol = ('A', 'B', 'C', 'D', 'E') self.exp_symbol = set() self.links = [] for exp in self.mechanism['Link_Expression'].split(';'): names = strbetween(exp, '[', ']').split(',') self.links.append(tuple(names)) for name in names: self.exp_symbol.add(name) self.exp_symbol = sorted(self.exp_symbol, key=lambda e: int(e.replace('P', ''))) #Error self.ERROR = False self.no_error = 0 #Timer start. timer = QTimer(self) timer.timeout.connect(self.change_index) timer.start(17)
def on_load_button_clicked(self): """Show up the dialog to load structure data.""" dlg = CollectionsDialog(self) dlg.show() if not dlg.exec_(): return self.profile_name = dlg.name_loaded params = dlg.mech_params #Add customize joints. G = Graph(params['Graph']) self.setGraph(G, params['pos']) self.PreviewWindow.cus = params['cus'] self.PreviewWindow.same = params['same'] #Grounded setting. drivers = set(params['Driver']) followers = set(params['Follower']) for row, link in enumerate(G.nodes): points = set('P{}'.format(n) for n, edge in edges_view(G) if link in edge) if (drivers | followers) <= points: self.grounded_list.setCurrentRow(row) break #Driver, Follower, Target for expr in params['Expression'].split(';'): if strbefore(expr, '[') != 'PLAP': continue base = strbetween(expr, '[', ']').split(',')[0] self.__find_follower_to_remove(base) rotator = strbetween(expr, '(', ')') self.driver_list.addItem("({}, {})".format(base, rotator)) self.__setWarning(self.driver_label, not self.driver_list.count()) self.target_list.addItems(list(params['Target'])) self.__setWarning(self.target_label, not self.target_list.count() > 0) #Constraints self.constraint_list.addItems( [", ".join(c) for c in params['constraint']]) #Expression for expr in params['Expression'].split(';'): func = strbefore(expr, '[') target = strbetween(expr, '(', ')') params = strbetween(expr, '[', ']').split(',') params.insert(0, func) params.append(target) self.__addSolution(*params) self.PreviewWindow.setStatus(target, True) self.__setWarning(self.expression_list_label, not self.PreviewWindow.isAllLock())
def on_expression_pop_clicked(self): """Remove the last solution.""" count = self.expression_list.count() if not count: return expr = self.expression_list.item(count - 1).text() self.expression_list.takeItem(count - 1) self.PreviewWindow.setStatus(strbetween(expr, '(', ')'), False) self.__setParmBind()
def __init__(self, mechanism, Path, parent=None): super(PreviewDialog, self).__init__(parent) self.setupUi(self) self.mechanism = mechanism self.setWindowTitle("Preview: {} (max {} generations)".format( self.mechanism['Algorithm'], self.mechanism['lastGen'])) self.setWindowFlags(self.windowFlags() | Qt.WindowMaximizeButtonHint) self.main_splitter.setSizes([800, 100]) self.splitter.setSizes([100, 100, 100]) previewWidget = DynamicCanvas(self.mechanism, Path, self) self.left_layout.insertWidget(0, previewWidget) #Basic information link_tags = [] for expr in self.mechanism['Expression'].split(';'): for p in strbetween(expr, '[', ']').split(','): if ('L' in p) and (p not in link_tags): link_tags.append(p) self.basic_label.setText("\n".join([ "{}: {}".format(tag, self.mechanism[tag]) for tag in ['Algorithm', 'time'] ] + [ "{}: {}".format(tag, self.mechanism[tag]) for tag in self.mechanism['Driver'] ] + [ "{}: {}".format(tag, self.mechanism[tag]) for tag in self.mechanism['Follower'] ] + [ "{}: {}".format(tag, self.mechanism[tag]) for tag in sorted(link_tags) ])) #Algorithm information interrupt = self.mechanism['interrupted'] fitness = self.mechanism['TimeAndFitness'][-1] self.algorithm_label.setText("<html><head/><body><p>" + "<br/>".join( ["Max generation: {}".format(self.mechanism['lastGen'])] + [ "Fitness: {}".format(fitness if type(fitness) == float else fitness[1]) ] + [ "<img src=\"{}\" width=\"15\"/>".format( ":/icons/task-completed.png" if interrupt == 'False' else ":/icons/question-mark.png" if interrupt == 'N/A' else ":/icons/interrupted.png") + "Interrupted at: {}".format(interrupt) ] + [ "{}: {}".format(k, v) for k, v in self.mechanism['settings'].items() ]) + "</p></body></html>") #Hardware information self.hardware_label.setText("\n".join([ "{}: {}".format(tag, self.mechanism['hardwareInfo'][tag]) for tag in ['os', 'memory', 'cpu'] ]))
def inputFrom(self, formatName: str, formatChoose: List[str], multiple: bool = False) -> str: """Get file name(s).""" args = ("Open {} file{}...".format(formatName, 's' if multiple else ''), self.env, ';;'.join(formatChoose)) if multiple: file_name_s, suffix = QFileDialog.getOpenFileNames(self, *args) else: file_name_s, suffix = QFileDialog.getOpenFileName(self, *args) if file_name_s: suffix = strbetween(suffix, '(', ')').split('*')[-1] print("Format: {}".format(suffix)) if type(file_name_s) == str: self.setLocate(QFileInfo(file_name_s).absolutePath()) else: self.setLocate(QFileInfo(file_name_s[0]).absolutePath()) return file_name_s
def from_profile(self, params: Dict[str, Any]): """Simple load by dict object.""" #Add customize joints. G = Graph(params['Graph']) self.setGraph(G, params['pos']) self.cus = params['cus'] self.same = params['same'] #Grounded setting. Driver = set(params['Driver']) Follower = set(params['Follower']) for row, link in enumerate(G.nodes): points = set('P{}'.format(n) for n, edge in edges_view(G) if link in edge) if (Driver | Follower) <= points: self.setGrounded(row) break #Expression for expr in params['Expression'].split(';'): self.setStatus(io.strbetween(expr, '(', ')'), True) self.update()
def __hasSolution(self, index=None): """Set buttons enable if there has solution.""" if index is None: index = self.joint_name.currentIndex() if not index > -1: self.status_show.setText("N/A") self.PLAP_solution.setEnabled(False) self.PLLP_solution.setEnabled(False) return status = self.PreviewWindow.getStatus(index) if not status: status_str = "Not known." elif index in self.PreviewWindow.same: status_str = "Same as P{}.".format(self.PreviewWindow.same[index]) else: status_str = "Grounded." for expr in list_texts(self.expression_list): if index == int(strbetween(expr, '(', ')').replace('P', '')): status_str = "From {}.".format(strbefore(expr, '[')) self.status_show.setText(status_str) self.PLAP_solution.setEnabled(not status) self.PLLP_solution.setEnabled(not status)
def paintEvent(self, event): """Draw the structure.""" width = self.width() height = self.height() self.ox = width / 2 self.oy = height / 2 sq_w = 240 if width <= height: self.zoom = width / sq_w else: self.zoom = height / sq_w super(PreviewCanvas, self).paintEvent(event) self.__drawLimit(sq_w) pen = QPen() pen.setWidth(RADIUS) self.painter.setPen(pen) self.painter.setBrush(QBrush(QColor(226, 219, 190, 150))) #Links for link in self.G.nodes: if link == self.grounded: continue points = [] #Points that is belong with the link. for num, edge in edges_view(self.G): if link in edge: if num in self.same: num = self.same[num] x, y = self.pos[num] points.append((x * self.zoom, y * -self.zoom)) #Customize points. for name, link_ in self.cus.items(): if link == link_: x, y = self.pos[int(name.replace('P', ''))] points.append((x * self.zoom, y * -self.zoom)) self.painter.drawPolygon(*convex_hull(points)) self.painter.setFont(QFont("Arial", self.fontSize)) #Nodes for node, (x, y) in self.pos.items(): if node in self.same: continue x *= self.zoom y *= -self.zoom if node in (self.Driver, self.Target): if node == self.Driver: pen.setColor(colorQt('Red')) elif node == self.Target: pen.setColor(colorQt('Yellow')) self.painter.setPen(pen) self.painter.drawEllipse(QPointF(x, y), RADIUS, RADIUS) if self.getStatus(node): color = colorQt('Dark-Magenta') else: color = colorQt('Green') pen.setColor(color) self.painter.setPen(pen) self.painter.setBrush(QBrush(color)) self.painter.drawEllipse(QPointF(x, y), RADIUS, RADIUS) pen.setColor(colorQt('Black')) self.painter.setPen(pen) #Solutions if self.showSolutions: solutions = self.get_solutions() if solutions: for expr in solutions.split(';'): self._BaseCanvas__drawSolution( io.strbefore(expr, '['), io.strbetween(expr, '[', ']').split(','), io.strbetween(expr, '(', ')'), self.pos) #Text of node. pen.setColor(Qt.black) self.painter.setPen(pen) for node, (x, y) in self.pos.items(): if node in self.same: continue self.painter.drawText( QPointF(x * self.zoom + 2 * RADIUS, y * -self.zoom), 'P{}'.format(node)) self.painter.end()