加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
stp_to_gltf.py 6.05 KB
一键复制 编辑 原始数据 按行查看 历史
huanghu.luo@designorder.cn 提交于 2021-06-08 17:33 . gltf
import json
import os
import random
import sys
from OCC.Core import TopAbs
from OCC.Core.BRep import BRep_Builder
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
from OCC.Core.BRepTools import breptools_Write, breptools_Read
from OCC.Core.IFSelect import IFSelect_ItemsByEntity, IFSelect_RetDone
from OCC.Core.RWStl import rwstl
from OCC.Core.STEPControl import STEPControl_Reader
from OCC.Core.Tesselator import ShapeTesselator
from OCC.Core.TopAbs import TopAbs_SOLID, TopAbs_FACE
from OCC.Core.TopExp import topexp_MapShapes, TopExp_Explorer
from OCC.Core.TopTools import TopTools_IndexedMapOfShape
from OCC.Display.OCCViewer import rgb_color
from OCC.Display.SimpleGui import init_display
from OCC.Extend.DataExchange import read_step_file_with_names_colors, read_step_file, write_step_file
from OCC.Extend.DataExchange import read_iges_file, read_stl_file
import time
from OCC.Extend.TopologyUtils import is_edge, is_vertex, is_solid
from occ_to_gltf import ShapeToGlb
from read_stp_file_colors import read_step_file_with_names_colors_
def stp_to_glb(stp_path, filename): # 成功调试
"""
:param stp_path: 原stp文件路径
:param filename: 输出文件名
:return: gltf文件
"""
# step2json(stp_path,filename)
# print(filename)
# shape_dict = read_step_file_with_names_colors(stp_path)
shape_dict = read_step_file(stp_path)
# print("开始")
# print("开始")
# a = rwstl.ReadFile(stp_path)
# print(a)
# shape_dict = read_stl_file(stp_path)
# print("................", shape_dict)//
# stp_split(stp_path, filename)
# write_step_file(shape_dict, filename + ".stp")
# with open("{}/{}_color.json".format(filename, filename), "r") as f:
# color_dict = json.loads(f.read())
# print("color", color_dict)
# for i in color_dict:
# file = "{}/{}.stp".format(filename, i)
# try:
# shape_ = read_step_file(filename+".stp")
# except:
# continue
# display, start_display, add_menu, add_function_to_menu = init_display()
# display.DisplayShape(shape_dict, update=True)
# start_display()
ShapeToGlb(shape_dict, path="./", filename=filename,step_file=stp_path)
# edges_map = TopTools_IndexedMapOfShape()
# topexp_MapShapes(shape_dict1, TopAbs.TopAbs_SHAPE, edges_map)
# topexp_MapShapes(shape_dict1, TopAbs.TopAbs_FACE, edges_map)
# topexp_MapShapes(shape_dict1, TopAbs.TopAbs_EDGE, edges_map)
# topexp_MapShapes(shape_dict1, TopAbs.TopAbs_VERTEX, edges_map)
# topExp = TopExp_Explorer()
# for a_shape in shape_dict1:
# print("a_shape", a_shape)
# s, c =shape_dict1[a_shape]
# print("self.shape", c)
# color = [c.Red(), c.Green(), c.Blue()]
# print(color)
# self.color = [0, 0.6313725490196078, 1]
# exp_type =
# if exp_type == 'solid':
# topExp.Init(shape_dict1, TopAbs_SOLID)
# i = 1
# while topExp.More():
# edges_map.FindKey(i)
# if is_solid(topExp.Current()):
# pre = "Shape_" + str(i)
# elif is_edge(topExp.Current()):
# pre = "Edge_" + str(i)
# elif is_vertex(topExp.Current()):
# pre = "Vertex_" + str(i)
# else:
# pre = "Face_" + str(i)
# print(pre)
# topExp.Next()
# i += 1
# topExp.Init(shape_dict1, TopAbs_SOLID)
# j = 1
# topExp1 = TopExp_Explorer()
# topExp1.Init(shape_dict1, TopAbs_FACE)
# while topExp1.More():
# edges_map.FindKey(j)
# if is_solid(topExp1.Current()):
# pre = "Shape_" + str(j)
# elif is_edge(topExp1.Current()):
# pre = "Edge_" + str(j)
# elif is_vertex(topExp1.Current()):
# pre = "Vertex_" + str(j)
# else:
# pre = "Face_" + str(j)
# print(pre)
# topExp1.Next()
# j += 1
# elif exp_type == 'face':
# topExp.Init(a_shape, TopAbs_FACE)
# for shp in list(shape_dict.keys()):
# idx = edges_map.FindIndex(shp)
# if is_solid(shp):
# pre = "Shape_" + str(idx)
# elif is_edge(shp):
# pre = "Edge_" + str(idx)
# elif is_vertex(shp):
# pre = "Vertex_" + str(idx)
# else:
# pre = "Face_" + str(idx)
# print(pre)
# for i in list(shape_dict1.keys()):
# br, r = shape_dict[i]
# display.DisplayShape(shape_dict, update=True)
# start_display()
def stp_split(stp_path, filename):
shape_dict = read_step_file_with_names_colors(stp_path)
print("shape_dict", shape_dict)
i = 0
color_dict = dict()
display, start_display, add_menu, add_function_to_menu = init_display()
for a_shape in shape_dict:
print("a_shape", a_shape)
s, c = shape_dict[a_shape]
display.DisplayShape(a_shape, update=True,color=rgb_color(c.Red(), c.Green(), c.Blue()))
print("self.shape", c)
color = [c.Red(), c.Green(), c.Blue()]
# shape_to_stp(a_shape, filename + "/" + filename + str(i) + ".stp")
# color_dict[filename + str(i)] = color
# i += 1
start_display()
#
with open("./{}/{}.json".format(filename, filename + "_color"), "w") as f:
f.write(json.dumps(color_dict))
def shape_to_stp(shape, filename):
write_step_file(shape, filename)
def create_brep():
box_shp = BRepPrimAPI_MakeBox(10, 20, 20).Shape()
breptools_Write(box_shp, './model/box.brep')
if __name__ == '__main__':
# display, start_display, add_menu, add_function_to_menu = init_display()
# try:
# stp_url = sys.argv[1]
# # print(stp_url)
# except:
#
# stp_url = None
# print("stp_name", stp_url)
# t1 = time.time()
# stp_url = "GUN1.1.1_SILHOUETTE3.stl"
# stp_url = "LQA-331060FD01(D15-040444133)-Geo-Fixture.stp"
# stp_url = "GUN.stp"
# stp_url = "RPS.stp"
# stp_url = "DO-001-A-01-BRACKET.stp"
stp_url = "DO-001-A-01-01-BRACKET.stp"
# if stp_url:
stp_to_glb(stp_url, stp_url.split(".")[0])
# else:
# print("没有文件路径")
# t2 = time.time()
# print("all", t2 - t1)
# print("stp_name", stp_url)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化