summaryrefslogtreecommitdiff
path: root/cad/src/operations/jigmakers_Mixin.py
blob: 1d642f52227d959f3352eecb3acc2b85c8c54530 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# Copyright 2004-2008 Nanorex, Inc.  See LICENSE file for details. 
"""
jigmakers_Mixin.py -- provides a mixin class to be inherited by class Part,
for providing operations for making specific kinds of Jigs, and associated
public helper functions.
 
@version: $Id$
@copyright: 2004-2008 Nanorex, Inc.  See LICENSE file for details.

History:

071023 bruce split this out of jigs.py, to remove some needless
or misleading import dependencies on jigs.py.
"""

import sys

from PyQt4.Qt import QMessageBox

from utilities.Log import redmsg, greenmsg, orangemsg
import foundation.env as env
from model.jigs import Anchor
from model.jigs import Stat
from model.jigs import Thermo
from model.jigs import AtomSet


class jigmakers_Mixin:
    """
    Provide Jig-making methods to class Part.
    These should be refactored into some common code
    and new methods in the specific Jig subclasses.
    """

    def makeRotaryMotor(self):
        """
        Creates a Rotary Motor edit controller, whhich in turn creates a
        rotory motor connected to the selected atoms.
        """    
        atoms = self.win.assy.selatoms_list()
        #This check fixes bug 2697. Simply don't enter the command (to create 
        #a new motor), if the there aren't enough atoms selected.
        if len(atoms) < 2:
            logMessage = "To create a rotary motor, you must select at least" \
                " two atoms. Rotary motor not created"
            env.history.message(redmsg(logMessage))
            return
        
        commandSequencer = self.assy.w.commandSequencer
        commandSequencer.userEnterCommand('ROTARY_MOTOR')
        assert commandSequencer.currentCommand.commandName == 'ROTARY_MOTOR'
        commandSequencer.currentCommand.runCommand()
      
    def makeLinearMotor(self):
        """
        Creates a Linear Motor edit controller, which in turn creates a
        linear motor connected to the selected atoms.
        """ 
        
        atoms = self.win.assy.selatoms_list()
        
        #This check fixes bug 2697. Simply don't enter the command (to create 
        #a new motor), if the there aren't enough atoms selected.
        
        if len(atoms) < 1:
            logMessage = "To create a linear motor, you must select at least" \
                " one atom. Linear motor not created"
            env.history.message(redmsg(logMessage))
            return
                    
        commandSequencer = self.assy.w.commandSequencer
        commandSequencer.userEnterCommand('LINEAR_MOTOR')
        assert commandSequencer.currentCommand.commandName == 'LINEAR_MOTOR'
        commandSequencer.currentCommand.runCommand()

    def makegamess(self):
        """
        Makes a GAMESS jig from the selected chunks or atoms.
        """
        # [mark 2007-05-07 modified docstring]
        
        if sys.platform == "win32":
            gms_str = "PC GAMESS"
        else:
            gms_str = "GAMESS"
            
        cmd = greenmsg(gms_str + ": ")
        
        atoms = []
        
        # Get a list of atoms from the selected chunks or atoms.
        atoms = self.assy.selected_atoms_list(
            include_atoms_in_selected_chunks = True)
        
        if not atoms:
            msg = "At least one atom must be selected to create a " + \
                  gms_str + " jig."
            env.history.message(cmd + redmsg(msg))
            return
        
        # Make sure that no more than 200 atoms are selected.
        nsa = len(atoms)
        if nsa > 200:
            msg = str(nsa) + " atoms selected.  The limit is 200."
            env.history.message(cmd + redmsg(msg))
            return
        
        # Bug 742.    Mark 050731.
        if nsa > 50:
            ret = QMessageBox.warning( self.assy.w, "Too many atoms?",
                gms_str + " jigs with more than 50 atoms may take an\n"
                "excessively long time to compute (days or weeks).\n"
                "Are you sure you want to continue?",
                "&Continue", "Cancel", "",
                0, 1 )
                
            if ret == 1: # Cancel
                return
                
        from analysis.GAMESS.jig_Gamess import Gamess
        m = Gamess(self.assy, atoms)
        m.edit()
            #bruce 050701 split edit method out of the constructor, so the
            # dialog doesn't show up when the jig is read from an mmp file
        if m.cancelled: # User hit 'Cancel' button in the jig dialog.
            env.history.message(cmd + "Cancelled")
            return
        self.unpickall_in_GLPane()
        self.place_new_jig(m)
        
        env.history.message(cmd + gms_str + " jig created")
        self.assy.w.win_update()
        
    def makeAnchor(self):
        """
        Anchors the selected atoms so that they will not move
        during a minimization or simulation run.
        """        
        cmd = greenmsg("Anchor: ")

        atoms = self.assy.selatoms_list()
        
        if not atoms:
            msg = "You must select at least one atom to create an Anchor."
            env.history.message(cmd + redmsg(msg))
            return
        
        # Print warning if over 200 atoms are selected.
        if atom_limit_exceeded_and_confirmed(self.assy.w,
                                             len(atoms),
                                             limit=200):
            return

        m = Anchor(self.assy, atoms)
        self.unpickall_in_GLPane()
        self.place_new_jig(m)
        
        env.history.message(cmd + "Anchor created")
        self.assy.w.win_update()

    def makestat(self):
        """
        Attaches a Langevin thermostat to the single atom selected.
        """
        cmd = greenmsg("Thermostat: ")

        atoms = self.assy.selatoms_list()
        
        if not atoms:
            msg = "You must select an atom on the chunk you want to " \
                  "associate with a Thermostat."
            env.history.message(cmd + redmsg(msg))
            return
        
        # Make sure only one atom is selected.
        if len(atoms) != 1: 
            msg = "To create a Thermostat, only one atom may be selected."
            env.history.message(cmd + redmsg(msg))
            return
        m = Stat(self.assy, atoms)
        self.unpickall_in_GLPane()
        self.place_new_jig(m)
        
        env.history.message(cmd + "Thermostat created")
        self.assy.w.win_update()
        
    def makethermo(self):
        """
        Attaches a thermometer to the single atom selected.
        """
        cmd = greenmsg("Thermometer: ")

        atoms = self.assy.selatoms_list()
        
        if not atoms:
            msg = "You must select an atom on the chunk you want to " \
                  "associate with a Thermometer."
            env.history.message(cmd + redmsg(msg))
            return
        
        # Make sure only one atom is selected.
        if len(atoms) != 1: 
            msg = "To create a Thermometer, only one atom may be selected."
            env.history.message(cmd + redmsg(msg))
            return
        
        m = Thermo(self.assy, atoms)
        self.unpickall_in_GLPane()
        self.place_new_jig(m)
        
        env.history.message(cmd + "Thermometer created")
        self.assy.w.win_update()
        
        
    def makeGridPlane(self):
        cmd = greenmsg("Grid Plane: ")

        atoms = self.assy.selatoms_list()
        
        if not atoms:
            msg = "You must select 3 or more atoms to create a Grid Plane."
            env.history.message(cmd + redmsg(msg))
            return
        
        # Make sure only one atom is selected.
        if len(atoms) < 3: 
            msg = "To create a Grid Plane, at least 3 atoms must be selected."
            env.history.message(cmd + redmsg(msg))
            return
        
        from model.jigs_planes import GridPlane
        m = GridPlane(self.assy, atoms)
        m.edit()
        if m.cancelled: # User hit 'Cancel' button in the jig dialog.
            env.history.message(cmd + "Cancelled")
            return 
        
        self.unpickall_in_GLPane()
        self.place_new_jig(m)
        
        #After placing the jig, remove the atom list from the jig.
        m.atoms = []
      
        env.history.message(cmd + "Grid Plane created")
        self.assy.w.win_update()
        return
        
    def makeESPImage(self):
        cmd = greenmsg("ESP Image: ")

        atoms = self.assy.selatoms_list()

        if len(atoms) < 3:
            msg = "You must select at least 3 atoms to create an ESP Image."
            env.history.message(cmd + redmsg(msg))
            return
        
        from analysis.ESP.ESPImage import ESPImage
        m = ESPImage(self.assy, atoms)
        m.edit()
        if m.cancelled: # User hit 'Cancel' button in the jig dialog.
            env.history.message(cmd + "Cancelled")
            return 
        
        self.unpickall_in_GLPane()
        self.place_new_jig(m)
        
        # After placing the jig, remove the atom list from the jig.
        m.atoms = []
        
        env.history.message(cmd + "ESP Image created.")
        self.assy.w.win_update()
        return
        
    def makeAtomSet(self):
        cmd = greenmsg("Atom Set: ")

        atoms = self.assy.selatoms_list()

        if not atoms:
            msg = "You must select at least one atom to create an Atom Set."
            env.history.message(cmd + redmsg(msg))
            return
            
        # Print warning if over 200 atoms are selected.
        if atom_limit_exceeded_and_confirmed(self.assy.w,
                                             len(atoms),
                                             limit = 200):
            return
        
        m = AtomSet(self.assy, atoms)
        
        self.place_new_jig(m)
        m.pick() # This is required to display the Atom Set wireframe boxes.
        
        env.history.message(cmd + "Atom Set created.")
        self.assy.w.win_update()
        return

    def makeMeasureDistance(self):
        """
        Creates a Measure Distance jig between two selected atoms.
        """
        
        cmd = greenmsg("Measure Distance Jig: ")

        atoms = self.assy.selatoms_list()

        if len(atoms) != 2:
            msg = "You must select 2 atoms to create a Distance jig."
            env.history.message(cmd + redmsg(msg))
            return
        
        from model.jigs_measurements import MeasureDistance
        d = MeasureDistance(self.assy, atoms)
        self.unpickall_in_GLPane()
        self.place_new_jig(d)
        
        env.history.message(cmd + "Measure Distance jig created")
        self.assy.w.win_update()


    def makeMeasureAngle(self):
        """
        Creates a Measure Angle jig connected to three selected atoms.
        """        
        cmd = greenmsg("Measure Angle Jig: ")

        atoms = self.assy.selatoms_list()

        if len(atoms) != 3:
            msg = "You must select 3 atoms to create an Angle jig."
            env.history.message(cmd + redmsg(msg))
            return
        
        from model.jigs_measurements import MeasureAngle
        d = MeasureAngle(self.assy, atoms)
        self.unpickall_in_GLPane()
        self.place_new_jig(d)
        
        env.history.message(cmd + "Measure Angle jig created")
        self.assy.w.win_update()

    def makeMeasureDihedral(self):
        """
        Creates a Measure Dihedral jig connected to three selected atoms.
        """
        cmd = greenmsg("Measure Dihedral Jig: ")

        atoms = self.assy.selatoms_list()

        if len(atoms) != 4:
            msg = "You must select 4 atoms to create a Dihedral jig."
            env.history.message(cmd + redmsg(msg))
            return
        
        from model.jigs_measurements import MeasureDihedral
        d = MeasureDihedral(self.assy, atoms)
        self.unpickall_in_GLPane()
        self.place_new_jig(d)
        
        env.history.message(cmd + "Measure Dihedral jig created")
        self.assy.w.win_update()
        
    pass # end of class jigmakers_Mixin

