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