Hide menu
Loading...
Searching...
No Matches
meshing/visualizationmesher/visualizationmesher.py

Refer to the Visualization Mesher Example.

1#!/usr/bin/env python3
2
3# $Id$
4
5# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
6# Copyright (C) 2014-2023, CADEX. All rights reserved.
7
8# This file is part of the CAD Exchanger software.
9
10# You may use this file under the terms of the BSD license as follows:
11
12# Redistribution and use in source and binary forms, with or without
13# modification, are permitted provided that the following conditions are met:
14# * Redistributions of source code must retain the above copyright notice,
15# this list of conditions and the following disclaimer.
16# * Redistributions in binary form must reproduce the above copyright notice,
17# this list of conditions and the following disclaimer in the documentation
18# and/or other materials provided with the distribution.
19
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30# POSSIBILITY OF SUCH DAMAGE.
31
32
33import sys
34from pathlib import Path
35import os
36
37import cadexchanger.CadExCore as cadex
38import cadexchanger.CadExMesh as mesh
39
40sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + r"/../../"))
41import cadex_license as license
42
43import math
44
45
46class FirstFaceGetter(cadex.ModelData_Model_VoidElementVisitor):
47 def __init__(self):
48 cadex.ModelData_Model_VoidElementVisitor.__init__(self)
49 self.myFace = None
50
51 def VisitPart(self, thePart: cadex.ModelData_Part):
52 if self.myFace is None:
53 aBRep = thePart.BRepRepresentation()
54 self.ExploreBRep(aBRep)
55
56 def ExploreBRep(self, theBRep: cadex.ModelData_BRepRepresentation):
57 aBodyList = theBRep.Get()
58 aFirstBody = aBodyList.First()
59 for aShape in cadex.ModelData_Shape_Iterator(aFirstBody, cadex.ModelData_ST_Face):
60 self.myFace = cadex.ModelData_Face.Cast(aShape)
61 break
62
63 def FirstFace(self):
64 return self.myFace
65
66def PrintFaceToPolyAssociation(theFace: cadex.ModelData_Face,
68 aMeshPatch = theAssociations.Get(theFace)
69 anITS = cadex.ModelData_IndexedTriangleSet.Cast(aMeshPatch.PVS())
70 aTriangleIndices = aMeshPatch.Indices()
71
72 print(f"Face triangulation contains {len(aTriangleIndices)} triangles.")
73
74 aNumberOfTrianglesToPrint = min(4, len(aTriangleIndices))
75
76 for i in range(aNumberOfTrianglesToPrint):
77 aTriangleIndex = aTriangleIndices.Element(i)
78 print(f"Triangle index {aTriangleIndex} with vertices: ")
79 for aVertexNumber in range(3):
80 aVertexIndex = anITS.CoordinateIndex(aTriangleIndex, aVertexNumber);
81 aPoint = anITS.Coordinate(aVertexIndex);
82 print(f" Vertex index {aVertexIndex} with coords",
83 f"(X: {aPoint.X()}, Y: {aPoint.Y()}, Z: {aPoint.Z()})")
84
85def main(theSource: str):
86 aKey = license.Value()
87
88 if not cadex.LicenseManager.Activate(aKey):
89 print("Failed to activate CAD Exchanger license.")
90 return 1
91
92 aModel = cadex.ModelData_Model()
93
94 if not cadex.ModelData_ModelReader().Read(cadex.Base_UTF16String(theSource), aModel):
95 print("Failed to read the file " + theSource)
96 return 1
97
98 # Set up mesher and parameters
100 aParam.SetAngularDeflection(math.pi * 10 / 180)
101 aParam.SetChordalDeflection(0.003)
102 aParam.SetSaveBRepToPolyAssociations(True)
103
104 aMesher = cadex.ModelAlgo_BRepMesher(aParam)
105 aMesher.Compute(aModel, True)
106
107 # Example of using B-Rep to Poly representation associations:
108 aVisitor = FirstFaceGetter();
109 aModel.AcceptElementVisitor(aVisitor);
110
111 aFace = aVisitor.FirstFace();
112 aBRepToPolyAssociations = aMesher.BRepToPolyAssociations();
113 PrintFaceToPolyAssociation(aFace, aBRepToPolyAssociations)
114
115 # Save the result
116 if not cadex.ModelData_ModelWriter().Write(aModel, cadex.Base_UTF16String("out/VisMesher.cdx")):
117 print("Unable to save the model")
118 return 1
119
120 print("Completed")
121 return 0
122
123if __name__ == "__main__":
124 if len(sys.argv) != 2:
125 print("Usage: " + os.path.abspath(Path(__file__).resolve()) + " <input_file>, where:")
126 print(" <input_file> is a name of the XML file to be read")
127 sys.exit(1)
128
129 aSource = os.path.abspath(sys.argv[1])
130
131 sys.exit(main(aSource))
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Computes a polygonal representation from a B-Rep one.
Definition: ModelAlgo_BRepMesher.hxx:48
Defines parameters used by the B-Rep mesher.
Definition: ModelAlgo_BRepMesherParameters.hxx:33
Contains connecting information between B-Rep and Poly representation.
Definition: ModelData_BRepToPolyAssociations.hxx:42
static const ModelData_Face & Cast(const ModelData_Shape &theShape)
Cast operator.
Definition: ModelData_Face.cxx:282
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33
Writes any format that CAD Exchanger can export.
Definition: ModelData_ModelWriter.hxx:33