0030507: Visualization - introduce AIS_ViewController
[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 #include <AIS_PlaneTrihedron.hxx>
61 #include <TopoDS_Face.hxx>
62 #include <TopExp_Explorer.hxx>
63 #include <TopoDS_Edge.hxx>
64 #include <BRepAdaptor_Curve.hxx>
65 #include <GC_MakePlane.hxx>
66
67 static Standard_Integer OCC159bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
68 {
69   if ( argc != 2) {
70     di << "ERROR : Usage : " << argv[0] << " Doc\n";
71     return 1;
72   }
73   
74   Handle(TDocStd_Document) D;    
75   if (!DDocStd::GetDocument(argv[1],D)) return 1; 
76
77   Standard_Integer DocRefCount1 = D->GetRefCount();
78   di << "DocRefCount1 = " << DocRefCount1 << "\n";
79
80   Handle(TDocStd_Owner) Owner;
81   if (!D->Main().Root().FindAttribute(TDocStd_Owner::GetID(),Owner)) return 1; 
82
83   Handle(TDocStd_Document) OwnerD1 = Owner->GetDocument();    
84   if (OwnerD1.IsNull()) {
85     di << "DocOwner1 = NULL\n";
86   } else {
87     di << "DocOwner1 = NOTNULL\n";
88   }
89  
90   Handle(TDocStd_Application) A = DDocStd::GetApplication();
91   A->Close(D); 
92
93   if (Handle(Draw_Drawable3D) DD = Draw::GetExisting (argv[1]))
94   {
95     dout.RemoveDrawable (DD);
96   }
97
98   Handle(TDocStd_Document) OwnerD2 = Owner->GetDocument();    
99   if (OwnerD2.IsNull()) {
100     di << "DocOwner2 = NULL\n";
101   } else {
102     di << "DocOwner2 = NOTNULL\n";
103   }
104
105   Standard_Integer DocRefCount2 = D->GetRefCount();
106   di << "DocRefCount2 = " << DocRefCount2 << "\n";
107
108   return 0;
109 }
110
111 static Standard_Integer OCC145bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
112 {
113   if ( argc != 3) {
114     di << "ERROR : Usage : " << argv[0] << " Shape MaxNbr\n";
115     return 1;
116   }
117
118   TCollection_AsciiString aFileName = argv[1];
119   Standard_Integer aMaxNbr = Draw::Atoi(argv[2]);
120
121   BRep_Builder aBld;
122   TopoDS_Shape aShape;
123
124   if (!BRepTools::Read(aShape, aFileName.ToCString(), aBld)) {
125     di << "ERROR :Could not read a shape!!!\n";
126     return 1;
127   }
128
129   Standard_Integer i;
130   TopoDS_Wire      aWire   = TopoDS::Wire(aShape);
131   
132   for (i = 1; i <= aMaxNbr; i++) {
133     BRepBuilderAPI_MakeFace aMF(aWire);
134     if (!aMF.IsDone()) {
135       di << "ERROR : Could not make a face\n";
136       return 1;
137     }
138   }
139
140   return 0;
141 }
142
143 static Standard_Integer OCC73_SelectionMode (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
144 {
145   if ( argc < 3) {
146     di << "ERROR : Usage : " << argv[0] << " DOC entry [SelectionMode]\n";
147     return 1;
148   }
149
150   Handle(TDocStd_Document) D;
151   //cout << "OCC73_SelectionMode  1" << endl;
152   if (!DDocStd::GetDocument(argv[1],D)) return 1;  
153   TDF_Label L;
154   //cout << "OCC73_SelectionMode  2" << endl;
155   if (!DDF::FindLabel(D->GetData(),argv[2],L)) return 1;  
156
157   Handle(TPrsStd_AISViewer) viewer;
158   //cout << "OCC73_SelectionMode  3" << endl;
159   if( !TPrsStd_AISViewer::Find(L, viewer) ) return 1;  
160
161   Handle(TPrsStd_AISPresentation) prs;
162   //cout << "OCC73_SelectionMode  4" << endl;
163   if(L.FindAttribute( TPrsStd_AISPresentation::GetID(), prs) ) {   
164     if( argc == 4 ) {
165       prs->SetSelectionMode((Standard_Integer)Draw::Atoi(argv[3]));
166       TPrsStd_AISViewer::Update(L);
167     }
168     else {
169       Standard_Integer SelectionMode = prs->SelectionMode();
170       //cout << "SelectionMode = " << SelectionMode << endl;
171       di<<SelectionMode;
172     }
173   }
174   //cout << "OCC73_SelectionMode  5" << endl;
175
176   return 0;
177 }
178
179 static Standard_Integer OCC10bug (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
180 {
181   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
182   if(aContext.IsNull()) { 
183     di << "use 'vinit' command before " << argv[0] << "\n";
184     return 1;
185   }
186
187   if(argc != 4) {
188     di << "Usage : " << argv[0] << " name plane Length\n";
189     return 1;
190   }
191
192   TopoDS_Shape S = DBRep::Get( argv[2] );
193   if ( S.IsNull() ) {
194     di << "Shape is empty\n";
195     return 1;
196   }
197
198   TCollection_AsciiString name(argv[1]);
199   Standard_Real Length = Draw::Atof(argv[3]);
200   
201   // Construction de l'AIS_PlaneTrihedron
202   Handle(AIS_PlaneTrihedron) theAISPlaneTri;
203
204   Standard_Boolean IsBound = GetMapOfAIS().IsBound2(name);
205   if (IsBound) {
206     // on recupere la shape dans la map des objets displayes
207     Handle(AIS_InteractiveObject) aShape = GetMapOfAIS().Find2(name);
208       
209     // On verifie que l'AIS InteraciveObject est bien 
210     // un AIS_PlaneTrihedron
211     if (aShape->Type()==AIS_KOI_Datum && aShape->Signature()==4) {
212       // On downcast aShape de AIS_InteractiveObject a AIS_PlaneTrihedron
213       theAISPlaneTri = Handle(AIS_PlaneTrihedron)::DownCast (aShape);
214
215       theAISPlaneTri->SetLength(Length);
216   
217       aContext->Redisplay(theAISPlaneTri, Standard_False);
218       aContext->UpdateCurrentViewer();
219     }
220   } else {
221     TopoDS_Face  FaceB=TopoDS::Face(S);
222   
223     // Construction du Plane
224     // recuperation des edges des faces.
225     TopExp_Explorer FaceExpB(FaceB,TopAbs_EDGE);
226   
227     TopoDS_Edge EdgeB=TopoDS::Edge(FaceExpB.Current() );
228     // declarations 
229     gp_Pnt A,B,C;
230   
231     // si il y a plusieurs edges
232     if (FaceExpB.More() ) {
233       FaceExpB.Next();
234       TopoDS_Edge EdgeC=TopoDS::Edge(FaceExpB.Current() );
235       BRepAdaptor_Curve theCurveB(EdgeB);
236       BRepAdaptor_Curve theCurveC(EdgeC);
237       A=theCurveC.Value(0.1);
238       B=theCurveC.Value(0.9);
239       C=theCurveB.Value(0.5);
240     }
241     else {
242       // FaceB a 1 unique edge courbe
243       BRepAdaptor_Curve theCurveB(EdgeB);
244       A=theCurveB.Value(0.1);
245       B=theCurveB.Value(0.9);
246       C=theCurveB.Value(0.5);
247     }
248     // Construction du Geom_Plane
249     GC_MakePlane MkPlane(A,B,C);
250     Handle(Geom_Plane) theGeomPlane=MkPlane.Value();
251     
252     // on le display & bind
253     theAISPlaneTri= new AIS_PlaneTrihedron(theGeomPlane );
254     
255     theAISPlaneTri->SetLength(Length);
256     
257     GetMapOfAIS().Bind ( theAISPlaneTri, name);
258     aContext->Display (theAISPlaneTri, Standard_True);
259   }
260
261   Standard_Real getLength = theAISPlaneTri->GetLength();
262   di << "Length = " << Length << "\n";
263   di << "getLength = " << getLength << "\n";
264
265   if (getLength == Length) {
266     di << "OCC10: OK\n";
267   } else {
268     di << "OCC10: ERROR\n";
269   }
270
271   return 0;
272 }
273
274 static Standard_Integer OCC74bug_set (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
275 {
276   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
277   if(aContext.IsNull()) { 
278     di << argv[0] << "ERROR : use 'vinit' command before \n";
279     return 1;
280   }
281
282   if ( argc != 3) {
283     di << "ERROR : Usage : " << argv[0] << " shape mode; set selection mode\n";
284     return 1;
285   }
286   
287   Standard_Boolean updateviewer = Standard_True;
288
289   ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
290   
291   TCollection_AsciiString aName(argv[1]);
292   Handle(AIS_InteractiveObject) AISObj;
293
294   Standard_Integer SelectMode = Draw::Atoi(argv[2]);
295   if (!aMap.Find2 (aName, AISObj)
296     || AISObj.IsNull())
297   {
298     di << "Use 'vdisplay' before\n";
299     return 1;
300   }
301
302   aContext->Erase(AISObj, updateviewer);
303   aContext->UpdateCurrentViewer();
304   aContext->SetAutoActivateSelection (Standard_False);
305   aContext->Display(AISObj, updateviewer);
306   aContext->Activate (AISObj, SelectMode);
307   aContext->UpdateCurrentViewer();
308   return 0;
309 }
310
311 static Standard_Integer OCC74bug_get (Draw_Interpretor& di, Standard_Integer argc, const char ** argv)
312 {
313   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
314   if(aContext.IsNull()) { 
315     di << argv[0] << "ERROR : use 'vinit' command before \n";
316     return 1;
317   }
318
319   if ( argc != 2) {
320     di << "ERROR : Usage : " << argv[0] << " shape; get selection mode\n";
321     return 1;
322   }
323
324   ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
325   
326   TCollection_AsciiString aName(argv[1]);
327   Handle(AIS_InteractiveObject) AISObj;
328   if (!aMap.Find2(aName, AISObj)
329    || AISObj.IsNull())
330   {
331     di << "Use 'vdisplay' before\n";
332     return 1;
333   }
334
335   TColStd_ListOfInteger anActivatedModes;
336   aContext->ActivatedModes (AISObj, anActivatedModes);
337   Standard_Integer aMode = anActivatedModes.IsEmpty() ? -1 : anActivatedModes.Last();
338   di << aMode << "\n";
339   return 0;
340 }
341
342 #include <BRepPrimAPI_MakeBox.hxx>
343 #include <TDF_Data.hxx>
344 #include <TNaming_Builder.hxx>
345 #include <TNaming_NamedShape.hxx>
346 #include <AIS_InteractiveObject.hxx>
347
348 static Standard_Integer OCC361bug (Draw_Interpretor& di, Standard_Integer nb, const char ** a)
349 {
350   if ( nb != 2) {
351     di << "ERROR : Usage : " << a[0] << " Doc\n";
352     di << "-1\n";
353     return -1;
354   }
355
356   Handle(TDocStd_Document) D;    
357   if (!DDocStd::GetDocument(a[1],D)) {
358     di << "-2\n";
359     return 1;  
360   }
361
362   BRepPrimAPI_MakeBox aBox(gp_Pnt(0, 0, 0), 100, 100, 100);
363   TopoDS_Shape aTBox = aBox.Shape();
364   aTBox.Orientation(TopAbs_FORWARD);
365
366   TDF_Label aTestLabel = D->Main();
367   
368   TNaming_Builder aBuilder(aTestLabel);
369   aBuilder.Generated(aTBox);
370     
371   TopoDS_Shape aTBox1 = aTBox;
372   aTBox1.Orientation(TopAbs_REVERSED);
373   aTestLabel.ForgetAllAttributes();
374
375   TNaming_Builder aBuilder2(aTestLabel);
376   aBuilder2.Generated( aTBox1);
377
378   aTBox = aBuilder2.NamedShape()->Get();
379   if(aTBox.Orientation() != TopAbs_REVERSED) {
380     di << "1\n";
381   } else {
382     di << "0\n";
383   }
384   return 0;
385 }
386
387 #include <Graphic3d_Texture2Dmanual.hxx>
388 #include <Image_AlienPixMap.hxx>
389 #include <OSD_OpenFile.hxx>
390 #include <Prs3d_ShadingAspect.hxx>
391 #include <Standard_ArrayStreamBuffer.hxx>
392 //=======================================================================
393 //function : OCC30182
394 //purpose  : Testing different interfaces of Image_AlienPixMap::Load()
395 //=======================================================================
396 static Standard_Integer OCC30182 (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
397 {
398   if (ViewerTest::CurrentView().IsNull())
399   {
400     std::cout << "Error: no active view\n";
401     return 1;
402   }
403
404   TCollection_AsciiString aPrsName, anImgPath;
405   Standard_Integer anOffset = 0;
406   Standard_Integer aSrc = 0; // 0 - file name, 1 - file stream, 2 - memory buffer
407   for (Standard_Integer anArgIter = 1; anArgIter < theNbArgs; ++anArgIter)
408   {
409     TCollection_AsciiString anArg (theArgVec[anArgIter]);
410     anArg.LowerCase();
411     if (anArg == "-offset"
412      && anArgIter + 1 < theNbArgs)
413     {
414       anOffset = Draw::Atoi (theArgVec[++anArgIter]);
415     }
416     else if (anArg == "-filename")
417     {
418       aSrc = 0;
419     }
420     else if (anArg == "-stream")
421     {
422       aSrc = 1;
423     }
424     else if (anArg == "-mem"
425           || anArg == "-memory")
426     {
427       aSrc = 2;
428     }
429     else if (aPrsName.IsEmpty())
430     {
431       aPrsName = theArgVec[anArgIter];
432     }
433     else if (anImgPath.IsEmpty())
434     {
435       anImgPath = theArgVec[anArgIter];
436     }
437     else
438     {
439       std::cout << "Syntax error at '" << anArg << "'\n";
440       return 1;
441     }
442   }
443   if (anImgPath.IsEmpty())
444   {
445     std::cout << "Syntax error: wrong number of arguments\n";
446     return 1;
447   }
448
449   Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap();
450   if (aSrc == 0)
451   {
452     if (!anImage->Load (anImgPath))
453     {
454       return 0;
455     }
456   }
457   else
458   {
459     std::ifstream aFile;
460     OSD_OpenStream (aFile, anImgPath.ToCString(), std::ios::in | std::ios::binary);
461     if (!aFile.is_open())
462     {
463       std::cout << "Syntax error: image file '" << anImgPath << "' cannot be found\n";
464       return 1;
465     }
466     if (anOffset != 0)
467     {
468       aFile.seekg (anOffset);
469     }
470
471     if (aSrc == 2)
472     {
473       aFile.seekg (0, std::ios::end);
474       Standard_Integer aLen = (Standard_Integer )aFile.tellg() - anOffset;
475       aFile.seekg (anOffset);
476       if (aLen <= 0)
477       {
478         std::cout << "Syntax error: wrong offset\n";
479         return 1;
480       }
481       NCollection_Array1<Standard_Byte> aBuff (1, aLen);
482       if (!aFile.read ((char* )&aBuff.ChangeFirst(), aBuff.Size()))
483       {
484         std::cout << "Error: unable to read file\n";
485         return 1;
486       }
487       aFile.close();
488       if (!anImage->Load (&aBuff.ChangeFirst(), aBuff.Size(), anImgPath))
489       {
490         return 0;
491       }
492     }
493     else
494     {
495       if (!anImage->Load (aFile, anImgPath))
496       {
497         return 0;
498       }
499     }
500   }
501
502   TopoDS_Shape aShape = BRepPrimAPI_MakeBox (100.0 * anImage->Ratio(), 100.0, 1.0).Shape();
503   Handle(AIS_Shape) aPrs = new AIS_Shape (aShape);
504   aPrs->SetDisplayMode (AIS_Shaded);
505   aPrs->Attributes()->SetupOwnShadingAspect();
506   const Handle(Graphic3d_AspectFillArea3d)& anAspect = aPrs->Attributes()->ShadingAspect()->Aspect();
507   anAspect->SetShadingModel (Graphic3d_TOSM_UNLIT);
508   anAspect->SetTextureMapOn (true);
509   anAspect->SetTextureMap (new Graphic3d_Texture2Dmanual (anImage));
510   if (anImage->IsTopDown())
511   {
512     anAspect->TextureMap()->GetParams()->SetTranslation(Graphic3d_Vec2 (0.0f, -1.0f));
513     anAspect->TextureMap()->GetParams()->SetScale      (Graphic3d_Vec2 (1.0f, -1.0f));
514   }
515
516   ViewerTest::Display (aPrsName, aPrs, true, true);
517   return 0;
518 }
519
520 void QABugs::Commands_1(Draw_Interpretor& theCommands) {
521   const char *group = "QABugs";
522
523   theCommands.Add ("OCC159", "OCC159 Doc", __FILE__, OCC159bug, group);
524   theCommands.Add ("OCC145", "OCC145 Shape MaxNbr", __FILE__, OCC145bug, group);
525
526   theCommands.Add ("OCC73_SelectionMode", "OCC73_SelectionMode DOC entry [SelectionMode]", __FILE__, OCC73_SelectionMode, group);
527
528   theCommands.Add ("OCC10", "OCC10 Shape MaxNbr", __FILE__, OCC10bug, group);
529
530   theCommands.Add ("OCC74_set", "OCC74_set shape mode;   set selection mode", __FILE__, OCC74bug_set, group);
531   theCommands.Add ("OCC74_get", "OCC74_get shape;   get selection mode", __FILE__, OCC74bug_get, group);
532
533   theCommands.Add ("OCC361", "OCC361 Doc ", __FILE__, OCC361bug, group);
534   theCommands.Add ("OCC30182",
535                    "OCC30182 name image [-offset Start] [-fileName] [-stream] [-memory]\n"
536                    "Decodes image either by passing file name, file stream or memory stream", __FILE__, OCC30182, group);
537   return;
538 }