Hide menu
Loading...
Searching...
No Matches
exploring/breprepresentation/breprepresentation.py

Refer to the B-Rep Representation 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
38
39sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + r"/../../"))
40import cadex_license as license
41
42
43class PartBRepVisitor(cadex.ModelData_Model_VoidElementVisitor):
44 def __init__(self):
45 super().__init__()
46 self.myNestingLevel = 0
47 self.myShapeSet = set()
48
49 def PrintUniqueShapesCount(self):
50 print();
51 print(f"Total unique shapes count: {len(self.myShapeSet)}")
52
53 def VisitPart(self, thePart: cadex.ModelData_Part):
54 aBRep = thePart.BRepRepresentation()
55 if aBRep:
56 self.ExploreBRep(aBRep)
57
58 def ExploreBRep(self, theBRep: cadex.ModelData_BRepRepresentation):
59 # Get() method retrieves bodies listed in B-Rep representation by calling data providers flushing
60 # Flushing isn't an elementary process so it can take a significant time (seconds, minutes depending on a model structure)
61 aBodyList = theBRep.Get()
62
63 for i, aBody in enumerate(aBodyList):
64 print("Body ", i, ": -type ", self.PrintBodyType(aBody))
65 self.ExploreShape(aBody)
66
67
68 # Recursive iterating over the Shape until reaching vertices
69 def ExploreShape(self, theShape: cadex.ModelData_Shape):
70 self.myShapeSet.add(theShape)
71 self.myNestingLevel += 1
72 for aShape in theShape.GetIterator():
73 self.PrintShapeInfo(aShape)
74 self.ExploreShape(aShape)
75
76 self.myNestingLevel -= 1
77
78 # Returns body type name
79 def PrintBodyType(self, theBody: cadex.ModelData_Body) -> str:
80 aType = theBody.BodyType()
81 if aType == cadex.ModelData_BT_Solid:
82 return "Solid"
83 if aType == cadex.ModelData_BT_Sheet:
84 return "Sheet"
85 if aType == cadex.ModelData_BT_Wireframe:
86 return "Wireframe"
87 if aType == cadex.ModelData_BT_Acorn:
88 return "Acorn"
89 return "Undefined"
90
91 # Prints shape type name and prints shape info in some cases
92 def PrintShapeInfo(self, theShape: cadex.ModelData_Shape) -> str:
93 self.PrintTabulation()
94
95 aType = theShape.Type()
96 if aType == cadex.ModelData_ST_Body:
97 print("Body", end="")
98 elif aType == cadex.ModelData_ST_Solid:
99 print("Solid", end="")
100 elif aType == cadex.ModelData_ST_Shell:
101 print("Shell", end="")
102 elif aType == cadex.ModelData_ST_Wire:
103 print("Wire", end="")
104 self.PrintWireInfo(cadex.ModelData_Wire.Cast(theShape))
105 elif aType == cadex.ModelData_ST_Face:
106 print("Face", end="")
107 self.PrintFaceInfo(cadex.ModelData_Face.Cast(theShape))
108 elif aType == cadex.ModelData_ST_Edge:
109 print("Edge", end="")
110 self.PrintEdgeInfo(cadex.ModelData_Edge.Cast(theShape))
111 elif aType == cadex.ModelData_ST_Vertex:
112 print("Vertex", end="")
113 self.PrintVertexInfo(cadex.ModelData_Vertex.Cast(theShape))
114 else:
115 print("Undefined", end="")
116
117 print()
118
119
120 def PrintOrientationInfo(self, theShape: cadex.ModelData_Shape):
121 print(". Orientation: ", end="")
122 anOrientation = theShape.Orientation()
123 if anOrientation == cadex.ModelData_SO_Forward:
124 print("Forward", end="")
125 elif anOrientation == cadex.ModelData_SO_Reversed:
126 print("Reversed", end="")
127
128 def PrintWireInfo(self, theWire: cadex.ModelData_Wire):
129 self.myNestingLevel += 1
130 self.PrintOrientationInfo(theWire)
131 self.myNestingLevel -= 1
132
133 def PrintFaceInfo(self, theFace: cadex.ModelData_Face):
134 self.myNestingLevel += 1
135 self.PrintOrientationInfo(theFace)
136 print()
137 aSurface = theFace.Surface()
138 self.PrintTabulation()
139 print(f"Surface: {self.PrintSurfaceType(aSurface)}", end="")
140 self.myNestingLevel -= 1
141
142 def PrintSurfaceType(self, theSurface: cadex.ModelData_Surface) -> str:
143 aType = theSurface
144 if aType == cadex.ModelData_ST_Plane:
145 return "Plane"
146 if aType == cadex.ModelData_ST_Cylinder:
147 return "Cylinder"
148 if aType == cadex.ModelData_ST_Cone:
149 return "Cone"
150 if aType == cadex.ModelData_ST_Sphere:
151 return "Sphere"
152 if aType == cadex.ModelData_ST_Torus:
153 return "Torus"
154 if aType == cadex.ModelData_ST_LinearExtrusion:
155 return "LinearExtrusion"
156 if aType == cadex.ModelData_ST_Revolution:
157 return "Revolution"
158 if aType == cadex.ModelData_ST_Bezier:
159 return "Bezier"
160 if aType == cadex.ModelData_ST_BSpline:
161 return "BSpline"
162 if aType == cadex.ModelData_ST_Offset:
163 return "Offset"
164 if aType == cadex.ModelData_ST_Trimmed:
165 return "Trimmed"
166 return "Undefined"
167
168 def PrintEdgeInfo(self, theEdge: cadex.ModelData_Edge):
169 self.myNestingLevel += 1
170 if theEdge.IsDegenerated():
171 print("(Degenerated)", end="")
172 self.PrintOrientationInfo(theEdge)
173 print(f". Tolerance {theEdge.Tolerance()}", end="")
174
175 if not theEdge.IsDegenerated():
176 print()
177 aCurve, aParamFirst, aParamLast = theEdge.Curve()
178 self.PrintTabulation()
179 print(f"Curve: {self.PrintCurveType(aCurve)}", end="")
180
181 self.myNestingLevel -= 1
182
183 def PrintCurveType(self, theCurve: cadex.ModelData_Curve) -> str:
184 aType = theCurve.Type()
185 if aType == cadex.ModelData_CT_Line:
186 return "Line"
187 if aType == cadex.ModelData_CT_Circle:
188 return "Circle"
189 if aType == cadex.ModelData_CT_Ellipse:
190 return "Ellipse"
191 if aType == cadex.ModelData_CT_Hyperbola:
192 return "Hyperbola"
193 if aType == cadex.ModelData_CT_Parabola:
194 return "Parabola"
195 if aType == cadex.ModelData_CT_Bezier:
196 return "Bezier"
197 if aType == cadex.ModelData_CT_BSpline:
198 return "BSpline"
199 if aType == cadex.ModelData_CT_Offset:
200 return "Offset"
201 if aType == cadex.ModelData_CT_Trimmed:
202 return "Trimmed"
203 return "Undefined"
204
205 def PrintVertexInfo(self, theVertex: cadex.ModelData_Vertex):
206 self.PrintOrientationInfo(theVertex)
207 print(f". Tolerance {theVertex.Tolerance()}", end="")
208
209 def PrintTabulation(self):
210 print("- " * self.myNestingLevel, end="")
211
212
213def main(theSource:str):
214 aKey = license.Value()
215
216 if not cadex.LicenseManager.Activate(aKey):
217 print("Failed to activate CAD Exchanger license.")
218 return 1
219
220 aModel = cadex.ModelData_Model()
221
222 if not cadex.ModelData_ModelReader().Read(cadex.Base_UTF16String(theSource), aModel):
223 print("Failed to read the file " + theSource)
224 return 1
225
226 # Explore B-Rep representation of model parts
227 aVisitor = PartBRepVisitor()
228 aModel.AcceptElementVisitor(aVisitor)
229
230 aVisitor.PrintUniqueShapesCount()
231
232 print("Completed")
233 return 0
234
235if __name__ == "__main__":
236 if len(sys.argv) != 2:
237 print("Usage: " + os.path.abspath(Path(__file__).resolve()) + " <input_file>, where:")
238 print(" <input_file> is a name of the XML file to be read")
239 sys.exit(1)
240
241 aSource = os.path.abspath(sys.argv[1])
242 sys.exit(main(aSource))
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
static const ModelData_Edge & Cast(const ModelData_Shape &theShape)
Casts a base class object to ModelData_Edge.
Definition: ModelData_Edge.cxx:688
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
static const ModelData_Vertex & Cast(const ModelData_Shape &theShape)
Definition: ModelData_Vertex.cxx:94
static const ModelData_Wire & Cast(const ModelData_Shape &theShape)
Definition: ModelData_Wire.cxx:88