|
| 1 | +##Copyright 2021 Thomas Paviot ([email protected]) |
| 2 | +## |
| 3 | +##This file is part of pythonOCC. |
| 4 | +## |
| 5 | +##pythonOCC is free software: you can redistribute it and/or modify |
| 6 | +##it under the terms of the GNU Lesser General Public License as published by |
| 7 | +##the Free Software Foundation, either version 3 of the License, or |
| 8 | +##(at your option) any later version. |
| 9 | +## |
| 10 | +##pythonOCC is distributed in the hope that it will be useful, |
| 11 | +##but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +##GNU Lesser General Public License for more details. |
| 14 | +## |
| 15 | +##You should have received a copy of the GNU Lesser General Public License |
| 16 | +##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +import os |
| 19 | +import random |
| 20 | + |
| 21 | +from OCC.Core.MeshDataSource import Mesh_DataSource |
| 22 | +from OCC.Core.RWStl import rwstl_ReadFile |
| 23 | +from OCC.Core.MeshVS import * |
| 24 | +from OCC.Core.Aspect import Aspect_SequenceOfColor |
| 25 | +from OCC.Core.Quantity import Quantity_Color, Quantity_NOC_RED, Quantity_NOC_BLUE1, Quantity_NOC_BLACK |
| 26 | +from OCC.Core.TColStd import TColStd_DataMapOfIntegerReal |
| 27 | + |
| 28 | +from OCC.Display.SimpleGui import init_display |
| 29 | + |
| 30 | +stl_filename = os.path.join('..', 'assets', 'models', 'fan.stl') |
| 31 | + |
| 32 | +# read the stl file |
| 33 | +a_stl_mesh = rwstl_ReadFile(stl_filename) |
| 34 | + |
| 35 | +# create the data source |
| 36 | +a_data_source = Mesh_DataSource(a_stl_mesh) |
| 37 | + |
| 38 | +# create a mesh from the data source |
| 39 | +a_mesh = MeshVS_Mesh() |
| 40 | +a_mesh.SetDataSource(a_data_source) |
| 41 | + |
| 42 | +# assign nodal builder to the mesh |
| 43 | +a_builder = MeshVS_NodalColorPrsBuilder(a_mesh, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask) |
| 44 | +a_builder.UseTexture(True) |
| 45 | + |
| 46 | +# prepare color map |
| 47 | +aColorMap = Aspect_SequenceOfColor() |
| 48 | +aColorMap.Append(Quantity_Color(Quantity_NOC_RED)) |
| 49 | +aColorMap.Append(Quantity_Color(Quantity_NOC_BLUE1)) |
| 50 | + |
| 51 | +# assign color scale map values (0..1) to nodes |
| 52 | +aScaleMap = TColStd_DataMapOfIntegerReal() |
| 53 | + |
| 54 | +# iterate through the nodes and add an node id and |
| 55 | +# an appropriate value to the map |
| 56 | +# color should be from 0. to 1. |
| 57 | +for anId in range(1000): # TODO use the mesh number of nodes |
| 58 | + aValue = random.uniform(0, 1) |
| 59 | + aScaleMap.Bind(anId, aValue) |
| 60 | + |
| 61 | +# pass color map and color scale values to the builder |
| 62 | +a_builder.SetColorMap(aColorMap) |
| 63 | +a_builder.SetInvalidColor(Quantity_Color(Quantity_NOC_BLACK)) |
| 64 | +a_builder.SetTextureCoords(aScaleMap) |
| 65 | +a_mesh.AddBuilder(a_builder, True) |
| 66 | + |
| 67 | +# display |
| 68 | +display, start_display, add_menu, add_function_to_menu = init_display() |
| 69 | +display.Context.Display(a_mesh, True) |
| 70 | +display.FitAll() |
| 71 | +start_display() |
0 commit comments