import os
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.gp import gp_Dir
from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_BLACK
from OCC.Display.OCCViewer import OffscreenRenderer
from OCC.Display.SimpleGui import init_display
from OCC.Core.AIS import AIS_Shape
from OCC.Core.Aspect import Aspect_GFM_NONE
from PIL import Image
def create_three_views(step_file, save_path=None,mapping_id=None):
# 读取STEP文件
step_reader = STEPControl_Reader()
step_reader.ReadFile(step_file)
step_reader.TransferRoot()
shape = step_reader.Shape()
# 初始化显示器
# display, start_display, add_menu, add_function_to_menu = init_display(size=(224, 224), display_triedron=False)
#截图时后台运行,不显示窗口
display = OffscreenRenderer(screen_size=(224, 224))
display.hide_triedron()
# 创建形状
ais_shape = AIS_Shape(shape)
# 设置显示属性
display.Context.Display(ais_shape, True)
# 设置显示为HLR模式,只显示可见的部分
display.SetModeWireFrame()
display.SetModeHLR()
# 设置线框显示模式和颜色
display.Context.SetDisplayMode(ais_shape, 0, True) # 线框模式
ais_shape.SetColor(Quantity_Color(Quantity_NOC_BLACK)) # 线框颜色为黑色
# 设置纯白背景
display.View.SetBackgroundColor(Quantity_Color(1, 1, 1, 1))
display.View.SetBgGradientColors(
Quantity_Color(1, 1, 1, 1),
Quantity_Color(1, 1, 1, 1),
Aspect_GFM_NONE
)
# 关闭阴影
display.View.SetShadingModel(0) # 使用最基本的渲染模式
# 设置为正交投影
display.View.Camera().SetProjectionType(0)
# 确保视图更新
display.FitAll()
display.Context.UpdateCurrentViewer()
display.Repaint()
# 设置视图方向和保存图片
views = ["front", "top", "left"]
for view_name in views:
if view_name == "front":
display.View_Front()
elif view_name == "top":
display.View_Top()
elif view_name == "left":
display.View_Left()
display.FitAll() # 调整视图以适应新的形状
# display.Repaint()
view_save_dir = save_path + "/" + view_name if save_path else "./" + view_name
if not os.path.exists(view_save_dir):
os.makedirs(view_save_dir)
file_baseName = os.path.splitext(os.path.basename(step_file))[0]
if mapping_id:
view_save_path = f"{view_save_dir}/{mapping_id}_{file_baseName}.png"
else:
view_save_path = f"{view_save_dir}/{view_name}_{file_baseName}.png"
display.View.Dump(view_save_path)
resize_images(view_save_path)
# 清理显示
display.Context.Remove(ais_shape, True)
def resize_images(filename):
# with Image.open(filename) as img:
# img_resized = img.resize((224, 224))
# img_resized.save(filename)
# 加载原图
image = Image.open(filename)
# 获取原图的宽度和高度
original_width, original_height = image.size
# 目标尺寸
target_width, target_height = 224, 224
# 计算缩放比例,保持图像的宽高比
scale_width = target_width / original_width
scale_height = target_height / original_height
scale = min(scale_width, scale_height)
# 计算新的宽高
new_width = int(original_width * scale)
new_height = int(original_height * scale)
# 调整图像大小,保持比例
resized_image = image.resize((new_width, new_height))
# 创建一个新的空白图像,大小为224x224
new_image = Image.new("RGB", (target_width, target_height), (255, 255, 255))
# 计算填充位置,居中对齐
top = (target_height - new_height) // 2
left = (target_width - new_width) // 2
# 将调整后的图像粘贴到新的空白图像上
new_image.paste(resized_image, (left, top))
# 显示或保存结果
new_image.save(filename)
使用pythonOCC生成step模型文件的前、左、上视角黑白线框图
Python3.8
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本
您可能感兴趣的与本文相关的镜像
Python3.8
Conda
Python
Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

被折叠的 条评论
为什么被折叠?



