diff --git a/src/Mod/Ship/CMakeLists.txt b/src/Mod/Ship/CMakeLists.txt index f1c2c74be..ca52205e7 100644 --- a/src/Mod/Ship/CMakeLists.txt +++ b/src/Mod/Ship/CMakeLists.txt @@ -86,6 +86,7 @@ SOURCE_GROUP("shipcapacitycurve" FILES ${ShipCapacityCurve_SRCS}) SET(ShipCreateLoadCondition_SRCS shipCreateLoadCondition/__init__.py + shipCreateLoadCondition/Tools.py ) SOURCE_GROUP("shipcreateloadcondition" FILES ${ShipCreateLoadCondition_SRCS}) diff --git a/src/Mod/Ship/Ship.py b/src/Mod/Ship/Ship.py index df1af2558..840711b52 100644 --- a/src/Mod/Ship/Ship.py +++ b/src/Mod/Ship/Ship.py @@ -30,8 +30,9 @@ __doc__="The Ships module provide a set of tools to make some specific Naval" \ " Architecture computations" from shipCreateShip.Tools import createShip -from shipHydrostatics.Tools import areas, displacement, wettedArea, moment, - floatingArea, BMT, mainFrameCoeff +from shipHydrostatics.Tools import areas, displacement, wettedArea, moment +from shipHydrostatics.Tools import floatingArea, BMT, mainFrameCoeff from shipCreateWeight.Tools import createWeight from shipCreateTank.Tools import createTank -from shipCapacityCurve.Tools import tankCapacityCurve \ No newline at end of file +from shipCapacityCurve.Tools import tankCapacityCurve +from shipCreateLoadCondition.Tools import createLoadCondition \ No newline at end of file diff --git a/src/Mod/Ship/shipCreateLoadCondition/Tools.py b/src/Mod/Ship/shipCreateLoadCondition/Tools.py new file mode 100644 index 000000000..adda78806 --- /dev/null +++ b/src/Mod/Ship/shipCreateLoadCondition/Tools.py @@ -0,0 +1,124 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2016 * +#* Jose Luis Cercos Pita * +#* * +#* This program is free software; you can redistribute it and/or modify * +#* it under the terms of the GNU Lesser General Public License (LGPL) * +#* as published by the Free Software Foundation; either version 2 of * +#* the License, or (at your option) any later version. * +#* for detail see the LICENCE text file. * +#* * +#* This program is distributed in the hope that it will be useful, * +#* but WITHOUT ANY WARRANTY; without even the implied warranty of * +#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +#* GNU Library General Public License for more detaillc. * +#* * +#* You should have received a copy of the GNU Library General Public * +#* License along with this program; if not, write to the Free Software * +#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * +#* USA * +#* * +#*************************************************************************** + + +import FreeCAD as App +import Spreadsheet +import Units + + +READ_ONLY_FOREGROUND = (0.5, 0.5, 0.5) +READ_ONLY_BACKGROUND = (0.9, 0.9, 0.9) + + +def createLoadCondition(ship): + """Create a new loading condition spreadsheet + + Position arguments: + ship -- Ship object + + Returned value: + lc -- The new loading condition spreadsheet object + + The new spreadsheet will initialize all the tanks as empty. To modify the + fluid density and the filling level percentages the columns D and E should + be edited (the first tank can be found at the row 6) + + For instance, the following code snippet can be used to set the first tank + 50% filled with water: + lc.set("D6", "998.0") + lc.set("E6", "0.5") + + The label of the tank can be extracted from the column C, for instance: + lc.get("C6") + Such information can be used to get the tank object (the followinbg code + may fail if either the tank has been removed, or several objects with the + same label already exist): + tank = App.ActiveDocument.getObjectsByLabel(lc.get("C6"))[0] + + The tool will claim the new spreadsheet loading condition as a child of the + ship object. Please do not remove the partner ship object before removing + this new loading condition before. + """ + # Create the spreadsheet + lc = App.activeDocument().addObject('Spreadsheet::Sheet', + 'LoadCondition') + + # Add a description + lc.setForeground('A1:B2', READ_ONLY_FOREGROUND) + lc.setBackground('A1:B2', READ_ONLY_BACKGROUND) + lc.setAlignment('B1:B2', 'center', 'keep') + lc.setStyle('B1:B2', 'italic', 'add') + lc.set("A1", "Ship:") + lc.set("A2", "Load condition:") + lc.set("B1", "=" + ship.Name + ".Label") + lc.set("B2", "=Label") + + # Add the weights data + lc.setAlignment('A4:A5', 'center', 'keep') + lc.setStyle('A4:A5', 'bold', 'add') + lc.setStyle('A4:A5', 'underline', 'add') + lc.set("A4", "WEIGHTS DATA") + lc.set("A5", "name") + for i in range(len(ship.Weights)): + weight = App.activeDocument().getObject(ship.Weights[i]) + lc.set("A{}".format(i + 6), "=" + weight.Name + ".Label") + lc.setForeground('A4:A{}'.format(5 + len(ship.Weights)), READ_ONLY_FOREGROUND) + lc.setBackground('A4:A{}'.format(5 + len(ship.Weights)), READ_ONLY_BACKGROUND) + + # Add the tanks data + lc.mergeCells('C4:E4') + lc.setForeground('C4:E5', READ_ONLY_FOREGROUND) + lc.setBackground('C4:E5', READ_ONLY_BACKGROUND) + lc.setAlignment('C4:E5', 'center', 'keep') + lc.setStyle('C4:E5', 'bold', 'add') + lc.setStyle('C4:E5', 'underline', 'add') + lc.set("C4", "TANKS DATA") + lc.set("C5", "name") + lc.set("D5", "Fluid density [kg/m^3]") + lc.set("E5", "Filling ratio (interval [0, 1])") + if len(ship.Tanks): + for i in range(len(ship.Tanks)): + tank = App.activeDocument().getObject(ship.Tanks[i]) + lc.set("C{}".format(i + 6), "=" + tank.Name + ".Label") + lc.set("D{}".format(i + 6), "998.0") + lc.set("E{}".format(i + 6), "0.0") + lc.setForeground('C6:C{}'.format(5 + len(ship.Tanks)), READ_ONLY_FOREGROUND) + lc.setBackground('C6:C{}'.format(5 + len(ship.Tanks)), READ_ONLY_BACKGROUND) + + lc.setColumnWidth('A', 128) + lc.setColumnWidth('B', 128) + lc.setColumnWidth('C', 128) + lc.setColumnWidth('D', 150) + lc.setColumnWidth('E', 200) + + # Add the spreadsheet to the list of loading conditions of the ship + lcs = ship.LoadConditions[:] + lcs.append(lc.Name) + ship.LoadConditions = lcs + ship.Proxy.cleanLoadConditions(ship) + + # Recompute to take the changes + App.activeDocument().recompute() + + return lc \ No newline at end of file diff --git a/src/Mod/Ship/shipCreateLoadCondition/__init__.py b/src/Mod/Ship/shipCreateLoadCondition/__init__.py index b33214bd5..7515040c5 100644 --- a/src/Mod/Ship/shipCreateLoadCondition/__init__.py +++ b/src/Mod/Ship/shipCreateLoadCondition/__init__.py @@ -23,7 +23,8 @@ import FreeCAD as App import FreeCADGui as Gui -import Spreadsheet +from PySide import QtGui +import Tools READ_ONLY_FOREGROUND = (0.5, 0.5, 0.5) @@ -73,120 +74,4 @@ def load(): App.Console.PrintError(msg + '\n') return - # Create the spreadsheet - s = App.activeDocument().addObject('Spreadsheet::Sheet', - 'LoadCondition') - - # Add a description - s.setForeground('A1:B2', READ_ONLY_FOREGROUND) - s.setBackground('A1:B2', READ_ONLY_BACKGROUND) - s.setAlignment('B1:B2', 'center', 'keep') - s.setStyle('B1:B2', 'italic', 'add') - s.set("A1", "Ship:") - s.set("A2", "Load condition:") - s.set("B1", "=" + ship.Name + ".Label") - s.set("B2", "=Label") - - # Add the weights data - s.setAlignment('A4:A5', 'center', 'keep') - s.setStyle('A4:A5', 'bold', 'add') - s.setStyle('A4:A5', 'underline', 'add') - s.set("A4", "WEIGHTS DATA") - s.set("A5", "name") - for i in range(len(ship.Weights)): - weight = App.activeDocument().getObject(ship.Weights[i]) - s.set("A{}".format(i + 6), "=" + weight.Name + ".Label") - s.setForeground('A4:A{}'.format(5 + len(ship.Weights)), READ_ONLY_FOREGROUND) - s.setBackground('A4:A{}'.format(5 + len(ship.Weights)), READ_ONLY_BACKGROUND) - - # Add the tanks data - s.mergeCells('C4:E4') - s.setForeground('C4:E5', READ_ONLY_FOREGROUND) - s.setBackground('C4:E5', READ_ONLY_BACKGROUND) - s.setAlignment('C4:E5', 'center', 'keep') - s.setStyle('C4:E5', 'bold', 'add') - s.setStyle('C4:E5', 'underline', 'add') - s.set("C4", "TANKS DATA") - s.set("C5", "name") - s.set("D5", "Fluid density [kg/m^3]") - s.set("E5", "Filling ratio (interval [0, 1])") - if len(ship.Tanks): - for i in range(len(ship.Tanks)): - tank = App.activeDocument().getObject(ship.Tanks[i]) - s.set("C{}".format(i + 6), "=" + tank.Name + ".Label") - s.set("D{}".format(i + 6), "998.0") - s.set("E{}".format(i + 6), "0.0") - s.setForeground('C6:C{}'.format(5 + len(ship.Tanks)), READ_ONLY_FOREGROUND) - s.setBackground('C6:C{}'.format(5 + len(ship.Tanks)), READ_ONLY_BACKGROUND) - - s.setColumnWidth('A', 128) - s.setColumnWidth('B', 128) - s.setColumnWidth('C', 128) - s.setColumnWidth('D', 150) - s.setColumnWidth('E', 200) - - """ - # Add a reference to the owner ship - s.mergeCells('A1:D1') - s.setAlignment('A1:B2', 'center', 'keep') - s.setStyle('A1:B2', 'bold', 'add') - s.setStyle('A1:B2', 'underline', 'add') - s.set("A1", "SHIP DATA") - s.set("A2", "ship") - s.set("A3", ship.Label) - s.set("B2", "internal ref") - s.set("B3", ship.Name) - s.setForeground('A1:B3', (0.5,0.5,0.5)) - - # Clean the Ship instance before generating the load condition - ship.Proxy.cleanWeights(ship) - ship.Proxy.cleanTanks(ship) - - # Add the weights data - s.mergeCells('A4:D4') - s.setAlignment('A4:B5', 'center', 'keep') - s.setStyle('A4:B5', 'bold', 'add') - s.setStyle('A4:B5', 'underline', 'add') - s.set("A4", "WEIGHTS DATA") - s.set("A5", "weight") - s.set("B5", "internal ref") - for i in range(len(ship.Weights)): - weight = App.activeDocument().getObject(ship.Weights[i]) - s.set("A{}".format(i + 6), weight.Label) - s.set("B{}".format(i + 6), weight.Name) - s.setForeground('A4:B{}'.format(5 + len(ship.Weights)), (0.5,0.5,0.5)) - - # Add the tanks data - s.mergeCells('A{0}:D{0}'.format(6 + len(ship.Weights))) - s.setAlignment('A{0}:A{0}'.format(6 + len(ship.Weights)), 'center', 'keep') - s.setAlignment('A{0}:D{0}'.format(7 + len(ship.Weights)), 'center', 'keep') - s.setStyle('A{0}:A{0}'.format(6 + len(ship.Weights)), 'bold', 'add') - s.setStyle('A{0}:D{0}'.format(7 + len(ship.Weights)), 'bold', 'add') - s.setStyle('A{0}:A{0}'.format(6 + len(ship.Weights)), 'underline', 'add') - s.setStyle('A{0}:D{0}'.format(7 + len(ship.Weights)), 'underline', 'add') - s.set("A{}".format(6 + len(ship.Weights)), "TANKS DATA") - s.set("A{}".format(7 + len(ship.Weights)), "tank") - s.set("B{}".format(7 + len(ship.Weights)), "internal ref") - s.set("C{}".format(7 + len(ship.Weights)), "Fluid density [kg/m^3]") - s.set("D{}".format(7 + len(ship.Weights)), "Filling ratio (interval [0.0,1.0])") - for i in range(len(ship.Tanks)): - tank = App.activeDocument().getObject(ship.Tanks[i]) - s.set("A{}".format(i + 8 + len(ship.Weights)), tank.Label) - s.set("B{}".format(i + 8 + len(ship.Weights)), tank.Name) - s.set("C{}".format(i + 8 + len(ship.Weights)), "998.0") - s.set("D{}".format(i + 8 + len(ship.Weights)), "0.0") - s.setForeground('A{0}:A{0}'.format(6 + len(ship.Weights)), (0.5,0.5,0.5)) - s.setForeground('A{0}:D{0}'.format(7 + len(ship.Weights)), (0.5,0.5,0.5)) - s.setForeground('A{}:B{}'.format(8 + len(ship.Weights), - 8 + len(ship.Weights) + len(ship.Tanks)), - (0.5,0.5,0.5)) - """ - - # Add the spreadsheet to the list of loading conditions of the ship - lcs = ship.LoadConditions[:] - lcs.append(s.Name) - ship.LoadConditions = lcs - ship.Proxy.cleanLoadConditions(ship) - - # Recompute to take the changes - App.activeDocument().recompute() \ No newline at end of file + Tools.createLoadCondition(ship) \ No newline at end of file