summaryrefslogtreecommitdiff
path: root/cad/src/model/ReferenceGeometry.py
blob: c2b06988327505480226ba3a1ac072a3ce407878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# Copyright 2007-2008 Nanorex, Inc.  See LICENSE file for details.
"""
ReferenceGeometry.py - Jig subclass used as superclass for Plane, Line, etc

@author: Ninad
@version: $Id$
@copyright: 2007-2008 Nanorex, Inc.  See LICENSE file for details.

History:
ninad 20070521 : Created

ninad 20070601: Implemented DragHandler and selobj interface for the
class Handles. (and for ReferenceGeoemtry which is a superclass of Plane)

ninad 20070604: Changed the superclass from Node to Jig based on a discussion
with Bruce. This allows us to use code for jig selection deletion in some select
modes. (note that it continues to use drag handler, selobj interfaces.).
This might CHANGE in future.  After making jig a superclass of ReferenceGeometry
some methods in this file have become overdefined. This needs cleanup
"""

#@NOTE:This file is subjected to major changes.

#@TODO: Ninad20070604 :
#- Needs documentation and code cleanup / organization post Alpha-9.
#-After making jig a superclass of ReferenceGeometry
#some methods in this file have become overdefined. This needs cleanup.

import foundation.env as env
from utilities import debug_flags

from utilities.debug import print_compact_traceback

from utilities.prefs_constants import selectionColor_prefs_key
from utilities.prefs_constants import hoverHighlightingColor_prefs_key
from utilities.constants import orange, yellow

from OpenGL.GL import glPushName
from OpenGL.GL import glPopName

from foundation.Utility import Node
from model.jigs import Jig

from graphics.drawables.DragHandler import DragHandler_API
from utilities.Log import greenmsg

class ReferenceGeometry(Jig, DragHandler_API):
    """
    Superclass for various reference geometries.
    Example or reference geometries: Plane, Point, Line.
    """

    sym             =  "Geometry" # affects name-making code in __init__
    # if not redefined, this means it's just a comment in an mmp file
    mmp_record_name = "#"
    featurename     =  ""
    color           =  orange
    normcolor       =  color
    atoms           =  []
    points          =  None
    handles         =  None

    copyable_attrs = Node.copyable_attrs + ('normcolor', 'color')

    def __init__(self, win):
        self.win = win
        #Node.__init__(self, win.assy, gensym("%s" % self.sym, win.assy))
        Jig.__init__(self, win.assy, self.atoms) # note: that sets self.glname
        self.glpane = self.assy.o
        #@@Geometry object with a visible direction arrow
        #(at present used in Plane only) This saves the last geometry object
        #for which the Direction arrow was drawn
        #(that decides the direction of a plane offset to the object)
        #The program needs to skip the arrow drawing (which is done inside the
        # objects _draw_geometry method) when prop manager is closed or when the
        #Offset option is no more requested.  -- ninad 20070612
        self.offsetParentGeometry = None

    def needs_atoms_to_survive(self):
        """
        Overrided method inherited from Jig. This is used to tell
        if the jig can be copied even it doesn't have atoms.
        """
        return False

    def _mmp_record_last_part(self, mapping):
        """
        Return a fake string 'type-geometry' as the last entry
        for the mmp record. This part of mmp record is NOT used so far
        for ReferenceGeometry Objects.
        """
        #This is needed as ReferenceGeometry is a subclass of Jig
        #(instead of Node) , Returning string 'geometry' overcomes
        #the problem faced due to a kludge in method mmp_record
        #(see file jigs.py)That kludge makes it not write the
        #proper mmp record. if last part is an empty string (?)--ninad 20070604
        return "type-geometry"


    def _draw(self, glpane, dispdef):
        self._draw_geometry(glpane, self.color)

    def _draw_geometry(self, glpane, color, highlighted = False):
        """
        The main code that draws the geometry.
        Subclasses should override this method.
        """
        pass

    def draw(self, glpane, dispdef):
        if self.hidden:
            return
        try:
            glPushName(self.glname)
            self._draw(glpane, dispdef)
        except:
            glPopName()
            print_compact_traceback(
                "ignoring exception when drawing Plane %r: " % self)
        else:
            glPopName()

    def draw_in_abs_coords(self, glpane, color):
        """
        Draws the reference geometry with highlighting.
        """
        self._draw_geometry(glpane,
                            env.prefs[hoverHighlightingColor_prefs_key],
                            highlighted = True)
        return

    def pick(self):
        """
        Select the reference geometry.
        """
        if not self.picked:
            Node.pick(self)
            self.normcolor = self.color
            self.color = env.prefs[selectionColor_prefs_key] # russ 080603: pref.
        return

    def unpick(self):
        """
        Unselect the reference geometry.
        """
        if self.picked:
            Node.unpick(self)
            # see also a copy method which has to use the same statement to
            #compensate for this kluge
            self.color = self.normcolor

    def rot(self, quat):
        pass

    ##===============copy methods ==================###
    #Reimplementing Jig.copy_full_in_mapping. Keeping an old comment by Bruce
    #-- commented niand 20070613
    def copy_full_in_mapping(self, mapping):
        #bruce 070430 revised to honor mapping.assy
        clas = self.__class__
        new = clas(mapping.assy.w, [])
        new._orig = self
        new._mapping = mapping
        return new

    ###============== selobj interface ===============###

    #Methods for selobj interface  . Note that draw_in_abs_coords method is
    #already defined above. All of the following is NIY  -- Ninad 20070522

    def leftClick(self, point, event, mode):
        """
        Method that handle Mouse Left click  event
        """
        mode.geometryLeftDown(self, event)
        mode.update_selobj(event)
        return self

    def mouseover_statusbar_message(self):
        """
        Status bar message to display when an object is highlighted.
        @return: name of the highlighted object
        @rtype:  str
        """
        return str(self.name)

    def highlight_color_for_modkeys(self, modkeys):
        """
        Highlight color for the highlighted object
        """
        return env.prefs[hoverHighlightingColor_prefs_key]

    # copying Bruce's code from class Highlightable with some mods. Need to see
    # if sleobj_still_ok method is needed. OK for now --Ninad 20070601
    def selobj_still_ok(self, glpane):
        res = self.__class__ is ReferenceGeometry
        if res:
            our_selobj = self
            glname = self.glname
            owner = glpane.assy.object_for_glselect_name(glname)
            if owner is not our_selobj:
                res = False
                # Do debug prints [perhaps never seen as of 061121]
                print "%r no longer owns glname %r, instead %r does" \
                      % (self, glname, owner)
        if not res and env.debug():
            print "debug: selobj_still_ok is false for %r" % self
        return res

    ###=========== Drag Handler interface =============###
    def handles_updates(self): #k guessing this one might be needed
        return True

    def DraggedOn(self, event, mode):
        mode.geometryLeftDrag(self, event)
        mode.update_selobj(event)
        return

    def ReleasedOn(self, selobj, event, mode):
        pass