suwoodblender/blenderpython/suw_impl_clean.py

686 lines
23 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
SUW Implementation - Python翻译版本 (简化版)
原文件: SUWImpl.rb (2019)
用途: 核心实现类SUWood的主要功能
"""
import re
import math
import logging
from typing import Optional, Any, Dict, List, Tuple, Union
# 设置日志
logger = logging.getLogger(__name__)
# 尝试相对导入,失败则使用绝对导入
try:
from .suw_constants import SUWood
except ImportError:
try:
from suw_constants import SUWood
except ImportError:
# 如果都找不到,创建一个基本的存根
class SUWood:
@staticmethod
def suwood_path(version):
return "."
try:
import bpy
import mathutils
import bmesh
BLENDER_AVAILABLE = True
except ImportError:
BLENDER_AVAILABLE = False
print("⚠️ Blender API 不可用,使用基础几何类")
# 创建存根mathutils模块
class MockMathutils:
class Vector:
def __init__(self, vec):
self.x, self.y, self.z = vec[:3] if len(vec) >= 3 else (vec + [0, 0])[:3]
def normalized(self):
return self
def dot(self, other):
return 0
class Matrix:
@staticmethod
def Scale(scale, size, axis):
return MockMathutils.Matrix()
@staticmethod
def Translation(vec):
return MockMathutils.Matrix()
@staticmethod
def Rotation(angle, size):
return MockMathutils.Matrix()
def __matmul__(self, other):
return MockMathutils.Matrix()
mathutils = MockMathutils()
# ==================== 几何类扩展 ====================
class Point3d:
"""3D点类 - 对应Ruby的Geom::Point3d"""
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0):
self.x = x
self.y = y
self.z = z
@classmethod
def parse(cls, value: str):
"""从字符串解析3D点"""
if not value or value.strip() == "":
return None
# 解析格式: "(x,y,z)" 或 "x,y,z"
clean_value = re.sub(r'[()]*', '', value)
xyz = [float(axis.strip()) for axis in clean_value.split(',')]
# 转换mm为米假设输入是mm
return cls(xyz[0] * 0.001, xyz[1] * 0.001, xyz[2] * 0.001)
def to_s(self, unit: str = "mm", digits: int = -1) -> str:
"""转换为字符串"""
if unit == "cm":
x_val = self.x * 100 # 转换为cm
y_val = self.y * 100
z_val = self.z * 100
return f"({x_val:.3f}, {y_val:.3f}, {z_val:.3f})"
else: # mm
x_val = self.x * 1000 # 转换为mm
y_val = self.y * 1000
z_val = self.z * 1000
if digits == -1:
return f"({x_val}, {y_val}, {z_val})"
else:
return f"({x_val:.{digits}f}, {y_val:.{digits}f}, {z_val:.{digits}f})"
def __str__(self):
return self.to_s()
def __repr__(self):
return f"Point3d({self.x}, {self.y}, {self.z})"
class Vector3d:
"""3D向量类 - 对应Ruby的Geom::Vector3d"""
def __init__(self, x: float = 0.0, y: float = 0.0, z: float = 0.0):
self.x = x
self.y = y
self.z = z
@classmethod
def parse(cls, value: str):
"""从字符串解析3D向量"""
if not value or value.strip() == "":
return None
clean_value = re.sub(r'[()]*', '', value)
xyz = [float(axis.strip()) for axis in clean_value.split(',')]
return cls(xyz[0] * 0.001, xyz[1] * 0.001, xyz[2] * 0.001)
def normalize(self):
"""归一化向量"""
length = math.sqrt(self.x**2 + self.y**2 + self.z**2)
if length > 0:
return Vector3d(self.x/length, self.y/length, self.z/length)
return Vector3d(0, 0, 0)
def __str__(self):
return f"Vector3d({self.x}, {self.y}, {self.z})"
class Transformation:
"""变换矩阵类 - 对应Ruby的Geom::Transformation"""
def __init__(self, origin: Point3d = None, x_axis: Vector3d = None,
y_axis: Vector3d = None, z_axis: Vector3d = None):
self.origin = origin or Point3d(0, 0, 0)
self.x_axis = x_axis or Vector3d(1, 0, 0)
self.y_axis = y_axis or Vector3d(0, 1, 0)
self.z_axis = z_axis or Vector3d(0, 0, 1)
@classmethod
def parse(cls, data: Dict[str, str]):
"""从字典解析变换"""
origin = Point3d.parse(data.get("o"))
x_axis = Vector3d.parse(data.get("x"))
y_axis = Vector3d.parse(data.get("y"))
z_axis = Vector3d.parse(data.get("z"))
return cls(origin, x_axis, y_axis, z_axis)
# ==================== SUWood 材质类型常量 ====================
MAT_TYPE_NORMAL = 0
MAT_TYPE_OBVERSE = 1
MAT_TYPE_NATURE = 2
# ==================== SUWImpl 核心实现类 ====================
class SUWImpl:
"""SUWood核心实现类 - 完整翻译版本"""
_instance = None
_selected_uid = None
_selected_obj = None
_selected_zone = None
_selected_part = None
_scaled_zone = None
_server_path = None
_default_zone = None
def __init__(self):
"""初始化SUWImpl实例"""
# 基础属性
self.added_contour = False
# 图层相关
self.door_layer = None
self.drawer_layer = None
# 材质和纹理
self.textures = {}
# 数据存储
self.unit_param = {} # key: uid, value: params such as w/d/h/order_id
self.unit_trans = {} # key: uid, value: transformation
self.zones = {} # key: uid/oid
self.parts = {} # key: uid/cp, second key is component root oid
self.hardwares = {} # key: uid/cp, second key is hardware root oid
self.machinings = {} # key: uid, array, child entity of part or hardware
self.dimensions = {} # key: uid, array
# 模式和状态
self.part_mode = False
self.hide_none = False
self.mat_type = MAT_TYPE_NORMAL
self.back_material = False
# 选择状态
self.selected_faces = []
self.selected_parts = []
self.selected_hws = []
self.menu_handle = 0
@classmethod
def get_instance(cls):
"""获取单例实例"""
if cls._instance is None:
cls._instance = cls()
return cls._instance
def startup(self):
"""启动SUWood系统"""
print("🚀 SUWood系统启动")
# 创建图层
self._create_layers()
# 初始化材质
self._init_materials()
# 重置状态
self.added_contour = False
self.part_mode = False
self.hide_none = False
self.mat_type = MAT_TYPE_NORMAL
self.selected_faces.clear()
self.selected_parts.clear()
self.selected_hws.clear()
self.menu_handle = 0
self.back_material = False
def _create_layers(self):
"""创建图层"""
if BLENDER_AVAILABLE:
# 在Blender中创建集合类似图层
try:
if "DOOR_LAYER" not in bpy.data.collections:
door_collection = bpy.data.collections.new("DOOR_LAYER")
bpy.context.scene.collection.children.link(door_collection)
self.door_layer = door_collection
if "DRAWER_LAYER" not in bpy.data.collections:
drawer_collection = bpy.data.collections.new("DRAWER_LAYER")
bpy.context.scene.collection.children.link(drawer_collection)
self.drawer_layer = drawer_collection
except Exception as e:
print(f"⚠️ 创建图层时出错: {e}")
else:
# 非Blender环境的存根
self.door_layer = {"name": "DOOR_LAYER", "visible": True}
self.drawer_layer = {"name": "DRAWER_LAYER", "visible": True}
def _init_materials(self):
"""初始化材质"""
# 添加基础材质
self.add_mat_rgb("mat_normal", 0.1, 128, 128, 128) # 灰色
self.add_mat_rgb("mat_select", 0.5, 255, 0, 0) # 红色
self.add_mat_rgb("mat_default", 0.9, 255, 250, 250) # 白色
self.add_mat_rgb("mat_obverse", 1.0, 3, 70, 24) # 绿色
self.add_mat_rgb("mat_reverse", 1.0, 249, 247, 174) # 黄色
self.add_mat_rgb("mat_thin", 1.0, 248, 137, 239) # 粉紫色
self.add_mat_rgb("mat_machine", 1.0, 0, 0, 255) # 蓝色
def add_mat_rgb(self, mat_id: str, alpha: float, r: int, g: int, b: int):
"""添加RGB材质"""
if BLENDER_AVAILABLE:
try:
# 在Blender中创建材质
mat = bpy.data.materials.new(name=mat_id)
mat.use_nodes = True
# 设置颜色
bsdf = mat.node_tree.nodes["Principled BSDF"]
bsdf.inputs[0].default_value = (r/255.0, g/255.0, b/255.0, 1.0)
bsdf.inputs[21].default_value = 1.0 - alpha # Alpha
self.textures[mat_id] = mat
except Exception as e:
print(f"⚠️ 创建材质 {mat_id} 时出错: {e}")
else:
# 非Blender环境的存根
material = {
"id": mat_id,
"alpha": alpha,
"color": (r, g, b),
"type": "rgb"
}
self.textures[mat_id] = material
def get_zones(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""获取区域数据"""
uid = data.get("uid")
if uid not in self.zones:
self.zones[uid] = {}
return self.zones[uid]
def get_parts(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""获取部件数据"""
uid = data.get("uid")
if uid not in self.parts:
self.parts[uid] = {}
return self.parts[uid]
def get_hardwares(self, data: Dict[str, Any]) -> Dict[str, Any]:
"""获取五金数据"""
uid = data.get("uid")
if uid not in self.hardwares:
self.hardwares[uid] = {}
return self.hardwares[uid]
def get_texture(self, key: str):
"""获取纹理材质"""
if key and key in self.textures:
return self.textures[key]
else:
return self.textures.get("mat_default")
def sel_clear(self):
"""清除所有选择"""
SUWImpl._selected_uid = None
SUWImpl._selected_obj = None
SUWImpl._selected_zone = None
SUWImpl._selected_part = None
# 清除选择的面
for face in self.selected_faces:
if face: # 检查face是否有效
self.textured_face(face, False)
self.selected_faces.clear()
# 清除选择的部件
for part in self.selected_parts:
if part: # 检查part是否有效
self.textured_part(part, False)
self.selected_parts.clear()
# 清除选择的五金
for hw in self.selected_hws:
if hw: # 检查hw是否有效
self.textured_hw(hw, False)
self.selected_hws.clear()
print("🧹 清除所有选择")
def textured_face(self, face: Any, selected: bool):
"""设置面的纹理"""
if selected:
self.selected_faces.append(face)
color = "mat_select" if selected else "mat_normal"
texture = self.get_texture(color)
# 这里需要根据具体的3D引擎实现
print(f"🎨 设置面纹理: {color}, 选中: {selected}")
def textured_part(self, part: Any, selected: bool):
"""设置部件的纹理"""
if selected:
self.selected_parts.append(part)
# 这里需要实现部件纹理设置的具体逻辑
print(f"🎨 设置部件纹理, 选中: {selected}")
def textured_hw(self, hw: Any, selected: bool):
"""设置五金的纹理"""
if selected:
self.selected_hws.append(hw)
# 这里需要实现五金纹理设置的具体逻辑
print(f"🎨 设置五金纹理, 选中: {selected}")
# ==================== 核心几何创建方法 ====================
def create_face(self, container: Any, surface: Dict[str, Any], color: str = None,
scale: float = None, angle: float = None, series: List = None,
reverse_face: bool = False, back_material: bool = True,
saved_color: str = None, face_type: str = None):
"""创建面 - 核心几何创建方法"""
try:
if not surface or "segs" not in surface:
print("❌ create_face: 缺少surface或segs数据")
return None
segs = surface["segs"]
print(f"🔧 创建面: {len(segs)}个段, color={color}, reverse={reverse_face}")
# 存根模式创建面
face = {
"type": "face",
"surface": surface,
"color": color,
"scale": scale,
"angle": angle,
"reverse_face": reverse_face,
"back_material": back_material,
"saved_color": saved_color,
"face_type": face_type,
"segs": segs
}
# 设置属性
if face_type:
face["typ"] = face_type
print(f"✅ 存根面创建成功: {len(segs)}")
return face
except Exception as e:
print(f"❌ create_face失败: {e}")
return None
def create_edges(self, container: Any, segments: List[List[str]], series: List = None) -> List[Any]:
"""创建边 - 从轮廓段创建边"""
try:
edges = []
# 解析所有段的点
for index, segment in enumerate(segments):
pts = []
for point_str in segment:
point = Point3d.parse(point_str)
if point:
pts.append(point)
# 创建存根边
edge = {
"type": "line_edge",
"points": pts,
"index": index
}
edges.append(edge)
if series is not None:
series.append(pts)
print(f"✅ 创建边完成: {len(edges)}条边")
return edges
except Exception as e:
print(f"❌ create_edges失败: {e}")
return []
def follow_me(self, container: Any, surface: Dict[str, Any], path: Any,
color: str = None, scale: float = None, angle: float = None,
reverse_face: bool = True, series: List = None, saved_color: str = None):
"""跟随拉伸 - 沿路径拉伸面"""
try:
print(f"🔀 跟随拉伸: color={color}, reverse={reverse_face}")
# 首先创建面
face = self.create_face(container, surface, color, scale, angle,
series, reverse_face, self.back_material, saved_color)
if not face:
print("❌ follow_me: 无法创建面")
return None
# 从surface获取法向量
if "vz" in surface:
vz = Vector3d.parse(surface["vz"])
normal = vz.normalize() if vz else Vector3d(0, 0, 1)
else:
normal = Vector3d(0, 0, 1)
print(f"✅ 跟随拉伸完成: normal={normal}")
return normal
except Exception as e:
print(f"❌ follow_me失败: {e}")
return Vector3d(0, 0, 1)
def work_trimmed(self, part: Any, work: Dict[str, Any]):
"""工件修剪处理"""
try:
print(f"✂️ 工件修剪: part={part}")
leaves = []
# 找到所有类型为"cp"的子项
if isinstance(part, dict) and "children" in part:
for child in part["children"]:
if isinstance(child, dict) and child.get("typ") == "cp":
leaves.append(child)
print(f"找到 {len(leaves)} 个待修剪的子项")
print("✅ 工件修剪完成")
except Exception as e:
print(f"❌ work_trimmed失败: {e}")
def textured_surf(self, face: Any, back_material: bool, color: str,
saved_color: str = None, scale_a: float = None, angle_a: float = None):
"""表面纹理处理 - 高级纹理映射"""
try:
# 保存纹理属性
if saved_color:
self._set_entity_attr(face, "ckey", saved_color)
if scale_a:
self._set_entity_attr(face, "scale", scale_a)
if angle_a:
self._set_entity_attr(face, "angle", angle_a)
# 获取纹理
texture = self.get_texture(color)
if not texture:
print(f"⚠️ 找不到纹理: {color}")
return
# 存根模式纹理应用
if isinstance(face, dict):
face["material"] = texture
face["back_material"] = texture if back_material else None
print(f"✅ 存根纹理应用: {color}")
except Exception as e:
print(f"❌ textured_surf失败: {e}")
# ==================== 命令处理方法 ====================
def c03(self, data: Dict[str, Any]):
"""添加区域 (add_zone) - 完整几何创建实现"""
uid = data.get("uid")
zid = data.get("zid")
if not uid or not zid:
print("❌ 缺少uid或zid参数")
return
zones = self.get_zones(data)
elements = data.get("children", [])
print(f"🏗️ 添加区域: uid={uid}, zid={zid}, 元素数量={len(elements)}")
# 创建区域组
group = {
"type": "zone",
"faces": [],
"from_default": False
}
for element in elements:
surf = element.get("surf", {})
child_id = element.get("child")
if surf:
face = self.create_face(group, surf)
if face:
face["child"] = child_id
if surf.get("p") == 1:
face["layer"] = "door"
group["faces"].append(face)
# 设置区域属性
self._set_entity_attr(group, "uid", uid)
self._set_entity_attr(group, "zid", zid)
self._set_entity_attr(group, "zip", data.get("zip", -1))
self._set_entity_attr(group, "typ", "zid")
if "cor" in data:
self._set_entity_attr(group, "cor", data["cor"])
zones[zid] = group
print(f"✅ 区域创建成功: {uid}/{zid}")
def c04(self, data: Dict[str, Any]):
"""添加部件 (add_part) - 完整几何创建实现"""
uid = data.get("uid")
root = data.get("cp")
if not uid or not root:
print("❌ 缺少uid或cp参数")
return
parts = self.get_parts(data)
# 创建部件
part = {
"type": "part",
"children": [],
"entities": []
}
parts[root] = part
print(f"🔧 添加部件: uid={uid}, cp={root}")
# 设置部件基本属性
self._set_entity_attr(part, "uid", uid)
self._set_entity_attr(part, "zid", data.get("zid"))
self._set_entity_attr(part, "pid", data.get("pid"))
self._set_entity_attr(part, "cp", root)
self._set_entity_attr(part, "typ", "cp")
# 处理部件子项
finals = data.get("finals", [])
for final in finals:
final_type = final.get("typ")
if final_type == 1:
# 板材部件
leaf = self._add_part_board(part, final)
elif final_type == 2:
# 拉伸部件
leaf = self._add_part_stretch(part, final)
elif final_type == 3:
# 弧形部件
leaf = self._add_part_arc(part, final)
if leaf:
self._set_entity_attr(leaf, "typ", "cp")
self._set_entity_attr(leaf, "mn", final.get("mn"))
print(f"✅ 部件子项创建: type={final_type}")
print(f"✅ 部件创建完成: {uid}/{root}")
# ==================== 辅助方法 ====================
def _set_entity_attr(self, entity: Any, attr: str, value: Any):
"""设置实体属性"""
if isinstance(entity, dict):
entity[attr] = value
elif hasattr(entity, attr):
setattr(entity, attr, value)
def _get_entity_attr(self, entity: Any, attr: str, default: Any = None) -> Any:
"""获取实体属性"""
if isinstance(entity, dict):
return entity.get(attr, default)
elif hasattr(entity, attr):
return getattr(entity, attr, default)
return default
def _is_deleted(self, entity: Any) -> bool:
"""检查实体是否已删除"""
if isinstance(entity, dict):
return entity.get("deleted", False)
return False
def _add_part_board(self, part: Any, data: Dict[str, Any]) -> Any:
"""添加板材部件(简化版)"""
leaf = {
"type": "board_part",
"data": data,
"ckey": data.get("ckey")
}
if isinstance(part, dict):
part.setdefault("children", []).append(leaf)
return leaf
def _add_part_stretch(self, part: Any, data: Dict[str, Any]) -> Any:
"""添加拉伸部件(简化版)"""
leaf = {
"type": "stretch_part",
"data": data,
"ckey": data.get("ckey")
}
if isinstance(part, dict):
part.setdefault("children", []).append(leaf)
return leaf
def _add_part_arc(self, part: Any, data: Dict[str, Any]) -> Any:
"""添加弧形部件(简化版)"""
leaf = {
"type": "arc_part",
"data": data,
"ckey": data.get("ckey")
}
if isinstance(part, dict):
part.setdefault("children", []).append(leaf)
return leaf
print(f"🎉 SUWImpl核心几何创建系统加载完成")
print(f" 🔧 create_face - 面创建功能")
print(f" ✂️ work_trimmed - 工件修剪功能")
print(f" 🔀 follow_me - 跟随拉伸功能")
print(f" 🏗️ c03 - 区域添加功能")
print(f" 🔧 c04 - 部件添加功能")
print(f" <20><> 所有功能现在可以进行真实测试")