LDViewer.py
author laurent
Tue, 24 May 2011 19:37:14 +0200
changeset 540 82fa901a2160
parent 510 e7327ea490b4
child 550 cfa295862d55
permissions -rw-r--r--
Fix bug when displaying errors when generating textual program in standalone mode
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     1
#!/usr/bin/env python
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     3
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     4
#This file is part of PLCOpenEditor, a library implementing an IEC 61131-3 editor
58
39cd981ff242 Changing file headers
lbessard
parents: 56
diff changeset
     5
#based on the plcopen standard. 
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     6
#
58
39cd981ff242 Changing file headers
lbessard
parents: 56
diff changeset
     7
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     8
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     9
#See COPYING file for copyrights details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    10
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    11
#This library is free software; you can redistribute it and/or
5
f8652b073e84 GPL->LGPL
etisserant
parents: 2
diff changeset
    12
#modify it under the terms of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    13
#License as published by the Free Software Foundation; either
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    14
#version 2.1 of the License, or (at your option) any later version.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    15
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    16
#This library is distributed in the hope that it will be useful,
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    17
#but WITHOUT ANY WARRANTY; without even the implied warranty of
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    18
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
58
39cd981ff242 Changing file headers
lbessard
parents: 56
diff changeset
    19
#General Public License for more details.
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    20
#
5
f8652b073e84 GPL->LGPL
etisserant
parents: 2
diff changeset
    21
