# -*- coding: cp936 -*-
import kcs_dex
import kcs_util
import kcs_ui
import os
panel_name=[]
panel_weight=[]
block_name=[]
panel_cog=[]
block_weight=0.0
ship_weight=0.0
xweight = 0.0
yweight = 0.0
zweight = 0.0
ship_xweight = 0.0
ship_yweight = 0.0
ship_zweight = 0.0
m=0
n=0
#提示开始计算
start = kcs_ui.answer_req("提示!","开始计算分段体结构重量和重心位置?")
if start == kcs_util.yes():
#打开一个文件准备记录
file = open("d:\重量重心.csv","w")
#首先提取各个分段的各个板架的重量----------------------------------#
string = "HULL.BLOCK(*).PANEL(*).WEIGHT"
kcs_dex.extract(string)
type = kcs_dex.next_result()
while type >= 0:
try:
#提取板架所属的分段名
command = kcs_dex.get_commandstring()
ind1 = command.find("K(") # find BLOCK('
ind2 = command.find(").P", ind1) # find ').PANEL
name = command[ind1+2:ind2] # 得到分段名
#判断,分段名是否为"block",如不是,则继续
if name != "BLOCK":
weight = kcs_dex.get_real()
#再判断,板架重量是否为零,如不是,继续
if weight != 0.0:
#找到板架名,并储存在panel_name的列表中
ind1 = command.find("L(")
ind2 = command.find(").W")
panel_name.append(command[ind1+2:ind2])
#将各板架重量存储在“panel_weight”的列表中
panel_weight.append(weight)
#将各板架重量累积,得到全船重量
ship_weight=ship_weight + weight
#记录各分段的名
block_name.append(name) # 得到分段名
except:
pass
type = kcs_dex.next_result()
#----------------------------------------------------------------#
#然后提取各个分段的各个板架的重心------------------------------------#
string = "HULL.BLOCK(*).PANEL(*).cog"
kcs_dex.extract(string)
type = kcs_dex.next_result()
while type >= 0:
try:
#提取板架所属的分段名
command = kcs_dex.get_commandstring()
ind1 = command.find("K(") # find BLOCK('
ind2 = command.find(").P", ind1) # find ').PANEL
name = command[ind1+2:ind2] # 得到分段名
#判断,分段名是否为"block",如不是,则继续
if name != "BLOCK":
cog = kcs_dex.get_reavec3d()
#再判断,重心是否也为零,如不是,则继续
if cog != (0.0,0.0,0.0):
#将各板架重心存储在“panel_cog”的列表中
panel_cog.append(cog)
#将重心分解为x,y和z方向
x = cog[0]
y = cog[1]
z = cog[2]
#计算全船的x,y和z方向的静距
ship_xweight = ship_xweight + panel_weight[m] * x
ship_yweight = ship_yweight + panel_weight[m] * y
ship_zweight = ship_zweight + panel_weight[m] * z
m=m+1
except:
pass
type = kcs_dex.next_result()
#计算全船重心位置
ship_xcog = ship_xweight / ship_weight
ship_ycog = ship_yweight / ship_weight
ship_zcog = ship_zweight / ship_weight
#------------------------------------------------------------------#
#将全船的重量和重心位置写入文件
s='船体结构重量'+','+'(吨)'+','+'船体结构重心'+','+'X方向(毫米)'+','+'Y方向(毫米)'+','+'Z方向(毫米)' + " \n"
file.write(s)
s=''+','+str(ship_weight)+','+''+','+str(int(ship_xcog))+','+str(int(ship_ycog))+','+str(int(ship_zcog)) + " \n"
file.write(s)
#循环判断分段名列表中各个分段是否一致-----------------------------------#
m=0
h=0
while n < len(block_name)-1:
#如是同一分段
if block_name[n]==block_name[n+1]:
h=h+1
m=m+1
#将该分段各板架重量累加
block_weight=block_weight + panel_weight[n]
#提取该分段各板架x,y和z方向的重心
reavec3d=panel_cog[n]
x = reavec3d[0]
y = reavec3d[1]
z = reavec3d[2]
#计算该分段的x,y和z方向的静距
xweight = xweight + panel_weight[n] * x
yweight = yweight + panel_weight[n] * y
zweight = zweight + panel_weight[n] * z
#如不是同一分段
else :
#将该分段最后一个板架重量计入,得到分段重量
block_weight=block_weight + panel_weight[n]
#提取该分段最后一个板架x,y和z方向的重心
reavec3d=panel_cog[n]
x = reavec3d[0]
y = reavec3d[1]
z = reavec3d[2]
#计算该分段最终x,y和z方向的静距
xweight = xweight + panel_weight[n] * x
yweight = yweight + panel_weight[n] * y
zweight = zweight + panel_weight[n] * z
#计算得到该分段重心位置
xcog = xweight / block_weight
ycog = yweight / block_weight
zcog = zweight / block_weight
#写入分段重量重心的开头
s='分段名'+','+'分段结构重量(吨)'+','+'分段结构重心'+','+'X方向(毫米)'+','+'Y方向毫米'+','+'Z方向毫米 \n'
file.write(s)
#将分段的重量重心写入文件
s=block_name[n]+','+str(block_weight)+','+''+','+str(int(xcog))+','+str(int(ycog))+','+str(int(zcog)) + " \n"
file.write(s)
#写入板架重量重心的开头
s='板架名'+','+'板架结构重量(吨)'+','+'板架结构重心'+','+'X方向(毫米)'+','+'Y方向毫米'+','+'Z方向毫米 \n'
file.write(s)
#循环写入该分段所有板架的重量重心
x1=m-h
x2=m+1
for a in range(x1,x2):
name = panel_name[a]
weight = panel_weight[a]
cog = panel_cog[a]
x = cog[0]
y = cog[1]
z = cog[2]
s = name+','+str(weight)+','+''+','+str(int(x))+','+str(int(y))+','+str(int(z)) + " \n"
file.write(s)
xweight = 0.0
yweight = 0.0
zweight = 0.0
block_weight=0.0
m=n+1
h=0
n=n+1
#--------------------------------------------------------------------------#
#将分段名列表中最后一个板架重量计入,得到分段重量--------------------------------#
block_weight=block_weight + panel_weight[n]
h=h+1
m=m+1
#然后提取该板架x,y和z方向的重心
reavec3d=panel_cog[n]
x = reavec3d[0]
y = reavec3d[1]
z = reavec3d[2]
#接着计算分段名列表中最后一个分段最终x,y和z方向的静距
xweight = xweight + panel_weight[n] * x
yweight = yweight + panel_weight[n] * y
zweight = zweight + panel_weight[n] * z
#并计算得到该分段的重心位置
xcog = xweight / block_weight
ycog = yweight / block_weight
zcog = zweight / block_weight
#写入分段重量重心的开头
s='分段名'+','+'分段结构重量(吨)'+','+'分段结构重心'+','+'X方向(毫米)'+','+'Y方向毫米'+','+'Z方向毫米 \n'
file.write(s)
#将其重量和重心结果写入文件
s=block_name[n]+','+str(block_weight)+','+''+','+str(int(xcog))+','+str(int(ycog))+','+str(int(zcog)) + " \n"
file.write(s)
#写入板架重量重心的开头
s='板架名'+','+'板架结构重量(吨)'+','+'板架结构重心'+','+'X方向(毫米)'+','+'Y方向毫米'+','+'Z方向毫米 \n'
file.write(s)
#写入最后一个分段所有板架的重量重心
#循环写入最后一个分段所有板架的重量重心
x1=m-h
for a in range(x1,m):
name = panel_name[a]
weight = panel_weight[a]
cog = panel_cog[a]
x = cog[0]
y = cog[1]
z = cog[2]
s = name+','+str(weight)+','+''+','+str(int(