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

Refer to the Appearance 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 SubshapeAppearancesCollector(cadex.ModelData_BRepRepresentation_SubshapeVisitor):
43 def __init__(self, theBRep: cadex.ModelData_BRepRepresentation, theAppSet: set):
44 super().__init__()
45 self.myBRep = theBRep
46 self.myAppSet = theAppSet
47
48 def VisitShape(self, theShape: cadex.ModelData_Shape):
49 self.ExploreShapeAppearances(theShape)
50
51 def ExploreShapeAppearances(self, theShape: cadex.ModelData_Shape):
52 anApp = self.myBRep.Appearance(theShape)
53 if anApp:
54 self.myAppSet.add(anApp)
55
56
57class RepVisitor(cadex.ModelData_Part_RepresentationVisitor):
58 def __init__(self, theAppSet: set):
59 super().__init__()
60 self.myAppSet = theAppSet
61
62 def VisitBRep(self, theBRep: cadex.ModelData_BRepRepresentation):
63 aCollector = SubshapeAppearancesCollector(theBRep, self.myAppSet)
64 theBRep.Accept(aCollector)
65
66 def VisitPoly(self, thePolyRep: cadex.ModelData_PolyRepresentation):
67 self.ExplorePVSAppearances(thePolyRep)
68
69 def ExplorePVSAppearances(self, thePolyRep: cadex.ModelData_PolyRepresentation):
70 aList = thePolyRep.Get()
71 for aPVS in aList:
72 anApp = aPVS.Appearance()
73 if anApp:
74 self.myAppSet.add(anApp)
75
76class SGEAppearancesCollector(cadex.ModelData_Model_CombinedElementVisitor):
77 def __init__(self, theAppSet):
78 super().__init__()
79 self.myAppSet = theAppSet
80
81 def VisitPart(self, thePart: cadex.ModelData_Part):
82 self.ExploreSGEAppearance(thePart)
83 aVisitor = RepVisitor(self.myAppSet)
84 thePart.Accept(aVisitor)
85
86 def VisitEnterSGE(self, theElement) -> bool:
87 self.ExploreSGEAppearance(theElement)
88 return True
89
90 # Collects SceneGraphElement appearance
91 def ExploreSGEAppearance(self, theSGE: cadex.ModelData_SceneGraphElement):
92 anApp = theSGE.Appearance()
93 if anApp:
94 self.myAppSet.add(anApp)
95
96
97class AppearancesCollector:
98 def __init__(self, theModel: cadex.ModelData_Model):
99 self.myModel = theModel
100 aCollector = SGEAppearancesCollector(set())
101 self.myModel.AcceptElementVisitor(aCollector)
102 self.myAppSet = aCollector.myAppSet
103
104
105 def PrintAppearancesCount(self):
106 print("Total model unique Appearances count:", len(self.myAppSet))
107
108
109def main(theSource: str):
110 aKey = license.Value()
111
112 if not cadex.LicenseManager.Activate(aKey):
113 print("Failed to activate CAD Exchanger license.")
114 return 1
115
116 aModel = cadex.ModelData_Model()
117
118 if not cadex.ModelData_ModelReader().Read(cadex.Base_UTF16String(theSource), aModel):
119 print("Failed to read the file", theSource)
120 return 1
121
122 # Explore model's appearances
123 aCollector = AppearancesCollector(aModel)
124
125 # Print the number of unique appearances in our model
126 aCollector.PrintAppearancesCount()
127
128 print("Completed")
129 return 0
130
131if __name__ == "__main__":
132 if len(sys.argv) != 2:
133 print("Usage: " + os.path.abspath(Path(__file__).resolve()) + " <input_file>, where:")
134 print(" <input_file> is a name of the XML file to be read")
135 sys.exit(1)
136
137 aSource = os.path.abspath(sys.argv[1])
138 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