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

Refer to the Layers 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
42
43class LayersFiller(cadex.ModelData_Model_CombinedElementVisitor):
44 def __init__(self):
45 super().__init__()
46 self.mySubShapesLayer = cadex.ModelData_Layer(cadex.Base_UTF16String("SubshapesLayer"))
47 self.mySGELayer = cadex.ModelData_Layer(cadex.Base_UTF16String("SGELayer"))
48
49 def VisitPart(self, thePart: cadex.ModelData_Part):
50 self.mySGELayer.Add(thePart)
51 aBRep = thePart.BRepRepresentation()
52 if aBRep:
53 aBodyList = aBRep.Get()
54 for aBody in aBodyList:
55 aBRep.AddToLayer(aBody, self.mySubShapesLayer)
56
57 def VisitEnterSGE(self, theElement) -> bool:
58 self.mySGELayer.Add(theElement)
59 return True
60
61
62class LayerItemVisitor(cadex.ModelData_Layer_ItemVisitor):
63 def __init__(self):
64 super().__init__()
65 self.myPartsNb = 0
66 self.myAssembliesNb = 0
67 self.myInstancesNb = 0
68 self.myShapesNb = 0
69
70 def GetElementsCount(self):
71 print(f"Number of parts: {self.myPartsNb}")
72 print(f"Number of assemblies: {self.myAssembliesNb}")
73 print(f"Number of instances: {self.myInstancesNb}")
74 print(f"Number of shapes: {self.myShapesNb}")
75
76 def VisitSGE(self, theSGE: cadex.ModelData_SceneGraphElement):
77 if theSGE.TypeId() == cadex.ModelData_Part.GetTypeId():
78 self.myPartsNb += 1
79 elif theSGE.TypeId() == cadex.ModelData_Assembly.GetTypeId():
80 self.myAssembliesNb += 1
81 elif theSGE.TypeId() == cadex.ModelData_Instance.GetTypeId():
82 self.myInstancesNb += 1
83
84 def VisitShape(self, theShape: cadex.ModelData_Shape, theRep: cadex.ModelData_Representation):
85 self.myShapesNb += 1
86
87class LayersVisitor(cadex.ModelData_Model_LayerVisitor):
88 def __init__(self):
89 super().__init__()
90
91 def VisitLayer(self, theLayer: cadex.ModelData_Layer):
92 aLayerItemVisitor = LayerItemVisitor()
93 theLayer.Accept(aLayerItemVisitor)
94 print(f"Layer {theLayer.Name()} contains:")
95 aLayerItemVisitor.GetElementsCount()
96
97
98def main(theSource: str):
99 aKey = license.Value()
100
101 if not cadex.LicenseManager.Activate(aKey):
102 print("Failed to activate CAD Exchanger license.")
103 return 1
104
105 aModel = cadex.ModelData_Model()
106
107 if not cadex.ModelData_ModelReader().Read(cadex.Base_UTF16String(theSource), aModel):
108 print("Failed to read the file " + theSource)
109 return 1
110
111 aLayerIt = aModel.GetLayerIterator()
112 if not aLayerIt.HasNext():
113 #Create a visitor to fill its layers
114 aVisitor = LayersFiller()
115 aModel.AcceptLayerVisitor(aVisitor)
116
117 # Add created layers to the model
118 aModel.AddLayer(aVisitor.mySGELayer)
119 aModel.AddLayer(aVisitor.mySubShapesLayer)
120
121 aLayerVisitor = LayersVisitor()
122 aModel.AcceptLayerVisitor(aLayerVisitor)
123
124 print("Completed")
125 return 0
126
127if __name__ == "__main__":
128 if len(sys.argv) != 2:
129 print("Usage: " + os.path.abspath(Path(__file__).resolve()) + " <input_file>, where:")
130 print(" <input_file> is a name of the XML file to be read")
131 sys.exit(1)
132
133 aSource = os.path.abspath(sys.argv[1])
134 sys.exit(main(aSource))
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
static IdType GetTypeId()
Definition: ModelData_Assembly.cxx:82
static IdType GetTypeId()
Definition: ModelData_Instance.cxx:144
Provides a layer of objects.
Definition: ModelData_Layer.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
static IdType GetTypeId()
Definition: ModelData_Part.cxx:270
Base class of part representations.
Definition: ModelData_Representation.hxx:30