1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
60
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
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
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
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="")
105 elif aType == cadex.ModelData_ST_Face:
106 print("Face", end="")
108 elif aType == cadex.ModelData_ST_Edge:
109 print("Edge", end="")
111 elif aType == cadex.ModelData_ST_Vertex:
112 print("Vertex", end="")
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
221
223 print("Failed to read the file " + theSource)
224 return 1
225
226
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