Hide menu
Loading...
Searching...
No Matches
projector/poly_projector/poly_projector.py
Refer to the Projector Example
1# $Id$
2
3# Copyright (C) 2008-2014, Roman Lygin. All rights reserved.
4# Copyright (C) 2014-2026, CADEX. All rights reserved.
5
6# This file is part of the Manufacturing Toolkit software.
7
8# You may use this file under the terms of the BSD license as follows:
9
10# Redistribution and use in source and binary forms, with or without
11# modification, are permitted provided that the following conditions are met:
12# * Redistributions of source code must retain the above copyright notice,
13# this list of conditions and the following disclaimer.
14# * Redistributions in binary form must reproduce the above copyright notice,
15# this list of conditions and the following disclaimer in the documentation
16# and/or other materials provided with the distribution.
17
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28# POSSIBILITY OF SUCH DAMAGE.
29
30import os
31import sys
32from pathlib import Path
33
34import manufacturingtoolkit.MTKCore as mtk
35
36sys.path.append(os.path.abspath(os.path.dirname(Path(__file__).resolve()) + "/../../"))
37
38import mtk_license as license
39
40def main(theSource: str, theOutFolder: str):
41 aKey = license.Value()
42
43 if not mtk.LicenseManager.Activate(aKey):
44 print("Failed to activate Manufacturing Toolkit license.")
45 return 1
46
47 aModel = mtk.ModelData_Model()
48
49 # Reading the file
50 if not mtk.ModelData_ModelReader().Read(mtk.UTF16String(theSource), aModel):
51 print("Failed to read the file " + theSource)
52 return 1
53
54 print("Model: ", aModel.Name(), "\n", sep="")
55
56 anOutX = mtk.UTF16String(theOutFolder + "projection_X.stl")
57 anOutY = mtk.UTF16String(theOutFolder + "projection_Y.stl")
58 anOutZ = mtk.UTF16String(theOutFolder + "projection_Z.stl")
59
60 if not ComputeProjection(aModel, mtk.Geom_Direction.XDir(), mtk.UTF16String("X"), anOutX):
61 print("Failed to write X projection.")
62 return 1
63
64 if not ComputeProjection(aModel, mtk.Geom_Direction.YDir(), mtk.UTF16String("Y"), anOutY):
65 print("Failed to write Y projection.")
66 return 1
67
68 if not ComputeProjection(aModel, mtk.Geom_Direction.ZDir(), mtk.UTF16String("Z"), anOutZ):
69 print("Failed to write Z projection.")
70 return 1
71
72 print("Output written:")
73 print(" " + str(anOutX))
74 print(" " + str(anOutY))
75 print(" " + str(anOutZ))
76
77 return 0
78
79if __name__ == "__main__":
80 if len(sys.argv) != 3:
81 print(" <input_file> is a name of the file to be read")
82 print(" <output_folder> is a folder to save projections to (must end with '/' or '\\')")
83 sys.exit(1)
84
85 aSource = os.path.abspath(sys.argv[1])
86 aOutFolder = os.path.abspath(sys.argv[2])
87
88 sys.exit(main(aSource, aOutFolder))
89
90def PrintProjectionInfo(thePart: mtk.ModelData_Part,
91 theDirectionName: mtk.UTF16String,
92 theProjection: mtk.Projector_Projection) -> None:
93 print(f"Part [{thePart.Name()}], projection {theDirectionName}:")
94 print(f" area = {theProjection.Area()} mm")
95 print(f" outer perimeter = {theProjection.OuterPerimeter()} mm")
96 print("")
97
98class ProjectionComputer(mtk.ModelData_ModelElementVoidVisitor):
99 def __init__(self, theDirection: mtk.Geom_Direction, theDirectionName: mtk.UTF16String):
100 super().__init__()
101 self.myDirection = theDirection
102 self.myDirectionName = theDirectionName
103 self.myProjector = mtk.Projector_PolyProjector()
104 self.myPartProjections = []
105
106 def SaveProjection(self, theFileName: mtk.UTF16String) -> bool:
107 aPart = mtk.ModelData_Part(mtk.UTF16String("Projection"))
108
109 for aProjection in self.myPartProjections:
110 aMeshBody = mtk.ModelData_MeshBody(aProjection.Mesh())
111 aPart.AddBody(aMeshBody)
112
113 anOutModel = mtk.ModelData_Model(mtk.UTF16String("Projector"))
114 anOutModel.AddRoot(aPart)
115
116 aWriter = mtk.ModelData_ModelWriter()
117 return aWriter.Write(anOutModel, theFileName)
118
119 def VisitPart(self, thePart: mtk.ModelData_Part):
120 aProjection = self.myProjector.Perform(thePart, self.myDirection)
121 PrintProjectionInfo(thePart, self.myDirectionName, aProjection)
122 self.myPartProjections.append(aProjection)
123
124
125def ComputeProjection(theModel: mtk.ModelData_Model,
126 theDirection: mtk.Geom_Direction,
127 theDirectionName: mtk.UTF16String,
128 theOutFileName: mtk.UTF16String) -> bool:
129 aComputer = ProjectionComputer(theDirection, theDirectionName)
130 theModel.Accept(aComputer)
131 return aComputer.SaveProjection(theOutFileName)