Hide menu
Loading...
Searching...
No Matches
meshing/lods/lods.py

Refer to the Level of Details (LOD's) 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 NumberOfTriangles(thePoly: cadex.ModelData_PolyRepresentation) -> int:
43 trianglesNB = 0
44
45 aList = thePoly.Get()
46 for aPVS in aList:
48 anITS = cadex.ModelData_IndexedTriangleSet.Cast(aPVS)
49 trianglesNB += anITS.NumberOfFaces()
50 return trianglesNB
51
52def CreateSphereBRep(thePosition: cadex.ModelData_Point, theRadius: float) -> cadex.ModelData_BRepRepresentation:
53 aSphere = cadex.ModelAlgo_TopoPrimitives.CreateSphere(thePosition, theRadius)
54 aBody = cadex.ModelData_Body.Create(aSphere)
56 return aBRep
57
58def AddPolyToPart(thePart: cadex.ModelData_Part, theLOD):
59 aBRep = thePart.BRepRepresentation()
61 aMesher = cadex.ModelAlgo_BRepMesher(aParam)
62
63 aPoly = aMesher.Compute(aBRep)
64
65 thePart.AddRepresentation(aPoly)
66 print(f"A polygonal representation with {NumberOfTriangles(aPoly)} triangles has been added");
67
68
69def main():
70 aKey = license.Value()
71
72 if not cadex.LicenseManager.Activate(aKey):
73 print("Failed to activate CAD Exchanger license.")
74 return 1
75
76 aBRep = CreateSphereBRep(cadex.ModelData_Point(0.0, 0.0, 0.0), 10)
77 aPart = cadex.ModelData_Part(aBRep, cadex.Base_UTF16String("Sphere"))
78
79 AddPolyToPart(aPart, cadex.ModelAlgo_BRepMesherParameters.Coarse)
80 AddPolyToPart(aPart, cadex.ModelAlgo_BRepMesherParameters.Medium)
81 AddPolyToPart(aPart, cadex.ModelAlgo_BRepMesherParameters.Fine)
82
83 aModel = cadex.ModelData_Model()
84 aModel.AddRoot(aPart)
85
86 if not cadex.ModelData_ModelWriter().Write(aModel, cadex.Base_UTF16String("out/SphereWithLODs.cdx")):
87 print("Unable to save the model")
88 return 1
89
90 print("Completed")
91 return 0
92
93if __name__ == "__main__":
94 sys.exit(main())
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 ModelData_Solid CreateSphere(double theRadius, double theU=2 *M_PI, double theVmin=-M_PI_2, double theVmax=M_PI_2)
Creates a sphere.
Definition: ModelAlgo_TopoPrimitives.cxx:148
Defines precise Boundary Representation of part.
Definition: ModelData_BRepRepresentation.hxx:39
static ModelData_Body Create(const ModelData_Shape &theShape)
Creates a body from an arbitrary shape.
Definition: ModelData_Body.cxx:223
static IdType GetTypeId()
Definition: ModelData_IndexedTriangleSet.cxx:134
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