0028162: Draw Harness - eliminate usage of deprecated Local Context
[occt.git] / src / QABugs / QABugs_1.cxx
1 // Created on: 2002-05-21
2 // Created by: QA Admin
3 // Copyright (c) 2002-2014 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 <QABugs.hxx>
17
18 #include <Draw_Interpretor.hxx>
19 #include <DBRep.hxx>
20 #include <DrawTrSurf.hxx>
21 #include <AIS_InteractiveContext.hxx>
22 #include <ViewerTest.hxx>
23 #include <AIS_Shape.hxx>
24 #include <TopoDS_Shape.hxx>
25
26 #include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
27 #include <TColStd_ListIteratorOfListOfInteger.hxx>
28 #include <TColStd_MapOfInteger.hxx>
29
30 #include <TDocStd_Document.hxx>
31 #include <TDocStd_Application.hxx>
32 #include <DDocStd.hxx>
33 #include <TDocStd_Owner.hxx>
34 #include <TDF_Label.hxx>
35 #include <DDF.hxx>
36 #include <TPrsStd_AISViewer.hxx>
37 #include <TPrsStd_AISPresentation.hxx>
38
39 #include <Draw_Viewer.hxx>
40 #include <Draw.hxx>
41
42 #ifndef _WIN32
43 extern Draw_Viewer dout;
44 #else
45 Standard_IMPORT Draw_Viewer dout;
46 #endif
47
48 #include <BRep_Builder.hxx>
49 #include <BRepTools.hxx>
50 #include <TopoDS_Wire.hxx>
51 #include <BRepBuilderAPI_MakeFace.hxx>
52 #include <TopoDS.hxx>
53
54 #if ! defined(_WIN32)
55 extern ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
56 #else
57 Standard_EXPORT ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS();
58 #endif
59
60 static TColStd_MapOfInteger theactivatedmodes(8);
61
62 #include <AIS_PlaneTrihedron.hxx>
63 #include <TopoDS_Face.hxx>
64 #include <TopExp_Explorer.hxx>
65 #include <TopoDS_Edge.hxx>
66 #include <BRepAdaptor_Curve.hxx>
67 #include <GC_MakePlane.hxx>
68
69 Standard_DISABLE_DEPRECATION_WARNINGS
70 static Standard_Integer OCC328bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
71 {
72   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
73   if(aContext.IsNull()) { 
74     di << argv[0] << "ERROR : use 'vinit' command before \n";
75     return 1;
76   }
77
78   if ( argc != 3) {
79     di << "ERROR : Usage : " << argv[0] << " shape mode\n";
80     return 1;
81   }
82   
83   Standard_Integer ChoosingMode = -1;
84   if ( strcmp (argv [2], "VERTEX") == 0 ) {
85     ChoosingMode = 1;
86   }
87   if ( strcmp (argv [2], "EDGE") == 0 ) {
88     ChoosingMode = 2;
89   }
90   if ( strcmp (argv [2], "WIRE") == 0 ) {
91     ChoosingMode = 3;
92   }
93   if ( strcmp (argv [2], "FACE") == 0 ) {
94     ChoosingMode = 4;
95   }
96   if ( strcmp (argv [2], "SHELL") == 0 ) {
97     ChoosingMode = 5;
98   }
99   if ( strcmp (argv [2], "SOLID") == 0 ) {
100     ChoosingMode = 6;
101   }
102   if ( strcmp (argv [2], "COMPOUND") == 0 ) {
103     ChoosingMode = 7;
104   }
105   if ( ChoosingMode == -1 ) {
106     di << "ERROR : " << argv[1] << " : vrong value of a mode\n";
107     return 1;
108   }
109
110   Standard_Boolean updateviewer = Standard_True;
111
112   ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
113   
114   TCollection_AsciiString aName(argv[1]);
115   Handle(AIS_InteractiveObject) AISObj;
116   
117   if(!aMap.IsBound2(aName)) {
118     di << "Use 'vdisplay' before\n";
119     return 1;
120   } else {
121     AISObj = Handle(AIS_InteractiveObject)::DownCast(aMap.Find2(aName));
122     if(AISObj.IsNull()){
123       di << argv[1] << " : No interactive object\n";
124       return 1;
125     } 
126
127     if (!aContext->HasOpenedContext()) {
128       aContext->OpenLocalContext();
129     }
130
131     if(!theactivatedmodes.Contains(ChoosingMode)) {
132       aContext->ActivateStandardMode(AIS_Shape::SelectionType(ChoosingMode));
133       theactivatedmodes.Add(ChoosingMode);
134     }
135     aContext->Erase(AISObj, updateviewer);
136     aContext->UpdateCurrentViewer();
137
138     aContext->Display(AISObj, updateviewer);
139     aContext->UpdateCurrentViewer();
140
141     const TColStd_ListOfInteger& aList = aContext->ActivatedStandardModes();
142     Standard_Integer SelectMode;
143     TCollection_AsciiString SelectModeString;
144     TColStd_ListIteratorOfListOfInteger itr(aList);
145     for (; itr.More(); itr.Next()) {
146       SelectMode = itr.Value();
147       //cout << "SelectMode = " << SelectMode << endl;
148
149       switch (SelectMode)
150         {
151         case 1:
152           SelectModeString.Copy("VERTEX");
153           break;
154         case 2:
155           SelectModeString.Copy("EDGE");
156           break;
157         case 3:
158           SelectModeString.Copy("WIRE");
159           break;
160         case 4:
161           SelectModeString.Copy("FACE");
162           break;
163         case 5:
164           SelectModeString.Copy("SHELL");
165           break;
166         case 6:
167           SelectModeString.Copy("SOLID");
168           break;
169         case 7:
170           SelectModeString.Copy("COMPOUND");
171           break;
172         default:
173           SelectModeString.Copy("UNKNOWN");
174         }
175       di << "SelectMode = " << SelectModeString.ToCString() << "\n";
176
177     }
178   }
179
180   return 0;
181 }
182 Standard_ENABLE_DEPRECATION_WARNINGS
183
184 static Standard_Integer OCC159bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
185 {
186   if ( argc != 2) {
187     di << "ERROR : Usage : " << argv[0] << " Doc\n";
188     return 1;
189   }
190   
191   Handle(TDocStd_Document) D;    
192   if (!DDocStd::GetDocument(argv[1],D)) return 1; 
193
194   Standard_Integer DocRefCount1 = D->GetRefCount();
195   di << "DocRefCount1 = " << DocRefCount1 << "\n";
196
197   Handle(TDocStd_Owner) Owner;
198   if (!D->Main().Root().FindAttribute(TDocStd_Owner::GetID(),Owner)) return 1; 
199
200   Handle(TDocStd_Document) OwnerD1 = Owner->GetDocument();    
201   if (OwnerD1.IsNull()) {
202     di << "DocOwner1 = NULL\n";
203   } else {
204     di << "DocOwner1 = NOTNULL\n";
205   }
206  
207   Handle(TDocStd_Application) A = DDocStd::GetApplication();
208   A->Close(D); 
209
210   Handle(Draw_Drawable3D) DD = Draw::Get(argv[1],Standard_False);
211   dout.RemoveDrawable (DD);
212
213   Handle(TDocStd_Document) OwnerD2 = Owner->GetDocument();    
214   if (OwnerD2.IsNull()) {
215     di << "DocOwner2 = NULL\n";
216   } else {
217     di << "DocOwner2 = NOTNULL\n";
218   }
219
220   Standard_Integer DocRefCount2 = D->GetRefCount();
221   di << "DocRefCount2 = " << DocRefCount2 << "\n";
222
223   return 0;
224 }
225
226 static Standard_Integer OCC145bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
227 {
228   if ( argc != 3) {
229     di << "ERROR : Usage : " << argv[0] << " Shape MaxNbr\n";
230     return 1;
231   }
232
233   TCollection_AsciiString aFileName = argv[1];
234   Standard_Integer aMaxNbr = Draw::Atoi(argv[2]);
235
236   BRep_Builder aBld;
237   TopoDS_Shape aShape;
238
239   if (!BRepTools::Read(aShape, aFileName.ToCString(), aBld)) {
240     di << "ERROR :Could not read a shape!!!\n";
241     return 1;
242   }
243
244   Standard_Integer i;
245   TopoDS_Wire      aWire   = TopoDS::Wire(aShape);
246   
247   for (i = 1; i <= aMaxNbr; i++) {
248     BRepBuilderAPI_MakeFace aMF(aWire);
249     if (!aMF.IsDone()) {
250       di << "ERROR : Could not make a face\n";
251       return 1;
252     }
253   }
254
255   return 0;
256 }
257
258 static Standard_Integer OCC73_SelectionMode (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
259 {
260   if ( argc < 3) {
261     di << "ERROR : Usage : " << argv[0] << " DOC entry [SelectionMode]\n";
262     return 1;
263   }
264
265   Handle(TDocStd_Document) D;
266   //cout << "OCC73_SelectionMode  1" << endl;
267   if (!DDocStd::GetDocument(argv[1],D)) return 1;  
268   TDF_Label L;
269   //cout << "OCC73_SelectionMode  2" << endl;
270   if (!DDF::FindLabel(D->GetData(),argv[2],L)) return 1;  
271
272   Handle(TPrsStd_AISViewer) viewer;
273   //cout << "OCC73_SelectionMode  3" << endl;
274   if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  
275
276   Handle(TPrsStd_AISPresentation) prs;
277   //cout << "OCC73_SelectionMode  4" << endl;
278   if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
279     if( argc == 4 ) {
280       prs->SetSelectionMode((Standard_Integer)Draw::Atoi(argv[3]));
281       TPrsStd_AISViewer::Update(L);
282     }
283     else {
284       Standard_Integer SelectionMode = prs->SelectionMode();
285       //cout << "SelectionMode = " << SelectionMode << endl;
286       di<<SelectionMode;
287     }
288   }
289   //cout << "OCC73_SelectionMode  5" << endl;
290
291   return 0;
292 }
293
294 static Standard_Integer OCC10bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
295 {
296   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
297   if(aContext.IsNull()) { 
298     di << "use 'vinit' command before " << argv[0] << "\n";
299     return 1;
300   }
301
302   if(argc != 4) {
303     di << "Usage : " << argv[0] << " name plane Length\n";
304     return 1;
305   }
306
307   TopoDS_Shape S = DBRep::Get( argv[2] );
308   if ( S.IsNull() ) {
309     di << "Shape is empty\n";
310     return 1;
311   }
312
313   TCollection_AsciiString name(argv[1]);
314   Standard_Real Length = Draw::Atof(argv[3]);
315   
316   // Construction de l'AIS_PlaneTrihedron
317   Handle(AIS_PlaneTrihedron) theAISPlaneTri;
318
319   Standard_Boolean IsBound = GetMapOfAIS().IsBound2(name);
320   if (IsBound) {
321     // on recupere la shape dans la map des objets displayes
322     Handle(AIS_InteractiveObject) aShape = 
323       Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(name));
324       
325     // On verifie que l'AIS InteraciveObject est bien 
326     // un AIS_PlaneTrihedron
327     if (aShape->Type()==AIS_KOI_Datum && aShape->Signature()==4) {
328       // On downcast aShape de AIS_InteractiveObject a AIS_PlaneTrihedron
329       theAISPlaneTri = Handle(AIS_PlaneTrihedron)::DownCast (aShape);
330
331       theAISPlaneTri->SetLength(Length);
332   
333       aContext->Redisplay(theAISPlaneTri, Standard_False);
334       aContext->UpdateCurrentViewer();
335     }
336   } else {
337     TopoDS_Face  FaceB=TopoDS::Face(S);
338   
339     // Construction du Plane
340     // recuperation des edges des faces.
341     TopExp_Explorer FaceExpB(FaceB,TopAbs_EDGE);
342   
343     TopoDS_Edge EdgeB=TopoDS::Edge(FaceExpB.Current() );
344     // declarations 
345     gp_Pnt A,B,C;
346   
347     // si il y a plusieurs edges
348     if (FaceExpB.More() ) {
349       FaceExpB.Next();
350       TopoDS_Edge EdgeC=TopoDS::Edge(FaceExpB.Current() );
351       BRepAdaptor_Curve theCurveB(EdgeB);
352       BRepAdaptor_Curve theCurveC(EdgeC);
353       A=theCurveC.Value(0.1);
354       B=theCurveC.Value(0.9);
355       C=theCurveB.Value(0.5);
356     }
357     else {
358       // FaceB a 1 unique edge courbe
359       BRepAdaptor_Curve theCurveB(EdgeB);
360       A=theCurveB.Value(0.1);
361       B=theCurveB.Value(0.9);
362       C=theCurveB.Value(0.5);
363     }
364     // Construction du Geom_Plane
365     GC_MakePlane MkPlane(A,B,C);
366     Handle(Geom_Plane) theGeomPlane=MkPlane.Value();
367     
368     // on le display & bind
369     theAISPlaneTri= new AIS_PlaneTrihedron(theGeomPlane );
370     
371     theAISPlaneTri->SetLength(Length);
372     
373     GetMapOfAIS().Bind ( theAISPlaneTri, name);
374     aContext->Display(theAISPlaneTri );
375   }
376
377   Standard_Real getLength = theAISPlaneTri->GetLength();
378   di << "Length = " << Length << "\n";
379   di << "getLength = " << getLength << "\n";
380
381   if (getLength == Length) {
382     di << "OCC10: OK\n";
383   } else {
384     di << "OCC10: ERROR\n";
385   }
386
387   return 0;
388 }
389
390 static Standard_Integer OCC74bug_set (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
391 {
392   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
393   if(aContext.IsNull()) { 
394     di << argv[0] << "ERROR : use 'vinit' command before \n";
395     return 1;
396   }
397
398   if ( argc != 3) {
399     di << "ERROR : Usage : " << argv[0] << " shape mode; set selection mode\n";
400     return 1;
401   }
402   
403   Standard_Boolean updateviewer = Standard_True;
404
405   ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
406   
407   TCollection_AsciiString aName(argv[1]);
408   Handle(AIS_InteractiveObject) AISObj;
409
410   Standard_Integer SelectMode = Draw::Atoi(argv[2]);
411   
412   if(!aMap.IsBound2(aName)) {
413     di << "Use 'vdisplay' before\n";
414     return 1;
415   } else {
416     AISObj = Handle(AIS_InteractiveObject)::DownCast(aMap.Find2(aName));
417     if(AISObj.IsNull()){
418       di << argv[1] << " : No interactive object\n";
419       return 1;
420     }
421     aContext->Erase(AISObj, updateviewer);
422     aContext->UpdateCurrentViewer();
423     aContext->SetAutoActivateSelection (Standard_False);
424     aContext->Display(AISObj, updateviewer);
425     aContext->Activate (AISObj, SelectMode);
426     aContext->UpdateCurrentViewer();
427   }
428   return 0;
429 }
430
431 static Standard_Integer OCC74bug_get (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
432 {
433   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
434   if(aContext.IsNull()) { 
435     di << argv[0] << "ERROR : use 'vinit' command before \n";
436     return 1;
437   }
438
439   if ( argc != 2) {
440     di << "ERROR : Usage : " << argv[0] << " shape; get selection mode\n";
441     return 1;
442   }
443
444   ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
445   
446   TCollection_AsciiString aName(argv[1]);
447   Handle(AIS_InteractiveObject) AISObj;
448
449   if(!aMap.IsBound2(aName)) {
450     di << "Use 'vdisplay' before\n";
451     return 1;
452   } else {
453     AISObj = Handle(AIS_InteractiveObject)::DownCast(aMap.Find2(aName));
454     if(AISObj.IsNull()){
455       di << argv[1] << " : No interactive object\n";
456       return 1;
457     } 
458     TColStd_ListOfInteger anActivatedModes;
459     aContext->ActivatedModes (AISObj, anActivatedModes);
460     Standard_Integer aMode = anActivatedModes.IsEmpty() ? -1 : anActivatedModes.Last();
461     di << aMode << "\n";
462   }
463
464   return 0;
465 }
466
467 #include <BRepPrimAPI_MakeBox.hxx>
468 #include <TDF_Data.hxx>
469 #include <TNaming_Builder.hxx>
470 #include <TNaming_NamedShape.hxx>
471 #include <AIS_InteractiveObject.hxx>
472
473 static Standard_Integer OCC361bug (Draw_Interpretor& di, Standard_Integer nb, const char ** a)
474 {
475   if ( nb != 2) {
476     di << "ERROR : Usage : " << a[0] << " Doc\n";
477     di << "-1\n";
478     return -1;
479   }
480
481   Handle(TDocStd_Document) D;    
482   if (!DDocStd::GetDocument(a[1],D)) {
483     di << "-2\n";
484     return 1;  
485   }
486
487   BRepPrimAPI_MakeBox aBox(gp_Pnt(0, 0, 0), 100, 100, 100);
488   TopoDS_Shape aTBox = aBox.Shape();
489   aTBox.Orientation(TopAbs_FORWARD);
490
491   TDF_Label aTestLabel = D->Main();
492   
493   TNaming_Builder aBuilder(aTestLabel);
494   aBuilder.Generated(aTBox);
495     
496   TopoDS_Shape aTBox1 = aTBox;
497   aTBox1.Orientation(TopAbs_REVERSED);
498   aTestLabel.ForgetAllAttributes();
499
500   TNaming_Builder aBuilder2(aTestLabel);
501   aBuilder2.Generated( aTBox1);
502
503   aTBox = aBuilder2.NamedShape()->Get();
504   if(aTBox.Orientation() != TopAbs_REVERSED) {
505     di << "1\n";
506   } else {
507     di << "0\n";
508   }
509   return 0;
510 }
511
512 void QABugs::Commands_1(Draw_Interpretor& theCommands) {
513   const char *group = "QABugs";
514
515   theCommands.Add ("OCC328", "OCC328 shape mode", __FILE__, OCC328bug, group);
516
517   theCommands.Add ("OCC159", "OCC159 Doc", __FILE__, OCC159bug, group);
518   theCommands.Add ("OCC145", "OCC145 Shape MaxNbr", __FILE__, OCC145bug, group);
519
520   theCommands.Add ("OCC73_SelectionMode", "OCC73_SelectionMode DOC entry [SelectionMode]", __FILE__, OCC73_SelectionMode, group);
521
522   theCommands.Add ("OCC10", "OCC10 Shape MaxNbr", __FILE__, OCC10bug, group);
523
524   theCommands.Add ("OCC74_set", "OCC74_set shape mode;   set selection mode", __FILE__, OCC74bug_set, group);
525   theCommands.Add ("OCC74_get", "OCC74_get shape;   get selection mode", __FILE__, OCC74bug_get, group);
526
527   theCommands.Add("OCC361", "OCC361 Doc ", __FILE__, OCC361bug, group);
528
529   return;
530 }