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

Refer to the Mesh Generation Example.

1#!/usr/bin/env python3
2
3# $Id$
4
5# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
6# Copyright (C) 2014-2025, CADEX. All rights reserved.
7
8# This file is part of the Manufacturing Toolkit 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
32import sys
33from pathlib import Path
34import os
35
36import manufacturingtoolkit.CadExMTK as mtk
37
38sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
39
40import mtk_license as license
41
42import math
43
44class FirstFaceGetter(mtk.ModelData_ModelElementVoidVisitor):
45 def __init__(self):
46 mtk.ModelData_ModelElementVoidVisitor.__init__(self)
47 self.myFace = None
48
49 def VisitPart(self, thePart: mtk.ModelData_Part):
50 if self.myFace is None:
51 aBodies = thePart.Bodies()
52 if thePart.NumberOfBodies() > 0:
53 self.ExploreBRep(aBodies)
54
55 def ExploreBRep(self, theBodies: mtk.Collections_BodyList):
56 for aBody in theBodies:
57 aShapeIt = mtk.ModelData_ShapeIterator(aBody, mtk.ShapeType_Face)
58 for aShape in aShapeIt:
59 self.myFace = mtk.ModelData_Face.Cast(aShape)
60 break
61
62 def FirstFace(self):
63 return self.myFace
64
65def PrintFaceTriangulationInfo(theFace: mtk.ModelData_Face):
66 anITS = theFace.Triangulation()
67
68 print(f"Face triangulation contains {anITS.NumberOfTriangles()} triangles.")
69
70 aNumberOfTrianglesToPrint = min(4, anITS.NumberOfTriangles())
71
72 for i in range(aNumberOfTrianglesToPrint):
73 print(f"Triangle index {i} with vertices: ")
74 for j in range(3):
75 aVertexIndex = anITS.TriangleVertexIndex (i, j);
76 aPoint = anITS.TriangleVertex(i, j);
77 print(f" Vertex index {aVertexIndex} with coords",
78 f"(X: {aPoint.X()}, Y: {aPoint.Y()}, Z: {aPoint.Z()})")
79
80def main(theSource: str):
81 aKey = license.Value()
82
83 if not mtk.LicenseManager.Activate(aKey):
84 print("Failed to activate Manufacturing Toolkit license.")
85 return 1
86
87 aModel = mtk.ModelData_Model()
88
89 if not mtk.ModelData_ModelReader().Read(mtk.UTF16String(theSource), aModel):
90 print("Failed to read the file " + theSource)
91 return 1
92
93 # Set up mesher and parameters
94 aParam = mtk.ModelAlgo_MeshGeneratorParameters()
95 aParam.SetAngularDeflection(math.pi * 10 / 180)
96 aParam.SetChordalDeflection(0.003)
97
98 aMesher = mtk.ModelAlgo_MeshGenerator(aParam)
99 aMesher.Generate(aModel)
100
101 aVisitor = FirstFaceGetter();
102 aModel.Accept(aVisitor);
103
104 aFace = aVisitor.FirstFace();
105 PrintFaceTriangulationInfo(aFace)
106
107 print("Completed")
108 return 0
109
110if __name__ == "__main__":
111 if len(sys.argv) != 2:
112 print("Usage: " + os.path.abspath(Path(__file__).resolve()) + " <input_file>, where:")
113 print(" <input_file> is a name of the file to be read")
114 sys.exit(1)
115
116 aSource = os.path.abspath(sys.argv[1])
117
118 sys.exit(main(aSource))