]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_TestOCC.cpp
Salome HOME
OCC Bugs are 24965, 24966
[modules/shaper.git] / src / PartSet / PartSet_TestOCC.cpp
1 // File:        PartSet_TestOCC.h
2 // Created:     28 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_TestOCC.h>
6
7 #include <XGUI_Workshop.h>
8 #include <XGUI_OperationMgr.h>
9 #include <XGUI_Displayer.h>
10 #include <XGUI_ViewerPrs.h>
11 #include <XGUI_ViewerProxy.h>
12 #include <PartSet_OperationSketchLine.h>
13
14 #include <ModelAPI_Feature.h>
15
16 #include <SketchPlugin_Line.h>
17 #include <ModelAPI_Document.h>
18
19 static double myTestDelta;
20 static boost::shared_ptr<ModelAPI_Feature> myTestFeature;
21
22 #include <AIS_InteractiveContext.hxx>
23 #include <AIS_Shape.hxx>
24 #include <V3d_View.hxx>
25 #include <BRepBuilderAPI_MakeEdge.hxx>
26 #include <BRep_Tool.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Edge.hxx>
29 #include <TopoDS.hxx>
30
31 void PartSet_TestOCC::testSelection(XGUI_Workshop* theWorkshop)
32 {
33   if (!myTestFeature) {
34     PartSet_TestOCC::createTestLine(theWorkshop);
35     PartSet_TestOCC::moveMouse(theWorkshop->viewer()->AISContext(),
36                                theWorkshop->viewer()->activeView());
37     PartSet_TestOCC::changeTestLine(theWorkshop);
38   }
39   Handle(AIS_InteractiveObject) anIO = theWorkshop->displayer()->GetAISObject(myTestFeature);
40   if (!anIO.IsNull()) {
41     theWorkshop->viewer()->AISContext()->MoveTo(0, 0, theWorkshop->viewer()->activeView());
42     theWorkshop->viewer()->AISContext()->Select(0, 0, 2500, 2500, theWorkshop->viewer()->activeView());
43   }
44 }
45
46 void PartSet_TestOCC::local_selection_change_shape(Handle_AIS_InteractiveContext theContext,
47                                                    Handle_V3d_View theView)
48 {
49   // 1. Create shape
50   gp_Pnt aPnt1(100, 100, 0);
51   gp_Pnt aPnt2(150, 300, 0);
52   TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
53
54   // 2. Display shape and activate it in the local context
55   Handle(AIS_Shape) anAIS = new AIS_Shape(anEdge);
56   if (!theContext->HasOpenedContext()) {
57     theContext->ClearCurrents(false);
58     theContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
59   }
60   theContext->Display(anAIS, false);
61   theContext->Load(anAIS, -1, true/*allow decomposition*/);
62   theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_VERTEX));
63   theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_EDGE));
64
65   // 3. Change selection in the viewer
66   // emulate rectangle selection in the viewer
67   theContext->Select(0, 0, 2500, 2500, theView);
68
69   // 3. Change the shape of AIS presentation
70   gp_Pnt aPnt3(-100, -100, 0);
71   gp_Pnt aPnt4(200, 200, 0);
72   TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(aPnt3, aPnt4).Edge();
73   anAIS->Set(anEdge2);
74   anAIS->Redisplay(Standard_True);
75   theContext->RecomputeSelectionOnly(anAIS);
76
77   // 4. Check the current viewer selection
78   Handle(AIS_InteractiveContext) aContext = theContext;
79   bool isFirstLinePoint = false;
80   bool isShapeSelected = false;
81   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
82     TopoDS_Shape aShape = aContext->SelectedShape();
83     if (aShape.ShapeType() == TopAbs_VERTEX) {
84       const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
85       if (!aVertex.IsNull()) {
86         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
87         double aD_1 = aPoint.Distance(aPnt1);
88         double aD_2 = aPoint.Distance(aPnt2);
89         isFirstLinePoint = aD_1 < Precision::Confusion() || aD_2 < Precision::Confusion();
90       }
91     }
92   }
93   if (isFirstLinePoint)
94     std::cout << "Error: The point of the first line should not be selected." << std::endl;
95 }
96
97 void PartSet_TestOCC::local_selection_erase(Handle_AIS_InteractiveContext theContext,
98                                     Handle_V3d_View theView)
99 {
100   // 1. Create shape
101   gp_Pnt aPnt1(100, 100, 0);
102   gp_Pnt aPnt2(150, 300, 0);
103   TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
104
105   // 2. Display shape and activate it in the local context
106   Handle(AIS_Shape) anAIS = new AIS_Shape(anEdge);
107   if (!theContext->HasOpenedContext()) {
108     theContext->ClearCurrents(false);
109     theContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
110   }
111   theContext->Display(anAIS, false);
112   theContext->Load(anAIS, -1, true/*allow decomposition*/);
113   theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_VERTEX));
114   theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_EDGE));
115
116   // 3. Change selection in the viewer
117   // emulate rectangle selection in the viewer
118   theContext->Select(0, 0, 2500, 2500, theView);
119
120   theContext->Erase(anAIS);
121 }
122
123 void PartSet_TestOCC::createTestLine(XGUI_Workshop* theWorkshop)
124 {
125   myTestDelta = 80;
126
127   ModuleBase_Operation* anOperation = theWorkshop->operationMgr()->currentOperation();
128   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
129     boost::shared_ptr<ModelAPI_Feature> aSketch;
130
131   if (aPreviewOp) {
132     // create a line
133     boost::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
134     boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature(
135                                                    PartSet_OperationSketchLine::Type().c_str());
136     if (aFeature) // TODO: generate an error if feature was not created
137       aFeature->execute();
138
139     boost::shared_ptr<SketchPlugin_Feature> aSketch = 
140                         boost::dynamic_pointer_cast<SketchPlugin_Feature>(aPreviewOp->sketch());
141     aSketch->addSub(aFeature);
142
143     PartSet_OperationSketchLine::setLinePoint(aFeature, 100, 100, LINE_ATTR_START);
144     PartSet_OperationSketchLine::setLinePoint(aFeature, 150, 300, LINE_ATTR_END);
145
146     boost::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchLine::preview(aFeature);
147
148     XGUI_Displayer* aDisplayer = theWorkshop->displayer();
149     aDisplayer->Redisplay(aFeature, aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
150     std::list<int> aModes;
151     aModes.push_back(TopAbs_VERTEX);
152     aModes.push_back(TopAbs_EDGE);
153     aDisplayer->ActivateInLocalContext(aFeature, aModes, true);
154
155     // change the line
156     /*double aDelta = -200;
157     for (int i = 0; i < 20; i++) {
158       aDelta = aDelta - i*2;
159       PartSet_OperationSketchLine::setLinePoint(aFeature, 100+aDelta, 200+aDelta, LINE_ATTR_START);
160       PartSet_OperationSketchLine::setLinePoint(aFeature, 300+aDelta, 500+aDelta, LINE_ATTR_END);
161       boost::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchLine::preview(aFeature);
162
163       theWorkshop->displayer()->Redisplay(aFeature, aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), true);
164
165       int aVal = 90;
166       for (int j = 0; j < 10000000; j++)
167         aVal = aVal/aVal+aVal*2;
168     }*/
169     //std::list<int> aModes;
170     //aModes.clear();
171     //aModes.push_back(TopAbs_VERTEX);
172     //aModes.push_back(TopAbs_EDGE);
173     //aDisplayer->ActivateInLocalContext(aFeature, aModes, true);
174     myTestFeature = aFeature;
175
176     std::list<XGUI_ViewerPrs> aPrs;
177     aPrs.push_back(XGUI_ViewerPrs(myTestFeature, TopoDS_Shape()));
178     aDisplayer->SetSelected(aPrs, true);
179   }
180 }
181
182 void PartSet_TestOCC::changeTestLine(XGUI_Workshop* theWorkshop)
183 {
184   // change the line
185   if (!myTestFeature)
186     return;
187   boost::shared_ptr<ModelAPI_Feature> aFeature = myTestFeature;
188
189   myTestDelta = myTestDelta - 50;
190   double aDelta = myTestDelta;
191   PartSet_OperationSketchLine::setLinePoint(aFeature, -100/*aDelta*/, -100/*aDelta*/, LINE_ATTR_START);
192   PartSet_OperationSketchLine::setLinePoint(aFeature, 200/*aDelta*2*/, 200/*aDelta*2*/, LINE_ATTR_END);
193   boost::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchLine::preview(aFeature);
194
195   theWorkshop->displayer()->Redisplay(aFeature, aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), true);
196   //std::list<int> aModes;
197   //aModes.clear();
198   //aModes.push_back(TopAbs_VERTEX);
199   //aModes.push_back(TopAbs_EDGE);
200   //aDisplayer->ActivateInLocalContext(aFeature, aModes, true);
201
202   /*std::list<XGUI_ViewerPrs> aPrs;
203   aPrs.push_back(XGUI_ViewerPrs(myTestFeature, TopoDS_Shape()));
204   theWorkshop->displayer()->SetSelected(aPrs, true);*/
205
206   theWorkshop->displayer()->UpdateViewer();
207 }
208
209 void PartSet_TestOCC::moveMouse(Handle(AIS_InteractiveContext) theContext, Handle(V3d_View) theView)
210 {
211   theContext->MoveTo(10, 10, theView);
212   theContext->Select();
213 }