Hide menu
Loading...
Searching...
No Matches
helpers/shape_processor.py

Refer to the Model Explore Helper Implementation

1# $Id$
2#
3# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
4# Copyright (C) 2014-2025, CADEX. All rights reserved.
5#
6# This file is part of the Manufacturing Toolkit software.
7#
8# You may use this file under the terms of the BSD license as follows:
9#
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions are met:
12# * Redistributions of source code must retain the above copyright notice,
13# this list of conditions and the following disclaimer.
14# * Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29
30from abc import abstractmethod
31
32import manufacturingtoolkit.CadExMTK as mtk
33
34class ShapeProcessor(mtk.ModelData_ModelElementVoidVisitor):
35 def __init__(self):
36 super().__init__()
37 self.myPartIndex = 0
38
39 def VisitPart(self, thePart: mtk.ModelData_Part):
40 aPartName = "noname" if thePart.Name().IsEmpty() else thePart.Name()
41
42 aBodyList = thePart.Bodies()
43 i = 0
44 for aBody in aBodyList:
45 aShapeIt = mtk.ModelData_ShapeIterator(aBody)
46 for aShape in aShapeIt:
47 if aShape.Type() == mtk.ShapeType_Solid:
48 print("Part #", self.myPartIndex, " [\"", aPartName, "\"] - solid #", i, " has:", sep="")
49 i += 1
50 self.ProcessSolid(mtk.ModelData_Solid.Cast(aShape))
51 elif aShape.Type() == mtk.ShapeType_Shell:
52 print("Part #", self.myPartIndex, " [\"", aPartName, "\"] - shell #", i, " has:", sep="")
53 i += 1
54 self.ProcessShell(mtk.ModelData_Shell.Cast (aShape))
55 self.myPartIndex += 1
56
57 @abstractmethod
58 def ProcessSolid(self, theSolid: mtk.ModelData_Solid):
59 pass
60
61 @abstractmethod
62 def ProcessShell(self, theShell: mtk.ModelData_Shell):
63 pass
64
65class SolidProcessor(mtk.ModelData_ModelElementVoidVisitor):
66 def __init__(self):
67 super().__init__()
68 self.myPartIndex = 0
69
70 def VisitPart(self, thePart: mtk.ModelData_Part):
71 aPartName = "noname" if thePart.Name().IsEmpty() else thePart.Name()
72
73 aBodyList = thePart.Bodies()
74 i = 0
75 for aBody in aBodyList:
76 aShapeIt = mtk.ModelData_ShapeIterator(aBody)
77 for aShape in aShapeIt:
78 if aShape.Type() == mtk.ShapeType_Solid:
79 print("Part #", self.myPartIndex, " [\"", aPartName, "\"] - solid #", i, " has:", sep="")
80 i += 1
81 self.ProcessSolid (mtk.ModelData_Solid.Cast (aShape))
82 self.myPartIndex += 1
83
84 @abstractmethod
85 def ProcessSolid(self, theSolid: mtk.ModelData_Solid):
86 pass
87
88class SolidAndMeshProcessor(mtk.ModelData_ModelElementVoidVisitor):
89 def __init__(self):
90 super().__init__()
91 self.myPartIndex = 0
92 self.myShapeIndex = 0
93
94 def VisitPart(self, thePart: mtk.ModelData_Part):
95 aPartName = "noname" if thePart.Name().IsEmpty() else thePart.Name()
96
97 myShapeIndex = 0
98 aBodyList = thePart.Bodies()
99 for aBody in aBodyList:
100 if mtk.ModelData_MeshBody.CompareType(aBody):
101 aMeshBody = mtk.ModelData_MeshBody.Cast(aBody)
102 self._ProcessMeshBody(aMeshBody, aPartName)
103 elif mtk.ModelData_SolidBody.CompareType(aBody):
104 aSolid = mtk.ModelData_SolidBody.Cast(aBody).Solid()
105 print("Part #", self.myPartIndex, " [\"", aPartName, "\"] - solid #", myShapeIndex, " has:", sep="")
106 self.ProcessSolid (aSolid, aPartName)
107 self.myShapeIndex += 1
108 self.myPartIndex += 1
109
110 @abstractmethod
111 def ProcessSolid(self, theSolid: mtk.ModelData_Solid, thePartName: mtk.UTF16String):
112 pass
113
114 @abstractmethod
115 def ProcessITS(self, theITS: mtk.ModelData_IndexedTriangleSet, thePartName: mtk.UTF16String):
116 pass
117
118 def _ProcessMeshBody(self, theMeshBody: mtk.ModelData_MeshBody, thePartName: mtk.UTF16String):
119 aMeshShapes = theMeshBody.Shapes()
120 for aMeshShape in aMeshShapes:
121 if(mtk.ModelData_IndexedTriangleSet.CompareType(aMeshShape)):
122 print("Part #", self.myPartIndex, " [\"", thePartName, "\"] - ITS #", self.myShapeIndex, " has:", sep="")
123 anITS = mtk.ModelData_IndexedTriangleSet.Cast(aMeshShape)
124 self.ProcessITS(anITS, thePartName)
125 self.myShapeIndex += 1
126