diff --git a/sf_dlm/models/product_template.py b/sf_dlm/models/product_template.py index 1e37be2b..9dbfed49 100644 --- a/sf_dlm/models/product_template.py +++ b/sf_dlm/models/product_template.py @@ -1,6 +1,12 @@ -from odoo import models, fields +from odoo import models, fields, api +from odoo.exceptions import ValidationError +from odoo.modules import get_resource_path +from OCC.Extend.DataExchange import read_step_file +from OCC.Extend.DataExchange import write_stl_file import logging import base64 +import hashlib +import os class ResProductTemplate(models.Model): @@ -9,7 +15,8 @@ class ResProductTemplate(models.Model): # 模型的长,宽,高,体积,精度,材料 model_name = fields.Char('模型名称') categ_type = fields.Selection( - [("成品", "成品"), ("胚料", "胚料"), ("原材料", "原材料")], string='产品的类别', related='categ_id.type', store=True) + [("成品", "成品"), ("胚料", "胚料"), ("原材料", "原材料")], string='产品的类别', related='categ_id.type', + store=True) model_long = fields.Float('模型长[mm]', digits=(16, 3)) model_width = fields.Float('模型宽[mm]', digits=(16, 3)) model_height = fields.Float('模型高[mm]', digits=(16, 3)) @@ -172,29 +179,65 @@ class ResProductTemplate(models.Model): # 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.model_create_multi + def create(self, vals_list): + for vals in vals_list: + if vals['upload_model_file']: + for item in vals['upload_model_file']: + attachment = self.env['ir.attachment'].sudo().search([('id', '=', int(item[2][0]))]) + base64_data = base64.b64encode(attachment.datas) + base64_datas = base64_data.decode('utf-8') + model_code = hashlib.sha1(base64_datas.encode('utf-8')).hexdigest() + report_path = attachment._full_path(attachment.store_fname) + vals['model_file'] = self.transition_glb_file(report_path, model_code) + self._sanitize_vals(vals) + templates = super(ResProductTemplate, self).create(vals_list) + if "create_product_product" not in self._context: + templates._create_variant_ids() + + # This is needed to set given values to first variant after creation + for template, vals in zip(templates, vals_list): + related_vals = {} + for field_name in self._get_related_fields_variant_template(): + if vals.get(field_name): + related_vals[field_name] = vals[field_name] + if related_vals: + template.write(related_vals) + + return templates + + @api.onchange('upload_model_file') + def onchange_model_file(self): + for item in self: + if item.upload_model_file: + if len(item.upload_model_file) > 1: + raise ValidationError('只允许上传一个文件') + manufacturing_order = self.env['mrp.production'].search([('product_id', '=', self.id)]) + if manufacturing_order: + raise ValidationError('该产品已生成制造订单,无法进行修改') + file_attachment_id = item.upload_model_file[0] + # 附件路径 + report_path = file_attachment_id._full_path(file_attachment_id.store_fname) + base64_data = base64.b64encode(file_attachment_id.datas) + base64_datas = base64_data.decode('utf-8') + model_code = hashlib.sha1(base64_datas.encode('utf-8')).hexdigest() + item.model_file = self.transition_glb_file(report_path, model_code) + + # 将attach的datas内容转为glb文件 + def transition_glb_file(self,report_path, code): + shapes = read_step_file(report_path) + output_file = os.path.join('/tmp', str(code) + '.stl') + write_stl_file(shapes, output_file, 'binary', 0.03, 0.5) + # 转化为glb + output_glb_file = os.path.join('/tmp', str(code) + '.glb') + util_path = get_resource_path('sf_dlm', 'static/util') + cmd = 'python3 %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) + return base64_data class ResMrpBom(models.Model): diff --git a/sf_dlm/static/file/out.glb b/sf_dlm/static/file/out.glb deleted file mode 100644 index c4a6352b..00000000 Binary files a/sf_dlm/static/file/out.glb and /dev/null differ diff --git a/sf_dlm/static/file/out.stl b/sf_dlm/static/file/out.stl deleted file mode 100644 index 4f8b834a..00000000 Binary files a/sf_dlm/static/file/out.stl and /dev/null differ diff --git a/sf_dlm/views/product_template_view.xml b/sf_dlm/views/product_template_view.xml index ddc2704b..95648241 100644 --- a/sf_dlm/views/product_template_view.xml +++ b/sf_dlm/views/product_template_view.xml @@ -13,7 +13,9 @@ - +