LDViewer.py
changeset 80 c798a68c5560
parent 67 3a1b0afdaf84
child 90 2245e8776086
equal deleted inserted replaced
79:b22f661cbcfb 80:c798a68c5560
   186             else:
   186             else:
   187                 self.RungComments.insert(i, None)
   187                 self.RungComments.insert(i, None)
   188     
   188     
   189     def loadInstance(self, instance, ids):
   189     def loadInstance(self, instance, ids):
   190         Viewer.loadInstance(self, instance, ids)
   190         Viewer.loadInstance(self, instance, ids)
   191         if instance["type"] == "leftPowerRail":
   191         if self.GetDrawingMode() != FREEDRAWING_MODE:
   192             element = self.FindElementById(instance["id"])
   192             if instance["type"] == "leftPowerRail":
   193             rung = Graphic_Group(self)
   193                 element = self.FindElementById(instance["id"])
   194             rung.SelectElement(element)
   194                 rung = Graphic_Group(self)
   195             self.Rungs.append(rung)
   195                 rung.SelectElement(element)
   196         elif instance["type"] == "rightPowerRail":
   196                 self.Rungs.append(rung)
   197             rungs = []
   197             elif instance["type"] == "rightPowerRail":
   198             for connector in instance["connectors"]:
   198                 rungs = []
   199                 for link in connector["links"]:
   199                 for connector in instance["connectors"]:
       
   200                     for link in connector["links"]:
       
   201                         connected = self.FindElementById(link["refLocalId"])
       
   202                         rung = self.FindRung(connected)
       
   203                         if rung not in rungs:
       
   204                             rungs.append(rung)
       
   205                 if len(rungs) > 1:
       
   206                     raise "ValueError", "Ladder element with id %d is on more than one rung."%instance["id"]
       
   207                 element = self.FindElementById(instance["id"])
       
   208                 self.Rungs[rungs[0]].SelectElement(element)
       
   209                 for connector in element.GetConnectors():
       
   210                     for wire, num in connector.GetWires():
       
   211                         self.Rungs[rungs[0]].SelectElement(wire)
       
   212                 self.RefreshPosition(element)
       
   213             elif instance["type"] in ["contact", "coil"]:
       
   214                 rungs = []
       
   215                 for link in instance["connectors"]["input"]["links"]:
   200                     connected = self.FindElementById(link["refLocalId"])
   216                     connected = self.FindElementById(link["refLocalId"])
   201                     rung = self.FindRung(connected)
   217                     rung = self.FindRung(connected)
   202                     if rung not in rungs:
   218                     if rung not in rungs:
   203                         rungs.append(rung)
   219                         rungs.append(rung)
   204             if len(rungs) > 1:
   220                 if len(rungs) > 1:
   205                 raise "ValueError", "Ladder element with id %d is on more than one rung."%instance["id"]
   221                     raise "ValueError", "Ladder element with id %d is on more than one rung."%instance["id"]
   206             element = self.FindElementById(instance["id"])
   222                 element = self.FindElementById(instance["id"])
   207             self.Rungs[rungs[0]].SelectElement(element)
   223                 self.Rungs[rungs[0]].SelectElement(element)
   208             for connector in element.GetConnectors():
   224                 for wire, num in element.GetConnectors()["input"].GetWires():
   209                 for wire, num in connector.GetWires():
       
   210                     self.Rungs[rungs[0]].SelectElement(wire)
   225                     self.Rungs[rungs[0]].SelectElement(wire)
   211             if self.GetDrawingMode() != FREEDRAWING_MODE:
       
   212                 self.RefreshPosition(element)
   226                 self.RefreshPosition(element)
   213         elif instance["type"] in ["contact", "coil"]:
   227             elif instance["type"] == "comment":
   214             rungs = []
   228                 element = self.FindElementById(instance["id"])
   215             for link in instance["connectors"]["input"]["links"]:
   229                 pos = element.GetPosition()
   216                 connected = self.FindElementById(link["refLocalId"])
   230                 i = 0
   217                 rung = self.FindRung(connected)
   231                 inserted = False
   218                 if rung not in rungs:
   232                 while i < len(self.RungComments) and not inserted: 
   219                     rungs.append(rung)
   233                     ipos = self.RungComments[i].GetPosition()
   220             if len(rungs) > 1:
   234                     if pos[1] < ipos[1]:
   221                 raise "ValueError", "Ladder element with id %d is on more than one rung."%instance["id"]
   235                         self.RungComments.insert(i, element)
   222             element = self.FindElementById(instance["id"])
   236                         inserted = True
   223             self.Rungs[rungs[0]].SelectElement(element)
   237                     i += 1
   224             for wire, num in element.GetConnectors()["input"].GetWires():
   238                 if not inserted:
   225                 self.Rungs[rungs[0]].SelectElement(wire)
   239                     self.RungComments.append(element)
   226             if self.GetDrawingMode() != FREEDRAWING_MODE:
       
   227                 self.RefreshPosition(element)
       
   228         elif instance["type"] == "comment":
       
   229             element = self.FindElementById(instance["id"])
       
   230             pos = element.GetPosition()
       
   231             i = 0
       
   232             inserted = False
       
   233             while i < len(self.RungComments) and not inserted: 
       
   234                 ipos = self.RungComments[i].GetPosition()
       
   235                 if pos[1] < ipos[1]:
       
   236                     self.RungComments.insert(i, element)
       
   237                     inserted = True
       
   238                 i += 1
       
   239             if not inserted:
       
   240                 self.RungComments.append(element)
       
   241 
   240 
   242 #-------------------------------------------------------------------------------
   241 #-------------------------------------------------------------------------------
   243 #                          Search Element functions
   242 #                          Search Element functions
   244 #-------------------------------------------------------------------------------
   243 #-------------------------------------------------------------------------------
   245 
   244 
   247         for i, rung in enumerate(self.Rungs):
   246         for i, rung in enumerate(self.Rungs):
   248             if rung.IsElementIn(element):
   247             if rung.IsElementIn(element):
   249                 return i
   248                 return i
   250         return None
   249         return None
   251 
   250 
   252     def FindElement(self, pos):
   251     def FindElement(self, pos, exclude_group = False):
   253         if self.GetDrawingMode() == FREEDRAWING_MODE:
   252         if self.GetDrawingMode() == FREEDRAWING_MODE:
   254             return Viewer.FindElement(self, pos)
   253             return Viewer.FindElement(self, pos, exclude_group)
   255         
   254         
       
   255         if self.SelectedElement and not (exclude_group and isinstance(self.SelectedElement, Graphic_Group)):
       
   256             if self.SelectedElement.HitTest(pos) or self.SelectedElement.TestHandle(pos) != (0, 0):
       
   257                 return self.SelectedElement
   256         elements = []
   258         elements = []
   257         for element in self.GetElements(sort_wires=True):
   259         for element in self.GetElements(sort_wires=True):
   258             if element.HitTest(pos) or element.TestHandle(pos) != (0, 0):
   260             if element.HitTest(pos) or element.TestHandle(pos) != (0, 0):
   259                 elements.append(element)
   261                 elements.append(element)
   260         if len(elements) == 1:
   262         if len(elements) == 1: