blenderpython/suw_constants.py

618 lines
18 KiB
Python
Raw Normal View History

2025-08-01 17:13:30 +08:00
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
SUWood 常量定义
翻译自: SUWConstants.rb
"""
import os
import logging
from pathlib import Path
from typing import Optional, Dict, Any
# 设置日志
logger = logging.getLogger(__name__)
# 检查Blender可用性
try:
import bpy
BLENDER_AVAILABLE = True
except ImportError:
BLENDER_AVAILABLE = False
# 辅助函数:获取选择状态信息
def _get_selection_info():
"""获取选择状态信息 - 替代SUWImpl的选择状态"""
try:
from .suw_core import get_selection_manager
selection_manager = get_selection_manager()
if selection_manager:
return {
'selected_uid': selection_manager.selected_uid(),
'selected_obj': selection_manager.selected_obj(),
'selected_zone': selection_manager.selected_zone(),
'selected_part': selection_manager.selected_part()
}
except ImportError:
pass
# 如果无法获取,返回默认值
return {
'selected_uid': None,
'selected_obj': None,
'selected_zone': None,
'selected_part': None
}
def _get_server_path():
"""获取服务器路径 - 替代SUWImpl.server_path"""
try:
from .suw_core import get_data_manager
data_manager = get_data_manager()
if data_manager and hasattr(data_manager, 'server_path'):
return data_manager.server_path
except ImportError:
pass
# 如果无法获取,返回默认路径
return os.path.dirname(__file__)
class SUWood:
"""SUWood 主要常量和功能类"""
# 场景操作常量
SUSceneNew = 1 # 清除之前的订单
SUSceneOpen = 2 # 清除之前的订单
SUSceneSave = 3
SUScenePrice = 4
# 单元操作常量
SUUnitPoint = 11
SUUnitFace = 12
SUUnitDelete = 13
SUUnitContour = 14
# 区域操作常量
SUZoneFront = 20
SUZoneDiv1 = 21
SUZoneResize = 22
SUZoneCombine = 23
SUZoneReplace = 24
SUZoneMaterial = 25
SUZoneHandle = 26
SUZoneCloth = 27
SUZoneLight = 28
# 空间位置常量
VSSpatialPos_F = 1 # 前
VSSpatialPos_K = 2 # 后
VSSpatialPos_L = 3 # 左
VSSpatialPos_R = 4 # 右
VSSpatialPos_B = 5 # 底
VSSpatialPos_T = 6 # 顶
# 单元轮廓常量
VSUnitCont_Zone = 1 # 区域轮廓
VSUnitCont_Part = 2 # 部件轮廓
VSUnitCont_Work = 3 # 挖洞轮廓
# 版本常量
V_Dealer = 1000
V_Machining = 1100
V_Division = 1200
V_PartCategory = 1300
V_Contour = 1400
V_Color = 1500
V_Profile = 1600
V_Surf = 1700
V_StretchPart = 1800
V_Material = 1900
V_Connection = 2000
V_HardwareSchema = 2050
V_HardwareSet = 2100
V_Hardware = 2200
V_Groove = 2300
V_DesignParam = 2400
V_ProfileSchema = 2500
V_StructPart = 2600
V_CraftPart = 2700
V_SeriesPart = 2800
V_Drawer = 2900
V_DesignTemplate = 3000
V_PriceTemplate = 3100
V_MachineCut = 3200
V_MachineCNC = 3300
V_CorpLabel = 3400
V_CorpCAM = 3500
V_PackLabel = 3600
V_Unit = 5000
# 路径常量
PATH = os.path.dirname(__file__)
def __init__(self):
"""初始化SUWood实例"""
pass
@classmethod
def icon_path(cls, icon_name, ext='png'):
"""获取图标路径"""
return f"{cls.PATH}/icon/{icon_name}.{ext}"
@classmethod
def unit_path(cls):
"""获取单元路径"""
try:
server_path = _get_server_path()
return f"{server_path}/drawings/Unit"
except ImportError:
return f"{cls.PATH}/drawings/Unit"
@classmethod
def suwood_path(cls, ref_v):
"""根据版本值获取SUWood路径"""
try:
server_path = _get_server_path()
except ImportError:
server_path = cls.PATH
path_mapping = {
cls.V_Material: f"{server_path}/images/texture",
cls.V_StretchPart: f"{server_path}/drawings/StretchPart",
cls.V_StructPart: f"{server_path}/drawings/StructPart",
cls.V_Unit: f"{server_path}/drawings/Unit",
cls.V_Connection: f"{server_path}/drawings/Connection",
cls.V_HardwareSet: f"{server_path}/drawings/HardwareSet",
cls.V_Hardware: f"{server_path}/drawings/Hardware",
}
return path_mapping.get(ref_v, server_path)
@classmethod
def suwood_pull_size(cls, pos):
"""根据位置获取拉手尺寸类型"""
size_mapping = {
1: "HW", # 右上
2: "W", # 右中
3: "HW", # 右下
4: "H", # 中上
6: "H", # 中下
11: "HW", # 右上-竖
12: "W", # 右中-竖
13: "HW", # 右下-竖
14: "H", # 中上-竖
16: "H", # 中下-竖
21: "HW", # 右上-横
22: "W", # 右中-横
23: "HW", # 右下-横
24: "H", # 中上-横
26: "H", # 中下-横
}
return size_mapping.get(pos)
@classmethod
def scene_save(cls):
"""保存场景"""
try:
import bpy # Blender Python API
scene = bpy.context.scene
order_id = scene.get("order_id")
if order_id is None:
return
data = {
"method": cls.SUSceneSave,
"order_id": order_id
}
cls.set_cmd("r00", data)
if not bpy.data.filepath:
server_path = _get_server_path()
scene_path = Path(f"{server_path}/blender")
scene_path.mkdir(exist_ok=True)
order_code = scene.get("order_code", "untitled")
filepath = scene_path / f"{order_code}.blend"
bpy.ops.wm.save_as_mainfile(filepath=str(filepath))
else:
bpy.ops.wm.save_mainfile()
except ImportError:
print("Blender API not available - scene_save not implemented")
@classmethod
def scene_price(cls):
"""场景价格计算"""
try:
import bpy
scene = bpy.context.scene
order_id = scene.get("order_id")
if order_id is None:
return
params = {
"method": cls.SUScenePrice,
"order_id": order_id
}
cls.set_cmd("r00", params)
except ImportError:
print("Blender API not available - scene_price not implemented")
@classmethod
def import_unit(cls, uid, values, mold):
"""点击创体(产品UID)"""
# 原本激活SketchUp工具这里需要适配到Blender
try:
from .suw_unit_point_tool import SUWUnitPointTool
# 创建单元点工具
width = values.get("width", 0) * 0.001 # 转换为米
depth = values.get("depth", 0) * 0.001
height = values.get("height", 0) * 0.001
tool = SUWUnitPointTool(width, depth, height, uid, mold)
# 在Blender中激活工具的逻辑需要根据具体实现
print(f"激活单元点工具: {uid}, 尺寸: {width}x{depth}x{height}")
except ImportError:
print("SUWUnitPointTool not available")
@classmethod
def import_face(cls, uid, values, mold):
"""选面创体(产品UID)"""
try:
from .suw_unit_face_tool import SUWUnitFaceTool
tool = SUWUnitFaceTool(cls.VSSpatialPos_F, uid, mold)
print(f"激活单元面工具: {uid}")
except ImportError:
print("SUWUnitFaceTool not available")
@classmethod
def front_view(cls):
"""前视图"""
try:
selection_info = _get_selection_info()
uid = selection_info['selected_uid']
obj = selection_info['selected_obj']
if uid is None or obj is None:
print("请先选择正视于的基准面!")
return
params = {
"method": cls.SUZoneFront,
"uid": uid,
"oid": obj
}
cls.set_cmd("r00", params)
except ImportError:
print("无法获取选择信息")
@classmethod
def delete_unit(cls):
"""删除单元"""
try:
import bpy
selection_info = _get_selection_info()
scene = bpy.context.scene
order_id = scene.get("order_id")
uid = selection_info['selected_uid']
obj = selection_info['selected_obj']
if uid is None:
print("请先选择待删除的柜体!")
return
elif order_id is None:
print("当前柜体不是场景方案的柜体!")
return
# 在实际应用中,这里应该有确认对话框
# 现在简化为直接执行
params = {
"method": cls.SUUnitDelete,
"order_id": order_id,
"uid": uid
}
if obj:
params["oid"] = obj
cls.set_cmd("r00", params)
except ImportError:
print("Blender API or SUWImpl not available")
@classmethod
def combine_unit(cls, uid, values, mold):
"""模块拼接"""
try:
selection_info = _get_selection_info()
selected_zone = selection_info['selected_zone']
if selected_zone is None:
print("请先选择待拼接的空区域!")
return
params = {
"method": cls.SUZoneCombine,
"uid": selected_zone.get("uid"),
"zid": selected_zone.get("zid"),
"source": uid
}
if mold:
params["module"] = mold
cls.set_cmd("r00", params)
except ImportError:
print("无法获取选择信息")
@classmethod
def replace_unit(cls, uid, values, mold):
"""模块/产品替换"""
try:
selection_info = _get_selection_info()
if selection_info['selected_zone'] is None and (mold == 1 or mold == 2):
print("请先选择待替换的区域!")
return
elif selection_info['selected_obj'] is None and (mold == 3):
print("请先选择待替换的部件!")
return
params = {
"method": cls.SUZoneReplace,
"source": uid,
"module": mold
}
cls.set_cmd("r00", params)
except ImportError:
print("无法获取选择信息")
@classmethod
def replace_mat(cls, uid, values, mat_type):
"""材料替换"""
try:
selection_info = _get_selection_info()
selected_zone = selection_info['selected_zone']
if selected_zone is None:
print("请先选择待替换材料的区域!")
return
params = {
"method": cls.SUZoneMaterial,
"mat_id": uid,
"type": mat_type
}
cls.set_cmd("r00", params)
except ImportError:
print("无法获取选择信息")
@classmethod
def replace_handle(cls, width, height, set_id, conn_id):
"""替换拉手"""
try:
selection_info = _get_selection_info()
selected_zone = selection_info['selected_zone']
if selected_zone is None:
print("请先选择待替换拉手的区域!")
return
params = {
"method": cls.SUZoneHandle,
"uid": selected_zone.get("uid"),
"zid": selected_zone.get("zid"),
"conn_id": conn_id,
"set_id": set_id
}
if width is not None and width != "":
params["width"] = int(width)
if height is not None and height != "":
params["height"] = int(height)
cls.set_cmd("r00", params)
except ImportError:
print("无法获取选择信息")
@classmethod
def clear_current(cls, ref_v):
"""清除当前选择"""
try:
selection_info = _get_selection_info()
if (ref_v == 2102 or ref_v == 2103) and selection_info['selected_zone']:
params = {
"uid": selection_info['selected_uid']
}
cls.set_cmd("r01", params)
# 清除选择
from .suw_core import get_selection_manager
selection_manager = get_selection_manager()
if selection_manager:
selection_manager.sel_clear()
except ImportError:
print("无法获取选择信息")
@classmethod
def replace_clothes(cls, front, back, set_id, conn_id):
"""挂衣杆替换"""
try:
selection_info = _get_selection_info()
selected_zone = selection_info['selected_zone']
if selected_zone is None:
print("请先选择待替换衣杆的区域!")
return
params = {
"method": cls.SUZoneCloth,
"uid": selected_zone.get("uid"),
"zid": selected_zone.get("zid"),
"conn_id": conn_id,
"set_id": set_id
}
if front != 0:
params["front"] = front
if back != 0:
params["back"] = back
cls.set_cmd("r00", params)
except ImportError:
print("无法获取选择信息")
@classmethod
def replace_lights(cls, front, back, set_id, conn_id):
"""灯带替换"""
try:
selection_info = _get_selection_info()
selected_zone = selection_info['selected_zone']
if selected_zone is None:
print("请先选择待替换灯带的区域!")
return
# 处理连接ID可能是数组
if isinstance(conn_id, list):
conns = ",".join(map(str, conn_id))
else:
conns = str(conn_id)
params = {
"method": cls.SUZoneLight,
"uid": selected_zone.get("uid"),
"zid": selected_zone.get("zid"),
"conn_id": conns,
"set_id": set_id
}
if front != 0:
params["front"] = front
if back != 0:
params["back"] = back
cls.set_cmd("r00", params)
except ImportError:
print("无法获取选择信息")
@classmethod
def set_cmd(cls, cmd_type, params):
"""设置命令"""
try:
from .suw_client import set_cmd
set_cmd(cmd_type, params)
except ImportError:
print(f"Command: {cmd_type}, Params: {params}")
# 创建全局实例
suwood = SUWood()
# 导出所有常量到模块级别,便于其他模块使用
# 场景操作常量
SUSceneNew = SUWood.SUSceneNew
SUSceneOpen = SUWood.SUSceneOpen
SUSceneSave = SUWood.SUSceneSave
SUScenePrice = SUWood.SUScenePrice
# 单元操作常量
SUUnitPoint = SUWood.SUUnitPoint
SUUnitFace = SUWood.SUUnitFace
SUUnitDelete = SUWood.SUUnitDelete
SUUnitContour = SUWood.SUUnitContour
# 区域操作常量
SUZoneFront = SUWood.SUZoneFront
SUZoneDiv1 = SUWood.SUZoneDiv1
SUZoneResize = SUWood.SUZoneResize
SUZoneCombine = SUWood.SUZoneCombine
SUZoneReplace = SUWood.SUZoneReplace
SUZoneMaterial = SUWood.SUZoneMaterial
SUZoneHandle = SUWood.SUZoneHandle
SUZoneCloth = SUWood.SUZoneCloth
SUZoneLight = SUWood.SUZoneLight
# 空间位置常量
VSSpatialPos_F = SUWood.VSSpatialPos_F
VSSpatialPos_K = SUWood.VSSpatialPos_K
VSSpatialPos_L = SUWood.VSSpatialPos_L
VSSpatialPos_R = SUWood.VSSpatialPos_R
VSSpatialPos_B = SUWood.VSSpatialPos_B
VSSpatialPos_T = SUWood.VSSpatialPos_T
# 单元轮廓常量
VSUnitCont_Zone = SUWood.VSUnitCont_Zone
VSUnitCont_Part = SUWood.VSUnitCont_Part
VSUnitCont_Work = SUWood.VSUnitCont_Work
# 版本常量
V_Dealer = SUWood.V_Dealer
V_Machining = SUWood.V_Machining
V_Division = SUWood.V_Division
V_PartCategory = SUWood.V_PartCategory
V_Contour = SUWood.V_Contour
V_Color = SUWood.V_Color
V_Profile = SUWood.V_Profile
V_Surf = SUWood.V_Surf
V_StretchPart = SUWood.V_StretchPart
V_Material = SUWood.V_Material
V_Connection = SUWood.V_Connection
V_HardwareSchema = SUWood.V_HardwareSchema
V_HardwareSet = SUWood.V_HardwareSet
V_Hardware = SUWood.V_Hardware
V_Groove = SUWood.V_Groove
V_DesignParam = SUWood.V_DesignParam
V_ProfileSchema = SUWood.V_ProfileSchema
V_StructPart = SUWood.V_StructPart
V_CraftPart = SUWood.V_CraftPart
V_SeriesPart = SUWood.V_SeriesPart
V_Drawer = SUWood.V_Drawer
V_DesignTemplate = SUWood.V_DesignTemplate
V_PriceTemplate = SUWood.V_PriceTemplate
V_MachineCut = SUWood.V_MachineCut
V_MachineCNC = SUWood.V_MachineCNC
V_CorpLabel = SUWood.V_CorpLabel
V_CorpCAM = SUWood.V_CorpCAM
V_PackLabel = SUWood.V_PackLabel
V_Unit = SUWood.V_Unit
# 路径常量
PATH = SUWood.PATH
# 导出所有类方法为模块级别函数
icon_path = SUWood.icon_path
unit_path = SUWood.unit_path
suwood_path = SUWood.suwood_path
suwood_pull_size = SUWood.suwood_pull_size
scene_save = SUWood.scene_save
scene_price = SUWood.scene_price
import_unit = SUWood.import_unit
import_face = SUWood.import_face
front_view = SUWood.front_view
delete_unit = SUWood.delete_unit
combine_unit = SUWood.combine_unit
replace_unit = SUWood.replace_unit
replace_mat = SUWood.replace_mat
replace_handle = SUWood.replace_handle
clear_current = SUWood.clear_current
replace_clothes = SUWood.replace_clothes
replace_lights = SUWood.replace_lights
set_cmd = SUWood.set_cmd