SFCViewer.py
changeset 42 4a8400732001
parent 28 fc23e1f415d8
child 44 c6e153273ea1
equal deleted inserted replaced
41:0688db995ddf 42:4a8400732001
    39     
    39     
    40     def ConnectConnectors(self, start, end):
    40     def ConnectConnectors(self, start, end):
    41         startpoint = [start.GetPosition(False), start.GetDirection()]
    41         startpoint = [start.GetPosition(False), start.GetDirection()]
    42         endpoint = [end.GetPosition(False), end.GetDirection()]
    42         endpoint = [end.GetPosition(False), end.GetDirection()]
    43         wire = Wire(self, startpoint, endpoint)
    43         wire = Wire(self, startpoint, endpoint)
    44         self.Wires.append(wire)
    44         self.AddWire(wire)
    45         self.Elements.append(wire)
       
    46         start.Connect((wire, 0), False)
    45         start.Connect((wire, 0), False)
    47         end.Connect((wire, -1), False)
    46         end.Connect((wire, -1), False)
    48         wire.ConnectStartPoint(None, start)
    47         wire.ConnectStartPoint(None, start)
    49         wire.ConnectEndPoint(None, end)
    48         wire.ConnectEndPoint(None, end)
    50         return wire
    49         return wire
    60         if isinstance(previous, SFC_Divergence):
    59         if isinstance(previous, SFC_Divergence):
    61             previous.RefreshConnectedPosition(connector)
    60             previous.RefreshConnectedPosition(connector)
    62         else:
    61         else:
    63             previous.RefreshOutputPosition()
    62             previous.RefreshOutputPosition()
    64         wire.SetPoints([wxPoint(pos.x, pos.y + GetWireSize(previous)), wxPoint(pos.x, pos.y)])
    63         wire.SetPoints([wxPoint(pos.x, pos.y + GetWireSize(previous)), wxPoint(pos.x, pos.y)])
    65         self.Blocks.append(transition)
    64         self.AddBlock(transition)
    66         self.Elements.append(transition)
       
    67         self.Controler.AddCurrentElementEditingTransition(id)
    65         self.Controler.AddCurrentElementEditingTransition(id)
    68         self.RefreshTransitionModel(transition)
    66         self.RefreshTransitionModel(transition)
    69         if next:
    67         if next:
    70             wire = self.ConnectConnectors(next, transition_connectors["output"])
    68             wire = self.ConnectConnectors(next, transition_connectors["output"])
    71             pos = transition_connectors["output"].GetPosition(False)
    69             pos = transition_connectors["output"].GetPosition(False)
    84         if len(input_wires) != 1:
    82         if len(input_wires) != 1:
    85             return
    83             return
    86         input_wire = input_wires[0][0]
    84         input_wire = input_wires[0][0]
    87         previous = input_wire.EndConnected
    85         previous = input_wire.EndConnected
    88         input_wire.Clean()
    86         input_wire.Clean()
    89         self.Wires.remove(input_wire)
    87         self.RemoveWire(input_wire)
    90         self.Elements.remove(input_wire)
       
    91         output_wires = connectors["output"].GetWires()
    88         output_wires = connectors["output"].GetWires()
    92         if len(output_wires) != 1:
    89         if len(output_wires) != 1:
    93             return
    90             return
    94         output_wire = output_wires[0][0]
    91         output_wire = output_wires[0][0]
    95         next = output_wire.StartConnected
    92         next = output_wire.StartConnected
    96         output_wire.Clean()
    93         output_wire.Clean()
    97         self.Wires.remove(output_wire)
    94         self.RemoveWire(output_wire)
    98         self.Elements.remove(output_wire)
       
    99         transition.Clean()
    95         transition.Clean()
   100         self.Blocks.remove(transition)
    96         self.RemoveBlock(transition)
   101         self.Elements.remove(transition)
       
   102         self.Controler.RemoveCurrentElementEditingInstance(transition.GetId())
    97         self.Controler.RemoveCurrentElementEditingInstance(transition.GetId())
   103         wire = self.ConnectConnectors(next, previous)
    98         wire = self.ConnectConnectors(next, previous)
   104         return wire
    99         return wire
   105     
   100     
   106     def CreateStep(self, name, connector, next = None):
   101     def CreateStep(self, name, connector, next = None):
   118         if isinstance(previous, SFC_Divergence):
   113         if isinstance(previous, SFC_Divergence):
   119             previous.RefreshConnectedPosition(connector)
   114             previous.RefreshConnectedPosition(connector)
   120         else:
   115         else:
   121             previous.RefreshOutputPosition()
   116             previous.RefreshOutputPosition()
   122         wire.SetPoints([wxPoint(pos.x, pos.y + GetWireSize(previous)), wxPoint(pos.x, pos.y)])
   117         wire.SetPoints([wxPoint(pos.x, pos.y + GetWireSize(previous)), wxPoint(pos.x, pos.y)])
   123         self.Blocks.append(step)
   118         self.AddBlock(step)
   124         self.Elements.append(step)
       
   125         self.Controler.AddCurrentElementEditingStep(id)
   119         self.Controler.AddCurrentElementEditingStep(id)
   126         self.RefreshStepModel(step)
   120         self.RefreshStepModel(step)
   127         if next:
   121         if next:
   128             wire = self.ConnectConnectors(next, step_connectors["output"])
   122             wire = self.ConnectConnectors(next, step_connectors["output"])
   129             pos = step_connectors["output"].GetPosition(False)
   123             pos = step_connectors["output"].GetPosition(False)
   143             if len(input_wires) != 1:
   137             if len(input_wires) != 1:
   144                 return
   138                 return
   145             input_wire = input_wires[0][0]
   139             input_wire = input_wires[0][0]
   146             previous = input_wire.EndConnected
   140             previous = input_wire.EndConnected
   147             input_wire.Clean()
   141             input_wire.Clean()
   148             self.Wires.remove(input_wire)
   142             self.RemoveWire(input_wire)
   149             self.Elements.remove(input_wire)
       
   150         else:
   143         else:
   151             previous = None
   144             previous = None
   152         if connectors["output"]:
   145         if connectors["output"]:
   153             output_wires = connectors["output"].GetWires()
   146             output_wires = connectors["output"].GetWires()
   154             if len(output_wires) != 1:
   147             if len(output_wires) != 1:
   155                 return
   148                 return
   156             output_wire = output_wires[0][0]
   149             output_wire = output_wires[0][0]
   157             next = output_wire.StartConnected
   150             next = output_wire.StartConnected
   158             output_wire.Clean()
   151             output_wire.Clean()
   159             self.Wires.remove(output_wire)
   152             self.RemoveWire(output_wire)
   160             self.Elements.remove(output_wire)
       
   161         else:
   153         else:
   162             next = None
   154             next = None
   163         action = step.GetActionConnector()
   155         action = step.GetActionConnector()
   164         if action:
   156         if action:
   165             self.DeleteActionBlock(action.GetParentBlock())
   157             self.DeleteActionBlock(action.GetParentBlock())
   166         step.Clean()
   158         step.Clean()
   167         self.Blocks.remove(step)
   159         self.RemoveBlock(step)
   168         self.Elements.remove(step)
       
   169         self.Controler.RemoveCurrentElementEditingInstance(step.GetId())
   160         self.Controler.RemoveCurrentElementEditingInstance(step.GetId())
   170         if next and previous:
   161         if next and previous:
   171             wire = self.ConnectConnectors(next, previous)
   162             wire = self.ConnectConnectors(next, previous)
   172             return wire
   163             return wire
   173         else:
   164         else:
   183         elif self.Mode == MODE_SELECTION:
   174         elif self.Mode == MODE_SELECTION:
   184             dc = self.GetLogicalDC()
   175             dc = self.GetLogicalDC()
   185             pos = event.GetLogicalPosition(dc)
   176             pos = event.GetLogicalPosition(dc)
   186             if event.ControlDown():
   177             if event.ControlDown():
   187                 element = self.FindElement(pos, True)
   178                 element = self.FindElement(pos, True)
   188                 if element and element not in self.Wires:
   179                 if element and not self.IsWire(element):
   189                     if isinstance(self.SelectedElement, Graphic_Group):
   180                     if isinstance(self.SelectedElement, Graphic_Group):
   190                         self.SelectedElement.SelectElement(element)
   181                         self.SelectedElement.SelectElement(element)
   191                     else:
   182                     else:
   192                         group = Graphic_Group(self)
   183                         group = Graphic_Group(self)
   193                         self.SelectedElement.SetSelected(False)
   184                         self.SelectedElement.SetSelected(False)
   200                     elif len(elements) == 1:
   191                     elif len(elements) == 1:
   201                         self.SelectedElement = elements[0]
   192                         self.SelectedElement = elements[0]
   202             else:
   193             else:
   203                 element = self.FindElement(pos)
   194                 element = self.FindElement(pos)
   204                 if self.SelectedElement and self.SelectedElement != element:
   195                 if self.SelectedElement and self.SelectedElement != element:
   205                     if self.SelectedElement in self.Wires:
   196                     if self.IsWire(self.SelectedElement):
   206                         self.SelectedElement.SetSelectedSegment(None)
   197                         self.SelectedElement.SetSelectedSegment(None)
   207                     else:
   198                     else:
   208                         self.SelectedElement.SetSelected(False)
   199                         self.SelectedElement.SetSelected(False)
   209                     self.SelectedElement = None
   200                     self.SelectedElement = None
   210                     self.Refresh()
   201                     self.Refresh()
   223             wire = Wire(self, [wxPoint(pos.x, pos.y), SOUTH], [wxPoint(pos.x, pos.y), NORTH])
   214             wire = Wire(self, [wxPoint(pos.x, pos.y), SOUTH], [wxPoint(pos.x, pos.y), NORTH])
   224             wire.oldPos = pos
   215             wire.oldPos = pos
   225             wire.Handle = (HANDLE_POINT, 0)
   216             wire.Handle = (HANDLE_POINT, 0)
   226             wire.ProcessDragging(0, 0)
   217             wire.ProcessDragging(0, 0)
   227             wire.Handle = (HANDLE_POINT, 1)
   218             wire.Handle = (HANDLE_POINT, 1)
   228             self.Wires.append(wire)
   219             self.AddWire(wire)
   229             self.Elements.append(wire)
       
   230             if self.SelectedElement:
   220             if self.SelectedElement:
   231                 self.SelectedElement.SetSelected(False)
   221                 self.SelectedElement.SetSelected(False)
   232             self.SelectedElement = wire
   222             self.SelectedElement = wire
   233             self.Refresh()
   223             self.Refresh()
   234         event.Skip()
   224         event.Skip()
   250                 self.rubberBand.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)
   240                 self.rubberBand.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)
   251                 wxCallAfter(self.AddComment, bbox)
   241                 wxCallAfter(self.AddComment, bbox)
   252         elif self.Mode == MODE_INITIALSTEP:
   242         elif self.Mode == MODE_INITIALSTEP:
   253             wxCallAfter(self.AddInitialStep, GetScaledEventPosition(event, self.GetLogicalDC(), self.Scaling))
   243             wxCallAfter(self.AddInitialStep, GetScaledEventPosition(event, self.GetLogicalDC(), self.Scaling))
   254         elif self.Mode == MODE_SELECTION and self.SelectedElement:
   244         elif self.Mode == MODE_SELECTION and self.SelectedElement:
   255             if self.SelectedElement in self.Wires:
   245             if self.IsWire(self.SelectedElement):
   256                 self.SelectedElement.SetSelectedSegment(0)
   246                 self.SelectedElement.SetSelectedSegment(0)
   257             else:
   247             else:
   258                 self.SelectedElement.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)
   248                 self.SelectedElement.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)
   259             wxCallAfter(self.SetCursor, wxNullCursor)
   249             wxCallAfter(self.SetCursor, wxNullCursor)
   260             self.ReleaseMouse()
   250             self.ReleaseMouse()
   277             element = self.FindElement(pos)
   267             element = self.FindElement(pos)
   278             if element:
   268             if element:
   279                 if self.SelectedElement and self.SelectedElement != element:
   269                 if self.SelectedElement and self.SelectedElement != element:
   280                     self.SelectedElement.SetSelected(False)
   270                     self.SelectedElement.SetSelected(False)
   281                 self.SelectedElement = element
   271                 self.SelectedElement = element
   282                 if self.SelectedElement in self.Wires:
   272                 if self.IsWire(self.SelectedElement):
   283                     self.SelectedElement.SetSelectedSegment(0)
   273                     self.SelectedElement.SetSelectedSegment(0)
   284                 else:
   274                 else:
   285                     self.SelectedElement.SetSelected(True)
   275                     self.SelectedElement.SetSelected(True)
   286                     self.SelectedElement.OnRightUp(event, dc, self.Scaling)
   276                     self.SelectedElement.OnRightUp(event, dc, self.Scaling)
   287                 wxCallAfter(self.SetCursor, wxNullCursor)
   277                 wxCallAfter(self.SetCursor, wxNullCursor)
   301         if self.GetDrawingMode() == FREEDRAWING_MODE:
   291         if self.GetDrawingMode() == FREEDRAWING_MODE:
   302             Viewer.OnViewerMotion(self, event)
   292             Viewer.OnViewerMotion(self, event)
   303         elif self.rubberBand.IsShown():
   293         elif self.rubberBand.IsShown():
   304             self.rubberBand.OnMotion(event, self.GetLogicalDC(), self.Scaling)
   294             self.rubberBand.OnMotion(event, self.GetLogicalDC(), self.Scaling)
   305         elif self.Mode == MODE_SELECTION and self.SelectedElement:
   295         elif self.Mode == MODE_SELECTION and self.SelectedElement:
   306             if self.SelectedElement not in self.Wires:
   296             if not self.IsWire(self.SelectedElement):
   307                 self.SelectedElement.OnMotion(event, self.GetLogicalDC(), self.Scaling)
   297                 self.SelectedElement.OnMotion(event, self.GetLogicalDC(), self.Scaling)
   308             self.Refresh()
   298             self.Refresh()
   309         elif self.Mode == MODE_WIRE and self.SelectedElement:
   299         elif self.Mode == MODE_WIRE and self.SelectedElement:
   310             self.SelectedElement.ResetPoints()
   300             self.SelectedElement.ResetPoints()
   311             self.SelectedElement.OnMotion(event, self.GetLogicalDC(), self.Scaling)
   301             self.SelectedElement.OnMotion(event, self.GetLogicalDC(), self.Scaling)
   325         else:
   315         else:
   326             scaling = (8, 8)
   316             scaling = (8, 8)
   327         if keycode == WXK_DELETE and self.SelectedElement:
   317         if keycode == WXK_DELETE and self.SelectedElement:
   328             self.SelectedElement.Delete()
   318             self.SelectedElement.Delete()
   329             self.SelectedElement = None
   319             self.SelectedElement = None
   330         elif keycode == WXK_LEFT and self.SelectedElement:
   320         elif keycode == WXK_LEFT:
   331             self.SelectedElement.Move(-scaling[0], 0)
   321             if event.ControlDown() and event.ShiftDown():
   332         elif keycode == WXK_RIGHT and self.SelectedElement:
   322                 self.Scroll(0, ypos)
   333             self.SelectedElement.Move(scaling[0], 0)
   323             elif event.ControlDown():
   334         elif keycode == WXK_UP and self.SelectedElement:
   324                 self.Scroll(max(0, xpos - 1), ypos)
   335             self.SelectedElement.Move(0, -scaling[1])
   325             elif self.SelectedElement:
   336         elif keycode == WXK_DOWN and self.SelectedElement:
   326                 self.SelectedElement.Move(-scaling[0], 0)
   337             self.SelectedElement.Move(0, scaling[1])
   327         elif keycode == WXK_RIGHT:
       
   328             if event.ControlDown() and event.ShiftDown():
       
   329                 self.Scroll(xmax, ypos)
       
   330             elif event.ControlDown():
       
   331                 self.Scroll(min(xpos + 1, xmax), ypos)
       
   332             elif self.SelectedElement:
       
   333                 self.SelectedElement.Move(scaling[0], 0)
       
   334         elif keycode == WXK_UP:
       
   335             if event.ControlDown() and event.ShiftDown():
       
   336                 self.Scroll(xpos, 0)
       
   337             elif event.ControlDown():
       
   338                 self.Scroll(xpos, max(0, ypos - 1))
       
   339             elif self.SelectedElement:
       
   340                 self.SelectedElement.Move(0, -scaling[1])
       
   341         elif keycode == WXK_DOWN:
       
   342             if event.ControlDown() and event.ShiftDown():
       
   343                 self.Scroll(xpos, ymax)
       
   344             elif event.ControlDown():
       
   345                 self.Scroll(xpos, min(ypos + 1, ymax))
       
   346             elif self.SelectedElement:
       
   347                 self.SelectedElement.Move(0, scaling[1])
   338         self.Refresh()
   348         self.Refresh()
   339         event.Skip()
       
   340         
   349         
   341 #-------------------------------------------------------------------------------
   350 #-------------------------------------------------------------------------------
   342 #                          Adding element functions
   351 #                          Adding element functions
   343 #-------------------------------------------------------------------------------
   352 #-------------------------------------------------------------------------------
   344 
   353 
   353             step = SFC_Step(self, name, True, id)
   362             step = SFC_Step(self, name, True, id)
   354             min_width, min_height = step.GetMinSize()
   363             min_width, min_height = step.GetMinSize()
   355             step.SetPosition(pos.x, pos.y)
   364             step.SetPosition(pos.x, pos.y)
   356             width, height = step.GetSize()
   365             width, height = step.GetSize()
   357             step.SetSize(max(min_width, width), max(min_height, height))
   366             step.SetSize(max(min_width, width), max(min_height, height))
   358             self.Blocks.append(step)
   367             self.AddBlock(step)
   359             self.Elements.append(step)
       
   360             self.Controler.AddCurrentElementEditingStep(id)
   368             self.Controler.AddCurrentElementEditingStep(id)
   361             self.RefreshStepModel(step)
   369             self.RefreshStepModel(step)
   362             self.Parent.RefreshProjectTree()
   370             self.Parent.RefreshProjectTree()
   363             self.Refresh()
   371             self.Refresh()
   364         dialog.Destroy()
   372         dialog.Destroy()
   369             dialog.SetPouNames(self.Controler.GetProjectPouNames())
   377             dialog.SetPouNames(self.Controler.GetProjectPouNames())
   370             dialog.SetVariables(self.Controler.GetCurrentElementEditingInterfaceVars())
   378             dialog.SetVariables(self.Controler.GetCurrentElementEditingInterfaceVars())
   371             dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step)])
   379             dialog.SetStepNames([block.GetName() for block in self.Blocks if isinstance(block, SFC_Step)])
   372             if dialog.ShowModal() == wxID_OK:
   380             if dialog.ShowModal() == wxID_OK:
   373                 name = dialog.GetValue()
   381                 name = dialog.GetValue()
   374                 if self.SelectedElement in self.Wires:
   382                 if self.IsWire(self.SelectedElement):
   375                     self.SelectedElement.SetSelectedSegment(None)
   383                     self.SelectedElement.SetSelectedSegment(None)
   376                     previous = self.SelectedElement.EndConnected
   384                     previous = self.SelectedElement.EndConnected
   377                     next = self.SelectedElement.StartConnected
   385                     next = self.SelectedElement.StartConnected
   378                     self.SelectedElement.Clean()
   386                     self.SelectedElement.Clean()
   379                     self.Wires.remove(self.SelectedElement)
   387                     self.RemoveWire(self.SelectedElement)
   380                     self.Elements.remove(self.SelectedElement)
       
   381                 else:
   388                 else:
   382                     connectors = self.SelectedElement.GetConnectors()
   389                     connectors = self.SelectedElement.GetConnectors()
   383                     if connectors["output"]:
   390                     if connectors["output"]:
   384                         previous = connectors["output"]
   391                         previous = connectors["output"]
   385                         wires = previous.GetWires()
   392                         wires = previous.GetWires()
   386                         if len(wires) != 1:
   393                         if len(wires) != 1:
   387                             return
   394                             return
   388                         wire = wires[0][0]
   395                         wire = wires[0][0]
   389                         next = wire.StartConnected
   396                         next = wire.StartConnected
   390                         wire.Clean()
   397                         wire.Clean()
   391                         self.Wires.remove(wire)
   398                         self.RemoveWire(wire)
   392                         self.Elements.remove(wire)
       
   393                     else:
   399                     else:
   394                         self.SelectedElement.AddOutput()
   400                         self.SelectedElement.AddOutput()
   395                         connectors = self.SelectedElement.GetConnectors()
   401                         connectors = self.SelectedElement.GetConnectors()
   396                         self.RefreshStepModel(self.SelectedElement)
   402                         self.RefreshStepModel(self.SelectedElement)
   397                         previous = connectors["output"]
   403                         previous = connectors["output"]
   405                     step = self.CreateStep(name, previous)
   411                     step = self.CreateStep(name, previous)
   406                     step.AddOutput()
   412                     step.AddOutput()
   407                     step.RefreshModel()
   413                     step.RefreshModel()
   408                     step_connectors = step.GetConnectors()
   414                     step_connectors = step.GetConnectors()
   409                     transition = self.CreateTransition(step_connectors["output"], next)
   415                     transition = self.CreateTransition(step_connectors["output"], next)
   410                 if self.SelectedElement in self.Wires:
   416                 if self.IsWire(self.SelectedElement):
   411                     self.SelectedElement = wire
   417                     self.SelectedElement = wire
   412                     self.SelectedElement.SetSelectedSegment(0)
   418                     self.SelectedElement.SetSelectedSegment(0)
   413                 else:
   419                 else:
   414                     self.SelectedElement.SetSelected(False)
   420                     self.SelectedElement.SetSelected(False)
   415                     self.SelectedElement = step
   421                     self.SelectedElement = step
   437                     actionblock.SetPosition(pos.x + SFC_WIRE_MIN_SIZE, pos.y - SFC_STEP_DEFAULT_SIZE[1] / 2)
   443                     actionblock.SetPosition(pos.x + SFC_WIRE_MIN_SIZE, pos.y - SFC_STEP_DEFAULT_SIZE[1] / 2)
   438                     actionblock_connector = actionblock.GetConnector()
   444                     actionblock_connector = actionblock.GetConnector()
   439                     wire = self.ConnectConnectors(actionblock_connector, connectors["action"])
   445                     wire = self.ConnectConnectors(actionblock_connector, connectors["action"])
   440                     wire.SetPoints([wxPoint(pos.x + SFC_WIRE_MIN_SIZE, pos.y), wxPoint(pos.x, pos.y)])
   446                     wire.SetPoints([wxPoint(pos.x + SFC_WIRE_MIN_SIZE, pos.y), wxPoint(pos.x, pos.y)])
   441                     actionblock.SetActions(actions)
   447                     actionblock.SetActions(actions)
   442                     self.Blocks.append(actionblock)
   448                     self.AddBlock(actionblock)
   443                     self.Elements.append(actionblock)
       
   444                     self.Controler.AddCurrentElementEditingActionBlock(id)
   449                     self.Controler.AddCurrentElementEditingActionBlock(id)
   445                     self.RefreshActionBlockModel(actionblock)
   450                     self.RefreshActionBlockModel(actionblock)
   446                     self.Refresh()
   451                     self.Refresh()
   447                 dialog.Destroy()
   452                 dialog.Destroy()
   448     
   453     
   455                     if self.SelectedElement in self.Wires and isinstance(self.SelectedElement.EndConnected.GetParentBlock(), SFC_Step):
   460                     if self.SelectedElement in self.Wires and isinstance(self.SelectedElement.EndConnected.GetParentBlock(), SFC_Step):
   456                         self.SelectedElement.SetSelectedSegment(None)
   461                         self.SelectedElement.SetSelectedSegment(None)
   457                         previous = self.SelectedElement.EndConnected
   462                         previous = self.SelectedElement.EndConnected
   458                         next = self.SelectedElement.StartConnected
   463                         next = self.SelectedElement.StartConnected
   459                         self.SelectedElement.Clean()
   464                         self.SelectedElement.Clean()
   460                         self.Wires.remove(self.SelectedElement)
   465                         self.RemoveWire(self.SelectedElement)
   461                         self.Elements.remove(self.SelectedElement)
       
   462                         self.SelectedElement = None
   466                         self.SelectedElement = None
   463                     elif isinstance(self.SelectedElement, SFC_Step):
   467                     elif isinstance(self.SelectedElement, SFC_Step):
   464                         connectors = self.SelectedElement.GetConnectors()
   468                         connectors = self.SelectedElement.GetConnectors()
   465                         if connectors["output"]:
   469                         if connectors["output"]:
   466                             previous = connectors["output"]
   470                             previous = connectors["output"]
   468                             if len(wires) != 1:
   472                             if len(wires) != 1:
   469                                 return
   473                                 return
   470                             wire = wires[0][0]
   474                             wire = wires[0][0]
   471                             next = wire.StartConnected
   475                             next = wire.StartConnected
   472                             wire.Clean()
   476                             wire.Clean()
   473                             self.Wires.remove(wire)
   477                             self.RemoveWire(wire)
   474                             self.Elements.remove(wire)
       
   475                         else:
   478                         else:
   476                             self.SelectedElement.AddOutput()
   479                             self.SelectedElement.AddOutput()
   477                             connectors = self.SelectedElement.GetConnectors()
   480                             connectors = self.SelectedElement.GetConnectors()
   478                             self.RefreshStepModel(self.SelectedElement)
   481                             self.RefreshStepModel(self.SelectedElement)
   479                             previous = connectors["output"]
   482                             previous = connectors["output"]
   488                     divergence.SetPosition(pos.x, pos.y + wire_size)
   491                     divergence.SetPosition(pos.x, pos.y + wire_size)
   489                     divergence_connectors = divergence.GetConnectors()
   492                     divergence_connectors = divergence.GetConnectors()
   490                     wire = self.ConnectConnectors(divergence_connectors["inputs"][0], previous)
   493                     wire = self.ConnectConnectors(divergence_connectors["inputs"][0], previous)
   491                     previous_block.RefreshOutputPosition()
   494                     previous_block.RefreshOutputPosition()
   492                     wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
   495                     wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
   493                     self.Blocks.append(divergence)
   496                     self.AddBlock(divergence)
   494                     self.Elements.append(divergence)
       
   495                     self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
   497                     self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
   496                     self.RefreshDivergenceModel(divergence)
   498                     self.RefreshDivergenceModel(divergence)
   497                     for index, connector in enumerate(divergence_connectors["outputs"]):
   499                     for index, connector in enumerate(divergence_connectors["outputs"]):
   498                         if next:
   500                         if next:
   499                             wire = self.ConnectConnectors(next, connector)
   501                             wire = self.ConnectConnectors(next, connector)
   513                     if self.SelectedElement in self.Wires and isinstance(self.SelectedElement.EndConnected.GetParentBlock(), SFC_Transition):
   515                     if self.SelectedElement in self.Wires and isinstance(self.SelectedElement.EndConnected.GetParentBlock(), SFC_Transition):
   514                         self.SelectedElement.SetSelectedSegment(None)
   516                         self.SelectedElement.SetSelectedSegment(None)
   515                         previous = self.SelectedElement.EndConnected
   517                         previous = self.SelectedElement.EndConnected
   516                         next = self.SelectedElement.StartConnected
   518                         next = self.SelectedElement.StartConnected
   517                         self.SelectedElement.Clean()
   519                         self.SelectedElement.Clean()
   518                         self.Wires.remove(self.SelectedElement)
   520                         self.RemoveWire(self.SelectedElement)
   519                         self.Elements.remove(self.SelectedElement)
       
   520                         self.SelectedElement = None
   521                         self.SelectedElement = None
   521                     elif isinstance(self.SelectedElement, SFC_Step):
   522                     elif isinstance(self.SelectedElement, SFC_Step):
   522                         connectors = self.SelectedElement.GetConnectors()
   523                         connectors = self.SelectedElement.GetConnectors()
   523                         if connectors["output"]:
   524                         if connectors["output"]:
   524                             previous = connectors["output"]
   525                             previous = connectors["output"]
   526                             if len(wires) != 1:
   527                             if len(wires) != 1:
   527                                 return
   528                                 return
   528                             wire = wires[0][0]
   529                             wire = wires[0][0]
   529                             next = wire.StartConnected
   530                             next = wire.StartConnected
   530                             wire.Clean()
   531                             wire.Clean()
   531                             self.Wires.remove(wire)
   532                             self.RemoveWire(wire)
   532                             self.Elements.remove(wire)
       
   533                         else:
   533                         else:
   534                             self.SelectedElement.AddOutput()
   534                             self.SelectedElement.AddOutput()
   535                             connectors = self.SelectedElement.GetConnectors()
   535                             connectors = self.SelectedElement.GetConnectors()
   536                             self.RefreshStepModel(self.SelectedElement)
   536                             self.RefreshStepModel(self.SelectedElement)
   537                             previous = connectors["output"]
   537                             previous = connectors["output"]
   549                     divergence.SetPosition(pos.x, pos.y + wire_size)
   549                     divergence.SetPosition(pos.x, pos.y + wire_size)
   550                     divergence_connectors = divergence.GetConnectors()
   550                     divergence_connectors = divergence.GetConnectors()
   551                     wire = self.ConnectConnectors(divergence_connectors["inputs"][0], previous)
   551                     wire = self.ConnectConnectors(divergence_connectors["inputs"][0], previous)
   552                     previous_block.RefreshOutputPosition()
   552                     previous_block.RefreshOutputPosition()
   553                     wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
   553                     wire.SetPoints([wxPoint(pos.x, pos.y + wire_size), wxPoint(pos.x, pos.y)])
   554                     self.Blocks.append(divergence)
   554                     self.AddBlock(divergence)
   555                     self.Elements.append(divergence)
       
   556                     self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
   555                     self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
   557                     self.RefreshDivergenceModel(divergence)
   556                     self.RefreshDivergenceModel(divergence)
   558                     for index, connector in enumerate(divergence_connectors["outputs"]):
   557                     for index, connector in enumerate(divergence_connectors["outputs"]):
   559                         if next:
   558                         if next:
   560                             wire = self.ConnectConnectors(next, connector)
   559                             wire = self.ConnectConnectors(next, connector)
   585                                 if len(wires) != 1:
   584                                 if len(wires) != 1:
   586                                     return
   585                                     return
   587                             wire = wires[0][0]
   586                             wire = wires[0][0]
   588                             next = wire.StartConnected
   587                             next = wire.StartConnected
   589                             wire.Clean()
   588                             wire.Clean()
   590                             self.Wires.remove(wire)
   589                             self.RemoveWire(wire)
   591                             self.Elements.remove(wire)
       
   592                     inputs = []
   590                     inputs = []
   593                     for input in self.SelectedElement.GetElements():
   591                     for input in self.SelectedElement.GetElements():
   594                         input_connectors = input.GetConnectors()
   592                         input_connectors = input.GetConnectors()
   595                         if not input_connectors["output"]:
   593                         if not input_connectors["output"]:
   596                             input.AddOutput()
   594                             input.AddOutput()
   622                         input_block = input.GetParentBlock()
   620                         input_block = input.GetParentBlock()
   623                         input_block.RefreshOutputPosition()
   621                         input_block.RefreshOutputPosition()
   624                     divergence.RefreshPosition()
   622                     divergence.RefreshPosition()
   625                     pos = divergence_connectors["outputs"][0].GetRelPosition()
   623                     pos = divergence_connectors["outputs"][0].GetRelPosition()
   626                     divergence.MoveConnector(divergence_connectors["outputs"][0], - pos.x)
   624                     divergence.MoveConnector(divergence_connectors["outputs"][0], - pos.x)
   627                     self.Blocks.append(divergence)
   625                     self.AddBlock(divergence)
   628                     self.Elements.append(divergence)
       
   629                     self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
   626                     self.Controler.AddCurrentElementEditingDivergence(id, value["type"])
   630                     self.RefreshDivergenceModel(divergence)
   627                     self.RefreshDivergenceModel(divergence)
   631                     if next:
   628                     if next:
   632                         wire = self.ConnectConnectors(next, divergence_connectors["outputs"][0])
   629                         wire = self.ConnectConnectors(next, divergence_connectors["outputs"][0])
   633                         pos = divergence_connectors["outputs"][0].GetPosition(False)
   630                         pos = divergence_connectors["outputs"][0].GetPosition(False)
   679                 transition_connectors = transition.GetConnectors()
   676                 transition_connectors = transition.GetConnectors()
   680                 id = self.GetNewId()
   677                 id = self.GetNewId()
   681                 jump = SFC_Jump(self, value, id)
   678                 jump = SFC_Jump(self, value, id)
   682                 pos = transition_connectors["output"].GetPosition(False)
   679                 pos = transition_connectors["output"].GetPosition(False)
   683                 jump.SetPosition(pos.x, pos.y + SFC_WIRE_MIN_SIZE)
   680                 jump.SetPosition(pos.x, pos.y + SFC_WIRE_MIN_SIZE)
   684                 self.Blocks.append(jump)
   681                 self.AddBlock(jump)
   685                 self.Elements.append(jump)
       
   686                 self.Controler.AddCurrentElementEditingJump(id)
   682                 self.Controler.AddCurrentElementEditingJump(id)
   687                 jump_connector = jump.GetConnector()
   683                 jump_connector = jump.GetConnector()
   688                 wire = self.ConnectConnectors(jump_connector, transition_connectors["output"])
   684                 wire = self.ConnectConnectors(jump_connector, transition_connectors["output"])
   689                 transition.RefreshOutputPosition()
   685                 transition.RefreshOutputPosition()
   690                 wire.SetPoints([wxPoint(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wxPoint(pos.x, pos.y)])
   686                 wire.SetPoints([wxPoint(pos.x, pos.y + SFC_WIRE_MIN_SIZE), wxPoint(pos.x, pos.y)])
   699             id = self.GetNewId()
   695             id = self.GetNewId()
   700             comment = Comment(self, value, id)
   696             comment = Comment(self, value, id)
   701             comment.SetPosition(bbox.x, bbox.y)
   697             comment.SetPosition(bbox.x, bbox.y)
   702             min_width, min_height = comment.GetMinSize()
   698             min_width, min_height = comment.GetMinSize()
   703             comment.SetSize(max(min_width,bbox.width),max(min_height,bbox.height))
   699             comment.SetSize(max(min_width,bbox.width),max(min_height,bbox.height))
   704             self.Elements.append(comment)
   700             self.AddComment(comment)
   705             self.Controler.AddCurrentElementEditingComment(id)
   701             self.Controler.AddCurrentElementEditingComment(id)
   706             self.RefreshCommentModel(comment)
   702             self.RefreshCommentModel(comment)
   707             self.Refresh()
   703             self.Refresh()
   708         dialog.Destroy()
   704         dialog.Destroy()
   709 
   705 
   799                 wire = self.RemoveStep(step)
   795                 wire = self.RemoveStep(step)
   800                 self.SelectedElement = None
   796                 self.SelectedElement = None
   801                 if next_block:
   797                 if next_block:
   802                     if isinstance(next_block, SFC_Divergence) and next_block.GetType() == SIMULTANEOUS_CONVERGENCE and isinstance(previous_block, SFC_Divergence) and previous_block.GetType() == SIMULTANEOUS_DIVERGENCE:
   798                     if isinstance(next_block, SFC_Divergence) and next_block.GetType() == SIMULTANEOUS_CONVERGENCE and isinstance(previous_block, SFC_Divergence) and previous_block.GetType() == SIMULTANEOUS_DIVERGENCE:
   803                         wire.Clean()
   799                         wire.Clean()
   804                         self.Wires.remove(wire)
   800                         self.RemoveWire(wire)
   805                         self.Elements.remove(wire)
       
   806                         next_block.RemoveBranch(next)
   801                         next_block.RemoveBranch(next)
   807                         if next_block.GetBranchNumber() < 2:
   802                         if next_block.GetBranchNumber() < 2:
   808                             self.DeleteDivergence(next_block)
   803                             self.DeleteDivergence(next_block)
   809                         else:
   804                         else:
   810                             next_block.RefreshModel()
   805                             next_block.RefreshModel()
   848                 wires = previous.GetWires()
   843                 wires = previous.GetWires()
   849                 if len(wires) != 1:
   844                 if len(wires) != 1:
   850                     return
   845                     return
   851                 wire = wires[0][0]
   846                 wire = wires[0][0]
   852                 wire.Clean()
   847                 wire.Clean()
   853                 self.Wires.remove(wire)
   848                 self.RemoveWire(wire)
   854                 self.Elements.remove(wire)
       
   855                 wires = next.GetWires()
   849                 wires = next.GetWires()
   856                 if len(wires) != 1:
   850                 if len(wires) != 1:
   857                     return
   851                     return
   858                 wire = wires[0][0]
   852                 wire = wires[0][0]
   859                 wire.Clean()
   853                 wire.Clean()
   860                 self.Wires.remove(wire)
   854                 self.RemoveWire(wire)
   861                 self.Elements.remove(wire)
       
   862                 transition.Clean()
   855                 transition.Clean()
   863                 self.Blocks.remove(transition)
   856                 self.AddBlock(transition)
   864                 self.Elements.remove(transition)
       
   865                 self.Controler.RemoveCurrentElementEditingInstance(transition.GetId())
   857                 self.Controler.RemoveCurrentElementEditingInstance(transition.GetId())
   866                 previous_block.RemoveBranch(previous)
   858                 previous_block.RemoveBranch(previous)
   867                 if previous_block.GetBranchNumber() < 2:
   859                 if previous_block.GetBranchNumber() < 2:
   868                     self.DeleteDivergence(previous_block)
   860                     self.DeleteDivergence(previous_block)
   869                 else:
   861                 else:
   888                 elif len(wires) == 1:
   880                 elif len(wires) == 1:
   889                     next = wires[0][0].StartConnected
   881                     next = wires[0][0].StartConnected
   890                     next_block = next.GetParentBlock()
   882                     next_block = next.GetParentBlock()
   891                     wire = wires[0][0]
   883                     wire = wires[0][0]
   892                     wire.Clean()
   884                     wire.Clean()
   893                     self.Wires.remove(wire)
   885                     self.RemoveWire(wire)
   894                     self.Elements.remove(wire)
       
   895                 else:
   886                 else:
   896                     next = None
   887                     next = None
   897                     next_block = None
   888                     next_block = None
   898                 for index, connector in enumerate(connectors["inputs"]):
   889                 for index, connector in enumerate(connectors["inputs"]):
   899                     if next and index == 0:
   890                     if next and index == 0:
   900                         wires = connector.GetWires()
   891                         wires = connector.GetWires()
   901                         wire = wires[0][0]
   892                         wire = wires[0][0]
   902                         previous = wires[0][0].EndConnected
   893                         previous = wires[0][0].EndConnected
   903                         wire.Clean()
   894                         wire.Clean()
   904                         self.Wires.remove(wire)
   895                         self.RemoveWire(wire)
   905                         self.Elements.remove(wire)
       
   906                     else:
   896                     else:
   907                         if type == SELECTION_CONVERGENCE:
   897                         if type == SELECTION_CONVERGENCE:
   908                             wires = connector.GetWires()
   898                             wires = connector.GetWires()
   909                             previous_block = wires[0][0].EndConnected.GetParentBlock()
   899                             previous_block = wires[0][0].EndConnected.GetParentBlock()
   910                             self.RemoveTransition(previous_block)
   900                             self.RemoveTransition(previous_block)
   911                         wires = connector.GetWires()
   901                         wires = connector.GetWires()
   912                         wire = wires[0][0]
   902                         wire = wires[0][0]
   913                         previous_connector = wire.EndConnected
   903                         previous_connector = wire.EndConnected
   914                         previous_block = previous_connector.GetParentBlock()
   904                         previous_block = previous_connector.GetParentBlock()
   915                         wire.Clean()
   905                         wire.Clean()
   916                         self.Wires.remove(wire)
   906                         self.RemoveWire(wire)
   917                         self.Elements.remove(wire)
       
   918                         if isinstance(previous_block, SFC_Step):
   907                         if isinstance(previous_block, SFC_Step):
   919                             previous_block.RemoveOutput()
   908                             previous_block.RemoveOutput()
   920                             self.RefreshStepModel(previous_block)
   909                             self.RefreshStepModel(previous_block)
   921                         elif isinstance(previous_block, SFC_Divergence):
   910                         elif isinstance(previous_block, SFC_Divergence):
   922                             if previous_block.GetType() in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
   911                             if previous_block.GetType() in [SELECTION_DIVERGENCE, SIMULTANEOUS_DIVERGENCE]:
   926                                 else:
   915                                 else:
   927                                     self.RefreshDivergenceModel(previous_block)
   916                                     self.RefreshDivergenceModel(previous_block)
   928                             else:
   917                             else:
   929                                 self.DeleteDivergence(previous_block)
   918                                 self.DeleteDivergence(previous_block)
   930                 divergence.Clean()
   919                 divergence.Clean()
   931                 self.Blocks.remove(divergence)
   920                 self.AddBlocks(divergence)
   932                 self.Elements.remove(divergence)
       
   933                 self.Controler.RemoveCurrentElementEditingInstance(divergence.GetId())
   921                 self.Controler.RemoveCurrentElementEditingInstance(divergence.GetId())
   934                 if next:
   922                 if next:
   935                     wire = self.ConnectConnectors(next, previous)
   923                     wire = self.ConnectConnectors(next, previous)
   936                     previous_block = previous.GetParentBlock()
   924                     previous_block = previous.GetParentBlock()
   937                     pos = previous.GetPosition(False)
   925                     pos = previous.GetPosition(False)
   949                     return
   937                     return
   950                 wire = wires[0][0]
   938                 wire = wires[0][0]
   951                 previous = wire.EndConnected
   939                 previous = wire.EndConnected
   952                 previous_block = previous.GetParentBlock()
   940                 previous_block = previous.GetParentBlock()
   953                 wire.Clean()
   941                 wire.Clean()
   954                 self.Wires.remove(wire)
   942                 self.RemoveWire(wire)
   955                 self.Elements.remove(wire)
       
   956                 wires = connectors["outputs"][0].GetWires()
   943                 wires = connectors["outputs"][0].GetWires()
   957                 if len(wires) != 1:
   944                 if len(wires) != 1:
   958                     return
   945                     return
   959                 wire = wires[0][0]
   946                 wire = wires[0][0]
   960                 next = wire.StartConnected
   947                 next = wire.StartConnected
   961                 next_block = next.GetParentBlock()
   948                 next_block = next.GetParentBlock()
   962                 wire.Clean()
   949                 wire.Clean()
   963                 self.Wires.remove(wire)
   950                 self.RemoveWire(wire)
   964                 self.Elements.remove(wire)
       
   965                 divergence.Clean()
   951                 divergence.Clean()
   966                 self.Blocks.remove(divergence)
   952                 self.AddBlock(divergence)
   967                 self.Elements.remove(divergence)
       
   968                 self.Controler.RemoveCurrentElementEditingInstance(divergence.GetId())
   953                 self.Controler.RemoveCurrentElementEditingInstance(divergence.GetId())
   969                 wire = self.ConnectConnectors(next, previous)
   954                 wire = self.ConnectConnectors(next, previous)
   970                 previous_pos = previous.GetPosition(False)
   955                 previous_pos = previous.GetPosition(False)
   971                 next_pos = next.GetPosition(False)
   956                 next_pos = next.GetPosition(False)
   972                 wire_size = GetWireSize(previous_block)
   957                 wire_size = GetWireSize(previous_block)
   994             wires = previous.GetWires()
   979             wires = previous.GetWires()
   995             if len(wires) != 1:
   980             if len(wires) != 1:
   996                 return
   981                 return
   997             wire = wires[0][0]
   982             wire = wires[0][0]
   998             wire.Clean()
   983             wire.Clean()
   999             self.Wires.remove(wire)
   984             self.RemoveWire(wire)
  1000             self.Elements.remove(wire)
       
  1001             jump.Clean()
   985             jump.Clean()
  1002             self.Blocks.remove(jump)
   986             self.AddBlocks(jump)
  1003             self.Elements.remove(jump)
       
  1004             self.Controler.RemoveCurrentElementEditingInstance(jump.GetId())
   987             self.Controler.RemoveCurrentElementEditingInstance(jump.GetId())
  1005             if isinstance(previous_block, SFC_Step):
   988             if isinstance(previous_block, SFC_Step):
  1006                 previous_block.RemoveOutput()
   989                 previous_block.RemoveOutput()
  1007                 self.RefreshStepModel(previous_block)
   990                 self.RefreshStepModel(previous_block)
  1008             elif isinstance(previous_block, SFC_Divergence):
   991             elif isinstance(previous_block, SFC_Divergence):
  1025             if len(wires) != 1:
  1008             if len(wires) != 1:
  1026                 return
  1009                 return
  1027             wire = wires[0][0]
  1010             wire = wires[0][0]
  1028             step = wire.EndConnected.GetParentBlock()
  1011             step = wire.EndConnected.GetParentBlock()
  1029             wire.Clean()
  1012             wire.Clean()
  1030             self.Wires.remove(wire)
  1013             self.RemoveWire(wire)
  1031             self.Elements.remove(wire)
       
  1032             actionblock.Clean()
  1014             actionblock.Clean()
  1033             self.Blocks.remove(actionblock)
  1015             self.AddBlock(actionblock)
  1034             self.Elements.remove(actionblock)
       
  1035             self.Controler.RemoveCurrentElementEditingInstance(actionblock.GetId())
  1016             self.Controler.RemoveCurrentElementEditingInstance(actionblock.GetId())
  1036             step.RemoveAction()
  1017             step.RemoveAction()
  1037             self.RefreshStepModel(step)
  1018             self.RefreshStepModel(step)
  1038             step.RefreshOutputPosition()
  1019             step.RefreshOutputPosition()
  1039             step.RefreshOutputModel(True)
  1020             step.RefreshOutputModel(True)