{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "from OCC.Core.IFSelect import IFSelect_RetDone\n", "from OCC.Core.STEPCAFControl import STEPCAFControl_Reader\n", "from OCC.Core.TCollection import TCollection_ExtendedString\n", "from OCC.Core.TDocStd import TDocStd_Document\n", "from OCC.Core.VrmlAPI import VrmlAPI_Writer\n", "from OCC.Core.XCAFDoc import XCAFDoc_ShapeTool, XCAFDoc_DocumentTool\n", "from OCC.Core.BRep import BRep_Tool\n", "from OCC.Core.TDF import TDF_LabelSequence, TDF_Label\n", "from OCC.Extend.TopologyUtils import TopologyExplorer\n", "from OCC.Core.TopAbs import topabs\n", "from OCC.Extend.DataExchange import read_step_file_with_names_colors\n", "from OCC.Display.WebGl.x3dom_renderer import X3DomRenderer, X3DExporter\n", "from IPython.display import HTML\n", "import xml.etree.ElementTree as ET\n", "import re" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Number of shapes at root : 1\n", "\n", "Name : AS1_PE_ASM\n", "Name : PLATE\n", "Name : SOLID\n", " instance color Name & RGB: 229 0.0 1.0 0.0\n", "Name : COMPOUND\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : L_BRACKET_ASSEMBLY_ASM\n", "Name : L-BRACKET\n", "Name : SOLID\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", "Name : COMPOUND\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT_BOLT_ASSEMBLY_ASM\n", "Name : BOLT\n", "Name : SOLID\n", " instance color Name & RGB: 22 0.0 0.0 1.0\n", "Name : COMPOUND\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT\n", "Name : SOLID\n", " instance color Name & RGB: 412 1.0 0.0 0.0\n", "Name : COMPOUND\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT_BOLT_ASSEMBLY_ASM\n", "Name : BOLT\n", "Name : SOLID\n", " instance color Name & RGB: 22 0.0 0.0 1.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT\n", "Name : SOLID\n", " instance color Name & RGB: 412 1.0 0.0 0.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT_BOLT_ASSEMBLY_ASM\n", "Name : BOLT\n", "Name : SOLID\n", " instance color Name & RGB: 22 0.0 0.0 1.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT\n", "Name : SOLID\n", " instance color Name & RGB: 412 1.0 0.0 0.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : L_BRACKET_ASSEMBLY_ASM\n", "Name : L-BRACKET\n", "Name : SOLID\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT_BOLT_ASSEMBLY_ASM\n", "Name : BOLT\n", "Name : SOLID\n", " instance color Name & RGB: 22 0.0 0.0 1.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT\n", "Name : SOLID\n", " instance color Name & RGB: 412 1.0 0.0 0.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT_BOLT_ASSEMBLY_ASM\n", "Name : BOLT\n", "Name : SOLID\n", " instance color Name & RGB: 22 0.0 0.0 1.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT\n", "Name : SOLID\n", " instance color Name & RGB: 412 1.0 0.0 0.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT_BOLT_ASSEMBLY_ASM\n", "Name : BOLT\n", "Name : SOLID\n", " instance color Name & RGB: 22 0.0 0.0 1.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT\n", "Name : SOLID\n", " instance color Name & RGB: 412 1.0 0.0 0.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : ROD_ASM\n", "Name : ROD\n", "Name : SOLID\n", " instance color Name & RGB: 22 0.0 0.0 1.0\n", "Name : COMPOUND\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT\n", "Name : SOLID\n", " instance color Name & RGB: 412 1.0 0.0 0.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n", "Name : NUT\n", "Name : SOLID\n", " instance color Name & RGB: 412 1.0 0.0 0.0\n", "Name : COMPOUND\n", " instance color Name & RGB: 510 1.0 1.0 0.0\n", " shape color Name & RGB: 510 1.0 1.0 0.0\n" ] } ], "source": [ "#stp_filename = 'VentilatorAP203' #'as1_pe_203'\n", "stp_filename = 'as1_pe_203'\n", "stp_path = os.path.join('..','assets', 'step', stp_filename + '.stp')\n", "#stp_path = os.path.join(stp_filename + '.stp')\n", "stpShapes = read_step_file_with_names_colors(stp_path)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "## x3dom webgl renderer - render axes/planes : True - axes/plane zoom factor : 1\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "X3D exporter, discretize an edge\n", "\\ meshing shapes... 100%" ] } ], "source": [ "x3domRenderer = X3DomRenderer(path=\"./\")\n", "for shape in stpShapes:\n", " #print(shape.ShapeType(),stpShapes[shape])\n", " #x3dexp = X3DExporter(shape, None, None, False, (1,0,0), (0,0,0), 0, 0, (0,0,0), 2, 1)\n", " #x3dexp.compute()\n", " #print(len(x3dexp._triangle_sets), len(x3dexp._line_sets))\n", " color = stpShapes[shape][1]\n", " colorRGB=(color.Red(), color.Green(), color.Blue())\n", " x3domRenderer.DisplayShape(shape=shape, color=colorRGB, export_edges=True)\n", "x3domRenderer.generate_html_file(False, 1.0)\n", " \n", " \n" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "#merge shp linesets\n", "for hash in x3domRenderer._x3d_shapes.keys():\n", " xml=ET.parse(hash+'.x3d')\n", " #print(xml)\n", " shp_vertexCounts=[]\n", " shp_points=[]\n", " for lineset in xml.iter('LineSet'):\n", " shp_vertexCounts.append(lineset.get('vertexCount'))\n", " shp_points.append(list(lineset.iter('Coordinate'))[0].get('point'))\n", " #print (shp_vertexCounts, shp_points)\n", " #reuse first lineset to hold all ines\n", " lset=list(xml.iter('LineSet'))[0]\n", " lset.set('vertexCount',\",\".join(shp_vertexCounts))\n", " coords=list(lset.iter('Coordinate'))[0]\n", " coords.set('point',\",\".join(shp_points))\n", " #remove all other lineset shapes\n", " group=list(xml.iter('Group'))[0]\n", " transforms=list(group.iter('Transform'))\n", " #skip first\n", " for transform in transforms[1:]:\n", " group.remove(transform)\n", " #print(ET.dump(group))\n", " xml.write(\"M_\"+hash+'.x3d')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "#merge edg linesets\n", "edg_vertexCounts=[]\n", "edg_points=[]\n", "for hash in x3domRenderer._x3d_edges.keys():\n", " xml=ET.parse(hash+'.x3d').getroot()\n", " firstlineset=list(xml.iter('LineSet'))[0]\n", " edg_vertexCounts.append(firstlineset.get('vertexCount'))\n", " edg_points.append(list(firstlineset.iter('Coordinate'))[0].get('point'))" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "firstedgex3d=list(x3domRenderer._x3d_edges.keys())[0]+'.x3d'\n", "xml=ET.parse(hash+'.x3d')\n", "lset=list(xml.iter('LineSet'))[0]\n", "lset.set('vertexCount',\",\".join(edg_vertexCounts))\n", "coords=list(lset.iter('Coordinate'))[0]\n", "coords.set('point',\",\".join(edg_points))\n", "ALLEDGESX3D='allEdges.x3d'\n", "xml.write(ALLEDGESX3D)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "with open('index.html','r') as file:\n", " html = file.read()" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", " pythonOCC 7.4.0 x3dom renderer\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", "\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\n", "\n", "
\n", " pythonocc-7.4.0 x3dom renderer\n", "
Check our blog at\n", " http://www.pythonocc.org\n", "
\n", "
\n", " t view/hide shape
\n", " r reset view
\n", " a show all
\n", " u upright
\n", "
\n", " \n", "\n", "\n", "\n" ] } ], "source": [ "html2=html.replace(firstedgex3d, ALLEDGESX3D)\n", "html2=re.sub(r\"\",\"\", html2)\n", "html2=re.sub(r'url=\"shp','url=\"M_shp', html2)\n", "print(html2)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " pythonOCC 7.4.0 x3dom renderer\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", "\n", " \n", "\t\n", "\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\t\n", "\t\t\n", "\t\n", "\n", "
\n", " pythonocc-7.4.0 x3dom renderer\n", "
Check our blog at\n", " http://www.pythonocc.org\n", "
\n", "
\n", " t view/hide shape
\n", " r reset view
\n", " a show all
\n", " u upright
\n", "
\n", " \n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTML(html2\n", " .replace('event.preventDefault','//event.preventDefault')\n", " .replace('height: 38px','_height: 38px')\n", " .replace('height: 65px','_height: 65px') )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 4 }