0023510: Integration of test grid "vis" into the new testing system
[occt.git] / src / QAViewer2dTest / QAViewer2dTest.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 #if defined(WOKC40)
19
20 #include <QAViewer2dTest.hxx>
21 #include <QAViewer2dTest_EventManager.hxx>
22 #include <QAViewer2dTest_DoubleMapOfInteractiveAndName.hxx>
23 #include <QAViewer2dTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName.hxx>
24
25 #include <Draw_Interpretor.hxx>
26
27 #include <TopoDS.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopTools_HArray1OfShape.hxx>
30
31 #include <TColStd_ListOfTransient.hxx>
32 #include <TColStd_MapOfInteger.hxx>
33 #include <TColStd_HArray1OfTransient.hxx>
34
35 #include <TCollection_AsciiString.hxx>
36
37 #include <AIS2D_ProjShape.hxx>
38 #include <AIS2D_InteractiveObject.hxx>
39 #include <AIS2D_InteractiveContext.hxx>
40
41 extern int ViewerMainLoop2d (Standard_Integer argc, char** argv);
42
43 TopoDS_Shape GetShapeFromName2d (char* name);
44
45 static TColStd_ListOfTransient theEventMgrs2d;
46
47 //==============================================================================
48 //  VIEWER OBJECT MANAGEMENT GLOBAL VARIABLES
49 //==============================================================================
50 QAViewer2dTest_DoubleMapOfInteractiveAndName& GetMapOfAIS2D()
51 {
52   static QAViewer2dTest_DoubleMapOfInteractiveAndName TheMap;
53   return TheMap;
54 }
55
56 static void VwrTst_InitEventMgr2d (const Handle(AIS2D_InteractiveContext)& Ctx)
57 {
58   theEventMgrs2d.Clear();
59   theEventMgrs2d.Prepend(new QAViewer2dTest_EventManager(Ctx));
60 }
61
62 static Handle(V2d_View)& a2DView()
63 {
64   static Handle(V2d_View) Viou;
65   return Viou;
66 }
67
68 Handle(AIS2D_InteractiveContext)& TheAIS2DContext()
69
70   static Handle(AIS2D_InteractiveContext) aContext;
71   return aContext;
72 }
73
74 Handle(V2d_View) QAViewer2dTest::CurrentView()
75 {
76   return a2DView();
77 }
78
79 void QAViewer2dTest::CurrentView (const Handle(V2d_View)& V)
80 {
81   a2DView() = V;
82 }
83
84 Handle(AIS2D_InteractiveContext) QAViewer2dTest::GetAIS2DContext()
85 {
86   return TheAIS2DContext();
87 }
88
89 Handle(V2d_Viewer) QAViewer2dTest::GetViewerFromContext()
90 {
91   return TheAIS2DContext()->CurrentViewer();
92 }
93
94 Handle(V2d_Viewer) QAViewer2dTest::GetCollectorFromContext()
95 {
96   return TheAIS2DContext()->CurrentViewer();
97 }
98
99 void QAViewer2dTest::SetAIS2DContext (const Handle(AIS2D_InteractiveContext)& aCtx)
100 {
101   TheAIS2DContext() = aCtx;
102   QAViewer2dTest::ResetEventManager();
103 }
104
105 void QAViewer2dTest::SetEventManager (const Handle(QAViewer2dTest_EventManager)& EM)
106 {
107   theEventMgrs2d.Prepend(EM);
108 }
109
110 void QAViewer2dTest::UnsetEventManager()
111 {
112   theEventMgrs2d.RemoveFirst();
113 }
114
115 void QAViewer2dTest::ResetEventManager()
116 {
117   VwrTst_InitEventMgr2d(QAViewer2dTest::GetAIS2DContext());
118 }
119
120 //==============================================================================
121 //function : CurrentEventManager
122 //purpose  : 
123 //==============================================================================
124 Handle(QAViewer2dTest_EventManager) QAViewer2dTest::CurrentEventManager()
125 {
126   Handle(QAViewer2dTest_EventManager) EM;
127   if(theEventMgrs2d.IsEmpty()) return EM;
128   Handle(Standard_Transient) Tr =  theEventMgrs2d.First();
129   EM = *((Handle(QAViewer2dTest_EventManager)*)&Tr);
130   return EM;
131 }
132
133 //==============================================================================
134 //function : Clear
135 //purpose  : Remove all the object from the viewer
136 //==============================================================================
137 void QAViewer2dTest::Clear()
138 {
139   if (!QAViewer2dTest::CurrentView().IsNull())
140   {
141     TheAIS2DContext()->CloseLocalContext();
142     QAViewer2dTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName it (GetMapOfAIS2D());
143     while (it.More())
144     {
145       cout << "Remove " << it.Key2() << endl;
146 //      TheAIS2DContext()->Remove(it.Key1(), Standard_False);
147       TheAIS2DContext()->Erase(it.Key1(), Standard_False, Standard_False);
148       it.Next();
149     }
150     TheAIS2DContext()->UpdateCurrentViewer();
151     GetMapOfAIS2D().Clear();
152   }
153 }
154
155 //==============================================================================
156 //function : StandardModeActivation
157 //purpose  : Activate a selection mode, vertex, edge, wire ..., in a local
158 //           Context
159 //==============================================================================
160 void QAViewer2dTest::StandardModeActivation (const Standard_Integer mode)
161 {
162   Handle(AIS2D_InteractiveContext) aContext = QAViewer2dTest::GetAIS2DContext();
163   aContext->SetPickMode(Graphic2d_PM_INTERSECT);
164
165   char *cmode = "?????????";
166
167   switch ((AIS2D_TypeOfDetection)mode)
168   {
169     case AIS2D_TOD_OBJECT:    cmode = "OBJECT"; break;
170     case AIS2D_TOD_PRIMITIVE: cmode = "PRIMITIVE"; break;
171     case AIS2D_TOD_ELEMENT:   cmode = "ELEMENT"; break;
172     case AIS2D_TOD_VERTEX:    cmode = "VERTEX"; break;
173     case AIS2D_TOD_NONE:      cmode = "NONE"; break;
174   }
175
176   // Activate
177   QAViewer2dTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName it (GetMapOfAIS2D());
178   while (it.More())
179   {
180     Handle(AIS2D_InteractiveObject) ashape = it.Key1();
181     ashape->SetContext(aContext);
182 //      ashape->SetSelectionMode(mode);
183     ashape->SetHighlightMode((AIS2D_TypeOfDetection)mode);
184     it.Next();
185   }
186 //      aContext->ActivateStandardMode(AIS2D_ProjShape::SelectionType(mode));
187   cout << "Mode " << cmode << " ON" << endl;
188 }
189
190 //=======================================================================
191 //function : PickObjects
192 //purpose  : 
193 //=======================================================================
194 Standard_Boolean QAViewer2dTest::PickObjects (Handle(TColStd_HArray1OfTransient)& arr,
195                                             const AIS_KindOfInteractive TheType,
196                                              const Standard_Integer /*TheSignature*/,
197                                              const Standard_Integer MaxPick)
198 {
199   Handle(AIS2D_InteractiveObject) IO;
200   Standard_Integer curindex = (TheType == AIS_KOI_None) ? 0 : TheAIS2DContext()->OpenLocalContext();
201   
202   // step 1: prepare the data
203   if (curindex != 0)
204   {
205 //    Handle(AIS2D_SignatureFilter) F1 = new AIS2D_SignatureFilter(TheType,TheSignature);
206 //    TheAIS2DContext()->AddFilter(F1);
207   }
208   
209   // step 2 : wait for the selection...
210 //  Standard_Boolean IsGood (Standard_False);
211 //  Standard_Integer NbPick(0);
212   Standard_Boolean NbPickGood (0), NbToReach (arr->Length());
213   Standard_Integer NbPickFail(0);
214   Standard_Integer argccc = 5;
215   char *bufff[] = { "A", "B", "C","D", "E" };
216   char **argvvv = (char **) bufff;
217   
218   while (NbPickGood < NbToReach && NbPickFail <= MaxPick)
219   {
220     while (ViewerMainLoop2d(argccc,argvvv)) {}
221 /*    Standard_Integer NbStored = TheAIS2DContext()->NbSelected();
222     if((unsigned int ) NbStored != NbPickGood)
223       NbPickGood= NbStored;
224     else
225       NbPickFail++;
226     cout<<"NbPicked =  "<<NbPickGood<<" |  Nb Pick Fail :"<<NbPickFail<<endl;*/
227   }
228   
229   // step3 get result.
230   
231   if ((unsigned int ) NbPickFail >= NbToReach) return Standard_False;
232
233   /*
234   Standard_Integer i(0);
235   for(TheAIS2DContext()->InitSelected();
236       TheAIS2DContext()->MoreSelected();
237       TheAIS2DContext()->NextSelected()){
238     i++;
239     Handle(AIS2D_InteractiveObject) IO = TheAIS2DContext()->SelectedInteractive();
240     arr->SetValue(i,IO);
241   }*/
242
243   if (curindex > 0) TheAIS2DContext()->CloseLocalContext(curindex);
244   return Standard_True;
245 }
246
247 //=======================================================================
248 //function : PickObject
249 //purpose  : 
250 //=======================================================================
251 /*Handle(AIS2D_InteractiveObject) QAViewer2dTest::PickObject
252        (const AIS2D_KindOfInteractive TheType,
253         const Standard_Integer TheSignature,
254         const Standard_Integer MaxPick)
255 {
256   Handle(AIS2D_InteractiveObject) IO;
257   Standard_Integer curindex = (TheType == AIS2D_KOI_None) ? 0 : TheAIS2DContext()->OpenLocalContext();
258   
259   // step 1: prepare the data
260   
261   if(curindex !=0){
262     Handle(AIS2D_SignatureFilter) F1 = new AIS2D_SignatureFilter(TheType,TheSignature);
263     TheAIS2DContext()->AddFilter(F1);
264   }
265
266   // step 2 : wait for the selection...
267   Standard_Boolean IsGood (Standard_False);
268   Standard_Integer NbPick(0);
269   Standard_Integer argccc = 5;
270   char *bufff[] = { "VPick", "X", "VPickY","VPickZ", "VPickShape" };
271   char **argvvv = bufff;
272   
273   
274   while(!IsGood && NbPick<= MaxPick){
275     while(ViewerMainLoop2d(argccc,argvvv)){}
276     IsGood = (TheAIS2DContext()->NbSelected()>0) ;
277     NbPick++;
278     cout<<"Nb Pick :"<<NbPick<<endl;
279   }
280   
281   
282   // step3 get result.
283   if(IsGood){
284     TheAIS2DContext()->InitSelected();
285     IO = TheAIS2DContext()->SelectedInteractive();
286   }
287   
288   if(curindex!=0)
289     TheAIS2DContext()->CloseLocalContext(curindex);
290   return IO;
291 }*/
292
293 //=======================================================================
294 //function : PickShape
295 //purpose  : First Activate the rightmode + Put Filters to be able to
296 //           pick objets that are of type <TheType>...
297 //=======================================================================
298 TopoDS_Shape QAViewer2dTest::PickShape (const TopAbs_ShapeEnum /*TheType*/,
299                                       const Standard_Integer MaxPick)
300 {
301   // step 1: prepare the data
302   Standard_Integer curindex = TheAIS2DContext()->OpenLocalContext();
303   TopoDS_Shape result;
304   
305 /*  if(TheType==TopAbs_SHAPE)
306   {
307     Handle(AIS2D_TypeFilter) F1 = new AIS2D_TypeFilter(AIS2D_KOI_Shape);
308     TheAIS2DContext()->AddFilter(F1);
309   }
310   else
311   {
312     Handle(StdSelect_ShapeTypeFilter) TF = new StdSelect_ShapeTypeFilter(TheType);
313     TheAIS2DContext()->AddFilter(TF);
314     TheAIS2DContext()->ActivateStandardMode(TheType);
315   }*/
316
317   // step 2 : wait for the selection...
318   Standard_Boolean NoShape (Standard_True);
319   Standard_Integer NbPick (0);
320   Standard_Integer argccc = 5;
321   char *bufff[] = { "VPick", "X", "VPickY","VPickZ", "VPickShape" };
322   char **argvvv = (char **) bufff;
323
324   while (NoShape && NbPick <= MaxPick)
325   {
326     while (ViewerMainLoop2d(argccc,argvvv)) {}
327 //    NoShape = (TheAIS2DContext()->NbSelected() == 0);
328     NbPick++;
329     cout << "Nb Pick :" << NbPick << endl;
330   }
331   
332   // step3 get result.
333   if (!NoShape)
334   {
335 /*    TheAIS2DContext()->InitSelected();
336     if (TheAIS2DContext()->HasSelectedShape())
337       result = TheAIS2DContext()->SelectedShape();
338     else
339     {
340       Handle(AIS2D_InteractiveObject) IO = TheAIS2DContext()->SelectedInteractive();
341       result = (*((Handle(AIS2D_ProjShape)*) &IO))->Shape();
342     }*/
343   }
344
345   if (curindex > 0) TheAIS2DContext()->CloseLocalContext(curindex);
346   return result;
347 }
348
349 //=======================================================================
350 //function : PickShapes
351 //purpose  : 
352 //=======================================================================
353 Standard_Boolean QAViewer2dTest::PickShapes (const TopAbs_ShapeEnum /*TheType*/,
354                                            Handle(TopTools_HArray1OfShape)& thearr,
355                                            const Standard_Integer /*MaxPick*/)
356 {
357   Standard_Integer Taille = thearr->Length();
358   if (Taille > 1)
359     cout << " WARNING : Pick with Shift+ MB1 for Selection of more than 1 object" << endl;
360   
361   // step 1: prepare the data
362   Standard_Integer curindex = TheAIS2DContext()->OpenLocalContext();
363 /*  if (TheType == TopAbs_SHAPE)
364   {
365     Handle(AIS2D_TypeFilter) F1 = new AIS2D_TypeFilter(AIS2D_KOI_Shape);
366     TheAIS2DContext()->AddFilter(F1);
367   }
368   else
369   {
370     Handle(StdSelect_ShapeTypeFilter) TF = new StdSelect_ShapeTypeFilter(TheType);
371     TheAIS2DContext()->AddFilter(TF);
372     TheAIS2DContext()->ActivateStandardMode(TheType);
373   }*/
374
375   // step 2 : wait for the selection...
376   Standard_Boolean NbToReach (thearr->Length());
377   Standard_Integer NbPickFail (0);
378   /*
379   Standard_Boolean NbPickGood (0) ;
380   Standard_Integer argccc = 5;
381   char *bufff[] = { "A", "B", "C","D", "E" };
382   char **argvvv = bufff;
383   
384   while (NbPickGood < NbToReach && NbPickFail <= MaxPick)
385   {
386     while (ViewerMainLoop2d(argccc,argvvv)) {}
387     Standard_Integer NbStored = TheAIS2DContext()->NbSelected();
388     if ((unsigned int ) NbStored != NbPickGood)
389       NbPickGood = NbStored;
390     else
391       NbPickFail++;
392     cout<<"NbPicked =  "<<NbPickGood<<" |  Nb Pick Fail :"<<NbPickFail<<endl;
393   }*/
394   
395   // step3 get result.
396   if ((unsigned int) NbPickFail >= NbToReach) return Standard_False;
397
398   /*
399   Standard_Integer i(0);
400   for (TheAIS2DContext()->InitSelected();
401        TheAIS2DContext()->MoreSelected();
402        TheAIS2DContext()->NextSelected())
403   {
404     i++;
405     if(TheAIS2DContext()->HasSelectedShape())
406       thearr->SetValue(i,TheAIS2DContext()->SelectedShape());
407     else
408     {
409       Handle(AIS2D_InteractiveObject) IO = TheAIS2DContext()->SelectedInteractive();
410       thearr->SetValue(i,(*((Handle(AIS2D_ProjShape)*) &IO))->Shape());
411     }
412   }*/
413   
414   TheAIS2DContext()->CloseLocalContext(curindex);
415   return Standard_True;
416 }
417
418 //=====================================================================
419 //========================= for testing Draft and Rib =================
420 //=====================================================================
421 #include <BRepOffsetAPI_MakeThickSolid.hxx>
422 #include <DBRep.hxx>
423 #include <TopoDS_Face.hxx>
424 #include <gp_Pln.hxx>
425 #include <BRepOffsetAPI_DraftAngle.hxx>
426 #include <Precision.hxx>
427 #include <OSD_Environment.hxx>
428 #include <DrawTrSurf.hxx>
429 #if defined(BRepAlgoAPI_def01)
430 #include <BRepAlgoAPI.hxx>
431 #else
432 #include <BRepAlgo.hxx>
433 #endif
434
435 //=======================================================================
436 //function : IsValid2d
437 //purpose  : 
438 //=======================================================================
439 static Standard_Boolean IsValid2d (const TopTools_ListOfShape& theArgs,
440                                    const TopoDS_Shape& theResult,
441                                    const Standard_Boolean closedSolid,
442                                    const Standard_Boolean GeomCtrl)
443 {
444   OSD_Environment check ("DONT_SWITCH_IS_VALID") ;
445   TCollection_AsciiString checkValid = check.Value();
446   Standard_Boolean ToCheck = Standard_True;
447   if (!checkValid.IsEmpty()) {
448 #ifdef DEB
449     cout <<"DONT_SWITCH_IS_VALID positionnee a :"<<checkValid.ToCString()<<endl;
450 #endif
451     if ( checkValid=="true" || checkValid=="TRUE" ) {
452       ToCheck= Standard_False;
453     } 
454   } else {
455 #ifdef DEB
456     cout <<"DONT_SWITCH_IS_VALID non positionne"<<endl;
457 #endif
458   }
459   Standard_Boolean IsValid = Standard_True;
460   ////////////if (ToCheck) 
461     ////////////IsValid = BRepAlgo::IsValid(theArgs,theResult,closedSolid,GeomCtrl) ;
462   if (ToCheck) {
463 #if defined(BRepAlgoAPI_def01)
464     cout <<"BRepAlgoAPI::IsValid()"<<endl;
465     IsValid = BRepAlgoAPI::IsValid(theArgs,theResult,closedSolid,GeomCtrl) ;
466 #else
467     cout <<"BRepAlgo::IsValid()"<<endl;
468     IsValid = BRepAlgo::IsValid(theArgs,theResult,closedSolid,GeomCtrl) ;
469 #endif
470   }
471   return IsValid;
472   
473 }
474
475 //===============================================================================
476 // TDraft : test draft, uses AIS2D Viewer
477 // Solid Face Plane Angle  Reverse
478 //=============================================================================== 
479 static Standard_Integer TDraft2d (Draw_Interpretor& di, Standard_Integer argc, char** argv)
480
481   if (argc < 5) return 1;
482 // argv[1] - TopoDS_Shape Solid
483 // argv[2] - TopoDS_Shape Face
484 // argv[3] - TopoDS_Shape Plane
485 // argv[4] - Standard_Real Angle
486 // argv[5] - Standard_Integer Reverse
487
488 //  sprintf(prefix, argv[1]);
489   Standard_Real anAngle = 0;
490   Standard_Boolean Rev = Standard_False;
491   Standard_Integer rev = 0;
492   TopoDS_Shape Solid  = GetShapeFromName2d(argv[1]);
493   TopoDS_Shape face   = GetShapeFromName2d(argv[2]);
494   TopoDS_Face Face    = TopoDS::Face(face);
495   TopoDS_Shape Plane  = GetShapeFromName2d(argv[3]);
496   if (Plane.IsNull ()) {
497     di << "TEST : Plane is NULL" << "\n";
498     return 1;
499   }
500   anAngle = atof(argv[4]);
501   anAngle = 2*M_PI * anAngle / 360.0;
502   gp_Pln aPln;
503   Handle(Geom_Surface) aSurf;
504 //  AIS2D_KindOfSurface aSurfType;
505 //  Standard_Real Offset;
506   gp_Dir aDir;
507   if(argc > 4) { // == 5
508     rev = atoi(argv[5]);
509     Rev = (rev)? Standard_True : Standard_False;
510   } 
511     
512   TopoDS_Face face2 = TopoDS::Face(Plane);
513 //  if (!AIS2D::GetPlaneFromFace(face2, aPln, aSurf, aSurfType, Offset))
514 //  {
515 //    cout << "TEST : Can't find plane" << endl;
516 //    return 1;
517 //  }
518
519   aDir = aPln.Axis().Direction();
520   if (!aPln.Direct()) 
521     aDir.Reverse();
522   if (Plane.Orientation() == TopAbs_REVERSED) 
523     aDir.Reverse();
524   di << "TEST : gp::Resolution() = " << gp::Resolution() << "\n";
525
526   BRepOffsetAPI_DraftAngle Draft (Solid);
527
528   if(Abs(anAngle)< Precision::Angular()) {
529     di << "TEST : NULL angle" << "\n";
530     return 1;}
531
532   if(Rev) anAngle = - anAngle;
533   Draft.Add (Face, aDir, anAngle, aPln);
534   Draft.Build ();
535   if (!Draft.IsDone())  {
536     di << "TEST : Draft Not DONE " << "\n";
537     return 1;
538   }
539   TopTools_ListOfShape Larg;
540   Larg.Append(Solid);
541   if (!IsValid2d(Larg,Draft.Shape(),Standard_True,Standard_False))
542   {
543     di << "TEST : DesignAlgo returns Not valid" << "\n";
544     return 1;
545   }
546
547   Handle(AIS2D_InteractiveContext) Ctx = QAViewer2dTest::GetAIS2DContext();
548   Handle(AIS2D_ProjShape) ais = new AIS2D_ProjShape();
549   ais->Add(Draft.Shape());
550
551   if (!ais.IsNull())
552   {
553 //    ais->SetColor(DEFAULT_COLOR);
554 //    ais->SetMaterial(DEFAULT_MATERIAL);
555     // Display the AIS2D_ProjShape without redraw
556     Ctx->Display(ais, Standard_False);
557
558     char *Name = "draft1";
559     Standard_Boolean IsBound = GetMapOfAIS2D().IsBound2(Name);
560     if (IsBound)
561     {
562       Handle(AIS2D_InteractiveObject) an_object = GetMapOfAIS2D().Find2(Name);
563 //      Ctx->Remove(an_object, Standard_True);
564       GetMapOfAIS2D().UnBind2(Name);
565     }
566     GetMapOfAIS2D().Bind(ais, Name);
567   }
568   Ctx->Display(ais, Standard_True);
569   return 0;
570 }
571
572 //============================================================================
573 //  MyCommands
574 //============================================================================
575 void QAViewer2dTest::MyCommands (Draw_Interpretor& theCommands)
576 {
577   DrawTrSurf::BasicCommands(theCommands);
578   char* group = "2D AIS Viewer - Check Features Operations commands";
579
580   theCommands.Add("Draft2d",
581                   "Draft2d Solid Face Plane Angle Reverse",
582                   __FILE__, TDraft2d, group); //Draft_Modification
583 }
584
585 //============================================================================
586 // All 2D AIS Viewer Commands
587 //============================================================================
588 void QAViewer2dTest::Commands (Draw_Interpretor& theCommands)
589 {
590   QAViewer2dTest::GeneralCommands(theCommands);
591   QAViewer2dTest::ViewerCommands (theCommands);
592   QAViewer2dTest::DisplayCommands(theCommands);
593   QAViewer2dTest::ObjectCommands (theCommands);
594 }
595 #endif