From 0a3a2b1de0e95e85eacdbde0856b83c3cbf42547 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Luis=20Cerc=C3=B3s=20pita?= Date: Mon, 14 May 2012 14:42:14 +0200 Subject: [PATCH] Added annotations to ship weights definition tool --- src/Mod/Ship/CMakeLists.txt | 1 + src/Mod/Ship/Makefile.am | 1 + src/Mod/Ship/tankWeights/Preview.py | 106 ++++++++++++++++++++++++++ src/Mod/Ship/tankWeights/TaskPanel.py | 34 +++++++++ 4 files changed, 142 insertions(+) create mode 100644 src/Mod/Ship/tankWeights/Preview.py diff --git a/src/Mod/Ship/CMakeLists.txt b/src/Mod/Ship/CMakeLists.txt index a3c14ce96..c44e313ab 100644 --- a/src/Mod/Ship/CMakeLists.txt +++ b/src/Mod/Ship/CMakeLists.txt @@ -101,6 +101,7 @@ SOURCE_GROUP("shiputils" FILES ${ShipUtils_SRCS}) SET(ShipWeights_SRCS tankWeights/__init__.py + tankWeights/Preview.py tankWeights/TaskPanel.py tankWeights/TaskPanel.ui ) diff --git a/src/Mod/Ship/Makefile.am b/src/Mod/Ship/Makefile.am index 35c1957b7..01b296c01 100644 --- a/src/Mod/Ship/Makefile.am +++ b/src/Mod/Ship/Makefile.am @@ -70,6 +70,7 @@ nobase_data_DATA = \ shipUtils/Paths.py \ shipUtils/Translator.py \ tankWeights/__init__.py \ + tankWeights/Preview.py \ tankWeights/TaskPanel.py \ tankWeights/TaskPanel.ui \ tankCreateTank/__init__.py \ diff --git a/src/Mod/Ship/tankWeights/Preview.py b/src/Mod/Ship/tankWeights/Preview.py new file mode 100644 index 000000000..571155bb9 --- /dev/null +++ b/src/Mod/Ship/tankWeights/Preview.py @@ -0,0 +1,106 @@ +#*************************************************************************** +#* * +#* Copyright (c) 2011, 2012 * +#* 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 details. * +#* * +#* 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 * +#* * +#*************************************************************************** + +# FreeCAD modules +import FreeCAD,FreeCADGui +from FreeCAD import Base +from FreeCAD import Part +# FreeCADShip modules +from shipUtils import Paths, Translator + +class Preview(object): + def __init__(self): + """ Constructor. + """ + self.objects = [] + + def reinit(self): + """ Reinitializate drawer. + """ + self.clean() + + def update(self, names, pos): + """ Update the 3D view printing annotations. + @param names Weight names. + @param pos Weight positions (FreeCAD::Base::Vector). + """ + # Destroy all previous entities + self.clean() + for i in range(0, len(names)): + # Draw gravity line + line = Part.makeLine((pos[i].x,pos[i].y,pos[i].z),(pos[i].x,pos[i].y,pos[i].z - 9.81)) + Part.show(line) + objs = FreeCAD.ActiveDocument.Objects + self.objects.append(objs[-1]) + objs[-1].Label = names[i] + 'Line' + # Draw circles + circle = Part.makeCircle(0.5, pos[i], Base.Vector(1.0,0.0,0.0)) + Part.show(circle) + objs = FreeCAD.ActiveDocument.Objects + self.objects.append(objs[-1]) + objs[-1].Label = names[i] + 'CircleX' + circle = Part.makeCircle(0.5, pos[i], Base.Vector(0.0,1.0,0.0)) + Part.show(circle) + objs = FreeCAD.ActiveDocument.Objects + self.objects.append(objs[-1]) + objs[-1].Label = names[i] + 'CircleY' + circle = Part.makeCircle(0.5, pos[i], Base.Vector(0.0,0.0,1.0)) + Part.show(circle) + objs = FreeCAD.ActiveDocument.Objects + self.objects.append(objs[-1]) + objs[-1].Label = names[i] + 'CircleZ' + # Draw annotation + self.objects.append(DrawText(names[i] + 'Text', names[i], Base.Vector(pos[i].x+1.0,pos[i].y,pos[i].z))) + + def clean(self): + """ Erase all annotations from screen. + """ + for i in range(0,len(self.objects)): + if not FreeCAD.ActiveDocument.getObject(self.objects[i].Name): + continue + FreeCAD.ActiveDocument.removeObject(self.objects[i].Name) + self.objects = [] + +def DrawText(name, string, position, displayMode="Screen", angle=0.0, justification="Left", colour=(0.00,0.00,0.00), size=12): + """ Draws a text in a desired position. + @param name Name of the object + @param string Text to draw (recommended format u'') + @param position Point to draw the text + @param angle Counter clockwise rotation of text + @param justification Alignement of the text ("Left", "Right" or "Center") + @param colour Colour of the text + @param size Font size + @return FreeCAD annotation object + """ + # Create the object + text = FreeCAD.ActiveDocument.addObject("App::Annotation",name) + # Set the text + text.LabelText = [string, u''] + # Set the options + text.Position = position + FreeCADGui.ActiveDocument.getObject(text.Name).Rotation = angle + FreeCADGui.ActiveDocument.getObject(text.Name).Justification = justification + FreeCADGui.ActiveDocument.getObject(text.Name).FontSize = size + FreeCADGui.ActiveDocument.getObject(text.Name).TextColor = colour + FreeCADGui.ActiveDocument.getObject(text.Name).DisplayMode = displayMode + return FreeCAD.ActiveDocument.getObject(text.Name) diff --git a/src/Mod/Ship/tankWeights/TaskPanel.py b/src/Mod/Ship/tankWeights/TaskPanel.py index 14cda21e5..a01f7c73c 100644 --- a/src/Mod/Ship/tankWeights/TaskPanel.py +++ b/src/Mod/Ship/tankWeights/TaskPanel.py @@ -27,6 +27,7 @@ import FreeCADGui as Gui # Qt library from PyQt4 import QtGui,QtCore # Module +import Preview from Instance import * from shipUtils import Paths, Translator @@ -34,8 +35,10 @@ class TaskPanel: def __init__(self): self.ui = Paths.modulePath() + "/tankWeights/TaskPanel.ui" self.ship = None + self.preview = Preview.Preview() def accept(self): + self.preview.clean() if not self.ship: return False # Setup lists @@ -62,6 +65,7 @@ class TaskPanel: return True def reject(self): + self.preview.clean() if not self.ship: return False return True @@ -98,6 +102,21 @@ class TaskPanel: self.retranslateUi() # Connect Signals and Slots QtCore.QObject.connect(form.weights,QtCore.SIGNAL("cellChanged(int,int)"),self.onTableItem); + # Update screen + name = [] + pos = [] + for i in range(0,self.form.weights.rowCount() - 1): + item = self.form.weights.item(i,0) + name.append(item.text().__str__()) + vec = [] + item = self.form.weights.item(i,2) + vec.append(item.text().toFloat()[0]) + item = self.form.weights.item(i,3) + vec.append(item.text().toFloat()[0]) + item = self.form.weights.item(i,4) + vec.append(item.text().toFloat()[0]) + pos.append(App.Base.Vector(vec[0],vec[1],vec[2])) + self.preview.update(name, pos) def getMainWindow(self): "returns the main window" @@ -202,6 +221,21 @@ class TaskPanel: (number,flag) = item.text().toFloat() if not flag: item.setText('0.0') + # Update screen annotations + name = [] + pos = [] + for i in range(0,self.form.weights.rowCount() - 1): + item = self.form.weights.item(i,0) + name.append(item.text().__str__()) + vec = [] + item = self.form.weights.item(i,2) + vec.append(item.text().toFloat()[0]) + item = self.form.weights.item(i,3) + vec.append(item.text().toFloat()[0]) + item = self.form.weights.item(i,4) + vec.append(item.text().toFloat()[0]) + pos.append(App.Base.Vector(vec[0],vec[1],vec[2])) + self.preview.update(name, pos) def createTask(): panel = TaskPanel()