1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
19 #include <Viewer2dTest.hxx>
20 #include <Viewer2dTest_EventManager.hxx>
21 #include <Viewer2dTest_DoubleMapOfInteractiveAndName.hxx>
22 #include <Viewer2dTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName.hxx>
24 #include <Draw_Interpretor.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopTools_HArray1OfShape.hxx>
30 #include <TColStd_ListOfTransient.hxx>
31 #include <TColStd_MapOfInteger.hxx>
32 #include <TColStd_HArray1OfTransient.hxx>
34 #include <TCollection_AsciiString.hxx>
36 #include <AIS2D_ProjShape.hxx>
37 #include <AIS2D_InteractiveObject.hxx>
38 #include <AIS2D_InteractiveContext.hxx>
40 extern int ViewerMainLoop2d (Standard_Integer argc, const char** argv);
42 TopoDS_Shape GetShapeFromName2d (const char* name);
44 static TColStd_ListOfTransient theEventMgrs2d;
46 //==============================================================================
47 // VIEWER OBJECT MANAGEMENT GLOBAL VARIABLES
48 //==============================================================================
49 Standard_EXPORT Viewer2dTest_DoubleMapOfInteractiveAndName& GetMapOfAIS2D()
51 static Viewer2dTest_DoubleMapOfInteractiveAndName TheMap;
55 static void VwrTst_InitEventMgr2d (const Handle(AIS2D_InteractiveContext)& Ctx)
57 theEventMgrs2d.Clear();
58 theEventMgrs2d.Prepend(new Viewer2dTest_EventManager(Ctx));
61 static Handle(V2d_View)& a2DView()
63 static Handle(V2d_View) Viou;
67 Standard_EXPORT Handle(AIS2D_InteractiveContext)& TheAIS2DContext()
69 static Handle(AIS2D_InteractiveContext) aContext;
73 Handle(V2d_View) Viewer2dTest::CurrentView()
78 void Viewer2dTest::CurrentView (const Handle(V2d_View)& V)
83 Handle(AIS2D_InteractiveContext) Viewer2dTest::GetAIS2DContext()
85 return TheAIS2DContext();
88 Handle(V2d_Viewer) Viewer2dTest::GetViewerFromContext()
90 return TheAIS2DContext()->CurrentViewer();
93 Handle(V2d_Viewer) Viewer2dTest::GetCollectorFromContext()
95 return TheAIS2DContext()->CurrentViewer();
98 void Viewer2dTest::SetAIS2DContext (const Handle(AIS2D_InteractiveContext)& aCtx)
100 TheAIS2DContext() = aCtx;
101 Viewer2dTest::ResetEventManager();
104 void Viewer2dTest::SetEventManager (const Handle(Viewer2dTest_EventManager)& EM)
106 theEventMgrs2d.Prepend(EM);
109 void Viewer2dTest::UnsetEventManager()
111 theEventMgrs2d.RemoveFirst();
114 void Viewer2dTest::ResetEventManager()
116 VwrTst_InitEventMgr2d(Viewer2dTest::GetAIS2DContext());
119 //==============================================================================
120 //function : CurrentEventManager
122 //==============================================================================
123 Handle(Viewer2dTest_EventManager) Viewer2dTest::CurrentEventManager()
125 Handle(Viewer2dTest_EventManager) EM;
126 if(theEventMgrs2d.IsEmpty()) return EM;
127 Handle(Standard_Transient) Tr = theEventMgrs2d.First();
128 EM = *((Handle(Viewer2dTest_EventManager)*)&Tr);
132 //==============================================================================
134 //purpose : Remove all the object from the viewer
135 //==============================================================================
136 void Viewer2dTest::Clear()
138 if (!Viewer2dTest::CurrentView().IsNull())
140 TheAIS2DContext()->CloseLocalContext();
141 Viewer2dTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName it (GetMapOfAIS2D());
144 cout << "Remove " << it.Key2() << endl;
145 // TheAIS2DContext()->Remove(it.Key1(), Standard_False);
146 TheAIS2DContext()->Erase(it.Key1(), Standard_False, Standard_False);
149 TheAIS2DContext()->UpdateCurrentViewer();
150 GetMapOfAIS2D().Clear();
154 //==============================================================================
155 //function : StandardModeActivation
156 //purpose : Activate a selection mode, vertex, edge, wire ..., in a local
158 //==============================================================================
159 void Viewer2dTest::StandardModeActivation (const Standard_Integer mode)
161 Handle(AIS2D_InteractiveContext) aContext = Viewer2dTest::GetAIS2DContext();
162 aContext->SetPickMode(Graphic2d_PM_INTERSECT);
164 const char* cmode = "??????????";
166 switch ((AIS2D_TypeOfDetection)mode)
168 case AIS2D_TOD_OBJECT: cmode = "OBJECT"; break;
169 case AIS2D_TOD_PRIMITIVE: cmode = "PRIMITIVE"; break;
170 case AIS2D_TOD_ELEMENT: cmode = "ELEMENT"; break;
171 case AIS2D_TOD_VERTEX: cmode = "VERTEX"; break;
172 case AIS2D_TOD_NONE: cmode = "NONE"; break;
176 Viewer2dTest_DoubleMapIteratorOfDoubleMapOfInteractiveAndName it (GetMapOfAIS2D());
179 Handle(AIS2D_InteractiveObject) ashape = it.Key1();
180 ashape->SetContext(aContext);
181 // ashape->SetSelectionMode(mode);
182 ashape->SetHighlightMode((AIS2D_TypeOfDetection)mode);
185 // aContext->ActivateStandardMode(AIS2D_ProjShape::SelectionType(mode));
186 cout << "Mode " << cmode << " ON" << endl;
189 //=======================================================================
190 //function : PickObjects
192 //=======================================================================
193 Standard_Boolean Viewer2dTest::PickObjects (Handle(TColStd_HArray1OfTransient)& arr,
194 const AIS_KindOfInteractive TheType,
195 const Standard_Integer /*TheSignature*/,
196 const Standard_Integer MaxPick)
198 Handle(AIS2D_InteractiveObject) IO;
199 Standard_Integer curindex = (TheType == AIS_KOI_None) ? 0 : TheAIS2DContext()->OpenLocalContext();
201 // step 1: prepare the data
204 // Handle(AIS2D_SignatureFilter) F1 = new AIS2D_SignatureFilter(TheType,TheSignature);
205 // TheAIS2DContext()->AddFilter(F1);
208 // step 2 : wait for the selection...
209 // Standard_Boolean IsGood (Standard_False);
210 // Standard_Integer NbPick(0);
211 Standard_Boolean NbPickGood (0), NbToReach (arr->Length());
212 Standard_Integer NbPickFail(0);
213 Standard_Integer argccc = 5;
214 const char *bufff[] = { "A", "B", "C","D", "E" };
215 const char **argvvv = (const char **) bufff;
217 while (NbPickGood < NbToReach && NbPickFail <= MaxPick)
219 while (ViewerMainLoop2d(argccc,argvvv)) {}
220 /* Standard_Integer NbStored = TheAIS2DContext()->NbSelected();
221 if((unsigned int ) NbStored != NbPickGood)
222 NbPickGood= NbStored;
225 cout<<"NbPicked = "<<NbPickGood<<" | Nb Pick Fail :"<<NbPickFail<<endl;*/
230 if ((unsigned int ) NbPickFail >= NbToReach) return Standard_False;
233 Standard_Integer i(0);
234 for(TheAIS2DContext()->InitSelected();
235 TheAIS2DContext()->MoreSelected();
236 TheAIS2DContext()->NextSelected()){
238 Handle(AIS2D_InteractiveObject) IO = TheAIS2DContext()->SelectedInteractive();
242 if (curindex > 0) TheAIS2DContext()->CloseLocalContext(curindex);
243 return Standard_True;
246 //=======================================================================
247 //function : PickObject
249 //=======================================================================
250 /*Handle(AIS2D_InteractiveObject) Viewer2dTest::PickObject
251 (const AIS2D_KindOfInteractive TheType,
252 const Standard_Integer TheSignature,
253 const Standard_Integer MaxPick)
255 Handle(AIS2D_InteractiveObject) IO;
256 Standard_Integer curindex = (TheType == AIS2D_KOI_None) ? 0 : TheAIS2DContext()->OpenLocalContext();
258 // step 1: prepare the data
261 Handle(AIS2D_SignatureFilter) F1 = new AIS2D_SignatureFilter(TheType,TheSignature);
262 TheAIS2DContext()->AddFilter(F1);
265 // step 2 : wait for the selection...
266 Standard_Boolean IsGood (Standard_False);
267 Standard_Integer NbPick(0);
268 Standard_Integer argccc = 5;
269 char *bufff[] = { "VPick", "X", "VPickY","VPickZ", "VPickShape" };
270 char **argvvv = bufff;
273 while(!IsGood && NbPick<= MaxPick){
274 while(ViewerMainLoop2d(argccc,argvvv)){}
275 IsGood = (TheAIS2DContext()->NbSelected()>0) ;
277 cout<<"Nb Pick :"<<NbPick<<endl;
283 TheAIS2DContext()->InitSelected();
284 IO = TheAIS2DContext()->SelectedInteractive();
288 TheAIS2DContext()->CloseLocalContext(curindex);
292 //=======================================================================
293 //function : PickShape
294 //purpose : First Activate the rightmode + Put Filters to be able to
295 // pick objets that are of type <TheType>...
296 //=======================================================================
297 TopoDS_Shape Viewer2dTest::PickShape (const TopAbs_ShapeEnum /*TheType*/,
298 const Standard_Integer MaxPick)
300 // step 1: prepare the data
301 Standard_Integer curindex = TheAIS2DContext()->OpenLocalContext();
304 /* if(TheType==TopAbs_SHAPE)
306 Handle(AIS2D_TypeFilter) F1 = new AIS2D_TypeFilter(AIS2D_KOI_Shape);
307 TheAIS2DContext()->AddFilter(F1);
311 Handle(StdSelect_ShapeTypeFilter) TF = new StdSelect_ShapeTypeFilter(TheType);
312 TheAIS2DContext()->AddFilter(TF);
313 TheAIS2DContext()->ActivateStandardMode(TheType);
316 // step 2 : wait for the selection...
317 Standard_Boolean NoShape (Standard_True);
318 Standard_Integer NbPick (0);
319 Standard_Integer argccc = 5;
320 const char *bufff[] = { "VPick", "X", "VPickY","VPickZ", "VPickShape" };
321 const char **argvvv = (const char **) bufff;
323 while (NoShape && NbPick <= MaxPick)
325 while (ViewerMainLoop2d(argccc,argvvv)) {}
326 // NoShape = (TheAIS2DContext()->NbSelected() == 0);
328 cout << "Nb Pick :" << NbPick << endl;
334 /* TheAIS2DContext()->InitSelected();
335 if (TheAIS2DContext()->HasSelectedShape())
336 result = TheAIS2DContext()->SelectedShape();
339 Handle(AIS2D_InteractiveObject) IO = TheAIS2DContext()->SelectedInteractive();
340 result = (*((Handle(AIS2D_ProjShape)*) &IO))->Shape();
344 if (curindex > 0) TheAIS2DContext()->CloseLocalContext(curindex);
348 //=======================================================================
349 //function : PickShapes
351 //=======================================================================
352 Standard_Boolean Viewer2dTest::PickShapes (const TopAbs_ShapeEnum /*TheType*/,
353 Handle(TopTools_HArray1OfShape)& thearr,
354 const Standard_Integer /*MaxPick*/)
356 Standard_Integer Taille = thearr->Length();
358 cout << " WARNING : Pick with Shift+ MB1 for Selection of more than 1 object" << endl;
360 // step 1: prepare the data
361 Standard_Integer curindex = TheAIS2DContext()->OpenLocalContext();
362 /* if (TheType == TopAbs_SHAPE)
364 Handle(AIS2D_TypeFilter) F1 = new AIS2D_TypeFilter(AIS2D_KOI_Shape);
365 TheAIS2DContext()->AddFilter(F1);
369 Handle(StdSelect_ShapeTypeFilter) TF = new StdSelect_ShapeTypeFilter(TheType);
370 TheAIS2DContext()->AddFilter(TF);
371 TheAIS2DContext()->ActivateStandardMode(TheType);
374 // step 2 : wait for the selection...
375 Standard_Boolean NbToReach (thearr->Length());
376 Standard_Integer NbPickFail (0);
378 Standard_Boolean NbPickGood (0) ;
379 Standard_Integer argccc = 5;
380 char *bufff[] = { "A", "B", "C","D", "E" };
381 char **argvvv = bufff;
383 while (NbPickGood < NbToReach && NbPickFail <= MaxPick)
385 while (ViewerMainLoop2d(argccc,argvvv)) {}
386 Standard_Integer NbStored = TheAIS2DContext()->NbSelected();
387 if ((unsigned int ) NbStored != NbPickGood)
388 NbPickGood = NbStored;
391 cout<<"NbPicked = "<<NbPickGood<<" | Nb Pick Fail :"<<NbPickFail<<endl;
395 if ((unsigned int) NbPickFail >= NbToReach) return Standard_False;
398 Standard_Integer i(0);
399 for (TheAIS2DContext()->InitSelected();
400 TheAIS2DContext()->MoreSelected();
401 TheAIS2DContext()->NextSelected())
404 if(TheAIS2DContext()->HasSelectedShape())
405 thearr->SetValue(i,TheAIS2DContext()->SelectedShape());
408 Handle(AIS2D_InteractiveObject) IO = TheAIS2DContext()->SelectedInteractive();
409 thearr->SetValue(i,(*((Handle(AIS2D_ProjShape)*) &IO))->Shape());
413 TheAIS2DContext()->CloseLocalContext(curindex);
414 return Standard_True;
417 //=====================================================================
418 //========================= for testing Draft and Rib =================
419 //=====================================================================
420 #include <BRepOffsetAPI_MakeThickSolid.hxx>
422 #include <TopoDS_Face.hxx>
423 #include <gp_Pln.hxx>
424 #include <BRepOffsetAPI_DraftAngle.hxx>
425 #include <Precision.hxx>
426 #include <BRepAlgo.hxx>
427 #include <OSD_Environment.hxx>
428 #include <DrawTrSurf.hxx>
430 //=======================================================================
431 //function : IsValid2d
433 //=======================================================================
434 static Standard_Boolean IsValid2d (const TopTools_ListOfShape& theArgs,
435 const TopoDS_Shape& theResult,
436 const Standard_Boolean closedSolid,
437 const Standard_Boolean GeomCtrl)
439 OSD_Environment check ("DONT_SWITCH_IS_VALID") ;
440 TCollection_AsciiString checkValid = check.Value();
441 Standard_Boolean ToCheck = Standard_True;
442 if (!checkValid.IsEmpty()) {
444 cout <<"DONT_SWITCH_IS_VALID positionnee a :"<<checkValid.ToCString()<<endl;
446 if ( checkValid=="true" || checkValid=="TRUE" ) {
447 ToCheck= Standard_False;
451 cout <<"DONT_SWITCH_IS_VALID non positionne"<<endl;
454 Standard_Boolean IsValid = Standard_True;
456 IsValid = BRepAlgo::IsValid(theArgs,theResult,closedSolid,GeomCtrl) ;
461 //===============================================================================
462 // TDraft : test draft, uses AIS2D Viewer
463 // Solid Face Plane Angle Reverse
464 //===============================================================================
465 static Standard_Integer TDraft2d (Draw_Interpretor& , Standard_Integer argc, const char** argv)
467 if (argc < 5) return 1;
468 // argv[1] - TopoDS_Shape Solid
469 // argv[2] - TopoDS_Shape Face
470 // argv[3] - TopoDS_Shape Plane
471 // argv[4] - Standard_Real Angle
472 // argv[5] - Standard_Integer Reverse
474 // sprintf(prefix, argv[1]);
475 Standard_Real anAngle = 0;
476 Standard_Boolean Rev = Standard_False;
477 Standard_Integer rev = 0;
478 TopoDS_Shape Solid = GetShapeFromName2d(argv[1]);
479 TopoDS_Shape face = GetShapeFromName2d(argv[2]);
480 TopoDS_Face Face = TopoDS::Face(face);
481 TopoDS_Shape Plane = GetShapeFromName2d(argv[3]);
482 if (Plane.IsNull ()) {
483 cout << "TEST : Plane is NULL" << endl;
486 anAngle = atof(argv[4]);
487 anAngle = 2*M_PI * anAngle / 360.0;
489 Handle(Geom_Surface) aSurf;
490 // AIS2D_KindOfSurface aSurfType;
491 // Standard_Real Offset;
493 if(argc > 4) { // == 5
495 Rev = (rev)? Standard_True : Standard_False;
498 TopoDS_Face face2 = TopoDS::Face(Plane);
499 // if (!AIS2D::GetPlaneFromFace(face2, aPln, aSurf, aSurfType, Offset))
501 // cout << "TEST : Can't find plane" << endl;
505 aDir = aPln.Axis().Direction();
508 if (Plane.Orientation() == TopAbs_REVERSED)
510 cout << "TEST : gp::Resolution() = " << gp::Resolution() << endl;
512 BRepOffsetAPI_DraftAngle Draft (Solid);
514 if(Abs(anAngle)< Precision::Angular()) {
515 cout << "TEST : NULL angle" << endl;
518 if(Rev) anAngle = - anAngle;
519 Draft.Add (Face, aDir, anAngle, aPln);
521 if (!Draft.IsDone()) {
522 cout << "TEST : Draft Not DONE " << endl;
525 TopTools_ListOfShape Larg;
527 if (!IsValid2d(Larg,Draft.Shape(),Standard_True,Standard_False))
529 cout << "TEST : DesignAlgo returns Not valid" << endl;
533 Handle(AIS2D_InteractiveContext) Ctx = Viewer2dTest::GetAIS2DContext();
534 Handle(AIS2D_ProjShape) ais = new AIS2D_ProjShape();
535 ais->Add(Draft.Shape());
539 // ais->SetColor(DEFAULT_COLOR);
540 // ais->SetMaterial(DEFAULT_MATERIAL);
541 // Display the AIS2D_ProjShape without redraw
542 Ctx->Display(ais, Standard_False);
544 const char *Name = "draft1";
545 Standard_Boolean IsBound = GetMapOfAIS2D().IsBound2(Name);
548 Handle(AIS2D_InteractiveObject) an_object = GetMapOfAIS2D().Find2(Name);
549 // Ctx->Remove(an_object, Standard_True);
550 GetMapOfAIS2D().UnBind2(Name);
552 GetMapOfAIS2D().Bind(ais, Name);
554 Ctx->Display(ais, Standard_True);
558 //============================================================================
560 //============================================================================
561 void Viewer2dTest::MyCommands (Draw_Interpretor& theCommands)
563 DrawTrSurf::BasicCommands(theCommands);
564 const char* group = "2D AIS Viewer - Check Features Operations commands";
566 theCommands.Add("Draft2d",
567 "Draft2d Solid Face Plane Angle Reverse",
568 __FILE__, TDraft2d, group); //Draft_Modification
571 //============================================================================
572 // All 2D AIS Viewer Commands
573 //============================================================================
574 void Viewer2dTest::Commands (Draw_Interpretor& theCommands)
576 Viewer2dTest::GeneralCommands(theCommands);
577 Viewer2dTest::ViewerCommands (theCommands);
578 Viewer2dTest::DisplayCommands(theCommands);
579 Viewer2dTest::ObjectCommands (theCommands);