{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x3d.py package loaded, have fun with X3D Graphics!\n" ] } ], "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.VrmlData import VrmlData_WorldInfo\n", "from OCC.Core.XCAFDoc import XCAFDoc_ShapeTool, XCAFDoc_DocumentTool, XCAFDoc_DocumentTool_ShapeTool\n", "from OCC.Core.XCAFApp import XCAFApp_Application_GetApplication\n", "from OCC.Core.XCAFDoc import XCAFDoc_DocumentTool_ShapeTool, xcafdoc\n", "from OCC.Core.BRep import BRep_Tool\n", "from OCC.Core.TDF import TDF_LabelSequence, TDF_Label, TDF_ChildIterator, TDF_AttributeIterator, TDF_Tool, TDF_AttributeMap, TDF_Attribute, TDF_ChildIDIterator\n", "from OCC.Core.TCollection import TCollection_AsciiString\n", "from OCC.Core.TDataStd import TDataStd_TreeNode\n", "from OCC.Core.Standard import Standard_GUID #, Standard_Handle\n", "#from OCC.Core.Standard import Standard_Real \n", "from OCC.Core.TNaming import TNaming_UsedShapes\n", "from OCC.Extend.TopologyUtils import TopologyExplorer\n", "from OCC.Extend.TopologyUtils import is_edge, is_wire, discretize_edge, discretize_wire\n", "from OCC.Extend.DataExchange import read_step_file_with_names_colors, read_step_file\n", "from OCC.Display.WebGl.x3dom_renderer import X3DExporter, X3DomRenderer\n", "from OCC.Core.Tesselator import ShapeTesselator\n", "from OCC.Core.TopAbs import topabs\n", "from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh\n", "from OCC.Core.gp import gp_XYZ, gp_Vec\n", "\n", "import xml.etree.ElementTree as ET\n", "from IPython.display import HTML\n", "import x3d.x3d as XX3D\n", "import re\n" ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [], "source": [ "#stp_filename = 'VentilatorAP203' #'as1_pe_203'\n", "#stp_filename = 'as1-oc-214'\n", "stp_filename = 'as1_pe_203'\n", "#stp_filename = 'screw'\n", "#stp_filename = 'RC_Buggy_2_front_suspension'\n", "stp_path = os.path.join('assets', stp_filename + '.stp')\n", "\n", "# load the STEP file\n", "doc = TDocStd_Document(TCollection_ExtendedString(\"pythonocc-doc\"))\n", "\n", "step_reader = STEPCAFControl_Reader()\n", "step_reader.SetColorMode(True)\n", "step_reader.SetLayerMode(True)\n", "step_reader.SetNameMode(True)\n", "step_reader.SetMatMode(True)\n", "step_reader.SetGDTMode(True)\n", "\n", "status = step_reader.ReadFile(stp_path)\n", "if status == IFSelect_RetDone:\n", " step_reader.Transfer(doc)\n", "else:\n", "\traise IOError(\"STEP file could not be read.\")" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 143, "metadata": {}, "output_type": "execute_result" } ], "source": [ "doc" ] }, { "cell_type": "code", "execution_count": 151, "metadata": {}, "outputs": [], "source": [ "##Copyright 2018 Thomas Paviot (tpaviot@gmail.com)\n", "##(C) 2020 Andreas Plesch\n", "##\n", "##This file is part of pythonOCC.\n", "##\n", "##pythonOCC is free software: you can redistribute it and/or modify\n", "##it under the terms of the GNU Lesser General Public License as published by\n", "##the Free Software Foundation, either version 3 of the License, or\n", "##(at your option) any later version.\n", "##\n", "##pythonOCC is distributed in the hope that it will be useful,\n", "##but WITHOUT ANY WARRANTY; without even the implied warranty of\n", "##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n", "##GNU Lesser General Public License for more details.\n", "##\n", "##You should have received a copy of the GNU Lesser General Public License\n", "##along with pythonOCC. If not, see .\n", "\n", "import os\n", "\n", "from OCC.Core.TopoDS import TopoDS_Shape\n", "from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh\n", "from OCC.Core.StlAPI import stlapi_Read, StlAPI_Writer\n", "from OCC.Core.BRep import BRep_Builder\n", "from OCC.Core.gp import gp_Pnt, gp_Dir, gp_Pnt2d\n", "from OCC.Core.Bnd import Bnd_Box2d\n", "from OCC.Core.TopoDS import TopoDS_Compound\n", "from OCC.Core.IGESControl import IGESControl_Reader, IGESControl_Writer\n", "from OCC.Core.STEPControl import STEPControl_Reader, STEPControl_Writer, STEPControl_AsIs\n", "from OCC.Core.Interface import Interface_Static_SetCVal\n", "from OCC.Core.IFSelect import IFSelect_RetDone, IFSelect_ItemsByEntity\n", "from OCC.Core.TDocStd import TDocStd_Document\n", "from OCC.Core.XCAFDoc import (XCAFDoc_DocumentTool_ShapeTool,\n", " XCAFDoc_DocumentTool_ColorTool)\n", "from OCC.Core.STEPCAFControl import STEPCAFControl_Reader\n", "from OCC.Core.TDF import TDF_LabelSequence, TDF_Label\n", "from OCC.Core.TCollection import TCollection_ExtendedString\n", "from OCC.Core.Quantity import Quantity_Color, Quantity_TOC_RGB\n", "from OCC.Core.TopLoc import TopLoc_Location\n", "from OCC.Core.BRepBuilderAPI import BRepBuilderAPI_Transform\n", "\n", "from OCC.Extend.TopologyUtils import (discretize_edge, get_sorted_hlr_edges,\n", " list_of_shapes_to_compound)\n", "\n", "try:\n", " import svgwrite\n", " HAVE_SVGWRITE = True\n", "except ImportError:\n", " HAVE_SVGWRITE = False\n", "\n", "def scenegraph_from_document_with_names_colors(doc):\n", " \"\"\" Returns a scene as list of node dictionaries\n", " Uses OCAF.\n", " \"\"\"\n", " output_shapes = {}\n", " scene = []\n", " visited = {}\n", " DEFset = set()\n", "\n", " shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())\n", " color_tool = XCAFDoc_DocumentTool_ColorTool(doc.Main())\n", " \n", " locs = []\n", " \n", " def _unescapeStep(name):\n", " \n", " def _toUnicode(match):\n", " return chr(int(match.group(1), 16))\n", " reg1 = re.compile(r'\\\\X\\\\(..)')\n", " reg2 = re.compile(r'\\\\X2\\\\(....)\\\\X0\\\\')\n", " reg3 = re.compile(r'\\\\X4\\\\(........)\\\\X0\\\\')\n", " \n", " return reg3.sub(_toUnicode, \n", " reg2.sub(_toUnicode, \n", " reg1.sub(_toUnicode, name)))\n", " \n", " def _get_sub_shapes(lab, loc, parent):\n", " \n", " name = _unescapeStep(lab.GetLabelName())\n", " labelString = lab.EntryDumpToString()\n", " if labelString in visited:\n", " return\n", " visited[labelString] = lab\n", "\n", " if shape_tool.IsAssembly(lab):\n", " node = {\n", " 'node' : 'Group',\n", " 'DEF' : labelString,\n", " 'name' : name,\n", " 'children' : []\n", " }\n", " l_c = TDF_LabelSequence()\n", " shape_tool.GetComponents(lab, l_c)\n", " for i in range(l_c.Length()):\n", " label = l_c.Value(i + 1)\n", " #print(\"Group Name DEF :\", name, labelString) \n", " if shape_tool.IsReference(label):\n", " print(\"######## component label :\", _unescapeStep(label.GetLabelName()) )\n", " loc = shape_tool.GetLocation(label)\n", " #print(\" Transform loc DEF :\", loc.HashCode(100))\n", " label_reference = TDF_Label()\n", " shape_tool.GetReferredShape(label, label_reference)\n", " reference_name = _unescapeStep(label_reference.GetLabelName())\n", " print(\"######## Transform USE to DEF ==> referenced label :\", reference_name )\n", " trafo = {\n", " 'node' : 'Transform',\n", " 'DEF' : label.EntryDumpToString(),\n", " 'name' : reference_name + '-trafo',\n", " 'transform' : loc,\n", " 'children': []\n", " } \n", " \n", " reference_entry = label_reference.EntryDumpToString()\n", " if reference_entry in DEFset: #already defined, use USE\n", " reference = {\n", " 'node' : 'Transform',\n", " 'USE' : reference_entry,\n", " 'name' : reference_name + '-ref'\n", " }\n", " trafo['children'].append(reference)\n", " else:\n", " DEFset.add(reference_entry)\n", " _get_sub_shapes(label_reference, loc, trafo['children']) \n", " node['children'].append(trafo)\n", " \n", " elif shape_tool.IsSimpleShape(lab):\n", " #print(\"Transform DEF Shape Name :\", name, labelString )\n", " shape = shape_tool.GetShape(lab)\n", " \n", " c = _set_color(lab, shape)\n", " clabel = color_tool.FindColor(c)\n", " clabelString = clabel.EntryDumpToString()\n", " \n", " n = c.Name(c.Red(), c.Green(), c.Blue())\n", " #print(' instance color Name & RGB: ', n, c.Red(), c.Green(), c.Blue())\n", " \n", " labloc = shape_tool.GetLocation(lab)\n", " #print(\" Shape Transform: \", labloc.HashCode(100))\n", " \n", " ##subshapes\n", " l_subss = TDF_LabelSequence()\n", " shape_tool.GetSubShapes(lab, l_subss)\n", " \n", " #node = {}\n", " \n", " if (l_subss.Length() == 0 and labloc.IsIdentity()): # does not need transform\n", " # but still needs transform wrapper for proper USE reference\n", " node = {\n", " 'node' : 'Transform',\n", " 'DEF' : labelString,\n", " 'name' : name + '-wrapper',\n", " 'children' : []\n", " }\n", " \n", " shapenode = {\n", " 'node' : 'Shape',\n", " 'label' : lab,\n", " 'shape' : shape,\n", " 'name' : name + '-shape',\n", " 'colorString' : f\"{c.Red()} {c.Green()} {c.Blue()}\",\n", " 'color' : (c.Red(), c.Green(), c.Blue()),\n", " 'colorDEF' : clabelString\n", " }\n", " \n", " node['children'].append(shapenode)\n", "\n", " else: # needs grouping or has transform\n", " node = {\n", " 'node' : 'Transform',\n", " 'DEF' : labelString,\n", " 'transform' : labloc,\n", " 'transformhash' : labloc.HashCode(100),\n", " 'name' : name,\n", " 'children' : []\n", " }\n", "\n", " shapenode = {\n", " 'node' : 'Shape',\n", " 'label' : lab,\n", " 'shape' : shape,\n", " 'name' : name + '-shape',\n", " 'colorString' : f\"{c.Red()} {c.Green()} {c.Blue()}\",\n", " 'color' : (c.Red(), c.Green(), c.Blue()),\n", " 'colorDEF' : clabelString\n", " }\n", " \n", " node['children'].append(shapenode)\n", " \n", " for i in range(l_subss.Length()):\n", " lab_subs = l_subss.Value(i+1)\n", " print(\"######## simpleshape subshape label :\", name)\n", " shape_sub = shape_tool.GetShape(lab_subs)\n", "\n", " c = _set_color(lab_subs, shape_sub)\n", " clabel = color_tool.FindColor(c)\n", " clabelString = clabel.EntryDumpToString()\n", "\n", " n = c.Name(c.Red(), c.Green(), c.Blue())\n", " #print(' shape color Name & RGB: ', n, c.Red(), c.Green(), c.Blue())\n", " \n", " subloc = shape_tool.GetLocation(lab_subs)\n", " #print(\" subshape Transform: \", subloc.HashCode(100))\n", " shapenode = {\n", " 'node' : 'SubShape',\n", " 'label' : lab_subs,\n", " 'shape' : shape_sub,\n", " 'DEF' : lab_subs.EntryDumpToString(),\n", " 'name' : _unescapeStep(lab_subs.GetLabelName())+'-subshape',\n", " 'colorString' : f\"{c.Red()} {c.Green()} {c.Blue()}\",\n", " 'color' : (c.Red(), c.Green(), c.Blue()),\n", " 'colorDEF' : clabelString,\n", " 'trafo' : subloc \n", " }\n", " node['children'].append(shapenode)\n", " \n", " parent.append(node)\n", "\n", " def _set_color(lab, shape):\n", " c = Quantity_Color(0.5, 0.5, 0.5, Quantity_TOC_RGB) # default color\n", " colorSet = False\n", " if (color_tool.GetInstanceColor(shape, 0, c) or\n", " color_tool.GetInstanceColor(shape, 1, c) or\n", " color_tool.GetInstanceColor(shape, 2, c)):\n", "\n", " colorSet = True\n", "\n", " if not colorSet:\n", " if (color_tool.GetColor(lab, 0, c) or\n", " color_tool.GetColor(lab, 1, c) or\n", " color_tool.GetColor(lab, 2, c)):\n", "\n", " colorSet = True\n", "\n", " if colorSet:\n", " color_tool.SetInstanceColor(shape, 0, c)\n", " color_tool.SetInstanceColor(shape, 1, c)\n", " color_tool.SetInstanceColor(shape, 2, c)\n", " \n", " return c\n", "\n", " def _get_shapes():\n", " labels = TDF_LabelSequence()\n", " shape_tool.GetFreeShapes(labels)\n", " \n", " print(\"Number of shapes at root :\", labels.Length())\n", " \n", " for i in range(labels.Length()):\n", " root_item = labels.Value(i+1)\n", " _get_sub_shapes(root_item, None, scene)\n", "\n", " _get_shapes()\n", " print('DONE')\n", " return scene\n", "\n", "def x3d_from_scenegraph(scene=[], doc=None):\n", " \n", " #shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())\n", " \n", " appDEFset = {}\n", " \n", " def _x3dapplyLocation(x3dtransformnode, location):\n", " # get translation and rotation from location\n", " transformation = location.Transformation()\n", " rot_axis = gp_XYZ()\n", " #rot_angle = 0.0\n", " success, rot_angle = transformation.GetRotation(rot_axis)#.GetVectorAndAngle(rot_axis, rot_angle)\n", " translation = transformation.TranslationPart()\n", " scale_factor = transformation.ScaleFactor()\n", " x3dtransformnode.rotation = (rot_axis.X(), rot_axis.Y(), rot_axis.Z(), rot_angle)\n", " x3dtransformnode.translation = (translation.X(), translation.Y(), translation.Z())\n", " x3dtransformnode.scale = (scale_factor, scale_factor, scale_factor)\n", " return\n", "\n", " def _x3dgeofromTShape(shape):\n", " geo = XX3D.Box()\n", " #if label.IsNull():\n", " # return geo\n", " #shape = shape_tool.GetShape(label)\n", " tesselator = ShapeTesselator(shape)\n", " tesselator.Compute(\n", " compute_edges=False,\n", " mesh_quality=1,\n", " parallel=True)\n", " x3dstring = tesselator.ExportShapeToX3DIndexedFaceSet()#x3dexp._triangle_sets[0] # there should be just one\n", " element = ET.XML(x3dstring)\n", " geo = XX3D.Box()\n", " if (element.tag == 'TriangleSet'):\n", " coordele = list(element.iter('Coordinate'))[0]\n", " normalele = list(element.iter('Normal'))[0]\n", " coord = XX3D.Coordinate(point = _MFVec3ffromString(coordele.attrib['point']))\n", " normal = XX3D.Normal(vector = _MFVec3ffromString(normalele.attrib['vector']))\n", " geo = XX3D.TriangleSet(coord = coord, normal=normal, solid=False)\n", " # get tesselated triangleset or lineset\n", " return geo\n", " \n", " def _MFVec3ffromString(sepString):\n", " mflist = sepString.split()\n", " mf = []\n", " for i in range(len(mflist)):\n", " if (i % 3 == 2):\n", " mf.append((float(mflist[i-2]), float(mflist[i-1]), float(mflist[i])))\n", " return mf\n", " \n", " def _x3dappfromColor(c, DEF):\n", " if DEF in appDEFset:\n", " return XX3D.Appearance(USE = DEF)\n", " else:\n", " x3dmat = XX3D.Material(diffuseColor = c, specularColor = (0.5, 0.5, 0.5))\n", " return XX3D.Appearance(DEF = DEF, material = x3dmat)\n", " \n", " def _getx3dnode(node):\n", " \n", " def _sanitizeDEF(name):\n", "# IdFirstChar ::=\n", "# Any ISO-10646 character encoded using UTF-8 except: 0x30-0x3a, 0x0-0x20, 0x22, 0x23, 0x27, 0x2b, 0x2c, 0x2d, 0x2e, 0x5b, 0x5c, 0x5d, 0x7b, 0x7d, 0x7f ;\n", "# first no [0-9],space,\",#,',+,comma,-,.,[,\\,],{,}\n", "# IdRestChars ::=\n", "# Any number of ISO-10646 characters except: 0x0-0x20, 0x22, 0x23, 0x27, 0x2c, 0x2e, 0x3a, 0x5b, 0x5c, 0x5d, 0x7b, 0x7d, 0x7f ;\n", "# rest no space,\",#,',comma,.,:,[,\\,],{,}\n", " return 'L-' + ( name\n", " .replace(\" \",\"_\")\n", " .replace('\"','^')\n", " .replace('#','N')\n", " .replace(\"'\",\"^\")\n", " .replace(\",\",\";\")\n", " .replace(\".\",\";\")\n", " .replace(\":\",\"-\")\n", " .replace(\"[\",\"(\")\n", " .replace(\"]\",\")\")\n", " .replace(\"{\",\"(\")\n", " .replace(\"}\",\")\")\n", " .replace(\"\\\\\",\"/\") )\n", "\n", " def _applyDEFUSE(x3dnode):\n", " if 'USE' in node:\n", " x3dnode.USE = _sanitizeDEF(node['USE'])\n", " if 'DEF' in node:\n", " x3dnode.DEF = _sanitizeDEF(node['DEF'])\n", " return\n", " \n", " def _applychildren(x3dnode):\n", " if 'children' in node:\n", " for n in node['children']:\n", " success, child = _getx3dnode(n)\n", " if success:\n", " x3dnode.children.append(child)\n", " return\n", " \n", " if not 'node' in node:\n", " print('no node type, skipping')\n", " return False, None\n", " \n", " ntype = node['node']\n", " \n", " # vrml does not allow : in DEF names but x3d does, TODO sanitize\n", " if (ntype == 'Group'):\n", " x3dnode = XX3D.Group(class_ = node['name'], children = [])\n", " _applyDEFUSE(x3dnode)\n", " _applychildren(x3dnode)\n", " \n", " elif (ntype == 'Transform'):\n", " x3dnode = XX3D.Transform(class_ = node['name'], children = [])\n", " if 'transform' in node:\n", " _x3dapplyLocation(x3dnode, node['transform'])\n", " _applyDEFUSE(x3dnode)\n", " _applychildren(x3dnode)\n", " \n", " elif (ntype == 'Shape' or ntype == 'SubShape'):\n", " x3dnode = XX3D.Shape (class_ = node['name'])\n", " _applyDEFUSE(x3dnode)\n", " if 'shape' in node:\n", " x3dnode.geometry = _x3dgeofromTShape(node['shape'])\n", " if 'color' in node:\n", " x3dnode.appearance = _x3dappfromColor(node['color'], node['colorDEF'])\n", " \n", " else:\n", " print ('unknown node: --' + ntype + \"--\")\n", " \n", " return True, x3dnode\n", "\n", " x3dscene = XX3D.Scene(children=[])\n", " x3ddoc = XX3D.X3D(Scene = x3dscene)\n", " \n", " for node in scene:\n", " success, x3dnode = _getx3dnode(node)\n", " if success:\n", " x3dscene.children.append(x3dnode)\n", " else:\n", " print('no x3d for root node, skipped')\n", " return x3ddoc" ] }, { "cell_type": "code", "execution_count": 152, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Number of shapes at root : 1\n", "######## component label : PLATE\n", "######## Transform USE to DEF ==> referenced label : PLATE\n", "######## component label : =>[0:1:1:3]\n", "######## Transform USE to DEF ==> referenced label : SOLID\n", "######## component label : =>[0:1:1:4]\n", "######## Transform USE to DEF ==> referenced label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## component label : L_BRACKET_ASSEMBLY\n", "######## Transform USE to DEF ==> referenced label : L_BRACKET_ASSEMBLY_ASM\n", "######## component label : L-BRACKET\n", "######## Transform USE to DEF ==> referenced label : L-BRACKET\n", "######## component label : =>[0:1:1:7]\n", "######## Transform USE to DEF ==> referenced label : SOLID\n", "######## component label : =>[0:1:1:8]\n", "######## Transform USE to DEF ==> referenced label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## component label : NUT_BOLT_ASSEMBLY\n", "######## Transform USE to DEF ==> referenced label : NUT_BOLT_ASSEMBLY_ASM\n", "######## component label : BOLT\n", "######## Transform USE to DEF ==> referenced label : BOLT\n", "######## component label : =>[0:1:1:11]\n", "######## Transform USE to DEF ==> referenced label : SOLID\n", "######## component label : =>[0:1:1:12]\n", "######## Transform USE to DEF ==> referenced label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## component label : NUT\n", "######## Transform USE to DEF ==> referenced label : NUT\n", "######## component label : =>[0:1:1:14]\n", "######## Transform USE to DEF ==> referenced label : SOLID\n", "######## component label : =>[0:1:1:15]\n", "######## Transform USE to DEF ==> referenced label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## component label : NUT_BOLT_ASSEMBLY\n", "######## Transform USE to DEF ==> referenced label : NUT_BOLT_ASSEMBLY_ASM\n", "######## component label : NUT_BOLT_ASSEMBLY\n", "######## Transform USE to DEF ==> referenced label : NUT_BOLT_ASSEMBLY_ASM\n", "######## component label : L_BRACKET_ASSEMBLY\n", "######## Transform USE to DEF ==> referenced label : L_BRACKET_ASSEMBLY_ASM\n", "######## component label : ROD\n", "######## Transform USE to DEF ==> referenced label : ROD_ASM\n", "######## component label : ROD\n", "######## Transform USE to DEF ==> referenced label : ROD\n", "######## component label : =>[0:1:1:18]\n", "######## Transform USE to DEF ==> referenced label : SOLID\n", "######## component label : =>[0:1:1:19]\n", "######## Transform USE to DEF ==> referenced label : COMPOUND\n", "######## simpleshape subshape label : COMPOUND\n", "######## component label : NUT\n", "######## Transform USE to DEF ==> referenced label : NUT\n", "######## component label : NUT\n", "######## Transform USE to DEF ==> referenced label : NUT\n", "DONE\n" ] } ], "source": [ "ascene=scenegraph_from_document_with_names_colors(doc)\n", "x3ddoc=x3d_from_scenegraph(ascene, doc)" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n" ] } ], "source": [ "print(x3ddoc.XML())" ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [], "source": [ "x3domHEAD = ''' \n", "'''" ] }, { "cell_type": "code", "execution_count": 155, "metadata": {}, "outputs": [], "source": [ "x3dele = list(ET.XML(x3ddoc.XML()).iter('X3D'))[0]\n", "x3dHTML = ET.tostring(x3dele, encoding=\"unicode\", short_empty_elements=False)" ] }, { "cell_type": "code", "execution_count": 156, "metadata": {}, "outputs": [ { "data": { "text/html": [ " \n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "" ], "text/plain": [ "" ] }, "execution_count": 156, "metadata": {}, "output_type": "execute_result" } ], "source": [ "HTML(x3domHEAD + x3dHTML)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "pythonocc", "language": "python", "name": "pythonocc" }, "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.8.2" } }, "nbformat": 4, "nbformat_minor": 4 }