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

Refer to the Property Table 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
42class PropertiesVisitor(cadex.ModelData_Model_CombinedElementVisitor):
43 def VisitPart(self, thePart: cadex.ModelData_Part):
44 aPT = thePart.Properties()
45 self.ExplorePropertyTable(aPT)
46
47 aBRep = thePart.BRepRepresentation()
48 if aBRep:
49 aVisitor = SubShapePropertiesVisitor(aBRep)
50 aBRep.Accept(aVisitor)
51
52 # Extract all PropertyTables from SubShapes in B-Rep and explore it
53 aPTList = aVisitor.PropertyTables()
54 for i in aPTList:
55 self.ExplorePropertyTable(i)
56
57
58 def VisitEnterSGE(self, theElement: cadex.ModelData_SceneGraphElement) -> bool:
59 aPT = theElement.Properties()
60 self.ExplorePropertyTable(aPT)
61
62 return True
63
64 def ExtractPropertyTables(self, theBRep: cadex.ModelData_BRepRepresentation) -> list:
65 aPTList = []
66 aBodyGen = theBRep.Get()
67 for aBody in aBodyGen:
68 aPTList.append(theBRep.PropertyTable(aBody))
69 return aPTList
70
71 def ExplorePropertyTable(self, thePT: cadex.ModelData_PropertyTable):
72 if thePT and not thePT.IsEmpty():
73 aPropVisitor = PropertyVisitor()
74 thePT.Accept(aPropVisitor)
75 else:
76 print("\nProperty Table is empty")
77
78class PropertyVisitor(cadex.ModelData_PropertyTable_VoidVisitor):
79 def __init__(self):
80 super().__init__()
81 print("\nProperty table: ")
82
83 def VisitI32(self, theName: cadex.Base_UTF16String, theValue):
84 self.OutputData(theName, theValue)
85
86 def VisitDouble(self, theName: cadex.Base_UTF16String, theValue):
87 self.OutputData(theName, theValue)
88
89 def VisitUTF16String(self, theName: cadex.Base_UTF16String, theValue: cadex.Base_UTF16String):
90 self.OutputData(theName, theValue)
91
92 def OutputData(self, theName: cadex.Base_UTF16String, theValue):
93 print(f"{theName}: {theValue}")
94
95 def ExplorePropertyTable(self, thePT: cadex.ModelData_PropertyTable):
96 # Traverse property table
97 aPropVisitor = PropertyVisitor()
98 thePT.Accept(aPropVisitor)
99
100class SubShapePropertiesVisitor(cadex.ModelData_BRepRepresentation_SubshapeVisitor):
101 def __init__(self, theBRep: cadex.ModelData_BRepRepresentation):
102 super().__init__()
103 self.myPTList = []
104 self.myBRep = theBRep
105
106 def PropertyTables(self) -> list:
107 return self.myPTList
108
109 def VisitShape(self, theShape: cadex.ModelData_Shape):
110 aPT = self.myBRep.PropertyTable(theShape)
111 self.myPTList.append(aPT)
112
113
114def main(theSource: str):
115 aKey = license.Value()
116
117 if not cadex.LicenseManager.Activate(aKey):
118 print("Failed to activate CAD Exchanger license.")
119 return 1
120
121 aModel = cadex.ModelData_Model()
122
123 if not cadex.ModelData_ModelReader().Read(cadex.Base_UTF16String(theSource), aModel):
124 print("Failed to read the file " + theSource )
125 return 1
126
127 aVisitor = PropertiesVisitor()
128 aModel.AcceptElementVisitor(aVisitor)
129
130 print("Completed")
131 return 0
132
133if __name__ == "__main__":
134 if len(sys.argv) != 2:
135 print("Usage: " + os.path.abspath(Path(__file__).resolve()) + " <input_file>, where:")
136 print(" <input_file> is a name of the XML file to be read")
137 sys.exit(1)
138
139 aSource = os.path.abspath(sys.argv[1])
140 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