def _scale_and_move_molecule(self, m, anchor_x, anchor_y): bbox, bl = m.get_geometry() maxx, maxy, minx, miny = m.bbox() if bl: scale = self.paper.any_to_px(self.paper.standard.bond_length) / bl else: scale = 1 # at first we scale to the standard bond length tr = transform() tr.set_move(-minx, -miny) tr.set_scaling(scale) m.transform(tr) # then we recalculate the bbox and position the molecule # we need to decide pos first, it is vital for bbox calculation and normaly done on draw [a.decide_pos() for a in m.atoms] maxx, maxy, minx, miny = m.bbox() m.move(anchor_x - minx, anchor_y - (maxy - miny) / 2 - miny) # import graphics # self.molecules.append( graphics.rect( self.paper, coords = m.bbox())) # self.molecules.append( graphics.rect( self.paper, coords = (anchor_x + (maxx), 100, anchor_x + (maxx) + 2, 300))) return anchor_x + maxx - minx
def _scale_and_move_molecule(self, m, anchor_x, anchor_y): bbox, bl = m.get_geometry() maxx, maxy, minx, miny = m.bbox() if bl: scale = self.paper.any_to_px(self.paper.standard.bond_length) / bl else: scale = 1 # at first we scale to the standard bond length tr = transform() tr.set_move(-minx, -miny) tr.set_scaling(scale) m.transform(tr) # then we recalculate the bbox and position the molecule # we need to decide pos first, it is vital for bbox calculation and normaly done on draw [a.decide_pos() for a in m.atoms] maxx, maxy, minx, miny = m.bbox() m.move(anchor_x - minx, anchor_y - (maxy - miny) / 2 - miny) #import graphics #self.molecules.append( graphics.rect( self.paper, coords = m.bbox())) #self.molecules.append( graphics.rect( self.paper, coords = (anchor_x + (maxx), 100, anchor_x + (maxx) + 2, 300))) return anchor_x + maxx - minx
def get_transformed_template(self, n, coords, type="empty", paper=None): """type is type of connection - 'bond', 'atom1'(for single atom), 'atom2'(for atom with more than 1 bond), 'empty'""" pap = paper or Store.app.paper pap.onread_id_sandbox_activate() # must be here to mangle the ids current = molecule(pap, package=self.templates[n]) pap.onread_id_sandbox_finish(apply_to=[current]) # id mangling current.name = "" self._scale_ratio = 1 trans = transform() # type empty - just draws the template - no conection if type == "empty": xt1, yt1 = current.t_atom.get_xy() xt2, yt2 = current.next_to_t_atom.get_xy() x1, y1 = coords bond_length = Screen.any_to_px(Store.app.paper.standard.bond_length) current.delete_items([current.t_atom], redraw=0, delete_single_atom=0) trans.set_move(-xt2, -yt2) trans.set_scaling(bond_length / math.sqrt((xt1 - xt2) ** 2 + (yt1 - yt2) ** 2)) trans.set_move(x1, y1) # type atom elif type == "atom1" or type == "atom2": xt1, yt1 = current.t_atom.get_xy() xt2, yt2 = current.next_to_t_atom.get_xy() x1, y1, x2, y2 = coords trans.set_move(-xt2, -yt2) trans.set_scaling( math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) / math.sqrt((xt1 - xt2) ** 2 + (yt1 - yt2) ** 2) ) trans.set_rotation(math.atan2(xt1 - xt2, yt1 - yt2) - math.atan2(x1 - x2, y1 - y2)) trans.set_move(x2, y2) # type bond elif type == "bond": if not (current.t_bond_first and current.t_bond_second): warn("this template is not capable to be added to bond - sorry.") return None current.delete_items([current.t_atom], redraw=0, delete_single_atom=0) xt1, yt1 = current.t_bond_first.get_xy() xt2, yt2 = current.t_bond_second.get_xy() x1, y1, x2, y2 = coords self._scale_ratio = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2) / math.sqrt( (xt1 - xt2) ** 2 + (yt1 - yt2) ** 2 ) # further needed for bond.bond_width transformation trans.set_move(-xt1, -yt1) trans.set_rotation(math.atan2(xt1 - xt2, yt1 - yt2) - math.atan2(x1 - x2, y1 - y2)) trans.set_scaling(self._scale_ratio) trans.set_move(x1, y1) self.transform_template(current, trans) # remove obsolete info from template if type == "atom1": current.delete_items([current.t_atom], redraw=0, delete_single_atom=0) elif type == "atom2": current.t_atom.x = x1 current.t_atom.y = y1 current.t_atom = None current.t_bond_first = None current.t_bond_second = None # return ready template return current
def get_transformed_template(self, n, coords, type='empty', paper=None): """type is type of connection - 'bond', 'atom1'(for single atom), 'atom2'(for atom with more than 1 bond), 'empty'""" pap = paper or Store.app.paper pap.onread_id_sandbox_activate() # must be here to mangle the ids current = molecule(pap, package=self.templates[n]) pap.onread_id_sandbox_finish(apply_to=[current]) # id mangling current.name = '' self._scale_ratio = 1 trans = transform() # type empty - just draws the template - no conection if type == 'empty': xt1, yt1 = current.t_atom.get_xy() xt2, yt2 = current.next_to_t_atom.get_xy() x1, y1 = coords bond_length = Screen.any_to_px( Store.app.paper.standard.bond_length) current.delete_items([current.t_atom], redraw=0, delete_single_atom=0) trans.set_move(-xt2, -yt2) trans.set_scaling(bond_length / math.sqrt((xt1 - xt2)**2 + (yt1 - yt2)**2)) trans.set_move(x1, y1) #type atom elif type == 'atom1' or type == 'atom2': xt1, yt1 = current.t_atom.get_xy() xt2, yt2 = current.next_to_t_atom.get_xy() x1, y1, x2, y2 = coords trans.set_move(-xt2, -yt2) trans.set_scaling( math.sqrt((x1 - x2)**2 + (y1 - y2)**2) / math.sqrt((xt1 - xt2)**2 + (yt1 - yt2)**2)) trans.set_rotation( math.atan2(xt1 - xt2, yt1 - yt2) - math.atan2(x1 - x2, y1 - y2)) trans.set_move(x2, y2) #type bond elif type == 'bond': if not (current.t_bond_first and current.t_bond_second): warn( "this template is not capable to be added to bond - sorry." ) return None current.delete_items([current.t_atom], redraw=0, delete_single_atom=0) xt1, yt1 = current.t_bond_first.get_xy() xt2, yt2 = current.t_bond_second.get_xy() x1, y1, x2, y2 = coords self._scale_ratio = math.sqrt( (x1 - x2)**2 + (y1 - y2)**2) / math.sqrt( (xt1 - xt2)**2 + (yt1 - yt2)** 2) # further needed for bond.bond_width transformation trans.set_move(-xt1, -yt1) trans.set_rotation( math.atan2(xt1 - xt2, yt1 - yt2) - math.atan2(x1 - x2, y1 - y2)) trans.set_scaling(self._scale_ratio) trans.set_move(x1, y1) self.transform_template(current, trans) #remove obsolete info from template if type == 'atom1': current.delete_items([current.t_atom], redraw=0, delete_single_atom=0) elif type == 'atom2': current.t_atom.x = x1 current.t_atom.y = y1 current.t_atom = None current.t_bond_first = None current.t_bond_second = None #return ready template return current