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

Refer to the Polygonal Representation 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 # Visits directly every part and calls Poly representation exploring if a part has one
43class PartPolyVisitor(cadex.ModelData_Model_VoidElementVisitor):
44 def VisitPart(self, thePart: cadex.ModelData_Part):
45 aPolyRep = thePart.PolyRepresentation(cadex.ModelData_RM_Poly)
46 if aPolyRep:
47 self.ExplorePoly(aPolyRep)
48
49 # Explores PolyVertexSets of Poly representation
50 def ExplorePoly(self, thePoly: cadex.ModelData_PolyRepresentation):
51 # Get() method retrieves PolyVertexSets listed in Poly representation by calling data providers flushing
52 # Because of internal mechanics providers flushing is faster than for B-Rep representations
53 aList = thePoly.Get()
54
55 # Iterate over PolyShape list
56 for i, aPVS in enumerate(aList):
57 print(f"PolyShape {i}:")
58 self.PrintPVSInfo(aPVS)
59
60 # Function to print a PolyVertexSet info
61 def PrintPVSInfo(self, thePVS: cadex.ModelData_PolyVertexSet):
63 print("IndexedTriangleSet type.")
64 ITS = cadex.ModelData_IndexedTriangleSet.Cast(thePVS)
65 self.DumpTriangleSet(ITS)
66 elif thePVS.TypeId() == cadex.ModelData_PolyLineSet.GetTypeId():
67 print("PolyLineSet type.")
68 PLS = cadex.ModelData_PolyLineSet.Cast(thePVS)
69 self.DumpPolyLineSet(PLS)
70 elif thePVS.TypeId() == cadex.ModelData_PolyPointSet.GetTypeId():
71 print("PolyPointSet type.")
72 PPS = cadex.ModelData_PolyPointSet.Cast(thePVS)
73 self.DumpPolyPointSet(PPS)
74 else:
75 print("Undefined type")
76
77 def DumpPolyPointSet(self, thePS: cadex.ModelData_PolyPointSet):
78 n = thePS.NumberOfVertices()
79
80 print(f"PolyPoint set contains {n} vertices")
81 for i in range(n):
82 print(f"Point {i}:")
83 print(" Node coordinates:")
84 aP = thePS.Coordinate(i)
85 print(f" ({aP.X()}, {aP.Y()}, {aP.Z()})")
86
87 # Prints number of PolyLines and local coordinates for every vertex of each PolyLine
88 def DumpPolyLineSet(self, thePLS: cadex.ModelData_PolyLineSet):
89 n = thePLS.NumberOfPolyLines()
90
91 print(f"PolyLine set contains {n} PolyLines")
92 for i in range(n):
93 print(f"PolyLine {i}:")
94 print(" Node coordinates:")
95 for j in range(thePLS.NumberOfVertices(i)):
96 aV = thePLS.Coordinate(i, j)
97 print(f" ({aV.X()}, {aV.Y()}, {aV.Z()})")
98
99 # Prints number of triangles and local data for each node
100 # Prints Coords and UV-Coords for each vertex and prints Normals and Colors for each triangle
101 def DumpTriangleSet(self, theTS: cadex.ModelData_IndexedTriangleSet):
102 n = theTS.NumberOfFaces()
103
104 print(f"Triangle set contains {n} number of faces")
105 for i in range(n):
106 print(f"Triangle {i}:")
107 for j in range(3):
108 print(f" Node {j}:")
109
110 # Coordinates
111 aV = theTS.Coordinate(i, j);
112 print(f" Coordinates: ({aV.X()}, {aV.Y()}, {aV.Z()})")
113
114 # UV-coordinates
115 if theTS.HasUVCoordinates():
116 aUV = theTS.UVCoordinate(i, j)
117 print(f" UV Coordinates: ({aUV.X()}, {aUV.Y()})")
118
119 # Normals
120 if theTS.HasNormals():
121 aN = theTS.VertexNormal(i, j)
122 print(f" Normal vector: ({aN.X()}, {aN.Y()}, {aN.Z()})")
123
124 # Colors
125 if theTS.HasColors():
126 aColor = theTS.VertexColor(i, j)
127
128
129def main(theSource: str):
130 aKey = license.Value()
131
132 if not cadex.LicenseManager.Activate(aKey):
133 print("Failed to activate CAD Exchanger license.")
134 return 1
135
136 aModel = cadex.ModelData_Model()
137
138 if not cadex.ModelData_ModelReader().Read(cadex.Base_UTF16String(theSource), aModel):
139 print("Failed to read the file " + theSource)
140 return 1
141
142 # If there is no Poly representation in the model, mesher will compute it
144 aMesherParams.SetGranularity(cadex.ModelAlgo_BRepMesherParameters.Fine)
145
146 aMesher = cadex.ModelAlgo_BRepMesher(aMesherParams)
147 aMesher.Compute(aModel)
148
149 # Explore Poly representation of model parts
150 aVisitor = PartPolyVisitor()
151 aModel.AcceptElementVisitor(aVisitor)
152
153 print("Completed")
154 return 0
155
156if __name__ == "__main__":
157 if len(sys.argv) != 2:
158 print("Usage: " + os.path.abspath(Path(__file__).resolve()) + " <input_file>, where:")
159 print(" <input_file> is a name of the XML file to be read")
160 sys.exit(1)
161
162 aSource = os.path.abspath(sys.argv[1])
163 sys.exit(main(aSource))
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Computes a polygonal representation from a B-Rep one.
Definition: ModelAlgo_BRepMesher.hxx:48
Defines parameters used by the B-Rep mesher.
Definition: ModelAlgo_BRepMesherParameters.hxx:33
static IdType GetTypeId()
Definition: ModelData_IndexedTriangleSet.cxx:134
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_PolyLineSet.cxx:57
static IdType GetTypeId()
Definition: ModelData_PolyPointSet.cxx:56