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-2026, 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 mtk.MTKCore as mtk
37
38sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
39sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../helpers/"))
40
41import LicenseHelper
42
43import mtk_license as license
44
45import math
46
47class FirstFaceGetter(mtk.ModelData_ModelElementVoidVisitor):
48 def __init__(self):
49 mtk.ModelData_ModelElementVoidVisitor.__init__(self)
50 self.myFace = None
51
52 def VisitPart(self, thePart: mtk.ModelData_Part):
53 if self.myFace is None:
54 aBodies = thePart.Bodies()
55 if thePart.NumberOfBodies() > 0:
56 self.ExploreBRep(aBodies)
57
58 def ExploreBRep(self, theBodies: mtk.Collections_BodyList):
59 for aBody in theBodies:
60 aShapeIt = mtk.ModelData_ShapeIterator(aBody, mtk.ShapeType_Face)
61 for aShape in aShapeIt:
62 self.myFace = mtk.ModelData_Face.Cast(aShape)
63 break
64
65 def FirstFace(self):
66 return self.myFace
67
68def PrintFaceTriangulationInfo(theFace: mtk.ModelData_Face):
69 anITS = theFace.Triangulation()
70
71 print(f"Face triangulation contains {anITS.NumberOfTriangles()} triangles.")
72
73 aNumberOfTrianglesToPrint = min(4, anITS.NumberOfTriangles())
74
75 for i in range(aNumberOfTrianglesToPrint):
76 print(f"Triangle index {i} with vertices: ")
77 for j in range(3):
78 aVertexIndex = anITS.TriangleVertexIndex (i, j);
79 aPoint = anITS.TriangleVertex(i, j);
80 print(f" Vertex index {aVertexIndex} with coords",
81 f"(X: {aPoint.X()}, Y: {aPoint.Y()}, Z: {aPoint.Z()})")
82
83def main(theSource: str):
84 aKey = license.Value()
85
86 if not LicenseHelper.SetupRuntimeKey() :
87 return 1
88
89 try:
90 mtk.LicenseManager.Activate(aKey)
91 except mtk.LicenseError as anException:
92 mtk.LicenseManager.Deactivate()
93 print("Failed to activate Manufacturing Toolkit license: " + anException.what())
94 return 1
95
96 aModel = mtk.ModelData_Model()
97
98 if not mtk.ModelData_ModelReader().Read(mtk.UTF16String(theSource), aModel):
99 mtk.LicenseManager.Deactivate()
100 print("Failed to read the file " + theSource)
101 return 1
102
103 # Set up mesher and parameters
104 aParam = mtk.ModelAlgo_MeshGeneratorParameters()
105 aParam.SetAngularDeflection(math.pi * 10 / 180)
106 aParam.SetChordalDeflection(0.003)
107
108 aMesher = mtk.ModelAlgo_MeshGenerator(aParam)
109 aMesher.Generate(aModel)
110
111 aVisitor = FirstFaceGetter();
112 aModel.Accept(aVisitor);
113
114 aFace = aVisitor.FirstFace();
115 PrintFaceTriangulationInfo(aFace)
116
117 print("Completed")
118
119 mtk.LicenseManager.Deactivate()
120
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 file to be read")
127 sys.exit(1)
128
129 aSource = os.path.abspath(sys.argv[1])
130
131 sys.exit(main(aSource))