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