产品和制造订单新增模型和occ查看模型模块

This commit is contained in:
jinling.yang
2022-12-30 10:00:17 +08:00
parent 5fcb17d048
commit a3e39d4fac
33 changed files with 1497 additions and 119 deletions

View File

@@ -1,12 +1,16 @@
from odoo import models, fields, api
from odoo.exceptions import ValidationError
import logging
import base64
import os
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'
# 模型的长,宽,高,体积,精度,材料
model_name = fields.Char('模型名称')
model_long = fields.Float('模型长[mm]', digits=(16, 3))
model_width = fields.Float('模型宽[mm]', digits=(16, 3))
model_height = fields.Float('模型高[mm]', digits=(16, 3))
@@ -29,7 +33,8 @@ class ResProductTemplate(models.Model):
materials_id = fields.Many2one('sf.production.materials', string='材料')
materials_type_id = fields.Many2one('sf.materials.model', string='材料型号')
single_manufacturing = fields.Boolean(string="单个制造")
model_id = fields.Many2one('ir.attachment', string='模型')
upload_model_file = fields.Many2many('ir.attachment', 'upload_model_file_attachment_ref', string='上传模型文件')
model_file = fields.Binary('模型文件')
# 胚料的库存路线设置
# def _get_routes(self, route_type):
@@ -56,6 +61,7 @@ class ResProductTemplate(models.Model):
copy_product_id = product_id.with_user(self.env.ref("base.user_admin")).copy()
copy_product_id.product_tmpl_id.active = True
model_type = self.env['sf.model.type'].search([], limit=1)
attachment = self.attachment_create(item['model_name'], item['model_data'])
vals = {
'name': '%s-%s' % (order_id.name, i),
'model_long': item['model_long'] + model_type.embryo_tolerance,
@@ -72,7 +78,9 @@ class ResProductTemplate(models.Model):
'width': item['model_width'],
'height': item['model_height'],
'volume': item['model_long'] * item['model_width'] * item['model_height'],
# 'model_price': item['price'],
'model_file': base64.b64decode(item['model_file']),
'model_name': attachment.name,
'upload_model_file': [(6, 0, [attachment.id])],
# 'single_manufacturing': True,
'list_price': item['price'],
# 'categ_id': self.env.ref('sf_dlm.product_category_finished_sf').id,
@@ -86,18 +94,18 @@ 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)
copy_product_id.model_id = self.attachment_create(item['model_name'], item['model_data'])
print(len(copy_product_id.model_file))
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.b64encode(data),
'datas': base64.b64decode(data),
'type': 'binary',
'description': '模型文件',
'name': name
@@ -153,19 +161,29 @@ class ResProductTemplate(models.Model):
product_id.product_tmpl_id.active = False
return no_bom_copy_product_id
# 根据模型类型默认给模型的长高宽加配置的长度;
@api.onchange('model_type_id')
def add_product_size(self):
if not self.model_type_id:
return
model_type = self.env['sf.model.type'].search(
[('id', '=', self.model_type_id.id)])
if model_type:
self.model_long = self.model_long + embryo_tolerance
self.model_width = self.model_width + embryo_tolerance
self.model_height = self.model_width + embryo_tolerance
else:
return
@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):