53a73fc1 |
1 | // Created on: 03/19/2018 |
2 | // Created by: Eugeny MALTCHIKOV |
3 | // Copyright (c) 2018 OPEN CASCADE SAS |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
16 | #include <BOPTest.hxx> |
17 | |
18 | #include <BOPAlgo_MakePeriodic.hxx> |
19 | |
20 | #include <BOPTest_DrawableShape.hxx> |
21 | #include <BOPTest_Objects.hxx> |
22 | |
23 | #include <BRep_Builder.hxx> |
24 | |
25 | #include <BRepTest_Objects.hxx> |
26 | |
27 | #include <DBRep.hxx> |
28 | #include <Draw.hxx> |
29 | |
30 | #include <TopoDS.hxx> |
31 | #include <TopoDS_Compound.hxx> |
32 | |
33 | static Standard_Integer MakePeriodic(Draw_Interpretor&, Standard_Integer, const char**); |
34 | static Standard_Integer GetTwins(Draw_Interpretor&, Standard_Integer, const char**); |
35 | static Standard_Integer RepeatShape(Draw_Interpretor&, Standard_Integer, const char**); |
36 | static Standard_Integer ClearRepetitions(Draw_Interpretor&, Standard_Integer, const char**); |
37 | |
38 | namespace |
39 | { |
40 | static BOPAlgo_MakePeriodic ThePeriodicityMaker; |
41 | } |
42 | |
43 | //======================================================================= |
44 | //function : PeriodicityCommands |
45 | //purpose : |
46 | //======================================================================= |
47 | void BOPTest::PeriodicityCommands(Draw_Interpretor& theCommands) |
48 | { |
49 | static Standard_Boolean done = Standard_False; |
50 | if (done) return; |
51 | done = Standard_True; |
52 | // Chapter's name |
53 | const char* group = "BOPTest commands"; |
54 | // Commands |
55 | theCommands.Add("makeperiodic", "makeperiodic result shape [-x/y/z period [-trim first]]\n" |
56 | "\t\tMake the shape periodic in the required directions.\n" |
57 | "\t\tresult - resulting periodic shape;\n" |
58 | "\t\t-x/y/z period - option to make the shape periodic in X, Y or Z\n " |
59 | "\t\t direction with the given period;\n" |
60 | "\t\t-trim first - option to trim the shape to fit the required period,\n" |
61 | "\t\t starting the period in first.", |
62 | __FILE__, MakePeriodic, group); |
63 | |
64 | theCommands.Add("periodictwins", "periodictwins twins shape\n" |
65 | "\t\tReturns the twins for the shape located on the opposite side of the periodic shape.", |
66 | __FILE__, GetTwins, group); |
67 | |
68 | // Repetition commands |
69 | theCommands.Add("repeatshape", "repeatshape result -x/y/z times\n" |
70 | "\t\tRepeats the periodic shape in periodic directions required number of times.\n" |
71 | "\t\tresult - resulting shape;\n" |
72 | "\t\t-x/y/z times - direction for repetition and number of repetitions.", |
73 | __FILE__, RepeatShape, group); |
74 | |
0c09fd3c |
75 | theCommands.Add("clearrepetitions", "clearrepetitions [result]\n" |
76 | "\t\tClears all previous repetitions of the periodic shape.", |
53a73fc1 |
77 | __FILE__, ClearRepetitions, group); |
78 | } |
79 | |
80 | //======================================================================= |
81 | //function : MakePeriodic |
82 | //purpose : |
83 | //======================================================================= |
84 | Standard_Integer MakePeriodic(Draw_Interpretor& theDI, |
85 | Standard_Integer theArgc, |
86 | const char ** theArgv) |
87 | { |
88 | if (theArgc < 5) |
89 | { |
90 | theDI.PrintHelp(theArgv[0]); |
91 | return 1; |
92 | } |
93 | |
94 | // Get the shape to make periodic |
95 | TopoDS_Shape aShape = DBRep::Get(theArgv[2]); |
96 | if (aShape.IsNull()) |
97 | { |
98 | theDI << "Error: " << theArgv[2] << " is a null shape.\n"; |
99 | return 1; |
100 | } |
101 | |
102 | ThePeriodicityMaker.Clear(); |
103 | ThePeriodicityMaker.SetShape(aShape); |
104 | |
105 | for (Standard_Integer i = 3; i < theArgc;) |
106 | { |
107 | // Get periodicity |
108 | Standard_Integer iDir = i; |
109 | |
110 | Standard_Integer aDirID = -1; |
111 | if (!strcasecmp(theArgv[i], "-x")) |
112 | aDirID = 0; |
113 | else if (!strcasecmp(theArgv[i], "-y")) |
114 | aDirID = 1; |
115 | else if (!strcasecmp(theArgv[i], "-z")) |
116 | aDirID = 2; |
117 | else |
118 | { |
119 | theDI << theArgv[i] << " - Invalid key\n"; |
120 | return 1; |
121 | } |
122 | |
123 | char cDirName[2]; |
124 | sprintf(cDirName, "%c", theArgv[iDir][1]); |
125 | |
126 | if (theArgc == (i + 1)) |
127 | { |
128 | theDI << "Period for " << cDirName << " direction is not set\n"; |
129 | return 1; |
130 | } |
131 | |
132 | Standard_Real aPeriod = Draw::Atof(theArgv[++i]); |
133 | |
134 | ThePeriodicityMaker.MakePeriodic(aDirID, Standard_True, aPeriod); |
135 | |
136 | ++i; |
137 | if (theArgc > i + 1) |
138 | { |
139 | // Check if trimming is necessary |
140 | if (!strcmp(theArgv[i], "-trim")) |
141 | { |
142 | if (theArgc == (i + 1)) |
143 | { |
144 | theDI << "Trim bounds for " << cDirName << " direction are not set\n"; |
145 | return 1; |
146 | } |
147 | Standard_Real aFirst = Draw::Atof(theArgv[++i]); |
148 | |
149 | ThePeriodicityMaker.SetTrimmed(aDirID, Standard_False, aFirst); |
150 | ++i; |
151 | } |
152 | } |
153 | } |
154 | |
155 | ThePeriodicityMaker.SetRunParallel(BOPTest_Objects::RunParallel()); |
156 | |
157 | // Perform operation |
158 | ThePeriodicityMaker.Perform(); |
159 | |
160 | // Print Error/Warning messages |
161 | BOPTest::ReportAlerts(ThePeriodicityMaker.GetReport()); |
162 | |
163 | // Set the history of the operation in session |
164 | BRepTest_Objects::SetHistory(ThePeriodicityMaker.History()); |
165 | |
166 | if (ThePeriodicityMaker.HasErrors()) |
167 | return 0; |
168 | |
169 | // Draw the result shape |
170 | const TopoDS_Shape& aResult = ThePeriodicityMaker.Shape(); |
171 | DBRep::Set(theArgv[1], aResult); |
172 | |
173 | return 0; |
174 | } |
175 | |
176 | //======================================================================= |
177 | //function : GetTwin |
178 | //purpose : |
179 | //======================================================================= |
180 | Standard_Integer GetTwins(Draw_Interpretor& theDI, |
181 | Standard_Integer theArgc, |
182 | const char ** theArgv) |
183 | { |
184 | if (theArgc != 3) |
185 | { |
186 | theDI.PrintHelp(theArgv[0]); |
187 | return 1; |
188 | } |
189 | |
190 | // Get the shape to find twins |
191 | TopoDS_Shape aShape = DBRep::Get(theArgv[2]); |
192 | if (aShape.IsNull()) |
193 | { |
194 | theDI << "Error: " << theArgv[2] << " is a null shape.\n"; |
195 | return 1; |
196 | } |
197 | |
198 | const TopTools_ListOfShape& aTwins = ThePeriodicityMaker.GetTwins(aShape); |
199 | |
200 | TopoDS_Shape aCTwins; |
201 | if (aTwins.IsEmpty()) |
202 | theDI << "No twins for the shape.\n"; |
203 | else if (aTwins.Extent() == 1) |
204 | aCTwins = aTwins.First(); |
205 | else |
206 | { |
207 | BRep_Builder().MakeCompound(TopoDS::Compound(aCTwins)); |
208 | for (TopTools_ListIteratorOfListOfShape it(aTwins); it.More(); it.Next()) |
209 | BRep_Builder().Add(aCTwins, it.Value()); |
210 | } |
211 | |
212 | DBRep::Set(theArgv[1], aCTwins); |
213 | |
214 | return 0; |
215 | } |
216 | |
217 | //======================================================================= |
218 | //function : RepeatShape |
219 | //purpose : |
220 | //======================================================================= |
221 | Standard_Integer RepeatShape(Draw_Interpretor& theDI, |
222 | Standard_Integer theArgc, |
223 | const char ** theArgv) |
224 | { |
225 | if (theArgc < 4) |
226 | { |
227 | theDI.PrintHelp(theArgv[0]); |
228 | return 1; |
229 | } |
230 | |
231 | for (Standard_Integer i = 2; i < theArgc; ++i) |
232 | { |
233 | Standard_Integer aDirID = -1; |
234 | if (!strcasecmp(theArgv[i], "-x")) |
235 | aDirID = 0; |
236 | else if (!strcasecmp(theArgv[i], "-y")) |
237 | aDirID = 1; |
238 | else if (!strcasecmp(theArgv[i], "-z")) |
239 | aDirID = 2; |
240 | else |
241 | { |
242 | theDI << theArgv[i] << " - Invalid key\n"; |
243 | return 1; |
244 | } |
245 | |
246 | char cDirName[2]; |
247 | sprintf(cDirName, "%c", theArgv[i][1]); |
248 | |
249 | Standard_Integer aTimes = 0; |
250 | if (theArgc > i + 1) |
251 | aTimes = Draw::Atoi(theArgv[++i]); |
252 | |
253 | if (aTimes == 0) |
254 | { |
255 | theDI << "Number of repetitions for " << cDirName << " direction is not set\n"; |
256 | return 1; |
257 | } |
258 | |
259 | ThePeriodicityMaker.RepeatShape(aDirID, aTimes); |
260 | } |
261 | |
262 | // Print Error/Warning messages |
263 | BOPTest::ReportAlerts(ThePeriodicityMaker.GetReport()); |
264 | |
265 | // Set the history of the operation in session |
266 | BRepTest_Objects::SetHistory(ThePeriodicityMaker.History()); |
267 | |
268 | if (ThePeriodicityMaker.HasErrors()) |
269 | return 0; |
270 | |
271 | // Draw the result shape |
272 | const TopoDS_Shape& aResult = ThePeriodicityMaker.RepeatedShape(); |
273 | DBRep::Set(theArgv[1], aResult); |
274 | |
275 | return 0; |
276 | } |
277 | |
278 | //======================================================================= |
279 | //function : ClearRepetitions |
280 | //purpose : |
281 | //======================================================================= |
282 | Standard_Integer ClearRepetitions(Draw_Interpretor&, |
0c09fd3c |
283 | Standard_Integer theArgc, |
284 | const char **theArgv) |
53a73fc1 |
285 | { |
286 | // Clear all previous repetitions |
287 | ThePeriodicityMaker.ClearRepetitions(); |
288 | |
289 | // Set the history of the operation in session |
290 | BRepTest_Objects::SetHistory(ThePeriodicityMaker.History()); |
291 | |
0c09fd3c |
292 | if (theArgc > 1) |
293 | DBRep::Set(theArgv[1], ThePeriodicityMaker.Shape()); |
294 | |
53a73fc1 |
295 | return 0; |
296 | } |