228 if rung not in rungs: |
228 if rung not in rungs: |
229 rungs.append(rung) |
229 rungs.append(rung) |
230 if len(rungs) > 1: |
230 if len(rungs) > 1: |
231 raise ValueError, _("Ladder element with id %d is on more than one rung.")%instance["id"] |
231 raise ValueError, _("Ladder element with id %d is on more than one rung.")%instance["id"] |
232 element = self.FindElementById(instance["id"]) |
232 element = self.FindElementById(instance["id"]) |
233 element_connectors = element.GetConnectors() |
233 element_connectors = element.GetConnectors() |
234 self.Rungs[rungs[0]].SelectElement(element) |
234 self.Rungs[rungs[0]].SelectElement(element) |
235 for wire, num in element_connectors["inputs"][0].GetWires(): |
235 for wire, num in element_connectors["inputs"][0].GetWires(): |
236 self.Rungs[rungs[0]].SelectElement(wire) |
236 self.Rungs[rungs[0]].SelectElement(wire) |
237 wx.CallAfter(self.RefreshPosition, element) |
237 wx.CallAfter(self.RefreshPosition, element) |
238 elif instance["type"] == "comment": |
238 elif instance["type"] == "comment": |
239 element = self.FindElementById(instance["id"]) |
239 element = self.FindElementById(instance["id"]) |
240 pos = element.GetPosition() |
240 pos = element.GetPosition() |
241 i = 0 |
241 i = 0 |
242 inserted = False |
242 inserted = False |
243 while i < len(self.RungComments) and not inserted: |
243 while i < len(self.RungComments) and not inserted: |
244 ipos = self.RungComments[i].GetPosition() |
244 ipos = self.RungComments[i].GetPosition() |
245 if pos[1] < ipos[1]: |
245 if pos[1] < ipos[1]: |
246 self.RungComments.insert(i, element) |
246 self.RungComments.insert(i, element) |
247 inserted = True |
247 inserted = True |
248 i += 1 |
248 i += 1 |
249 if not inserted: |
249 if not inserted: |
250 self.RungComments.append(element) |
250 self.RungComments.append(element) |
251 |
251 |
252 #------------------------------------------------------------------------------- |
252 #------------------------------------------------------------------------------- |
253 # Search Element functions |
253 # Search Element functions |
254 #------------------------------------------------------------------------------- |
254 #------------------------------------------------------------------------------- |
255 |
255 |
256 def FindRung(self, element): |
256 def FindRung(self, element): |
260 return None |
260 return None |
261 |
261 |
262 def FindElement(self, event, exclude_group = False, connectors = True): |
262 def FindElement(self, event, exclude_group = False, connectors = True): |
263 if self.GetDrawingMode() == FREEDRAWING_MODE: |
263 if self.GetDrawingMode() == FREEDRAWING_MODE: |
264 return Viewer.FindElement(self, event, exclude_group, connectors) |
264 return Viewer.FindElement(self, event, exclude_group, connectors) |
265 |
265 |
266 dc = self.GetLogicalDC() |
266 dc = self.GetLogicalDC() |
267 pos = event.GetLogicalPosition(dc) |
267 pos = event.GetLogicalPosition(dc) |
268 if self.SelectedElement and not isinstance(self.SelectedElement, (Graphic_Group, Wire)): |
268 if self.SelectedElement and not isinstance(self.SelectedElement, (Graphic_Group, Wire)): |
269 if self.SelectedElement.HitTest(pos, connectors) or self.SelectedElement.TestHandle(pos) != (0, 0): |
269 if self.SelectedElement.HitTest(pos, connectors) or self.SelectedElement.TestHandle(pos) != (0, 0): |
270 return self.SelectedElement |
270 return self.SelectedElement |
343 if len(elements) > 0: |
343 if len(elements) > 0: |
344 self.SelectedElement = Graphic_Group(self) |
344 self.SelectedElement = Graphic_Group(self) |
345 self.SelectedElement.SetElements(elements) |
345 self.SelectedElement.SetElements(elements) |
346 self.SelectedElement.SetSelected(True) |
346 self.SelectedElement.SetSelected(True) |
347 elif self.Mode == MODE_SELECTION and self.SelectedElement: |
347 elif self.Mode == MODE_SELECTION and self.SelectedElement: |
348 dc = self.GetLogicalDC() |
348 dc = self.GetLogicalDC() |
349 if not isinstance(self.SelectedElement, Graphic_Group): |
349 if not isinstance(self.SelectedElement, Graphic_Group): |
350 if self.IsWire(self.SelectedElement): |
350 if self.IsWire(self.SelectedElement): |
351 result = self.SelectedElement.TestSegment(event.GetLogicalPosition(dc), True) |
351 result = self.SelectedElement.TestSegment(event.GetLogicalPosition(dc), True) |
352 if result and result[1] in [EAST, WEST]: |
352 if result and result[1] in [EAST, WEST]: |
353 self.SelectedElement.SetSelectedSegment(result[0]) |
353 self.SelectedElement.SetSelectedSegment(result[0]) |
498 if len(self.Rungs) > 0: |
498 if len(self.Rungs) > 0: |
499 bbox = self.Rungs[-1].GetBoundingBox() |
499 bbox = self.Rungs[-1].GetBoundingBox() |
500 starty = bbox.y + bbox.height |
500 starty = bbox.y + bbox.height |
501 starty += LD_OFFSET[1] |
501 starty += LD_OFFSET[1] |
502 rung = Graphic_Group(self) |
502 rung = Graphic_Group(self) |
503 |
503 |
504 # Create comment |
504 # Create comment |
505 id = self.GetNewId() |
505 id = self.GetNewId() |
506 comment = Comment(self, _("Comment"), id) |
506 comment = Comment(self, _("Comment"), id) |
507 comment.SetPosition(startx, starty) |
507 comment.SetPosition(startx, starty) |
508 comment.SetSize(LD_COMMENT_DEFAULTSIZE[0], LD_COMMENT_DEFAULTSIZE[1]) |
508 comment.SetSize(LD_COMMENT_DEFAULTSIZE[0], LD_COMMENT_DEFAULTSIZE[1]) |
509 self.AddComment(comment) |
509 self.AddComment(comment) |
510 self.RungComments.append(comment) |
510 self.RungComments.append(comment) |
511 self.Controler.AddEditedElementComment(self.TagName, id) |
511 self.Controler.AddEditedElementComment(self.TagName, id) |
512 self.RefreshCommentModel(comment) |
512 self.RefreshCommentModel(comment) |
513 starty += LD_COMMENT_DEFAULTSIZE[1] + LD_OFFSET[1] |
513 starty += LD_COMMENT_DEFAULTSIZE[1] + LD_OFFSET[1] |
514 |
514 |
515 # Create LeftPowerRail |
515 # Create LeftPowerRail |
516 id = self.GetNewId() |
516 id = self.GetNewId() |
517 leftpowerrail = LD_PowerRail(self, LEFTRAIL, id) |
517 leftpowerrail = LD_PowerRail(self, LEFTRAIL, id) |
518 leftpowerrail.SetPosition(startx, starty) |
518 leftpowerrail.SetPosition(startx, starty) |
519 leftpowerrail_connectors = leftpowerrail.GetConnectors() |
519 leftpowerrail_connectors = leftpowerrail.GetConnectors() |
520 self.AddBlock(leftpowerrail) |
520 self.AddBlock(leftpowerrail) |
521 rung.SelectElement(leftpowerrail) |
521 rung.SelectElement(leftpowerrail) |
522 self.Controler.AddEditedElementPowerRail(self.TagName, id, LEFTRAIL) |
522 self.Controler.AddEditedElementPowerRail(self.TagName, id, LEFTRAIL) |
523 self.RefreshPowerRailModel(leftpowerrail) |
523 self.RefreshPowerRailModel(leftpowerrail) |
524 |
524 |
525 # Create Coil |
525 # Create Coil |
526 id = self.GetNewId() |
526 id = self.GetNewId() |
527 coil = LD_Coil(self, values["type"], values["name"], id) |
527 coil = LD_Coil(self, values["type"], values["name"], id) |
528 coil.SetPosition(startx, starty + (LD_LINE_SIZE - LD_ELEMENT_SIZE[1]) / 2) |
528 coil.SetPosition(startx, starty + (LD_LINE_SIZE - LD_ELEMENT_SIZE[1]) / 2) |
529 coil_connectors = coil.GetConnectors() |
529 coil_connectors = coil.GetConnectors() |
530 self.AddBlock(coil) |
530 self.AddBlock(coil) |
531 rung.SelectElement(coil) |
531 rung.SelectElement(coil) |
532 self.Controler.AddEditedElementCoil(self.TagName, id) |
532 self.Controler.AddEditedElementCoil(self.TagName, id) |
533 |
533 |
534 # Create Wire between LeftPowerRail and Coil |
534 # Create Wire between LeftPowerRail and Coil |
535 wire = Wire(self) |
535 wire = Wire(self) |
536 start_connector = coil_connectors["inputs"][0] |
536 start_connector = coil_connectors["inputs"][0] |
537 end_connector = leftpowerrail_connectors["outputs"][0] |
537 end_connector = leftpowerrail_connectors["outputs"][0] |
538 start_connector.Connect((wire, 0), False) |
538 start_connector.Connect((wire, 0), False) |
539 end_connector.Connect((wire, -1), False) |
539 end_connector.Connect((wire, -1), False) |
540 wire.ConnectStartPoint(None, start_connector) |
540 wire.ConnectStartPoint(None, start_connector) |
541 wire.ConnectEndPoint(None, end_connector) |
541 wire.ConnectEndPoint(None, end_connector) |
542 self.AddWire(wire) |
542 self.AddWire(wire) |
543 rung.SelectElement(wire) |
543 rung.SelectElement(wire) |
544 |
544 |
545 # Create RightPowerRail |
545 # Create RightPowerRail |
546 id = self.GetNewId() |
546 id = self.GetNewId() |
547 rightpowerrail = LD_PowerRail(self, RIGHTRAIL, id) |
547 rightpowerrail = LD_PowerRail(self, RIGHTRAIL, id) |
548 rightpowerrail.SetPosition(startx, starty) |
548 rightpowerrail.SetPosition(startx, starty) |
549 rightpowerrail_connectors = rightpowerrail.GetConnectors() |
549 rightpowerrail_connectors = rightpowerrail.GetConnectors() |
550 self.AddBlock(rightpowerrail) |
550 self.AddBlock(rightpowerrail) |
551 rung.SelectElement(rightpowerrail) |
551 rung.SelectElement(rightpowerrail) |
552 self.Controler.AddEditedElementPowerRail(self.TagName, id, RIGHTRAIL) |
552 self.Controler.AddEditedElementPowerRail(self.TagName, id, RIGHTRAIL) |
553 |
553 |
554 # Create Wire between LeftPowerRail and Coil |
554 # Create Wire between LeftPowerRail and Coil |
555 wire = Wire(self) |
555 wire = Wire(self) |
556 start_connector = rightpowerrail_connectors["inputs"][0] |
556 start_connector = rightpowerrail_connectors["inputs"][0] |
557 end_connector = coil_connectors["outputs"][0] |
557 end_connector = coil_connectors["outputs"][0] |
558 start_connector.Connect((wire, 0), False) |
558 start_connector.Connect((wire, 0), False) |
808 dialog.SetValues({"name":"","type":COIL_NORMAL}) |
808 dialog.SetValues({"name":"","type":COIL_NORMAL}) |
809 if dialog.ShowModal() == wx.ID_OK: |
809 if dialog.ShowModal() == wx.ID_OK: |
810 values = dialog.GetValues() |
810 values = dialog.GetValues() |
811 powerrail = right_elements[0].GetParentBlock() |
811 powerrail = right_elements[0].GetParentBlock() |
812 index = 0 |
812 index = 0 |
813 for right_element in right_elements: |
813 for right_element in right_elements: |
814 index = max(index, powerrail.GetConnectorIndex(right_element)) |
814 index = max(index, powerrail.GetConnectorIndex(right_element)) |
815 powerrail.InsertConnector(index + 1) |
815 powerrail.InsertConnector(index + 1) |
816 powerrail.RefreshModel() |
816 powerrail.RefreshModel() |
817 connectors = powerrail.GetConnectors() |
817 connectors = powerrail.GetConnectors() |
818 |
818 |
819 # Create Coil |
819 # Create Coil |
820 id = self.GetNewId() |
820 id = self.GetNewId() |
821 coil = LD_Coil(self, values["type"], values["name"], id) |
821 coil = LD_Coil(self, values["type"], values["name"], id) |
822 pos = blocks[0].GetPosition() |
822 pos = blocks[0].GetPosition() |
823 coil.SetPosition(pos[0], pos[1] + LD_LINE_SIZE) |
823 coil.SetPosition(pos[0], pos[1] + LD_LINE_SIZE) |
824 self.AddBlock(coil) |
824 self.AddBlock(coil) |
825 rung.SelectElement(coil) |
825 rung.SelectElement(coil) |
826 self.Controler.AddEditedElementCoil(self.TagName, id) |
826 self.Controler.AddEditedElementCoil(self.TagName, id) |
827 coil_connectors = coil.GetConnectors() |
827 coil_connectors = coil.GetConnectors() |
828 |
828 |
829 # Create Wire between LeftPowerRail and Coil |
829 # Create Wire between LeftPowerRail and Coil |
830 wire = Wire(self) |
830 wire = Wire(self) |
831 connectors["inputs"][index + 1].Connect((wire, 0), False) |
831 connectors["inputs"][index + 1].Connect((wire, 0), False) |
832 coil_connectors["outputs"][0].Connect((wire, -1), False) |
832 coil_connectors["outputs"][0].Connect((wire, -1), False) |
833 wire.ConnectStartPoint(None, connectors["inputs"][index + 1]) |
833 wire.ConnectStartPoint(None, connectors["inputs"][index + 1]) |
834 wire.ConnectEndPoint(None, coil_connectors["outputs"][0]) |
834 wire.ConnectEndPoint(None, coil_connectors["outputs"][0]) |
835 self.AddWire(wire) |
835 self.AddWire(wire) |
836 rung.SelectElement(wire) |
836 rung.SelectElement(wire) |
837 left_elements.reverse() |
837 left_elements.reverse() |
838 |
838 |
839 for i, left_element in enumerate(left_elements): |
839 for i, left_element in enumerate(left_elements): |
840 # Create Wire between LeftPowerRail and Coil |
840 # Create Wire between LeftPowerRail and Coil |
841 new_wire = Wire(self) |
841 new_wire = Wire(self) |
842 wires.append(new_wire) |
842 wires.append(new_wire) |
843 coil_connectors["inputs"][0].Connect((new_wire, 0), False) |
843 coil_connectors["inputs"][0].Connect((new_wire, 0), False) |
844 left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False) |
844 left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False) |
845 new_wire.ConnectStartPoint(None, coil_connectors["inputs"][0]) |
845 new_wire.ConnectStartPoint(None, coil_connectors["inputs"][0]) |
846 new_wire.ConnectEndPoint(None, left_element) |
846 new_wire.ConnectEndPoint(None, left_element) |
847 |
847 |
848 self.RefreshPosition(coil) |
848 self.RefreshPosition(coil) |
849 else: |
849 else: |
850 left_elements.reverse() |
850 left_elements.reverse() |
851 right_elements.reverse() |
851 right_elements.reverse() |
852 for i, left_element in enumerate(left_elements): |
852 for i, left_element in enumerate(left_elements): |
1101 onlyone[-1] &= len(wire.EndConnected.GetWires()) == 1 |
1101 onlyone[-1] &= len(wire.EndConnected.GetWires()) == 1 |
1102 leftblock = wire.EndConnected.GetParentBlock() |
1102 leftblock = wire.EndConnected.GetParentBlock() |
1103 pos = leftblock.GetPosition() |
1103 pos = leftblock.GetPosition() |
1104 size = leftblock.GetSize() |
1104 size = leftblock.GetSize() |
1105 maxx = max(maxx, pos[0] + size[0]) |
1105 maxx = max(maxx, pos[0] + size[0]) |
1106 |
1106 |
1107 # Refresh position of element |
1107 # Refresh position of element |
1108 if isinstance(element, LD_Coil): |
1108 if isinstance(element, LD_Coil): |
1109 interval = LD_WIRECOIL_SIZE |
1109 interval = LD_WIRECOIL_SIZE |
1110 else: |
1110 else: |
1111 interval = LD_WIRE_SIZE |
1111 interval = LD_WIRE_SIZE |
1112 if False in onlyone: |
1112 if False in onlyone: |
1113 interval += LD_WIRE_SIZE |
1113 interval += LD_WIRE_SIZE |
1114 movex = maxx + interval - position[0] |
1114 movex = maxx + interval - position[0] |
1115 element.Move(movex, 0) |
1115 element.Move(movex, 0) |
1116 position = element.GetPosition() |
1116 position = element.GetPosition() |
1117 |
1117 |
1118 # Extract blocks connected to inputs |
1118 # Extract blocks connected to inputs |
1119 blocks = [] |
1119 blocks = [] |
1120 for i, connector in enumerate(connectors["inputs"]): |
1120 for i, connector in enumerate(connectors["inputs"]): |
1121 for j, (wire, handle) in enumerate(connector.GetWires()): |
1121 for j, (wire, handle) in enumerate(connector.GetWires()): |
1122 blocks.append(wire.EndConnected.GetParentBlock()) |
1122 blocks.append(wire.EndConnected.GetParentBlock()) |
1123 |
1123 |
1124 for i, connector in enumerate(connectors["inputs"]): |
1124 for i, connector in enumerate(connectors["inputs"]): |
1125 startpoint = connector.GetPosition(False) |
1125 startpoint = connector.GetPosition(False) |
1126 previous_blocks = [] |
1126 previous_blocks = [] |
1127 block_list = [] |
1127 block_list = [] |
1128 start_offset = 0 |
1128 start_offset = 0 |
1166 points = [startpoint, endpoint] |
1166 points = [startpoint, endpoint] |
1167 wire.SetPoints(points, False) |
1167 wire.SetPoints(points, False) |
1168 previous_blocks.append(block) |
1168 previous_blocks.append(block) |
1169 blocks.remove(block) |
1169 blocks.remove(block) |
1170 ExtractNextBlocks(block, block_list) |
1170 ExtractNextBlocks(block, block_list) |
1171 |
1171 |
1172 element.RefreshModel(False) |
1172 element.RefreshModel(False) |
1173 if recursive: |
1173 if recursive: |
1174 for connector in connectors["outputs"]: |
1174 for connector in connectors["outputs"]: |
1175 for wire, handle in connector.GetWires(): |
1175 for wire, handle in connector.GetWires(): |
1176 self.RefreshPosition(wire.StartConnected.GetParentBlock()) |
1176 self.RefreshPosition(wire.StartConnected.GetParentBlock()) |
1177 |
1177 |
1178 def RefreshRungs(self, movey, fromidx): |
1178 def RefreshRungs(self, movey, fromidx): |
1179 if movey != 0: |
1179 if movey != 0: |
1180 for i in xrange(fromidx, len(self.Rungs)): |
1180 for i in xrange(fromidx, len(self.Rungs)): |
1181 self.RungComments[i].Move(0, movey) |
1181 self.RungComments[i].Move(0, movey) |
1182 self.RungComments[i].RefreshModel() |
1182 self.RungComments[i].RefreshModel() |