Hide menu
Loading...
Searching...
No Matches
modeling/poly/poly.py

Refer to the Polygonal Modeling 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
42def CreatePolyPointSet() -> cadex.ModelData_PolyPointSet:
43 aPoints1 = cadex.ModelData_PointList()
44 aPoints2 = cadex.ModelData_PointList()
45
46 for i in range(10):
47 aPoints1.append(cadex.ModelData_Point(i, 0, 0))
48 aPoints2.append(cadex.ModelData_Point(i, 0, i))
49
50 # It's possible to add as much containers of points to same PolyPointSet as you want
52 aPPS.Add(aPoints1) # aPPS.NumberOfVertices() == 10
53 aPPS.Add(aPoints2) # aPPS.NumberOfVertices() == 20
54 return aPPS
55
56# Create PLS from lists of points: each list builds a cadex.PolyLine
57def CreatePolyLineSet() -> cadex.ModelData_PolyPointSet:
58 aPoints1 = cadex.ModelData_PointList()
59 aPoints2 = cadex.ModelData_PointList()
60
61 for i in range(10):
62 aPoints1.append(cadex.ModelData_Point(i, 3, i / 2))
63 aPoints2.append(cadex.ModelData_Point(i, 0, i % 2))
64
65 # For each cadex.list of points there will be a cadex.PolyLine
67 aPLS.AddPolyline(aPoints1) # aPLS.NumberOfPolyLines() == 1
68 aPLS.AddPolyline(aPoints2) # aPLS.NumberOfPolyLines() == 2
69 return aPLS
70
71# Creates ITS with full information provided
72def CreateITS() -> cadex.ModelData_IndexedTriangleSet:
73 aCoords = [cadex.ModelData_Point( 1.0, 1.0, 1.0),
74 cadex.ModelData_Point(-1.0, 1.0, 1.0),
75 cadex.ModelData_Point(-1.0, -1.0, 1.0),
76 cadex.ModelData_Point( 1.0, -1.0, 1.0),
77 cadex.ModelData_Point( 1.0, 1.0, -1.0),
78 cadex.ModelData_Point(-1.0, 1.0, -1.0),
79 cadex.ModelData_Point(-1.0, -1.0, -1.0),
80 cadex.ModelData_Point( 1.0, -1.0, -1.0)
81 ]
82
83
84
85 aVerticesIndices = [0, 1, 2, 3, #1
86 1, 0, 4, 5, #2
87 2, 1, 5, 6, #3
88 3, 2, 6, 7, #4
89 0, 3, 7, 4, #5
90 7, 6, 5, 4 #6
91 ]
92
93 aCounts = [ 4, 4, 4, 4, 4, 4 ]
94
95 aNormals = [cadex.ModelData_Vectorf( 0, 0, 1),
98 cadex.ModelData_Vectorf( 0, -1, 0),
100 cadex.ModelData_Vectorf( 0, 0, -1)
101 ]
102
103 aNormalsIndices = [0, 0, 0, 0, #1
104 1, 1, 1, 1, #2
105 2, 2, 2, 2, #3
106 3, 3, 3, 3, #4
107 4, 4, 4, 4, #5
108 5, 5, 5, 5 #6
109 ]
110
111 aColors = [cadex.ModelData_Color(255, 0, 0),
112 cadex.ModelData_Color( 0, 255, 0),
113 cadex.ModelData_Color( 0, 0, 255),
114 cadex.ModelData_Color(255, 255, 0),
115 cadex.ModelData_Color( 0, 255, 255),
116 cadex.ModelData_Color(255, 255, 255)
117 ]
118
119 aColorIndices = [0, 0, 0, 0, #1
120 1, 1, 1, 1, #2
121 2, 2, 2, 2, #3
122 3, 3, 3, 3, #4
123 4, 4, 4, 4, #5
124 5, 5, 5, 5 #6
125 ]
126
128 anITS.AddCoordinates(aCoords, aVerticesIndices, aCounts)
129 anITS.AddNormals(aNormals, aNormalsIndices, aCounts)
130 anITS.AddColors(aColors, aColorIndices, aCounts)
131 return anITS
132
133def main():
134 aKey = license.Value()
135
136 if not cadex.LicenseManager.Activate(aKey):
137 print("Failed to activate CAD Exchanger license.")
138 return 1
139 # Create PolyPointSet and explore it
140 aPPS = CreatePolyPointSet()
141
142 # Create PolyLineSet and explore it
143 aPLS = CreatePolyLineSet()
144
145 # Create IndexedTriangleSet and explore it
146 anITS = CreateITS()
147
148 aPolyWithPPS = cadex.ModelData_PolyRepresentation(aPPS)
149 aPolyWithPLS = cadex.ModelData_PolyRepresentation(aPLS)
150 aPolyWithITS = cadex.ModelData_PolyRepresentation(anITS)
151
152 aPart = cadex.ModelData_Part()
153
154 aPart.AddRepresentation(aPolyWithPPS)
155 aPart.AddRepresentation(aPolyWithPLS)
156 aPart.AddRepresentation(aPolyWithITS)
157
158 aModel = cadex.ModelData_Model()
159 aModel.AddRoot(aPart)
160 cadex.ModelData_ModelWriter().Write(aModel, cadex.Base_UTF16String("out/Poly.cdx"))
161
162 print("Completed")
163 return 0
164
165if __name__ == "__main__":
166 sys.exit(main())
Defines a Unicode (UTF-16) string wrapping a standard string.
Definition: Base_UTF16String.hxx:34
Defines an RGBA color (with alpha channel).
Definition: ModelData_Color.hxx:34
Defines a polygonal shape consisting of triangles.
Definition: ModelData_IndexedTriangleSet.hxx:35
Provides CAD Exchanger data model.
Definition: ModelData_Model.hxx:43
Writes any format that CAD Exchanger can export.
Definition: ModelData_ModelWriter.hxx:33
Defines a leaf node in the scene graph hiearchy.
Definition: ModelData_Part.hxx:35
Defines a 3D point.
Definition: ModelData_Point.hxx:295
Defines a polygonal shape consisting of polylines.
Definition: ModelData_PolyLineSet.hxx:31
Defines a polygonal shape consisting of individual points.
Definition: ModelData_PolyPointSet.hxx:31
Defines polygonal (faceted or tessellated) representation of part.
Definition: ModelData_PolyRepresentation.hxx:39
cadex::internal::ModelData_BaseVector< float > ModelData_Vectorf
Definition: ModelData_Vector.hxx:432