Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[modules/shaper.git] / src / PartSet / PartSet_TestOCC.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_TestOCC.h
4 // Created:     28 Apr 2014
5 // Author:      Natalia ERMOLAEVA
6
7 #include <PartSet_TestOCC.h>
8
9 #include <XGUI_Workshop.h>
10 #include <XGUI_OperationMgr.h>
11 #include <XGUI_Displayer.h>
12 #include <XGUI_ViewerProxy.h>
13 #include <PartSet_Tools.h>
14
15 #include <ModuleBase_ViewerPrs.h>
16 #include <ModelAPI_Feature.h>
17
18 #include <SketchPlugin_Line.h>
19 #include <ModelAPI_Document.h>
20
21 static double myTestDelta;
22 static ResultPtr myTestObject;
23
24 #include <AIS_InteractiveContext.hxx>
25 #include <AIS_Shape.hxx>
26 #include <V3d_View.hxx>
27 #include <BRepBuilderAPI_MakeEdge.hxx>
28 #include <BRep_Tool.hxx>
29 #include <TopoDS_Shape.hxx>
30 #include <TopoDS_Edge.hxx>
31 #include <TopoDS.hxx>
32
33 void PartSet_TestOCC::testSelection(XGUI_Workshop* theWorkshop)
34 {
35   if (!myTestObject) {
36     PartSet_TestOCC::createTestLine(theWorkshop);
37     PartSet_TestOCC::moveMouse(theWorkshop->viewer()->AISContext(),
38                                theWorkshop->viewer()->activeView());
39     PartSet_TestOCC::changeTestLine(theWorkshop);
40   }
41   std::shared_ptr<GeomAPI_AISObject> anIO = theWorkshop->displayer()->getAISObject(myTestObject);
42   if (!anIO->empty()) {
43     theWorkshop->viewer()->AISContext()->MoveTo(0, 0, theWorkshop->viewer()->activeView());
44     theWorkshop->viewer()->AISContext()->Select(0, 0, 2500, 2500,
45                                                 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     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->moduleDocument();
137     FeaturePtr aFeature = aDoc->addFeature(SketchPlugin_Line::ID());
138     if (aFeature)  // TODO: generate an error if feature was not created
139       aFeature->execute();
140
141     std::shared_ptr<SketchPlugin_Feature> aSketch = std::dynamic_pointer_cast<
142         SketchPlugin_Feature>(aPreviewOp->sketch());
143     aSketch->addSub(aFeature);
144
145     PartSet_Tools::setFeaturePoint(aFeature, 100, 100, SketchPlugin_Line::START_ID());
146     PartSet_Tools::setFeaturePoint(aFeature, 150, 300, SketchPlugin_Line::END_ID());
147
148     std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
149
150     XGUI_Displayer* aDisplayer = theWorkshop->displayer();
151
152     std::shared_ptr<GeomAPI_AISObject> aPrevAIS;
153     std::shared_ptr<SketchPlugin_Feature> aSPFeature = std::dynamic_pointer_cast<
154         SketchPlugin_Feature>(aFeature);
155     //std::shared_ptr<GeomAPI_AISObject> anAIS = aSPFeature->getAISObject(aPrevAIS);
156     //if (!anAIS->empty())
157     aDisplayer->display(aFeature, false);
158     //aDisplayer->redisplay(aFeature->firstResult(), anAIS, false);
159
160     std::list<int> aModes;
161     aModes.push_back(TopAbs_VERTEX);
162     aModes.push_back(TopAbs_EDGE);
163     aDisplayer->activateInLocalContext(aFeature->firstResult(), aModes, true);
164
165     // change the line
166     /*double aDelta = -200;
167      for (int i = 0; i < 20; i++) {
168      aDelta = aDelta - i*2;
169      PartSet_Tools::setFeaturePoint(aFeature, 100+aDelta, 200+aDelta, SketchPlugin_Line::START_ID());
170      PartSet_Tools::setFeaturePoint(aFeature, 300+aDelta, 500+aDelta, SketchPlugin_Line::END_ID());
171
172      std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
173      Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
174      aFeature, aSketch,
175      aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), NULL);
176      if (!anAIS.IsNull())
177      aDisplayer->redisplay(aFeature, anAIS, true);
178
179      int aVal = 90;
180      for (int j = 0; j < 10000000; j++)
181      aVal = aVal/aVal+aVal*2;
182      }*/
183     //std::list<int> aModes;
184     //aModes.clear();
185     //aModes.push_back(TopAbs_VERTEX);
186     //aModes.push_back(TopAbs_EDGE);
187     //aDisplayer->activateInLocalContext(aFeature, aModes, true);
188     myTestObject = aFeature->firstResult();
189
190     QList<ObjectPtr> aFeatureList;
191     aFeatureList.append(myTestObject);
192     aDisplayer->setSelected(aFeatureList, true);
193   }
194 }
195
196 void PartSet_TestOCC::changeTestLine(XGUI_Workshop* theWorkshop)
197 {
198   // change the line
199   if (!myTestObject)
200     return;
201   ResultPtr aFeature = myTestObject;
202
203   myTestDelta = myTestDelta - 50;
204   double aDelta = myTestDelta;
205   // TODO
206   //PartSet_Tools::setFeaturePoint(aFeature, -100/*aDelta*/, -100/*aDelta*/, LINE_ATTR_START);
207   //PartSet_Tools::setFeaturePoint(aFeature, 200/*aDelta*2*/, 200/*aDelta*2*/, LINE_ATTR_END);
208   //std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
209
210   std::shared_ptr<GeomAPI_AISObject> aPrevAIS;
211   std::shared_ptr<SketchPlugin_Feature> aSPFeature = std::dynamic_pointer_cast<
212       SketchPlugin_Feature>(aFeature);
213   //std::shared_ptr<GeomAPI_AISObject> anAIS = aSPFeature->getAISObject(aPrevAIS);
214   //if (!anAIS->empty())
215   theWorkshop->displayer()->display(aFeature, true);
216   //  theWorkshop->displayer()->redisplay(aFeature, anAIS, true);
217   //std::list<int> aModes;
218   //aModes.clear();
219   //aModes.push_back(TopAbs_VERTEX);
220   //aModes.push_back(TopAbs_EDGE);
221   //aDisplayer->activateInLocalContext(aFeature, aModes, true);
222
223   /*QFeatureList aFeatureList;
224    aFeatureList.append(myTestObject);
225    theWorkshop->displayer()->setSelected(aFeatureList, true);*/
226
227   theWorkshop->displayer()->updateViewer();
228 }
229
230 void PartSet_TestOCC::moveMouse(Handle(AIS_InteractiveContext) theContext, Handle(V3d_View) theView)
231 {
232   theContext->MoveTo(10, 10, theView);
233   theContext->Select();
234 }