from odoo import models, fields, api from odoo.modules import get_resource_path # from OCC.Extend.DataExchange import read_step_file # from OCC.Extend.DataExchange import write_stl_file from odoo.addons.sf_base.commons.common import Common from odoo.exceptions import ValidationError import requests import json import logging import base64 import hashlib class FixtureMaterial(models.Model): _name = 'sf.fixture.material' _description = "夹具物料" code = fields.Char(string='编码') name = fields.Char(string="名称", size=15) category = fields.Selection([('零点夹具', '零点夹具')], string="类别") remark = fields.Char(string="备注") active = fields.Boolean(string="有效", default=True) class Multi_MountingType(models.Model): _name = 'sf.multi_mounting.type' _description = "联装类型" code = fields.Char(string='编码') name = fields.Char(string="名称", size=15) remark = fields.Char(string="备注") active = fields.Boolean(string="有效", default=True) class FixtureModel(models.Model): _name = 'sf.fixture.model' _description = "夹具型号" name = fields.Char(string="名称", size=15) fixture_material_id = fields.Many2one('sf.fixture.material', string="夹具物料", ) fixture_material_type = fields.Char(string="夹具物料类型", related='fixture_material_id.name', store=True) multi_mounting_type_id = fields.Many2one('sf.multi_mounting.type', string="联装类型") brand_id = fields.Many2one('sf.machine.brand', string="品牌") manufacturer_model_number = fields.Char(string="厂家型号") clamping_way = fields.Char(string="装夹方式") port_type = fields.Char(string="接口类型") model_file = fields.Binary(string="3D模型图") length = fields.Char(string="长度[mm]", size=6) width = fields.Char(string="宽度[mm]", size=6) height = fields.Char(string="高度[mm]", size=6) weight = fields.Char(string="重量[kg]", size=4) clamp_workpiece_length_max = fields.Integer(string="夹持工件长度MAX[mm]", size=6) clamp_workpiece_width_max = fields.Integer(string="夹持工件宽度MAX[mm]", size=6) clamp_workpiece_height_max = fields.Integer(string="夹持工件高度MAX[mm]", size=6) clamp_workpiece_diameter_max = fields.Float(string="夹持工件直径MAX[mm]", size=6) maximum_carrying_weight = fields.Float(string="最大承载重量[kg]", size=4) maximum_clamping_force = fields.Integer(string="最大夹持力[n]", size=8) materials_model_id = fields.Many2one('sf.materials.model', string="材料型号") driving_way = fields.Selection([('气动', '气动'), ('液压', '液压'), ('机械', '机械')], string="驱动方式") apply_machine_tool_type_ids = fields.Many2many('sf.machine_tool.type', 'rel_fixture_model_machine_tool_type', string="适用机床型号") through_hole_size = fields.Integer(string="过孔大小[mm]", size=6) screw_size = fields.Integer(string="螺牙大小[mm]", size=6) upload_model_file = fields.Many2many('ir.attachment', 'upload_fixture_model_file_attachment_ref', string='上传模型文件') def _get_code(self, fixture_model_type_code): fixture_model = self.env['sf.fixture.model'].sudo().search( [('code', 'ilike', fixture_model_type_code)], limit=1, order="id desc") if not fixture_model: num = "%03d" % 1 else: m = int(fixture_model.code[-3:]) + 1 num = "%03d" % m return "%s%s" % (fixture_model_type_code, num) code = fields.Char(string='编码', readonly=True) @api.model def create(self, vals): if vals.get('upload_model_file'): logging.info('create-attachment:%s' % vals['upload_model_file'][0]) for item in vals['upload_model_file']: print(len(item[2])) if len(item[2]) > 0: logging.info('create-attachment:%s' % int(item[2][0])) 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) logging.info('create-model_file:%s' % len(vals['model_file'])) obj = super(FixtureModel, self).create(vals) return obj # 将attach的datas内容转为glb文件 def transition_glb_file(self, report_path, model_code): shapes = read_step_file(report_path) # output_file = os.path.join('C:/Users/43484/Desktop/机企猫工作文档', str(model_code) + '.stl') # output_file = os.path.join('/tmp', str(model_code) + '.stl') # write_stl_file(shapes, output_file, 'binary', 0.03, 0.5) # # 转化为glb # # output_glb_file = os.path.join('C:/Users/43484/Desktop/机企猫工作文档', str(model_code) + '.glb') # output_glb_file = os.path.join('/tmp', str(model_code) + '.glb') # util_path = get_resource_path('mrs_base', '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 return False @api.onchange('brand_id') def _onchange_brand_id(self): if self.brand_id: self.manufacturer_model_number = self.brand_id.manufacturer_model_number def name_get(self): result = [] for parameter in self: if parameter.fixture_material_type in ['虎钳托盘', '零点卡盘']: if parameter.driving_way: name = parameter.multi_mounting_type_id.name + '-' + parameter.driving_way + '-' + parameter.name else: name = parameter.multi_mounting_type_id.name + '-' + parameter.name else: name = parameter.multi_mounting_type_id.name + '-' + parameter.name result.append((parameter.id, name)) return result @api.onchange('fixture_material_id') def _onchange_fixture_material_id(self): if self.fixture_material_id: if self.fixture_material_id.name == "气动托盘": self.code = self._get_code("JKM-C-JJWL-QDTP-") elif self.fixture_material_id.name == "转接板(锁板)托盘": self.code = self._get_code("JKM-C-JJWL-ZJBTP-") elif self.fixture_material_id.name == "磁吸托盘": self.code = self._get_code("JKM-C-JJWL-CXTP-") elif self.fixture_material_id.name == "虎钳托盘": self.code = self._get_code("JKM-C-JJWL-HQTP-") else: self.code = self._get_code("JKM-C-JJWL-LDKP-") @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] # 附件路径 report_path = file_attachment_id._full_path(file_attachment_id.store_fname) logging.info("模型路径: %s" % report_path) 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() logging.info("模型编码: %s" % model_code) item.model_file = self.transition_glb_file(report_path, model_code) else: item.model_file = False def _json_apply_machine_tool_type_item_code(self, item): code_arr = [] for i in item.apply_machine_tool_type_ids: code_arr.append(i.code) return code_arr # 注册到cloud的夹具型号中 def register_fixture_model(self): sf_sync_config = self.env['res.config.settings'].get_values() token = sf_sync_config['token'] sf_secret_key = sf_sync_config['sf_secret_key'] headers = Common.get_headers(self, token, sf_secret_key) strurl = sf_sync_config['sf_url'] + self.crea_url objs_all = self.search([('code', '=', self.code)]) fixture_model_list = [] if objs_all: for item in objs_all: val = { 'code': item.code, 'factory_token': token, 'name': item.name, 'code': item.code, 'fixture_material_code': self.env['sf.fixture.material'].search( [('id', '=', item.fixture_material_id.id)]).code, 'multi_mounting_type_code': self.env['sf.multi_mounting.type'].search( [('id', '=', item.multi_mounting_type_id.id)]).code, 'brand_code': self.env['sf.machine.brand'].search( [('id', '=', item.brand_id.id)]).code, 'manufacturer_model_number': item.manufacturer_model_number, 'clamping_way': item.clamping_way, 'materials_model_code': self.env['sf.materials.model'].search( [('id', '=', item.materials_model_id.id)]).code, 'apply_machine_tool_type_code': self.env['sf.machine_tool.type'].search( [('id', '=', item.type_id.id)]).code, 'port_type': item.port_type, 'model_file': item.model_file, 'length': item.length, 'width': item.width, 'height': item.height, 'weight': item.weight, 'clamp_workpiece_length_max': item.state, 'clamp_workpiece_width_max': item.clamp_workpiece_width_max, 'clamp_workpiece_height_max': item.clamp_workpiece_height_max, 'clamp_workpiece_diameter_max': item.clamp_workpiece_diameter_max, 'maximum_carrying_weight': item.maximum_carrying_weight, 'maximum_clamping_force': item.maximum_clamping_force, 'driving_way': item.driving_way, 'through_hole_size': item.through_hole_size, 'screw_size': item.screw_size, } fixture_model_list.append(val) # kw = machine_tool_list kw = json.dumps(fixture_model_list, ensure_ascii=False) r = requests.post(strurl, json={}, data={'kw': kw, 'token': token}, headers=headers) ret = r.json() self.code = ret['message'] self.state_zc = "已注册" if r == 200: return "注册成功" else: raise ValidationError("没有夹具型号注册信息")