1.快速订单新增特征识别路径字段

2.修复销售经理和销售总监看不到快速订单菜单
3.优化系统设置页面,新增特征识别路径展示,去掉重复的业务平台参数配置展示
4.快速订单去掉老版本的特征识别的代码及包,新增最新的特征识别方法
This commit is contained in:
jinling.yang
2024-02-20 17:33:06 +08:00
parent 4379854b89
commit 5646cc492e
11 changed files with 843 additions and 39 deletions

View File

@@ -2,17 +2,19 @@ import logging
import base64
import hashlib
import os
import platform
import json
from datetime import datetime
import requests
from OCC.Extend.DataExchange import read_step_file
from OCC.Extend.DataExchange import write_stl_file
from odoo import http
from odoo.http import request
# from OCC.Extend.DataExchange import read_step_file
# from OCC.Extend.DataExchange import write_stl_file
from odoo import models, fields, api
from odoo.modules import get_resource_path
from odoo.exceptions import ValidationError, UserError
from odoo.addons.sf_base.commons.common import Common
from . import parser_and_calculate_work_time as pc
@@ -77,11 +79,11 @@ class QuickEasyOrder(models.Model):
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)
# 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.model_analyze(attachment)
# logging.info('create-model_file:%s' % len(vals['model_file']))
obj = super(QuickEasyOrder, self).create(vals)
@@ -91,6 +93,147 @@ class QuickEasyOrder(models.Model):
obj.state = '待接单'
return obj
def model_analyze(self,model_attachment):
"""
step模型解析上传模型时转为web可显示的格式
:return:
"""
config = request.env['res.config.settings'].sudo().get_values()
try:
# 获取当前操作系统
os_name = platform.system()
for item in model_attachment:
# 将拿到的3D模型数据存入文件
# 定义文件名和文件的二进制内容
file_name = item.name # 请将这里替换为你的文件名
print('file_name', file_name)
# base64_data = base64.b64encode(item.datas)
# base64_datas = base64_data.decode('utf-8')
binary_content = item.datas # 请将这里替换为你的文件的二进制内容
# binary_content从字符串转为二进制
binary_content = base64.b64decode(binary_content)
# 定义新的文件夹路径
# 根据操作系统不同,文件路径不同
path_header = '/model_parser' if os_name == 'Linux' else 'D:/model_analysis'
# new_folder_path = 'D:/11111final' + '/' + item['name'].split(".")[0]
new_folder_path = path_header + '/' + item.name.rpartition('.')[0]
print('new_folder_path', new_folder_path)
# 检查新的文件夹是否存在,如果不存在,则创建
if not os.path.exists(new_folder_path):
os.makedirs(new_folder_path)
# 定义新的文件路径
new_file_path = os.path.join(new_folder_path, file_name)
# 将二进制内容写入新的文件
with open(new_file_path, 'wb') as f:
f.write(binary_content)
# 检查文件是否已经成功写入
if os.path.exists(new_file_path):
print(f'Successfully wrote binary content to {new_file_path}')
else:
print(f'Failed to write binary content to {new_file_path}')
# 附件
# attachment = request.env['ir.attachment'].sudo().create({
# 'datas': item['data'].encode('utf-8'),
# 'type': 'binary',
# 'description': '模型文件',
# 'name': item['name'],
# 'public': True,
# 'model_name': item['name'],
# })
headers = {'Content-Type': 'application/json'}
# 调用写入宿主机接口
# url_dir = 'http://192.168.50.202:8000/create_and_write_file'
url_dir = config['model_parser_url'] + '/create_and_write_file'
data = {
'folder_path': new_folder_path, # 您想要创建的文件夹路径
'file_path': new_file_path, # 您想要创建的文件名
'content': item['data'] # 您想要写入文件的内容
}
requests.post(url_dir, json=data, headers=headers)
# 调用特征包接口
url = config['model_parser_url'] + '/process_file'
payload = {
'file_path': new_file_path,
'dest_path': new_folder_path,
'back_url': config['bfm_url']
}
response = requests.post(url, json=payload, headers=headers)
if response.status_code == 200:
print("Request was successful.")
print("Response: ", response.json())
else:
print("Request failed.")
# 特征识别
xml_path = new_folder_path + '/' + item.name.rpartition('.')[0] + '_FeatrueTable.XML'
print('xml_path', xml_path)
parser_obj = pc.FeatureParser(xml_path)
print('parser_obj', parser_obj)
slot = parser_obj.slots
print('slot', slot)
hole = parser_obj.holes
print('hole', hole)
size = parser_obj.size
print('size', size)
open_slot = parser_obj.open_slots
print('open_slot', open_slot)
vector = parser_obj.vectors
print('vector', vector)
print('all parcer', size)
try:
hole_time = pc.hole_time(parser_obj)
print('hole_time', hole_time)
except Exception as e:
return json.dumps({'code': 400, 'msg': '孔尺寸超限', 'error_msg': str(e)})
try:
slot_time = pc.slot_time(parser_obj)
print('slot_time', slot_time)
except Exception as e:
return json.dumps({'code': 400, 'msg': '槽尺寸超限', 'error_msg': str(e)})
try:
open_slot_time = pc.open_slot_time(parser_obj)
print('open_slot_time', open_slot_time)
except Exception as e:
return json.dumps({'code': 400, 'msg': '开口槽尺寸超限', 'error_msg': str(e)})
total_time = hole_time + slot_time + open_slot_time
print(hole_time, slot_time, open_slot_time)
print('total_time', total_time)
ret = {'feature_infos': [{'name': 'all_feature', 'type': '', 'process_time': total_time}],
'boxshape': size, 'slugX': 10.0, 'slugY': 90.0, 'slugZ': 42.0,
'turn_over_times': 2,
'target_faces': ['A', 'B']}
self.model_feature = json.dumps(ret['feature_infos'], ensure_ascii=False)
self.model_length = size['length'] # 长 单位mm
self.model_width = size['width'] # 宽
self.model_height = size['height'] # 高
self.model_volume = size['length'] * size['width'] * size['height']
# 附件处理
base64_data = base64.b64encode(item.datas)
base64_datas = base64_data.decode('utf-8')
model_code = hashlib.sha1(base64_datas.encode('utf-8')).hexdigest()
# 读取文件
shapes = read_step_file(new_file_path)
output_file = os.path.join(new_folder_path, str(model_code) + '.stl')
write_stl_file(shapes, output_file, 'binary', 0.03, 0.5)
# 转化为glb
output_glb_file = os.path.join(new_folder_path, str(model_code) + '.glb')
util_path = get_resource_path('jikimo_gateway_api', 'static/util')
# 根据操作系统确定使用 'python' 还是 'python3'
python_cmd = 'python3' if os_name == 'Linux' else 'python'
print('python_cmd', python_cmd)
print('os_name', os_name)
# 使用引号包围路径
cmd = '%s "%s/stl2gltf.py" "%s" "%s" -b' % (python_cmd, util_path, output_file, output_glb_file)
logging.info(cmd)
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
except Exception as e:
return UserError('模型自动报价失败,请联系管理员')
# 将attach的datas内容转为glb文件
def transition_glb_file(self, report_path, model_code):
shapes = read_step_file(report_path)
@@ -116,24 +259,7 @@ class QuickEasyOrder(models.Model):
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)
ret = self.feature_recognition(report_path, model_code)
logging.info("自动报价返回值: %s" % ret)
boxshape = ret['boxshape'].tolist()
logging.info("自动报价boxshape: %s" % boxshape)
logging.info('自动报价feature_infos:%s' % ret['feature_infos'])
item.model_length = boxshape[0] # 长 单位mm
item.model_width = boxshape[1] # 宽
item.model_height = boxshape[2] # 高
item.model_volume = boxshape[0] * boxshape[1] * boxshape[2]
item.model_feature = json.dumps(ret['feature_infos'], ensure_ascii=False)
item.model_file = self.model_analyze(file_attachment_id)
self._get_price(item)
else:
item.model_file = False