Simulation thread control developed.

This commit is contained in:
Jose Luis Cercós pita 2012-07-08 14:23:06 +02:00
parent a455cf6fa8
commit 9c4b3cc618
12 changed files with 3336 additions and 1184 deletions

View File

@ -43,6 +43,8 @@ SET(ShipIcons_SRCS
Icons/SimCreateIco.xpm
Icons/SimRunIco.png
Icons/SimRunIco.xpm
Icons/SimStopIco.png
Icons/SimStopIco.xpm
Icons/Tank.png
Icons/Tank.xcf
Icons/Tank.xpm

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 14 KiB

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

File diff suppressed because it is too large Load Diff

View File

@ -42,7 +42,7 @@ class ShipWorkbench ( Workbench ):
msg = Translator.translate("pyOpenCL not installed, ship simulations disabled\n")
App.Console.PrintWarning(msg)
else:
list = ["Ship_CreateSim", "Ship_RunSim"]
list = ["Ship_CreateSim", "Ship_RunSim", "Ship_StopSim"]
self.appendToolbar("Simulation",list)
# Menu
@ -55,7 +55,7 @@ class ShipWorkbench ( Workbench ):
except ImportError:
pass
else:
list = ["Ship_CreateSim", "Ship_RunSim"]
list = ["Ship_CreateSim", "Ship_RunSim", "Ship_StopSim"]
self.appendToolbar("Simulation",list)
Gui.addWorkbench(ShipWorkbench())

View File

@ -44,6 +44,8 @@ nobase_data_DATA = \
Icons/SimCreateIco.xpm \
Icons/SimRunIco.png \
Icons/SimRunIco.xpm \
Icons/SimStopIco.png \
Icons/SimStopIco.xpm \
Icons/Tank.png \
Icons/Tank.xcf \
Icons/Tank.xpm \

View File

@ -144,6 +144,18 @@ class RunSim:
ToolTip = str(Translator.translate('Run a simulation'))
return {'Pixmap' : IconPath, 'MenuText': MenuText, 'ToolTip': ToolTip}
class StopSim:
def Activated(self):
import simRun
simRun.stop()
def GetResources(self):
from shipUtils import Paths, Translator
IconPath = Paths.iconsPath() + "/SimStopIco.png"
MenuText = str(Translator.translate('Stop active simulation'))
ToolTip = str(Translator.translate('Stop active simulation'))
return {'Pixmap' : IconPath, 'MenuText': MenuText, 'ToolTip': ToolTip}
FreeCADGui.addCommand('Ship_LoadExample', LoadExample())
FreeCADGui.addCommand('Ship_CreateShip', CreateShip())
FreeCADGui.addCommand('Ship_OutlineDraw', OutlineDraw())
@ -154,3 +166,4 @@ FreeCADGui.addCommand('Ship_CreateTank', CreateTank())
FreeCADGui.addCommand('Ship_GZ', GZ())
FreeCADGui.addCommand('Ship_CreateSim', CreateSim())
FreeCADGui.addCommand('Ship_RunSim', RunSim())
FreeCADGui.addCommand('Ship_StopSim', StopSim())

View File

@ -35,7 +35,18 @@ from FreeCAD import Part, Base, Vector
# Ship design module
from shipUtils import Paths, Translator, Math
class Singleton(type):
def __init__(cls, name, bases, dct):
cls.__instance = None
type.__init__(cls, name, bases, dct)
def __call__(cls, *args, **kw):
if cls.__instance is None:
cls.__instance = type.__call__(cls, *args,**kw)
return cls.__instance
class FreeCADShipSimulation(threading.Thread):
__metaclass__ = Singleton
def __init__ (self, device, endTime, output, FSmesh, waves):
""" Thread constructor.
@param device Device to use.
@ -45,6 +56,8 @@ class FreeCADShipSimulation(threading.Thread):
@param waves Waves parameters (A,T,phi,heading)
"""
threading.Thread.__init__(self)
# Setup as stopped
self.active = False
# Build OpenCL context and command queue
self.device = device
self.context = cl.Context(devices=[self.device])
@ -58,8 +71,25 @@ class FreeCADShipSimulation(threading.Thread):
def run(self):
""" Runs the simulation.
"""
self.active = True
# Perform work here
print("Im thread, Im running...")
time.sleep(2)
# ...
print("Im thread, I end!")
while self.active:
print("Im thread, Im running...")
time.sleep(1)
# ...
print("Im thread, step done!")
# Set thread as stopped (and prepare it to restarting)
self.active = False
threading.Event().set()
threading.Thread.__init__(self)
def stop(self):
""" Call to stop execution.
"""
self.active = False
def isRunning(self):
""" Report thread state
@return True if thread is running, False otherwise.
"""
return self.active

View File

@ -33,6 +33,8 @@ import SimInstance
from shipUtils import Paths, Translator
from Simulation import FreeCADShipSimulation as Sim
import time
class TaskPanel:
def __init__(self):
self.ui = Paths.modulePath() + "/simRun/TaskPanel.ui"
@ -184,3 +186,18 @@ def createTask():
Gui.Control.closeDialog(panel)
return None
return panel
def stopSimulation():
try:
simulator = Sim()
if not simulator.isRunning():
msg = Translator.translate("Simulation already stopped\n")
App.Console.PrintWarning(msg)
return
except:
msg = Translator.translate("Any active simulation to stop!\n")
App.Console.PrintError(msg)
return
simulator.stop()
msg = Translator.translate("Simulation will stop at the end of actual iteration\n")
App.Console.PrintMessage(msg)

View File

@ -34,3 +34,7 @@ import TaskPanel
def load():
""" Loads the tool """
TaskPanel.createTask()
def stop():
""" Stops the simulation """
TaskPanel.stopSimulation()