157 |
157 |
158 #------------------------------------------------------------------------------- |
158 #------------------------------------------------------------------------------- |
159 # Ladder Diagram Graphic elements Viewer class |
159 # Ladder Diagram Graphic elements Viewer class |
160 #------------------------------------------------------------------------------- |
160 #------------------------------------------------------------------------------- |
161 |
161 |
|
162 if wx.VERSION >= (2, 8, 0): |
|
163 import wx.aui |
|
164 |
|
165 class LD_MDIViewer(wx.aui.AuiMDIChildFrame): |
|
166 def __init__(self, parent, tagname, window, controler): |
|
167 wx.aui.AuiMDIChildFrame.__init__(self, parent, -1, title = "") |
|
168 |
|
169 sizer = wx.BoxSizer(wx.HORIZONTAL) |
|
170 |
|
171 self.Viewer = LD_Viewer(self, tagname, window, controler) |
|
172 |
|
173 sizer.AddWindow(self.Viewer, 1, border=0, flag=wx.GROW) |
|
174 |
|
175 self.SetSizer(sizer) |
|
176 |
|
177 def GetViewer(self): |
|
178 return self.Viewer |
|
179 |
162 """ |
180 """ |
163 Class derived from Viewer class that implements a Viewer of Ladder Diagram |
181 Class derived from Viewer class that implements a Viewer of Ladder Diagram |
164 """ |
182 """ |
165 |
183 |
166 class LD_Viewer(Viewer): |
184 class LD_Viewer(Viewer): |
167 |
185 |
168 def __init__(self, parent, window, controler): |
186 def __init__(self, parent, tagname, window, controler): |
169 Viewer.__init__(self, parent, window, controler) |
187 Viewer.__init__(self, parent, tagname, window, controler) |
170 self.Rungs = [] |
188 self.Rungs = [] |
171 self.RungComments = [] |
189 self.RungComments = [] |
172 self.CurrentLanguage = "LD" |
190 self.CurrentLanguage = "LD" |
173 |
191 |
174 #------------------------------------------------------------------------------- |
192 #------------------------------------------------------------------------------- |
450 #------------------------------------------------------------------------------- |
468 #------------------------------------------------------------------------------- |
451 |
469 |
452 def AddLadderRung(self): |
470 def AddLadderRung(self): |
453 dialog = LDElementDialog(self.ParentWindow, "coil") |
471 dialog = LDElementDialog(self.ParentWindow, "coil") |
454 varlist = [] |
472 varlist = [] |
455 vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
473 vars = self.Controler.GetEditedElementInterfaceVars(self.TagName) |
456 if vars: |
474 if vars: |
457 for var in vars: |
475 for var in vars: |
458 if var["Class"] != "Input" and var["Type"] == "BOOL": |
476 if var["Class"] != "Input" and var["Type"] == "BOOL": |
459 varlist.append(var["Name"]) |
477 varlist.append(var["Name"]) |
460 returntype = self.Controler.GetCurrentElementEditingInterfaceReturnType() |
478 returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName) |
461 if returntype == "BOOL": |
479 if returntype == "BOOL": |
462 varlist.append(self.Controler.GetCurrentElementEditingName()) |
480 varlist.append(self.Controler.GetEditedElementName(self.TagName)) |
463 dialog.SetVariables(varlist) |
481 dialog.SetVariables(varlist) |
464 dialog.SetValues({"name":"","type":COIL_NORMAL}) |
482 dialog.SetValues({"name":"","type":COIL_NORMAL}) |
465 if dialog.ShowModal() == wx.ID_OK: |
483 if dialog.ShowModal() == wx.ID_OK: |
466 values = dialog.GetValues() |
484 values = dialog.GetValues() |
467 startx, starty = LD_OFFSET[0], 0 |
485 startx, starty = LD_OFFSET[0], 0 |
475 comment = Comment(self, "Commentaire", id) |
493 comment = Comment(self, "Commentaire", id) |
476 comment.SetPosition(startx, starty) |
494 comment.SetPosition(startx, starty) |
477 comment.SetSize(LD_COMMENT_DEFAULTSIZE[0], LD_COMMENT_DEFAULTSIZE[1]) |
495 comment.SetSize(LD_COMMENT_DEFAULTSIZE[0], LD_COMMENT_DEFAULTSIZE[1]) |
478 self.AddComment(comment) |
496 self.AddComment(comment) |
479 self.RungComments.append(comment) |
497 self.RungComments.append(comment) |
480 self.Controler.AddCurrentElementEditingComment(id) |
498 self.Controler.AddEditedElementComment(self.TagName, id) |
481 self.RefreshCommentModel(comment) |
499 self.RefreshCommentModel(comment) |
482 starty += LD_COMMENT_DEFAULTSIZE[1] + LD_OFFSET[1] |
500 starty += LD_COMMENT_DEFAULTSIZE[1] + LD_OFFSET[1] |
483 # Create LeftPowerRail |
501 # Create LeftPowerRail |
484 id = self.GetNewId() |
502 id = self.GetNewId() |
485 leftpowerrail = LD_PowerRail(self, LEFTRAIL, id) |
503 leftpowerrail = LD_PowerRail(self, LEFTRAIL, id) |
486 leftpowerrail.SetPosition(startx, starty) |
504 leftpowerrail.SetPosition(startx, starty) |
487 self.AddBlock(leftpowerrail) |
505 self.AddBlock(leftpowerrail) |
488 rung.SelectElement(leftpowerrail) |
506 rung.SelectElement(leftpowerrail) |
489 self.Controler.AddCurrentElementEditingPowerRail(id, LEFTRAIL) |
507 self.Controler.AddEditedElementPowerRail(self.TagName, id, LEFTRAIL) |
490 self.RefreshPowerRailModel(leftpowerrail) |
508 self.RefreshPowerRailModel(leftpowerrail) |
491 # Create Coil |
509 # Create Coil |
492 id = self.GetNewId() |
510 id = self.GetNewId() |
493 coil = LD_Coil(self, values["type"], values["name"], id) |
511 coil = LD_Coil(self, values["type"], values["name"], id) |
494 coil.SetPosition(startx, starty + (LD_LINE_SIZE - LD_ELEMENT_SIZE[1]) / 2) |
512 coil.SetPosition(startx, starty + (LD_LINE_SIZE - LD_ELEMENT_SIZE[1]) / 2) |
495 self.AddBlock(coil) |
513 self.AddBlock(coil) |
496 rung.SelectElement(coil) |
514 rung.SelectElement(coil) |
497 self.Controler.AddCurrentElementEditingCoil(id) |
515 self.Controler.AddEditedElementCoil(self.TagName, id) |
498 # Create Wire between LeftPowerRail and Coil |
516 # Create Wire between LeftPowerRail and Coil |
499 wire = Wire(self) |
517 wire = Wire(self) |
500 start_connector = coil.GetConnectors()["input"] |
518 start_connector = coil.GetConnectors()["input"] |
501 end_connector = leftpowerrail.GetConnectors()[0] |
519 end_connector = leftpowerrail.GetConnectors()[0] |
502 start_connector.Connect((wire, 0), False) |
520 start_connector.Connect((wire, 0), False) |
509 id = self.GetNewId() |
527 id = self.GetNewId() |
510 rightpowerrail = LD_PowerRail(self, RIGHTRAIL, id) |
528 rightpowerrail = LD_PowerRail(self, RIGHTRAIL, id) |
511 rightpowerrail.SetPosition(startx, starty) |
529 rightpowerrail.SetPosition(startx, starty) |
512 self.AddBlock(rightpowerrail) |
530 self.AddBlock(rightpowerrail) |
513 rung.SelectElement(rightpowerrail) |
531 rung.SelectElement(rightpowerrail) |
514 self.Controler.AddCurrentElementEditingPowerRail(id, RIGHTRAIL) |
532 self.Controler.AddEditedElementPowerRail(self.TagName, id, RIGHTRAIL) |
515 # Create Wire between LeftPowerRail and Coil |
533 # Create Wire between LeftPowerRail and Coil |
516 wire = Wire(self) |
534 wire = Wire(self) |
517 start_connector = rightpowerrail.GetConnectors()[0] |
535 start_connector = rightpowerrail.GetConnectors()[0] |
518 end_connector = coil.GetConnectors()["output"] |
536 end_connector = coil.GetConnectors()["output"] |
519 start_connector.Connect((wire, 0), False) |
537 start_connector.Connect((wire, 0), False) |
539 for element in self.SelectedElement.GetElements(): |
557 for element in self.SelectedElement.GetElements(): |
540 wires.append(element) |
558 wires.append(element) |
541 if len(wires) > 0: |
559 if len(wires) > 0: |
542 dialog = LDElementDialog(self.ParentWindow, "contact") |
560 dialog = LDElementDialog(self.ParentWindow, "contact") |
543 varlist = [] |
561 varlist = [] |
544 vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
562 vars = self.Controler.GetEditedElementInterfaceVars(self.TagName) |
545 if vars: |
563 if vars: |
546 for var in vars: |
564 for var in vars: |
547 if var["Class"] != "Output" and var["Type"] == "BOOL": |
565 if var["Class"] != "Output" and var["Type"] == "BOOL": |
548 varlist.append(var["Name"]) |
566 varlist.append(var["Name"]) |
549 dialog.SetVariables(varlist) |
567 dialog.SetVariables(varlist) |
553 points = wires[0].GetSelectedSegmentPoints() |
571 points = wires[0].GetSelectedSegmentPoints() |
554 id = self.GetNewId() |
572 id = self.GetNewId() |
555 contact = LD_Contact(self, values["type"], values["name"], id) |
573 contact = LD_Contact(self, values["type"], values["name"], id) |
556 contact.SetPosition(0, points[0].y - (LD_ELEMENT_SIZE[1] + 1) / 2) |
574 contact.SetPosition(0, points[0].y - (LD_ELEMENT_SIZE[1] + 1) / 2) |
557 self.AddBlock(contact) |
575 self.AddBlock(contact) |
558 self.Controler.AddCurrentElementEditingContact(id) |
576 self.Controler.AddEditedElementContact(self.TagName, id) |
559 rungindex = self.FindRung(wires[0]) |
577 rungindex = self.FindRung(wires[0]) |
560 rung = self.Rungs[rungindex] |
578 rung = self.Rungs[rungindex] |
561 old_bbox = rung.GetBoundingBox() |
579 old_bbox = rung.GetBoundingBox() |
562 rung.SelectElement(contact) |
580 rung.SelectElement(contact) |
563 connectors = contact.GetConnectors() |
581 connectors = contact.GetConnectors() |
760 new_wire.ConnectEndPoint(None, connectors[index + 1]) |
778 new_wire.ConnectEndPoint(None, connectors[index + 1]) |
761 right_elements.reverse() |
779 right_elements.reverse() |
762 elif right_powerrail: |
780 elif right_powerrail: |
763 dialog = LDElementDialog(self.ParentWindow, "coil") |
781 dialog = LDElementDialog(self.ParentWindow, "coil") |
764 varlist = [] |
782 varlist = [] |
765 vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
783 vars = self.Controler.GetEditedElementInterfaceVars(self.TagName) |
766 if vars: |
784 if vars: |
767 for var in vars: |
785 for var in vars: |
768 if var["Class"] != "Input" and var["Type"] == "BOOL": |
786 if var["Class"] != "Input" and var["Type"] == "BOOL": |
769 varlist.append(var["Name"]) |
787 varlist.append(var["Name"]) |
770 returntype = self.Controler.GetCurrentElementEditingInterfaceReturnType() |
788 returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName) |
771 if returntype == "BOOL": |
789 if returntype == "BOOL": |
772 varlist.append(self.Controler.GetCurrentElementEditingName()) |
790 varlist.append(self.Controler.GetEditedElementName(self.TagName)) |
773 dialog.SetVariables(varlist) |
791 dialog.SetVariables(varlist) |
774 dialog.SetValues({"name":"","type":COIL_NORMAL}) |
792 dialog.SetValues({"name":"","type":COIL_NORMAL}) |
775 if dialog.ShowModal() == wx.ID_OK: |
793 if dialog.ShowModal() == wx.ID_OK: |
776 values = dialog.GetValues() |
794 values = dialog.GetValues() |
777 powerrail = right_elements[0].GetParentBlock() |
795 powerrail = right_elements[0].GetParentBlock() |
788 coil = LD_Coil(self, values["type"], values["name"], id) |
806 coil = LD_Coil(self, values["type"], values["name"], id) |
789 pos = blocks[0].GetPosition() |
807 pos = blocks[0].GetPosition() |
790 coil.SetPosition(pos[0], pos[1] + LD_LINE_SIZE) |
808 coil.SetPosition(pos[0], pos[1] + LD_LINE_SIZE) |
791 self.AddBlock(coil) |
809 self.AddBlock(coil) |
792 rung.SelectElement(coil) |
810 rung.SelectElement(coil) |
793 self.Controler.AddCurrentElementEditingCoil(id) |
811 self.Controler.AddEditedElementCoil(self.TagName, id) |
794 coil_connectors = coil.GetConnectors() |
812 coil_connectors = coil.GetConnectors() |
795 # Create Wire between LeftPowerRail and Coil |
813 # Create Wire between LeftPowerRail and Coil |
796 wire = Wire(self) |
814 wire = Wire(self) |
797 connectors[index + 1].Connect((wire, 0), False) |
815 connectors[index + 1].Connect((wire, 0), False) |
798 coil_connectors["output"].Connect((wire, -1), False) |
816 coil_connectors["output"].Connect((wire, -1), False) |
914 rung.SelectElement(wire) |
932 rung.SelectElement(wire) |
915 right_elements.reverse() |
933 right_elements.reverse() |
916 for right_element, right_index in right_elements: |
934 for right_element, right_index in right_elements: |
917 self.RefreshPosition(right_element.GetParentBlock()) |
935 self.RefreshPosition(right_element.GetParentBlock()) |
918 self.RemoveBlock(contact) |
936 self.RemoveBlock(contact) |
919 self.Controler.RemoveCurrentElementEditingInstance(contact.GetId()) |
937 self.Controler.RemoveEditedElementInstance(self.TagName, contact.GetId()) |
920 rung.RefreshBoundingBox() |
938 rung.RefreshBoundingBox() |
921 new_bbox = rung.GetBoundingBox() |
939 new_bbox = rung.GetBoundingBox() |
922 self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1) |
940 self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1) |
923 self.SelectedElement = None |
941 self.SelectedElement = None |
924 |
942 |
931 for wire in input_wires: |
949 for wire in input_wires: |
932 wire.Clean() |
950 wire.Clean() |
933 self.RemoveWire(wire) |
951 self.RemoveWire(wire) |
934 rung.SelectElement(wire) |
952 rung.SelectElement(wire) |
935 self.RemoveBlock(element) |
953 self.RemoveBlock(element) |
936 self.Controler.RemoveCurrentElementEditingInstance(element.GetId()) |
954 self.Controler.RemoveEditedElementInstance(self.TagName, element.GetId()) |
937 for left_element in left_elements: |
955 for left_element in left_elements: |
938 block = left_element.GetParentBlock() |
956 block = left_element.GetParentBlock() |
939 if len(left_element.GetWires()) == 0: |
957 if len(left_element.GetWires()) == 0: |
940 self.RecursiveDeletion(block, rung) |
958 self.RecursiveDeletion(block, rung) |
941 else: |
959 else: |
981 if self.IsWire(element): |
999 if self.IsWire(element): |
982 element.Clean() |
1000 element.Clean() |
983 self.RemoveWire(element) |
1001 self.RemoveWire(element) |
984 for element in rung.GetElements(): |
1002 for element in rung.GetElements(): |
985 if self.IsBlock(element): |
1003 if self.IsBlock(element): |
986 self.Controler.RemoveCurrentElementEditingInstance(element.GetId()) |
1004 self.Controler.RemoveEditedElementInstance(self.TagName, element.GetId()) |
987 self.RemoveBlock(element) |
1005 self.RemoveBlock(element) |
988 self.Controler.RemoveCurrentElementEditingInstance(self.Comments[rungindex].GetId()) |
1006 self.Controler.RemoveEditedElementInstance(self.TagName, self.Comments[rungindex].GetId()) |
989 self.RemoveComment(self.RungComments[rungindex]) |
1007 self.RemoveComment(self.RungComments[rungindex]) |
990 self.RungComments.pop(rungindex) |
1008 self.RungComments.pop(rungindex) |
991 self.Rungs.pop(rungindex) |
1009 self.Rungs.pop(rungindex) |
992 if rungindex < len(self.Rungs): |
1010 if rungindex < len(self.Rungs): |
993 next_bbox = self.Rungs[rungindex].GetBoundingBox() |
1011 next_bbox = self.Rungs[rungindex].GetBoundingBox() |
1181 if self.GetDrawingMode() == FREEDRAWING_MODE: |
1199 if self.GetDrawingMode() == FREEDRAWING_MODE: |
1182 Viewer.EditContactContent(self, contact) |
1200 Viewer.EditContactContent(self, contact) |
1183 else: |
1201 else: |
1184 dialog = LDElementDialog(self.ParentWindow, "contact") |
1202 dialog = LDElementDialog(self.ParentWindow, "contact") |
1185 varlist = [] |
1203 varlist = [] |
1186 vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
1204 vars = self.Controler.GetEditedElementInterfaceVars(self.TagName) |
1187 if vars: |
1205 if vars: |
1188 for var in vars: |
1206 for var in vars: |
1189 if var["Class"] != "Output" and var["Type"] == "BOOL": |
1207 if var["Class"] != "Output" and var["Type"] == "BOOL": |
1190 varlist.append(var["Name"]) |
1208 varlist.append(var["Name"]) |
1191 dialog.SetVariables(varlist) |
1209 dialog.SetVariables(varlist) |
1204 if self.GetDrawingMode() == FREEDRAWING_MODE: |
1222 if self.GetDrawingMode() == FREEDRAWING_MODE: |
1205 Viewer.EditCoilContent(self, coil) |
1223 Viewer.EditCoilContent(self, coil) |
1206 else: |
1224 else: |
1207 dialog = LDElementDialog(self.ParentWindow, "coil") |
1225 dialog = LDElementDialog(self.ParentWindow, "coil") |
1208 varlist = [] |
1226 varlist = [] |
1209 vars = self.Controler.GetCurrentElementEditingInterfaceVars() |
1227 vars = self.Controler.GetEditedElementInterfaceVars(self.TagName) |
1210 if vars: |
1228 if vars: |
1211 for var in vars: |
1229 for var in vars: |
1212 if var["Class"] != "Input" and var["Type"] == "BOOL": |
1230 if var["Class"] != "Input" and var["Type"] == "BOOL": |
1213 varlist.append(var["Name"]) |
1231 varlist.append(var["Name"]) |
1214 returntype = self.Controler.GetCurrentElementEditingInterfaceReturnType() |
1232 returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName) |
1215 if returntype == "BOOL": |
1233 if returntype == "BOOL": |
1216 varlist.append(self.Controler.GetCurrentElementEditingName()) |
1234 varlist.append(self.Controler.GetEditedElementName(self.TagName)) |
1217 dialog.SetVariables(varlist) |
1235 dialog.SetVariables(varlist) |
1218 dialog.SetValues({"name":coil.GetName(),"type":coil.GetType()}) |
1236 dialog.SetValues({"name":coil.GetName(),"type":coil.GetType()}) |
1219 if dialog.ShowModal() == wx.ID_OK: |
1237 if dialog.ShowModal() == wx.ID_OK: |
1220 values = dialog.GetValues() |
1238 values = dialog.GetValues() |
1221 coil.SetName(values["name"]) |
1239 coil.SetName(values["name"]) |