0030416: Incorrect implementation of the method Bnd_OBB::SquareExtent
[occt.git] / src / BRepTest / BRepTest_HistoryCommands.cxx
1 // Created on: 2018/03/21
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 <BRepTest.hxx>
17
18 #include <BRep_Builder.hxx>
19
20 #include <BRepTest_DrawableHistory.hxx>
21 #include <BRepTest_Objects.hxx>
22
23 #include <Draw.hxx>
24 #include <DBRep.hxx>
25
26 #include <TopoDS.hxx>
27 #include <TopoDS_Compound.hxx>
28
29 static Standard_Integer SetFillHistory(Draw_Interpretor&, Standard_Integer, const char**);
30 static Standard_Integer SaveHistory   (Draw_Interpretor&, Standard_Integer, const char**);
31 static Standard_Integer Modified      (Draw_Interpretor&, Standard_Integer, const char**);
32 static Standard_Integer Generated     (Draw_Interpretor&, Standard_Integer, const char**);
33 static Standard_Integer IsDeleted     (Draw_Interpretor&, Standard_Integer, const char**);
34
35 //=======================================================================
36 //function : HistoryCommands
37 //purpose  : 
38 //=======================================================================
39 void BRepTest::HistoryCommands(Draw_Interpretor& theCommands)
40 {
41   static Standard_Boolean isDone = Standard_False;
42   if (isDone) return;
43   isDone = Standard_True;
44   // Chapter's name
45   const char* group = "History commands";
46
47   // Commands
48   theCommands.Add("setfillhistory" , "Controls the history collection by the algorithms and its saving into the session after algorithm is done.\n"
49                   "\t\tUsage: setfillhistory [flag]\n"
50                   "\t\tw/o arguments prints the current state of the option;\n"
51                   "\t\tflag == 0 - history will not be collected and saved;\n"
52                   "\t\tflag != 0 - history will be collected and saved into the session (default).",
53                   __FILE__, SetFillHistory , group);
54
55   theCommands.Add("savehistory" , "savehistory name\n"
56                   "\t\tSaves the history from the session into a drawable object with the name <name>.",
57                   __FILE__, SaveHistory , group);
58
59   theCommands.Add("modified" , "modified modified_shapes history shape\n"
60                   "\t\tReturns the shapes Modified from the given shape in the given history",
61                   __FILE__, Modified , group);
62
63   theCommands.Add("generated", "generated generated_shapes history shape\n"
64                   "\t\tReturns the shapes Generated from the given shape in the given history",
65                   __FILE__, Generated, group);
66
67   theCommands.Add("isdeleted", "isdeleted history shape\n"
68                   "\t\tChecks if the given shape has been deleted in the given history",
69                   __FILE__, IsDeleted, group);
70 }
71
72 //=======================================================================
73 //function : SetFillHistory
74 //purpose  : 
75 //=======================================================================
76 Standard_Integer SetFillHistory(Draw_Interpretor& theDI,
77                                 Standard_Integer theArgc,
78                                 const char** theArgv)
79 {
80   if (theArgc > 2)
81   {
82     theDI.PrintHelp(theArgv[0]);
83     return 1;
84   }
85
86   if (theArgc == 1)
87   {
88     theDI << "Filling of the history is " <<
89       (BRepTest_Objects::IsHistoryNeeded() ? "enabled." : "disabled.");
90   }
91   else
92   {
93     Standard_Integer iHist = Draw::Atoi(theArgv[1]);
94     BRepTest_Objects::SetToFillHistory(iHist != 0);
95   }
96   return 0;
97 }
98
99 //=======================================================================
100 //function : SaveHistory
101 //purpose  : 
102 //=======================================================================
103 Standard_Integer SaveHistory(Draw_Interpretor& theDI,
104                              Standard_Integer theArgc,
105                              const char** theArgv)
106 {
107   if (theArgc != 2)
108   {
109     theDI.PrintHelp(theArgv[0]);
110     return 1;
111   }
112
113   // Get the history from the session
114   Handle(BRepTools_History) aHistory = BRepTest_Objects::History();
115   if (aHistory.IsNull())
116   {
117     theDI << "No history has been prepared yet.";
118     return 1;
119   }
120
121   Handle(BRepTest_DrawableHistory) aDrawHist = new BRepTest_DrawableHistory(aHistory);
122
123   Draw::Set(theArgv[1], aDrawHist);
124
125   return 0;
126 }
127
128 //=======================================================================
129 //function : GetHistory
130 //purpose  : 
131 //=======================================================================
132 static Handle(BRepTools_History) GetHistory(Draw_Interpretor& theDI,
133                                             Standard_CString theName)
134 {
135   Handle(BRepTest_DrawableHistory) aHistory =
136     Handle(BRepTest_DrawableHistory)::DownCast(Draw::Get(theName));
137
138   if (aHistory.IsNull() || aHistory->History().IsNull())
139   {
140     theDI << "History with the name " << theName << " does not exist.";
141     return NULL;
142   }
143
144   return aHistory->History();
145 }
146
147 //=======================================================================
148 //function : GetShape
149 //purpose  : 
150 //=======================================================================
151 static TopoDS_Shape GetShape(Draw_Interpretor& theDI,
152                              Standard_CString theName)
153 {
154   TopoDS_Shape aS = DBRep::Get(theName);
155
156   if (aS.IsNull())
157   {
158     theDI << theName << " is a null shape.";
159     return TopoDS_Shape();
160   }
161
162   if (!BRepTools_History::IsSupportedType(aS))
163   {
164     theDI << "History is not supported for this kind of shape.";
165     return TopoDS_Shape();
166   }
167   return aS;
168 }
169
170 //=======================================================================
171 //function : MakeCompound
172 //purpose  : 
173 //=======================================================================
174 static TopoDS_Shape MakeCompound(const TopTools_ListOfShape& theLS)
175 {
176   TopoDS_Shape aC;
177   if (theLS.Extent() == 1)
178     aC = theLS.First();
179   else
180   {
181     BRep_Builder().MakeCompound(TopoDS::Compound(aC));
182     TopTools_ListIteratorOfListOfShape it(theLS);
183     for (; it.More(); it.Next())
184       BRep_Builder().Add(aC, it.Value());
185   }
186   return aC;
187 }
188
189 //=======================================================================
190 //function : Modified
191 //purpose  : 
192 //=======================================================================
193 Standard_Integer Modified(Draw_Interpretor& theDI,
194                           Standard_Integer theArgc,
195                           const char** theArgv)
196 {
197   if (theArgc != 4)
198   {
199     theDI.PrintHelp(theArgv[0]);
200     return 1;
201   }
202
203   Handle(BRepTools_History) aHistory = GetHistory(theDI, theArgv[2]);
204   if (aHistory.IsNull())
205     return 1;
206
207   TopoDS_Shape aS = GetShape(theDI, theArgv[3]);
208   if (aS.IsNull())
209     return 1;
210
211   const TopTools_ListOfShape& aModified = aHistory->Modified(aS);
212
213   if (aModified.IsEmpty())
214   {
215     theDI << "The shape has not been modified.";
216     return 0;
217   }
218
219   DBRep::Set(theArgv[1], MakeCompound(aModified));
220
221   return 0;
222 }
223
224 //=======================================================================
225 //function : Generated
226 //purpose  : 
227 //=======================================================================
228 Standard_Integer Generated(Draw_Interpretor& theDI,
229                            Standard_Integer theArgc,
230                            const char** theArgv)
231 {
232   if (theArgc != 4)
233   {
234     theDI.PrintHelp(theArgv[0]);
235     return 1;
236   }
237
238   Handle(BRepTools_History) aHistory = GetHistory(theDI, theArgv[2]);
239   if (aHistory.IsNull())
240     return 1;
241
242   TopoDS_Shape aS = GetShape(theDI, theArgv[3]);
243   if (aS.IsNull())
244     return 1;
245
246   const TopTools_ListOfShape& aGenerated = aHistory->Generated(aS);
247
248   if (aGenerated.IsEmpty())
249   {
250     theDI << "No shapes were generated from the shape.";
251     return 0;
252   }
253
254   DBRep::Set(theArgv[1], MakeCompound(aGenerated));
255
256   return 0;
257 }
258
259 //=======================================================================
260 //function : IsDeleted
261 //purpose  : 
262 //=======================================================================
263 Standard_Integer IsDeleted(Draw_Interpretor& theDI,
264                            Standard_Integer theArgc,
265                            const char** theArgv)
266 {
267   if (theArgc != 3)
268   {
269     theDI.PrintHelp(theArgv[0]);
270     return 1;
271   }
272
273   Handle(BRepTools_History) aHistory = GetHistory(theDI, theArgv[1]);
274   if (aHistory.IsNull())
275     return 1;
276
277   TopoDS_Shape aS = GetShape(theDI, theArgv[2]);
278   if (aS.IsNull())
279     return 1;
280
281   theDI << (aHistory->IsRemoved(aS) ? "Deleted." : "Not deleted.");
282
283   return 0;
284 }