Salome HOME
47e3cf87052a94de060529151200c4d155d9cdf0
[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_ViewerProxy.h>
11 #include <PartSet_Tools.h>
12 #include <PartSet_OperationSketchBase.h>
13
14 #include <ModuleBase_ViewerPrs.h>
15 #include <ModelAPI_Feature.h>
16
17 #include <SketchPlugin_Line.h>
18 #include <ModelAPI_Document.h>
19
20 static double myTestDelta;
21 static ResultPtr myTestObject;
22
23 #include <AIS_InteractiveContext.hxx>
24 #include <AIS_Shape.hxx>
25 #include <V3d_View.hxx>
26 #include <BRepBuilderAPI_MakeEdge.hxx>
27 #include <BRep_Tool.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <TopoDS_Edge.hxx>
30 #include <TopoDS.hxx>
31
32 void PartSet_TestOCC::testSelection(XGUI_Workshop* theWorkshop)
33 {
34   if (!myTestObject) {
35     PartSet_TestOCC::createTestLine(theWorkshop);
36     PartSet_TestOCC::moveMouse(theWorkshop->viewer()->AISContext(),
37                                theWorkshop->viewer()->activeView());
38     PartSet_TestOCC::changeTestLine(theWorkshop);
39   }
40   boost::shared_ptr<GeomAPI_AISObject> anIO = theWorkshop->displayer()->getAISObject(myTestObject);
41   if (!anIO->empty()) {
42     theWorkshop->viewer()->AISContext()->MoveTo(0, 0, theWorkshop->viewer()->activeView());
43     theWorkshop->viewer()->AISContext()->Select(0, 0, 2500, 2500, theWorkshop->viewer()->activeView());
44   }
45 }
46
47 void PartSet_TestOCC::local_selection_change_shape(Handle_AIS_InteractiveContext theContext,
48                                                    Handle_V3d_View theView)
49 {
50   // 1. Create shape
51   gp_Pnt aPnt1(100, 100, 0);
52   gp_Pnt aPnt2(150, 300, 0);
53   TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
54
55   // 2. Display shape and activate it in the local context
56   Handle(AIS_Shape) anAIS = new AIS_Shape(anEdge);
57   if (!theContext->HasOpenedContext()) {
58     theContext->ClearCurrents(false);
59     theContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
60   }
61   theContext->Display(anAIS, false);
62   theContext->Load(anAIS, -1, true/*allow decomposition*/);
63   theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_VERTEX));
64   theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_EDGE));
65
66   // 3. Change selection in the viewer
67   // emulate rectangle selection in the viewer
68   theContext->Select(0, 0, 2500, 2500, theView);
69
70   // 3. Change the shape of AIS presentation
71   gp_Pnt aPnt3(-100, -100, 0);
72   gp_Pnt aPnt4(200, 200, 0);
73   TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(aPnt3, aPnt4).Edge();
74   anAIS->Set(anEdge2);
75   anAIS->Redisplay(Standard_True);
76   theContext->RecomputeSelectionOnly(anAIS);
77
78   // 4. Check the current viewer selection
79   Handle(AIS_InteractiveContext) aContext = theContext;
80   bool isFirstLinePoint = false;
81   bool isShapeSelected = false;
82   for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
83     TopoDS_Shape aShape = aContext->SelectedShape();
84     if (aShape.ShapeType() == TopAbs_VERTEX) {
85       const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
86       if (!aVertex.IsNull()) {
87         gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
88         double aD_1 = aPoint.Distance(aPnt1);
89         double aD_2 = aPoint.Distance(aPnt2);
90         isFirstLinePoint = aD_1 < Precision::Confusion() || aD_2 < Precision::Confusion();
91       }
92     }
93   }
94   if (isFirstLinePoint)
95     std::cout << "Error: The point of the first line should not be selected." << std::endl;
96 }
97
98 void PartSet_TestOCC::local_selection_erase(Handle_AIS_InteractiveContext theContext,
99                                     Handle_V3d_View theView)
100 {
101   // 1. Create shape
102   gp_Pnt aPnt1(100, 100, 0);
103   gp_Pnt aPnt2(150, 300, 0);
104   TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
105
106   // 2. Display shape and activate it in the local context
107   Handle(AIS_Shape) anAIS = new AIS_Shape(anEdge);
108   if (!theContext->HasOpenedContext()) {
109     theContext->ClearCurrents(false);
110     theContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
111   }
112   theContext->Display(anAIS, false);
113   theContext->Load(anAIS, -1, true/*allow decomposition*/);
114   theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_VERTEX));
115   theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_EDGE));
116
117   // 3. Change selection in the viewer
118   // emulate rectangle selection in the viewer
119   theContext->Select(0, 0, 2500, 2500, theView);
120
121   theContext->Erase(anAIS);
122 }
123
124 void PartSet_TestOCC::createTestLine(XGUI_Workshop* theWorkshop)
125 {
126   myTestDelta = 80;
127
128   ModuleBase_Operation* anOperation = theWorkshop->operationMgr()->currentOperation();
129   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
130     FeaturePtr aSketch;
131
132   if (aPreviewOp) {
133     // create a line
134     boost::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
135     FeaturePtr aFeature = aDoc->addFeature(SketchPlugin_Line::ID());
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_Tools::setFeaturePoint(aFeature, 100, 100, SketchPlugin_Line::START_ID());
144     PartSet_Tools::setFeaturePoint(aFeature, 150, 300, SketchPlugin_Line::END_ID());
145
146     boost::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
147
148     XGUI_Displayer* aDisplayer = theWorkshop->displayer();
149
150     boost::shared_ptr<GeomAPI_AISObject> aPrevAIS;
151     boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
152       boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
153     //boost::shared_ptr<GeomAPI_AISObject> anAIS = aSPFeature->getAISObject(aPrevAIS);
154     //if (!anAIS->empty())
155     aDisplayer->display(aFeature, false);
156       //aDisplayer->redisplay(aFeature->firstResult(), anAIS, false);
157
158     std::list<int> aModes;
159     aModes.push_back(TopAbs_VERTEX);
160     aModes.push_back(TopAbs_EDGE);
161     aDisplayer->activateInLocalContext(aFeature->firstResult(), 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, SketchPlugin_Line::START_ID());
168       PartSet_Tools::setFeaturePoint(aFeature, 300+aDelta, 500+aDelta, SketchPlugin_Line::END_ID());
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, 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     myTestObject = aFeature->firstResult();
187
188     QResultList aFeatureList;
189     aFeatureList.append(myTestObject);
190     aDisplayer->setSelected(aFeatureList, true);
191   }
192 }
193
194 void PartSet_TestOCC::changeTestLine(XGUI_Workshop* theWorkshop)
195 {
196   // change the line
197   if (!myTestObject)
198     return;
199   ResultPtr aFeature = myTestObject;
200
201   myTestDelta = myTestDelta - 50;
202   double aDelta = myTestDelta;
203   // TODO
204   //PartSet_Tools::setFeaturePoint(aFeature, -100/*aDelta*/, -100/*aDelta*/, LINE_ATTR_START);
205   //PartSet_Tools::setFeaturePoint(aFeature, 200/*aDelta*2*/, 200/*aDelta*2*/, LINE_ATTR_END);
206   //boost::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
207
208   boost::shared_ptr<GeomAPI_AISObject> aPrevAIS;
209   boost::shared_ptr<SketchPlugin_Feature> aSPFeature = 
210     boost::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
211   //boost::shared_ptr<GeomAPI_AISObject> anAIS = aSPFeature->getAISObject(aPrevAIS);
212   //if (!anAIS->empty())
213   theWorkshop->displayer()->display(aFeature, true);
214   //  theWorkshop->displayer()->redisplay(aFeature, anAIS, 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   /*QFeatureList aFeatureList;
222   aFeatureList.append(myTestObject);
223   theWorkshop->displayer()->setSelected(aFeatureList, 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 }