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
30import os
31import sys
32
33from pathlib import Path
34
35import manufacturingtoolkit.CadExMTK as mtk
36
37sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
38
39import mtk_license as license
40
41class TabulatedOutput:
42 myNestingLevel = 0
43
44 @classmethod
45 def WriteLine(cls, theObject: str):
46 cls.PrintTabulation()
47 print(theObject)
48
49 @classmethod
50 def IncreaseIndent(cls):
51 cls.myNestingLevel += 1
52
53 @classmethod
54 def DecreaseIndent(cls):
55 cls.myNestingLevel -= 1
56
57 @classmethod
58 def PrintTabulation(cls):
59 if cls.myNestingLevel <= 0:
60 return
61
62 for i in range(cls.myNestingLevel - 1):
63 if i < 2 or i == 3:
64 print("| ", end="")
65 else:
66 print(" ", end="")
67 print("|__", end="")
68 if cls.myNestingLevel > 3:
69 print(" ", end="")
70
71class SceneGraphVisitor(mtk.ModelData_ModelElementVisitor):
72 def VisitPart(self, thePart: mtk.ModelData_Part):
73 self.PrintName("Part", thePart.Name())
74 self.ExplorePMI(thePart)
75
76 def VisitEnterInstance(self, theInstance: mtk.ModelData_Instance):
77 TabulatedOutput.IncreaseIndent()
78 self.PrintName("Instance", theInstance.Name())
79 self.ExplorePMI(theInstance)
80 return True
81
82 def VisitEnterAssembly(self, theAssembly: mtk.ModelData_Assembly):
83 TabulatedOutput.IncreaseIndent()
84 self.PrintName("Assembly", theAssembly.Name())
85 self.ExplorePMI(theAssembly)
86 return True
87
88 def VisitLeaveInstance(self, theInstance: mtk.ModelData_Instance):
89 TabulatedOutput.DecreaseIndent()
90
91 def VisitLeaveAssembly(self, theAssembly: mtk.ModelData_Assembly):
92 TabulatedOutput.DecreaseIndent()
93
94 def ExplorePMI(self, theSGE: mtk.ModelData_ModelElement):
95 aPMIData : mtk.PMI_Data = theSGE.PMI()
96 if aPMIData:
97 TabulatedOutput.WriteLine("PMI Data:")
98 TabulatedOutput.IncreaseIndent()
99
100 anElements = aPMIData.Elements()
101 for anElement in anElements:
102 TabulatedOutput.WriteLine(f"PMI Element: {anElement.Name()}")
103
104 TabulatedOutput.IncreaseIndent()
105
106 aSemanticRepresentation = anElement.SemanticRepresentation()
107 if aSemanticRepresentation:
108 TabulatedOutput.WriteLine("Semantic Representation:")
109 TabulatedOutput.IncreaseIndent()
110 aVisitor = PMISemanticVisitor()
111 aSemanticRepresentation.Accept(aVisitor)
112 TabulatedOutput.DecreaseIndent()
113
114 aGraphicalRepresentation = anElement.GraphicalRepresentation()
115 if aGraphicalRepresentation:
116 TabulatedOutput.WriteLine("Graphical Representation:")
117 TabulatedOutput.IncreaseIndent()
118 aVisitor = PMIGraphicalVisitor()
119 aGraphicalRepresentation.Accept(aVisitor)
120 TabulatedOutput.DecreaseIndent()
121
122 TabulatedOutput.DecreaseIndent()
123 TabulatedOutput.DecreaseIndent()
124
125 def PrintName(self, theSGElement: str, theName: str):
126 if theName:
127 TabulatedOutput.WriteLine(f"{theSGElement}: {theName}")
128 else:
129 TabulatedOutput.WriteLine(f"{theSGElement}: <noname>")
130
131class PMISemanticVisitor(mtk.PMI_SemanticComponentVisitor):
132 def VisitDatumComponent(self, theComponent: mtk.PMI_DatumComponent):
133 TabulatedOutput.WriteLine("Datum")
134 TabulatedOutput.IncreaseIndent()
135 TabulatedOutput.WriteLine(f"Label: {theComponent.Label()}")
136 self.PrintAttributes(theComponent)
137 TabulatedOutput.DecreaseIndent()
138
139 def VisitDimensionComponent(self, theComponent: mtk.PMI_DimensionComponent):
140 TabulatedOutput.WriteLine("Dimension")
141 TabulatedOutput.IncreaseIndent()
142 TabulatedOutput.WriteLine(f"Nominal Value: {theComponent.NominalValue()}")
143 TabulatedOutput.WriteLine(f"Type of dimension: {int(theComponent.TypeOfDimension())}")
144 self.PrintAttributes(theComponent)
145 TabulatedOutput.DecreaseIndent()
146
147 def VisitGeometricToleranceComponent(self, theComponent: mtk.PMI_GeometricToleranceComponent):
148 TabulatedOutput.WriteLine("Geometric tolerance")
149 TabulatedOutput.IncreaseIndent()
150 TabulatedOutput.WriteLine(f"Magnitude: {theComponent.Magnitude()}")
151 TabulatedOutput.WriteLine(f"Type of tolerance: {int(theComponent.TypeOfTolerance())}")
152 TabulatedOutput.WriteLine(f"Tolerance zone form: {int(theComponent.ToleranceZoneForm())}")
153 self.PrintAttributes(theComponent)
154 TabulatedOutput.DecreaseIndent()
155
156 def VisitSurfaceFinishComponent(self, theComponent: mtk.PMI_SurfaceFinishComponent):
157 TabulatedOutput.WriteLine("Surface Finish")
158 TabulatedOutput.IncreaseIndent()
159 TabulatedOutput.WriteLine(f"Material removal: {int(theComponent.MaterialRemoval())}")
160 TabulatedOutput.WriteLine(f"Lay direction: {int(theComponent.LayDirection())}")
161 TabulatedOutput.WriteLine(f"All around flag: {int(theComponent.IsAllAround())}")
162 TabulatedOutput.WriteLine(f"Manufacturing method: {theComponent.ManufacturingMethod()}")
163 self.PrintAttributes(theComponent)
164 TabulatedOutput.DecreaseIndent()
165
166 def PrintAttributes(self, theComponent: mtk.PMI_SemanticComponent):
167 if theComponent.HasAttributes():
168 aVisitor = PMISemanticAttributeVisitor()
169 theComponent.Accept(aVisitor)
170
171class PMISemanticAttributeVisitor(mtk.PMI_SemanticAttributeVisitor):
172 def VisitModifierAttribute(self, theAttribute: mtk.PMI_ModifierAttribute):
173 TabulatedOutput.WriteLine(f"Modifier: {theAttribute.Modifier()}")
174
175 def VisitModifierWithValueAttribute(self, theAttribute: mtk.PMI_ModifierWithValueAttribute):
176 TabulatedOutput.WriteLine(f"ModifierWithValue: modifier={theAttribute.Modifier()}, value={theAttribute.Value()}")
177
178 def VisitQualifierAttribute(self, theAttribute: mtk.PMI_QualifierAttribute):
179 TabulatedOutput.WriteLine(f"Qualifier: {theAttribute.Qualifier()}")
180
181 def VisitPlusMinusBoundsAttribute(self, theAttribute: mtk.PMI_PlusMinusBoundsAttribute):
182 TabulatedOutput.WriteLine(f"PlusMinusBounds: ({theAttribute.LowerBound()}, {theAttribute.UpperBound()})")
183
184 def VisitRangeAttribute(self, theAttribute: mtk.PMI_RangeAttribute):
185 TabulatedOutput.WriteLine(f"Range: [{theAttribute.LowerLimit()}, {theAttribute.UpperLimit()}]")
186
187 def VisitLimitsAndFitsAttribute(self, theAttribute: mtk.PMI_LimitsAndFitsAttribute):
188 TabulatedOutput.WriteLine(f"LimitsAndFits: value={theAttribute.Value()} + {type}={theAttribute.Type()}")
189
190 def VisitDatumTargetAttribute(self, theAttribute: mtk.PMI_DatumTargetAttribute):
191 TabulatedOutput.WriteLine(f"DatumTarget: index={theAttribute.Index()}, description={theAttribute.Description()}")
192
193 def VisitDatumRefAttribute(self, theAttribute: mtk.PMI_DatumRefAttribute):
194 TabulatedOutput.WriteLine(f"DatumRef: precedence={theAttribute.Precedence()}, targetLabel={theAttribute.TargetLabel()}")
195
196 def VisitDatumRefCompartmentAttribute(self, theAttribute: mtk.PMI_DatumRefCompartmentAttribute):
197 TabulatedOutput.WriteLine("DatumRefCompartment:")
198
199 TabulatedOutput.IncreaseIndent()
200
201 aNumberOfReferences = theAttribute.NumberOfReferences()
202 if aNumberOfReferences > 0:
203 TabulatedOutput.WriteLine("References:")
204 TabulatedOutput.IncreaseIndent()
205 for i in range(aNumberOfReferences):
206 theAttribute.Reference(i).Accept(self)
207 TabulatedOutput.DecreaseIndent()
208
209 aNumberOfModifierAttributes = theAttribute.NumberOfModifierAttributes()
210 if aNumberOfModifierAttributes > 0:
211 TabulatedOutput.WriteLine("Modifiers:")
212 TabulatedOutput.IncreaseIndent()
213 for i in range(aNumberOfModifierAttributes):
214 theAttribute.ModifierAttribute(i).Accept(self)
215 TabulatedOutput.DecreaseIndent()
216
217 TabulatedOutput.DecreaseIndent()
218
219 def VisitMaximumValueAttribute(self, theAttribute: mtk.PMI_MaximumValueAttribute):
220 TabulatedOutput.WriteLine(f"MaximumValue: {theAttribute.MaxValue()}")
221
222 def VisitDisplacementAttribute(self, theAttribute: mtk.PMI_DisplacementAttribute):
223 TabulatedOutput.WriteLine(f"Displacement: {theAttribute.Displacement()}")
224
225 def VisitLengthUnitAttribute(self, theAttribute: mtk.PMI_LengthUnitAttribute):
226 TabulatedOutput.WriteLine(f"LengthUnit: {theAttribute.Unit()}")
227
228 def VisitAngleUnitAttribute(self, theAttribute: mtk.PMI_AngleUnitAttribute):
229 TabulatedOutput.WriteLine(f"AngleUnit: {theAttribute.Unit()}")
230
231 def VisitMachiningAllowanceAttribute(self, theAttribute: mtk.PMI_MachiningAllowanceAttribute):
232 TabulatedOutput.WriteLine("Machining allowance")
233 TabulatedOutput.IncreaseIndent()
234 TabulatedOutput.WriteLine(f"Value: {theAttribute.Value()}")
235 TabulatedOutput.WriteLine(f"Upper bound: {theAttribute.UpperBound()}")
236 TabulatedOutput.WriteLine(f"Lower bound: {theAttribute.LowerBound()}")
237 TabulatedOutput.DecreaseIndent()
238
239 def VisitSurfaceTextureRequirementAttribute(self, theAttribute: mtk.PMI_SurfaceTextureRequirementAttribute):
240 TabulatedOutput.WriteLine(f"Surface texture requirement #: {int(theAttribute.Precedence())}")
241 TabulatedOutput.IncreaseIndent()
242 TabulatedOutput.WriteLine(f"Specification limit: {int(theAttribute.SpecificationLimit())}")
243 TabulatedOutput.WriteLine(f"Filter name: {theAttribute.FilterName()}")
244 TabulatedOutput.WriteLine(f"Short wave filter: {theAttribute.ShortWaveFilter()}")
245 TabulatedOutput.WriteLine(f"Long wave filter: {theAttribute.LongWaveFilter()}")
246 TabulatedOutput.WriteLine(f"Surface parameter: {int(theAttribute.SurfaceParameter())}")
247 TabulatedOutput.WriteLine(f"Evaluation length: {theAttribute.EvaluationLength()}")
248 TabulatedOutput.WriteLine(f"Comparison rule: {int(theAttribute.ComparisonRule())}")
249 TabulatedOutput.WriteLine(f"Limit value: {theAttribute.LimitValue()}")
250 TabulatedOutput.DecreaseIndent()
251
252class PMIGraphicalVisitor(mtk.PMI_GraphicalComponentVisitor):
253 def VisitOutlinedComponent(self, theComponent: mtk.PMI_OutlinedComponent):
254 TabulatedOutput.WriteLine("Outline")
255 TabulatedOutput.IncreaseIndent()
256 aVisitor = PMIOutlineVisitor()
257 theComponent.Outline().Accept(aVisitor)
258 TabulatedOutput.DecreaseIndent()
259
260 def VisitTextComponent(self, theComponent: mtk.PMI_TextComponent):
261 TabulatedOutput.WriteLine(f"Text [{theComponent.Text()}]")
262
263 def VisitTriangulatedComponent(self, theComponent: mtk.PMI_TriangulatedComponent):
264 TabulatedOutput.WriteLine(f"Triangulation [{theComponent.TriangleSet().NumberOfTriangles()} triangles]")
265
266class PMIOutlineVisitor(mtk.PMI_OutlineVisitor):
267 def VisitPolyOutline(self, theOutline: mtk.PMI_PolyOutline):
268 TabulatedOutput.WriteLine(f"PolyLine set [{theOutline.LineSet().NumberOfPolylines()} polylines]")
269
270 def VisitPoly2dOutline(self, theOutline: mtk.PMI_Poly2dOutline):
271 TabulatedOutput.WriteLine(f"PolyLine2d set [{theOutline.LineSet().NumberOfPolylines()} polylines]")
272
273 def VisitCurveOutline(self, theOutline: mtk.PMI_CurveOutline):
274 TabulatedOutput.WriteLine(f"Curve set [{theOutline.NumberOfCurves()} curves]")
275
276 def VisitCurve2dOutline(self, theOutline: mtk.PMI_Curve2dOutline):
277 TabulatedOutput.WriteLine(f"Curve2d set [{theOutline.NumberOfCurves()} curves]")
278
279 def VisitEnterCompositeOutline(self, theOutline: mtk.PMI_CompositeOutline):
280 TabulatedOutput.WriteLine("Composite outline:")
281 TabulatedOutput.IncreaseIndent()
282 return True
283
284 def VisitLeaveCompositeOutline(self, theOutline: mtk.PMI_CompositeOutline):
285 TabulatedOutput.DecreaseIndent()
286
287def main(theSource: str):
288 aKey = license.Value()
289
290 if not mtk.LicenseManager.Activate(aKey):
291 print("Failed to activate Manufacturing Toolkit license.")
292 return 1
293
294 aModel = mtk.ModelData_Model()
295 aReader = mtk.ModelData_ModelReader()
296 aParams = mtk.ModelData_ModelReaderParameters()
297 aParams.SetReadPMI(True)
298 aReader.SetParameters(aParams)
299
300
301 if not aReader.Read(mtk.UTF16String(theSource), aModel):
302 print("Failed to open and convert the file " + theSource)
303 return 1
304
305 print("Model: ", aModel.Name(), "\n", sep="")
306
307
308 aVisitor = SceneGraphVisitor()
309 aModel.Accept(aVisitor)
310
311 print("Completed")
312
313 return 0
314
315if __name__ == "__main__":
316 if len(sys.argv) != 2:
317 print("Usage: <input_file>, where:")
318 print(" <input_file> is a name of the file to be read")
319 sys.exit()
320
321 aSource = os.path.abspath(sys.argv[1])
322
323 sys.exit(main(aSource))