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