#You should have received a copy of the GNU General Public
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    22
#License along with this library; if not, write to the Free Software
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    23
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    24
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    25
import wx
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    26
from types import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    27
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    28
from Viewer import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    29
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    30
def ExtractNextBlocks(block, block_list):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    31
    current_list = [block]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    32
    while len(current_list) > 0:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    33
        next_list = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    34
        for current in current_list:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    35
            connectors = current.GetConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    36
            input_connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    37
            if isinstance(current, LD_PowerRail) and current.GetType() == RIGHTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    38
                input_connectors = connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    39
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    40
                if "inputs" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    41
                    input_connectors = connectors["inputs"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    42
                if "input" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    43
                    input_connectors = [connectors["input"]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    44
            for connector in input_connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    45
                for wire, handle in connector.GetWires():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    46
                    next = wire.EndConnected.GetParentBlock()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    47
                    if not isinstance(next, LD_PowerRail) and next not in block_list:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    48
                        block_list.append(next)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    49
                        next_list.append(next)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    50
        current_list = next_list
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    51
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    52
def CalcBranchSize(elements, stops):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    53
    branch_size = 0
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    54
    stop_list = stops
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    55
    for stop in stops:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    56
        ExtractNextBlocks(stop, stop_list)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    57
    element_tree = {}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    58
    for element in elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    59
        if element not in element_tree:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    60
            element_tree[element] = {"parents":["start"], "children":[], "weight":None}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    61
            GenerateTree(element, element_tree, stop_list)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    62
        elif element_tree[element]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    63
            element_tree[element]["parents"].append("start")
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    64
    remove_stops = {"start":[], "stop":[]}
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    65
    for element, values in element_tree.items():
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    66
        if "stop" in values["children"]:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    67
            removed = []
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    68
            for child in values["children"]:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    69
                if child != "stop":
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    70
##                    if child in elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    71
##                        RemoveElement(child, element_tree)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    72
##                        removed.append(child)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    73
                    if "start" in element_tree[child]["parents"]:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    74
                        if element not in remove_stops["stop"]:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    75
                            remove_stops["stop"].append(element)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    76
                        if child not in remove_stops["start"]:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    77
                            remove_stops["start"].append(child)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    78
            for child in removed:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    79
                values["children"].remove(child)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    80
    for element in remove_stops["start"]:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    81
        element_tree[element]["parents"].remove("start")
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    82
    for element in remove_stops["stop"]:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    83
        element_tree[element]["children"].remove("stop")
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    84
    for element, values in element_tree.items():
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    85
        if values and "stop" in values["children"]:
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    86
            CalcWeight(element, element_tree)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    87
            if values["weight"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    88
                branch_size += values["weight"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    89
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    90
                return 1
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    91
    #print branch_size
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    92
    return branch_size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    93
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    94
def RemoveElement(remove, element_tree):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    95
    if remove in element_tree and element_tree[remove]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    96
        for child in element_tree[remove]["children"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    97
            if child != "stop":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    98
                RemoveElement(child, element_tree)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
    99
        element_tree.pop(remove)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   100
##        element_tree[remove] = None
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   101
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   102
def GenerateTree(element, element_tree, stop_list):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   103
    if element in element_tree:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   104
        connectors = element.GetConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   105
        input_connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   106
        if isinstance(element, LD_PowerRail) and element.GetType() == RIGHTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   107
            input_connectors = connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   108
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   109
            if "inputs" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   110
                input_connectors = connectors["inputs"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   111
            if "input" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   112
                input_connectors = [connectors["input"]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   113
        for connector in input_connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   114
            for wire, handle in connector.GetWires():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   115
                next = wire.EndConnected.GetParentBlock()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   116
                if isinstance(next, LD_PowerRail) and next.GetType() == LEFTRAIL or next in stop_list:
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   117
##                    for remove in element_tree[element]["children"]:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   118
##                        RemoveElement(remove, element_tree)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   119
##                    element_tree[element]["children"] = ["stop"]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   120
                    element_tree[element]["children"].append("stop")
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   121
##                elif element_tree[element]["children"] == ["stop"]:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   122
##                    element_tree[next] = None
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   123
                elif next not in element_tree or element_tree[next]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   124
                    element_tree[element]["children"].append(next)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   125
                    if next in element_tree:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   126
                        element_tree[next]["parents"].append(element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   127
                    else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   128
                        element_tree[next] = {"parents":[element], "children":[], "weight":None}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   129
                        GenerateTree(next, element_tree, stop_list)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   130
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   131
def CalcWeight(element, element_tree):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   132
    weight = 0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   133
    parts = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   134
    if element in element_tree:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   135
        for parent in element_tree[element]["parents"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   136
            if parent == "start":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   137
                weight += 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   138
            elif parent in element_tree:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   139
                if not parts:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   140
                    parts = len(element_tree[parent]["children"])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   141
                else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   142
                    parts = min(parts, len(element_tree[parent]["children"]))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   143
                if not element_tree[parent]["weight"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   144
                    CalcWeight(parent, element_tree)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   145
                if element_tree[parent]["weight"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   146
                    weight += element_tree[parent]["weight"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   147
                else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   148
                    element_tree[element]["weight"] = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   149
                    return
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   150
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   151
                element_tree[element]["weight"] = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   152
                return
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   153
        if not parts:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   154
            parts = 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   155
        element_tree[element]["weight"] = max(1, weight / parts)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   156
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   157
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   158
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   159
#                     Ladder Diagram Graphic elements Viewer class
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   160
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   161
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   162
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   163
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   164
Class derived from Viewer class that implements a Viewer of Ladder Diagram
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   165
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   166
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   167
class LD_Viewer(Viewer):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   168
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   169
    def __init__(self, parent, tagname, window, controler, debug = False, instancepath = ""):
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   170
        Viewer.__init__(self, parent, tagname, window, controler, debug, instancepath)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   171
        self.Rungs = []
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   172
        self.RungComments = []
112
317148fc1225 Adding support for block copy
lbessard
parents: 90
diff changeset
   173
        self.CurrentLanguage = "LD"
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   174
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   175
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   176
#                          Refresh functions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   177
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   178
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   179
    def ResetView(self):
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   180
        self.Rungs = []
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   181
        self.RungComments = []
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   182
        Viewer.ResetView(self)
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   183
426
afe2488a4635 fix an exception when pasting in the LD editor
b.taylor@willowglen.ca
parents: 391
diff changeset
   184
    def RefreshView(self, selection=None):
afe2488a4635 fix an exception when pasting in the LD editor
b.taylor@willowglen.ca
parents: 391
diff changeset
   185
        Viewer.RefreshView(self, selection)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   186
        for i, rung in enumerate(self.Rungs):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   187
            bbox = rung.GetBoundingBox()
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   188
            if i < len(self.RungComments):
67
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 64
diff changeset
   189
                if self.RungComments[i]:
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 64
diff changeset
   190
                    pos = self.RungComments[i].GetPosition()
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 64
diff changeset
   191
                    if pos[1] > bbox.y:
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 64
diff changeset
   192
                        self.RungComments.insert(i, None)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   193
            else:
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   194
                self.RungComments.insert(i, None)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   195
    
383
25ffba02b6a8 Improving viewer loading instances procedure to faster
laurent
parents: 382
diff changeset
   196
    def loadInstance(self, instance, ids, selection):
25ffba02b6a8 Improving viewer loading instances procedure to faster
laurent
parents: 382
diff changeset
   197
        Viewer.loadInstance(self, instance, ids, selection)
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   198
        if self.GetDrawingMode() != FREEDRAWING_MODE:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   199
            if instance["type"] == "leftPowerRail":
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   200
                element = self.FindElementById(instance["id"])
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   201
                rung = Graphic_Group(self)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   202
                rung.SelectElement(element)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   203
                self.Rungs.append(rung)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   204
            elif instance["type"] == "rightPowerRail":
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   205
                rungs = []
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   206
                for connector in instance["connectors"]:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   207
                    for link in connector["links"]:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   208
                        connected = self.FindElementById(link["refLocalId"])
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   209
                        rung = self.FindRung(connected)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   210
                        if rung not in rungs:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   211
                            rungs.append(rung)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   212
                if len(rungs) > 1:
391
07447ee3538e Adding support for internationalization
laurent
parents: 383
diff changeset
   213
                    raise ValueError, _("Ladder element with id %d is on more than one rung.")%instance["id"]
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   214
                element = self.FindElementById(instance["id"])
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   215
                self.Rungs[rungs[0]].SelectElement(element)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   216
                for connector in element.GetConnectors():
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   217
                    for wire, num in connector.GetWires():
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   218
                        self.Rungs[rungs[0]].SelectElement(wire)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   219
                self.RefreshPosition(element)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   220
            elif instance["type"] in ["contact", "coil"]:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   221
                rungs = []
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   222
                for link in instance["connectors"]["input"]["links"]:
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   223
                    connected = self.FindElementById(link["refLocalId"])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   224
                    rung = self.FindRung(connected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   225
                    if rung not in rungs:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   226
                        rungs.append(rung)
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   227
                if len(rungs) > 1:
391
07447ee3538e Adding support for internationalization
laurent
parents: 383
diff changeset
   228
                    raise ValueError, _("Ladder element with id %d is on more than one rung.")%instance["id"]
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   229
                element = self.FindElementById(instance["id"])
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   230
                self.Rungs[rungs[0]].SelectElement(element)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   231
                for wire, num in element.GetConnectors()["input"].GetWires():
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   232
                    self.Rungs[rungs[0]].SelectElement(wire)
67
3a1b0afdaf84 Adding support for automatically generate function blocks in interface when a block is added
lbessard
parents: 64
diff changeset
   233
                self.RefreshPosition(element)
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   234
            elif instance["type"] == "comment":
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   235
                element = self.FindElementById(instance["id"])
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   236
                pos = element.GetPosition()
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   237
                i = 0
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   238
                inserted = False
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   239
                while i < len(self.RungComments) and not inserted: 
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   240
                    ipos = self.RungComments[i].GetPosition()
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   241
                    if pos[1] < ipos[1]:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   242
                        self.RungComments.insert(i, element)
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   243
                        inserted = True
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   244
                    i += 1
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   245
                if not inserted:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   246
                    self.RungComments.append(element)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   247
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   248
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   249
#                          Search Element functions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   250
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   251
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   252
    def FindRung(self, element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   253
        for i, rung in enumerate(self.Rungs):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   254
            if rung.IsElementIn(element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   255
                return i
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   256
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   257
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   258
    def FindElement(self, pos, exclude_group = False):
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   259
        if self.GetDrawingMode() == FREEDRAWING_MODE:
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   260
            return Viewer.FindElement(self, pos, exclude_group)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   261
        
80
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   262
        if self.SelectedElement and not (exclude_group and isinstance(self.SelectedElement, Graphic_Group)):
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   263
            if self.SelectedElement.HitTest(pos) or self.SelectedElement.TestHandle(pos) != (0, 0):
c798a68c5560 Lots of bugs fixed
lbessard
parents: 67
diff changeset
   264
                return self.SelectedElement
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   265
        elements = []
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   266
        for element in self.GetElements(sort_wires=True):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   267
            if element.HitTest(pos) or element.TestHandle(pos) != (0, 0):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   268
                elements.append(element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   269
        if len(elements) == 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   270
            return elements[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   271
        elif len(elements) > 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   272
            group = Graphic_Group(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   273
            for element in elements:
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   274
                if self.IsBlock(element):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   275
                    return element
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   276
                group.SelectElement(element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   277
            return group
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   278
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   279
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   280
    def SearchElements(self, bbox):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   281
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   282
            return Viewer.SearchElements(self, bbox)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   283
        
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   284
        elements = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   285
        for element in self.Blocks:
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   286
            if element.IsInSelection(bbox):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   287
                elements.append(element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   288
        return elements
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   289
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   290
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   291
#                          Mouse event functions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   292
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   293
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   294
    def OnViewerLeftDown(self, event):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   295
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   296
            Viewer.OnViewerLeftDown(self, event)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   297
        elif self.Mode == MODE_SELECTION:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   298
            dc = self.GetLogicalDC()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   299
            pos = event.GetLogicalPosition(dc)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   300
            element = self.FindElement(pos)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   301
            if self.SelectedElement:
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   302
                if not isinstance(self.SelectedElement, Graphic_Group):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   303
                    if self.SelectedElement != element:
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   304
                        if self.IsWire(self.SelectedElement):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   305
                            self.SelectedElement.SetSelectedSegment(None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   306
                        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   307
                            self.SelectedElement.SetSelected(False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   308
                    else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   309
                        self.SelectedElement = None
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   310
                elif element and isinstance(element, Graphic_Group):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   311
                    if self.SelectedElement.GetElements() != element.GetElements():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   312
                        for elt in self.SelectedElement.GetElements():
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   313
                            if self.IsWire(elt):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   314
                                elt.SetSelectedSegment(None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   315
                        self.SelectedElement.SetSelected(False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   316
                        self.SelectedElement = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   317
                else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   318
                    for elt in self.SelectedElement.GetElements():
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   319
                        if self.IsWire(elt):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   320
                            elt.SetSelectedSegment(None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   321
                    self.SelectedElement.SetSelected(False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   322
                    self.SelectedElement = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   323
            if element:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   324
                self.SelectedElement = element
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   325
                self.SelectedElement.OnLeftDown(event, dc, self.Scaling)
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 122
diff changeset
   326
                self.SelectedElement.Refresh()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   327
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   328
                self.rubberBand.Reset()
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   329
                self.rubberBand.OnLeftDown(event, dc, self.Scaling)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   330
        event.Skip()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   331
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   332
    def OnViewerLeftUp(self, event):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   333
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   334
            Viewer.OnViewerLeftUp(self, event)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   335
        elif self.rubberBand.IsShown():
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   336
            if self.Mode == MODE_SELECTION:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   337
                elements = self.SearchElements(self.rubberBand.GetCurrentExtent())
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   338
                self.rubberBand.OnLeftUp(event, self.GetLogicalDC(), self.Scaling)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   339
                if len(elements) > 0:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   340
                    self.SelectedElement = Graphic_Group(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   341
                    self.SelectedElement.SetElements(elements)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   342
                    self.SelectedElement.SetSelected(True)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   343
        elif self.Mode == MODE_SELECTION and self.SelectedElement:
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   344
            dc = self.GetLogicalDC() 
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   345
            if not isinstance(self.SelectedElement, Graphic_Group):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   346
                if self.IsWire(self.SelectedElement):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   347
                    result = self.SelectedElement.TestSegment(event.GetLogicalPosition(dc), True)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   348
                    if result and result[1] in [EAST, WEST]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   349
                        self.SelectedElement.SetSelectedSegment(result[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   350
                else:
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   351
                    self.SelectedElement.OnLeftUp(event, dc, self.Scaling)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   352
            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   353
                for element in self.SelectedElement.GetElements():
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   354
                    if self.IsWire(element):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   355
                        result = element.TestSegment(event.GetLogicalPosition(dc), True)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   356
                        if result and result[1] in [EAST, WEST]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   357
                            element.SetSelectedSegment(result[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   358
                    else:
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   359
                        element.OnLeftUp(event, dc, self.Scaling)
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 122
diff changeset
   360
            self.SelectedElement.Refresh()
382
42a9b03bba82 Redefine cursor switching procedure in graphic viewers
laurent
parents: 249
diff changeset
   361
            wx.CallAfter(self.SetCurrentCursor, 0)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   362
        event.Skip()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   363
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   364
    def OnViewerRightUp(self, event):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   365
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   366
            Viewer.OnViewerRightUp(self, event)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   367
        else:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   368
            dc = self.GetLogicalDC()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   369
            pos = event.GetLogicalPosition(dc)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   370
            element = self.FindElement(pos)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   371
            if element:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   372
                if self.SelectedElement and self.SelectedElement != element:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   373
                    self.SelectedElement.SetSelected(False)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   374
                self.SelectedElement = element
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   375
                if self.IsWire(self.SelectedElement):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   376
                    self.SelectedElement.SetSelectedSegment(0)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   377
                else:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   378
                    self.SelectedElement.SetSelected(True)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   379
                    self.SelectedElement.OnRightUp(event, dc, self.Scaling)
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 122
diff changeset
   380
                    self.SelectedElement.Refresh()
382
42a9b03bba82 Redefine cursor switching procedure in graphic viewers
laurent
parents: 249
diff changeset
   381
                wx.CallAfter(self.SetCurrentCursor, 0)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   382
        event.Skip()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   383
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   384
    def OnViewerLeftDClick(self, event):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   385
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   386
            Viewer.OnViewerLeftDClick(self, event)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   387
        elif self.Mode == MODE_SELECTION and self.SelectedElement:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   388
            self.SelectedElement.OnLeftDClick(event, self.GetLogicalDC(), self.Scaling)
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 122
diff changeset
   389
            self.SelectedElement.Refresh()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   390
        event.Skip()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   391
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   392
    def OnViewerMotion(self, event):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   393
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   394
            Viewer.OnViewerMotion(self, event)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   395
        elif self.rubberBand.IsShown():
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   396
            self.rubberBand.OnMotion(event, self.GetLogicalDC(), self.Scaling)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   397
        event.Skip()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   398
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   399
#-------------------------------------------------------------------------------
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   400
#                          Keyboard event functions
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   401
#-------------------------------------------------------------------------------
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   402
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   403
    def OnChar(self, event):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   404
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   405
            Viewer.OnChar(self, event)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   406
        else:
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   407
            xpos, ypos = self.GetScrollPos(wx.HORIZONTAL), self.GetScrollPos(wx.VERTICAL)
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   408
            xmax = self.GetScrollRange(wx.HORIZONTAL) - self.GetScrollThumb(wx.HORIZONTAL)
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   409
            ymax = self.GetScrollRange(wx.VERTICAL) - self.GetScrollThumb(wx.VERTICAL)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   410
            keycode = event.GetKeyCode()
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   411
            if keycode == wx.WXK_DELETE and self.SelectedElement:
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   412
                if self.IsBlock(self.SelectedElement):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   413
                    self.SelectedElement.Delete()
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   414
                elif self.IsWire(self.SelectedElement):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   415
                    self.DeleteWire(self.SelectedElement)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   416
                elif isinstance(self.SelectedElement, Graphic_Group):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   417
                    all_wires = True
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   418
                    for element in self.SelectedElement.GetElements():
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   419
                        all_wires &= self.IsWire(element)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   420
                    if all_wires:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   421
                        self.DeleteWire(self.SelectedElement)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   422
                    else:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   423
                        self.SelectedElement.Delete()
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   424
                self.RefreshBuffer()
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   425
                self.RefreshScrollBars()
155
b695f7459ef6 Removing flickering on Windows
lbessard
parents: 144
diff changeset
   426
                self.Refresh(False)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   427
            elif keycode == wx.WXK_LEFT:
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   428
                if event.ControlDown() and event.ShiftDown():
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   429
                    self.Scroll(0, ypos)
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   430
                elif event.ControlDown():
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   431
                    self.Scroll(max(0, xpos - 1), ypos)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   432
            elif keycode == wx.WXK_RIGHT:
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   433
                if event.ControlDown() and event.ShiftDown():
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   434
                    self.Scroll(xmax, ypos)
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   435
                elif event.ControlDown():
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   436
                    self.Scroll(min(xpos + 1, xmax), ypos)
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   437
            elif keycode == wx.WXK_UP:
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   438
                if event.ControlDown() and event.ShiftDown():
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   439
                    self.Scroll(xpos, 0)
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   440
                elif event.ControlDown():
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   441
                    self.Scroll(xpos, max(0, ypos - 1))
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   442
            elif keycode == wx.WXK_DOWN:
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   443
                if event.ControlDown() and event.ShiftDown():
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   444
                    self.Scroll(xpos, ymax)
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   445
                elif event.ControlDown():
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   446
                    self.Scroll(xpos, min(ypos + 1, ymax))
112
317148fc1225 Adding support for block copy
lbessard
parents: 90
diff changeset
   447
            else:
317148fc1225 Adding support for block copy
lbessard
parents: 90
diff changeset
   448
                event.Skip()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   449
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   450
#-------------------------------------------------------------------------------
510
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   451
#                  Model adding functions from Drop Target
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   452
#-------------------------------------------------------------------------------
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   453
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   454
    def AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type):
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   455
        if var_type == "BOOL":
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   456
            id = self.GetNewId()
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   457
            if var_class == INPUT:
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   458
                contact = LD_Contact(self, CONTACT_NORMAL, var_name, id)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   459
                width, height = contact.GetMinSize()
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   460
                if scaling is not None:
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   461
                    x = round(float(x) / float(scaling[0])) * scaling[0]
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   462
                    y = round(float(y) / float(scaling[1])) * scaling[1]
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   463
                    width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   464
                    height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   465
                contact.SetPosition(x, y)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   466
                contact.SetSize(width, height)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   467
                self.AddBlock(contact)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   468
                self.Controler.AddEditedElementContact(self.GetTagName(), id)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   469
                self.RefreshContactModel(contact)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   470
            else:
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   471
                coil = LD_Coil(self, COIL_NORMAL, var_name, id)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   472
                width, height = coil.GetMinSize()
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   473
                if scaling is not None:
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   474
                    x = round(float(x) / float(scaling[0])) * scaling[0]
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   475
                    y = round(float(y) / float(scaling[1])) * scaling[1]
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   476
                    width = round(float(width) / float(scaling[0]) + 0.5) * scaling[0]
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   477
                    height = round(float(height) / float(scaling[1]) + 0.5) * scaling[1]
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   478
                coil.SetPosition(x, y)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   479
                coil.SetSize(width, height)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   480
                self.AddBlock(coil)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   481
                self.Controler.AddEditedElementCoil(self.GetTagName(), id)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   482
                self.RefreshCoilModel(coil)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   483
            self.RefreshBuffer()
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   484
            self.RefreshScrollBars()
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   485
            self.RefreshVisibleElements()
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   486
            self.Refresh(False)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   487
        else:
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   488
            Viewer.AddVariableBlock(self, x, y, scaling, var_class, var_name, var_type)
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   489
e7327ea490b4 Adding support for creating contact and coil in LD Viewer instead of variable blocks when dropping a variable
laurent
parents: 426
diff changeset
   490
#-------------------------------------------------------------------------------
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   491
#                          Adding element functions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   492
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   493
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   494
    def AddLadderRung(self):
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 80
diff changeset
   495
        dialog = LDElementDialog(self.ParentWindow, "coil")
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 155
diff changeset
   496
        dialog.SetPreviewFont(self.GetFont())
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   497
        varlist = []
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   498
        vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   499
        if vars:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   500
            for var in vars:
6
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
   501
                if var["Class"] != "Input" and var["Type"] == "BOOL":
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   502
                    varlist.append(var["Name"])
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   503
        returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   504
        if returntype == "BOOL":
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   505
            varlist.append(self.Controler.GetEditedElementName(self.TagName))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   506
        dialog.SetVariables(varlist)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   507
        dialog.SetValues({"name":"","type":COIL_NORMAL})
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   508
        if dialog.ShowModal() == wx.ID_OK:
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   509
            values = dialog.GetValues()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   510
            startx, starty = LD_OFFSET[0], 0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   511
            if len(self.Rungs) > 0:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   512
                bbox = self.Rungs[-1].GetBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   513
                starty = bbox.y + bbox.height
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   514
            starty += LD_OFFSET[1]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   515
            rung = Graphic_Group(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   516
            # Create comment
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   517
            id = self.GetNewId()
391
07447ee3538e Adding support for internationalization
laurent
parents: 383
diff changeset
   518
            comment = Comment(self, _("Comment"), id)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   519
            comment.SetPosition(startx, starty)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   520
            comment.SetSize(LD_COMMENT_DEFAULTSIZE[0], LD_COMMENT_DEFAULTSIZE[1])
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   521
            self.AddComment(comment)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   522
            self.RungComments.append(comment)
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   523
            self.Controler.AddEditedElementComment(self.TagName, id)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   524
            self.RefreshCommentModel(comment)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   525
            starty += LD_COMMENT_DEFAULTSIZE[1] + LD_OFFSET[1]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   526
            # Create LeftPowerRail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   527
            id = self.GetNewId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   528
            leftpowerrail = LD_PowerRail(self, LEFTRAIL, id)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   529
            leftpowerrail.SetPosition(startx, starty)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   530
            self.AddBlock(leftpowerrail)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   531
            rung.SelectElement(leftpowerrail)
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   532
            self.Controler.AddEditedElementPowerRail(self.TagName, id, LEFTRAIL)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   533
            self.RefreshPowerRailModel(leftpowerrail)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   534
            # Create Coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   535
            id = self.GetNewId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   536
            coil = LD_Coil(self, values["type"], values["name"], id)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   537
            coil.SetPosition(startx, starty + (LD_LINE_SIZE - LD_ELEMENT_SIZE[1]) / 2)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   538
            self.AddBlock(coil)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   539
            rung.SelectElement(coil)
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   540
            self.Controler.AddEditedElementCoil(self.TagName, id)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   541
            # Create Wire between LeftPowerRail and Coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   542
            wire = Wire(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   543
            start_connector = coil.GetConnectors()["input"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   544
            end_connector = leftpowerrail.GetConnectors()[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   545
            start_connector.Connect((wire, 0), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   546
            end_connector.Connect((wire, -1), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   547
            wire.ConnectStartPoint(None, start_connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   548
            wire.ConnectEndPoint(None, end_connector)
50
4610aafc884e Bugs fixed
lbessard
parents: 45
diff changeset
   549
            self.AddWire(wire)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   550
            rung.SelectElement(wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   551
            # Create RightPowerRail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   552
            id = self.GetNewId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   553
            rightpowerrail = LD_PowerRail(self, RIGHTRAIL, id)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   554
            rightpowerrail.SetPosition(startx, starty)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   555
            self.AddBlock(rightpowerrail)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   556
            rung.SelectElement(rightpowerrail)
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   557
            self.Controler.AddEditedElementPowerRail(self.TagName, id, RIGHTRAIL)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   558
            # Create Wire between LeftPowerRail and Coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   559
            wire = Wire(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   560
            start_connector = rightpowerrail.GetConnectors()[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   561
            end_connector = coil.GetConnectors()["output"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   562
            start_connector.Connect((wire, 0), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   563
            end_connector.Connect((wire, -1), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   564
            wire.ConnectStartPoint(None, start_connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   565
            wire.ConnectEndPoint(None, end_connector)
50
4610aafc884e Bugs fixed
lbessard
parents: 45
diff changeset
   566
            self.AddWire(wire)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   567
            rung.SelectElement(wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   568
            self.RefreshPosition(coil)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   569
            self.Rungs.append(rung)
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   570
            self.RefreshBuffer()
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   571
            self.RefreshScrollBars()
155
b695f7459ef6 Removing flickering on Windows
lbessard
parents: 144
diff changeset
   572
            self.Refresh(False)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   573
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   574
    def AddLadderContact(self):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   575
        wires = []
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   576
        if self.IsWire(self.SelectedElement):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   577
            left_element = self.SelectedElement.EndConnected
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   578
            if not isinstance(left_element.GetParentBlock(), LD_Coil):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   579
                wires.append(self.SelectedElement)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   580
        elif self.SelectedElement and isinstance(self.SelectedElement,Graphic_Group):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   581
            if False not in [self.IsWire(element) for element in self.SelectedElement.GetElements()]:
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   582
                for element in self.SelectedElement.GetElements():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   583
                    wires.append(element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   584
        if len(wires) > 0:
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 80
diff changeset
   585
            dialog = LDElementDialog(self.ParentWindow, "contact")
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 155
diff changeset
   586
            dialog.SetPreviewFont(self.GetFont())
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   587
            varlist = []
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   588
            vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   589
            if vars:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   590
                for var in vars:
6
c8cf918ee7ea Bug corrected and improvements
lbessard
parents: 5
diff changeset
   591
                    if var["Class"] != "Output" and var["Type"] == "BOOL":
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   592
                        varlist.append(var["Name"])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   593
            dialog.SetVariables(varlist)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   594
            dialog.SetValues({"name":"","type":CONTACT_NORMAL})
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   595
            if dialog.ShowModal() == wx.ID_OK:
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   596
                values = dialog.GetValues()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   597
                points = wires[0].GetSelectedSegmentPoints()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   598
                id = self.GetNewId()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   599
                contact = LD_Contact(self, values["type"], values["name"], id)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   600
                contact.SetPosition(0, points[0].y - (LD_ELEMENT_SIZE[1] + 1) / 2)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   601
                self.AddBlock(contact)
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   602
                self.Controler.AddEditedElementContact(self.TagName, id)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   603
                rungindex = self.FindRung(wires[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   604
                rung = self.Rungs[rungindex]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   605
                old_bbox = rung.GetBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   606
                rung.SelectElement(contact)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   607
                connectors = contact.GetConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   608
                left_elements = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   609
                right_elements = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   610
                left_index = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   611
                right_index = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   612
                for wire in wires:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   613
                    if wire.EndConnected not in left_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   614
                        left_elements.append(wire.EndConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   615
                        left_index.append(wire.EndConnected.GetWireIndex(wire))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   616
                    else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   617
                        idx = left_elements.index(wire.EndConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   618
                        left_index[idx] = min(left_index[idx], wire.EndConnected.GetWireIndex(wire))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   619
                    if wire.StartConnected not in right_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   620
                        right_elements.append(wire.StartConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   621
                        right_index.append(wire.StartConnected.GetWireIndex(wire))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   622
                    else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   623
                        idx = right_elements.index(wire.StartConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   624
                        right_index[idx] = min(right_index[idx], wire.StartConnected.GetWireIndex(wire))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   625
                    wire.SetSelectedSegment(None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   626
                    wire.Clean()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   627
                    rung.SelectElement(wire)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   628
                    self.RemoveWire(wire)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   629
                wires = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   630
                right_wires = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   631
                for i, left_element in enumerate(left_elements):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   632
                    wire = Wire(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   633
                    wires.append(wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   634
                    connectors["input"].Connect((wire, 0), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   635
                    left_element.InsertConnect(left_index[i], (wire, -1), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   636
                    wire.ConnectStartPoint(None, connectors["input"])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   637
                    wire.ConnectEndPoint(None, left_element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   638
                for i, right_element in enumerate(right_elements):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   639
                    wire = Wire(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   640
                    wires.append(wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   641
                    right_wires.append(wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   642
                    right_element.InsertConnect(right_index[i], (wire, 0), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   643
                    connectors["output"].Connect((wire, -1), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   644
                    wire.ConnectStartPoint(None, right_element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   645
                    wire.ConnectEndPoint(None, connectors["output"])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   646
                right_wires.reverse()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   647
                for wire in wires:
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   648
                    self.AddWire(wire)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   649
                    rung.SelectElement(wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   650
                self.RefreshPosition(contact)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   651
                if len(right_wires) > 1:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   652
                    group = Graphic_Group(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   653
                    group.SetSelected(False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   654
                    for wire in right_wires:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   655
                        wire.SetSelectedSegment(-1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   656
                        group.SelectElement(wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   657
                    self.SelectedElement = group
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   658
                else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   659
                    right_wires[0].SetSelectedSegment(-1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   660
                    self.SelectedElement = right_wires[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   661
                rung.RefreshBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   662
                new_bbox = rung.GetBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   663
                self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   664
                self.RefreshBuffer()
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   665
                self.RefreshScrollBars()
155
b695f7459ef6 Removing flickering on Windows
lbessard
parents: 144
diff changeset
   666
                self.Refresh(False)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   667
        else:
391
07447ee3538e Adding support for internationalization
laurent
parents: 383
diff changeset
   668
            message = wx.MessageDialog(self, _("You must select the wire where a contact should be added!"), _("Error"), wx.OK|wx.ICON_ERROR)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   669
            message.ShowModal()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   670
            message.Destroy()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   671
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   672
    def AddLadderBranch(self):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   673
        blocks = []
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   674
        if self.IsBlock(self.SelectedElement):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   675
            blocks = [self.SelectedElement]
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   676
        elif isinstance(self.SelectedElement, Graphic_Group):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   677
            elements = self.SelectedElement.GetElements()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   678
            for element in elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   679
                blocks.append(element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   680
        if len(blocks) > 0:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   681
            blocks_infos = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   682
            left_elements = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   683
            left_index = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   684
            right_elements = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   685
            right_index = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   686
            for block in blocks:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   687
                connectors = block.GetConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   688
                block_infos = {"inputs":[],"outputs":[],"lefts":[],"rights":[]}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   689
                if "inputs" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   690
                    block_infos["inputs"] = connectors["inputs"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   691
                if "outputs" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   692
                    block_infos["outputs"] = connectors["outputs"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   693
                if "input" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   694
                    block_infos["inputs"] = [connectors["input"]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   695
                if "output" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   696
                    block_infos["outputs"] = [connectors["output"]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   697
                for connector in block_infos["inputs"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   698
                    for wire, handle in connector.GetWires():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   699
                        found = False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   700
                        for infos in blocks_infos:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   701
                            if wire.EndConnected in infos["outputs"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   702
                                for left_element in infos["lefts"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   703
                                    if left_element not in block_infos["lefts"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   704
                                        block_infos["lefts"].append(left_element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   705
                                found = True
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   706
                        if not found and wire.EndConnected not in block_infos["lefts"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   707
                            block_infos["lefts"].append(wire.EndConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   708
                            if wire.EndConnected not in left_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   709
                                left_elements.append(wire.EndConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   710
                                left_index.append(wire.EndConnected.GetWireIndex(wire))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   711
                            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   712
                                index = left_elements.index(wire.EndConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   713
                                left_index[index] = max(left_index[index], wire.EndConnected.GetWireIndex(wire))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   714
                for connector in block_infos["outputs"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   715
                    for wire, handle in connector.GetWires():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   716
                        found = False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   717
                        for infos in blocks_infos:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   718
                            if wire.StartConnected in infos["inputs"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   719
                                for right_element in infos["rights"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   720
                                    if right_element not in block_infos["rights"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   721
                                        block_infos["rights"].append(right_element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   722
                                found = True
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   723
                        if not found and wire.StartConnected not in block_infos["rights"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   724
                            block_infos["rights"].append(wire.StartConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   725
                            if wire.StartConnected not in right_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   726
                                right_elements.append(wire.StartConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   727
                                right_index.append(wire.StartConnected.GetWireIndex(wire))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   728
                            else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   729
                                index = right_elements.index(wire.StartConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   730
                                right_index[index] = max(right_index[index], wire.StartConnected.GetWireIndex(wire))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   731
                for connector in block_infos["inputs"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   732
                    for infos in blocks_infos:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   733
                        if connector in infos["rights"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   734
                            infos["rights"].remove(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   735
                            if connector in right_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   736
                                index = right_elements.index(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   737
                                right_elements.pop(index)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   738
                                right_index.pop(index)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   739
                            for right_element in block_infos["rights"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   740
                                if right_element not in infos["rights"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   741
                                    infos["rights"].append(right_element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   742
                for connector in block_infos["outputs"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   743
                    for infos in blocks_infos:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   744
                        if connector in infos["lefts"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   745
                            infos["lefts"].remove(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   746
                            if connector in left_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   747
                                index = left_elements.index(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   748
                                left_elements.pop(index)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   749
                                left_index.pop(index)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   750
                            for left_element in block_infos["lefts"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   751
                                if left_element not in infos["lefts"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   752
                                    infos["lefts"].append(left_element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   753
                blocks_infos.append(block_infos)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   754
            for infos in blocks_infos:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   755
                left_elements = [element for element in infos["lefts"]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   756
                for left_element in left_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   757
                    if isinstance(left_element.GetParentBlock(), LD_PowerRail):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   758
                        infos["lefts"].remove(left_element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   759
                        if "LD_PowerRail" not in infos["lefts"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   760
                            infos["lefts"].append("LD_PowerRail")
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   761
                right_elements = [element for element in infos["rights"]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   762
                for right_element in right_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   763
                    if isinstance(right_element.GetParentBlock(), LD_PowerRail):
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   764
                        infos["rights"].remove(right_element)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   765
                        if "LD_PowerRail" not in infos["rights"]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   766
                            infos["rights"].append("LD_PowerRail")
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   767
                infos["lefts"].sort()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   768
                infos["rights"].sort()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   769
            lefts = blocks_infos[0]["lefts"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   770
            rights = blocks_infos[0]["rights"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   771
            good = True
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   772
            for infos in blocks_infos[1:]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   773
                good &= infos["lefts"] == lefts
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   774
                good &= infos["rights"] == rights
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   775
            if good:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   776
                rungindex = self.FindRung(blocks[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   777
                rung = self.Rungs[rungindex]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   778
                old_bbox = rung.GetBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   779
                left_powerrail = True
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   780
                right_powerrail = True
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   781
                for element in left_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   782
                    left_powerrail &= isinstance(element.GetParentBlock(), LD_PowerRail)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   783
                for element in right_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   784
                    right_powerrail &= isinstance(element.GetParentBlock(), LD_PowerRail)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   785
                if not left_powerrail or not right_powerrail:
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   786
                    wires = []
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   787
                    if left_powerrail:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   788
                        powerrail = left_elements[0].GetParentBlock()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   789
                        index = 0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   790
                        for left_element in left_elements: 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   791
                            index = max(index, powerrail.GetConnectorIndex(left_element))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   792
                        if powerrail.IsNullConnector(index + 1):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   793
                            powerrail.DeleteConnector(index + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   794
                        powerrail.InsertConnector(index + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   795
                        powerrail.RefreshModel()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   796
                        connectors = powerrail.GetConnectors()
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   797
                        right_elements.reverse()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   798
                        for i, right_element in enumerate(right_elements):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   799
                            new_wire = Wire(self)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   800
                            wires.append(new_wire)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   801
                            right_element.InsertConnect(right_index[i] + 1, (new_wire, 0), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   802
                            connectors[index + 1].Connect((new_wire, -1), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   803
                            new_wire.ConnectStartPoint(None, right_element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   804
                            new_wire.ConnectEndPoint(None, connectors[index + 1])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   805
                        right_elements.reverse()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   806
                    elif right_powerrail:
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 80
diff changeset
   807
                        dialog = LDElementDialog(self.ParentWindow, "coil")
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 155
diff changeset
   808
                        dialog.SetPreviewFont(self.GetFont())
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   809
                        varlist = []
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   810
                        vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   811
                        if vars:
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   812
                            for var in vars:
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   813
                                if var["Class"] != "Input" and var["Type"] == "BOOL":
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   814
                                    varlist.append(var["Name"])
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
   815
                        returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   816
                        if returntype == "BOOL":
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   817
                            varlist.append(self.Controler.GetEditedElementName(self.TagName))
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   818
                        dialog.SetVariables(varlist)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   819
                        dialog.SetValues({"name":"","type":COIL_NORMAL})
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
   820
                        if dialog.ShowModal() == wx.ID_OK:
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   821
                            values = dialog.GetValues()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   822
                            powerrail = right_elements[0].GetParentBlock()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   823
                            index = 0
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   824
                            for right_element in right_elements: 
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   825
                                index = max(index, powerrail.GetConnectorIndex(right_element))
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   826
                            if powerrail.IsNullConnector(index + 1):
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   827
                                powerrail.DeleteConnector(index + 1)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   828
                            powerrail.InsertConnector(index + 1)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   829
                            powerrail.RefreshModel()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   830
                            connectors = powerrail.GetConnectors()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   831
                            # Create Coil
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   832
                            id = self.GetNewId()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   833
                            coil = LD_Coil(self, values["type"], values["name"], id)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   834
                            pos = blocks[0].GetPosition()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   835
                            coil.SetPosition(pos[0], pos[1] + LD_LINE_SIZE)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   836
                            self.AddBlock(coil)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   837
                            rung.SelectElement(coil)
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   838
                            self.Controler.AddEditedElementCoil(self.TagName, id)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   839
                            coil_connectors = coil.GetConnectors()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   840
                            # Create Wire between LeftPowerRail and Coil
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   841
                            wire = Wire(self)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   842
                            connectors[index + 1].Connect((wire, 0), False)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   843
                            coil_connectors["output"].Connect((wire, -1), False)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   844
                            wire.ConnectStartPoint(None, connectors[index + 1])
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   845
                            wire.ConnectEndPoint(None, coil_connectors["output"])
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   846
                            self.AddWire(wire)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   847
                            rung.SelectElement(wire)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   848
                            left_elements.reverse()
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   849
                            for i, left_element in enumerate(left_elements):
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   850
                                # Create Wire between LeftPowerRail and Coil
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   851
                                new_wire = Wire(self)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   852
                                wires.append(new_wire)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   853
                                coil_connectors["input"].Connect((new_wire, 0), False)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   854
                                left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   855
                                new_wire.ConnectStartPoint(None, coil_connectors["input"])
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   856
                                new_wire.ConnectEndPoint(None, left_element)
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   857
                            self.RefreshPosition(coil)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   858
                    else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   859
                        left_elements.reverse()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   860
                        right_elements.reverse()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   861
                        for i, left_element in enumerate(left_elements):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   862
                            for j, right_element in enumerate(right_elements):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   863
                                exist = False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   864
                                for wire, handle in right_element.GetWires():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   865
                                    exist |= wire.EndConnected == left_element
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   866
                                if not exist:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   867
                                    new_wire = Wire(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   868
                                    wires.append(new_wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   869
                                    right_element.InsertConnect(right_index[j] + 1, (new_wire, 0), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   870
                                    left_element.InsertConnect(left_index[i] + 1, (new_wire, -1), False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   871
                                    new_wire.ConnectStartPoint(None, right_element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   872
                                    new_wire.ConnectEndPoint(None, left_element)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   873
                    wires.reverse()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   874
                    for wire in wires:
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   875
                        self.AddWire(wire)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   876
                        rung.SelectElement(wire)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   877
                    right_elements.reverse()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   878
                for block in blocks:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   879
                    self.RefreshPosition(block)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   880
                for right_element in right_elements:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   881
                    self.RefreshPosition(right_element.GetParentBlock())
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   882
                self.SelectedElement.RefreshBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   883
                rung.RefreshBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   884
                new_bbox = rung.GetBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   885
                self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   886
                self.RefreshBuffer()
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
   887
                self.RefreshScrollBars()
155
b695f7459ef6 Removing flickering on Windows
lbessard
parents: 144
diff changeset
   888
                self.Refresh(False)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   889
            else:
391
07447ee3538e Adding support for internationalization
laurent
parents: 383
diff changeset
   890
                message = wx.MessageDialog(self, _("The group of block must be coherent!"), _("Error"), wx.OK|wx.ICON_ERROR)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   891
                message.ShowModal()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   892
                message.Destroy()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   893
        else:
391
07447ee3538e Adding support for internationalization
laurent
parents: 383
diff changeset
   894
            message = wx.MessageDialog(self, _("You must select the block or group of blocks around which a branch should be added!"), _("Error"), wx.OK|wx.ICON_ERROR)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   895
            message.ShowModal()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   896
            message.Destroy()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   897
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   898
    def AddLadderBlock(self):
391
07447ee3538e Adding support for internationalization
laurent
parents: 383
diff changeset
   899
        message = wx.MessageDialog(self, _("This option isn't available yet!"), _("Warning"), wx.OK|wx.ICON_EXCLAMATION)
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   900
        message.ShowModal()
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 6
diff changeset
   901
        message.Destroy()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   902
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   903
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   904
#                          Delete element functions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   905
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   906
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   907
    def DeleteContact(self, contact):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   908
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   909
            Viewer.DeleteContact(self, contact)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   910
        else:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   911
            rungindex = self.FindRung(contact)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   912
            rung = self.Rungs[rungindex]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   913
            old_bbox = rung.GetBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   914
            connectors = contact.GetConnectors()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   915
            input_wires = [wire for wire, handle in connectors["input"].GetWires()]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   916
            output_wires = [wire for wire, handle in connectors["output"].GetWires()]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   917
            left_elements = [(wire.EndConnected, wire.EndConnected.GetWireIndex(wire)) for wire in input_wires]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   918
            right_elements = [(wire.StartConnected, wire.StartConnected.GetWireIndex(wire)) for wire in output_wires]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   919
            for wire in input_wires:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   920
                wire.Clean()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   921
                rung.SelectElement(wire)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   922
                self.RemoveWire(wire)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   923
            for wire in output_wires:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   924
                wire.Clean()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   925
                rung.SelectElement(wire)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   926
                self.RemoveWire(wire)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   927
            rung.SelectElement(contact)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   928
            contact.Clean()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   929
            left_elements.reverse()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   930
            right_elements.reverse()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   931
            powerrail = len(left_elements) == 1 and isinstance(left_elements[0][0].GetParentBlock(), LD_PowerRail)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   932
            for left_element, left_index in left_elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   933
                for right_element, right_index in right_elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   934
                    wire_removed = []
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   935
                    for wire, handle in right_element.GetWires():
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   936
                        if wire.EndConnected == left_element:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   937
                            wire_removed.append(wire)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   938
                        elif isinstance(wire.EndConnected.GetParentBlock(), LD_PowerRail) and powerrail:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   939
                            left_powerrail = wire.EndConnected.GetParentBlock()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   940
                            index = left_powerrail.GetConnectorIndex(wire.EndConnected)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   941
                            left_powerrail.DeleteConnector(index)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   942
                            wire_removed.append(wire)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   943
                    for wire in wire_removed:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   944
                        wire.Clean()
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   945
                        self.RemoveWire(wire)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   946
                        rung.SelectElement(wire)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   947
            wires = []
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   948
            for left_element, left_index in left_elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   949
                for right_element, right_index in right_elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   950
                    wire = Wire(self)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   951
                    wires.append(wire)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   952
                    right_element.InsertConnect(right_index, (wire, 0), False)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   953
                    left_element.InsertConnect(left_index, (wire, -1), False)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   954
                    wire.ConnectStartPoint(None, right_element)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   955
                    wire.ConnectEndPoint(None, left_element)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   956
            wires.reverse()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   957
            for wire in wires:
50
4610aafc884e Bugs fixed
lbessard
parents: 45
diff changeset
   958
                self.AddWire(wire)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   959
                rung.SelectElement(wire)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   960
            right_elements.reverse()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   961
            for right_element, right_index in right_elements:
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   962
                self.RefreshPosition(right_element.GetParentBlock())
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   963
            self.RemoveBlock(contact)
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   964
            self.Controler.RemoveEditedElementInstance(self.TagName, contact.GetId())
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   965
            rung.RefreshBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   966
            new_bbox = rung.GetBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   967
            self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   968
            self.SelectedElement = None
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   969
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   970
    def RecursiveDeletion(self, element, rung):
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   971
        connectors = element.GetConnectors()
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   972
        input_wires = [wire for wire, handle in connectors["input"].GetWires()]
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   973
        left_elements = [wire.EndConnected for wire in input_wires]
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   974
        rung.SelectElement(element)
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   975
        element.Clean()
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   976
        for wire in input_wires:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   977
            wire.Clean()
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   978
            self.RemoveWire(wire)
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   979
            rung.SelectElement(wire)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   980
        self.RemoveBlock(element)
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
   981
        self.Controler.RemoveEditedElementInstance(self.TagName, element.GetId())
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   982
        for left_element in left_elements:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   983
            block = left_element.GetParentBlock()
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   984
            if len(left_element.GetWires()) == 0:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   985
                self.RecursiveDeletion(block, rung)
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   986
            else:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   987
                self.RefreshPosition(block)
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   988
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   989
    def DeleteCoil(self, coil):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   990
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   991
            Viewer.DeleteContact(self, coil)
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   992
        else:
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   993
            rungindex = self.FindRung(coil)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   994
            rung = self.Rungs[rungindex]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   995
            old_bbox = rung.GetBoundingBox()
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   996
            nbcoils = 0
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   997
            for element in rung.GetElements():
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   998
                if isinstance(element, LD_Coil):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
   999
                    nbcoils += 1
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1000
            if nbcoils > 1:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1001
                connectors = coil.GetConnectors()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1002
                output_wires = [wire for wire, handle in connectors["output"].GetWires()]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1003
                right_elements = [wire.StartConnected for wire in output_wires]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1004
                for wire in output_wires:
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1005
                    wire.Clean()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1006
                    self.Wires.remove(wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1007
                    self.Elements.remove(wire)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1008
                    rung.SelectElement(wire)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1009
                for right_element in right_elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1010
                    right_block = right_element.GetParentBlock()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1011
                    if isinstance(right_block, LD_PowerRail):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1012
                        if len(right_element.GetWires()) == 0:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1013
                            index = right_block.GetConnectorIndex(right_element)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1014
                            right_block.DeleteConnector(index)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1015
                            powerrail_connectors = right_block.GetConnectors()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1016
                            for connector in powerrail_connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1017
                                for wire, handle in connector.GetWires():
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1018
                                    block = wire.EndConnected.GetParentBlock()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1019
                                    endpoint = wire.EndConnected.GetPosition(False)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1020
                                    startpoint = connector.GetPosition(False)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1021
                                    block.Move(0, startpoint.y - endpoint.y)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1022
                                    self.RefreshPosition(block)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1023
                self.RecursiveDeletion(coil, rung)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1024
            else:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1025
                for element in rung.GetElements():
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1026
                    if self.IsWire(element):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1027
                        element.Clean()
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1028
                        self.RemoveWire(element)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1029
                for element in rung.GetElements():
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1030
                    if self.IsBlock(element):
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
  1031
                        self.Controler.RemoveEditedElementInstance(self.TagName, element.GetId())
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1032
                        self.RemoveBlock(element)
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
  1033
                self.Controler.RemoveEditedElementInstance(self.TagName, self.Comments[rungindex].GetId())
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1034
                self.RemoveComment(self.RungComments[rungindex])
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1035
                self.RungComments.pop(rungindex)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1036
                self.Rungs.pop(rungindex)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1037
                if rungindex < len(self.Rungs):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1038
                    next_bbox = self.Rungs[rungindex].GetBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1039
                    self.RefreshRungs(old_bbox.y - next_bbox.y, rungindex)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1040
            self.SelectedElement = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1041
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1042
    def DeleteWire(self, wire):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1043
        if self.GetDrawingMode() == FREEDRAWING_MODE:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1044
            Viewer.DeleteWire(self, wire)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1045
        else:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1046
            wires = []
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1047
            left_elements = []
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1048
            right_elements = []
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1049
            if self.IsWire(wire):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1050
                wires = [wire]
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1051
            elif isinstance(wire, Graphic_Group):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1052
                for element in wire.GetElements():
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1053
                    if self.IsWire(element):
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1054
                        wires.append(element)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1055
                    else:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1056
                        wires = []
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1057
                        break
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1058
            if len(wires) > 0:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1059
                rungindex = self.FindRung(wires[0])
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1060
                rung = self.Rungs[rungindex]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1061
                old_bbox = rung.GetBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1062
                for wire in wires:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1063
                    connections = wire.GetSelectedSegmentConnections()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1064
                    left_block = wire.EndConnected.GetParentBlock()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1065
                    if wire.EndConnected not in left_elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1066
                        left_elements.append(wire.EndConnected)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1067
                    if wire.StartConnected not in right_elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1068
                        right_elements.append(wire.StartConnected)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1069
                    if connections == (False, False) or connections == (False, True) and isinstance(left_block, LD_PowerRail):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1070
                        wire.Clean()
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1071
                        self.RemoveWire(wire)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1072
                        rung.SelectElement(wire)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1073
                for left_element in left_elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1074
                    left_block = left_element.GetParentBlock()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1075
                    if isinstance(left_block, LD_PowerRail):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1076
                        if len(left_element.GetWires()) == 0:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1077
                            index = left_block.GetConnectorIndex(left_element)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1078
                            left_block.DeleteConnector(index)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1079
                    else:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1080
                        connectors = left_block.GetConnectors()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1081
                        output_connectors = []
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1082
                        if "outputs" in connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1083
                            output_connectors = connectors["outputs"]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1084
                        if "output" in connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1085
                            output_connectors = [connectors["output"]]
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1086
                        for connector in output_connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1087
                            for wire, handle in connector.GetWires():
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1088
                                self.RefreshPosition(wire.StartConnected.GetParentBlock())
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1089
                for right_element in right_elements:
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1090
                    self.RefreshPosition(right_element.GetParentBlock())
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1091
                rung.RefreshBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1092
                new_bbox = rung.GetBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1093
                self.RefreshRungs(new_bbox.height - old_bbox.height, rungindex + 1)
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1094
                self.SelectedElement = None
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1095
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1096
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1097
#                        Refresh element position functions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1098
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1099
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1100
    def RefreshPosition(self, element, recursive=True):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1101
        if isinstance(element, LD_PowerRail) and element.GetType() == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1102
            element.RefreshModel()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1103
            return
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1104
        connectors = element.GetConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1105
        input_connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1106
        output_connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1107
        if isinstance(element, LD_PowerRail) and element.GetType() == RIGHTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1108
            input_connectors = connectors
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1109
            for i, connector in enumerate(input_connectors):
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1110
                for j, (wire, handle) in enumerate(connector.GetWires()):
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1111
                    block = wire.EndConnected.GetParentBlock()
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1112
                    self.RefreshPosition(block, False)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1113
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1114
            if "inputs" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1115
                input_connectors = connectors["inputs"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1116
            if "outputs" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1117
                output_connectors = connectors["outputs"]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1118
            if "input" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1119
                input_connectors = [connectors["input"]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1120
            if "output" in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1121
                output_connectors = [connectors["output"]]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1122
        position = element.GetPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1123
        minx = 0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1124
        onlyone = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1125
        for connector in input_connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1126
            onlyone.append(len(connector.GetWires()) == 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1127
            for wire, handle in connector.GetWires():
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1128
                onlyone[-1] &= len(wire.EndConnected.GetWires()) == 1
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1129
                leftblock = wire.EndConnected.GetParentBlock()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1130
                pos = leftblock.GetPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1131
                size = leftblock.GetSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1132
                minx = max(minx, pos[0] + size[0])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1133
        if isinstance(element, LD_Coil):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1134
            interval = LD_WIRECOIL_SIZE
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1135
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1136
            interval = LD_WIRE_SIZE
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1137
        if False in onlyone:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1138
            interval += LD_WIRE_SIZE
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1139
        movex = minx + interval - position[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1140
        element.Move(movex, 0)
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1141
        position = element.GetPosition()
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1142
        blocks = []
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1143
        for i, connector in enumerate(input_connectors):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1144
            for j, (wire, handle) in enumerate(connector.GetWires()):
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1145
                blocks.append(wire.EndConnected.GetParentBlock())
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1146
        for i, connector in enumerate(input_connectors):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1147
            startpoint = connector.GetPosition(False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1148
            previous_blocks = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1149
            block_list = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1150
            start_offset = 0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1151
            if not onlyone[i]:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1152
                middlepoint = minx + LD_WIRE_SIZE
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1153
            for j, (wire, handle) in enumerate(connector.GetWires()):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1154
                block = wire.EndConnected.GetParentBlock()
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1155
                if isinstance(element, LD_PowerRail):
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1156
                    pos = block.GetPosition()
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1157
                    size = leftblock.GetSize()
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1158
                    movex = position[0] - LD_WIRE_SIZE - size[0] - pos[0]
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1159
                    block.Move(movex, 0)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1160
                endpoint = wire.EndConnected.GetPosition(False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1161
                if j == 0:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1162
                    if not onlyone[i] and wire.EndConnected.GetWireIndex(wire) > 0:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1163
                        start_offset = endpoint.y - startpoint.y
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1164
                    offset = start_offset
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1165
                else:
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1166
                    offset = start_offset + LD_LINE_SIZE * CalcBranchSize(previous_blocks, blocks)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1167
                if block in block_list:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1168
                    wires = wire.EndConnected.GetWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1169
                    endmiddlepoint = wires[0][0].StartConnected.GetPosition(False)[0] - LD_WIRE_SIZE
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
  1170
                    points = [startpoint, wx.Point(middlepoint, startpoint.y),
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
  1171
                              wx.Point(middlepoint, startpoint.y + offset),
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
  1172
                              wx.Point(endmiddlepoint, startpoint.y + offset),
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
  1173
                              wx.Point(endmiddlepoint, endpoint.y), endpoint]
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1174
                else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1175
                    if startpoint.y + offset != endpoint.y:
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1176
                        if isinstance(element, LD_PowerRail):
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1177
                            diff = (startpoint.y - endpoint.y) / LD_LINE_SIZE
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1178
                            for k in xrange(abs(diff)):
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1179
                                if diff < 0:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1180
                                    element.DeleteConnector(i - 1 - k)
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1181
                                else:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1182
                                    element.InsertConnector(i + k, False)
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1183
                        elif isinstance(block, LD_PowerRail):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1184
                            index = block.GetConnectorIndex(wire.EndConnected)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1185
                            if index:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1186
                                diff = (startpoint.y - endpoint.y) / LD_LINE_SIZE
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1187
                                for k in xrange(abs(diff)):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1188
                                    if diff < 0:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1189
                                        block.DeleteConnector(index - 1 - k)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1190
                                    else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1191
                                        block.InsertConnector(index + k, False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1192
                        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1193
                            block.Move(0, startpoint.y + offset - endpoint.y)
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1194
                            self.RefreshPosition(block, False)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1195
                        endpoint = wire.EndConnected.GetPosition(False)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1196
                    if not onlyone[i]:
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
  1197
                        points = [startpoint, wx.Point(middlepoint, startpoint.y),
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
  1198
                                  wx.Point(middlepoint, endpoint.y), endpoint]
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1199
                    else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1200
                        points = [startpoint, endpoint]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1201
                wire.SetPoints(points)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1202
                previous_blocks.append(block)
27
dae55dd9ee14 Current developping version
lbessard
parents: 9
diff changeset
  1203
                blocks.remove(block)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1204
                ExtractNextBlocks(block, block_list)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1205
        element.RefreshModel(False)
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1206
        if recursive:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1207
            for connector in output_connectors:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1208
                for wire, handle in connector.GetWires():
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
  1209
                    self.RefreshPosition(wire.StartConnected.GetParentBlock())
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1210
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1211
    def RefreshRungs(self, movey, fromidx):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1212
        if movey != 0:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1213
            for i in xrange(fromidx, len(self.Rungs)):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1214
                self.RungComments[i].Move(0, movey)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1215
                self.RungComments[i].RefreshModel()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1216
                self.Rungs[i].Move(0, movey)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1217
                for element in self.Rungs[i].GetElements():
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
  1218
                    if self.IsBlock(element):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1219
                        self.RefreshPosition(element)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1220
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1221
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1222
#                          Edit element content functions
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1223
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1224
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1225
    def EditContactContent(self, contact):
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1226
        if self.GetDrawingMode() == FREEDRAWING_MODE:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1227
            Viewer.EditContactContent(self, contact)
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1228
        else:
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 80
diff changeset
  1229
            dialog = LDElementDialog(self.ParentWindow, "contact")
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 155
diff changeset
  1230
            dialog.SetPreviewFont(self.GetFont())
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1231
            varlist = []
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
  1232
            vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1233
            if vars:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1234
                for var in vars:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1235
                    if var["Class"] != "Output" and var["Type"] == "BOOL":
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1236
                        varlist.append(var["Name"])
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1237
            dialog.SetVariables(varlist)
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1238
            dialog.SetValues({"name":contact.GetName(),"type":contact.GetType()})
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
  1239
            if dialog.ShowModal() == wx.ID_OK:
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1240
                values = dialog.GetValues()
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1241
                contact.SetName(values["name"])
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1242
                contact.SetType(values["type"])
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1243
                contact.RefreshModel(False)
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
  1244
                self.RefreshBuffer()
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
  1245
                self.RefreshScrollBars()
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 122
diff changeset
  1246
                contact.Refresh()
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1247
            dialog.Destroy()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1248
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
  1249
    def EditCoilContent(self, coil):
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1250
        if self.GetDrawingMode() == FREEDRAWING_MODE:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1251
            Viewer.EditCoilContent(self, coil)
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1252
        else:
90
2245e8776086 Adding support support for using PLCOpenEditor with Beremiz
lbessard
parents: 80
diff changeset
  1253
            dialog = LDElementDialog(self.ParentWindow, "coil")
165
e464a4e4e06d Adding Font support in Dialog
lbessard
parents: 155
diff changeset
  1254
            dialog.SetPreviewFont(self.GetFont())
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1255
            varlist = []
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
  1256
            vars = self.Controler.GetEditedElementInterfaceVars(self.TagName, self.Debug)
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1257
            if vars:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1258
                for var in vars:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1259
                    if var["Class"] != "Input" and var["Type"] == "BOOL":
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1260
                        varlist.append(var["Name"])
249
d8425712acef Adding support for Debugging in PLCOpenEditor
lbessard
parents: 235
diff changeset
  1261
            returntype = self.Controler.GetEditedElementInterfaceReturnType(self.TagName, self.Debug)
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1262
            if returntype == "BOOL":
121
40b91ba978db Improving PLCOpenEditor for using wx2.8 AUI
lbessard
parents: 112
diff changeset
  1263
                varlist.append(self.Controler.GetEditedElementName(self.TagName))
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1264
            dialog.SetVariables(varlist)
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1265
            dialog.SetValues({"name":coil.GetName(),"type":coil.GetType()})
64
dd6f693e46a1 Cleaning code for using only wxPython 2.6 class naming
lbessard
parents: 58
diff changeset
  1266
            if dialog.ShowModal() == wx.ID_OK:
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1267
                values = dialog.GetValues()
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1268
                coil.SetName(values["name"])
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1269
                coil.SetType(values["type"])
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1270
                coil.RefreshModel(False)
56
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
  1271
                self.RefreshBuffer()
7187e1c00975 Adding support for Undo/Redo and Unsaved File On Close detection
lbessard
parents: 50
diff changeset
  1272
                self.RefreshScrollBars()
144
b67a5de5a24a Adding optimization on Viewer redrawing
lbessard
parents: 122
diff changeset
  1273
                coil.Refresh()
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1274
            dialog.Destroy()
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1275
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1276
    def EditPowerRailContent(self, powerrail):
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1277
        if self.GetDrawingMode() == FREEDRAWING_MODE:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1278
            Viewer.EditPowerRailContent(self, powerrail)
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
  1279
122
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1280
#-------------------------------------------------------------------------------
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1281
#                          Model update functions
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1282
#-------------------------------------------------------------------------------
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1283
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1284
    def RefreshBlockModel(self, block):
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1285
        blockid = block.GetId()
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1286
        infos = {}
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1287
        infos["type"] = block.GetType()
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1288
        infos["name"] = block.GetName()
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1289
        infos["x"], infos["y"] = block.GetPosition()
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1290
        infos["width"], infos["height"] = block.GetSize()
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1291
        infos["connectors"] = block.GetConnectors()
e6faee0c271b Adding support for Project Tree selected item following current item edited.
lbessard
parents: 121
diff changeset
  1292
        self.Controler.SetEditedElementBlockInfos(self.TagName, blockid, infos)