import os
from OCC.Core.IFSelect import IFSelect_RetDone
from OCC.Core.STEPCAFControl import STEPCAFControl_Reader
from OCC.Core.TCollection import TCollection_ExtendedString
from OCC.Core.TDocStd import TDocStd_Document
from OCC.Core.VrmlAPI import VrmlAPI_Writer
from OCC.Core.XCAFDoc import XCAFDoc_ShapeTool, XCAFDoc_DocumentTool
from OCC.Core.BRep import BRep_Tool
from OCC.Core.TDF import TDF_LabelSequence, TDF_Label
from OCC.Extend.TopologyUtils import TopologyExplorer
from OCC.Core.TopAbs import topabs
from OCC.Extend.DataExchange import read_step_file_with_names_colors
from OCC.Display.WebGl.x3dom_renderer import X3DomRenderer, X3DExporter
from IPython.display import HTML
import xml.etree.ElementTree as ET
import re
#stp_filename = 'VentilatorAP203' #'as1_pe_203'
stp_filename = 'as1_pe_203'
stp_path = os.path.join('..','assets', 'step', stp_filename + '.stp')
#stp_path = os.path.join(stp_filename + '.stp')
stpShapes = read_step_file_with_names_colors(stp_path)
Number of shapes at root : 1 Name : AS1_PE_ASM Name : PLATE Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 229 0.0 1.0 0.0 Name : COMPOUND shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : L_BRACKET_ASSEMBLY_ASM Name : L-BRACKET Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : COMPOUND shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT_BOLT_ASSEMBLY_ASM Name : BOLT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 22 0.0 0.0 1.0 Name : COMPOUND shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 412 1.0 0.0 0.0 Name : COMPOUND shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT_BOLT_ASSEMBLY_ASM Name : BOLT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 22 0.0 0.0 1.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 412 1.0 0.0 0.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT_BOLT_ASSEMBLY_ASM Name : BOLT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 22 0.0 0.0 1.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 412 1.0 0.0 0.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : L_BRACKET_ASSEMBLY_ASM Name : L-BRACKET Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT_BOLT_ASSEMBLY_ASM Name : BOLT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 22 0.0 0.0 1.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 412 1.0 0.0 0.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT_BOLT_ASSEMBLY_ASM Name : BOLT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 22 0.0 0.0 1.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 412 1.0 0.0 0.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT_BOLT_ASSEMBLY_ASM Name : BOLT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 22 0.0 0.0 1.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 412 1.0 0.0 0.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : ROD_ASM Name : ROD Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 22 0.0 0.0 1.0 Name : COMPOUND shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 412 1.0 0.0 0.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 Name : NUT Name : SOLID instance color Name & RGB: <class 'Quantity_Color'> 412 1.0 0.0 0.0 Name : COMPOUND instance color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0 shape color Name & RGB: <class 'Quantity_Color'> 510 1.0 1.0 0.0
x3domRenderer = X3DomRenderer(path="./")
for shape in stpShapes:
#print(shape.ShapeType(),stpShapes[shape])
#x3dexp = X3DExporter(shape, None, None, False, (1,0,0), (0,0,0), 0, 0, (0,0,0), 2, 1)
#x3dexp.compute()
#print(len(x3dexp._triangle_sets), len(x3dexp._line_sets))
color = stpShapes[shape][1]
colorRGB=(color.Red(), color.Green(), color.Blue())
x3domRenderer.DisplayShape(shape=shape, color=colorRGB, export_edges=True)
x3domRenderer.generate_html_file(False, 1.0)
## x3dom webgl renderer - render axes/planes : True - axes/plane zoom factor : 1 X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge X3D exporter, discretize an edge \ meshing shapes... 100%
#merge shp linesets
for hash in x3domRenderer._x3d_shapes.keys():
xml=ET.parse(hash+'.x3d')
#print(xml)
shp_vertexCounts=[]
shp_points=[]
for lineset in xml.iter('LineSet'):
shp_vertexCounts.append(lineset.get('vertexCount'))
shp_points.append(list(lineset.iter('Coordinate'))[0].get('point'))
#print (shp_vertexCounts, shp_points)
#reuse first lineset to hold all ines
lset=list(xml.iter('LineSet'))[0]
lset.set('vertexCount',",".join(shp_vertexCounts))
coords=list(lset.iter('Coordinate'))[0]
coords.set('point',",".join(shp_points))
#remove all other lineset shapes
group=list(xml.iter('Group'))[0]
transforms=list(group.iter('Transform'))
#skip first
for transform in transforms[1:]:
group.remove(transform)
#print(ET.dump(group))
xml.write("M_"+hash+'.x3d')
#merge edg linesets
edg_vertexCounts=[]
edg_points=[]
for hash in x3domRenderer._x3d_edges.keys():
xml=ET.parse(hash+'.x3d').getroot()
firstlineset=list(xml.iter('LineSet'))[0]
edg_vertexCounts.append(firstlineset.get('vertexCount'))
edg_points.append(list(firstlineset.iter('Coordinate'))[0].get('point'))
firstedgex3d=list(x3domRenderer._x3d_edges.keys())[0]+'.x3d'
xml=ET.parse(hash+'.x3d')
lset=list(xml.iter('LineSet'))[0]
lset.set('vertexCount',",".join(edg_vertexCounts))
coords=list(lset.iter('Coordinate'))[0]
coords.set('point',",".join(edg_points))
ALLEDGESX3D='allEdges.x3d'
xml.write(ALLEDGESX3D)
with open('index.html','r') as file:
html = file.read()
html2=html.replace(firstedgex3d, ALLEDGESX3D)
html2=re.sub(r"<Inline.*edg.*</Inline>","", html2)
html2=re.sub(r'url="shp','url="M_shp', html2)
print(html2)
<!DOCTYPE HTML> <html lang="en"> <head> <title>pythonOCC 7.4.0 x3dom renderer</title> <meta name='Author' content='Thomas Paviot - tpaviot@gmail.com'> <meta name='Keywords' content='WebGl,pythonOCC'> <meta charset="utf-8"> <link rel="stylesheet" type="text/css" href="https://x3dom.org/release/x3dom.css"> <script src="https://x3dom.org/release/x3dom.js"></script> <style> body { background: linear-gradient(#ced7de, #808080); margin: 0px; overflow: hidden; } #pythonocc_rocks { padding: 5px; position: absolute; left: 1%; bottom: 2%; height: 38px; width: 280px; border-radius: 5px; border: 2px solid #f7941e; opacity: 0.7; font-family: Arial; background-color: #414042; color: #ffffff; font-size: 14px; opacity: 0.5; } #commands { padding: 5px; position: absolute; right: 1%; top: 2%; height: 65px; width: 180px; border-radius: 5px; border: 2px solid #f7941e; opacity: 0.7; font-family: Arial; background-color: #414042; color: #ffffff; font-size: 14px; opacity: 0.5; } a { color: #f7941e; text-decoration: none; } a:hover { color: #ffffff; } </style> </head> <body> <x3d id="pythonocc-x3d-scene" style="width:100%;border: none" > <Scene> <transform id="glbal_scene_rotation_Id" rotation="1 0 0 -1.57079632679"> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpc25d160f781f422ca31bc794b3618642.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpa0f0ed052c344d3696261f9899ada935.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp2449bc1902454ab4bab3d21add1e77dc.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp5657e463e7674666a424c6d2b4623dbb.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp8f770254e0bb4a5fbab99b99641b15f7.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp122af778af0448b79c5675fdc6f5222a.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpaa6f8bf289594f90afb3b537a1edff16.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpda38e1c2fda04374a402bac67b8d9f02.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpa700bb2638324993845e39dcb40a1aca.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpf646e0a35d004f8fb4d6d0eb0ddd43f1.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp72958ff606ab4149b4b674fc3aa773c9.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp149c23c0f5aa4eb5ae8817fb3f1e5c75.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpf29ffc76f6ef45af82130e33ea47853d.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp48499428b1f847eea959a83ceaf9b257.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp127aae94205844cca5fdb2ff95da5810.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp29bcf31694c84b379a253ab999e1355e.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpfc1c1eb4f6c241ceac82f34128aae916.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp61168515df204cfeb8941740e3991b1c.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp0b9fea39194e40e78c9df43527cd4935.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp8085468b17ec4912bcafb5e5717300a1.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp81f16c7400854e76b40c43e3672b33d8.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp34b62cd62c47485499cc655c990959ee.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpd6adf1b6027242be890f59c1f8592fa4.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpe1f07203ac8748048a46c594dc7aba07.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpd91f7691aa07495c936062ce4ca0a779.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpaba2385472d3430687ad204ce5dc19ef.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp712c147d000d42989c0e1112e3f211dd.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpff70ce255d0642c7a1c5ee8af4164456.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpd55d42fbf28f4317bc0b9503f2449cd1.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpda4a14efa04443988f17a84cba2fc19a.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp9db7fe0f40124a07af5918cf465c0e76.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpef5147fa214a4c23b25d91bb64b4ec2b.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp821b3b9bc76c4a9e97e26c3c11bd20e9.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shpd9146d69a6564bccbe31f44b7c9e1a79.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp2d37e25e37d140ae8d5e7181108909e9.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="M_shp4668f885628f4e7784fc9a8107a78d94.x3d"></Inline> <Inline onload="fitCamera()" mapDEFToID="true" url="allEdges.x3d"></Inline> </transform> </Scene> </x3d> <div id="pythonocc_rocks"> pythonocc-7.4.0 <a href="https://www.x3dom.org" target="_blank">x3dom</a> renderer <br>Check our blog at <a href=http://www.pythonocc.org>http://www.pythonocc.org</a> </div> <div id="commands"> <b>t</b> view/hide shape<br> <b>r</b> reset view<br> <b>a</b> show all<br> <b>u</b> upright<br> </div> <script> var selected_target_color = null; var current_selected_shape = null; var current_mat = null; function fitCamera() { var x3dElem = document.getElementById('pythonocc-x3d-scene'); x3dElem.runtime.fitAll(); } function select(the_shape) // called whenever a shape is clicked { // restore color for previous selected shape if (current_mat) { current_mat.diffuseColor = selected_target_color; } // store the shape for future process current_selected_shape = the_shape; console.log(the_shape); // store color, to be restored later appear = current_selected_shape.getElementsByTagName("Appearance")[0]; mat = appear.getElementsByTagName("Material")[0]; current_mat = mat; console.log(mat); selected_target_color = mat.diffuseColor; mat.diffuseColor = "1, 0.65, 0"; //console.log(the_shape.getElementsByTagName("Appearance"));//.getAttribute('diffuseColor')); } function onDocumentKeyPress(event) { event.preventDefault(); if (event.key=="t") { // t key if (current_selected_shape) { if (current_selected_shape.render == "true") { current_selected_shape.render = "false"; } else { current_selected_shape.render = "true"; } } } } // add events document.addEventListener('keypress', onDocumentKeyPress, false); </script> </body> </html>
HTML(html2
.replace('event.preventDefault','//event.preventDefault')
.replace('height: 38px','_height: 38px')
.replace('height: 65px','_height: 65px') )