合并工单优化
This commit is contained in:
@@ -4,7 +4,6 @@ import math
|
||||
import requests
|
||||
import logging
|
||||
import base64
|
||||
import hashlib
|
||||
# import subprocess
|
||||
from datetime import datetime
|
||||
from dateutil.relativedelta import relativedelta
|
||||
@@ -48,12 +47,9 @@ class ResMrpWorkOrder(models.Model):
|
||||
workorder.user_permissions = False
|
||||
|
||||
user_permissions = fields.Boolean('用户权限', compute='get_user_permissions')
|
||||
programming_no = fields.Char('编程单号')
|
||||
work_state = fields.Char('业务状态')
|
||||
programming_state = fields.Char('编程状态')
|
||||
cnc_worksheet = fields.Binary(
|
||||
'工作指令', readonly=True)
|
||||
material_center_point = fields.Char(string='配料中心点')
|
||||
material_center_point = fields.Char(string='胚料中心点')
|
||||
X1_axis = fields.Float(default=0)
|
||||
Y1_axis = fields.Float(default=0)
|
||||
Z1_axis = fields.Float(default=0)
|
||||
@@ -89,48 +85,50 @@ class ResMrpWorkOrder(models.Model):
|
||||
string="检测结果")
|
||||
cnc_ids = fields.One2many("sf.cnc.processing", 'workorder_id', string="CNC加工")
|
||||
tray_code = fields.Char(string="托盘")
|
||||
programming_no = fields.Char('编程单号')
|
||||
is_programming = fields.Boolean('是否编程', default=False)
|
||||
|
||||
# 计算配料中心点和与x轴倾斜度方法
|
||||
def getcenter(self):
|
||||
x1 = self.X1_axis
|
||||
x2 = self.X2_axis
|
||||
x3 = self.X3_axis
|
||||
x4 = self.X4_axis
|
||||
x5 = self.X5_axis
|
||||
x6 = self.X6_axis
|
||||
x7 = self.X7_axis
|
||||
x8 = self.X8_axis
|
||||
y1 = self.Y1_axis
|
||||
y2 = self.Y2_axis
|
||||
y3 = self.Y3_axis
|
||||
y4 = self.Y4_axis
|
||||
y5 = self.Y5_axis
|
||||
y6 = self.Y6_axis
|
||||
y7 = self.Y7_axis
|
||||
y8 = self.Y8_axis
|
||||
z1 = self.Z9_axis
|
||||
x0 = ((x3 - x4) * (x2 * y1 - x1 * y2) - (x1 - x2) * (x4 * y3 - x3 * y4)) / (
|
||||
(x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4))
|
||||
y0 = ((y3 - y4) * (y2 * x1 - y1 * x2) - (y1 - y2) * (y4 * x3 - y3 * x4)) / (
|
||||
(y3 - y4) * (x1 - x2) - (y1 - y2) * (x3 - x4))
|
||||
x1 = ((x7 - x8) * (x6 * y5 - x5 * y6) - (x5 - x6) * (x8 * y7 - x7 * y8)) / (
|
||||
(x7 - x8) * (y5 - y6) - (x5 - x6) * (y7 - y8));
|
||||
y1 = ((y7 - y8) * (y6 * x5 - y5 * x6) - (y5 - y6) * (y8 * x7 - y7 * x8)) / (
|
||||
(y7 - y8) * (x5 - x6) - (y5 - y6) * (x7 - x8))
|
||||
x = (x0 + x1) / 2
|
||||
y = (y0 + y1) / 2
|
||||
z = z1 / 2
|
||||
try:
|
||||
x1 = self.X1_axis
|
||||
x2 = self.X2_axis
|
||||
x3 = self.X3_axis
|
||||
x4 = self.X4_axis
|
||||
x5 = self.X5_axis
|
||||
x6 = self.X6_axis
|
||||
x7 = self.X7_axis
|
||||
x8 = self.X8_axis
|
||||
y1 = self.Y1_axis
|
||||
y2 = self.Y2_axis
|
||||
y3 = self.Y3_axis
|
||||
y4 = self.Y4_axis
|
||||
y5 = self.Y5_axis
|
||||
y6 = self.Y6_axis
|
||||
y7 = self.Y7_axis
|
||||
y8 = self.Y8_axis
|
||||
z1 = self.Z9_axis
|
||||
x0 = ((x3 - x4) * (x2 * y1 - x1 * y2) - (x1 - x2) * (x4 * y3 - x3 * y4)) / (
|
||||
(x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4))
|
||||
y0 = ((y3 - y4) * (y2 * x1 - y1 * x2) - (y1 - y2) * (y4 * x3 - y3 * x4)) / (
|
||||
(y3 - y4) * (x1 - x2) - (y1 - y2) * (x3 - x4))
|
||||
x1 = ((x7 - x8) * (x6 * y5 - x5 * y6) - (x5 - x6) * (x8 * y7 - x7 * y8)) / (
|
||||
(x7 - x8) * (y5 - y6) - (x5 - x6) * (y7 - y8));
|
||||
y1 = ((y7 - y8) * (y6 * x5 - y5 * x6) - (y5 - y6) * (y8 * x7 - y7 * x8)) / (
|
||||
(y7 - y8) * (x5 - x6) - (y5 - y6) * (x7 - x8))
|
||||
x = (x0 + x1) / 2
|
||||
y = (y0 + y1) / 2
|
||||
z = z1 / 2
|
||||
|
||||
jd = math.atan2((x5 - x6), (y5 - y6))
|
||||
jdz = jd * 180 / math.pi
|
||||
print("(%.2f,%.2f)" % (x, y))
|
||||
self.material_center_point = ("(%.2f,%.2f,%.2f)" % (x, y, z))
|
||||
self.X_deviation_angle = jdz
|
||||
return self.material_center_point
|
||||
except:
|
||||
raise UserError("参数计算有误")
|
||||
|
||||
jd = math.atan2((x5 - x6), (y5 - y6))
|
||||
jdz = jd * 180 / math.pi
|
||||
print("(%.2f,%.2f)" % (x, y))
|
||||
self.material_center_point = ("(%.2f,%.2f,%.2f)" % (x, y, z))
|
||||
self.X_deviation_angle = jdz
|
||||
# 将补偿值写入CNC加工工单
|
||||
workorder = self.env['mrp.workorder'].browse(self.ids)
|
||||
work = workorder.production_id.workorder_ids
|
||||
work.compensation_value_x = eval(self.material_center_point)[0]
|
||||
work.compensation_value_y = eval(self.material_center_point)[1]
|
||||
|
||||
def json_workorder_str(self, k, production, route):
|
||||
workorders_values_str = [0, '', {
|
||||
@@ -140,7 +138,6 @@ class ResMrpWorkOrder(models.Model):
|
||||
'name': route.route_workcenter_id.name,
|
||||
'processing_panel': k,
|
||||
'routing_type': route.routing_type,
|
||||
'work_state': '' if not route.routing_type == '获取CNC加工程序' else '待发起',
|
||||
'workcenter_id': self.env['mrp.routing.workcenter'].get_workcenter(route.workcenter_ids.ids),
|
||||
'date_planned_start': False,
|
||||
'date_planned_finished': False,
|
||||
@@ -167,8 +164,9 @@ class ResMrpWorkOrder(models.Model):
|
||||
'target': 'new',
|
||||
'domain': [('workorder_id', '=', self.id)]
|
||||
}
|
||||
|
||||
tray_id = fields.Many2one('sf.tray', string="托盘信息")
|
||||
# 扫码绑定托盘方法
|
||||
|
||||
def gettray(self):
|
||||
if self.tray_code != False:
|
||||
values = self.env['sf.tray'].search([("code", "=", self.tray_code)])
|
||||
@@ -183,10 +181,11 @@ class ResMrpWorkOrder(models.Model):
|
||||
'production_id': self.production_id,
|
||||
'state': '占用',
|
||||
})
|
||||
self.tray_id = values
|
||||
else:
|
||||
raise UserError('该托盘编码已失效')
|
||||
else:
|
||||
return ""
|
||||
raise UserError('托盘码不能为空')
|
||||
|
||||
def gettray_auto(self, barcode):
|
||||
if barcode != False:
|
||||
@@ -205,15 +204,45 @@ class ResMrpWorkOrder(models.Model):
|
||||
else:
|
||||
raise UserError('该托盘编码已失效')
|
||||
else:
|
||||
return ""
|
||||
return {
|
||||
'name': _('New Maintenance Request'),
|
||||
'view_mode': 'form',
|
||||
'res_model': 'maintenance.request',
|
||||
'type': 'ir.actions.act_window',
|
||||
'context': {
|
||||
'default_company_id': self.company_id.id,
|
||||
'default_production_id': self.id,
|
||||
},
|
||||
'domain': [('production_id', '=', self.id)],
|
||||
}
|
||||
|
||||
# 解除托盘绑定
|
||||
def unbindtray(self):
|
||||
tray = self.env['sf.tray'].search([("production_id", "=", self.production_id.id)])
|
||||
if tray:
|
||||
tray.unclamp()
|
||||
return {
|
||||
'name': _("工单"),
|
||||
'view_mode': 'form',
|
||||
'res_model': 'mrp.workorder',
|
||||
'res_id': self.id,
|
||||
'type': 'ir.actions.act_window',
|
||||
'target': 'new'
|
||||
}
|
||||
|
||||
return ""
|
||||
# return {
|
||||
# 'name': _('New Maintenance Request'),
|
||||
# 'view_mode': 'form',
|
||||
# 'res_model': 'maintenance.request',
|
||||
# 'res_id':self.id,
|
||||
# 'type': 'ir.actions.act_window',
|
||||
# 'context': {
|
||||
# 'default_company_id': self.company_id.id,
|
||||
# 'default_production_id': self.id,
|
||||
# },
|
||||
# 'domain': [('production_id', '=', self.id)],
|
||||
# 'target':'new'
|
||||
# }
|
||||
|
||||
def recreateManufacturingOrWorkerOrder(self):
|
||||
"""
|
||||
@@ -256,74 +285,45 @@ class ResMrpWorkOrder(models.Model):
|
||||
self.env['stock.move'].sudo().create(productions._get_moves_finished_values())
|
||||
productions._create_workorder2(self.processing_panel)
|
||||
else:
|
||||
return True
|
||||
|
||||
# def fetchCNCing(self):
|
||||
# return None
|
||||
return {
|
||||
'name': _("工单"),
|
||||
'view_mode': 'form',
|
||||
'res_model': 'mrp.workorder',
|
||||
'res_id': self.id,
|
||||
'type': 'ir.actions.act_window',
|
||||
}
|
||||
|
||||
# cnc程序获取
|
||||
# cnc程序获取
|
||||
def fetchCNC(self):
|
||||
try:
|
||||
cnc = self.env['mrp.workorder'].search(
|
||||
[('routing_type', '=', 'CNC加工'), ('production_id', '=', self.production_id.id)], limit=1)
|
||||
logging.info('fetchCNC-cnc:%s' % cnc)
|
||||
# if cnc.product_id.upload_model_file:
|
||||
# logging.info('fetchCNC-upload_model_file:%s' % cnc.product_id.upload_model_file)
|
||||
# attachments = cnc.product_id.upload_model_file[0]
|
||||
# logging.info('fetchCNC-attachment1:%s' % attachments)
|
||||
# logging.info('fetchCNC-attachment1:%s' % cnc.product_id.upload_model_file[0])
|
||||
# logging.info('fetchCNC-attachment2:%s' % cnc.product_id.upload_model_file[0].datas)
|
||||
# logging.info('fetchCNC-attachment:%s' % attachments.datas)
|
||||
# base64_data = base64.b64encode(attachments.datas)
|
||||
# logging.info('fetchCNC-attachment1:%s' % attachments)
|
||||
# base64_datas = base64_data.decode('utf-8')
|
||||
# model_code = hashlib.sha1(base64_datas.encode('utf-8')).hexdigest()
|
||||
# logging.info('fetchCNC-model_code:%s' % model_code)
|
||||
logging.info('fetchCNC-model_code1:%s' % cnc.product_id.model_code)
|
||||
res = {'model_code': '' if not cnc.product_id.model_code else cnc.product_id.model_code,
|
||||
'production_no': self.production_id.name,
|
||||
'machine_tool_code': cnc.workcenter_id.machine_tool_id.code,
|
||||
'material_code': cnc.env['sf.production.materials'].search(
|
||||
[('id', '=', cnc.product_id.materials_id.id)]).materials_no,
|
||||
'material_type_code': cnc.env['sf.materials.model'].search(
|
||||
[('id', '=', cnc.product_id.materials_type_id.id)]).materials_no,
|
||||
'machining_processing_panel': cnc.product_id.model_processing_panel,
|
||||
# 'machining_precision': cnc.product_id.model_machining_precision,
|
||||
'embryo_long': cnc.product_id.bom_ids.bom_line_ids.product_id.length,
|
||||
'embryo_height': cnc.product_id.bom_ids.bom_line_ids.product_id.height,
|
||||
'embryo_width': cnc.product_id.bom_ids.bom_line_ids.product_id.width,
|
||||
'order_no': cnc.production_id.origin,
|
||||
'user': self.env.user.name,
|
||||
'model_file': '' if not cnc.product_id.model_file else base64.b64encode(
|
||||
cnc.product_id.model_file).decode('utf-8')
|
||||
}
|
||||
logging.info('res:%s' % res)
|
||||
configsettings = self.env['res.config.settings'].get_values()
|
||||
config_header = Common.get_headers(self, configsettings['token'], configsettings['sf_secret_key'])
|
||||
url = '/api/intelligent_programming/create'
|
||||
config_url = configsettings['sf_url'] + url
|
||||
# res_str = json.dumps(res)
|
||||
ret = requests.post(config_url, json={}, data=res, headers=config_header)
|
||||
ret = ret.json()
|
||||
logging.info('fetchCNC-ret:%s' % ret)
|
||||
if ret['status'] == 1:
|
||||
self.write(
|
||||
{'programming_no': ret['programming_no'], 'programming_state': '编程中', 'work_state': '编程中'})
|
||||
else:
|
||||
logging.info('fetchCNC-error:%s' % cnc)
|
||||
raise UserError(ret['message'])
|
||||
except Exception as e:
|
||||
logging.info('fetchCNC error:%s' % e)
|
||||
raise UserError(e)
|
||||
|
||||
# return {
|
||||
# 'name': _("工单"),
|
||||
# 'view_mode': 'form',
|
||||
# 'res_model': 'mrp.workorder',
|
||||
# 'res_id': self.id,
|
||||
# 'type': 'ir.actions.act_window',
|
||||
# 'target': 'new'
|
||||
# }
|
||||
cnc = self.env['mrp.workorder'].search(
|
||||
[('routing_type', '=', 'CNC加工'), ('production_id', '=', self.production_id.id)], limit=1)
|
||||
res = {'model_code': cnc.product_id.barcode, 'production_no': self.production_id.name,
|
||||
'machine_tool_code': cnc.workcenter_id.machine_tool_id.code,
|
||||
'material_code': cnc.env['sf.production.materials'].search(
|
||||
[('id', '=', cnc.product_id.materials_id.id)]).materials_no,
|
||||
'material_type_code': cnc.env['sf.materials.model'].search(
|
||||
[('id', '=', cnc.product_id.materials_type_id.id)]).materials_no,
|
||||
'machining_precision': cnc.product_id.model_machining_precision,
|
||||
'embryo_long': cnc.product_id.bom_ids.bom_line_ids.product_id.length,
|
||||
'embryo_height': cnc.product_id.bom_ids.bom_line_ids.product_id.height,
|
||||
'embryo_width': cnc.product_id.bom_ids.bom_line_ids.product_id.width,
|
||||
'order_no': cnc.production_id.origin,
|
||||
'user': self.env.user.name,
|
||||
'model_file': '' if not cnc.product_id.model_file else base64.b64encode(
|
||||
cnc.product_id.model_file).decode('utf-8')
|
||||
}
|
||||
logging.info('res:%s' % res)
|
||||
configsettings = self.env['res.config.settings'].get_values()
|
||||
config_header = Common.get_headers(self, configsettings['token'], configsettings['sf_secret_key'])
|
||||
url = '/api/intelligent_programming/create'
|
||||
config_url = configsettings['sf_url'] + url
|
||||
# res_str = json.dumps(res)
|
||||
ret = requests.post(config_url, json={}, data=res, headers=config_header)
|
||||
ret = ret.json()
|
||||
if ret['status'] == 1:
|
||||
return self.write({'programming_no': ret['programming_no'], 'is_programming': True})
|
||||
|
||||
def json_workorder_str1(self, k, production, route):
|
||||
workorders_values_str = [0, '', {
|
||||
@@ -333,7 +333,6 @@ class ResMrpWorkOrder(models.Model):
|
||||
'name': route.route_workcenter_id.name,
|
||||
'processing_panel': k,
|
||||
'routing_type': route.routing_type,
|
||||
'work_state': '' if not route.routing_type == '获取CNC加工程序' else '待发起',
|
||||
'workcenter_id': self.env['mrp.routing.workcenter'].get_workcenter(route.workcenter_ids.ids),
|
||||
'date_planned_start': False,
|
||||
'date_planned_finished': False,
|
||||
@@ -412,86 +411,70 @@ class CNCprocessing(models.Model):
|
||||
workorder_id = fields.Many2one('mrp.workorder', string="工单")
|
||||
|
||||
# mrs下发编程单创建CNC加工
|
||||
def cnc_processing_create(self, cnc_workorder, ret):
|
||||
logging.info('ret:%s' % ret)
|
||||
for obj in ret['programming_list']:
|
||||
workorder = self.env['mrp.workorder'].search([('production_id.name', '=', ret['production_order_no']),
|
||||
('processing_panel', '=', obj['processing_panel']),
|
||||
('routing_type', '=', 'CNC加工')])
|
||||
cnc_processing = self.env['sf.cnc.processing'].create({
|
||||
'workorder_id': workorder.id,
|
||||
'sequence_number': obj['sequence_number'],
|
||||
'program_name': obj['program_name'],
|
||||
'cutting_tool_name': obj['cutting_tool_name'],
|
||||
'cutting_tool_no': obj['cutting_tool_no'],
|
||||
'processing_type': obj['processing_type'],
|
||||
'margin_x_y': obj['margin_x_y'],
|
||||
'margin_z': obj['margin_z'],
|
||||
'depth_of_processing_z': obj['depth_of_processing_z'],
|
||||
'cutting_tool_extension_length': obj['cutting_tool_extension_length'],
|
||||
'cutting_tool_handle_type': obj['cutting_tool_handle_type'],
|
||||
'estimated_processing_time': obj['estimated_processing_time'],
|
||||
'remark': obj['remark']
|
||||
})
|
||||
self.get_cnc_processing_file(ret['folder_name'], cnc_processing, workorder.processing_panel)
|
||||
cnc_workorder.state = 'done'
|
||||
cnc_workorder.work_state = '已编程'
|
||||
cnc_workorder.programming_state = '已编程'
|
||||
cnc_workorder.time_ids.date_end = datetime.now()
|
||||
cnc_workorder.button_finish()
|
||||
|
||||
def get_cnc_processing_file(self, folder_name, cnc_processing, processing_panel):
|
||||
logging.info('folder_name:%s' % folder_name)
|
||||
serverdir = os.path.join('/tmp', folder_name, 'return', processing_panel)
|
||||
logging.info('serverdir:%s' % serverdir)
|
||||
for root, dirs, files in os.walk(serverdir):
|
||||
for f in files:
|
||||
logging.info('f:%s' % f)
|
||||
if os.path.splitext(f)[1] == ".pdf":
|
||||
full_path = os.path.join(serverdir, root, f)
|
||||
logging.info('pdf:%s' % full_path)
|
||||
if full_path != False:
|
||||
if not cnc_processing.workorder_id.cnc_worksheet:
|
||||
cnc_processing.workorder_id.cnc_worksheet = base64.b64encode(
|
||||
open(full_path, 'rb').read())
|
||||
else:
|
||||
if cnc_processing.program_name == f.split('.')[0]:
|
||||
cnc_file_path = os.path.join(serverdir, root, f)
|
||||
logging.info('cnc_file_path:%s' % cnc_file_path)
|
||||
self.write_file(cnc_file_path, cnc_processing)
|
||||
def cnc_processing_create(self, obj):
|
||||
workorder = self.env['mrp.workorder'].search([('production_id.name', '=', obj['production_order_no']),
|
||||
('processing_panel', '=', obj['processing_panel']),
|
||||
('routing_type', '=', 'CNC加工')])
|
||||
vals = {
|
||||
'workorder_id': workorder.id,
|
||||
'sequence_number': obj['sequence_number'],
|
||||
'program_name': obj['program_name'],
|
||||
'cutting_tool_name': obj['cutting_tool_name'],
|
||||
'cutting_tool_no': obj['cutting_tool_no'],
|
||||
'processing_type': obj['processing_type'],
|
||||
'margin_x_y': obj['margin_x_y'],
|
||||
'margin_z': obj['margin_z'],
|
||||
'depth_of_processing_z': obj['depth_of_processing_z'],
|
||||
'cutting_tool_extension_length': obj['cutting_tool_extension_length'],
|
||||
'cutting_tool_handle_type': obj['cutting_tool_handle_type'],
|
||||
'estimated_processing_time': obj['estimated_processing_time'],
|
||||
'remark': obj['remark']
|
||||
}
|
||||
return self.env['sf.cnc.processing'].create(vals)
|
||||
|
||||
# 创建附件(nc文件)
|
||||
def attachment_create(self, name, data):
|
||||
attachment = self.env['ir.attachment'].create({
|
||||
'datas': base64.b64encode(data),
|
||||
'type': 'binary',
|
||||
'public': True,
|
||||
'description': '程序文件',
|
||||
'name': name
|
||||
})
|
||||
return attachment
|
||||
|
||||
# 将FTP的nc文件下载到临时目录
|
||||
def download_file_tmp(self, production_no, processing_panel):
|
||||
remotepath = os.path.join('/', production_no, 'return', processing_panel)
|
||||
serverdir = os.path.join('/tmp', production_no, 'return', processing_panel)
|
||||
ftp_resconfig = self.env['res.config.settings'].get_values()
|
||||
ftp = FtpController(str(ftp_resconfig['ftp_host']), int(ftp_resconfig['ftp_port']), ftp_resconfig['ftp_user'],
|
||||
ftp_resconfig['ftp_password'])
|
||||
download_state = ftp.download_file_tree(remotepath, serverdir)
|
||||
return download_state
|
||||
def download_file_tmp(self, model_code, processing_panel):
|
||||
remotepath = os.path.join('/', model_code, 'return', processing_panel)
|
||||
serverdir = os.path.join('/tmp', model_code, 'return', processing_panel)
|
||||
ftp = FtpController()
|
||||
ftp.download_file_tree(remotepath, serverdir)
|
||||
return serverdir
|
||||
|
||||
# 将nc文件存到attach的datas里
|
||||
def write_file(self, nc_file_path, cnc):
|
||||
if os.path.exists(nc_file_path):
|
||||
with open(nc_file_path, 'rb') as file:
|
||||
data_bytes = file.read()
|
||||
attachment = self.attachment_create(cnc.program_name, data_bytes)
|
||||
cnc.write({'cnc_id': attachment.id})
|
||||
file.close()
|
||||
attachment = self.attachment_create(cnc.program_name + '.NC', data_bytes)
|
||||
cnc.write({'cnc_id': attachment.id})
|
||||
file.close()
|
||||
else:
|
||||
return False
|
||||
|
||||
# 将nc文件对应的excel清单转为pdf
|
||||
# def to_pdf(self, excel_path, pdf_path):
|
||||
# """
|
||||
# 需要在linux中下载好libreoffice
|
||||
# """
|
||||
# logging.info('pdf_path:%s' % pdf_path)
|
||||
# logging.info('pdf_path:%s' % excel_path)
|
||||
# # 注意cmd中的libreoffice要和linux中安装的一致
|
||||
# cmd = 'soffice --headless --convert-to pdf'.split() + [excel_path] + ['--outdir'] + [pdf_path]
|
||||
# p = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1)
|
||||
# # p.wait(timeout=30) # 停顿30秒等待转化
|
||||
# # stdout, stderr = p.communicate()
|
||||
# p.communicate()
|
||||
|
||||
|
||||
class SfWorkOrderBarcodes(models.Model):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user