{ "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.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.TopAbs import topabs\n", "from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh\n", "import xml.etree.ElementTree as ET\n", "from IPython.display import HTML\n", "import x3d.x3d as X3D\n", "import re\n" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "#stp_filename = 'VentilatorAP203' #'as1_pe_203'\n", "stp_filename = 'as1-oc-214'\n", "#stp_filename = 'as1_pe_203'\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": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "doc" ] }, { "cell_type": "code", "execution_count": 9, "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 read_document_with_names_colors(doc):\n", " \"\"\" Returns list of tuples (topods_shape, label, color)\n", " Use OCAF.\n", " \"\"\"\n", " output_shapes = {}\n", " scene = []\n", " visited = {}\n", " DEFset = set()\n", "\n", " # Get root assembly\n", " shape_tool = XCAFDoc_DocumentTool_ShapeTool(doc.Main())\n", " color_tool = XCAFDoc_DocumentTool_ColorTool(doc.Main())\n", " \n", " locs = []\n", "\n", " def _get_sub_shapes(lab, loc, parent):\n", " #global cnt, lvl\n", " #cnt += 1\n", " #print(\"\\n[%d] level %d, handling LABEL %s\\n\" % (cnt, lvl, _get_label_name(lab)))\n", " #print()\n", " #print(lab.DumpToString())\n", " #print()\n", " #print(\"Is Assembly :\", shape_tool.IsAssembly(lab))\n", " #print(\"Is Free :\", shape_tool.IsFree(lab))\n", " #print(\"Is Shape :\", shape_tool.IsShape(lab))\n", " #print(\"Is Compound :\", shape_tool.IsCompound(lab))\n", " #print(\"Is Component :\", shape_tool.IsComponent(lab))\n", " #print(\"Is SimpleShape :\", shape_tool.IsSimpleShape(lab))\n", " #print(\"Is Reference :\", shape_tool.IsReference(lab))\n", "\n", " #users = TDF_LabelSequence()\n", " #users_cnt = shape_tool.GetUsers(lab, users)\n", " #print(\"Nr Users :\", users_cnt)\n", "\n", " #print(\"Nb subshapes :\", l_subss.Length())\n", " #l_comps = TDF_LabelSequence()\n", " #shape_tool.GetComponents(lab, l_comps)\n", " #print(\"Nb components :\", l_comps.Length())\n", " #print()\n", " name = 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 :\", label.GetLabelName() )\n", " loc = shape_tool.GetLocation(label)\n", " print(\" Transform loc DEF :\", loc.HashCode(100))\n", " trafo = {\n", " 'node' : 'Transform',\n", " 'DEF' : label.EntryDumpToString(),\n", " 'name' : 'reference location',\n", " 'transform' : loc,\n", " 'children': []\n", " }\n", " locs.append(loc)\n", " print(\"loc chain depth :\", len(locs))\n", " \n", " label_reference = TDF_Label()\n", " shape_tool.GetReferredShape(label, label_reference)\n", " print(\"######## Transform USE to DEF ==> referenced label :\", label_reference.GetLabelName() )\n", " reference_entry = label_reference.EntryDumpToString()\n", " if reference_entry not in DEFset:\n", " DEFset.add(reference_entry)\n", " _get_sub_shapes(label_reference, loc, trafo['children'])\n", " else:\n", " trafo['children'].append({\n", " 'node' : 'Transform',\n", " 'USE' : reference_entry,\n", " 'refname' : label_reference.GetLabelName()\n", " })\n", " locs.pop()\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", " #print(\" all ass locs :\", locs)\n", "# loc = TopLoc_Location()\n", "# for l in locs:\n", "# loc = loc.Multiplied(l)\n", "\n", " c = _set_color(lab, shape)\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", " node = {\n", " 'node' : 'Transform',\n", " 'DEF' : labelString,\n", " 'name' : name,\n", " }\n", " \n", " #shape_disp = BRepBuilderAPI_Transform(shape, loc.Transformation()).Shape()\n", " #shape_disp = shape\n", " #if not shape_disp in output_shapes:\n", " # output_shapes[shape_disp] = [lab.GetLabelName(), c]\n", " \n", " ##subshapes\n", " l_subss = TDF_LabelSequence()\n", " shape_tool.GetSubShapes(lab, l_subss)\n", " \n", " if (l_subss.Length() == 0 and labloc.IsIdentity()): # does not need transform\n", " node['node'] = 'Shape'\n", " node['shape'] = lab\n", " node['name'] = name+'-shape'\n", " node['color'] = f\"{c.Red()} {c.Green()} {c.Blue()}\"\n", " else: # needs grouping or has transform\n", " node['transform'] = labloc\n", " node['children'] = []\n", " node['transformhash'] = labloc.HashCode(100)\n", " node['children'].append({\n", " 'node' : 'Shape',\n", " 'shape' : lab,\n", " 'name' : name+'-shape',\n", " 'color' : f\"{c.Red()} {c.Green()} {c.Blue()}\"\n", " })\n", " \n", " for i in range(l_subss.Length()):\n", " lab_subs = l_subss.Value(i+1)\n", " print(\"######## Transform DEF simpleshape subshape label :\", lab.GetLabelName())\n", " shape_sub = shape_tool.GetShape(lab_subs)\n", "\n", " c = _set_color(lab_subs, shape_sub)\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", " node['children'].append({\n", " 'node' : 'SubShape',\n", " 'shape' : lab_subs,\n", " 'DEF' : lab_subs.EntryDumpToString(),\n", " 'name' : lab_subs.GetLabelName()+'-subshape',\n", " 'color' : f\"{c.Red()} {c.Green()} {c.Blue()}\",\n", " 'trafo' : subloc \n", " })\n", " #shape_to_disp = BRepBuilderAPI_Transform(shape_sub, loc.Transformation()).Shape()\n", " #shape_to_disp = shape_sub\n", " # position the subshape to display\n", " #if not shape_to_disp in output_shapes:\n", " # output_shapes[shape_to_disp] = [lab_subs.GetLabelName(), c]\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", "\n", " def _get_shapes():\n", " labels = TDF_LabelSequence()\n", " shape_tool.GetFreeShapes(labels)\n", " #global cnt\n", " #cnt += 1\n", " \n", " print()\n", " print(\"Number of shapes at root :\", labels.Length())\n", " print()\n", " for i in range(labels.Length()):\n", " root_item = labels.Value(i+1)\n", " _get_sub_shapes(root_item, None, scene)\n", " _get_shapes()\n", " print('DONE')\n", " return scene\n", "\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Number of shapes at root : 1\n", "\n", "Group Name DEF : as1 0:1:1:1\n", "######## component label : rod-assembly_1\n", " Transform loc DEF : 25\n", "loc chain depth : 1\n", "######## Transform USE to DEF ==> referenced label : rod-assembly\n", "Group Name DEF : rod-assembly 0:1:1:2\n", "######## component label : nut_1\n", " Transform loc DEF : 73\n", "loc chain depth : 2\n", "######## Transform USE to DEF ==> referenced label : nut\n", "Transform DEF Shape Name : nut 0:1:1:3\n", " instance color Name & RGB: 412 1.0 0.0 0.0\n", " Shape Transform: 1\n", "Group Name DEF : rod-assembly 0:1:1:2\n", "######## component label : nut_2\n", " Transform loc DEF : 21\n", "loc chain depth : 2\n", "######## Transform USE to DEF ==> referenced label : nut\n", "Group Name DEF : rod-assembly 0:1:1:2\n", "######## component label : rod_1\n", " Transform loc DEF : 29\n", "loc chain depth : 2\n", "######## Transform USE to DEF ==> referenced label : rod\n", "Transform DEF Shape Name : rod 0:1:1:4\n", " instance color Name & RGB: 79 1.0 0.5 0.0\n", " Shape Transform: 1\n", "Group Name DEF : as1 0:1:1:1\n", "######## component label : l-bracket-assembly_1\n", " Transform loc DEF : 17\n", "loc chain depth : 1\n", "######## Transform USE to DEF ==> referenced label : l-bracket-assembly\n", "Group Name DEF : l-bracket-assembly 0:1:1:5\n", "######## component label : nut-bolt-assembly_1\n", " Transform loc DEF : 81\n", "loc chain depth : 2\n", "######## Transform USE to DEF ==> referenced label : nut-bolt-assembly\n", "Group Name DEF : nut-bolt-assembly 0:1:1:6\n", "######## component label : bolt_1\n", " Transform loc DEF : 49\n", "loc chain depth : 3\n", "######## Transform USE to DEF ==> referenced label : bolt\n", "Transform DEF Shape Name : bolt 0:1:1:7\n", " instance color Name & RGB: 22 0.0 0.0 1.0\n", " Shape Transform: 1\n", "Group Name DEF : nut-bolt-assembly 0:1:1:6\n", "######## component label : nut_3\n", " Transform loc DEF : 65\n", "loc chain depth : 3\n", "######## Transform USE to DEF ==> referenced label : nut\n", "Group Name DEF : l-bracket-assembly 0:1:1:5\n", "######## component label : nut-bolt-assembly_2\n", " Transform loc DEF : 45\n", "loc chain depth : 2\n", "######## Transform USE to DEF ==> referenced label : nut-bolt-assembly\n", "Group Name DEF : l-bracket-assembly 0:1:1:5\n", "######## component label : nut-bolt-assembly_3\n", " Transform loc DEF : 13\n", "loc chain depth : 2\n", "######## Transform USE to DEF ==> referenced label : nut-bolt-assembly\n", "Group Name DEF : l-bracket-assembly 0:1:1:5\n", "######## component label : l-bracket_1\n", " Transform loc DEF : 37\n", "loc chain depth : 2\n", "######## Transform USE to DEF ==> referenced label : l-bracket\n", "Transform DEF Shape Name : l-bracket 0:1:1:8\n", " instance color Name & RGB: 229 0.0 1.0 0.0\n", " Shape Transform: 1\n", "Group Name DEF : as1 0:1:1:1\n", "######## component label : plate_1\n", " Transform loc DEF : 61\n", "loc chain depth : 1\n", "######## Transform USE to DEF ==> referenced label : plate\n", "Transform DEF Shape Name : plate 0:1:1:9\n", " instance color Name & RGB: 512 0.800000011920929 1.0 0.0\n", " Shape Transform: 1\n", "Group Name DEF : as1 0:1:1:1\n", "######## component label : l-bracket-assembly_2\n", " Transform loc DEF : 57\n", "loc chain depth : 1\n", "######## Transform USE to DEF ==> referenced label : l-bracket-assembly\n", "DONE\n" ] } ], "source": [ "scene=read_document_with_names_colors(doc)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "[{'node': 'Group',\n", " 'DEF': '0:1:1:1',\n", " 'name': 'as1',\n", " 'children': [{'node': 'Transform',\n", " 'DEF': '0:1:1:1:1',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Group',\n", " 'DEF': '0:1:1:2',\n", " 'name': 'rod-assembly',\n", " 'children': [{'node': 'Transform',\n", " 'DEF': '0:1:1:2:1',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Shape',\n", " 'DEF': '0:1:1:3',\n", " 'name': 'nut-shape',\n", " 'shape': ,\n", " 'color': '1.0 0.0 0.0'}]},\n", " {'node': 'Transform',\n", " 'DEF': '0:1:1:2:2',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Transform',\n", " 'USE': '0:1:1:3',\n", " 'refname': 'nut'}]},\n", " {'node': 'Transform',\n", " 'DEF': '0:1:1:2:3',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Shape',\n", " 'DEF': '0:1:1:4',\n", " 'name': 'rod-shape',\n", " 'shape': ,\n", " 'color': '1.0 0.5 0.0'}]}]}]},\n", " {'node': 'Transform',\n", " 'DEF': '0:1:1:1:2',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Group',\n", " 'DEF': '0:1:1:5',\n", " 'name': 'l-bracket-assembly',\n", " 'children': [{'node': 'Transform',\n", " 'DEF': '0:1:1:5:1',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Group',\n", " 'DEF': '0:1:1:6',\n", " 'name': 'nut-bolt-assembly',\n", " 'children': [{'node': 'Transform',\n", " 'DEF': '0:1:1:6:1',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Shape',\n", " 'DEF': '0:1:1:7',\n", " 'name': 'bolt-shape',\n", " 'shape': ,\n", " 'color': '0.0 0.0 1.0'}]},\n", " {'node': 'Transform',\n", " 'DEF': '0:1:1:6:2',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Transform',\n", " 'USE': '0:1:1:3',\n", " 'refname': 'nut'}]}]}]},\n", " {'node': 'Transform',\n", " 'DEF': '0:1:1:5:2',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Transform',\n", " 'USE': '0:1:1:6',\n", " 'refname': 'nut-bolt-assembly'}]},\n", " {'node': 'Transform',\n", " 'DEF': '0:1:1:5:3',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Transform',\n", " 'USE': '0:1:1:6',\n", " 'refname': 'nut-bolt-assembly'}]},\n", " {'node': 'Transform',\n", " 'DEF': '0:1:1:5:4',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Shape',\n", " 'DEF': '0:1:1:8',\n", " 'name': 'l-bracket-shape',\n", " 'shape': ,\n", " 'color': '0.0 1.0 0.0'}]}]}]},\n", " {'node': 'Transform',\n", " 'DEF': '0:1:1:1:3',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Shape',\n", " 'DEF': '0:1:1:9',\n", " 'name': 'plate-shape',\n", " 'shape': ,\n", " 'color': '0.800000011920929 1.0 0.0'}]},\n", " {'node': 'Transform',\n", " 'DEF': '0:1:1:1:4',\n", " 'name': 'reference location',\n", " 'transform': ,\n", " 'children': [{'node': 'Transform',\n", " 'USE': '0:1:1:5',\n", " 'refname': 'l-bracket-assembly'}]}]}]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "scene" ] } ], "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 }