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