0025098: Visualization, Select3D_SensitiveCurve - fix computation of the depth
[occt.git] / src / ViewerTest / ViewerTest.cxx
1 // Created on: 1997-07-23
2 // Created by: Henri JEANNIN
3 // Copyright (c) 1997-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 // Modified by  Eric Gouthiere [sep-oct 98] -> add commands for display...
18 // Modified by  Robert Coublanc [nov 16-17-18 1998]
19 //             -split ViewerTest.cxx into 3 files : ViewerTest.cxx,
20 //                                                  ViewerTest_ObjectCommands.cxx
21 //                                                  ViewerTest_RelationCommands.cxx
22 //             -add Functions and commands for interactive selection of shapes and objects
23 //              in AIS Viewers. (PickShape(s), PickObject(s),
24
25 #include <Standard_Stream.hxx>
26
27 #include <ViewerTest.hxx>
28
29 #include <TopLoc_Location.hxx>
30 #include <TopTools_HArray1OfShape.hxx>
31 #include <TColStd_HArray1OfTransient.hxx>
32 #include <TColStd_HSequenceOfAsciiString.hxx>
33 #include <OSD_Timer.hxx>
34 #include <Geom_Axis2Placement.hxx>
35 #include <Geom_Axis1Placement.hxx>
36 #include <gp_Trsf.hxx>
37 #include <TopExp_Explorer.hxx>
38 #include <BRepAdaptor_Curve.hxx>
39 #include <StdSelect_ShapeTypeFilter.hxx>
40 #include <AIS.hxx>
41 #include <AIS_Drawer.hxx>
42 #include <AIS_ColoredShape.hxx>
43 #include <AIS_InteractiveObject.hxx>
44 #include <AIS_Trihedron.hxx>
45 #include <AIS_Axis.hxx>
46 #include <AIS_Relation.hxx>
47 #include <AIS_TypeFilter.hxx>
48 #include <AIS_SignatureFilter.hxx>
49 #include <AIS_LocalContext.hxx>
50 #include <AIS_ListOfInteractive.hxx>
51 #include <AIS_ListIteratorOfListOfInteractive.hxx>
52 #include <Aspect_InteriorStyle.hxx>
53 #include <Aspect_Window.hxx>
54 #include <Graphic3d_AspectFillArea3d.hxx>
55 #include <Graphic3d_AspectLine3d.hxx>
56 #include <Graphic3d_TextureRoot.hxx>
57 #include <Image_AlienPixMap.hxx>
58 #include <Prs3d_ShadingAspect.hxx>
59 #include <Prs3d_IsoAspect.hxx>
60 #include <Select3D_SensitiveWire.hxx>
61 #include <SelectMgr_EntityOwner.hxx>
62 #include <StdSelect_BRepOwner.hxx>
63 #include <StdSelect_ViewerSelector3d.hxx>
64 #include <Select3D_Projector.hxx>
65 #include <TopTools_MapOfShape.hxx>
66 #include <ViewerTest_AutoUpdater.hxx>
67
68 #include <stdio.h>
69
70 #include <Draw_Interpretor.hxx>
71 #include <TCollection_AsciiString.hxx>
72 #include <Draw_PluginMacro.hxx>
73
74 // avoid warnings on 'extern "C"' functions returning C++ classes
75 #ifdef WNT
76 #define _CRT_SECURE_NO_DEPRECATE
77 #pragma warning(4:4190)
78 #pragma warning (disable:4996)
79 #endif
80
81 #include <NIS_InteractiveContext.hxx>
82 #include <NIS_Triangulated.hxx>
83 extern int ViewerMainLoop(Standard_Integer argc, const char** argv);
84
85 #include <Quantity_Color.hxx>
86 #include <Quantity_NameOfColor.hxx>
87
88 #include <Graphic3d_NameOfMaterial.hxx>
89
90 #define DEFAULT_COLOR    Quantity_NOC_GOLDENROD
91 #define DEFAULT_MATERIAL Graphic3d_NOM_BRASS
92
93 //=======================================================================
94 //function : GetColorFromName
95 //purpose  : get the Quantity_NameOfColor from a string
96 //=======================================================================
97
98 Quantity_NameOfColor ViewerTest::GetColorFromName (const Standard_CString theName)
99 {
100   for (Standard_Integer anIter = Quantity_NOC_BLACK; anIter <= Quantity_NOC_WHITE; ++anIter)
101   {
102     Standard_CString aColorName = Quantity_Color::StringName (Quantity_NameOfColor (anIter));
103     if (strcasecmp (theName, aColorName) == 0)
104     {
105       return Quantity_NameOfColor (anIter);
106     }
107   }
108
109   return DEFAULT_COLOR;
110 }
111
112 //=======================================================================
113 //function : GetTypeNames
114 //purpose  :
115 //=======================================================================
116 static const char** GetTypeNames()
117 {
118   static const char* names[14] = {"Point","Axis","Trihedron","PlaneTrihedron", "Line","Circle","Plane",
119                           "Shape","ConnectedShape","MultiConn.Shape",
120                           "ConnectedInter.","MultiConn.",
121                           "Constraint","Dimension"};
122   static const char** ThePointer = names;
123   return ThePointer;
124 }
125
126 //=======================================================================
127 //function : GetTypeAndSignfromString
128 //purpose  :
129 //=======================================================================
130 void GetTypeAndSignfromString (const char* name,AIS_KindOfInteractive& TheType,Standard_Integer& TheSign)
131 {
132   const char ** thefullnames = GetTypeNames();
133   Standard_Integer index(-1);
134
135   for(Standard_Integer i=0;i<=13 && index==-1;i++)
136     if(!strcasecmp(name,thefullnames[i]))
137       index = i;
138
139   if(index ==-1){
140     TheType = AIS_KOI_None;
141     TheSign = -1;
142     return;
143   }
144
145   if(index<=6){
146     TheType = AIS_KOI_Datum;
147     TheSign = index+1;
148   }
149   else if (index <=9){
150     TheType = AIS_KOI_Shape;
151     TheSign = index-7;
152   }
153   else if(index<=11){
154     TheType = AIS_KOI_Object;
155     TheSign = index-10;
156   }
157   else{
158     TheType = AIS_KOI_Relation;
159     TheSign = index-12;
160   }
161
162 }
163
164
165
166 #include <string.h>
167 #include <Draw_Interpretor.hxx>
168 #include <Draw.hxx>
169 #include <Draw_Appli.hxx>
170 #include <DBRep.hxx>
171
172
173 #include <TCollection_AsciiString.hxx>
174 #include <V3d_Viewer.hxx>
175 #include <V3d_View.hxx>
176 #include <V3d.hxx>
177
178 #include <AIS_InteractiveContext.hxx>
179 #include <AIS_Shape.hxx>
180 #include <AIS_TexturedShape.hxx>
181 #include <AIS_DisplayMode.hxx>
182 #include <TColStd_MapOfInteger.hxx>
183 #include <AIS_MapOfInteractive.hxx>
184 #include <ViewerTest_DoubleMapOfInteractiveAndName.hxx>
185 #include <ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName.hxx>
186 #include <ViewerTest_EventManager.hxx>
187
188 #include <TopoDS_Solid.hxx>
189 #include <BRepTools.hxx>
190 #include <BRep_Builder.hxx>
191 #include <TopAbs_ShapeEnum.hxx>
192
193 #include <TopoDS.hxx>
194 #include <BRep_Tool.hxx>
195
196
197 #include <Draw_Window.hxx>
198 #include <AIS_ListIteratorOfListOfInteractive.hxx>
199 #include <AIS_ListOfInteractive.hxx>
200 #include <AIS_DisplayMode.hxx>
201 #include <TopTools_ListOfShape.hxx>
202 #include <BRepOffsetAPI_MakeThickSolid.hxx>
203 #include <BRepOffset.hxx>
204
205 //==============================================================================
206 //  VIEWER OBJECT MANAGEMENT GLOBAL VARIABLES
207 //==============================================================================
208 Standard_EXPORT ViewerTest_DoubleMapOfInteractiveAndName& GetMapOfAIS(){
209   static ViewerTest_DoubleMapOfInteractiveAndName TheMap;
210   return TheMap;
211 }
212
213
214 //==============================================================================
215 //function : VDisplayAISObject
216 //purpose  : register interactive object in the map of AIS objects;
217 //           if other object with such name already registered, it will be kept
218 //           or replaced depending on value of <theReplaceIfExists>,
219 //           if "true" - the old object will be cleared from AIS context;
220 //           returns Standard_True if <theAISObj> registered in map;
221 //==============================================================================
222 Standard_EXPORT Standard_Boolean VDisplayAISObject (const TCollection_AsciiString& theName,
223                                                     const Handle(AIS_InteractiveObject)& theAISObj,
224                                                     Standard_Boolean theReplaceIfExists = Standard_True)
225 {
226   ViewerTest_DoubleMapOfInteractiveAndName& aMap = GetMapOfAIS();
227   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
228   if (aContextAIS.IsNull())
229   {
230     std::cout << "AIS context is not available.\n";
231     return Standard_False;
232   }
233
234   if (aMap.IsBound2 (theName))
235   {
236     if (!theReplaceIfExists)
237     {
238       std::cout << "Other interactive object has been already "
239                 << "registered with name: " << theName << ".\n"
240                 << "Please use another name.\n";
241       return Standard_False;
242     }
243
244     // stop displaying object
245     Handle(AIS_InteractiveObject) anOldObj =
246        Handle(AIS_InteractiveObject)::DownCast (aMap.Find2 (theName));
247
248     if (!anOldObj.IsNull())
249       aContextAIS->Clear (anOldObj, Standard_True);
250
251     // remove name and old object from map
252     aMap.UnBind2 (theName);
253   }
254
255   if (theAISObj.IsNull())
256   {
257     // object with specified name already unbound
258     return Standard_True;
259   }
260
261   // unbind AIS object if was bound with another name
262   aMap.UnBind1 (theAISObj);
263
264   // can be registered without rebinding
265   aMap.Bind (theAISObj, theName);
266   aContextAIS->Display (theAISObj, Standard_True);
267   return Standard_True;
268 }
269
270 static TColStd_MapOfInteger theactivatedmodes(8);
271 static TColStd_ListOfTransient theEventMgrs;
272
273 static void VwrTst_InitEventMgr(const Handle(NIS_View)& aView,
274                                 const Handle(AIS_InteractiveContext)& Ctx)
275 {
276   theEventMgrs.Clear();
277   theEventMgrs.Prepend(new ViewerTest_EventManager(aView, Ctx));
278 }
279
280 static Handle(V3d_View)&  a3DView()
281 {
282   static Handle(V3d_View) Viou;
283   return Viou;
284 }
285
286
287 Standard_EXPORT Handle(AIS_InteractiveContext)& TheAISContext(){
288   static Handle(AIS_InteractiveContext) aContext;
289   return aContext;
290 }
291
292 const Handle(V3d_View)& ViewerTest::CurrentView()
293 {
294   return a3DView();
295 }
296 void ViewerTest::CurrentView(const Handle(V3d_View)& V)
297 {
298   a3DView() = V;
299 }
300
301 Standard_EXPORT const Handle(NIS_InteractiveContext)& TheNISContext()
302 {
303   static Handle(NIS_InteractiveContext) aContext;
304   if (aContext.IsNull()) {
305     aContext = new NIS_InteractiveContext;
306     aContext->SetSelectionMode (NIS_InteractiveContext::Mode_Normal);
307   }
308   return aContext;
309 }
310
311 const Handle(AIS_InteractiveContext)& ViewerTest::GetAISContext()
312 {
313   return TheAISContext();
314 }
315
316 void ViewerTest::SetAISContext (const Handle(AIS_InteractiveContext)& aCtx)
317 {
318   TheAISContext() = aCtx;
319   ViewerTest::ResetEventManager();
320 }
321
322 Handle(V3d_Viewer) ViewerTest::GetViewerFromContext()
323 {
324   return !TheAISContext().IsNull() ? TheAISContext()->CurrentViewer() : Handle(V3d_Viewer)();
325 }
326
327 Handle(V3d_Viewer) ViewerTest::GetCollectorFromContext()
328 {
329   return !TheAISContext().IsNull() ? TheAISContext()->CurrentViewer() : Handle(V3d_Viewer)();
330 }
331
332
333 void ViewerTest::SetEventManager(const Handle(ViewerTest_EventManager)& EM){
334   theEventMgrs.Prepend(EM);
335 }
336
337 void ViewerTest::UnsetEventManager()
338 {
339   theEventMgrs.RemoveFirst();
340 }
341
342
343 void ViewerTest::ResetEventManager()
344 {
345   const Handle(NIS_View) aView =
346     Handle(NIS_View)::DownCast(ViewerTest::CurrentView());
347   VwrTst_InitEventMgr(aView, ViewerTest::GetAISContext());
348 }
349
350 Handle(ViewerTest_EventManager) ViewerTest::CurrentEventManager()
351 {
352   Handle(ViewerTest_EventManager) EM;
353   if(theEventMgrs.IsEmpty()) return EM;
354   Handle(Standard_Transient) Tr =  theEventMgrs.First();
355   EM = *((Handle(ViewerTest_EventManager)*)&Tr);
356   return EM;
357 }
358
359 //=======================================================================
360 //function : Get Context and active view
361 //purpose  :
362 //=======================================================================
363 static Standard_Boolean getCtxAndView (Handle(AIS_InteractiveContext)& theCtx,
364                                        Handle(V3d_View)&               theView)
365 {
366   theCtx  = ViewerTest::GetAISContext();
367   theView = ViewerTest::CurrentView();
368   if (theCtx.IsNull()
369    || theView.IsNull())
370   {
371     std::cout << "Error: cannot find an active view!\n";
372     return Standard_False;
373   }
374   return Standard_True;
375 }
376
377 //==============================================================================
378 //function : GetShapeFromName
379 //purpose  : Compute an Shape from a draw variable or a file name
380 //==============================================================================
381
382 static TopoDS_Shape GetShapeFromName(const char* name)
383 {
384   TopoDS_Shape S = DBRep::Get(name);
385
386   if ( S.IsNull() ) {
387         BRep_Builder aBuilder;
388         BRepTools::Read( S, name, aBuilder);
389   }
390
391   return S;
392 }
393
394 //==============================================================================
395 //function : GetAISShapeFromName
396 //purpose  : Compute an AIS_Shape from a draw variable or a file name
397 //==============================================================================
398 Handle(AIS_Shape) GetAISShapeFromName(const char* name)
399 {
400   Handle(AIS_Shape) retsh;
401
402   if(GetMapOfAIS().IsBound2(name)){
403     const Handle(AIS_InteractiveObject) IO =
404       Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(name));
405     if (!IO.IsNull()) {
406       if(IO->Type()==AIS_KOI_Shape) {
407         if(IO->Signature()==0){
408           retsh = *((Handle(AIS_Shape)*)&IO);
409         }
410         else
411           cout << "an Object which is not an AIS_Shape "
412             "already has this name!!!"<<endl;
413       }
414     }
415     return retsh;
416   }
417
418
419   TopoDS_Shape S = GetShapeFromName(name);
420   if ( !S.IsNull() ) {
421     retsh = new AIS_Shape(S);
422   }
423   return retsh;
424 }
425
426
427 //==============================================================================
428 //function : Clear
429 //purpose  : Remove all the object from the viewer
430 //==============================================================================
431 void ViewerTest::Clear()
432 {
433   if ( !a3DView().IsNull() ) {
434     if (TheAISContext()->HasOpenedContext())
435       TheAISContext()->CloseLocalContext();
436     ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName it(GetMapOfAIS());
437     while ( it.More() ) {
438       cout << "Remove " << it.Key2() << endl;
439       if (it.Key1()->IsKind(STANDARD_TYPE(AIS_InteractiveObject))) {
440         const Handle(AIS_InteractiveObject) anObj =
441           Handle(AIS_InteractiveObject)::DownCast (it.Key1());
442         TheAISContext()->Remove(anObj,Standard_False);
443       } else if (it.Key1()->IsKind(STANDARD_TYPE(NIS_InteractiveObject))) {
444         const Handle(NIS_InteractiveObject) anObj =
445           Handle(NIS_InteractiveObject)::DownCast (it.Key1());
446         TheNISContext()->Remove(anObj);
447       }
448       it.Next();
449     }
450     TheAISContext()->UpdateCurrentViewer();
451 //    TheNISContext()->UpdateViews();
452     GetMapOfAIS().Clear();
453   }
454 }
455
456 //==============================================================================
457 //function : StandardModesActivation
458 //purpose  : Activate a selection mode, vertex, edge, wire ..., in a local
459 //           Context
460 //==============================================================================
461 void ViewerTest::StandardModeActivation(const Standard_Integer mode )
462 {
463   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
464   if(mode==0) {
465     if (TheAISContext()->HasOpenedContext())
466       aContext->CloseLocalContext();
467   } else {
468
469     if(!aContext->HasOpenedContext()) {
470       // To unhilight the preselected object
471       aContext->UnhilightCurrents(Standard_False);
472       // Open a local Context in order to be able to select subshape from
473       // the selected shape if any or for all if there is no selection
474       if (!aContext->FirstCurrentObject().IsNull()){
475         aContext->OpenLocalContext(Standard_False);
476
477         for(aContext->InitCurrent();aContext->MoreCurrent();aContext->NextCurrent()){
478           aContext->Load(       aContext->Current(),-1,Standard_True);
479         }
480       }
481       else
482         aContext->OpenLocalContext();
483     }
484
485     const char *cmode="???";
486
487     switch (mode) {
488     case 0: cmode = "Shape"; break;
489     case 1: cmode = "Vertex"; break;
490     case 2: cmode = "Edge"; break;
491     case 3: cmode = "Wire"; break;
492     case 4: cmode = "Face"; break;
493     case 5: cmode = "Shell"; break;
494     case 6: cmode = "Solid"; break;
495     case 7: cmode = "Compsolid"; break;
496     case 8: cmode = "Compound"; break;
497     }
498
499     if(theactivatedmodes.Contains(mode))
500       { // Desactivate
501         aContext->DeactivateStandardMode(AIS_Shape::SelectionType(mode));
502         theactivatedmodes.Remove(mode);
503         cout<<"Mode "<< cmode <<" OFF"<<endl;
504       }
505     else
506       { // Activate
507         aContext->ActivateStandardMode(AIS_Shape::SelectionType(mode));
508         theactivatedmodes.Add(mode);
509         cout<<"Mode "<< cmode << " ON" << endl;
510       }
511   }
512 }
513
514 //==============================================================================
515 //function : CopyIsoAspect
516 //purpose  : Returns copy Prs3d_IsoAspect with new number of isolines.
517 //==============================================================================
518 static Handle(Prs3d_IsoAspect) CopyIsoAspect
519       (const Handle(Prs3d_IsoAspect) &theIsoAspect,
520        const Standard_Integer theNbIsos)
521 {
522   Quantity_Color    aColor;
523   Aspect_TypeOfLine aType;
524   Standard_Real     aWidth;
525
526   theIsoAspect->Aspect()->Values(aColor, aType, aWidth);
527
528   Handle(Prs3d_IsoAspect) aResult =
529     new Prs3d_IsoAspect(aColor, aType, aWidth, theNbIsos);
530
531   return aResult;
532 }
533
534 //==============================================================================
535 //function : visos
536 //purpose  : Returns or sets the number of U- and V- isos and isIsoOnPlane flag
537 //Draw arg : [name1 ...] [nbUIsos nbVIsos IsoOnPlane(0|1)]
538 //==============================================================================
539 static int visos (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
540 {
541   if (TheAISContext().IsNull()) {
542     di << argv[0] << " Call 'vinit' before!\n";
543     return 1;
544   }
545
546   if (argc <= 1) {
547     di << "Current number of isos : " <<
548       TheAISContext()->IsoNumber(AIS_TOI_IsoU) << " " <<
549       TheAISContext()->IsoNumber(AIS_TOI_IsoV) << "\n";
550     di << "IsoOnPlane mode is " <<
551       (TheAISContext()->IsoOnPlane() ? "ON" : "OFF") << "\n";
552
553     return 0;
554   }
555
556   Standard_Integer aLastInd = argc - 1;
557   Standard_Boolean isChanged = Standard_False;
558   Standard_Integer aNbUIsos = 0;
559   Standard_Integer aNbVIsos = 0;
560
561   if (aLastInd >= 3) {
562     Standard_Boolean isIsoOnPlane = Standard_False;
563
564     if (strcmp(argv[aLastInd], "1") == 0) {
565       isIsoOnPlane = Standard_True;
566       isChanged    = Standard_True;
567     } else if (strcmp(argv[aLastInd], "0") == 0) {
568       isIsoOnPlane = Standard_False;
569       isChanged    = Standard_True;
570     }
571
572     if (isChanged) {
573       aNbVIsos = Draw::Atoi(argv[aLastInd - 1]);
574       aNbUIsos = Draw::Atoi(argv[aLastInd - 2]);
575       aLastInd -= 3;
576
577       di << "New number of isos : " << aNbUIsos << " " << aNbVIsos << "\n";
578       di << "New IsoOnPlane mode is " << (isIsoOnPlane ? "ON" : "OFF") << "\n";
579
580       TheAISContext()->IsoOnPlane(isIsoOnPlane);
581
582       if (aLastInd == 0) {
583         // If there are no shapes provided set the default numbers.
584         TheAISContext()->SetIsoNumber(aNbUIsos, AIS_TOI_IsoU);
585         TheAISContext()->SetIsoNumber(aNbVIsos, AIS_TOI_IsoV);
586       }
587     }
588   }
589
590   Standard_Integer i;
591
592   for (i = 1; i <= aLastInd; i++) {
593     TCollection_AsciiString name(argv[i]);
594     Standard_Boolean IsBound = GetMapOfAIS().IsBound2(name);
595
596     if (IsBound) {
597       const Handle(Standard_Transient) anObj = GetMapOfAIS().Find2(name);
598       if (anObj->IsKind(STANDARD_TYPE(AIS_InteractiveObject))) {
599         const Handle(AIS_InteractiveObject) aShape =
600         Handle(AIS_InteractiveObject)::DownCast (anObj);
601         Handle(AIS_Drawer) CurDrawer = aShape->Attributes();
602         Handle(Prs3d_IsoAspect) aUIso = CurDrawer->UIsoAspect();
603         Handle(Prs3d_IsoAspect) aVIso = CurDrawer->VIsoAspect();
604
605         if (isChanged) {
606           CurDrawer->SetUIsoAspect(CopyIsoAspect(aUIso, aNbUIsos));
607           CurDrawer->SetVIsoAspect(CopyIsoAspect(aVIso, aNbVIsos));
608           TheAISContext()->SetLocalAttributes
609                   (aShape, CurDrawer, Standard_False);
610           TheAISContext()->Redisplay(aShape);
611         } else {
612           di << "Number of isos for " << argv[i] << " : "
613              << aUIso->Number() << " " << aVIso->Number() << "\n";
614         }
615       } else {
616         di << argv[i] << ": Not an AIS interactive object!\n";
617       }
618     } else {
619       di << argv[i] << ": Use 'vdisplay' before\n";
620     }
621   }
622
623   if (isChanged) {
624     TheAISContext()->UpdateCurrentViewer();
625   }
626
627   return 0;
628 }
629
630 //==============================================================================
631 //function : VDispAreas,VDispSensitive,...
632 //purpose  :
633 //==============================================================================
634 static Standard_Integer VDispAreas (Draw_Interpretor& ,
635                                     Standard_Integer  theArgNb,
636                                     Standard_CString* )
637 {
638   if (theArgNb > 1)
639   {
640     std::cout << "Error: wrong syntax!\n";
641     return 1;
642   }
643
644   Handle(AIS_InteractiveContext) aCtx;
645   Handle(V3d_View)               aView;
646   if (!getCtxAndView (aCtx, aView))
647   {
648     return 1;
649   }
650
651   aCtx->DisplayActiveAreas (aView);
652   return 0;
653 }
654 static Standard_Integer VClearAreas (Draw_Interpretor& ,
655                                      Standard_Integer  theArgNb,
656                                      Standard_CString* )
657 {
658   if (theArgNb > 1)
659   {
660     std::cout << "Error: wrong syntax!\n";
661     return 1;
662   }
663
664   Handle(AIS_InteractiveContext) aCtx;
665   Handle(V3d_View)               aView;
666   if (!getCtxAndView (aCtx, aView))
667   {
668     return 1;
669   }
670
671   aCtx->ClearActiveAreas (aView);
672   return 0;
673
674 }
675 static Standard_Integer VDispSensi (Draw_Interpretor& ,
676                                     Standard_Integer  theArgNb,
677                                     Standard_CString* )
678 {
679   if (theArgNb > 1)
680   {
681     std::cout << "Error: wrong syntax!\n";
682     return 1;
683   }
684
685   Handle(AIS_InteractiveContext) aCtx;
686   Handle(V3d_View)               aView;
687   if (!getCtxAndView (aCtx, aView))
688   {
689     return 1;
690   }
691
692   aCtx->DisplayActiveSensitive (aView);
693   return 0;
694
695 }
696
697 static Standard_Integer VClearSensi (Draw_Interpretor& ,
698                                      Standard_Integer  theArgNb,
699                                      Standard_CString* )
700 {
701   if (theArgNb > 1)
702   {
703     std::cout << "Error: wrong syntax!\n";
704     return 1;
705   }
706
707   Handle(AIS_InteractiveContext) aCtx;
708   Handle(V3d_View)               aView;
709   if (!getCtxAndView (aCtx, aView))
710   {
711     return 1;
712   }
713   aCtx->ClearActiveSensitive (aView);
714   return 0;
715 }
716
717 //==============================================================================
718 //function : VDir
719 //purpose  : To list the displayed object with their attributes
720 //==============================================================================
721 static int VDir (Draw_Interpretor& theDI,
722                  Standard_Integer ,
723                  const char** )
724 {
725   if (!a3DView().IsNull())
726   {
727     return 0;
728   }
729
730   for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
731        anIter.More(); anIter.Next())
732   {
733     theDI << "\t" << anIter.Key2().ToCString() << "\n";
734   }
735   return 0;
736 }
737
738 //==============================================================================
739 //function : VSelPrecision
740 //purpose  : To set the selection precision mode and tolerance value
741 //Draw arg : Selection precision mode (0 for window, 1 for view) and tolerance
742 //           value (integer number of pixel for window mode, double value of
743 //           sensitivity for view mode). Without arguments the function just
744 //           prints the current precision mode and the corresponding tolerance.
745 //==============================================================================
746 static int VSelPrecision(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
747 {
748   if( argc > 3 )
749   {
750     di << "Use: " << argv[0] << " [precision_mode [tolerance_value]]\n";
751     return 1;
752   }
753
754   Handle(AIS_InteractiveContext) aContext = ViewerTest::GetAISContext();
755   if( aContext.IsNull() )
756     return 1;
757
758   if( argc == 1 )
759   {
760     StdSelect_SensitivityMode aMode = aContext->SensitivityMode();
761     if( aMode == StdSelect_SM_WINDOW )
762     {
763       Standard_Integer aPixelTolerance = aContext->PixelTolerance();
764       di << "Precision mode  : 0 (window)\n";
765       di << "Pixel tolerance : " << aPixelTolerance << "\n";
766     }
767     else if( aMode == StdSelect_SM_VIEW )
768     {
769       Standard_Real aSensitivity = aContext->Sensitivity();
770       di << "Precision mode : 1 (view)\n";
771       di << "Sensitivity    : " << aSensitivity << "\n";
772     }
773   }
774   else if( argc > 1 )
775   {
776     StdSelect_SensitivityMode aMode = ( StdSelect_SensitivityMode )Draw::Atoi( argv[1] );
777     aContext->SetSensitivityMode( aMode );
778     if( argc > 2 )
779     {
780       if( aMode == StdSelect_SM_WINDOW )
781       {
782         Standard_Integer aPixelTolerance = Draw::Atoi( argv[2] );
783         aContext->SetPixelTolerance( aPixelTolerance );
784       }
785       else if( aMode == StdSelect_SM_VIEW )
786       {
787         Standard_Real aSensitivity = Draw::Atof( argv[2] );
788         aContext->SetSensitivity( aSensitivity );
789       }
790     }
791   }
792   return 0;
793 }
794
795 //==============================================================================
796 //function : VDump
797 //purpose  : To dump the active view snapshot to image file
798 //==============================================================================
799 static Standard_Integer VDump (Draw_Interpretor& theDI,
800                                Standard_Integer  theArgNb,
801                                Standard_CString* theArgVec)
802 {
803   if (theArgNb < 2)
804   {
805     std::cout << "Error: wrong number of arguments! Image file name should be specified at least.\n";
806     return 1;
807   }
808
809   Standard_Integer      anArgIter   = 1;
810   Standard_CString      aFilePath   = theArgVec[anArgIter++];
811   Graphic3d_BufferType  aBufferType = Graphic3d_BT_RGB;
812   V3d_StereoDumpOptions aStereoOpts = V3d_SDO_MONO;
813   Standard_Integer      aWidth      = 0;
814   Standard_Integer      aHeight     = 0;
815   for (; anArgIter < theArgNb; ++anArgIter)
816   {
817     TCollection_AsciiString anArg (theArgVec[anArgIter]);
818     anArg.LowerCase();
819     if (anArg == "rgba")
820     {
821       aBufferType = Graphic3d_BT_RGBA;
822     }
823     else if (anArg == "rgb")
824     {
825       aBufferType = Graphic3d_BT_RGB;
826     }
827     else if (anArg == "depth")
828     {
829       aBufferType = Graphic3d_BT_Depth;
830     }
831     else if (anArg == "l"
832           || anArg == "left")
833     {
834       aStereoOpts = V3d_SDO_LEFT_EYE;
835     }
836     else if (anArg == "r"
837           || anArg == "right")
838     {
839       aStereoOpts = V3d_SDO_RIGHT_EYE;
840     }
841     else if (anArg == "mono")
842     {
843       aStereoOpts = V3d_SDO_MONO;
844     }
845     else if (anArg == "w"
846           || anArg == "width")
847     {
848       if (aWidth  != 0)
849       {
850         std::cout << "Error: wrong syntax at " << theArgVec[anArgIter] << "\n";
851         return 1;
852       }
853       else if (++anArgIter >= theArgNb)
854       {
855         std::cout << "Error: integer value is expected right after 'width'\n";
856         return 1;
857       }
858       aWidth = Draw::Atoi (theArgVec[anArgIter]);
859     }
860     else if (anArg == "h"
861           || anArg == "height")
862     {
863       if (aHeight != 0)
864       {
865         std::cout << "Error: wrong syntax at " << theArgVec[anArgIter] << "\n";
866         return 1;
867       }
868       if (++anArgIter >= theArgNb)
869       {
870         std::cout << "Error: integer value is expected right after 'height'\n";
871         return 1;
872       }
873       aHeight = Draw::Atoi (theArgVec[anArgIter]);
874     }
875     else if (anArg.IsIntegerValue())
876     {
877       // compatibility with old syntax
878       if (aWidth  != 0
879        || aHeight != 0)
880       {
881         std::cout << "Error: wrong syntax at " << theArgVec[anArgIter] << "\n";
882         return 1;
883       }
884       else if (++anArgIter >= theArgNb)
885       {
886         std::cout << "Error: height value is expected right after width\n";
887         return 1;
888       }
889       aWidth  = Draw::Atoi (theArgVec[anArgIter - 1]);
890       aHeight = Draw::Atoi (theArgVec[anArgIter]);
891     }
892     else
893     {
894       std::cout << "Error: unknown argument '" << theArgVec[anArgIter] << "'\n";
895       return 1;
896     }
897   }
898   if ((aWidth <= 0 && aHeight >  0)
899    || (aWidth >  0 && aHeight <= 0))
900   {
901     std::cout << "Error: dimensions " << aWidth << "x" << aHeight << " are incorrect\n";
902     return 1;
903   }
904
905   Handle(V3d_View) aView = ViewerTest::CurrentView();
906   if (aView.IsNull())
907   {
908     std::cout << "Error: cannot find an active view!\n";
909     return 1;
910   }
911
912   if (aWidth <= 0 || aHeight <= 0)
913   {
914     if (aStereoOpts != V3d_SDO_MONO)
915     {
916       aView->Window()->Size (aWidth, aHeight);
917     }
918     else
919     {
920       if (!aView->Dump (aFilePath, aBufferType))
921       {
922         theDI << "Fail: view dump failed!\n";
923       }
924       return 0;
925     }
926   }
927
928   Image_AlienPixMap aPixMap;
929   if (!aView->ToPixMap (aPixMap, aWidth, aHeight, aBufferType, Standard_True, aStereoOpts))
930   {
931     theDI << "Fail: view dump failed!\n";
932     return 0;
933   }
934
935   if (aPixMap.SizeX() != Standard_Size(aWidth)
936    || aPixMap.SizeY() != Standard_Size(aHeight))
937   {
938     theDI << "Fail: dumped dimensions "    << (Standard_Integer )aPixMap.SizeX() << "x" << (Standard_Integer )aPixMap.SizeY()
939           << " are lesser than requested " << aWidth << "x" << aHeight << "\n";
940   }
941   if (!aPixMap.Save (aFilePath))
942   {
943     theDI << "Fail: image can not be saved!\n";
944   }
945   return 0;
946 }
947
948
949 //==============================================================================
950 //function : Displays,Erase...
951 //purpose  :
952 //Draw arg :
953 //==============================================================================
954 static int VwrTst_DispErase(const Handle(AIS_InteractiveObject)& IO,
955                             const Standard_Integer Mode,
956                             const Standard_Integer TypeOfOperation,
957                             const Standard_Boolean Upd)
958 {
959   Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
960
961   switch(TypeOfOperation){
962   case 1:
963     Ctx->Display(IO,Mode,Upd);
964     break;
965   case 2:{
966     Ctx->Erase(IO,Upd);
967     break;
968   }
969   case 3:{
970     if(IO.IsNull())
971       Ctx->SetDisplayMode((AIS_DisplayMode)Mode,Upd);
972     else
973       Ctx->SetDisplayMode(IO,Mode,Upd);
974     break;
975   }
976   case 4:{
977     if(IO.IsNull())
978       Ctx->SetDisplayMode(0,Upd);
979     else
980       Ctx->UnsetDisplayMode(IO,Upd);
981     break;
982   }
983   }
984   return 0;
985 }
986
987 //=======================================================================
988 //function :
989 //purpose  :
990 //=======================================================================
991 static int VDispMode (Draw_Interpretor& , Standard_Integer argc, const char** argv)
992 {
993
994   TCollection_AsciiString name;
995   if(argc>3)
996     return 1;
997   // display others presentations
998   Standard_Integer TypeOfOperation = (strcasecmp(argv[0],"vdispmode")==0)? 1:
999     (strcasecmp(argv[0],"verasemode")==0) ? 2 :
1000       (strcasecmp(argv[0],"vsetdispmode")==0) ? 3 :
1001         (strcasecmp(argv[0],"vunsetdispmode")==0) ? 4 : -1;
1002
1003   Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
1004
1005   //unset displaymode.. comportement particulier...
1006   if(TypeOfOperation==4){
1007     if(argc==1){
1008       if(Ctx->NbCurrents()==0 ||
1009          Ctx->NbSelected()==0){
1010         Handle(AIS_InteractiveObject) IO;
1011         VwrTst_DispErase(IO,-1,4,Standard_False);
1012       }
1013       else if(!Ctx->HasOpenedContext()){
1014         for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent())
1015           VwrTst_DispErase(Ctx->Current(),-1,4,Standard_False);
1016       }
1017       else{
1018         for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
1019           VwrTst_DispErase(Ctx->Interactive(),-1,4,Standard_False);}
1020       Ctx->UpdateCurrentViewer();
1021     }
1022     else{
1023       Handle(AIS_InteractiveObject) IO;
1024       name = argv[1];
1025       if(GetMapOfAIS().IsBound2(name)){
1026         IO = Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(name));
1027         if (!IO.IsNull())
1028           VwrTst_DispErase(IO,-1,4,Standard_True);
1029       }
1030     }
1031   }
1032   else if(argc==2){
1033     Standard_Integer Dmode = Draw::Atoi(argv[1]);
1034     if(Ctx->NbCurrents()==0 && TypeOfOperation==3){
1035       Handle(AIS_InteractiveObject) IO;
1036       VwrTst_DispErase(IO,Dmode,TypeOfOperation,Standard_True);
1037     }
1038     if(!Ctx->HasOpenedContext()){
1039       // set/unset display mode sur le Contexte...
1040       for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent()){
1041         VwrTst_DispErase(Ctx->Current(),Dmode,TypeOfOperation,Standard_False);
1042       }
1043       Ctx->UpdateCurrentViewer();
1044     }
1045     else{
1046       for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected())
1047         Ctx->Display(Ctx->Interactive(),Dmode);
1048     }
1049   }
1050   else{
1051     Handle(AIS_InteractiveObject) IO;
1052     name = argv[1];
1053     if(GetMapOfAIS().IsBound2(name))
1054       IO = Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(name));
1055     if (!IO.IsNull())
1056       VwrTst_DispErase(IO,Draw::Atoi(argv[2]),TypeOfOperation,Standard_True);
1057   }
1058   return 0;
1059 }
1060
1061
1062 //=======================================================================
1063 //function :
1064 //purpose  :
1065 //=======================================================================
1066 static int VSubInt(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
1067 {
1068   if(argc==1) return 1;
1069   Standard_Integer On = Draw::Atoi(argv[1]);
1070   const Handle(AIS_InteractiveContext)& Ctx = ViewerTest::GetAISContext();
1071
1072   if(argc==2){
1073
1074     if(!Ctx->HasOpenedContext()){
1075       di<<"sub intensite ";
1076       if(On==1) di<<"On";
1077       else di<<"Off";
1078       di<<" pour "<<Ctx->NbCurrents()<<"  objets"<<"\n";
1079       for(Ctx->InitCurrent();Ctx->MoreCurrent();Ctx->NextCurrent()){
1080         if(On==1){
1081           Ctx->SubIntensityOn(Ctx->Current(),Standard_False);}
1082         else{
1083           di <<"passage dans off"<<"\n";
1084           Ctx->SubIntensityOff(Ctx->Current(),Standard_False);
1085         }
1086       }
1087     }
1088     else{
1089       for(Ctx->InitSelected();Ctx->MoreSelected();Ctx->NextSelected()){
1090         if(On==1){
1091           Ctx->SubIntensityOn(Ctx->Interactive(),Standard_False);}
1092         else{
1093           Ctx->SubIntensityOff(Ctx->Interactive(),Standard_False);}
1094       }
1095     }
1096     Ctx->UpdateCurrentViewer();
1097   }
1098   else {
1099     Handle(AIS_InteractiveObject) IO;
1100     TCollection_AsciiString name = argv[2];
1101     if(GetMapOfAIS().IsBound2(name)){
1102       IO = Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(name));
1103       if (!IO.IsNull()) {
1104         if(On==1)
1105           Ctx->SubIntensityOn(IO);
1106         else
1107           Ctx->SubIntensityOff(IO);
1108       }
1109     }
1110     else return 1;
1111   }
1112   return 0;
1113 }
1114
1115 //! Auxiliary class to iterate presentations from different collections.
1116 class ViewTest_PrsIter
1117 {
1118 public:
1119
1120   //! Create and initialize iterator object.
1121   ViewTest_PrsIter (const TCollection_AsciiString& theName)
1122   : mySource (IterSource_All)
1123   {
1124     NCollection_Sequence<TCollection_AsciiString> aNames;
1125     if (!theName.IsEmpty())
1126     aNames.Append (theName);
1127     Init (aNames);
1128   }
1129
1130   //! Create and initialize iterator object.
1131   ViewTest_PrsIter (const NCollection_Sequence<TCollection_AsciiString>& theNames)
1132   : mySource (IterSource_All)
1133   {
1134     Init (theNames);
1135   }
1136
1137   //! Initialize the iterator.
1138   void Init (const NCollection_Sequence<TCollection_AsciiString>& theNames)
1139   {
1140     Handle(AIS_InteractiveContext) aCtx = ViewerTest::GetAISContext();
1141     mySeq = theNames;
1142     mySelIter.Nullify();
1143     myCurrent.Nullify();
1144     myCurrentTrs.Nullify();
1145     if (!mySeq.IsEmpty())
1146     {
1147       mySource = IterSource_List;
1148       mySeqIter = NCollection_Sequence<TCollection_AsciiString>::Iterator (mySeq);
1149     }
1150     else if (aCtx->NbCurrents() > 0)
1151     {
1152       mySource  = IterSource_Selected;
1153       mySelIter = aCtx;
1154       mySelIter->InitCurrent();
1155     }
1156     else
1157     {
1158       mySource = IterSource_All;
1159       myMapIter.Initialize (GetMapOfAIS());
1160     }
1161     initCurrent();
1162   }
1163
1164   const TCollection_AsciiString& CurrentName() const
1165   {
1166     return myCurrentName;
1167   }
1168
1169   const Handle(AIS_InteractiveObject)& Current() const
1170   {
1171     return myCurrent;
1172   }
1173
1174   const Handle(Standard_Transient)& CurrentTrs() const
1175   {
1176     return myCurrentTrs;
1177   }
1178
1179   //! @return true if iterator points to valid object within collection
1180   Standard_Boolean More() const
1181   {
1182     switch (mySource)
1183     {
1184       case IterSource_All:      return myMapIter.More();
1185       case IterSource_List:     return mySeqIter.More();
1186       case IterSource_Selected: return mySelIter->MoreCurrent();
1187     }
1188     return Standard_False;
1189   }
1190
1191   //! Go to the next item.
1192   void Next()
1193   {
1194     myCurrentName.Clear();
1195     myCurrentTrs.Nullify();
1196     myCurrent.Nullify();
1197     switch (mySource)
1198     {
1199       case IterSource_All:
1200       {
1201         myMapIter.Next();
1202         break;
1203       }
1204       case IterSource_List:
1205       {
1206         mySeqIter.Next();
1207         break;
1208       }
1209       case IterSource_Selected:
1210       {
1211         mySelIter->NextCurrent();
1212         break;
1213       }
1214     }
1215     initCurrent();
1216   }
1217
1218 private:
1219
1220   void initCurrent()
1221   {
1222     switch (mySource)
1223     {
1224       case IterSource_All:
1225       {
1226         if (myMapIter.More())
1227         {
1228           myCurrentName = myMapIter.Key2();
1229           myCurrentTrs  = myMapIter.Key1();
1230           myCurrent     = Handle(AIS_InteractiveObject)::DownCast (myCurrentTrs);
1231         }
1232         break;
1233       }
1234       case IterSource_List:
1235       {
1236         if (mySeqIter.More())
1237         {
1238           if (!GetMapOfAIS().IsBound2 (mySeqIter.Value()))
1239           {
1240             std::cout << "Error: object " << mySeqIter.Value() << " is not displayed!\n";
1241             return;
1242           }
1243           myCurrentName = mySeqIter.Value();
1244           myCurrentTrs  = GetMapOfAIS().Find2 (mySeqIter.Value());
1245           myCurrent     = Handle(AIS_InteractiveObject)::DownCast (myCurrentTrs);
1246         }
1247         break;
1248       }
1249       case IterSource_Selected:
1250       {
1251         if (mySelIter->MoreCurrent())
1252         {
1253           myCurrentName = GetMapOfAIS().Find1 (mySelIter->Current());
1254           myCurrent     = mySelIter->Current();
1255         }
1256         break;
1257       }
1258     }
1259   }
1260
1261 private:
1262
1263   enum IterSource
1264   {
1265     IterSource_All,
1266     IterSource_List,
1267     IterSource_Selected
1268   };
1269
1270 private:
1271
1272   Handle(AIS_InteractiveContext) mySelIter;    //!< iterator for current (selected) objects (IterSource_Selected)
1273   ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName myMapIter; //!< iterator for map of all objects (IterSource_All)
1274   NCollection_Sequence<TCollection_AsciiString>           mySeq;
1275   NCollection_Sequence<TCollection_AsciiString>::Iterator mySeqIter;
1276
1277   TCollection_AsciiString        myCurrentName;//!< current item name
1278   Handle(Standard_Transient)     myCurrentTrs; //!< current item (as transient object)
1279   Handle(AIS_InteractiveObject)  myCurrent;    //!< current item
1280
1281   IterSource                     mySource;     //!< iterated collection
1282
1283 };
1284
1285 //==============================================================================
1286 //function : VInteriorStyle
1287 //purpose  : sets interior style of the a selected or named or displayed shape
1288 //==============================================================================
1289 static int VSetInteriorStyle (Draw_Interpretor& theDI,
1290                               Standard_Integer  theArgNb,
1291                               const char**      theArgVec)
1292 {
1293   const Handle(AIS_InteractiveContext)& aCtx = ViewerTest::GetAISContext();
1294   ViewerTest_AutoUpdater anUpdateTool (aCtx, ViewerTest::CurrentView());
1295   if (aCtx.IsNull())
1296   {
1297     std::cerr << "Error: no active view!\n";
1298     return 1;
1299   }
1300
1301   Standard_Integer anArgIter = 1;
1302   for (; anArgIter < theArgNb; ++anArgIter)
1303   {
1304     if (!anUpdateTool.parseRedrawMode (theArgVec[anArgIter]))
1305     {
1306       break;
1307     }
1308   }
1309   TCollection_AsciiString aName;
1310   if (theArgNb - anArgIter == 2)
1311   {
1312     aName = theArgVec[anArgIter++];
1313   }
1314   else if (theArgNb - anArgIter != 1)
1315   {
1316     std::cout << "Error: wrong number of arguments! See usage:\n";
1317     theDI.PrintHelp (theArgVec[0]);
1318     return 1;
1319   }
1320   Standard_Integer        anInterStyle = Aspect_IS_SOLID;
1321   TCollection_AsciiString aStyleArg (theArgVec[anArgIter++]);
1322   aStyleArg.LowerCase();
1323   if (aStyleArg == "empty")
1324   {
1325     anInterStyle = 0;
1326   }
1327   else if (aStyleArg == "hollow")
1328   {
1329     anInterStyle = 1;
1330   }
1331   else if (aStyleArg == "hatch")
1332   {
1333     anInterStyle = 2;
1334   }
1335   else if (aStyleArg == "solid")
1336   {
1337     anInterStyle = 3;
1338   }
1339   else if (aStyleArg == "hiddenline")
1340   {
1341     anInterStyle = 4;
1342   }
1343   else
1344   {
1345     anInterStyle = aStyleArg.IntegerValue();
1346   }
1347   if (anInterStyle < Aspect_IS_EMPTY
1348    || anInterStyle > Aspect_IS_HIDDENLINE)
1349   {
1350     std::cout << "Error: style must be within a range [0 (Aspect_IS_EMPTY), "
1351               << Aspect_IS_HIDDENLINE << " (Aspect_IS_HIDDENLINE)]\n";
1352     return 1;
1353   }
1354
1355   if (!aName.IsEmpty()
1356    && !GetMapOfAIS().IsBound2 (aName))
1357   {
1358     std::cout << "Error: object " << aName << " is not displayed!\n";
1359     return 1;
1360   }
1361
1362   if (aCtx->HasOpenedContext())
1363   {
1364     aCtx->CloseLocalContext();
1365   }
1366   for (ViewTest_PrsIter anIter (aName); anIter.More(); anIter.Next())
1367   {
1368     const Handle(AIS_InteractiveObject)& anIO = anIter.Current();
1369     if (!anIO.IsNull())
1370     {
1371       const Handle(Prs3d_Drawer)& aDrawer        = anIO->Attributes();
1372       Handle(Prs3d_ShadingAspect) aShadingAspect = aDrawer->ShadingAspect();
1373       Handle(Graphic3d_AspectFillArea3d) aFillAspect = aShadingAspect->Aspect();
1374       aFillAspect->SetInteriorStyle ((Aspect_InteriorStyle )anInterStyle);
1375       aCtx->RecomputePrsOnly (anIO, Standard_False, Standard_True);
1376     }
1377   }
1378   return 0;
1379 }
1380
1381 //! Auxiliary structure for VAspects
1382 struct ViewerTest_AspectsChangeSet
1383 {
1384   Standard_Integer         ToSetColor;
1385   Quantity_Color           Color;
1386
1387   Standard_Integer         ToSetLineWidth;
1388   Standard_Real            LineWidth;
1389
1390   Standard_Integer         ToSetTransparency;
1391   Standard_Real            Transparency;
1392
1393   Standard_Integer         ToSetMaterial;
1394   Graphic3d_NameOfMaterial Material;
1395   TCollection_AsciiString  MatName;
1396
1397   NCollection_Sequence<TopoDS_Shape> SubShapes;
1398
1399   //! Empty constructor
1400   ViewerTest_AspectsChangeSet()
1401   : ToSetColor        (0),
1402     Color             (DEFAULT_COLOR),
1403     ToSetLineWidth    (0),
1404     LineWidth         (1.0),
1405     ToSetTransparency (0),
1406     Transparency      (0.0),
1407     ToSetMaterial     (0),
1408     Material (Graphic3d_NOM_DEFAULT) {}
1409
1410   //! @return true if no changes have been requested
1411   Standard_Boolean IsEmpty() const
1412   {
1413     return ToSetLineWidth    == 0
1414         && ToSetTransparency == 0
1415         && ToSetColor        == 0
1416         && ToSetMaterial     == 0;
1417   }
1418
1419   //! @return true if properties are valid
1420   Standard_Boolean Validate (const Standard_Boolean theIsSubPart) const
1421   {
1422     Standard_Boolean isOk = Standard_True;
1423     if (LineWidth <= 0.0
1424      || LineWidth >  10.0)
1425     {
1426       std::cout << "Error: the width should be within [1; 10] range (specified " << LineWidth << ")\n";
1427       isOk = Standard_False;
1428     }
1429     if (Transparency < 0.0
1430      || Transparency > 1.0)
1431     {
1432       std::cout << "Error: the transparency should be within [0; 1] range (specified " << Transparency << ")\n";
1433       isOk = Standard_False;
1434     }
1435     if (theIsSubPart
1436      && ToSetTransparency)
1437     {
1438       std::cout << "Error: the transparency can not be defined for sub-part of object!\n";
1439       isOk = Standard_False;
1440     }
1441     if (ToSetMaterial == 1
1442      && Material == Graphic3d_NOM_DEFAULT)
1443     {
1444       std::cout << "Error: unknown material " << MatName << ".\n";
1445       isOk = Standard_False;
1446     }
1447     return isOk;
1448   }
1449
1450 };
1451
1452 //==============================================================================
1453 //function : VAspects
1454 //purpose  :
1455 //==============================================================================
1456 static Standard_Integer VAspects (Draw_Interpretor& /*theDI*/,
1457                                   Standard_Integer  theArgNb,
1458                                   const char**      theArgVec)
1459 {
1460   TCollection_AsciiString aCmdName (theArgVec[0]);
1461   const Handle(AIS_InteractiveContext)& aCtx = ViewerTest::GetAISContext();
1462   ViewerTest_AutoUpdater anUpdateTool (aCtx, ViewerTest::CurrentView());
1463   if (aCtx.IsNull())
1464   {
1465     std::cerr << "Error: no active view!\n";
1466     return 1;
1467   }
1468
1469   Standard_Integer anArgIter = 1;
1470   NCollection_Sequence<TCollection_AsciiString> aNames;
1471   for (; anArgIter < theArgNb; ++anArgIter)
1472   {
1473     TCollection_AsciiString anArg = theArgVec[anArgIter];
1474     if (anUpdateTool.parseRedrawMode (anArg))
1475     {
1476       continue;
1477     }
1478     else if (!anArg.IsEmpty()
1479            && anArg.Value (1) != '-')
1480     {
1481       aNames.Append (anArg);
1482     }
1483     else
1484     {
1485       break;
1486     }
1487   }
1488
1489   NCollection_Sequence<ViewerTest_AspectsChangeSet> aChanges;
1490   aChanges.Append (ViewerTest_AspectsChangeSet());
1491   ViewerTest_AspectsChangeSet* aChangeSet = &aChanges.ChangeLast();
1492
1493   // parse syntax of legacy commands
1494   if (aCmdName == "vsetwidth")
1495   {
1496     if (aNames.IsEmpty()
1497     || !aNames.Last().IsRealValue())
1498     {
1499       std::cout << "Error: not enough arguments!\n";
1500       return 1;
1501     }
1502     aChangeSet->ToSetLineWidth = 1;
1503     aChangeSet->LineWidth = aNames.Last().RealValue();
1504     aNames.Remove (aNames.Length());
1505   }
1506   else if (aCmdName == "vunsetwidth")
1507   {
1508     aChangeSet->ToSetLineWidth = -1;
1509   }
1510   else if (aCmdName == "vsetcolor")
1511   {
1512     if (aNames.IsEmpty())
1513     {
1514       std::cout << "Error: not enough arguments!\n";
1515       return 1;
1516     }
1517     aChangeSet->ToSetColor = 1;
1518     aChangeSet->Color  = ViewerTest::GetColorFromName (aNames.Last().ToCString());
1519     aNames.Remove (aNames.Length());
1520   }
1521   else if (aCmdName == "vunsetcolor")
1522   {
1523     aChangeSet->ToSetColor = -1;
1524   }
1525   else if (aCmdName == "vsettransparency")
1526   {
1527     if (aNames.IsEmpty()
1528     || !aNames.Last().IsRealValue())
1529     {
1530       std::cout << "Error: not enough arguments!\n";
1531       return 1;
1532     }
1533     aChangeSet->ToSetTransparency = 1;
1534     aChangeSet->Transparency  = aNames.Last().RealValue();
1535     aNames.Remove (aNames.Length());
1536   }
1537   else if (aCmdName == "vunsettransparency")
1538   {
1539     aChangeSet->ToSetTransparency = -1;
1540   }
1541   else if (aCmdName == "vsetmaterial")
1542   {
1543     if (aNames.IsEmpty())
1544     {
1545       std::cout << "Error: not enough arguments!\n";
1546       return 1;
1547     }
1548     aChangeSet->ToSetMaterial = 1;
1549     aChangeSet->MatName  = aNames.Last();
1550     aChangeSet->Material = Graphic3d_MaterialAspect::MaterialFromName (aChangeSet->MatName.ToCString());
1551     aNames.Remove (aNames.Length());
1552   }
1553   else if (aCmdName == "vunsetmaterial")
1554   {
1555     aChangeSet->ToSetMaterial = -1;
1556   }
1557   else if (anArgIter >= theArgNb)
1558   {
1559     std::cout << "Error: not enough arguments!\n";
1560     return 1;
1561   }
1562
1563   if (!aChangeSet->IsEmpty())
1564   {
1565     anArgIter = theArgNb;
1566   }
1567   for (; anArgIter < theArgNb; ++anArgIter)
1568   {
1569     TCollection_AsciiString anArg = theArgVec[anArgIter];
1570     anArg.LowerCase();
1571     if (anArg == "-setwidth"
1572      || anArg == "-setlinewidth")
1573     {
1574       if (++anArgIter >= theArgNb)
1575       {
1576         std::cout << "Error: wrong syntax at " << anArg << "\n";
1577         return 1;
1578       }
1579       aChangeSet->ToSetLineWidth = 1;
1580       aChangeSet->LineWidth = Draw::Atof (theArgVec[anArgIter]);
1581     }
1582     else if (anArg == "-unsetwidth"
1583           || anArg == "-unsetlinewidth")
1584     {
1585       aChangeSet->ToSetLineWidth = -1;
1586       aChangeSet->LineWidth = 1.0;
1587     }
1588     else if (anArg == "-settransp"
1589           || anArg == "-settransparency")
1590     {
1591       if (++anArgIter >= theArgNb)
1592       {
1593         std::cout << "Error: wrong syntax at " << anArg << "\n";
1594         return 1;
1595       }
1596       aChangeSet->ToSetTransparency = 1;
1597       aChangeSet->Transparency = Draw::Atof (theArgVec[anArgIter]);
1598       if (aChangeSet->Transparency >= 0.0
1599        && aChangeSet->Transparency <= Precision::Confusion())
1600       {
1601         aChangeSet->ToSetTransparency = -1;
1602         aChangeSet->Transparency = 0.0;
1603       }
1604     }
1605     else if (anArg == "-setalpha")
1606     {
1607       if (++anArgIter >= theArgNb)
1608       {
1609         std::cout << "Error: wrong syntax at " << anArg << "\n";
1610         return 1;
1611       }
1612       aChangeSet->ToSetTransparency = 1;
1613       aChangeSet->Transparency  = Draw::Atof (theArgVec[anArgIter]);
1614       if (aChangeSet->Transparency < 0.0
1615        || aChangeSet->Transparency > 1.0)
1616       {
1617         std::cout << "Error: the transparency should be within [0; 1] range (specified " << aChangeSet->Transparency << ")\n";
1618         return 1;
1619       }
1620       aChangeSet->Transparency = 1.0 - aChangeSet->Transparency;
1621       if (aChangeSet->Transparency >= 0.0
1622        && aChangeSet->Transparency <= Precision::Confusion())
1623       {
1624         aChangeSet->ToSetTransparency = -1;
1625         aChangeSet->Transparency = 0.0;
1626       }
1627     }
1628     else if (anArg == "-unsettransp"
1629           || anArg == "-unsettransparency"
1630           || anArg == "-unsetalpha"
1631           || anArg == "-opaque")
1632     {
1633       aChangeSet->ToSetTransparency = -1;
1634       aChangeSet->Transparency = 0.0;
1635     }
1636     else if (anArg == "-setcolor")
1637     {
1638       if (++anArgIter >= theArgNb)
1639       {
1640         std::cout << "Error: wrong syntax at " << anArg << "\n";
1641         return 1;
1642       }
1643       aChangeSet->ToSetColor = 1;
1644       aChangeSet->Color = ViewerTest::GetColorFromName (theArgVec[anArgIter]);
1645     }
1646     else if (anArg == "-unsetcolor")
1647     {
1648       aChangeSet->ToSetColor = -1;
1649       aChangeSet->Color = DEFAULT_COLOR;
1650     }
1651     else if (anArg == "-setmat"
1652           || anArg == "-setmaterial")
1653     {
1654       if (++anArgIter >= theArgNb)
1655       {
1656         std::cout << "Error: wrong syntax at " << anArg << "\n";
1657         return 1;
1658       }
1659       aChangeSet->ToSetMaterial = 1;
1660       aChangeSet->MatName  = theArgVec[anArgIter];
1661       aChangeSet->Material = Graphic3d_MaterialAspect::MaterialFromName (aChangeSet->MatName.ToCString());
1662     }
1663     else if (anArg == "-unsetmat"
1664           || anArg == "-unsetmaterial")
1665     {
1666       aChangeSet->ToSetMaterial = -1;
1667       aChangeSet->Material = Graphic3d_NOM_DEFAULT;
1668     }
1669     else if (anArg == "-subshape"
1670           || anArg == "-subshapes")
1671     {
1672       if (aNames.IsEmpty())
1673       {
1674         std::cout << "Error: main objects should specified explicitly when -subshapes is used!\n";
1675         return 1;
1676       }
1677
1678       aChanges.Append (ViewerTest_AspectsChangeSet());
1679       aChangeSet = &aChanges.ChangeLast();
1680
1681       for (++anArgIter; anArgIter < theArgNb; ++anArgIter)
1682       {
1683         Standard_CString aSubShapeName = theArgVec[anArgIter];
1684         if (*aSubShapeName == '-')
1685         {
1686           --anArgIter;
1687           break;
1688         }
1689
1690         TopoDS_Shape aSubShape = DBRep::Get (aSubShapeName);
1691         if (aSubShape.IsNull())
1692         {
1693           std::cerr << "Error: shape " << aSubShapeName << " doesn't found!\n";
1694           return 1;
1695         }
1696         aChangeSet->SubShapes.Append (aSubShape);
1697       }
1698
1699       if (aChangeSet->SubShapes.IsEmpty())
1700       {
1701         std::cerr << "Error: empty list is specified after -subshapes!\n";
1702         return 1;
1703       }
1704     }
1705     else if (anArg == "-unset")
1706     {
1707       aChangeSet->ToSetLineWidth = -1;
1708       aChangeSet->LineWidth = 1.0;
1709       aChangeSet->ToSetTransparency = -1;
1710       aChangeSet->Transparency = 0.0;
1711       aChangeSet->ToSetColor = -1;
1712       aChangeSet->Color = DEFAULT_COLOR;
1713       aChangeSet->ToSetMaterial = -1;
1714       aChangeSet->Material = Graphic3d_NOM_DEFAULT;
1715     }
1716     else
1717     {
1718       std::cout << "Error: wrong syntax at " << anArg << "\n";
1719       return 1;
1720     }
1721   }
1722
1723   Standard_Boolean isFirst = Standard_True;
1724   for (NCollection_Sequence<ViewerTest_AspectsChangeSet>::Iterator aChangesIter (aChanges);
1725        aChangesIter.More(); aChangesIter.Next())
1726   {
1727     if (!aChangesIter.Value().Validate (!isFirst))
1728     {
1729       return 1;
1730     }
1731     isFirst = Standard_False;
1732   }
1733
1734   if (aCtx->HasOpenedContext())
1735   {
1736     aCtx->CloseLocalContext();
1737   }
1738   for (ViewTest_PrsIter aPrsIter (aNames); aPrsIter.More(); aPrsIter.Next())
1739   {
1740     const TCollection_AsciiString& aName = aPrsIter.CurrentName();
1741     Handle(AIS_InteractiveObject)  aPrs  = aPrsIter.Current();
1742     Handle(AIS_ColoredShape) aColoredPrs;
1743     Standard_Boolean toDisplay = Standard_False;
1744     if (aChanges.Length() > 1)
1745     {
1746       Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast (aPrs);
1747       if (aShapePrs.IsNull())
1748       {
1749         std::cout << "Error: an object " << aName << " is not an AIS_Shape presentation!\n";
1750         return 1;
1751       }
1752       aColoredPrs = Handle(AIS_ColoredShape)::DownCast (aShapePrs);
1753       if (aColoredPrs.IsNull())
1754       {
1755         aColoredPrs = new AIS_ColoredShape (aShapePrs);
1756         aCtx->Remove (aShapePrs, Standard_False);
1757         GetMapOfAIS().UnBind2 (aName);
1758         GetMapOfAIS().Bind (aColoredPrs, aName);
1759         toDisplay = Standard_True;
1760         aShapePrs = aColoredPrs;
1761         aPrs      = aColoredPrs;
1762       }
1763     }
1764
1765     if (!aPrs.IsNull())
1766     {
1767       NCollection_Sequence<ViewerTest_AspectsChangeSet>::Iterator aChangesIter (aChanges);
1768       aChangeSet = &aChangesIter.ChangeValue();
1769       if (aChangeSet->ToSetMaterial == 1)
1770       {
1771         aCtx->SetMaterial (aPrs, aChangeSet->Material, Standard_False);
1772       }
1773       else if (aChangeSet->ToSetMaterial == -1)
1774       {
1775         aCtx->UnsetMaterial (aPrs, Standard_False);
1776       }
1777       if (aChangeSet->ToSetColor == 1)
1778       {
1779         aCtx->SetColor (aPrs, aChangeSet->Color, Standard_False);
1780       }
1781       else if (aChangeSet->ToSetColor == -1)
1782       {
1783         aCtx->UnsetColor (aPrs, Standard_False);
1784       }
1785       if (aChangeSet->ToSetTransparency == 1)
1786       {
1787         aCtx->SetTransparency (aPrs, aChangeSet->Transparency, Standard_False);
1788       }
1789       else if (aChangeSet->ToSetTransparency == -1)
1790       {
1791         aCtx->UnsetTransparency (aPrs, Standard_False);
1792       }
1793       if (aChangeSet->ToSetLineWidth == 1)
1794       {
1795         aCtx->SetWidth (aPrs, aChangeSet->LineWidth, Standard_False);
1796       }
1797       else if (aChangeSet->ToSetLineWidth == -1)
1798       {
1799         aCtx->UnsetWidth (aPrs, Standard_False);
1800       }
1801
1802       for (aChangesIter.Next(); aChangesIter.More(); aChangesIter.Next())
1803       {
1804         aChangeSet = &aChangesIter.ChangeValue();
1805         for (NCollection_Sequence<TopoDS_Shape>::Iterator aSubShapeIter (aChangeSet->SubShapes);
1806              aSubShapeIter.More(); aSubShapeIter.Next())
1807         {
1808           const TopoDS_Shape& aSubShape = aSubShapeIter.Value();
1809           if (aChangeSet->ToSetColor == 1)
1810           {
1811             aColoredPrs->SetCustomColor (aSubShape, aChangeSet->Color);
1812           }
1813           if (aChangeSet->ToSetLineWidth == 1)
1814           {
1815             aColoredPrs->SetCustomWidth (aSubShape, aChangeSet->LineWidth);
1816           }
1817           if (aChangeSet->ToSetColor     == -1
1818            || aChangeSet->ToSetLineWidth == -1)
1819           {
1820             aColoredPrs->UnsetCustomAspects (aSubShape, Standard_True);
1821           }
1822         }
1823       }
1824       if (toDisplay)
1825       {
1826         aCtx->Display (aPrs, Standard_False);
1827       }
1828       else if (!aColoredPrs.IsNull())
1829       {
1830         aColoredPrs->Redisplay();
1831       }
1832     }
1833     else
1834     {
1835       Handle(NIS_InteractiveObject) aNisObj = Handle(NIS_InteractiveObject)::DownCast (aPrsIter.CurrentTrs());
1836       Handle(NIS_Triangulated)      aNisTri = Handle(NIS_Triangulated)::DownCast (aNisObj);
1837       if (!aNisObj.IsNull())
1838       {
1839         if (aChangeSet->ToSetTransparency != 0)
1840         {
1841           aNisObj->SetTransparency (aChangeSet->Transparency);
1842         }
1843       }
1844       if (!aNisTri.IsNull())
1845       {
1846         if (aChangeSet->ToSetColor != 0)
1847         {
1848           aNisTri->SetColor (aChangeSet->Color);
1849         }
1850         if (aChangeSet->ToSetLineWidth != 0)
1851         {
1852           aNisTri->SetLineWidth (aChangeSet->LineWidth);
1853         }
1854       }
1855     }
1856   }
1857   return 0;
1858 }
1859
1860 //==============================================================================
1861 //function : VDonly2
1862 //author   : ege
1863 //purpose  : Display only a selected or named  object
1864 //           if there is no selected or named object s, nothing is done
1865 //==============================================================================
1866 static int VDonly2 (Draw_Interpretor& ,
1867                     Standard_Integer  theArgNb,
1868                     const char**      theArgVec)
1869 {
1870   const Handle(AIS_InteractiveContext)& aCtx = ViewerTest::GetAISContext();
1871   ViewerTest_AutoUpdater anUpdateTool (aCtx, ViewerTest::CurrentView());
1872   if (aCtx.IsNull())
1873   {
1874     std::cerr << "Error: no active view!\n";
1875     return 1;
1876   }
1877
1878   if (aCtx->HasOpenedContext())
1879   {
1880     aCtx->CloseLocalContext();
1881   }
1882
1883   Standard_Integer anArgIter = 1;
1884   for (; anArgIter < theArgNb; ++anArgIter)
1885   {
1886     if (!anUpdateTool.parseRedrawMode (theArgVec[anArgIter]))
1887     {
1888       break;
1889     }
1890   }
1891
1892   NCollection_Map<Handle(Standard_Transient)> aDispSet;
1893   if (anArgIter >= theArgNb)
1894   {
1895     // display only selected objects
1896     if (aCtx->NbCurrents() < 1)
1897     {
1898       return 0;
1899     }
1900
1901     for (aCtx->InitCurrent(); aCtx->MoreCurrent(); aCtx->NextCurrent())
1902     {
1903       aDispSet.Add (aCtx->Current());
1904     }
1905   }
1906   else
1907   {
1908     // display only specified objects
1909     for (; anArgIter < theArgNb; ++anArgIter)
1910     {
1911       TCollection_AsciiString aName = theArgVec[anArgIter];
1912       if (GetMapOfAIS().IsBound2 (aName))
1913       {
1914         const Handle(Standard_Transient) anObj = GetMapOfAIS().Find2 (aName);
1915         if (anObj->IsKind (STANDARD_TYPE(AIS_InteractiveObject)))
1916         {
1917           const Handle(AIS_InteractiveObject) aShape = Handle(AIS_InteractiveObject)::DownCast (anObj);
1918           aCtx->Display (aShape, Standard_False);
1919         }
1920         else if (anObj->IsKind (STANDARD_TYPE(NIS_InteractiveObject)))
1921         {
1922           Handle(NIS_InteractiveObject) aShape = Handle(NIS_InteractiveObject)::DownCast (anObj);
1923           TheNISContext()->Display (aShape);
1924         }
1925         aDispSet.Add (anObj);
1926       }
1927     }
1928   }
1929
1930   // weed out other objects
1931   for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS()); anIter.More(); anIter.Next())
1932   {
1933     if (aDispSet.Contains (anIter.Key1()))
1934     {
1935       continue;
1936     }
1937
1938     if (anIter.Key1()->IsKind (STANDARD_TYPE(AIS_InteractiveObject)))
1939     {
1940       const Handle(AIS_InteractiveObject) aShape = Handle(AIS_InteractiveObject)::DownCast (anIter.Key1());
1941       aCtx->Erase (aShape, Standard_False);
1942     }
1943     else if (anIter.Key1()->IsKind (STANDARD_TYPE(NIS_InteractiveObject)))
1944     {
1945       const Handle(NIS_InteractiveObject) aShape = Handle(NIS_InteractiveObject)::DownCast (anIter.Key1());
1946       TheNISContext()->Erase (aShape);
1947     }
1948   }
1949   return 0;
1950 }
1951
1952 //==============================================================================
1953 //function : VRemove
1954 //purpose  : Removes selected or named objects.
1955 //           If there is no selected or named objects,
1956 //           all objects in the viewer can be removed with argument -all.
1957 //           If -context is in arguments, the object is not deleted from the map of
1958 //           objects (deleted only from the current context).
1959 //==============================================================================
1960 int VRemove (Draw_Interpretor& theDI,
1961              Standard_Integer  theArgNb,
1962              const char**      theArgVec)
1963 {
1964   const Handle(AIS_InteractiveContext)& aCtx = ViewerTest::GetAISContext();
1965   ViewerTest_AutoUpdater anUpdateTool (aCtx, ViewerTest::CurrentView());
1966   if (aCtx.IsNull())
1967   {
1968     std::cout << "Error: no active view!\n";
1969     return 1;
1970   }
1971
1972   aCtx->CloseAllContexts (Standard_False);
1973
1974   Standard_Boolean isContextOnly = Standard_False;
1975   Standard_Boolean toRemoveAll   = Standard_False;
1976   Standard_Boolean toPrintInfo   = Standard_True;
1977
1978   Standard_Integer anArgIter = 1;
1979   for (; anArgIter < theArgNb; ++anArgIter)
1980   {
1981     TCollection_AsciiString anArg = theArgVec[anArgIter];
1982     anArg.LowerCase();
1983     if (anArg == "-context")
1984     {
1985       isContextOnly = Standard_True;
1986     }
1987     else if (anArg == "-all")
1988     {
1989       toRemoveAll = Standard_True;
1990     }
1991     else if (anArg == "-noinfo")
1992     {
1993       toPrintInfo = Standard_False;
1994     }
1995     else if (!anUpdateTool.parseRedrawMode (anArg))
1996     {
1997       break;
1998     }
1999   }
2000   if (toRemoveAll
2001    && anArgIter < theArgNb)
2002   {
2003     std::cout << "Error: wrong syntax!\n";
2004     return 1;
2005   }
2006
2007   NCollection_List<TCollection_AsciiString> anIONameList;
2008   if (toRemoveAll)
2009   {
2010     for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
2011          anIter.More(); anIter.Next())
2012     {
2013       anIONameList.Append (anIter.Key2());
2014     }
2015   }
2016   else if (anArgIter < theArgNb) // removed objects names are in argument list
2017   {
2018     for (; anArgIter < theArgNb; ++anArgIter)
2019     {
2020       TCollection_AsciiString aName = theArgVec[anArgIter];
2021       if (!GetMapOfAIS().IsBound2 (aName))
2022       {
2023         theDI << aName.ToCString() << " was not bound to some object.\n";
2024         continue;
2025       }
2026
2027       const Handle(Standard_Transient)& aTransientObj = GetMapOfAIS().Find2 (aName);
2028
2029       const Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (aTransientObj);
2030       if (!anIO.IsNull())
2031       {
2032         if (anIO->GetContext() != aCtx)
2033         {
2034           theDI << aName.ToCString() << " was not displayed in current context.\n";
2035           theDI << "Please activate view with this object displayed and try again.\n";
2036           continue;
2037         }
2038
2039         anIONameList.Append (aName);
2040         continue;
2041       }
2042
2043       const Handle(NIS_InteractiveObject) aNisIO = Handle(NIS_InteractiveObject)::DownCast (aTransientObj);
2044       if (!aNisIO.IsNull())
2045       {
2046         anIONameList.Append (aName);
2047       }
2048     }
2049   }
2050   else if (aCtx->NbCurrents() > 0
2051         || TheNISContext()->GetSelected().Extent() > 0)
2052   {
2053     for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
2054          anIter.More(); anIter.Next())
2055     {
2056       const Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anIter.Key1());
2057       if (!anIO.IsNull())
2058       {
2059         if (!aCtx->IsCurrent (anIO))
2060         {
2061           continue;
2062         }
2063
2064         anIONameList.Append (anIter.Key2());
2065         continue;
2066       }
2067
2068       const Handle(NIS_InteractiveObject) aNisIO = Handle(NIS_InteractiveObject)::DownCast (anIter.Key1());
2069       if (!aNisIO.IsNull())
2070       {
2071         if (!TheNISContext()->IsSelected (aNisIO))
2072         {
2073           continue;
2074         }
2075
2076         anIONameList.Append (anIter.Key2());
2077       }
2078     }
2079   }
2080
2081   // Unbind all removed objects from the map of displayed IO.
2082   for (NCollection_List<TCollection_AsciiString>::Iterator anIter (anIONameList);
2083        anIter.More(); anIter.Next())
2084   {
2085     const Handle(AIS_InteractiveObject) anIO  = Handle(AIS_InteractiveObject)::DownCast (GetMapOfAIS().Find2 (anIter.Value()));
2086     if (!anIO.IsNull())
2087     {
2088       aCtx->Remove (anIO, Standard_False);
2089       if (toPrintInfo)
2090       {
2091         theDI << anIter.Value().ToCString() << " was removed\n";
2092       }
2093     }
2094     else
2095     {
2096       const Handle(NIS_InteractiveObject) aNisIO = Handle(NIS_InteractiveObject)::DownCast (GetMapOfAIS().Find2 (anIter.Value()));
2097       if (!aNisIO.IsNull())
2098       {
2099         TheNISContext()->Remove (aNisIO);
2100         if (toPrintInfo)
2101         {
2102           theDI << anIter.Value().ToCString() << " was removed\n";
2103         }
2104       }
2105     }
2106     if (!isContextOnly)
2107     {
2108       GetMapOfAIS().UnBind2 (anIter.Value());
2109     }
2110   }
2111   return 0;
2112 }
2113
2114 //==============================================================================
2115 //function : VErase
2116 //purpose  : Erase some selected or named objects
2117 //           if there is no selected or named objects, the whole viewer is erased
2118 //==============================================================================
2119 int VErase (Draw_Interpretor& theDI,
2120             Standard_Integer  theArgNb,
2121             const char**      theArgVec)
2122 {
2123   const Handle(AIS_InteractiveContext)& aCtx = ViewerTest::GetAISContext();
2124   ViewerTest_AutoUpdater anUpdateTool (aCtx, ViewerTest::CurrentView());
2125   if (aCtx.IsNull())
2126   {
2127     std::cout << "Error: no active view!\n";
2128     return 1;
2129   }
2130   aCtx->CloseAllContexts (Standard_False);
2131
2132   const Standard_Boolean toEraseAll = TCollection_AsciiString (theArgNb > 0 ? theArgVec[0] : "") == "veraseall";
2133
2134   Standard_Integer anArgIter = 1;
2135   for (; anArgIter < theArgNb; ++anArgIter)
2136   {
2137     if (!anUpdateTool.parseRedrawMode (theArgVec[anArgIter]))
2138     {
2139       break;
2140     }
2141   }
2142
2143   if (anArgIter < theArgNb)
2144   {
2145     if (toEraseAll)
2146     {
2147       std::cerr << "Error: wrong syntax, " << theArgVec[0] << " too much arguments.\n";
2148       return 1;
2149     }
2150
2151     // has a list of names
2152     for (; anArgIter < theArgNb; ++anArgIter)
2153     {
2154       TCollection_AsciiString aName = theArgVec[anArgIter];
2155       if (!GetMapOfAIS().IsBound2 (aName))
2156       {
2157         continue;
2158       }
2159
2160       const Handle(Standard_Transient)    anObj = GetMapOfAIS().Find2 (aName);
2161       const Handle(AIS_InteractiveObject) anIO  = Handle(AIS_InteractiveObject)::DownCast (anObj);
2162       theDI << aName.ToCString() << " ";
2163       if (!anIO.IsNull())
2164       {
2165         aCtx->Erase (anIO, Standard_False);
2166       }
2167       else
2168       {
2169         const Handle(NIS_InteractiveObject) aNisIO = Handle(NIS_InteractiveObject)::DownCast (anObj);
2170         if (!aNisIO.IsNull())
2171         {
2172           TheNISContext()->Erase (aNisIO);
2173         }
2174       }
2175     }
2176   }
2177   else if (!toEraseAll
2178         && aCtx->NbCurrents() > 0)
2179   {
2180     // remove all currently selected objects
2181     for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
2182          anIter.More(); anIter.Next())
2183     {
2184       const Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anIter.Key1());
2185       if (!anIO.IsNull()
2186        && aCtx->IsCurrent (anIO))
2187       {
2188         theDI << anIter.Key2().ToCString() << " ";
2189         aCtx->Erase (anIO, Standard_False);
2190       }
2191     }
2192   }
2193   else
2194   {
2195     // erase entire viewer
2196     for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
2197          anIter.More(); anIter.Next())
2198     {
2199       const Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anIter.Key1());
2200       if (!anIO.IsNull())
2201       {
2202         aCtx->Erase (anIO, Standard_False);
2203       }
2204       else
2205       {
2206         const Handle(NIS_InteractiveObject) aNisIO = Handle(NIS_InteractiveObject)::DownCast (anIter.Key1());
2207         if (!aNisIO.IsNull())
2208         {
2209           TheNISContext()->Erase (aNisIO);
2210         }
2211       }
2212     }
2213   }
2214   return 0;
2215 }
2216
2217 //==============================================================================
2218 //function : VDisplayAll
2219 //author   : ege
2220 //purpose  : Display all the objects of the Map
2221 //==============================================================================
2222 static int VDisplayAll (Draw_Interpretor& ,
2223                         Standard_Integer  theArgNb,
2224                         const char**      theArgVec)
2225
2226 {
2227   const Handle(AIS_InteractiveContext)& aCtx = ViewerTest::GetAISContext();
2228   ViewerTest_AutoUpdater anUpdateTool (aCtx, ViewerTest::CurrentView());
2229   if (aCtx.IsNull())
2230   {
2231     std::cout << "Error: no active view!\n";
2232     return 1;
2233   }
2234
2235   Standard_Integer anArgIter = 1;
2236   for (; anArgIter < theArgNb; ++anArgIter)
2237   {
2238     if (!anUpdateTool.parseRedrawMode (theArgVec[anArgIter]))
2239     {
2240       break;
2241     }
2242   }
2243   if (anArgIter < theArgNb)
2244   {
2245     std::cout << theArgVec[0] << "Error: wrong syntax\n";
2246     return 1;
2247   }
2248
2249   if (aCtx->HasOpenedContext())
2250   {
2251     aCtx->CloseLocalContext();
2252   }
2253
2254   for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
2255        anIter.More(); anIter.Next())
2256   {
2257     if (anIter.Key1()->IsKind (STANDARD_TYPE(AIS_InteractiveObject)))
2258     {
2259       const Handle(AIS_InteractiveObject) aShape = Handle(AIS_InteractiveObject)::DownCast (anIter.Key1());
2260       aCtx->Erase (aShape, Standard_False);
2261     }
2262     else if (anIter.Key1()->IsKind(STANDARD_TYPE(NIS_InteractiveObject)))
2263     {
2264       const Handle(NIS_InteractiveObject) aShape = Handle(NIS_InteractiveObject)::DownCast (anIter.Key1());
2265       TheNISContext()->Erase (aShape);
2266     }
2267   }
2268
2269   for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
2270        anIter.More(); anIter.Next())
2271   {
2272     if (anIter.Key1()->IsKind (STANDARD_TYPE(AIS_InteractiveObject)))
2273     {
2274       const Handle(AIS_InteractiveObject) aShape = Handle(AIS_InteractiveObject)::DownCast (anIter.Key1());
2275       aCtx->Display (aShape, Standard_False);
2276     }
2277     else if (anIter.Key1()->IsKind (STANDARD_TYPE(NIS_InteractiveObject)))
2278     {
2279       Handle(NIS_InteractiveObject) aShape = Handle(NIS_InteractiveObject)::DownCast (anIter.Key1());
2280       TheNISContext()->Display (aShape);
2281     }
2282   }
2283   return 0;
2284 }
2285
2286 //! Auxiliary method to find presentation
2287 inline Handle(PrsMgr_Presentation) findPresentation (const Handle(AIS_InteractiveContext)& theCtx,
2288                                                      const Handle(AIS_InteractiveObject)&  theIO,
2289                                                      const Standard_Integer                theMode)
2290 {
2291   if (theIO.IsNull())
2292   {
2293     return Handle(PrsMgr_Presentation)();
2294   }
2295
2296   if (theMode != -1)
2297   {
2298     if (theCtx->MainPrsMgr()->HasPresentation (theIO, theMode))
2299     {
2300       return theCtx->MainPrsMgr()->Presentation (theIO, theMode);
2301     }
2302   }
2303   else if (theCtx->MainPrsMgr()->HasPresentation (theIO, theIO->DisplayMode()))
2304   {
2305     return theCtx->MainPrsMgr()->Presentation (theIO, theIO->DisplayMode());
2306   }
2307   else if (theCtx->MainPrsMgr()->HasPresentation (theIO, theCtx->DisplayMode()))
2308   {
2309     return theCtx->MainPrsMgr()->Presentation (theIO, theCtx->DisplayMode());
2310   }
2311   return Handle(PrsMgr_Presentation)();
2312 }
2313
2314 enum ViewerTest_BndAction
2315 {
2316   BndAction_Hide,
2317   BndAction_Show,
2318   BndAction_Print
2319 };
2320
2321 //! Auxiliary method to print bounding box of presentation
2322 inline void bndPresentation (Draw_Interpretor&                  theDI,
2323                              const Handle(PrsMgr_Presentation)& thePrs,
2324                              const TCollection_AsciiString&     theName,
2325                              const ViewerTest_BndAction         theAction)
2326 {
2327   switch (theAction)
2328   {
2329     case BndAction_Hide:
2330     {
2331       thePrs->Presentation()->GraphicUnHighlight();
2332       break;
2333     }
2334     case BndAction_Show:
2335     {
2336       thePrs->Presentation()->BoundBox();
2337       break;
2338     }
2339     case BndAction_Print:
2340     {
2341       Graphic3d_Vec3d aMin, aMax;
2342       thePrs->Presentation()->MinMaxValues (aMin.x(), aMin.y(), aMin.z(),
2343                                             aMax.x(), aMax.y(), aMax.z());
2344       theDI << theName  << "\n"
2345             << aMin.x() << " " << aMin.y() << " " << aMin.z() << " "
2346             << aMax.x() << " " << aMax.y() << " " << aMax.z() << "\n";
2347       break;
2348     }
2349   }
2350 }
2351
2352 //==============================================================================
2353 //function : VBounding
2354 //purpose  :
2355 //==============================================================================
2356 int VBounding (Draw_Interpretor& theDI,
2357                Standard_Integer  theArgNb,
2358                const char**      theArgVec)
2359 {
2360   Handle(AIS_InteractiveContext) aCtx = ViewerTest::GetAISContext();
2361   ViewerTest_AutoUpdater anUpdateTool (aCtx, ViewerTest::CurrentView());
2362   if (aCtx.IsNull())
2363   {
2364     std::cout << "Error: no active view!\n";
2365     return 1;
2366   }
2367
2368   ViewerTest_BndAction anAction = BndAction_Show;
2369   Standard_Integer     aMode    = -1;
2370
2371   Standard_Integer anArgIter = 1;
2372   for (; anArgIter < theArgNb; ++anArgIter)
2373   {
2374     TCollection_AsciiString anArg (theArgVec[anArgIter]);
2375     anArg.LowerCase();
2376     if (anArg == "-print")
2377     {
2378       anAction = BndAction_Print;
2379     }
2380     else if (anArg == "-show")
2381     {
2382       anAction = BndAction_Show;
2383     }
2384     else if (anArg == "-hide")
2385     {
2386       anAction = BndAction_Hide;
2387     }
2388     else if (anArg == "-mode")
2389     {
2390       if (++anArgIter >= theArgNb)
2391       {
2392         std::cout << "Error: wrong syntax at " << anArg << "\n";
2393         return 1;
2394       }
2395       aMode = Draw::Atoi (theArgVec[anArgIter]);
2396     }
2397     else if (!anUpdateTool.parseRedrawMode (anArg))
2398     {
2399       break;
2400     }
2401   }
2402
2403   if (anArgIter < theArgNb)
2404   {
2405     // has a list of names
2406     for (; anArgIter < theArgNb; ++anArgIter)
2407     {
2408       TCollection_AsciiString aName = theArgVec[anArgIter];
2409       if (!GetMapOfAIS().IsBound2 (aName))
2410       {
2411         std::cout << "Error: presentation " << aName << " does not exist\n";
2412         return 1;
2413       }
2414
2415       Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (GetMapOfAIS().Find2 (aName));
2416       Handle(PrsMgr_Presentation)   aPrs = findPresentation (aCtx, anIO, aMode);
2417       if (aPrs.IsNull())
2418       {
2419         std::cout << "Error: presentation " << aName << " does not exist\n";
2420         return 1;
2421       }
2422       bndPresentation (theDI, aPrs, aName, anAction);
2423     }
2424   }
2425   else if (aCtx->NbCurrents() > 0)
2426   {
2427     // remove all currently selected objects
2428     for (aCtx->InitCurrent(); aCtx->MoreCurrent(); aCtx->NextCurrent())
2429     {
2430       Handle(AIS_InteractiveObject) anIO = aCtx->Current();
2431       Handle(PrsMgr_Presentation)   aPrs = findPresentation (aCtx, anIO, aMode);
2432       if (!aPrs.IsNull())
2433       {
2434         bndPresentation (theDI, aPrs, GetMapOfAIS().IsBound1 (anIO) ? GetMapOfAIS().Find1 (anIO) : "", anAction);
2435       }
2436     }
2437   }
2438   else
2439   {
2440     // all objects
2441     for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anIter (GetMapOfAIS());
2442          anIter.More(); anIter.Next())
2443     {
2444       Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast (anIter.Key1());
2445       Handle(PrsMgr_Presentation)   aPrs = findPresentation (aCtx, anIO, aMode);
2446       if (!aPrs.IsNull())
2447       {
2448         bndPresentation (theDI, aPrs, anIter.Key2(), anAction);
2449       }
2450     }
2451   }
2452   return 0;
2453 }
2454
2455 //==============================================================================
2456 //function : VTexture
2457 //purpose  :
2458 //==============================================================================
2459 Standard_Integer VTexture (Draw_Interpretor& theDi, Standard_Integer theArgsNb, const char** theArgv)
2460 {
2461   TCollection_AsciiString aCommandName (theArgv[0]);
2462
2463   NCollection_DataMap<TCollection_AsciiString, Handle(TColStd_HSequenceOfAsciiString)> aMapOfArgs;
2464   if (aCommandName == "vtexture")
2465   {
2466     if (theArgsNb < 2)
2467     {
2468       std::cout << theArgv[0] << ": " << " invalid arguments.\n";
2469       std::cout << "Type help for more information.\n";
2470       return 1;
2471     }
2472
2473     // look for options of vtexture command
2474     TCollection_AsciiString aParseKey;
2475     for (Standard_Integer anArgIt = 2; anArgIt < theArgsNb; ++anArgIt)
2476     {
2477       TCollection_AsciiString anArg (theArgv [anArgIt]);
2478
2479       anArg.UpperCase();
2480       if (anArg.Value (1) == '-' && !anArg.IsRealValue())
2481       {
2482         aParseKey = anArg;
2483         aParseKey.Remove (1);
2484         aParseKey.UpperCase();
2485         aMapOfArgs.Bind (aParseKey, new TColStd_HSequenceOfAsciiString);
2486         continue;
2487       }
2488
2489       if (aParseKey.IsEmpty())
2490       {
2491         continue;
2492       }
2493
2494       aMapOfArgs(aParseKey)->Append (anArg);
2495     }
2496   }
2497   else if (aCommandName == "vtexscale"
2498         || aCommandName == "vtexorigin"
2499         || aCommandName == "vtexrepeat")
2500   {
2501     // scan for parameters of vtexscale, vtexorigin, vtexrepeat commands
2502     // equal to -scale, -origin, -repeat options of vtexture command
2503     if (theArgsNb < 2 || theArgsNb > 4)
2504     {
2505       std::cout << theArgv[0] << ": " << " invalid arguments.\n";
2506       std::cout << "Type help for more information.\n";
2507       return 1;
2508     }
2509
2510     Handle(TColStd_HSequenceOfAsciiString) anArgs = new TColStd_HSequenceOfAsciiString;
2511     if (theArgsNb == 2)
2512     {
2513       anArgs->Append ("OFF");
2514     }
2515     else if (theArgsNb == 4)
2516     {
2517       anArgs->Append (TCollection_AsciiString (theArgv[2]));
2518       anArgs->Append (TCollection_AsciiString (theArgv[3]));
2519     }
2520
2521     TCollection_AsciiString anArgKey;
2522     if (aCommandName == "vtexscale")
2523     {
2524       anArgKey = "SCALE";
2525     }
2526     else if (aCommandName == "vtexorigin")
2527     {
2528       anArgKey = "ORIGIN";
2529     }
2530     else
2531     {
2532       anArgKey = "REPEAT";
2533     }
2534
2535     aMapOfArgs.Bind (anArgKey, anArgs);
2536   }
2537   else if (aCommandName == "vtexdefault")
2538   {
2539     // scan for parameters of vtexdefault command
2540     // equal to -default option of vtexture command
2541     aMapOfArgs.Bind ("DEFAULT", new TColStd_HSequenceOfAsciiString);
2542   }
2543
2544   // Check arguments for validity
2545   NCollection_DataMap<TCollection_AsciiString, Handle(TColStd_HSequenceOfAsciiString)>::Iterator aMapIt (aMapOfArgs);
2546   for (; aMapIt.More(); aMapIt.Next())
2547   {
2548     const TCollection_AsciiString& aKey = aMapIt.Key();
2549     const Handle(TColStd_HSequenceOfAsciiString)& anArgs = aMapIt.Value();
2550
2551     // -scale, -origin, -repeat: one argument "off", or two real values
2552     if ((aKey.IsEqual ("SCALE") || aKey.IsEqual ("ORIGIN") || aKey.IsEqual ("REPEAT"))
2553       && ((anArgs->Length() == 1 && anArgs->Value(1) == "OFF")
2554        || (anArgs->Length() == 2 && anArgs->Value(1).IsRealValue() && anArgs->Value(2).IsRealValue())))
2555     {
2556       continue;
2557     }
2558
2559     // -modulate: single argument "on" / "off"
2560     if (aKey.IsEqual ("MODULATE") && anArgs->Length() == 1 && (anArgs->Value(1) == "OFF" || anArgs->Value(1) == "ON"))
2561     {
2562       continue;
2563     }
2564
2565     // -default: no arguments
2566     if (aKey.IsEqual ("DEFAULT") && anArgs->IsEmpty())
2567     {
2568       continue;
2569     }
2570
2571     TCollection_AsciiString aLowerKey;
2572     aLowerKey  = "-";
2573     aLowerKey += aKey;
2574     aLowerKey.LowerCase();
2575     std::cout << theArgv[0] << ": " << aLowerKey << " is unknown option, or the arguments are unacceptable.\n";
2576     std::cout << "Type help for more information.\n";
2577     return 1;
2578   }
2579
2580   Handle(AIS_InteractiveContext) anAISContext = ViewerTest::GetAISContext();
2581   if (anAISContext.IsNull())
2582   {
2583     std::cout << aCommandName << ": " << " please use 'vinit' command to initialize view.\n";
2584     return 1;
2585   }
2586
2587   Standard_Integer aPreviousMode = 0;
2588
2589   ViewerTest::CurrentView()->SetSurfaceDetail (V3d_TEX_ALL);
2590
2591   TCollection_AsciiString aShapeName (theArgv[1]);
2592   Handle(AIS_InteractiveObject) anIO;
2593
2594   const ViewerTest_DoubleMapOfInteractiveAndName& aMapOfIO = GetMapOfAIS();
2595   if (aMapOfIO.IsBound2 (aShapeName))
2596   {
2597     anIO = Handle(AIS_InteractiveObject)::DownCast (aMapOfIO.Find2 (aShapeName));
2598   }
2599
2600   if (anIO.IsNull())
2601   {
2602     std::cout << aCommandName << ": shape " << aShapeName << " does not exists.\n";
2603     return 1;
2604   }
2605
2606   Handle(AIS_TexturedShape) aTexturedIO;
2607   if (anIO->IsKind (STANDARD_TYPE (AIS_TexturedShape)))
2608   {
2609     aTexturedIO = Handle(AIS_TexturedShape)::DownCast (anIO);
2610     aPreviousMode = aTexturedIO->DisplayMode();
2611   }
2612   else
2613   {
2614     anAISContext->Clear (anIO, Standard_False);
2615     aTexturedIO = new AIS_TexturedShape (DBRep::Get (theArgv[1]));
2616     GetMapOfAIS().UnBind1 (anIO);
2617     GetMapOfAIS().UnBind2 (aShapeName);
2618     GetMapOfAIS().Bind (aTexturedIO, aShapeName);
2619   }
2620
2621   // -------------------------------------------
2622   //  Turn texturing on/off - only for vtexture
2623   // -------------------------------------------
2624
2625   if (aCommandName == "vtexture")
2626   {
2627     TCollection_AsciiString aTextureArg (theArgsNb > 2 ? theArgv[2] : "");
2628
2629     if (aTextureArg.IsEmpty())
2630     {
2631       std::cout << aCommandName << ": " << " Texture mapping disabled.\n";
2632       std::cout << "To enable it, use 'vtexture NameOfShape NameOfTexture'\n" << "\n";
2633
2634       anAISContext->SetDisplayMode (aTexturedIO, AIS_Shaded, Standard_False);
2635       if (aPreviousMode == 3)
2636       {
2637         anAISContext->RecomputePrsOnly (aTexturedIO);
2638       }
2639
2640       anAISContext->Display (aTexturedIO, Standard_True);
2641       return 0;
2642     }
2643     else if (aTextureArg.Value(1) != '-') // "-option" on place of texture argument
2644     {
2645       if (aTextureArg == "?")
2646       {
2647         TCollection_AsciiString aTextureFolder = Graphic3d_TextureRoot::TexturesFolder();
2648
2649         theDi << "\n Files in current directory : \n" << "\n";
2650         theDi.Eval ("glob -nocomplain *");
2651
2652         TCollection_AsciiString aCmnd ("glob -nocomplain ");
2653         aCmnd += aTextureFolder;
2654         aCmnd += "/* ";
2655
2656         theDi << "Files in " << aTextureFolder.ToCString() << " : \n" << "\n";
2657         theDi.Eval (aCmnd.ToCString());
2658         return 0;
2659       }
2660       else
2661       {
2662         aTexturedIO->SetTextureFileName (aTextureArg);
2663       }
2664     }
2665   }
2666
2667   // ------------------------------------
2668   //  Process other options and commands
2669   // ------------------------------------
2670
2671   Handle(TColStd_HSequenceOfAsciiString) aValues;
2672   if (aMapOfArgs.Find ("DEFAULT", aValues))
2673   {
2674     aTexturedIO->SetTextureRepeat (Standard_False);
2675     aTexturedIO->SetTextureOrigin (Standard_False);
2676     aTexturedIO->SetTextureScale  (Standard_False);
2677     aTexturedIO->EnableTextureModulate();
2678   }
2679   else
2680   {
2681     if (aMapOfArgs.Find ("SCALE", aValues))
2682     {
2683       if (aValues->Value(1) != "OFF")
2684       {
2685         aTexturedIO->SetTextureScale (Standard_True, aValues->Value(1).RealValue(), aValues->Value(2).RealValue());
2686       }
2687       else
2688       {
2689         aTexturedIO->SetTextureScale (Standard_False);
2690       }
2691     }
2692
2693     if (aMapOfArgs.Find ("ORIGIN", aValues))
2694     {
2695       if (aValues->Value(1) != "OFF")
2696       {
2697         aTexturedIO->SetTextureOrigin (Standard_True, aValues->Value(1).RealValue(), aValues->Value(2).RealValue());
2698       }
2699       else
2700       {
2701         aTexturedIO->SetTextureOrigin (Standard_False);
2702       }
2703     }
2704
2705     if (aMapOfArgs.Find ("REPEAT", aValues))
2706     {
2707       if (aValues->Value(1) != "OFF")
2708       {
2709         aTexturedIO->SetTextureRepeat (Standard_True, aValues->Value(1).RealValue(), aValues->Value(2).RealValue());
2710       }
2711       else
2712       {
2713         aTexturedIO->SetTextureRepeat (Standard_False);
2714       }
2715     }
2716
2717     if (aMapOfArgs.Find ("MODULATE", aValues))
2718     {
2719       if (aValues->Value(1) == "ON")
2720       {
2721         aTexturedIO->EnableTextureModulate();
2722       }
2723       else
2724       {
2725         aTexturedIO->DisableTextureModulate();
2726       }
2727     }
2728   }
2729
2730   if (aTexturedIO->DisplayMode() == 3 || aPreviousMode == 3)
2731   {
2732     anAISContext->RecomputePrsOnly (aTexturedIO);
2733   }
2734   else
2735   {
2736     anAISContext->SetDisplayMode (aTexturedIO, 3, Standard_False);
2737     anAISContext->Display (aTexturedIO, Standard_True);
2738     anAISContext->Update (aTexturedIO,Standard_True);
2739   }
2740
2741   return 0;
2742 }
2743
2744 //==============================================================================
2745 //function : VDisplay2
2746 //author   : ege
2747 //purpose  : Display an object from its name
2748 //==============================================================================
2749 static int VDisplay2 (Draw_Interpretor& theDI,
2750                       Standard_Integer  theArgNb,
2751                       const char**      theArgVec)
2752 {
2753   Handle(AIS_InteractiveContext) aCtx = ViewerTest::GetAISContext();
2754   if (theArgNb < 2)
2755   {
2756     std::cout << theArgVec[0] << "Error: wrong syntax!\n";
2757     return 1;
2758   }
2759   else if (aCtx.IsNull())
2760   {
2761     ViewerTest::ViewerInit();
2762     std::cout << "Command vinit should be called before!\n";
2763     // return 1;
2764     aCtx = ViewerTest::GetAISContext();
2765   }
2766
2767   ViewerTest_AutoUpdater anUpdateTool (aCtx, ViewerTest::CurrentView());
2768   if (aCtx->HasOpenedContext())
2769   {
2770     aCtx->CloseLocalContext();
2771   }
2772
2773   Standard_Integer isMutable = -1;
2774   for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
2775   {
2776     const TCollection_AsciiString aName     = theArgVec[anArgIter];
2777     TCollection_AsciiString       aNameCase = aName;
2778     aNameCase.LowerCase();
2779     if (anUpdateTool.parseRedrawMode (aName))
2780     {
2781       continue;
2782     }
2783     else if (aNameCase == "-mutable")
2784     {
2785       isMutable = 1;
2786       continue;
2787     }
2788     else if (!GetMapOfAIS().IsBound2 (aName))
2789     {
2790       // create the AIS_Shape from a name
2791       const Handle(AIS_InteractiveObject) aShape = GetAISShapeFromName (aName.ToCString());
2792       if (!aShape.IsNull())
2793       {
2794         if (isMutable != -1)
2795         {
2796           aShape->SetMutable (isMutable == 1);
2797         }
2798         GetMapOfAIS().Bind (aShape, aName);
2799         aCtx->Display (aShape, Standard_False);
2800       }
2801       continue;
2802     }
2803
2804     Handle(Standard_Transient) anObj = GetMapOfAIS().Find2 (aName);
2805     if (anObj->IsKind (STANDARD_TYPE (AIS_InteractiveObject)))
2806     {
2807       Handle(AIS_InteractiveObject) aShape = Handle(AIS_InteractiveObject)::DownCast (anObj);
2808       if (isMutable != -1)
2809       {
2810         aShape->SetMutable (isMutable == 1);
2811       }
2812
2813       if (aShape->Type() == AIS_KOI_Datum)
2814       {
2815         aCtx->Display (aShape, Standard_False);
2816       }
2817       else
2818       {
2819         theDI << "Display " << aName.ToCString() << "\n";
2820         // get the Shape from a name
2821         TopoDS_Shape aNewShape = GetShapeFromName (aName.ToCString());
2822
2823         // update the Shape in the AIS_Shape
2824         Handle(AIS_Shape) aShapePrs = Handle(AIS_Shape)::DownCast(aShape);
2825         if (!aShapePrs.IsNull())
2826         {
2827           aShapePrs->Set (aNewShape);
2828         }
2829         aCtx->Redisplay (aShape, Standard_False);
2830         aCtx->Display   (aShape, Standard_False);
2831       }
2832     }
2833     else if (anObj->IsKind (STANDARD_TYPE (NIS_InteractiveObject)))
2834     {
2835       Handle(NIS_InteractiveObject) aShape = Handle(NIS_InteractiveObject)::DownCast (anObj);
2836       TheNISContext()->Display (aShape);
2837     }
2838   }
2839   return 0;
2840 }
2841
2842 //===============================================================================================
2843 //function : VUpdate
2844 //purpose  :
2845 //===============================================================================================
2846 static int VUpdate (Draw_Interpretor& /*theDi*/, Standard_Integer theArgsNb, const char** theArgVec)
2847 {
2848   Handle(AIS_InteractiveContext) aContextAIS = ViewerTest::GetAISContext();
2849   if (aContextAIS.IsNull())
2850   {
2851     std::cout << theArgVec[0] << "AIS context is not available.\n";
2852     return 1;
2853   }
2854
2855   if (theArgsNb < 2)
2856   {
2857     std::cout << theArgVec[0] << ": insufficient arguments. Type help for more information.\n";
2858     return 1;
2859   }
2860
2861   const ViewerTest_DoubleMapOfInteractiveAndName& anAISMap = GetMapOfAIS();
2862
2863   AIS_ListOfInteractive aListOfIO;
2864
2865   for (int anArgIt = 1; anArgIt < theArgsNb; ++anArgIt)
2866   {
2867     TCollection_AsciiString aName = TCollection_AsciiString (theArgVec[anArgIt]);
2868
2869     Handle(AIS_InteractiveObject) anAISObj;
2870     if (anAISMap.IsBound2 (aName))
2871     {
2872       anAISObj = Handle(AIS_InteractiveObject)::DownCast (anAISMap.Find2 (aName));
2873     }
2874
2875     if (anAISObj.IsNull())
2876     {
2877       std::cout << theArgVec[0] << ": no AIS interactive object named \"" << aName << "\".\n";
2878       return 1;
2879     }
2880
2881     aListOfIO.Append (anAISObj);
2882   }
2883
2884   AIS_ListIteratorOfListOfInteractive anIOIt (aListOfIO);
2885   for (; anIOIt.More(); anIOIt.Next())
2886   {
2887     aContextAIS->Update (anIOIt.Value(), Standard_False);
2888   }
2889
2890   aContextAIS->UpdateCurrentViewer();
2891
2892   return 0;
2893 }
2894
2895 //==============================================================================
2896 //function : VPerf
2897 //purpose  : Test the annimation of an object along a
2898 //           predifined trajectory
2899 //Draw arg : vperf ShapeName 1/0(Transfo/Location) 1/0(Primitives sensibles ON/OFF)
2900 //==============================================================================
2901
2902 static int VPerf(Draw_Interpretor& di, Standard_Integer , const char** argv) {
2903
2904   OSD_Timer myTimer;
2905   if (TheAISContext()->HasOpenedContext())
2906     TheAISContext()->CloseLocalContext();
2907
2908   Standard_Real Step=4*M_PI/180;
2909   Standard_Real Angle=0;
2910
2911   Handle(AIS_InteractiveObject) aIO;
2912   if (GetMapOfAIS().IsBound2(argv[1]))
2913     aIO = Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(argv[1]));
2914   if (aIO.IsNull())
2915     return 1;
2916
2917   Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast(aIO);
2918
2919   myTimer.Start();
2920
2921   if (Draw::Atoi(argv[3])==1 ) {
2922     di<<" Primitives sensibles OFF"<<"\n";
2923     TheAISContext()->Deactivate(aIO);
2924   }
2925   else {
2926     di<<" Primitives sensibles ON"<<"\n";
2927   }
2928   // Movement par transformation
2929   if(Draw::Atoi(argv[2]) ==1) {
2930     di<<" Calcul par Transformation"<<"\n";
2931     for (Standard_Real myAngle=0;Angle<10*2*M_PI; myAngle++) {
2932
2933       Angle=Step*myAngle;
2934       gp_Trsf myTransfo;
2935       myTransfo.SetRotation(gp_Ax1(gp_Pnt(0,0,0),gp_Dir(0,0,1) ) ,Angle );
2936       TheAISContext()->SetLocation(aShape,myTransfo);
2937       TheAISContext() ->UpdateCurrentViewer();
2938
2939     }
2940   }
2941   else {
2942     di<<" Calcul par Locations"<<"\n";
2943     gp_Trsf myAngleTrsf;
2944     myAngleTrsf.SetRotation(gp_Ax1(gp_Pnt(0,0,0),gp_Dir(0,0,1) ), Step  );
2945     TopLoc_Location myDeltaAngle (myAngleTrsf);
2946     TopLoc_Location myTrueLoc;
2947
2948     for (Standard_Real myAngle=0;Angle<10*2*M_PI; myAngle++) {
2949
2950       Angle=Step*myAngle;
2951       myTrueLoc=myTrueLoc*myDeltaAngle;
2952       TheAISContext()->SetLocation(aShape,myTrueLoc );
2953       TheAISContext() ->UpdateCurrentViewer();
2954     }
2955   }
2956   if (Draw::Atoi(argv[3])==1 ){
2957     // On reactive la selection des primitives sensibles
2958     TheAISContext()->Activate(aIO,0);
2959   }
2960   a3DView() -> Redraw();
2961   myTimer.Stop();
2962   di<<" Temps ecoule "<<"\n";
2963   myTimer.Show();
2964   return 0;
2965 }
2966
2967
2968 //==================================================================================
2969 // Function : VAnimation
2970 //==================================================================================
2971 static int VAnimation (Draw_Interpretor& di, Standard_Integer argc, const char** argv) {
2972   if (argc != 5) {
2973     di<<"Use: "<<argv[0]<<" CrankArmFile CylinderHeadFile PropellerFile EngineBlockFile"<<"\n";
2974     return 1;
2975   }
2976
2977   Standard_Real thread = 4;
2978   Standard_Real angleA=0;
2979   Standard_Real angleB;
2980   Standard_Real X;
2981   gp_Ax1 Ax1(gp_Pnt(0,0,0),gp_Vec(0,0,1));
2982
2983   BRep_Builder B;
2984   TopoDS_Shape CrankArm;
2985   TopoDS_Shape CylinderHead;
2986   TopoDS_Shape Propeller;
2987   TopoDS_Shape EngineBlock;
2988
2989   //BRepTools::Read(CrankArm,"/dp_26/Indus/ege/assemblage/CrankArm.rle",B);
2990   //BRepTools::Read(CylinderHead,"/dp_26/Indus/ege/assemblage/CylinderHead.rle",B);
2991   //BRepTools::Read(Propeller,"/dp_26/Indus/ege/assemblage/Propeller.rle",B);
2992   //BRepTools::Read(EngineBlock,"/dp_26/Indus/ege/assemblage/EngineBlock.rle",B);
2993   BRepTools::Read(CrankArm,argv[1],B);
2994   BRepTools::Read(CylinderHead,argv[2],B);
2995   BRepTools::Read(Propeller,argv[3],B);
2996   BRepTools::Read(EngineBlock,argv[4],B);
2997
2998   if (CrankArm.IsNull() || CylinderHead.IsNull() || Propeller.IsNull() || EngineBlock.IsNull()) {di<<" Syntaxe error:loading failure."<<"\n";}
2999
3000
3001   OSD_Timer myTimer;
3002   myTimer.Start();
3003
3004   Handle(AIS_Shape) myAisCylinderHead = new AIS_Shape (CylinderHead);
3005   Handle(AIS_Shape) myAisEngineBlock  = new AIS_Shape (EngineBlock);
3006   Handle(AIS_Shape) myAisCrankArm     = new AIS_Shape (CrankArm);
3007   Handle(AIS_Shape) myAisPropeller    = new AIS_Shape (Propeller);
3008
3009   GetMapOfAIS().Bind(myAisCylinderHead,"a");
3010   GetMapOfAIS().Bind(myAisEngineBlock,"b");
3011   GetMapOfAIS().Bind(myAisCrankArm,"c");
3012   GetMapOfAIS().Bind(myAisPropeller,"d");
3013
3014   myAisCylinderHead->SetMutable (Standard_True);
3015   myAisEngineBlock ->SetMutable (Standard_True);
3016   myAisCrankArm    ->SetMutable (Standard_True);
3017   myAisPropeller   ->SetMutable (Standard_True);
3018
3019   TheAISContext()->SetColor (myAisCylinderHead, Quantity_NOC_INDIANRED);
3020   TheAISContext()->SetColor (myAisEngineBlock,  Quantity_NOC_RED);
3021   TheAISContext()->SetColor (myAisPropeller,    Quantity_NOC_GREEN);
3022
3023   TheAISContext()->Display (myAisCylinderHead, Standard_False);
3024   TheAISContext()->Display (myAisEngineBlock,  Standard_False);
3025   TheAISContext()->Display (myAisCrankArm,     Standard_False);
3026   TheAISContext()->Display (myAisPropeller,    Standard_False);
3027
3028   TheAISContext()->Deactivate(myAisCylinderHead);
3029   TheAISContext()->Deactivate(myAisEngineBlock );
3030   TheAISContext()->Deactivate(myAisCrankArm    );
3031   TheAISContext()->Deactivate(myAisPropeller   );
3032
3033   // Boucle de mouvement
3034   for (Standard_Real myAngle = 0;angleA<2*M_PI*10.175 ;myAngle++) {
3035
3036     angleA = thread*myAngle*M_PI/180;
3037     X = Sin(angleA)*3/8;
3038     angleB = atan(X / Sqrt(-X * X + 1));
3039     Standard_Real decal(25*0.6);
3040
3041
3042     //Build a transformation on the display
3043     gp_Trsf aPropellerTrsf;
3044     aPropellerTrsf.SetRotation(Ax1,angleA);
3045     TheAISContext()->SetLocation(myAisPropeller,aPropellerTrsf);
3046
3047     gp_Ax3 base(gp_Pnt(3*decal*(1-Cos(angleA)),-3*decal*Sin(angleA),0),gp_Vec(0,0,1),gp_Vec(1,0,0));
3048     gp_Trsf aCrankArmTrsf;
3049     aCrankArmTrsf.SetTransformation(   base.Rotated(gp_Ax1(gp_Pnt(3*decal,0,0),gp_Dir(0,0,1)),angleB));
3050     TheAISContext()->SetLocation(myAisCrankArm,aCrankArmTrsf);
3051
3052     TheAISContext()->UpdateCurrentViewer();
3053   }
3054
3055   TopoDS_Shape myNewCrankArm  =myAisCrankArm ->Shape().Located( myAisCrankArm ->Transformation() );
3056   TopoDS_Shape myNewPropeller =myAisPropeller->Shape().Located( myAisPropeller->Transformation() );
3057
3058   myAisCrankArm ->ResetTransformation();
3059   myAisPropeller->ResetTransformation();
3060
3061   myAisCrankArm  -> Set(myNewCrankArm );
3062   myAisPropeller -> Set(myNewPropeller);
3063
3064   TheAISContext()->Activate(myAisCylinderHead,0);
3065   TheAISContext()->Activate(myAisEngineBlock,0 );
3066   TheAISContext()->Activate(myAisCrankArm ,0   );
3067   TheAISContext()->Activate(myAisPropeller ,0  );
3068
3069   myTimer.Stop();
3070   myTimer.Show();
3071   myTimer.Start();
3072
3073   TheAISContext()->Redisplay(myAisCrankArm ,Standard_False);
3074   TheAISContext()->Redisplay(myAisPropeller,Standard_False);
3075
3076   TheAISContext()->UpdateCurrentViewer();
3077   a3DView()->Redraw();
3078
3079   myTimer.Stop();
3080   myTimer.Show();
3081
3082   return 0;
3083
3084 }
3085
3086 //==============================================================================
3087 //function : VShading
3088 //purpose  : Sharpen or roughten the quality of the shading
3089 //Draw arg : vshading ShapeName 0.1->0.00001  1 deg-> 30 deg
3090 //==============================================================================
3091 static int VShading(Draw_Interpretor& ,Standard_Integer argc, const char** argv)
3092 {
3093   Standard_Real    myDevCoef;
3094   Handle(AIS_InteractiveObject) TheAisIO;
3095
3096   // Verifications
3097   const Standard_Boolean HaveToSet = (strcasecmp(argv[0],"vsetshading") == 0);
3098
3099   if (TheAISContext()->HasOpenedContext())
3100     TheAISContext()->CloseLocalContext();
3101
3102   if (argc < 3) {
3103     myDevCoef  = 0.0008;
3104   } else {
3105     myDevCoef  =Draw::Atof(argv[2]);
3106   }
3107
3108   TCollection_AsciiString name=argv[1];
3109   if (GetMapOfAIS().IsBound2(name ))
3110     TheAisIO = Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(name));
3111   if (TheAisIO.IsNull())
3112     TheAisIO=GetAISShapeFromName((const char *)name.ToCString());
3113
3114   if (HaveToSet)
3115     TheAISContext()->SetDeviationCoefficient(TheAisIO,myDevCoef,Standard_True);
3116   else
3117     TheAISContext()->SetDeviationCoefficient(TheAisIO,0.0008,Standard_True);
3118
3119   TheAISContext()->Redisplay(TheAisIO);
3120   return 0;
3121 }
3122 //==============================================================================
3123 //function : HaveMode
3124 //use      : VActivatedModes
3125 //==============================================================================
3126 #include <TColStd_ListIteratorOfListOfInteger.hxx>
3127
3128 Standard_Boolean  HaveMode(const Handle(AIS_InteractiveObject)& TheAisIO,const Standard_Integer mode  )
3129 {
3130   TColStd_ListOfInteger List;
3131   TheAISContext()->ActivatedModes (TheAisIO,List);
3132   TColStd_ListIteratorOfListOfInteger it;
3133   Standard_Boolean Found=Standard_False;
3134   for (it.Initialize(List); it.More()&&!Found; it.Next() ){
3135     if (it.Value()==mode ) Found=Standard_True;
3136   }
3137   return Found;
3138 }
3139
3140
3141
3142 //==============================================================================
3143 //function : VActivatedMode
3144 //author   : ege
3145 //purpose  : permet d'attribuer a chacune des shapes un mode d'activation
3146 //           (edges,vertex...)qui lui est propre et le mode de selection standard.
3147 //           La fonction s'applique aux shapes selectionnees(current ou selected dans le viewer)
3148 //             Dans le cas ou on veut psser la shape en argument, la fonction n'autorise
3149 //           qu'un nom et qu'un mode.
3150 //Draw arg : vsetam  [ShapeName] mode(0,1,2,3,4,5,6,7)
3151 //==============================================================================
3152 #include <AIS_ListIteratorOfListOfInteractive.hxx>
3153
3154 static int VActivatedMode (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
3155
3156 {
3157   Standard_Boolean ThereIsName = Standard_False ;
3158
3159   if(!a3DView().IsNull()){
3160
3161     const Standard_Boolean HaveToSet = (strcasecmp(argv[0],"vsetam") == 0);
3162     // verification des arguments
3163     if (HaveToSet) {
3164       if (argc<2||argc>3) { di<<" Syntaxe error"<<"\n";return 1;}
3165       ThereIsName = (argc == 3);
3166     }
3167     else {
3168       // vunsetam
3169       if (argc>1) {di<<" Syntaxe error"<<"\n";return 1;}
3170       else {
3171         di<<" R.A.Z de tous les modes de selecion"<<"\n";
3172         di<<" Fermeture du Context local"<<"\n";
3173         if (TheAISContext()->HasOpenedContext())
3174           TheAISContext()->CloseLocalContext();
3175       }
3176     }
3177
3178     // IL n'y a aps de nom de shape passe en argument
3179     if (HaveToSet && !ThereIsName){
3180       Standard_Integer aMode=Draw::Atoi(argv [1]);
3181
3182       const char *cmode="???";
3183       switch (aMode) {
3184       case 0: cmode = "Shape"; break;
3185       case 1: cmode = "Vertex"; break;
3186       case 2: cmode = "Edge"; break;
3187       case 3: cmode = "Wire"; break;
3188       case 4: cmode = "Face"; break;
3189       case 5: cmode = "Shell"; break;
3190       case 6: cmode = "Solid"; break;
3191       case 7: cmode = "Compound"; break;
3192       }
3193
3194       if( !TheAISContext()->HasOpenedContext() ) {
3195         // il n'y a pas de Context local d'ouvert
3196         // on en ouvre un et on charge toutes les shapes displayees
3197         // on load tous les objets displayees et on Activate les objets de la liste
3198         AIS_ListOfInteractive ListOfIO;
3199         // on sauve dans une AISListOfInteractive tous les objets currents
3200         if (TheAISContext()->NbCurrents()>0 ){
3201           TheAISContext()->UnhilightCurrents(Standard_False);
3202
3203           for (TheAISContext()->InitCurrent(); TheAISContext()->MoreCurrent(); TheAISContext()->NextCurrent() ){
3204             ListOfIO.Append(TheAISContext()->Current() );
3205           }
3206         }
3207
3208         TheAISContext()->OpenLocalContext(Standard_False);
3209         ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName
3210           it (GetMapOfAIS());
3211         while(it.More()){
3212           Handle(AIS_InteractiveObject) aIO =
3213             Handle(AIS_InteractiveObject)::DownCast(it.Key1());
3214           if (!aIO.IsNull())
3215             TheAISContext()->Load(aIO,0,Standard_False);
3216           it.Next();
3217         }
3218         // traitement des objets qui etaient currents dans le Contexte global
3219         if (!ListOfIO.IsEmpty() ) {
3220           // il y avait des objets currents
3221           AIS_ListIteratorOfListOfInteractive iter;
3222           for (iter.Initialize(ListOfIO); iter.More() ; iter.Next() ) {
3223             Handle(AIS_InteractiveObject) aIO=iter.Value();
3224             TheAISContext()->Activate(aIO,aMode);
3225             di<<" Mode: "<<cmode<<" ON pour "<<GetMapOfAIS().Find1(aIO).ToCString()  <<"\n";
3226           }
3227         }
3228         else {
3229           // On applique le mode a tous les objets displayes
3230           ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName
3231             it2 (GetMapOfAIS());
3232           while(it2.More()){
3233             Handle(AIS_InteractiveObject) aIO =
3234               Handle(AIS_InteractiveObject)::DownCast(it2.Key1());
3235             if (!aIO.IsNull()) {
3236               di<<" Mode: "<<cmode<<" ON pour "<<it2.Key2().ToCString() <<"\n";
3237               TheAISContext()->Activate(aIO,aMode);
3238             }
3239             it2.Next();
3240           }
3241         }
3242
3243       }
3244
3245       else {
3246         // un Context local est deja ouvert
3247         // Traitement des objets du Context local
3248         if (TheAISContext()->NbSelected()>0 ){
3249           TheAISContext()->UnhilightSelected(Standard_False);
3250           // il y a des objets selected,on les parcourt
3251           for (TheAISContext()->InitSelected(); TheAISContext()->MoreSelected(); TheAISContext()->NextSelected() ){
3252             Handle(AIS_InteractiveObject) aIO=TheAISContext()->Interactive();
3253
3254
3255             if (HaveMode(aIO,aMode) ) {
3256               di<<" Mode: "<<cmode<<" OFF pour "<<GetMapOfAIS().Find1(aIO).ToCString() <<"\n";
3257               TheAISContext()->Deactivate(aIO,aMode);
3258             }
3259             else{
3260               di<<" Mode: "<<cmode<<" ON pour "<<GetMapOfAIS().Find1(aIO).ToCString() <<"\n";
3261               TheAISContext()->Activate(aIO,aMode);
3262             }
3263
3264           }
3265         }
3266         else{
3267           // il n'y a pas d'objets selected
3268           // tous les objets diplayes sont traites
3269           ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName
3270             it (GetMapOfAIS());
3271           while(it.More()){
3272             Handle(AIS_InteractiveObject) aIO =
3273               Handle(AIS_InteractiveObject)::DownCast(it.Key1());
3274             if (!aIO.IsNull()) {
3275               if (HaveMode(aIO,aMode) ) {
3276                 di<<" Mode: "<<cmode<<" OFF pour "
3277                   <<GetMapOfAIS().Find1(aIO).ToCString() <<"\n";
3278                 TheAISContext()->Deactivate(aIO,aMode);
3279               }
3280               else{
3281                 di<<" Mode: "<<cmode<<" ON pour"
3282                   <<GetMapOfAIS().Find1(aIO).ToCString() <<"\n";
3283                 TheAISContext()->Activate(aIO,aMode);
3284               }
3285             }
3286             it.Next();
3287           }
3288         }
3289       }
3290     }
3291     else if (HaveToSet && ThereIsName){
3292       Standard_Integer aMode=Draw::Atoi(argv [2]);
3293       Handle(AIS_InteractiveObject) aIO =
3294         Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(argv[1]));
3295
3296       if (!aIO.IsNull()) {
3297         const char *cmode="???";
3298
3299         switch (aMode) {
3300         case 0: cmode = "Shape"; break;
3301         case 1: cmode = "Vertex"; break;
3302         case 2: cmode = "Edge"; break;
3303         case 3: cmode = "Wire"; break;
3304         case 4: cmode = "Face"; break;
3305         case 5: cmode = "Shell"; break;
3306         case 6: cmode = "Solid"; break;
3307         case 7: cmode = "Compound"; break;
3308         }
3309
3310         if( !TheAISContext()->HasOpenedContext() ) {
3311           TheAISContext()->OpenLocalContext(Standard_False);
3312           // On charge tous les objets de la map
3313           ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName it (GetMapOfAIS());
3314           while(it.More()){
3315             Handle(AIS_InteractiveObject) aShape=
3316               Handle(AIS_InteractiveObject)::DownCast(it.Key1());
3317             if (!aShape.IsNull())
3318               TheAISContext()->Load(aShape,0,Standard_False);
3319             it.Next();
3320           }
3321           TheAISContext()->Activate(aIO,aMode);
3322           di<<" Mode: "<<cmode<<" ON pour "<<argv[1]<<"\n";
3323         }
3324
3325         else {
3326           // un Context local est deja ouvert
3327           if (HaveMode(aIO,aMode) ) {
3328             di<<" Mode: "<<cmode<<" OFF pour "<<argv[1]<<"\n";
3329             TheAISContext()->Deactivate(aIO,aMode);
3330           }
3331           else{
3332             di<<" Mode: "<<cmode<<" ON pour "<<argv[1]<<"\n";
3333             TheAISContext()->Activate(aIO,aMode);
3334           }
3335         }
3336       }
3337     }
3338   }
3339   return 0;
3340 }
3341
3342 //! Auxiliary method to print Interactive Object information
3343 static void objInfo (const NCollection_Map<Handle(AIS_InteractiveObject)>& theDetected,
3344                      const Handle(Standard_Transient)&                     theObject,
3345                      Draw_Interpretor&                                     theDI)
3346 {
3347   const Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (theObject);
3348   if (anObj.IsNull())
3349   {
3350     theDI << theObject->DynamicType()->Name() << " is not AIS presentation\n";
3351     return;
3352   }
3353
3354   theDI << (TheAISContext()->IsDisplayed  (anObj) ? "Displayed"  : "Hidden   ")
3355         << (TheAISContext()->IsSelected   (anObj) ? " Selected" : "         ")
3356         << (theDetected.Contains (anObj)          ? " Detected" : "         ")
3357         << " Type: ";
3358   if (anObj->Type() == AIS_KOI_Datum)
3359   {
3360     // AIS_Datum
3361     if      (anObj->Signature() == 3) { theDI << " AIS_Trihedron"; }
3362     else if (anObj->Signature() == 2) { theDI << " AIS_Axis"; }
3363     else if (anObj->Signature() == 6) { theDI << " AIS_Circle"; }
3364     else if (anObj->Signature() == 5) { theDI << " AIS_Line"; }
3365     else if (anObj->Signature() == 7) { theDI << " AIS_Plane"; }
3366     else if (anObj->Signature() == 1) { theDI << " AIS_Point"; }
3367     else if (anObj->Signature() == 4) { theDI << " AIS_PlaneTrihedron"; }
3368   }
3369   // AIS_Shape
3370   else if (anObj->Type()      == AIS_KOI_Shape
3371         && anObj->Signature() == 0)
3372   {
3373     theDI << " AIS_Shape";
3374   }
3375   else if (anObj->Type() == AIS_KOI_Relation)
3376   {
3377     // AIS_Dimention and AIS_Relation
3378     Handle(AIS_Relation) aRelation = Handle(AIS_Relation)::DownCast (anObj);
3379     switch (aRelation->KindOfDimension())
3380     {
3381       case AIS_KOD_PLANEANGLE:     theDI << " AIS_AngleDimension"; break;
3382       case AIS_KOD_LENGTH:         theDI << " AIS_Chamf2/3dDimension/AIS_LengthDimension"; break;
3383       case AIS_KOD_DIAMETER:       theDI << " AIS_DiameterDimension"; break;
3384       case AIS_KOD_ELLIPSERADIUS:  theDI << " AIS_EllipseRadiusDimension"; break;
3385       //case AIS_KOD_FILLETRADIUS:   theDI << " AIS_FilletRadiusDimension "; break;
3386       case AIS_KOD_OFFSET:         theDI << " AIS_OffsetDimension"; break;
3387       case AIS_KOD_RADIUS:         theDI << " AIS_RadiusDimension"; break;
3388       default:                     theDI << " UNKNOWN dimension"; break;
3389     }
3390   }
3391   else
3392   {
3393     theDI << " UserPrs";
3394   }
3395   theDI << " (" << theObject->DynamicType()->Name() << ")";
3396 }
3397
3398 //! Print information about locally selected sub-shapes
3399 static void localCtxInfo (Draw_Interpretor& theDI)
3400 {
3401   Handle(AIS_InteractiveContext) aCtx = TheAISContext();
3402   if (!aCtx->HasOpenedContext())
3403   {
3404     return;
3405   }
3406
3407   TCollection_AsciiString aPrevName;
3408   Handle(AIS_LocalContext) aCtxLoc = aCtx->LocalContext();
3409   for (aCtxLoc->InitSelected(); aCtxLoc->MoreSelected(); aCtxLoc->NextSelected())
3410   {
3411     const TopoDS_Shape      aSubShape = aCtxLoc->SelectedShape();
3412     const Handle(AIS_Shape) aShapeIO  = Handle(AIS_Shape)::DownCast (aCtxLoc->SelectedInteractive());
3413     if (aSubShape.IsNull()
3414       || aShapeIO.IsNull()
3415       || !GetMapOfAIS().IsBound1 (aShapeIO))
3416     {
3417       continue;
3418     }
3419
3420     const TCollection_AsciiString aParentName = GetMapOfAIS().Find1 (aShapeIO);
3421     TopTools_MapOfShape aFilter;
3422     Standard_Integer    aNumber = 0;
3423     const TopoDS_Shape  aShape  = aShapeIO->Shape();
3424     for (TopExp_Explorer anIter (aShape, aSubShape.ShapeType());
3425          anIter.More(); anIter.Next())
3426     {
3427       if (!aFilter.Add (anIter.Current()))
3428       {
3429         continue; // filter duplicates
3430       }
3431
3432       ++aNumber;
3433       if (!anIter.Current().IsSame (aSubShape))
3434       {
3435         continue;
3436       }
3437
3438       Standard_CString aShapeName = NULL;
3439       switch (aSubShape.ShapeType())
3440       {
3441         case TopAbs_COMPOUND:  aShapeName = " Compound"; break;
3442         case TopAbs_COMPSOLID: aShapeName = "CompSolid"; break;
3443         case TopAbs_SOLID:     aShapeName = "    Solid"; break;
3444         case TopAbs_SHELL:     aShapeName = "    Shell"; break;
3445         case TopAbs_FACE:      aShapeName = "     Face"; break;
3446         case TopAbs_WIRE:      aShapeName = "     Wire"; break;
3447         case TopAbs_EDGE:      aShapeName = "     Edge"; break;
3448         case TopAbs_VERTEX:    aShapeName = "   Vertex"; break;
3449         default:
3450         case TopAbs_SHAPE:     aShapeName = "    Shape"; break;
3451       }
3452
3453       if (aParentName != aPrevName)
3454       {
3455         theDI << "Locally selected sub-shapes within " << aParentName << ":\n";
3456         aPrevName = aParentName;
3457       }
3458       theDI << "  " << aShapeName << " #" << aNumber << "\n";
3459       break;
3460     }
3461   }
3462 }
3463
3464 //==============================================================================
3465 //function : VState
3466 //purpose  :
3467 //==============================================================================
3468 static Standard_Integer VState (Draw_Interpretor& theDI,
3469                                 Standard_Integer  theArgNb,
3470                                 Standard_CString* theArgVec)
3471 {
3472   Handle(AIS_InteractiveContext) aCtx = TheAISContext();
3473   if (aCtx.IsNull())
3474   {
3475     std::cerr << "Error: No opened viewer!\n";
3476     return 1;
3477   }
3478
3479   TCollection_AsciiString anOption (theArgNb >= 2 ? theArgVec[1] : "");
3480   anOption.LowerCase();
3481   if (anOption == "-detectedEntities"
3482    || anOption == "-entities")
3483   {
3484     theDI << "Detected entities:\n";
3485     Handle(StdSelect_ViewerSelector3d) aSelector = aCtx->HasOpenedContext() ? aCtx->LocalSelector() : aCtx->MainSelector();
3486     for (aSelector->Init(); aSelector->More(); aSelector->Next())
3487     {
3488       Handle(SelectBasics_SensitiveEntity) anEntity = aSelector->Primitive (0);
3489       Standard_Real aMatchDMin  = 0.0;
3490       Standard_Real aMatchDepth = Precision::Infinite();
3491       anEntity->Matches (aSelector->LastPickingArguments(), aMatchDMin, aMatchDepth);
3492
3493       Handle(SelectMgr_EntityOwner) anOwner    = Handle(SelectMgr_EntityOwner)::DownCast (anEntity->OwnerId());
3494       Handle(AIS_InteractiveObject) anObj      = Handle(AIS_InteractiveObject)::DownCast (anOwner->Selectable());
3495
3496       const gp_Lin aLine = aSelector->LastPickingArguments().PickLine();
3497       const gp_Pnt aPnt  = aLine.Location().Translated (gp_Vec (aLine.Direction()) * aMatchDepth);
3498
3499       TCollection_AsciiString aName = GetMapOfAIS().Find1 (anObj);
3500       aName.LeftJustify (20, ' ');
3501       char anInfoStr[512];
3502       Sprintf (anInfoStr, " Depth: %+.3f Distance: %+.3f Point: %+.3f %+.3f %+.3f", aMatchDepth, aMatchDMin, aPnt.X(), aPnt.Y(), aPnt.Z());
3503       theDI << "  " << aName
3504             << anInfoStr
3505             << " (" << anEntity->DynamicType()->Name() << ")"
3506             << "\n";
3507
3508       Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast (anOwner);
3509       if (!aBRepOwner.IsNull())
3510       {
3511         theDI << "                       Detected Shape: "
3512               << aBRepOwner->Shape().TShape()->DynamicType()->Name()
3513               << "\n";
3514       }
3515
3516       Handle(Select3D_SensitiveWire) aWire = Handle(Select3D_SensitiveWire)::DownCast (anEntity);
3517       if (!aWire.IsNull())
3518       {
3519         Handle(Select3D_SensitiveEntity) aSen = aWire->GetLastDetected();
3520         theDI << "                       Detected Child: "
3521               << aSen->DynamicType()->Name()
3522               << "\n";
3523       }
3524     }
3525     return 0;
3526   }
3527
3528   NCollection_Map<Handle(AIS_InteractiveObject)> aDetected;
3529   for (aCtx->InitDetected(); aCtx->MoreDetected(); aCtx->NextDetected())
3530   {
3531     aDetected.Add (aCtx->DetectedCurrentObject());
3532   }
3533
3534   const Standard_Boolean toShowAll = (theArgNb >= 2 && *theArgVec[1] == '*');
3535   if (theArgNb >= 2
3536    && !toShowAll)
3537   {
3538     for (Standard_Integer anArgIter = 1; anArgIter < theArgNb; ++anArgIter)
3539     {
3540       const TCollection_AsciiString anObjName = theArgVec[anArgIter];
3541       if (!GetMapOfAIS().IsBound2 (anObjName))
3542       {
3543         theDI << anObjName << " doesn't exist!\n";
3544         continue;
3545       }
3546
3547       const Handle(Standard_Transient) anObjTrans = GetMapOfAIS().Find2 (anObjName);
3548       TCollection_AsciiString aName = anObjName;
3549       aName.LeftJustify (20, ' ');
3550       theDI << "  " << aName << " ";
3551       objInfo (aDetected, anObjTrans, theDI);
3552       theDI << "\n";
3553     }
3554     return 0;
3555   }
3556
3557   if (aCtx->NbCurrents() > 0
3558    && !toShowAll)
3559   {
3560     for (aCtx->InitCurrent(); aCtx->MoreCurrent(); aCtx->NextCurrent())
3561     {
3562       Handle(AIS_InteractiveObject) anObj = aCtx->Current();
3563       TCollection_AsciiString aName = GetMapOfAIS().Find1 (anObj);
3564       aName.LeftJustify (20, ' ');
3565       theDI << aName << " ";
3566       objInfo (aDetected, anObj, theDI);
3567       theDI << "\n";
3568     }
3569     return 0;
3570   }
3571
3572   theDI << "Neutral-point state:\n";
3573   for (ViewerTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName anObjIter (GetMapOfAIS());
3574        anObjIter.More(); anObjIter.Next())
3575   {
3576     Handle(AIS_InteractiveObject) anObj = Handle(AIS_InteractiveObject)::DownCast (anObjIter.Key1());
3577     if (anObj.IsNull())
3578     {
3579       continue;
3580     }
3581
3582     TCollection_AsciiString aName = anObjIter.Key2();
3583     aName.LeftJustify (20, ' ');
3584     theDI << "  " << aName << " ";
3585     objInfo (aDetected, anObj, theDI);
3586     theDI << "\n";
3587   }
3588   localCtxInfo (theDI);
3589   return 0;
3590 }
3591
3592 //=======================================================================
3593 //function : PickObjects
3594 //purpose  :
3595 //=======================================================================
3596 Standard_Boolean  ViewerTest::PickObjects(Handle(TColStd_HArray1OfTransient)& arr,
3597                                           const AIS_KindOfInteractive TheType,
3598                                           const Standard_Integer TheSignature,
3599                                           const Standard_Integer MaxPick)
3600 {
3601   Handle(AIS_InteractiveObject) IO;
3602   Standard_Integer curindex = (TheType == AIS_KOI_None) ? 0 : TheAISContext()->OpenLocalContext();
3603
3604   // step 1: prepare the data
3605   if(curindex !=0){
3606     Handle(AIS_SignatureFilter) F1 = new AIS_SignatureFilter(TheType,TheSignature);
3607     TheAISContext()->AddFilter(F1);
3608   }
3609
3610   // step 2 : wait for the selection...
3611 //  Standard_Boolean IsGood (Standard_False);
3612 //  Standard_Integer NbPick(0);
3613   Standard_Boolean NbPickGood (0),NbToReach(arr->Length());
3614   Standard_Integer NbPickFail(0);
3615   Standard_Integer argccc = 5;
3616   const char *bufff[] = { "A", "B", "C","D", "E" };
3617   const char **argvvv = (const char **) bufff;
3618
3619
3620   while(NbPickGood<NbToReach && NbPickFail <= MaxPick){
3621     while(ViewerMainLoop(argccc,argvvv)){}
3622     Standard_Integer NbStored = TheAISContext()->NbSelected();
3623     if((unsigned int ) NbStored != NbPickGood)
3624       NbPickGood= NbStored;
3625     else
3626       NbPickFail++;
3627     cout<<"NbPicked =  "<<NbPickGood<<" |  Nb Pick Fail :"<<NbPickFail<<endl;
3628   }
3629
3630   // step3 get result.
3631
3632   if((unsigned int ) NbPickFail >= NbToReach) return Standard_False;
3633
3634   Standard_Integer i(0);
3635   for(TheAISContext()->InitSelected();
3636       TheAISContext()->MoreSelected();
3637       TheAISContext()->NextSelected()){
3638     i++;
3639     Handle(AIS_InteractiveObject) IO2 = TheAISContext()->SelectedInteractive();
3640     arr->SetValue(i,IO2);
3641   }
3642
3643
3644   if(curindex>0)
3645     TheAISContext()->CloseLocalContext(curindex);
3646
3647   return Standard_True;
3648 }
3649
3650
3651 //=======================================================================
3652 //function : PickObject
3653 //purpose  :
3654 //=======================================================================
3655 Handle(AIS_InteractiveObject) ViewerTest::PickObject(const AIS_KindOfInteractive TheType,
3656                                                      const Standard_Integer TheSignature,
3657                                                      const Standard_Integer MaxPick)
3658 {
3659   Handle(AIS_InteractiveObject) IO;
3660   Standard_Integer curindex = (TheType == AIS_KOI_None) ? 0 : TheAISContext()->OpenLocalContext();
3661
3662   // step 1: prepare the data
3663
3664   if(curindex !=0){
3665     Handle(AIS_SignatureFilter) F1 = new AIS_SignatureFilter(TheType,TheSignature);
3666     TheAISContext()->AddFilter(F1);
3667   }
3668
3669   // step 2 : wait for the selection...
3670   Standard_Boolean IsGood (Standard_False);
3671   Standard_Integer NbPick(0);
3672   Standard_Integer argccc = 5;
3673   const char *bufff[] = { "VPick", "X", "VPickY","VPickZ", "VPickShape" };
3674   const char **argvvv = (const char **) bufff;
3675
3676
3677   while(!IsGood && NbPick<= MaxPick){
3678     while(ViewerMainLoop(argccc,argvvv)){}
3679     IsGood = (TheAISContext()->NbSelected()>0) ;
3680     NbPick++;
3681     cout<<"Nb Pick :"<<NbPick<<endl;
3682   }
3683
3684
3685   // step3 get result.
3686   if(IsGood){
3687     TheAISContext()->InitSelected();
3688     IO = TheAISContext()->SelectedInteractive();
3689   }
3690
3691   if(curindex!=0)
3692     TheAISContext()->CloseLocalContext(curindex);
3693   return IO;
3694 }
3695
3696 //=======================================================================
3697 //function : PickShape
3698 //purpose  : First Activate the rightmode + Put Filters to be able to
3699 //           pick objets that are of type <TheType>...
3700 //=======================================================================
3701
3702 TopoDS_Shape ViewerTest::PickShape(const TopAbs_ShapeEnum TheType,
3703                                    const Standard_Integer MaxPick)
3704 {
3705
3706   // step 1: prepare the data
3707
3708   Standard_Integer curindex = TheAISContext()->OpenLocalContext();
3709   TopoDS_Shape result;
3710
3711   if(TheType==TopAbs_SHAPE){
3712     Handle(AIS_TypeFilter) F1 = new AIS_TypeFilter(AIS_KOI_Shape);
3713     TheAISContext()->AddFilter(F1);
3714   }
3715   else{
3716     Handle(StdSelect_ShapeTypeFilter) TF = new StdSelect_ShapeTypeFilter(TheType);
3717     TheAISContext()->AddFilter(TF);
3718     TheAISContext()->ActivateStandardMode(TheType);
3719
3720   }
3721
3722
3723   // step 2 : wait for the selection...
3724   Standard_Boolean NoShape (Standard_True);
3725   Standard_Integer NbPick(0);
3726   Standard_Integer argccc = 5;
3727   const char *bufff[] = { "VPick", "X", "VPickY","VPickZ", "VPickShape" };
3728   const char **argvvv = (const char **) bufff;
3729
3730
3731   while(NoShape && NbPick<= MaxPick){
3732     while(ViewerMainLoop(argccc,argvvv)){}
3733     NoShape = (TheAISContext()->NbSelected()==0) ;
3734     NbPick++;
3735     cout<<"Nb Pick :"<<NbPick<<endl;
3736   }
3737
3738   // step3 get result.
3739
3740   if(!NoShape){
3741
3742     TheAISContext()->InitSelected();
3743     if(TheAISContext()->HasSelectedShape())
3744       result = TheAISContext()->SelectedShape();
3745     else{
3746       Handle(AIS_InteractiveObject) IO = TheAISContext()->SelectedInteractive();
3747       result = (*((Handle(AIS_Shape)*) &IO))->Shape();
3748     }
3749   }
3750
3751   if(curindex>0)
3752     TheAISContext()->CloseLocalContext(curindex);
3753
3754   return result;
3755 }
3756
3757
3758 //=======================================================================
3759 //function : PickShapes
3760 //purpose  :
3761 //=======================================================================
3762 Standard_Boolean ViewerTest::PickShapes (const TopAbs_ShapeEnum TheType,
3763                                          Handle(TopTools_HArray1OfShape)& thearr,
3764                                          const Standard_Integer MaxPick)
3765 {
3766
3767   Standard_Integer Taille = thearr->Length();
3768   if(Taille>1)
3769     cout<<" WARNING : Pick with Shift+ MB1 for Selection of more than 1 object"<<"\n";
3770
3771   // step 1: prepare the data
3772   Standard_Integer curindex = TheAISContext()->OpenLocalContext();
3773   if(TheType==TopAbs_SHAPE){
3774     Handle(AIS_TypeFilter) F1 = new AIS_TypeFilter(AIS_KOI_Shape);
3775     TheAISContext()->AddFilter(F1);
3776   }
3777   else{
3778     Handle(StdSelect_ShapeTypeFilter) TF = new StdSelect_ShapeTypeFilter(TheType);
3779     TheAISContext()->AddFilter(TF);
3780     TheAISContext()->ActivateStandardMode(TheType);
3781
3782   }
3783
3784   // step 2 : wait for the selection...
3785
3786   Standard_Boolean NbPickGood (0),NbToReach(thearr->Length());
3787   Standard_Integer NbPickFail(0);
3788   Standard_Integer argccc = 5;
3789   const char *bufff[] = { "A", "B", "C","D", "E" };
3790   const char **argvvv = (const char **) bufff;
3791
3792
3793   while(NbPickGood<NbToReach && NbPickFail <= MaxPick){
3794     while(ViewerMainLoop(argccc,argvvv)){}
3795     Standard_Integer NbStored = TheAISContext()->NbSelected();
3796     if((unsigned int ) NbStored != NbPickGood)
3797       NbPickGood= NbStored;
3798     else
3799       NbPickFail++;
3800     cout<<"NbPicked =  "<<NbPickGood<<" |  Nb Pick Fail :"<<NbPickFail<<"\n";
3801   }
3802
3803   // step3 get result.
3804
3805   if((unsigned int ) NbPickFail >= NbToReach) return Standard_False;
3806
3807   Standard_Integer i(0);
3808   for(TheAISContext()->InitSelected();TheAISContext()->MoreSelected();TheAISContext()->NextSelected()){
3809     i++;
3810     if(TheAISContext()->HasSelectedShape())
3811       thearr->SetValue(i,TheAISContext()->SelectedShape());
3812     else{
3813       Handle(AIS_InteractiveObject) IO = TheAISContext()->SelectedInteractive();
3814       thearr->SetValue(i,(*((Handle(AIS_Shape)*) &IO))->Shape());
3815     }
3816   }
3817
3818   TheAISContext()->CloseLocalContext(curindex);
3819   return Standard_True;
3820 }
3821
3822
3823 //=======================================================================
3824 //function : VPickShape
3825 //purpose  :
3826 //=======================================================================
3827 static int VPickShape( Draw_Interpretor& di, Standard_Integer argc, const char** argv)
3828 {
3829   TopoDS_Shape PickSh;
3830   TopAbs_ShapeEnum theType = TopAbs_COMPOUND;
3831
3832   if(argc==1)
3833     theType = TopAbs_SHAPE;
3834   else{
3835     if(!strcasecmp(argv[1],"V" )) theType = TopAbs_VERTEX;
3836     else if (!strcasecmp(argv[1],"E" )) theType = TopAbs_EDGE;
3837     else if (!strcasecmp(argv[1],"W" )) theType = TopAbs_WIRE;
3838     else if (!strcasecmp(argv[1],"F" )) theType = TopAbs_FACE;
3839     else if(!strcasecmp(argv[1],"SHAPE" )) theType = TopAbs_SHAPE;
3840     else if (!strcasecmp(argv[1],"SHELL" )) theType = TopAbs_SHELL;
3841     else if (!strcasecmp(argv[1],"SOLID" )) theType = TopAbs_SOLID;
3842   }
3843
3844   static Standard_Integer nbOfSub[8]={0,0,0,0,0,0,0,0};
3845   static TCollection_AsciiString nameType[8] = {"COMPS","SOL","SHE","F","W","E","V","SHAP"};
3846
3847   TCollection_AsciiString name;
3848
3849
3850   Standard_Integer NbToPick = argc>2 ? argc-2 : 1;
3851   if(NbToPick==1){
3852     PickSh = ViewerTest::PickShape(theType);
3853
3854     if(PickSh.IsNull())
3855       return 1;
3856     if(argc>2){
3857       name += argv[2];
3858     }
3859     else{
3860
3861       if(!PickSh.IsNull()){
3862         nbOfSub[Standard_Integer(theType)]++;
3863         name += "Picked_";
3864         name += nameType[Standard_Integer(theType)];
3865         TCollection_AsciiString indxstring(nbOfSub[Standard_Integer(theType)]);
3866         name +="_";
3867         name+=indxstring;
3868       }
3869     }
3870     // si on avait une petite methode pour voir si la shape
3871     // est deja dans la Double map, ca eviterait de creer....
3872     DBRep::Set(name.ToCString(),PickSh);
3873
3874     Handle(AIS_Shape) newsh = new AIS_Shape(PickSh);
3875     GetMapOfAIS().Bind(newsh, name);
3876     TheAISContext()->Display(newsh);
3877     di<<"Nom de la shape pickee : "<<name.ToCString()<<"\n";
3878   }
3879
3880   // Plusieurs objets a picker, vite vite vite....
3881   //
3882   else{
3883     Standard_Boolean autonaming = !strcasecmp(argv[2],".");
3884     Handle(TopTools_HArray1OfShape) arr = new TopTools_HArray1OfShape(1,NbToPick);
3885     if(ViewerTest::PickShapes(theType,arr)){
3886       for(Standard_Integer i=1;i<=NbToPick;i++){
3887         PickSh = arr->Value(i);
3888         if(!PickSh.IsNull()){
3889           if(autonaming){
3890             nbOfSub[Standard_Integer(theType)]++;
3891             name.Clear();
3892             name += "Picked_";
3893             name += nameType[Standard_Integer(theType)];
3894             TCollection_AsciiString indxstring(nbOfSub[Standard_Integer(theType)]);
3895             name +="_";
3896             name+=indxstring;
3897           }
3898         }
3899         else
3900           name = argv[1+i];
3901
3902         DBRep::Set(name.ToCString(),PickSh);
3903         Handle(AIS_Shape) newsh = new AIS_Shape(PickSh);
3904         GetMapOfAIS().Bind(newsh, name);
3905         di<<"display of picke shape #"<<i<<" - nom : "<<name.ToCString()<<"\n";
3906         TheAISContext()->Display(newsh);
3907
3908       }
3909     }
3910   }
3911   return 0;
3912 }
3913
3914 //=======================================================================
3915 //function : list of known objects
3916 //purpose  :
3917 //=======================================================================
3918 static int VIOTypes( Draw_Interpretor& di, Standard_Integer , const char** )
3919 {
3920   //                             1234567890         12345678901234567         123456789
3921   TCollection_AsciiString Colum [3]={"Standard Types","Type Of Object","Signature"};
3922   TCollection_AsciiString BlankLine(64,'_');
3923   Standard_Integer i ;
3924
3925   di<<"/n"<<BlankLine.ToCString()<<"\n";
3926
3927   for( i =0;i<=2;i++)
3928     Colum[i].Center(20,' ');
3929   for(i=0;i<=2;i++)
3930     di<<"|"<<Colum[i].ToCString();
3931   di<<"|"<<"\n";
3932
3933   di<<BlankLine.ToCString()<<"\n";
3934
3935   //  TCollection_AsciiString thetypes[5]={"Datum","Shape","Object","Relation","None"};
3936   const char ** names = GetTypeNames();
3937
3938   TCollection_AsciiString curstring;
3939   TCollection_AsciiString curcolum[3];
3940
3941
3942   // les objets de type Datum..
3943   curcolum[1]+="Datum";
3944   for(i =0;i<=6;i++){
3945     curcolum[0].Clear();
3946     curcolum[0] += names[i];
3947
3948     curcolum[2].Clear();
3949     curcolum[2]+=TCollection_AsciiString(i+1);
3950
3951     for(Standard_Integer j =0;j<=2;j++){
3952       curcolum[j].Center(20,' ');
3953       di<<"|"<<curcolum[j].ToCString();
3954     }
3955     di<<"|"<<"\n";
3956   }
3957   di<<BlankLine.ToCString()<<"\n";
3958
3959   // les objets de type shape
3960   curcolum[1].Clear();
3961   curcolum[1]+="Shape";
3962   curcolum[1].Center(20,' ');
3963
3964   for(i=0;i<=2;i++){
3965     curcolum[0].Clear();
3966     curcolum[0] += names[7+i];
3967     curcolum[2].Clear();
3968     curcolum[2]+=TCollection_AsciiString(i);
3969
3970     for(Standard_Integer j =0;j<=2;j++){
3971       curcolum[j].Center(20,' ');
3972       di<<"|"<<curcolum[j].ToCString();
3973     }
3974     di<<"|"<<"\n";
3975   }
3976   di<<BlankLine.ToCString()<<"\n";
3977   // les IO de type objet...
3978   curcolum[1].Clear();
3979   curcolum[1]+="Object";
3980   curcolum[1].Center(20,' ');
3981   for(i=0;i<=1;i++){
3982     curcolum[0].Clear();
3983     curcolum[0] += names[10+i];
3984     curcolum[2].Clear();
3985     curcolum[2]+=TCollection_AsciiString(i);
3986
3987     for(Standard_Integer j =0;j<=2;j++){
3988       curcolum[j].Center(20,' ');
3989       di<<"|"<<curcolum[j].ToCString();
3990     }
3991     di<<"|"<<"\n";
3992   }
3993   di<<BlankLine.ToCString()<<"\n";
3994   // les contraintes et dimensions.
3995   // pour l'instant on separe juste contraintes et dimensions...
3996   // plus tard, on detaillera toutes les sortes...
3997   curcolum[1].Clear();
3998   curcolum[1]+="Relation";
3999   curcolum[1].Center(20,' ');
4000   for(i=0;i<=1;i++){
4001     curcolum[0].Clear();
4002     curcolum[0] += names[12+i];
4003     curcolum[2].Clear();
4004     curcolum[2]+=TCollection_AsciiString(i);
4005
4006     for(Standard_Integer j =0;j<=2;j++){
4007       curcolum[j].Center(20,' ');
4008       di<<"|"<<curcolum[j].ToCString();
4009     }
4010     di<<"|"<<"\n";
4011   }
4012   di<<BlankLine.ToCString()<<"\n";
4013
4014
4015   return 0;
4016 }
4017
4018
4019 static int VEraseType( Draw_Interpretor& , Standard_Integer argc, const char** argv)
4020 {
4021   if(argc!=2) return 1;
4022
4023   AIS_KindOfInteractive TheType;
4024   Standard_Integer TheSign(-1);
4025   GetTypeAndSignfromString(argv[1],TheType,TheSign);
4026
4027
4028   AIS_ListOfInteractive LIO;
4029
4030   // en attendant l'amelioration ais pour les dimensions...
4031   //
4032   Standard_Integer dimension_status(-1);
4033   if(TheType==AIS_KOI_Relation){
4034     dimension_status = TheSign ==1 ? 1 : 0;
4035     TheSign=-1;
4036   }
4037
4038   TheAISContext()->DisplayedObjects(TheType,TheSign,LIO);
4039   Handle(AIS_InteractiveObject) curio;
4040   for(AIS_ListIteratorOfListOfInteractive it(LIO);it.More();it.Next()){
4041     curio  = it.Value();
4042
4043     if(dimension_status == -1)
4044       TheAISContext()->Erase(curio,Standard_False);
4045     else {
4046       AIS_KindOfDimension KOD = (*((Handle(AIS_Relation)*)&curio))->KindOfDimension();
4047       if ((dimension_status==0 && KOD == AIS_KOD_NONE)||
4048           (dimension_status==1 && KOD != AIS_KOD_NONE))
4049         TheAISContext()->Erase(curio,Standard_False);
4050     }
4051   }
4052   TheAISContext()->UpdateCurrentViewer();
4053   return 0;
4054 }
4055 static int VDisplayType(Draw_Interpretor& , Standard_Integer argc, const char** argv)
4056 {
4057   if(argc!=2) return 1;
4058
4059   AIS_KindOfInteractive TheType;
4060   Standard_Integer TheSign(-1);
4061   GetTypeAndSignfromString(argv[1],TheType,TheSign);
4062
4063   // en attendant l'amelioration ais pour les dimensions...
4064   //
4065   Standard_Integer dimension_status(-1);
4066   if(TheType==AIS_KOI_Relation){
4067     dimension_status = TheSign ==1 ? 1 : 0;
4068     TheSign=-1;
4069   }
4070
4071   AIS_ListOfInteractive LIO;
4072   TheAISContext()->ObjectsInside(LIO,TheType,TheSign);
4073   Handle(AIS_InteractiveObject) curio;
4074   for(AIS_ListIteratorOfListOfInteractive it(LIO);it.More();it.Next()){
4075     curio  = it.Value();
4076     if(dimension_status == -1)
4077       TheAISContext()->Display(curio,Standard_False);
4078     else {
4079       AIS_KindOfDimension KOD = (*((Handle(AIS_Relation)*)&curio))->KindOfDimension();
4080       if ((dimension_status==0 && KOD == AIS_KOD_NONE)||
4081           (dimension_status==1 && KOD != AIS_KOD_NONE))
4082         TheAISContext()->Display(curio,Standard_False);
4083     }
4084
4085   }
4086
4087   TheAISContext()->UpdateCurrentViewer();
4088   return 0;
4089 }
4090
4091 //==============================================================================
4092 //function : VSetTransMode
4093 //purpose  :
4094 //Draw arg : vsettransmode shape flag1 [flag2] [flag3] [X Y Z]
4095 //==============================================================================
4096
4097 static int VSetTransMode ( Draw_Interpretor& di, Standard_Integer argc, const char** argv ) {
4098   // Verification des arguments
4099   if ( a3DView().IsNull() ) {
4100     ViewerTest::ViewerInit();
4101     di << "La commande vinit n'a pas ete appele avant" << "\n";
4102   }
4103
4104   if ( argc < 3 || argc > 8 ) {
4105     di << argv[0] << " Invalid number of arguments" << "\n";
4106     return 1;
4107   }
4108
4109   TCollection_AsciiString shapeName;
4110   shapeName = argv[1];
4111   Standard_Integer persFlag1 = Draw::Atoi(argv[2]);
4112   Standard_Integer persFlag2 = 0;
4113   Standard_Integer persFlag3 = 0;
4114   gp_Pnt origin = gp_Pnt( 0.0, 0.0, 0.0 );
4115   if ( argc == 4 || argc == 5 || argc == 7 || argc == 8 ) {
4116     persFlag2 = Draw::Atoi(argv[3]);
4117   }
4118   if ( argc == 5 || argc == 8 ) {
4119     persFlag3 = Draw::Atoi(argv[4]);
4120   }
4121   if ( argc >= 6 ) {
4122     origin.SetX( Draw::Atof(argv[argc - 3]) );
4123     origin.SetY( Draw::Atof(argv[argc - 2]) );
4124     origin.SetZ( Draw::Atof(argv[argc - 1]) );
4125   }
4126
4127   Standard_Boolean IsBound = GetMapOfAIS().IsBound2(shapeName);
4128   Handle(Standard_Transient) anObj;
4129   if ( IsBound ) {
4130     anObj = GetMapOfAIS().Find2(shapeName);
4131     if ( anObj->IsKind(STANDARD_TYPE(AIS_InteractiveObject)) ) {
4132       Handle(AIS_InteractiveObject) aShape = Handle(AIS_InteractiveObject)::DownCast(anObj);
4133       aShape->SetTransformPersistence( (persFlag1 | persFlag2 | persFlag3), origin );
4134       if ( persFlag1 == 0 && persFlag2 == 0 && persFlag3 == 0 ) {
4135         di << argv[0] << " All persistence modifiers were removed" << "\n";
4136       }
4137     } else {
4138       di << argv[0] << " Wrong object type" << "\n";
4139       return 1;
4140     }
4141   } else { // Create the AIS_Shape from a name
4142     const Handle(AIS_InteractiveObject) aShape = GetAISShapeFromName((const char* )shapeName.ToCString());
4143     if ( !aShape.IsNull() ) {
4144       GetMapOfAIS().Bind( aShape, shapeName );
4145       aShape->SetTransformPersistence( (persFlag1 | persFlag2 | persFlag3), origin );
4146       TheAISContext()->Display( aShape, Standard_False );
4147     } else {
4148       di << argv[0] << " Object not found" << "\n";
4149       return 1;
4150     }
4151   }
4152
4153   // Upadate the screen and redraw the view
4154   TheAISContext()->UpdateCurrentViewer();
4155   return 0;
4156 }
4157
4158 static Standard_Integer vr(Draw_Interpretor& , Standard_Integer , const char** a)
4159 {
4160   ifstream s(a[1]);
4161   BRep_Builder builder;
4162   TopoDS_Shape shape;
4163   BRepTools::Read(shape, s, builder);
4164   DBRep::Set(a[1], shape);
4165   Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
4166   Handle(AIS_Shape) ais = new AIS_Shape(shape);
4167   Ctx->Display(ais);
4168   return 0;
4169 }
4170
4171 //==============================================================================
4172 //function : ViewerTest::Commands
4173 //purpose  : Add all the viewer command in the Draw_Interpretor
4174 //==============================================================================
4175
4176 void ViewerTest::Commands(Draw_Interpretor& theCommands)
4177 {
4178   ViewerTest::ViewerCommands(theCommands);
4179   ViewerTest::RelationCommands(theCommands);
4180   ViewerTest::ObjectCommands(theCommands);
4181   ViewerTest::FilletCommands(theCommands);
4182   ViewerTest::VoxelCommands(theCommands);
4183   ViewerTest::OpenGlCommands(theCommands);
4184
4185   const char *group = "AIS_Display";
4186
4187   // display
4188   theCommands.Add("visos",
4189                   "visos [name1 ...] [nbUIsos nbVIsos IsoOnPlane(0|1)]\n"
4190                   "\tIf last 3 optional parameters are not set prints numbers of U-, V- isolines and IsoOnPlane.\n",
4191                   __FILE__, visos, group);
4192
4193   theCommands.Add("vdisplay",
4194                   "vdisplay [-noupdate|-update] [-mutable] name1 [name2] ... [name n]"
4195       "\n\t\t: Displays named objects."
4196       "\n\t\t: Option -noupdate suppresses viewer redraw call."
4197       "\n\t\t: Option -mutable enables optimizations for mutable objects."
4198                   __FILE__,VDisplay2,group);
4199
4200   theCommands.Add ("vupdate",
4201       "vupdate name1 [name2] ... [name n]"
4202       "\n\t\t: Updates named objects in interactive context",
4203       __FILE__, VUpdate, group);
4204
4205   theCommands.Add("verase",
4206       "verase [-noupdate|-update] [name1] ...  [name n]"
4207       "\n\t\t: Erases selected or named objects."
4208       "\n\t\t: If there are no selected or named objects the whole viewer is erased.",
4209                   __FILE__, VErase, group);
4210
4211   theCommands.Add("vremove",
4212     "vremove [-noupdate|-update] [-context] [-all] [-noinfo] [name1] ...  [name n]"
4213     "or vremove [-context] -all to remove all objects"
4214       "\n\t\t: Removes selected or named objects."
4215       "\n\t\t  If -context is in arguments, the objects are not deleted"
4216       "\n\t\t  from the map of objects and names."
4217       "\n\t\t: Option -noupdate suppresses viewer redraw call."
4218       "\n\t\t: Option -noinfo suppresses displaying the list of removed objects.",
4219       __FILE__, VRemove, group);
4220
4221   theCommands.Add("vdonly",
4222                   "vdonly [-noupdate|-update] [name1] ...  [name n]"
4223       "\n\t\t: Displays only selected or named objects",
4224                   __FILE__,VDonly2,group);
4225
4226   theCommands.Add("vdisplayall",
4227                   "Displays all erased interactive objects (see vdir and vstate)",
4228                   __FILE__,VDisplayAll,group);
4229
4230   theCommands.Add("veraseall",
4231                   "Erases all objects displayed in the viewer",
4232                   __FILE__, VErase, group);
4233
4234   theCommands.Add("verasetype",
4235                   "verasetype <Type>"
4236       "\n\t\t: Erase all the displayed objects of one given kind (see vtypes)",
4237                   __FILE__,VEraseType,group);
4238
4239   theCommands.Add("vbounding",
4240               "vbounding [-noupdate|-update] [-mode] name1 [name2 [...]]"
4241       "\n\t\t:           [-print] [-hide]"
4242       "\n\t\t: Temporarily display bounding box of specified Interactive"
4243       "\n\t\t: Objects, or print it to console if -print is specified."
4244       "\n\t\t: Already displayed box might be hidden by -hide option.",
4245                   __FILE__,VBounding,group);
4246
4247   theCommands.Add("vdisplaytype",
4248                   "vdisplaytype        : vdisplaytype <Type> <Signature> \n\t display all the objects of one given kind (see vtypes) which are stored the AISContext ",
4249                   __FILE__,VDisplayType,group);
4250
4251   theCommands.Add("vdisplaymode",
4252                   "vdispmode       : vdispmode  [name] mode(1,2,..) : no name -> on selected objects ",
4253                   __FILE__,VDispMode,group);
4254
4255   theCommands.Add("verasemode",
4256                   "verasemode      : verasemode [name] mode(1,2,..) : no name -> on selected objects",
4257                   __FILE__,VDispMode,group);
4258
4259   theCommands.Add("vsetdispmode",
4260                   "vsetdispmode [name] mode(1,2,..)"
4261       "\n\t\t: Sets display mode for all, selected or named objects.",
4262                   __FILE__,VDispMode,group);
4263
4264   theCommands.Add("vunsetdispmode",
4265                   "vunsetdispmode [name]"
4266       "\n\t\t: Unsets custom display mode for selected or named objects.",
4267                   __FILE__,VDispMode,group);
4268
4269   theCommands.Add("vdir",
4270                   "Lists all objects displayed in 3D viewer",
4271                   __FILE__,VDir,group);
4272
4273   theCommands.Add("vdump",
4274     #ifdef HAVE_FREEIMAGE
4275               "vdump <filename>.{png|bmp|jpg|gif} [rgb|rgba|depth=rgb] [mono|left|right=mono]"
4276       "\n\t\t:                                    [width Width=0 height Height=0]"
4277       "\n\t\t: Dumps content of the active view into PNG, BMP, JPEG or GIF file",
4278     #else
4279               "vdump <filename>.{ppm} [rgb|rgba|depth=rgb] [mono|left|right=mono]"
4280       "\n\t\t:                        [width Width=0 height Height=0]"
4281       "\n\t\t: Dumps content of the active view into PPM image file",
4282     #endif
4283                   __FILE__,VDump,group);
4284
4285   theCommands.Add("vsub",      "vsub 0/1 (off/on) [obj]        : Subintensity(on/off) of selected objects",
4286                   __FILE__,VSubInt,group);
4287
4288   theCommands.Add("vaspects",
4289               "vaspects [-noupdate|-update] [name1 [name2 [...]]]"
4290       "\n\t\t:          [-setcolor ColorName] [-unsetcolor]"
4291       "\n\t\t:          [-setmaterial MatName] [-unsetmaterial]"
4292       "\n\t\t:          [-settransparency Transp] [-unsettransparency]"
4293       "\n\t\t:          [-setwidth LineWidth] [-unsetwidth]"
4294       "\n\t\t:          [-subshapes subname1 [subname2 [...]]]"
4295       "\n\t\t: Manage presentation properties of all, selected or named objects."
4296       "\n\t\t: When -subshapes is specified than following properties will be"
4297       "\n\t\t: assigned to specified sub-shapes.",
4298                   __FILE__,VAspects,group);
4299
4300   theCommands.Add("vsetcolor",
4301       "vsetcolor [-noupdate|-update] [name] ColorName"
4302       "\n\t\t: Sets color for all, selected or named objects."
4303       "\n\t\t: Alias for vaspects -setcolor [name] ColorName.",
4304                   __FILE__,VAspects,group);
4305
4306   theCommands.Add("vunsetcolor",
4307                   "vunsetcolor [-noupdate|-update] [name]"
4308       "\n\t\t: Resets color for all, selected or named objects."
4309       "\n\t\t: Alias for vaspects -unsetcolor [name].",
4310                   __FILE__,VAspects,group);
4311
4312   theCommands.Add("vsettransparency",
4313                   "vsettransparency [-noupdate|-update] [name] Coefficient"
4314       "\n\t\t: Sets transparency for all, selected or named objects."
4315       "\n\t\t: The Coefficient may be between 0.0 (opaque) and 1.0 (fully transparent)."
4316       "\n\t\t: Alias for vaspects -settransp [name] Coefficient.",
4317                   __FILE__,VAspects,group);
4318
4319   theCommands.Add("vunsettransparency",
4320                   "vunsettransparency [-noupdate|-update] [name]"
4321       "\n\t\t: Resets transparency for all, selected or named objects."
4322       "\n\t\t: Alias for vaspects -unsettransp [name].",
4323                   __FILE__,VAspects,group);
4324
4325   theCommands.Add("vsetmaterial",
4326                   "vsetmaterial [-noupdate|-update] [name] MaterialName"
4327       "\n\t\t: Alias for vaspects -setmaterial [name] MaterialName.",
4328                   __FILE__,VAspects,group);
4329
4330   theCommands.Add("vunsetmaterial",
4331                   "vunsetmaterial [-noupdate|-update] [name]"
4332       "\n\t\t: Alias for vaspects -unsetmaterial [name].",
4333                   __FILE__,VAspects,group);
4334
4335   theCommands.Add("vsetwidth",
4336                   "vsetwidth [-noupdate|-update] [name] width(0->10)"
4337       "\n\t\t: Alias for vaspects -setwidth [name] width.",
4338                   __FILE__,VAspects,group);
4339
4340   theCommands.Add("vunsetwidth",
4341                   "vunsetwidth [-noupdate|-update] [name]"
4342       "\n\t\t: Alias for vaspects -unsetwidth [name] width.",
4343                   __FILE__,VAspects,group);
4344
4345   theCommands.Add("vsetinteriorstyle",
4346                   "vsetinteriorstyle [-noupdate|-update] [name] style"
4347       "\n\t\t: Where style is: 0 = EMPTY, 1 = HOLLOW, 2 = HATCH, 3 = SOLID, 4 = HIDDENLINE.",
4348                   __FILE__,VSetInteriorStyle,group);
4349
4350   theCommands.Add("vardis",
4351                   "vardis          : display activeareas",
4352                   __FILE__,VDispAreas,group);
4353
4354   theCommands.Add("varera",
4355                   "varera           : erase activeareas",
4356                   __FILE__,VClearAreas,group);
4357
4358   theCommands.Add("vsensdis",
4359                   "vardisp           : display active entities",
4360                   __FILE__,VDispSensi,group);
4361   theCommands.Add("vsensera",
4362                   "vardisp           : erase  active entities",
4363                   __FILE__,VClearSensi,group);
4364
4365   theCommands.Add("vselprecision",
4366                   "vselprecision : vselprecision [precision_mode [tolerance_value]]",
4367                   __FILE__,VSelPrecision,group);
4368
4369   theCommands.Add("vperf",
4370                   "vperf: vperf  ShapeName 1/0(Transfo/Location) 1/0(Primitives sensibles ON/OFF)",
4371                   __FILE__,VPerf,group);
4372
4373   theCommands.Add("vanimation",
4374                   "vanimation CrankArmFile CylinderHeadFile PropellerFile EngineBlockFile",
4375                   __FILE__,VAnimation,group);
4376
4377   theCommands.Add("vsetshading",
4378                   "vsetshading  : vsetshading name Quality(default=0.0008) ",
4379                   __FILE__,VShading,group);
4380
4381   theCommands.Add("vunsetshading",
4382                   "vunsetshading :vunsetshading name ",
4383                   __FILE__,VShading,group);
4384
4385   theCommands.Add ("vtexture",
4386                    "\n'vtexture NameOfShape [TextureFile | IdOfTexture]\n"
4387                    "                         [-scale u v]  [-scale off]\n"
4388                    "                         [-origin u v] [-origin off]\n"
4389                    "                         [-repeat u v] [-repeat off]\n"
4390                    "                         [-modulate {on | off}]"
4391                    "                         [-default]'\n"
4392                    " The texture can be specified by filepath or as ID (0<=IdOfTexture<=20)\n"
4393                    " specifying one of the predefined textures.\n"
4394                    " The options are: \n"
4395                    "  -scale u v : enable texture scaling and set scale factors\n"
4396                    "  -scale off : disable texture scaling\n"
4397                    "  -origin u v : enable texture origin positioning and set the origin\n"
4398                    "  -origin off : disable texture origin positioning\n"
4399                    "  -repeat u v : enable texture repeat and set texture coordinate scaling\n"
4400                    "  -repeat off : disable texture repeat\n"
4401                    "  -modulate {on | off} : enable or disable texture modulation\n"
4402                    "  -default : sets texture mapping default parameters\n"
4403                    "or 'vtexture NameOfShape' if you want to disable texture mapping\n"
4404                    "or 'vtexture NameOfShape ?' to list available textures\n",
4405                     __FILE__, VTexture, group);
4406
4407   theCommands.Add("vtexscale",
4408                   "'vtexscale  NameOfShape ScaleU ScaleV' \n \
4409                    or 'vtexscale NameOfShape ScaleUV' \n \
4410                    or 'vtexscale NameOfShape' to disable scaling\n ",
4411                   __FILE__,VTexture,group);
4412
4413   theCommands.Add("vtexorigin",
4414                   "'vtexorigin NameOfShape UOrigin VOrigin' \n \
4415                    or 'vtexorigin NameOfShape UVOrigin' \n \
4416                    or 'vtexorigin NameOfShape' to disable origin positioning\n ",
4417                   __FILE__,VTexture,group);
4418
4419   theCommands.Add("vtexrepeat",
4420                   "'vtexrepeat  NameOfShape URepeat VRepeat' \n \
4421                    or 'vtexrepeat NameOfShape UVRepeat \n \
4422                    or 'vtexrepeat NameOfShape' to disable texture repeat \n ",
4423                   VTexture,group);
4424
4425   theCommands.Add("vtexdefault",
4426                   "'vtexdefault NameOfShape' to set texture mapping default parameters \n",
4427                   VTexture,group);
4428
4429   theCommands.Add("vsetam",
4430                   "vsetActivatedModes: vsetam mode(1->7)  ",
4431                   __FILE__,VActivatedMode,group);
4432
4433   theCommands.Add("vunsetam",
4434                   "vunsetActivatedModes:   vunsetam  ",
4435                   __FILE__,VActivatedMode,group);
4436
4437   theCommands.Add("vstate",
4438       "vstate [-entities] [name1] ... [nameN]"
4439       "\n\t\t: Reports show/hidden state for selected or named objects"
4440       "\n\t\t:   -entities - print low-level information about detected entities",
4441                   __FILE__,VState,group);
4442
4443   theCommands.Add("vpickshapes",
4444                   "vpickshape subtype(VERTEX,EDGE,WIRE,FACE,SHELL,SOLID) [name1 or .] [name2 or .] [name n or .]",
4445                   __FILE__,VPickShape,group);
4446
4447   theCommands.Add("vtypes",
4448                   "vtypes : list of known types and signatures in AIS - To be Used in vpickobject command for selection with filters",
4449                   VIOTypes,group);
4450
4451   theCommands.Add("vsettransmode",
4452                   "vsettransmode   : vsettransmode shape flag1 [flag2] [flag3] [X Y Z]",
4453                   __FILE__,VSetTransMode,group);
4454
4455   theCommands.Add("vr", "vr : reading of the shape",
4456                   __FILE__,vr, group);
4457
4458 }
4459
4460 //=====================================================================
4461 //========================= for testing Draft and Rib =================
4462 //=====================================================================
4463 #include <BRepOffsetAPI_MakeThickSolid.hxx>
4464 #include <DBRep.hxx>
4465 #include <TopoDS_Face.hxx>
4466 #include <gp_Pln.hxx>
4467 #include <AIS_KindOfSurface.hxx>
4468 #include <BRepOffsetAPI_DraftAngle.hxx>
4469 #include <Precision.hxx>
4470 #include <BRepAlgo.hxx>
4471 #include <OSD_Environment.hxx>
4472 #include <DrawTrSurf.hxx>
4473 //#include <DbgTools.hxx>
4474 //#include <FeatAlgo_MakeLinearForm.hxx>
4475
4476
4477
4478
4479 //=======================================================================
4480 //function : IsValid
4481 //purpose  :
4482 //=======================================================================
4483 static Standard_Boolean IsValid(const TopTools_ListOfShape& theArgs,
4484                                 const TopoDS_Shape& theResult,
4485                                 const Standard_Boolean closedSolid,
4486                                 const Standard_Boolean GeomCtrl)
4487 {
4488   OSD_Environment check ("DONT_SWITCH_IS_VALID") ;
4489   TCollection_AsciiString checkValid = check.Value();
4490   Standard_Boolean ToCheck = Standard_True;
4491   if (!checkValid.IsEmpty()) {
4492 #ifdef DEB
4493     cout <<"DONT_SWITCH_IS_VALID positionnee a :"<<checkValid.ToCString()<<"\n";
4494 #endif
4495     if ( checkValid=="true" || checkValid=="TRUE" ) {
4496       ToCheck= Standard_False;
4497     }
4498   } else {
4499 #ifdef DEB
4500     cout <<"DONT_SWITCH_IS_VALID non positionne"<<"\n";
4501 #endif
4502   }
4503   Standard_Boolean IsValid = Standard_True;
4504   if (ToCheck)
4505     IsValid = BRepAlgo::IsValid(theArgs,theResult,closedSolid,GeomCtrl) ;
4506   return IsValid;
4507
4508 }
4509
4510 //===============================================================================
4511 // TDraft : test draft, uses AIS Viewer
4512 // Solid Face Plane Angle  Reverse
4513 //===============================================================================
4514 static Standard_Integer TDraft(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
4515 {
4516   if (argc < 5) return 1;
4517 // argv[1] - TopoDS_Shape Solid
4518 // argv[2] - TopoDS_Shape Face
4519 // argv[3] - TopoDS_Shape Plane
4520 // argv[4] - Standard_Real Angle
4521 // argv[5] - Standard_Integer Reverse
4522
4523 //  Sprintf(prefix, argv[1]);
4524   Standard_Real anAngle = 0;
4525   Standard_Boolean Rev = Standard_False;
4526   Standard_Integer rev = 0;
4527   TopoDS_Shape Solid  = GetShapeFromName(argv[1]);
4528   TopoDS_Shape face   = GetShapeFromName(argv[2]);
4529   TopoDS_Face Face    = TopoDS::Face(face);
4530   TopoDS_Shape Plane  = GetShapeFromName(argv[3]);
4531   if (Plane.IsNull ()) {
4532     di << "TEST : Plane is NULL" << "\n";
4533     return 1;
4534   }
4535   anAngle = Draw::Atof(argv[4]);
4536   anAngle = 2*M_PI * anAngle / 360.0;
4537   gp_Pln aPln;
4538   Handle( Geom_Surface )aSurf;
4539   AIS_KindOfSurface aSurfType;
4540   Standard_Real Offset;
4541   gp_Dir aDir;
4542   if(argc > 4) { // == 5
4543     rev = Draw::Atoi(argv[5]);
4544     Rev = (rev)? Standard_True : Standard_False;
4545   }
4546
4547   TopoDS_Face face2 = TopoDS::Face(Plane);
4548   if(!AIS::GetPlaneFromFace(face2, aPln, aSurf, aSurfType, Offset))
4549     {
4550       di << "TEST : Can't find plane" << "\n";
4551       return 1;
4552     }
4553
4554   aDir = aPln.Axis().Direction();
4555   if (!aPln.Direct())
4556     aDir.Reverse();
4557   if (Plane.Orientation() == TopAbs_REVERSED)
4558     aDir.Reverse();
4559   di << "TEST : gp::Resolution() = " << gp::Resolution() << "\n";
4560
4561   BRepOffsetAPI_DraftAngle Draft (Solid);
4562
4563   if(Abs(anAngle)< Precision::Angular()) {
4564     di << "TEST : NULL angle" << "\n";
4565     return 1;}
4566
4567   if(Rev) anAngle = - anAngle;
4568   Draft.Add (Face, aDir, anAngle, aPln);
4569   Draft.Build ();
4570   if (!Draft.IsDone())  {
4571     di << "TEST : Draft Not DONE " << "\n";
4572     return 1;
4573   }
4574   TopTools_ListOfShape Larg;
4575   Larg.Append(Solid);
4576   if (!IsValid(Larg,Draft.Shape(),Standard_True,Standard_False)) {
4577     di << "TEST : DesignAlgo returns Not valid" << "\n";
4578     return 1;
4579   }
4580
4581   Handle(AIS_InteractiveContext) Ctx = ViewerTest::GetAISContext();
4582   Handle(AIS_Shape) ais = new AIS_Shape(Draft.Shape());
4583
4584   if ( !ais.IsNull() ) {
4585     ais->SetColor(DEFAULT_COLOR);
4586     ais->SetMaterial(DEFAULT_MATERIAL);
4587     // Display the AIS_Shape without redraw
4588     Ctx->Display(ais, Standard_False);
4589
4590     const char *Name = "draft1";
4591     Standard_Boolean IsBound = GetMapOfAIS().IsBound2(Name);
4592     if (IsBound) {
4593       Handle(AIS_InteractiveObject) an_object =
4594         Handle(AIS_InteractiveObject)::DownCast(GetMapOfAIS().Find2(Name));
4595       if (!an_object.IsNull()) {
4596         Ctx->Remove(an_object,
4597                     Standard_True) ;
4598         GetMapOfAIS().UnBind2(Name) ;
4599       }
4600     }
4601     GetMapOfAIS().Bind(ais, Name);
4602 //  DBRep::Set("draft", ais->Shape());
4603   }
4604   Ctx->Display(ais, Standard_True);
4605   return 0;
4606 }
4607
4608 //==============================================================================
4609 //function : splitParameter
4610 //purpose  : Split parameter string to parameter name and parameter value
4611 //==============================================================================
4612 Standard_Boolean ViewerTest::SplitParameter (const TCollection_AsciiString& theString,
4613                                              TCollection_AsciiString&       theName,
4614                                              TCollection_AsciiString&       theValue)
4615 {
4616   Standard_Integer aParamNameEnd = theString.FirstLocationInSet ("=", 1, theString.Length());
4617
4618   if (aParamNameEnd == 0)
4619   {
4620     return Standard_False;
4621   }
4622
4623   TCollection_AsciiString aString (theString);
4624   if (aParamNameEnd != 0)
4625   {
4626     theValue = aString.Split (aParamNameEnd);
4627     aString.Split (aString.Length() - 1);
4628     theName = aString;
4629   }
4630
4631   return Standard_True;
4632 }
4633
4634 //============================================================================
4635 //  MyCommands
4636 //============================================================================
4637 void ViewerTest::MyCommands( Draw_Interpretor& theCommands)
4638 {
4639
4640   DrawTrSurf::BasicCommands(theCommands);
4641   const char* group = "Check Features Operations commands";
4642
4643   theCommands.Add("Draft","Draft    Solid Face Plane Angle Reverse",
4644                   __FILE__,
4645                   &TDraft,group); //Draft_Modification
4646 }
4647
4648 //==============================================================================
4649 // ViewerTest::Factory
4650 //==============================================================================
4651 void ViewerTest::Factory(Draw_Interpretor& theDI)
4652 {
4653   // definition of Viewer Command
4654   ViewerTest::Commands(theDI);
4655   ViewerTest::AviCommands(theDI);
4656
4657 #ifdef DEB
4658       theDI << "Draw Plugin : OCC V2d & V3d commands are loaded" << "\n";
4659 #endif
4660 }
4661
4662 // Declare entry point PLUGINFACTORY
4663 DPLUGIN(ViewerTest)