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

Refer to the PMI 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
38import cadexchanger.CadExSTEP as step
39
40sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + r"/../../"))
41import cadex_license as license
42
43class TabulatedOutput:
44 myNestingLevel = 0
45
46 @classmethod
47 def WriteLine(cls, theObject: str):
48 cls.PrintTabulation()
49 print(theObject)
50
51 @classmethod
52 def IncreaseIndent(cls):
53 cls.myNestingLevel += 1
54
55 @classmethod
56 def DecreaseIndent(cls):
57 cls.myNestingLevel -= 1
58
59 @classmethod
60 def PrintTabulation(cls):
61 if cls.myNestingLevel <= 0:
62 return
63 # Emulate tabulation like tree.
64 for i in range(cls.myNestingLevel - 1):
65 if i < 2 or i == 3:
66 print("| ", end="")
67 else:
68 print(" ", end="")
69 print("|__", end="")
70 if cls.myNestingLevel > 3:
71 print(" ", end="")
72
73
74class SceneGraphVisitor(cadex.ModelData_Model_ElementVisitor):
75 def VisitPart(self, thePart: cadex.ModelData_Part):
76 self.PrintName("Part", thePart.Name())
77 self.ExplorePMI(thePart)
78
79 def VisitEnterInstance(self, theInstance: cadex.ModelData_Instance):
80 TabulatedOutput.IncreaseIndent()
81 self.PrintName("Instance", theInstance.Name())
82 self.ExplorePMI(theInstance)
83 return True
84
85 def VisitEnterAssembly(self, theAssembly: cadex.ModelData_Assembly):
86 TabulatedOutput.IncreaseIndent()
87 self.PrintName("Assembly", theAssembly.Name())
88 self.ExplorePMI(theAssembly)
89 return True
90
91 def VisitLeaveInstance(self, theInstance: cadex.ModelData_Instance):
92 TabulatedOutput.DecreaseIndent()
93
94 def VisitLeaveAssembly(self, theAssembly: cadex.ModelData_Assembly):
95 TabulatedOutput.DecreaseIndent()
96
97 def ExplorePMI(self, theSGE: cadex.ModelData_SceneGraphElement):
98 aPMITable : cadex.ModelData_PMITable = theSGE.PMI()
99 if aPMITable:
100 TabulatedOutput.WriteLine("PMI Table:")
101 TabulatedOutput.IncreaseIndent()
102
103 aDataIt = aPMITable.GetPMIDataIterator()
104 for aData in aDataIt:
105 TabulatedOutput.WriteLine(f"PMI Data: {aData.Name()}")
106
107 TabulatedOutput.IncreaseIndent()
108
109 aSemanticElement = aData.SemanticElement()
110 if aSemanticElement:
111 TabulatedOutput.WriteLine("Semantic element:")
112 TabulatedOutput.IncreaseIndent()
113 aVisitor = PMISemanticVisitor()
114 aSemanticElement.Accept(aVisitor)
115 TabulatedOutput.DecreaseIndent()
116
117 aGraphicalElement = aData.GraphicalElement()
118 if aGraphicalElement:
119 TabulatedOutput.WriteLine("Graphical element:")
120 TabulatedOutput.IncreaseIndent()
121 aVisitor = PMIGraphicalVisitor()
122 aGraphicalElement.Accept(aVisitor)
123 TabulatedOutput.DecreaseIndent()
124
125 TabulatedOutput.DecreaseIndent()
126 TabulatedOutput.DecreaseIndent()
127
128
129 def PrintName(self, theSGElement: str, theName: str):
130 if theName:
131 TabulatedOutput.WriteLine(f"{theSGElement}: {theName}")
132 else:
133 TabulatedOutput.WriteLine(f"{theSGElement}: <noname>")
134
135
137 def VisitDatumComponent(self, theComponent: cadex.ModelData_PMIDatumComponent):
138 TabulatedOutput.WriteLine("Datum")
139 TabulatedOutput.IncreaseIndent()
140 TabulatedOutput.WriteLine(f"Label: {theComponent.Label()}")
141 self.PrintAttributes(theComponent)
142 TabulatedOutput.DecreaseIndent()
143
144 def VisitDimensionComponent(self, theComponent: cadex.ModelData_PMIDimensionComponent):
145 TabulatedOutput.WriteLine("Dimension")
146 TabulatedOutput.IncreaseIndent()
147 TabulatedOutput.WriteLine(f"Nominal Value: {theComponent.NominalValue()}")
148 TabulatedOutput.WriteLine(f"Type of dimension: {int(theComponent.TypeOfDimension())}")
149 self.PrintAttributes(theComponent)
150 TabulatedOutput.DecreaseIndent()
151
152 def VisitGeometricToleranceComponent(self, theComponent: cadex.ModelData_PMIGeometricToleranceComponent):
153 TabulatedOutput.WriteLine("Geometric tolerance")
154 TabulatedOutput.IncreaseIndent()
155 TabulatedOutput.WriteLine(f"Magnitude: {theComponent.Magnitude()}")
156 TabulatedOutput.WriteLine(f"Type of tolerance: {int(theComponent.TypeOfTolerance())}")
157 TabulatedOutput.WriteLine(f"Tolerance zone form: {int(theComponent.ToleranceZoneForm())}")
158 self.PrintAttributes(theComponent)
159 TabulatedOutput.DecreaseIndent()
160
161 def VisitSurfaceFinishComponent(self, theComponent: cadex.ModelData_PMISurfaceFinishComponent):
162 TabulatedOutput.WriteLine("Surface Finish")
163 TabulatedOutput.IncreaseIndent()
164 TabulatedOutput.WriteLine(f"Material removal: {int(theComponent.MaterialRemoval())}")
165 TabulatedOutput.WriteLine(f"Lay direction: {int(theComponent.LayDirection())}")
166 TabulatedOutput.WriteLine(f"All around flag: {int(theComponent.IsAllAround())}")
167 TabulatedOutput.WriteLine(f"Manufacturing method: {theComponent.ManufacturingMethod()}")
168 self.PrintAttributes(theComponent)
169 TabulatedOutput.DecreaseIndent()
170
171 def PrintAttributes(self, theComponent: cadex.ModelData_PMISemanticElementComponent):
172 if theComponent.HasAttributes():
173 aVisitor = PMISemanticAttributeVisitor()
174 theComponent.Accept(aVisitor)
175
176class PMISemanticAttributeVisitor(cadex.ModelData_PMISemanticAttributeVisitor):
177 def VisitModifierAttribute(self, theAttribute: cadex.ModelData_PMIModifierAttribute):
178 TabulatedOutput.WriteLine(f"Modifier: {theAttribute.Modifier()}")
179
180 def VisitModifierWithValueAttribute(self, theAttribute: cadex.ModelData_PMIModifierWithValueAttribute):
181 TabulatedOutput.WriteLine(f"ModifierWithValue: modifier={theAttribute.Modifier()}, value={theAttribute.Value()}")
182
183 def VisitQualifierAttribute(self, theAttribute: cadex.ModelData_PMIQualifierAttribute):
184 TabulatedOutput.WriteLine(f"Qualifier: {theAttribute.Qualifier()}")
185
186 def VisitPlusMinusBoundsAttribute(self, theAttribute: cadex.ModelData_PMIPlusMinusBoundsAttribute):
187 TabulatedOutput.WriteLine(f"PlusMinusBounds: ({theAttribute.LowerBound()}, {theAttribute.UpperBound()})")
188
189 def VisitRangeAttribute(self, theAttribute: cadex.ModelData_PMIRangeAttribute):
190 TabulatedOutput.WriteLine(f"Range: [{theAttribute.LowerLimit()}, {theAttribute.UpperLimit()}]")
191
192 def VisitLimitsAndFitsAttribute(self, theAttribute: cadex.ModelData_PMILimitsAndFitsAttribute):
193 TabulatedOutput.WriteLine(f"LimitsAndFits: value={theAttribute.Value()} + {type}={theAttribute.Type()}")
194
195 def VisitDatumTargetAttribute(self, theAttribute: cadex.ModelData_PMIDatumTargetAttribute):
196 TabulatedOutput.WriteLine(f"DatumTarget: index={theAttribute.Index()}, description={theAttribute.Description()}")
197
198 def VisitDatumRefAttribute(self, theAttribute: cadex.ModelData_PMIDatumRefAttribute):
199 TabulatedOutput.WriteLine(f"DatumRef: precedence={theAttribute.Precedence()}, targetLabel={theAttribute.TargetLabel()}")
200
201 def VisitDatumRefCompartmentAttribute(self, theAttribute: cadex.ModelData_PMIDatumRefCompartmentAttribute):
202 TabulatedOutput.WriteLine("DatumRefCompartment:")
203
204 TabulatedOutput.IncreaseIndent()
205
206 aNumberOfReferences = theAttribute.NumberOfReferences()
207 if aNumberOfReferences > 0:
208 TabulatedOutput.WriteLine("References:")
209 TabulatedOutput.IncreaseIndent()
210 for i in range(aNumberOfReferences):
211 theAttribute.Reference(i).Accept(self)
212 TabulatedOutput.DecreaseIndent()
213
214 aNumberOfModifierAttributes = theAttribute.NumberOfModifierAttributes()
215 if aNumberOfModifierAttributes > 0:
216 TabulatedOutput.WriteLine("Modifiers:")
217 TabulatedOutput.IncreaseIndent()
218 for i in range(aNumberOfModifierAttributes):
219 theAttribute.ModifierAttribute(i).Accept(self)
220 TabulatedOutput.DecreaseIndent()
221
222 TabulatedOutput.DecreaseIndent()
223
224 def VisitMaximumValueAttribute(self, theAttribute: cadex.ModelData_PMIMaximumValueAttribute):
225 TabulatedOutput.WriteLine(f"MaximumValue: {theAttribute.MaxValue()}")
226
227 def VisitDisplacementAttribute(self, theAttribute: cadex.ModelData_PMIDisplacementAttribute):
228 TabulatedOutput.WriteLine(f"Displacement: {theAttribute.Displacement()}")
229
230 def VisitLengthUnitAttribute(self, theAttribute: cadex.ModelData_PMILengthUnitAttribute):
231 TabulatedOutput.WriteLine(f"LengthUnit: {theAttribute.Unit()}")
232
233 def VisitAngleUnitAttribute(self, theAttribute: cadex.ModelData_PMIAngleUnitAttribute):
234 TabulatedOutput.WriteLine(f"AngleUnit: {theAttribute.Unit()}")
235
236 def VisitMachiningAllowanceAttribute(self, theAttribute: cadex.ModelData_PMIMachiningAllowanceAttribute):
237 TabulatedOutput.WriteLine("Machining allowance")
238 TabulatedOutput.IncreaseIndent()
239 TabulatedOutput.WriteLine(f"Value: {theAttribute.Value()}")
240 TabulatedOutput.WriteLine(f"Upper bound: {theAttribute.UpperBound()}")
241 TabulatedOutput.WriteLine(f"Lower bound: {theAttribute.LowerBound()}")
242 TabulatedOutput.DecreaseIndent()
243
244 def VisitSurfaceTextureRequirementAttribute(self, theAttribute: cadex.ModelData_PMISurfaceTextureRequirementAttribute):
245 TabulatedOutput.WriteLine(f"Surface texture requirement #: {int(theAttribute.Precedence())}")
246 TabulatedOutput.IncreaseIndent()
247 TabulatedOutput.WriteLine(f"Specification limit: {int(theAttribute.SpecificationLimit())}")
248 TabulatedOutput.WriteLine(f"Filter name: {theAttribute.FilterName()}")
249 TabulatedOutput.WriteLine(f"Short wave filter: {theAttribute.ShortWaveFilter()}")
250 TabulatedOutput.WriteLine(f"Long wave filter: {theAttribute.LongWaveFilter()}")
251 TabulatedOutput.WriteLine(f"Surface parameter: {int(theAttribute.SurfaceParameter())}")
252 TabulatedOutput.WriteLine(f"Evaluation length: {theAttribute.EvaluationLength()}")
253 TabulatedOutput.WriteLine(f"Comparison rule: {int(theAttribute.ComparisonRule())}")
254 TabulatedOutput.WriteLine(f"Limit value: {theAttribute.LimitValue()}")
255 TabulatedOutput.DecreaseIndent()
256
258 def VisitOutlinedComponent(self, theComponent: cadex.ModelData_PMIOutlinedComponent):
259 TabulatedOutput.WriteLine("Outline")
260 TabulatedOutput.IncreaseIndent()
261 aVisitor = PMIOutlineVisitor()
262 theComponent.Outline().Accept(aVisitor)
263 TabulatedOutput.DecreaseIndent()
264
265 def VisitTextComponent(self, theComponent: cadex.ModelData_PMITextComponent):
266 TabulatedOutput.WriteLine(f"Text [{theComponent.Text()}]")
267
268 def VisitTriangulatedComponent(self, theComponent: cadex.ModelData_PMITriangulatedComponent):
269 TabulatedOutput.WriteLine(f"Triangulation [{theComponent.TriangleSet().NumberOfFaces()} triangles]")
270class PMIOutlineVisitor(cadex.ModelData_PMIOutlineVisitor):
271 def VisitPolyOutline(self, theOutline: cadex.ModelData_PMIPolyOutline):
272 TabulatedOutput.WriteLine(f"PolyLine set [{theOutline.LineSet().NumberOfPolyLines()} polylines]")
273
274 def VisitPoly2dOutline(self, theOutline: cadex.ModelData_PMIPoly2dOutline):
275 TabulatedOutput.WriteLine(f"PolyLine2d set [{theOutline.LineSet().NumberOfPolyLines()} polylines]")
276
277 def VisitCurveOutline(self, theOutline: cadex.ModelData_PMICurveOutline):
278 TabulatedOutput.WriteLine(f"Curve set [{theOutline.NumberOfCurves()} curves]")
279
280 def VisitCurve2dOutline(self, theOutline: cadex.ModelData_PMICurve2dOutline):
281 TabulatedOutput.WriteLine(f"Curve2d set [{theOutline.NumberOfCurves()} curves]")
282
283 def VisitEnterCompositeOutline(self, theOutline: cadex.ModelData_PMICompositeOutline):
284 TabulatedOutput.WriteLine("Composite outline:")
285 TabulatedOutput.IncreaseIndent()
286 return True
287
288 def VisitLeaveCompositeOutline(self, theOutline: cadex.ModelData_PMICompositeOutline):
289 TabulatedOutput.DecreaseIndent()
290
291def main(theSource: str):
292 aKey = license.Value()
293
294 if not cadex.LicenseManager.Activate(aKey):
295 print("Failed to activate CAD Exchanger license.")
296 return 1
297
298 aModel = cadex.ModelData_Model()
300 aParams = step.STEP_ReaderParameters()
301 aParams.SetReadPMI(True)
302 aReader.SetReaderParameters(aParams)
303
304 # Opening and converting the file
305 if not aReader.Read(cadex.Base_UTF16String(theSource), aModel):
306 print("Failed to open and convert the file ", theSource)
307 return 1
308
309 # Create a PMI visitor
310 aVisitor = SceneGraphVisitor()
311 aModel.AcceptElementVisitor(aVisitor)
312
313 print("Completed")
314 return 0
315
316if __name__ == "__main__":
317 if len(sys.argv) != 2:
318 print(" <input_file> is a name of the STEP file to be read")
319 sys.exit()
320
321 aSource = os.path.abspath(sys.argv[1])
322
323 sys.exit(main(aSource))
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
Reads any format that CAD Exchanger can import.
Definition: ModelData_ModelReader.hxx:33
Defines a visitor of the components.
Definition: ModelData_PMIGraphicalElementComponentVisitor.hxx:32
Defines a visitor of the outlines.
Definition: ModelData_PMIOutlineVisitor.hxx:34
Defines a visitor of the attributes.
Definition: ModelData_PMISemanticAttributeVisitor.hxx:44
Defines a visitor of the components.
Definition: ModelData_PMISemanticElementComponentVisitor.hxx:33
Defines a container storing PMI data.
Definition: ModelData_PMITable.hxx:40