This commit is contained in:
gqh
2023-01-09 23:04:53 +08:00
parent d0497aa043
commit 311c7c723a
52 changed files with 1572 additions and 331 deletions

View File

@@ -3,9 +3,10 @@ from odoo.exceptions import ValidationError
import logging
import base64
import os
from OCC.Extend.DataExchange import read_step_file, write_stl_file
# from OCC.Extend.DataExchange import read_step_file, write_stl_file
from odoo.modules import get_resource_path
class ResProductTemplate(models.Model):
_inherit = 'product.template'
@@ -34,6 +35,15 @@ class ResProductTemplate(models.Model):
materials_type_id = fields.Many2one('sf.materials.model', string='材料型号')
single_manufacturing = fields.Boolean(string="单个制造")
upload_model_file = fields.Many2many('ir.attachment', 'upload_model_file_attachment_ref', string='上传模型文件')
model_code = fields.Char('模型编码')
def _get_volume_uom_id_from_ir_config_parameter(self):
product_length_in_feet_param = self.env['ir.config_parameter'].sudo().get_param('product.volume_in_cubic_feet')
if product_length_in_feet_param == '1':
return self.env.ref('uom.product_uom_cubic_foot')
else:
return self.env.ref('sf_dlm.product_uom_cubic_millimeter')
# model_file = fields.Binary('模型文件')
# 胚料的库存路线设置
@@ -72,16 +82,18 @@ class ResProductTemplate(models.Model):
item['model_height'] + model_type.embryo_tolerance),
'model_type_id': 1,
# 'model_machining_precision': item['model_machining_precision'],
'model_processing_panel': 'A',
'model_processing_panel': 'R',
'model_machining_precision': '±0.10mm',
'model_code': item['barcode'],
'length': item['model_long'],
'width': item['model_width'],
'height': item['model_height'],
'volume': item['model_long'] * item['model_width'] * item['model_height'],
'model_file': base64.b64decode(item['model_file']),
'model_file': '' if not item['model_file'] else base64.b64decode(item['model_file']),
'model_name': attachment.name,
'upload_model_file': [(6, 0, [attachment.id])],
# 'single_manufacturing': True,
'tracking': 'serial',
'list_price': item['price'],
# 'categ_id': self.env.ref('sf_dlm.product_category_finished_sf').id,
'materials_id': self.env['sf.production.materials'].search(
@@ -94,18 +106,17 @@ class ResProductTemplate(models.Model):
# [('process_encode', '=', item['process_parameters_code'])]).id,
'model_remark': item['remark'],
'default_code': '%s-%s' % (order_number, i),
#'barcode': item['barcode'],
# 'barcode': item['barcode'],
'active': True,
# 'route_ids': self._get_routes('')
}
copy_product_id.sudo().write(vals)
print(len(copy_product_id.model_file))
product_id.product_tmpl_id.active = False
# product_id.product_tmpl_id.active = False
return copy_product_id
def attachment_create(self, name, data):
attachment = self.env['ir.attachment'].create({
'datas': base64.b64decode(data),
'datas': base64.b64decode(data),
'type': 'binary',
'description': '模型文件',
'name': name
@@ -113,7 +124,7 @@ class ResProductTemplate(models.Model):
return attachment
# 创建胚料
def no_bom_product_create(self, product_id, item, order_id, route_type):
def no_bom_product_create(self, product_id, item, order_id, route_type, i):
no_bom_copy_product_id = product_id.with_user(self.env.ref("base.user_admin")).copy()
no_bom_copy_product_id.product_tmpl_id.active = True
materials_id = self.env['sf.production.materials'].search(
@@ -124,9 +135,10 @@ class ResProductTemplate(models.Model):
supplier = self.env['mrp.bom'].get_supplier(materials_type_id)
logging.info('no_bom_copy_product_supplier-vals:%s' % supplier)
vals = {
'name': '%s %s %s %s * %s * %s' % (
order_id.name, materials_id.name, materials_type_id.name, item['model_long'], item['model_width'],
item['model_height']),
'name': '%s-%s %s %s %s * %s * %s' % (
order_id.name, i, materials_id.name, materials_type_id.name,
item['model_long'] + model_type.embryo_tolerance, item['model_width'] + model_type.embryo_tolerance,
item['model_height'] + model_type.embryo_tolerance),
'length': item['model_long'] + model_type.embryo_tolerance,
'width': item['model_width'] + model_type.embryo_tolerance,
'height': item['model_height'] + model_type.embryo_tolerance,
@@ -158,32 +170,32 @@ class ResProductTemplate(models.Model):
logging.info('no_bom_copy_product_id-seller_ids-vals:%s' % no_bom_copy_product_id.seller_ids)
no_bom_copy_product_id.write(vals)
logging.info('no_bom_copy_product_id-vals:%s' % vals)
product_id.product_tmpl_id.active = False
# product_id.product_tmpl_id.active = False
return no_bom_copy_product_id
@api.onchange('upload_model_file')
def onchange_model_file(self):
for item in self:
if len(item.upload_model_file) > 1:
raise ValidationError('只允许上传一个文件')
if item.upload_model_file:
file_attachment_id = item.upload_model_file[0]
item.model_name = file_attachment_id.name
# 附件路径
report_path = file_attachment_id._full_path(file_attachment_id.store_fname)
shapes = read_step_file(report_path)
output_file = get_resource_path('sf_dlm', 'static/file', 'out.stl')
write_stl_file(shapes, output_file, 'binary', 0.03, 0.5)
# 转化为glb
output_glb_file = get_resource_path('sf_dlm', 'static/file', 'out.glb')
util_path = get_resource_path('sf_dlm', 'static/util')
cmd = 'python %s/stl2gltf.py %s %s -b' % (util_path, output_file, output_glb_file)
os.system(cmd)
# 转base64
with open(output_glb_file, 'rb') as fileObj:
image_data = fileObj.read()
base64_data = base64.b64encode(image_data)
item.model_file = base64_data
# @api.onchange('upload_model_file')
# def onchange_model_file(self):
# for item in self:
# if len(item.upload_model_file) > 1:
# raise ValidationError('只允许上传一个文件')
# if item.upload_model_file:
# file_attachment_id = item.upload_model_file[0]
# item.model_name = file_attachment_id.name
# # 附件路径
# report_path = file_attachment_id._full_path(file_attachment_id.store_fname)
# shapes = read_step_file(report_path)
# output_file = get_resource_path('sf_dlm', 'static/file', 'out.stl')
# write_stl_file(shapes, output_file, 'binary', 0.03, 0.5)
# # 转化为glb
# output_glb_file = get_resource_path('sf_dlm', 'static/file', 'out.glb')
# util_path = get_resource_path('sf_dlm', 'static/util')
# cmd = 'python %s/stl2gltf.py %s %s -b' % (util_path, output_file, output_glb_file)
# os.system(cmd)
# # 转base64
# with open(output_glb_file, 'rb') as fileObj:
# image_data = fileObj.read()
# base64_data = base64.b64encode(image_data)
# item.model_file = base64_data
class ResMrpBom(models.Model):
@@ -244,8 +256,6 @@ class ResMrpBom(models.Model):
# 匹配bom
def get_bom(self, product):
logging.info('get_bom-product:%s' % product)
logging.info('get_bom-product:%s' % product.materials_type_id.id)
embryo_has = self.env['product.product'].search(
[('categ_id.type', '=', '胚料'), ('materials_type_id', '=', product.materials_type_id.id),
('length', '>', product.length), ('width', '>', product.width),
@@ -257,7 +267,7 @@ class ResMrpBom(models.Model):
logging.info('get_bom-vals:%s' % embryo_has)
if embryo_has:
rate_of_waste = ((embryo_has.volume - product.model_volume) % embryo_has.volume) * 100
if rate_of_waste >= 20:
if rate_of_waste <= 20:
return embryo_has
else:
return