# ==

def atom_limit_exceeded_and_confirmed(parent, natoms, limit = 200):
    """
    Displays a warning message if 'natoms' exceeds 'limit'.
    Returns False if the number of atoms does not exceed the limit or if the
    user confirms that the jigs should still be created even though the limit
    was exceeded.
    If parent is 0, the message box becomes an application-global modal dialog
    box. 
    If parent is a widget, the message box becomes modal relative to parent.
    """
    if natoms < limit:
        return False # Atom limit not exceeded.

    wmsg = "Warning: Creating a jig with " + str(natoms) \
        + " atoms may degrade performance.\nDo you still want to add the jig?"
    
    dialog = QMessageBox("Warning", wmsg, 
                    QMessageBox.Warning, 
                    QMessageBox.Yes, 
                    QMessageBox.No, 
                    QMessageBox.NoButton, 
                    parent)

    # We want to add a "Do not show this message again." checkbox to the dialog
    # like this:
    #     checkbox = QCheckBox("Do not show this message again.", dialog)
    # The line of code above works, but places the checkbox in the upperleft
    # corner of the dialog, obscuring important text.  I'll fix this later.
    # Mark 051122.

    ret = dialog.exec_()

    if ret != QMessageBox.Yes:
        return True
    
    # Print warning msg in history widget whenever the user adds new jigs with
    # more than 'limit' atoms.
    wmsg = "Warning: " + str(natoms) + " atoms selected.  A jig with more " \
        "than " + str(limit) + " atoms may degrade performance."
    env.history.message(orangemsg(wmsg))
        
    return False # from atom_limit_exceeded_and_confirmed


# end