diff --git a/src/Mod/Ship/Instance.py b/src/Mod/Ship/Instance.py index 042a6c097..be4481ed8 100644 --- a/src/Mod/Ship/Instance.py +++ b/src/Mod/Ship/Instance.py @@ -98,7 +98,6 @@ class Ship: @param nS Number of sections @param nP Number of points per section """ - print(nS,nP) self.obj.addProperty("App::PropertyInteger","nSections","Ship", str(Translator.translate("Number of sections"))).nSections=nS self.obj.addProperty("App::PropertyIntegerList","nPoints","Ship", str(Translator.translate("List of number of points per sections (accumulated histogram)"))).nPoints=[0] self.obj.addProperty("App::PropertyFloatList","xSection","Ship", str(Translator.translate("List of sections x coordinate"))).xSection=[] @@ -168,6 +167,8 @@ class Ship: for j in range(0,len(section)): mSections.append(section[j]) # Save data + for i in range(1,len(nPoints)): + nPoints[i] = nPoints[i] + nPoints[i-1] self.obj.nPoints = nPoints[:] self.obj.xSection = xSection[:] self.obj.mSections = mSections[:] @@ -661,10 +662,8 @@ def sections(obj): histogram = obj.nPoints[:] points = obj.mSections[:] sections = [] - print(histogram) for i in range(0, len(histogram) - 1): sections.append([]) - print(histogram[i],histogram[i+1]) for j in range(histogram[i],histogram[i+1]): sections[i].append(points[j]) return sections diff --git a/src/Mod/Ship/shipAreasCurve/TaskPanel.py b/src/Mod/Ship/shipAreasCurve/TaskPanel.py index 9b43c99c2..f076794ae 100644 --- a/src/Mod/Ship/shipAreasCurve/TaskPanel.py +++ b/src/Mod/Ship/shipAreasCurve/TaskPanel.py @@ -181,14 +181,14 @@ class TaskPanel: # Calculate drafts angle = math.radians(self.form.trim.value()) L = self.ship.Length - draftAP = self.form.trim.value() + 0.5*L*math.tan(angle) + draftAP = self.form.draft.value() + 0.5*L*math.tan(angle) if draftAP < 0.0: draftAP = 0.0 - draftFP = self.form.trim.value() - 0.5*L*math.tan(angle) + draftFP = self.form.draft.value() - 0.5*L*math.tan(angle) if draftFP < 0.0: draftFP = 0.0 # Calculate hydrostatics involved - disp = Hydrostatics.Displacement(self.ship,self.form.draft.value(),self.form.trim.value()) + data = Hydrostatics.Displacement(self.ship,self.form.draft.value(),self.form.trim.value()) # Prepare the string in html format string = 'L = %g [m]
' % (self.ship.Length) string = string + 'B = %g [m]
' % (self.ship.Beam) @@ -196,8 +196,8 @@ class TaskPanel: string = string + 'Trim = %g [degrees]
' % (self.form.trim.value()) string = string + 'TAP = %g [m]
' % (draftAP) string = string + 'TFP = %g [m]
' % (draftFP) - string = string + Translator.translate('Displacement') + ' = %g [ton]
' % (disp[0]) - string = string + 'XCB = %g [m]' % (disp[0]) + string = string + Translator.translate('Displacement') + ' = %g [ton]
' % (data[1]) + string = string + 'XCB = %g [m]' % (data[2]) # Set the document self.form.output.setHtml(string) diff --git a/src/Mod/Ship/shipHydrostatics/Tools.py b/src/Mod/Ship/shipHydrostatics/Tools.py index 282ccb71a..32ce25dc0 100644 --- a/src/Mod/Ship/shipHydrostatics/Tools.py +++ b/src/Mod/Ship/shipHydrostatics/Tools.py @@ -25,12 +25,80 @@ import math # FreeCAD modules import FreeCAD as App import FreeCADGui as Gui +# Module +import Instance def Displacement(ship, draft, trim): """ Calculate ship displacement. @param ship Selected ship instance @param traft Draft. @param trim Trim in degrees. - @return Ship displacement, also X bouyance center coordinate is provided. + @return [areas,disp,xcb]: \n + areas : Area of each section \n + disp: Ship displacement \n + xcb: X bouyance center coordinate """ - return [0.0, 0.0] + angle = math.radians(trim) + sections = Instance.sections(ship) + xCoord = ship.xSection[:] + areas = [] + vol = 0.0 + moment = 0.0 + if not sections: + return [[],0.0,0.0] + for i in range(0, len(sections)): + # Get the section + section = sections[i] + if len(section) < 2: # Empty section + areas.append(0.0) + continue + # Get the position of the section + x = xCoord[i] + # Get the maximum Z value + Z = draft - x*math.tan(angle) + # Count the number of valid points + n = 0 + for j in range(0,len(section)): + z = section[j].z + if z > Z: + break + n = n+1 + # Discard invalid sections + if n == 0: + areas.append(0.0) + continue + # Truncate only valid points + points = section[0:n] + # Study if additional point is needed + if n < len(section): + y0 = section[n-1].y + z0 = section[n-1].z + y1 = section[n].y + z1 = section[n].z + factor = (Z - z0) / (z1 - z0) + y = y0 + factor*(y1 - y0) + points.append(App.Base.Vector(x,y,Z)) + # Integrate area + area = 0.0 + for j in range(0, len(points)-1): + y0 = abs(points[j].y) + z0 = points[j].z + y1 = abs(points[j+1].y) + z1 = points[j+1].z + y = 0.5 * (y0 + y1) + dz = z1 - z0 + area = area + 2.0*y*dz # 2x because only half ship is represented + areas.append(area) + # Add volume & moment if proceed + if i > 0: + dx = xCoord[i] - xCoord[i-1] + x = 0.5*(xCoord[i] + xCoord[i-1]) + area = 0.5*(areas[i] + areas[i-1]) + vol = vol + area*dx + moment = moment + area*dx*x + # Compute displacement and xcb + disp = vol / 1.025 # rho = 1.025 ton/m3 (salt water density) + xcb = 0.0 + if vol > 0.0: + xcb = moment / vol + return [areas,disp,xcb]