graphics/LD_Objects.py
author lbessard
Wed, 18 Jul 2007 16:22:39 +0200
changeset 44 c6e153273ea1
parent 42 4a8400732001
child 50 4610aafc884e
permissions -rw-r--r--
Bugs on SFCViewer fixed
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
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     5
#based on the plcopen standard. 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     6
#
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
     7
#Copyright (C): Edouard TISSERANT and Laurent BESSARD
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: 0
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
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    19
#Lesser General Public License for more details.
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    20
#
5
f8652b073e84 GPL->LGPL
etisserant
parents: 0
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
from wxPython.wx import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    26
import wx
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 GraphicCommons import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    29
from plcopen.structures import *
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    30
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    31
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    32
#                         Ladder Diagram PowerRail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    33
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    34
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    35
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    36
Class that implements the graphic representation of a power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    37
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    38
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    39
class LD_PowerRail(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    40
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    41
    # Create a new power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    42
    def __init__(self, parent, type, id = None, connectors = [True]):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    43
        Graphic_Element.__init__(self, parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    44
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    45
        self.Id = id
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    46
        self.Extensions = [LD_LINE_SIZE / 2, LD_LINE_SIZE / 2]
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    47
        if len(connectors) < 1:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    48
            connectors = [True]
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    49
        # Create a connector or a blank according to 'connectors' and add it in
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    50
        # the connectors list
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    51
        self.Connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    52
        for connector in connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    53
            self.AddConnector(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    54
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    55
        
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    56
    # Destructor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    57
    def __del__(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    58
        self.Connectors = []
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    59
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    60
    # Forbids to change the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    61
    def SetSize(self, width, height):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    62
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    63
            Graphic_Element.SetSize(self, width, height)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    64
            self.RefreshConnectors()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    65
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    66
    # Forbids to select a power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    67
    def HitTest(self, pt):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    68
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    69
            return Graphic_Element.HitTest(self, pt) or self.TestConnector(pt, False) != None
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    70
        return False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    71
    
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    72
    # Forbids to select a power rail
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    73
    def IsInSelection(self, rect):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    74
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    75
            return Graphic_Element.IsInSelection(rect)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    76
        return False
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
    77
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    78
    # Deletes this power rail by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    79
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    80
        self.Parent.DeletePowerRail(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    81
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    82
    # Unconnect all connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    83
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    84
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    85
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    86
                connector.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    87
                
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    88
    # Refresh the power rail bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    89
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    90
        dc = wxClientDC(self.Parent)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    91
        self.BoundingBox = wxRect(self.Pos.x, self.Pos.y, self.Size[0], self.Size[1] + 1)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    92
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    93
    # Refresh the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    94
    def RefreshSize(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    95
        self.Size = wxSize(2, LD_LINE_SIZE * len(self.Connectors))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    96
        self.RefreshBoundingBox()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
    97
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    98
    # Returns the block minimum size
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
    99
    def GetMinSize(self):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   100
        return 2, LD_LINE_SIZE * len(self.Connectors)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   101
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   102
    # Add a connector or a blank to this power rail at the last place
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   103
    def AddConnector(self, connector = True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   104
        self.InsertConnector(len(self.Connectors), connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   105
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   106
    # Add a connector or a blank to this power rail at the place given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   107
    def InsertConnector(self, idx, connector = True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   108
        if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   109
            if self.Type == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   110
                connector = Connector(self, "", "BOOL", wxPoint(2, 0), EAST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   111
            elif self.Type == RIGHTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   112
                connector = Connector(self, "", "BOOL", wxPoint(0, 0), WEST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   113
            self.Connectors.insert(idx, connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   114
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   115
            self.Connectors.insert(idx, None)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   116
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   117
        self.RefreshConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   118
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   119
    # Moves the divergence connector given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   120
    def MoveConnector(self, connector, movey):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   121
        position = connector.GetRelPosition()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   122
        connector.SetPosition(wxPoint(position.x, position.y + movey))
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   123
        miny = self.Size[1]
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   124
        maxy = 0
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   125
        for connect in self.Connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   126
            connect_pos = connect.GetRelPosition()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   127
            miny = min(miny, connect_pos.y)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   128
            maxy = max(maxy, connect_pos.y)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   129
        min_pos = self.Pos.y + miny - self.Extensions[0]
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   130
        self.Pos.y = min(min_pos, self.Pos.y)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   131
        if min_pos == self.Pos.y:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   132
            for connect in self.Connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   133
                connect_pos = connect.GetRelPosition()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   134
                connect.SetPosition(wxPoint(connect_pos.x, connect_pos.y - miny + self.Extensions[0]))
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   135
        self.Size[1] = max(maxy - miny + self.Extensions[0] + self.Extensions[1], self.Size[1])
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   136
        connector.MoveConnected()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   137
        self.RefreshBoundingBox()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   138
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   139
    # Returns the index in connectors list for the connector given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   140
    def GetConnectorIndex(self, connector):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   141
        if connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   142
            return self.Connectors.index(connector)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   143
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   144
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   145
    # Returns if there is a connector in connectors list at the index given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   146
    def IsNullConnector(self, idx):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   147
        if idx < len(self.Connectors):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   148
            return self.Connectors[idx] == None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   149
        return False
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   150
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   151
    # Delete the connector or blank from connectors list at the index given
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   152
    def DeleteConnector(self, idx):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   153
        self.Connectors.pop(idx)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   154
        self.RefreshConnectors()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   155
        self.RefreshSize()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   156
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   157
    # Refresh the positions of the power rail connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   158
    def RefreshConnectors(self):
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   159
        position = self.Extensions[0]
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   160
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   161
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   162
                if self.Type == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   163
                    connector.SetPosition(wxPoint(self.Size[0], position))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   164
                elif self.Type == RIGHTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   165
                    connector.SetPosition(wxPoint(0, position))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   166
            position += LD_LINE_SIZE
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   167
        self.RefreshConnected()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   168
    
7
f1691e685c49 Adding error messages on LD editor
lbessard
parents: 5
diff changeset
   169
    # Refresh the position of wires connected to power rail
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   170
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   171
        for connector in self.Connectors:
8
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   172
            if connector:
7ceec5c40d77 Adding auto-completion into ST and IL Editors
lbessard
parents: 7
diff changeset
   173
                connector.MoveConnected(exclude)
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
    # Returns the power rail connector that starts with the point given if it exists 
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   176
    def GetConnector(self, position, name = None):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   177
        # if a name is given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   178
        if name:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   179
            # Test each connector if it exists
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   180
            for connector in self.Connectors:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   181
                if connector and name == connector.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   182
                    return connector
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   183
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   184
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   185
                connector_pos = connector.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   186
                if position.x == self.Pos.x + connector_pos.x and position.y == self.Pos.y + connector_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   187
                    return connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   188
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   189
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   190
    # Returns all the power rail connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   191
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   192
        return [connector for connector in self.Connectors if connector]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   193
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   194
    # Test if point given is on one of the power rail connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   195
    def TestConnector(self, pt, exclude=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   196
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   197
            if connector and connector.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   198
                return connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   199
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   200
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   201
    # Returns the power rail type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   202
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   203
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   204
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   205
    # Method called when a LeftDown event have been generated
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   206
    def OnLeftDown(self, event, dc, scaling):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   207
        pos = GetScaledEventPosition(event, dc, scaling)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   208
        # Test if a connector have been handled
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   209
        connector = self.TestConnector(pos, False)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   210
        if connector:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   211
            self.Handle = (HANDLE_CONNECTOR, connector)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   212
            self.Parent.SetCursor(wxStockCursor(wxCURSOR_HAND))
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   213
            self.Selected = False
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   214
            # Initializes the last position
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   215
            self.oldPos = GetScaledEventPosition(event, dc, scaling)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   216
        else:
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   217
##            self.RealConnectors = {"Inputs":[],"Outputs":[]}
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   218
##            for input in self.Inputs:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   219
##                position = input.GetRelPosition()
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   220
##                self.RealConnectors["Inputs"].append(float(position.x)/float(self.Size[0]))
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   221
##            for output in self.Outputs:
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   222
##                position = output.GetRelPosition()
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   223
##                self.RealConnectors["Outputs"].append(float(position.x)/float(self.Size[0]))
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   224
            Graphic_Element.OnLeftDown(self, event, dc, scaling)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   225
    
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   226
    # Method called when a LeftUp event have been generated
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   227
    def OnLeftUp(self, event, dc, scaling):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   228
        self.RealConnectors = None
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   229
        handle_type, handle = self.Handle
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   230
        if handle_type == HANDLE_CONNECTOR:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   231
            wires = handle.GetWires()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   232
            if len(wires) != 1:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   233
                return
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   234
            if handle == wires[0][0].StartConnected:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   235
                block = wires[0][0].EndConnected.GetParentBlock()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   236
            else:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   237
                block = wires[0][0].StartConnected.GetParentBlock()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   238
            block.RefreshModel(False)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   239
        Graphic_Element.OnLeftUp(self, event, dc, scaling)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   240
    
28
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   241
    # Method called when a LeftDClick event have been generated
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   242
    def OnLeftDClick(self, event, dc, scaling):
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   243
        # Edit the powerrail properties
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   244
        self.Parent.EditPowerRailContent(self)
fc23e1f415d8 Adding support for concurrent overriden standard function
lbessard
parents: 27
diff changeset
   245
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   246
    # Method called when a RightUp event have been generated
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   247
    def OnRightUp(self, event, dc, scaling):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   248
        pos = GetScaledEventPosition(event, dc, scaling)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   249
        # Popup the menu with special items for a block and a connector if one is handled
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   250
        connector = self.TestConnector(pos, False)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   251
        if connector:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   252
            self.Handle = (HANDLE_CONNECTOR, connector)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   253
        #    self.Parent.PopupDivergenceMenu(True)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   254
        #else:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   255
            # Popup the divergence menu without delete branch
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   256
        #    self.Parent.PopupDivergenceMenu(False)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   257
    
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   258
    # Refreshes the divergence state according to move defined and handle selected
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   259
    def ProcessDragging(self, movex, movey):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   260
        handle_type, handle = self.Handle
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   261
        # A connector has been handled
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   262
        if handle_type == HANDLE_CONNECTOR:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   263
            self.MoveConnector(handle, movey)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   264
        else:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   265
            Graphic_Element.ProcessDragging(self, movex, movey)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   266
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   267
    # Refreshes the power rail model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   268
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   269
        self.Parent.RefreshPowerRailModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   270
        # If power rail has moved and power rail is of type LEFT, refresh the model 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   271
        # of wires connected to connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   272
        if move and self.Type == LEFTRAIL:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   273
            for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   274
                if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   275
                    connector.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   276
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   277
    # Draws power rail
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   278
    def Draw(self, dc):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   279
        dc.SetPen(wxBLACK_PEN)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   280
        dc.SetBrush(wxBLACK_BRUSH)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   281
        # Draw a rectangle with the power rail size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   282
        dc.DrawRectangle(self.Pos.x, self.Pos.y, self.Size[0] + 1, self.Size[1] + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   283
        # Draw connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   284
        for connector in self.Connectors:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   285
            if connector:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   286
                connector.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   287
        Graphic_Element.Draw(self, dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   288
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
#                         Ladder Diagram Contact
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
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   295
Class that implements the graphic representation of a contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   296
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   297
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   298
class LD_Contact(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   299
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   300
    # Create a new contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   301
    def __init__(self, parent, type, name, id = None):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   302
        Graphic_Element.__init__(self, parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   303
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   304
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   305
        self.Id = id
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   306
        self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   307
        # Create an input and output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   308
        self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   309
        self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   310
        self.RefreshNameSize()
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   311
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   312
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   313
    # Destructor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   314
    def __del__(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   315
        self.Input = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   316
        self.Output = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   317
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   318
    # Forbids to change the contact size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   319
    def SetSize(self, width, height):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   320
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   321
            Graphic_Element.SetSize(self, width, height)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   322
            self.RefreshConnectors()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   323
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   324
    # Delete this contact by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   325
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   326
        self.Parent.DeleteContact(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   327
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   328
    # Unconnect input and output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   329
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   330
        self.Input.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   331
        self.Output.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   332
    
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   333
    # Refresh the size of text for name
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   334
    def RefreshNameSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   335
        dc = wxClientDC(self.Parent)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   336
        if self.Name != "":
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   337
            self.NameSize = dc.GetTextExtent(self.Name)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   338
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   339
            self.NameSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   340
    
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   341
    # Refresh the size of text for type
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   342
    def RefreshTypeSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   343
        dc = wxClientDC(self.Parent)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   344
        typetext = ""
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   345
        if self.Type == CONTACT_REVERSE:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   346
            typetext = "/"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   347
        elif self.Type == CONTACT_RISING:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   348
            typetext = "P"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   349
        elif self.Type == CONTACT_FALLING:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   350
            typetext = "N"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   351
        if typetext != "":
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   352
            self.TypeSize = dc.GetTextExtent(typetext)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   353
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   354
            self.TypeSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   355
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   356
    # Refresh the contact bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   357
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   358
        dc = wxClientDC(self.Parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   359
        # Calculate the size of the name outside the contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   360
        text_width, text_height = dc.GetTextExtent(self.Name)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   361
        # Calculate the bounding box size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   362
        if self.Name != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   363
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   364
            bbx_width = max(self.Size[0], text_width)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   365
            bbx_y = self.Pos.y - (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   366
            bbx_height = self.Size[1] + (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   367
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   368
            bbx_x = self.Pos.x
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   369
            bbx_width = self.Size[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   370
            bbx_y = self.Pos.y
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   371
            bbx_height = self.Size[1]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   372
        self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   373
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   374
    # Returns the block minimum size
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   375
    def GetMinSize(self):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   376
        return LD_ELEMENT_SIZE
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   377
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   378
    # Refresh the position of wire connected to contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   379
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   380
        self.Input.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   381
        self.Output.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   382
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   383
    # Returns the contact connector that starts with the point given if it exists 
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   384
    def GetConnector(self, position, name = None):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   385
        # if a name is given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   386
        if name:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   387
            # Test input and output connector
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   388
            if name == self.Input.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   389
                return self.Input
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   390
            if name == self.Output.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   391
                return self.Output
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   392
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   393
        input_pos = self.Input.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   394
        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   395
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   396
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   397
        output_pos = self.Output.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   398
        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   399
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   400
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   401
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   402
    # Returns input and output contact connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   403
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   404
        return {"input":self.Input,"output":self.Output}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   405
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   406
    # Test if point given is on contact input or output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   407
    def TestConnector(self, pt, exclude=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   408
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   409
        if self.Input.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   410
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   411
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   412
        if self.Output.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   413
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   414
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   415
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   416
    # Refresh the positions of the block connectors
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   417
    def RefreshConnectors(self):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   418
        self.Input.SetPosition(wxPoint(0, self.Size[1] / 2 + 1))
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   419
        self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2 + 1))
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   420
        self.RefreshConnected()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   421
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   422
    # Changes the contact name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   423
    def SetName(self, name):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   424
        self.Name = name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   425
        self.RefreshNameSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   426
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   427
    # Returns the contact name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   428
    def GetName(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   429
        return self.Name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   430
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   431
    # Changes the contact type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   432
    def SetType(self, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   433
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   434
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   435
    # Returns the contact type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   436
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   437
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   438
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   439
    # Method called when a LeftDClick event have been generated
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   440
    def OnLeftDClick(self, event, dc, scaling):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   441
        # Edit the contact properties
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   442
        self.Parent.EditContactContent(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   443
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   444
    # Refreshes the contact model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   445
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   446
        self.Parent.RefreshContactModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   447
        # If contact has moved, refresh the model of wires connected to output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   448
        if move:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   449
            self.Output.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   450
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   451
    # Draws contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   452
    def Draw(self, dc):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   453
        dc.SetPen(wxBLACK_PEN)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   454
        dc.SetBrush(wxBLACK_BRUSH)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   455
        # Draw two rectangles for representing the contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   456
        dc.DrawRectangle(self.Pos.x, self.Pos.y, 2, self.Size[1] + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   457
        dc.DrawRectangle(self.Pos.x + self.Size[0] - 1, self.Pos.y, 2, self.Size[1] + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   458
        # Draw contact name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   459
        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   460
                self.Pos.y - (self.NameSize[1] + 2))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   461
        # Draw the modifier symbol in the middle of contact
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   462
        typetext = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   463
        if self.Type == CONTACT_REVERSE:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   464
            typetext = "/"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   465
        elif self.Type == CONTACT_RISING:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   466
            typetext = "P"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   467
        elif self.Type == CONTACT_FALLING:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   468
            typetext = "N"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   469
        if typetext != "":
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   470
            dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1,
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   471
                    self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   472
        # Draw input and output connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   473
        self.Input.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   474
        self.Output.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   475
        Graphic_Element.Draw(self, dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   476
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   477
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   478
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   479
#                         Ladder Diagram Coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   480
#-------------------------------------------------------------------------------
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   481
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   482
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   483
Class that implements the graphic representation of a coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   484
"""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   485
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   486
class LD_Coil(Graphic_Element):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   487
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   488
    # Create a new coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   489
    def __init__(self, parent, type, name, id = None):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   490
        Graphic_Element.__init__(self, parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   491
        self.Type = type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   492
        self.Name = name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   493
        self.Id = id
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   494
        self.Size = wxSize(LD_ELEMENT_SIZE[0], LD_ELEMENT_SIZE[1])
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   495
        # Create an input and output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   496
        self.Input = Connector(self, "", "BOOL", wxPoint(0, self.Size[1] / 2 + 1), WEST)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   497
        self.Output = Connector(self, "", "BOOL", wxPoint(self.Size[0], self.Size[1] / 2 + 1), EAST)
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   498
        self.RefreshNameSize()
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   499
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   500
        
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   501
    # Destructor
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   502
    def __del__(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   503
        self.Input = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   504
        self.Output = None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   505
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   506
    # Forbids to change the contact size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   507
    def SetSize(self, width, height):
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   508
        if self.Parent.GetDrawingMode() == FREEDRAWING_MODE:
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   509
            Graphic_Element.SetSize(self, width, height)
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   510
            self.RefreshConnectors()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   511
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   512
    # Delete this coil by calling the appropriate method
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   513
    def Delete(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   514
        self.Parent.DeleteCoil(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   515
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   516
    # Unconnect input and output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   517
    def Clean(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   518
        self.Input.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   519
        self.Output.UnConnect()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   520
                
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   521
    # Refresh the size of text for name
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   522
    def RefreshNameSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   523
        dc = wxClientDC(self.Parent)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   524
        if self.Name != "":
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   525
            self.NameSize = dc.GetTextExtent(self.Name)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   526
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   527
            self.NameSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   528
    
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   529
    # Refresh the size of text for type
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   530
    def RefreshTypeSize(self):
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   531
        dc = wxClientDC(self.Parent)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   532
        typetext = ""
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   533
        if self.Type == COIL_REVERSE:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   534
            typetext = "/"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   535
        elif self.Type == COIL_SET:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   536
            typetext = "S"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   537
        elif self.Type == COIL_RESET:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   538
            typetext = "R"
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   539
        if typetext != "":
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   540
            self.TypeSize = dc.GetTextExtent(typetext)
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   541
        else:
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   542
            self.TypeSize = 0, 0
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   543
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   544
    # Refresh the coil bounding box
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   545
    def RefreshBoundingBox(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   546
        dc = wxClientDC(self.Parent)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   547
        # Calculate the size of the name outside the coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   548
        text_width, text_height = dc.GetTextExtent(self.Name)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   549
        # Calculate the bounding box size
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   550
        if self.Name != "":
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   551
            bbx_x = self.Pos.x - max(0, (text_width - self.Size[0]) / 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   552
            bbx_width = max(self.Size[0], text_width)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   553
            bbx_y = self.Pos.y - (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   554
            bbx_height = self.Size[1] + (text_height + 2)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   555
        else:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   556
            bbx_x = self.Pos.x
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   557
            bbx_width = self.Size[0]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   558
            bbx_y = self.Pos.y
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   559
            bbx_height = self.Size[1]
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   560
        self.BoundingBox = wxRect(bbx_x, bbx_y, bbx_width + 1, bbx_height + 1)
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   561
        
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   562
    # Returns the block minimum size
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   563
    def GetMinSize(self):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   564
        return LD_ELEMENT_SIZE
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   565
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   566
    # Refresh the position of wire connected to coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   567
    def RefreshConnected(self, exclude = []):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   568
        self.Input.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   569
        self.Output.MoveConnected(exclude)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   570
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   571
    # Returns the coil connector that starts with the point given if it exists 
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   572
    def GetConnector(self, position, name = None):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   573
        # if a name is given
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   574
        if name:
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   575
            # Test input and output connector
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   576
            if self.Input and name == self.Input.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   577
                return self.Input
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   578
            if self.Output and name == self.Output.GetName():
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   579
                return self.Output
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   580
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   581
        input_pos = self.Input.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   582
        if position.x == self.Pos.x + input_pos.x and position.y == self.Pos.y + input_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   583
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   584
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   585
        output_pos = self.Output.GetRelPosition()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   586
        if position.x == self.Pos.x + output_pos.x and position.y == self.Pos.y + output_pos.y:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   587
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   588
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   589
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   590
    # Returns input and output coil connectors 
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   591
    def GetConnectors(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   592
        return {"input":self.Input,"output":self.Output}
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   593
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   594
    # Test if point given is on coil input or output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   595
    def TestConnector(self, pt, exclude=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   596
        # Test input connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   597
        if self.Input.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   598
            return self.Input
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   599
        # Test output connector
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   600
        if self.Output.TestPoint(pt, exclude):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   601
            return self.Output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   602
        return None
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   603
    
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   604
    # Refresh the positions of the block connectors
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   605
    def RefreshConnectors(self):
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   606
        self.Input.SetPosition(wxPoint(0, self.Size[1] / 2 + 1))
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   607
        self.Output.SetPosition(wxPoint(self.Size[0], self.Size[1] / 2 + 1))
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   608
        self.RefreshConnected()
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   609
    
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   610
    # Changes the coil name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   611
    def SetName(self, name):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   612
        self.Name = name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   613
        self.RefreshNameSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   614
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   615
    # Returns the coil name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   616
    def GetName(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   617
        return self.Name
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   618
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   619
    # Changes the coil type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   620
    def SetType(self, type):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   621
        self.Type = type
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   622
        self.RefreshTypeSize()
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   623
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   624
    # Returns the coil type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   625
    def GetType(self):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   626
        return self.Type
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   627
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   628
    # Method called when a LeftDClick event have been generated
27
dae55dd9ee14 Current developping version
lbessard
parents: 8
diff changeset
   629
    def OnLeftDClick(self, event, dc, scaling):
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   630
        # Edit the coil properties
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   631
        self.Parent.EditCoilContent(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   632
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   633
    # Refreshes the coil model
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   634
    def RefreshModel(self, move=True):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   635
        self.Parent.RefreshCoilModel(self)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   636
        # If coil has moved, refresh the model of wires connected to output
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   637
        if move:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   638
            self.Output.RefreshWires()
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   639
    
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   640
    # Draws coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   641
    def Draw(self, dc):
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   642
        dc.SetPen(wxPen(wxBLACK, 2, wxSOLID))
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   643
        dc.SetBrush(wxTRANSPARENT_BRUSH)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   644
        # Draw a two circle arcs for representing the coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   645
        dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, 135, 225)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   646
        dc.DrawEllipticArc(self.Pos.x, self.Pos.y - int(self.Size[1] * (sqrt(2) - 1.) / 2.) + 1, self.Size[0], int(self.Size[1] * sqrt(2)) - 1, -45, 45)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   647
        dc.SetPen(wxBLACK_PEN)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   648
        dc.DrawPoint(self.Pos.x + 1, self.Pos.y + self.Size[1] / 2 + 1)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   649
        # Draw coil name
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   650
        dc.DrawText(self.Name, self.Pos.x + (self.Size[0] - self.NameSize[0]) / 2,
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   651
                self.Pos.y - (self.NameSize[1] + 2))
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   652
        # Draw the modifier symbol in the middle of coil
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   653
        typetext = ""
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   654
        if self.Type == COIL_REVERSE:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   655
            typetext = "/"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   656
        elif self.Type == COIL_SET:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   657
            typetext = "S"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   658
        elif self.Type == COIL_RESET:
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   659
            typetext = "R"
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   660
        if typetext != "":
42
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   661
            dc.DrawText(typetext, self.Pos.x + (self.Size[0] - self.TypeSize[0]) / 2 + 1,
4a8400732001 Adding optimization on redrawing
lbessard
parents: 28
diff changeset
   662
                    self.Pos.y + (self.Size[1] - self.TypeSize[1]) / 2)
0
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   663
        # Draw input and output connectors
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   664
        self.Input.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   665
        self.Output.Draw(dc)
b622defdfd98 PLCOpenEditor initial commit. 31/01/07.
etisserant
parents:
diff changeset
   666
        Graphic_Element.Draw(self, dc)