1 // File: PartSet_TestOCC.h
2 // Created: 28 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_TestOCC.h>
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_OperationSketchLine.h>
14 #include <ModelAPI_Feature.h>
16 #include <SketchPlugin_Line.h>
17 #include <ModelAPI_Document.h>
19 static double myTestDelta;
20 static boost::shared_ptr<ModelAPI_Feature> myTestFeature;
22 #include <AIS_InteractiveContext.hxx>
23 #include <AIS_Shape.hxx>
24 #include <V3d_View.hxx>
25 #include <BRepBuilderAPI_MakeEdge.hxx>
26 #include <BRep_Tool.hxx>
27 #include <TopoDS_Shape.hxx>
28 #include <TopoDS_Edge.hxx>
31 void PartSet_TestOCC::testSelection(XGUI_Workshop* theWorkshop)
34 PartSet_TestOCC::createTestLine(theWorkshop);
35 PartSet_TestOCC::moveMouse(theWorkshop->viewer()->AISContext(),
36 theWorkshop->viewer()->activeView());
37 PartSet_TestOCC::changeTestLine(theWorkshop);
39 Handle(AIS_InteractiveObject) anIO = theWorkshop->displayer()->GetAISObject(myTestFeature);
41 theWorkshop->viewer()->AISContext()->MoveTo(0, 0, theWorkshop->viewer()->activeView());
42 theWorkshop->viewer()->AISContext()->Select(0, 0, 2500, 2500, theWorkshop->viewer()->activeView());
46 void PartSet_TestOCC::testSelection_OCC(Handle_AIS_InteractiveContext theContext,
47 Handle_V3d_View theView)
50 gp_Pnt aPnt1(100, 100, 0);
51 gp_Pnt aPnt2(150, 300, 0);
52 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
54 // 2. Display shape and activate it in the local context
55 Handle(AIS_Shape) anAIS = new AIS_Shape(anEdge);
56 if (!theContext->HasOpenedContext()) {
57 theContext->ClearCurrents(false);
58 theContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
60 theContext->Display(anAIS, false);
61 theContext->Load(anAIS, -1, true/*allow decomposition*/);
62 theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_VERTEX));
63 theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_EDGE));
65 // 3. Change selection in the viewer
66 // emulate rectangle selection in the viewer
67 theContext->Select(0, 0, 2500, 2500, theView);
69 // 3. Change the shape of AIS presentation
70 gp_Pnt aPnt3(-100, -100, 0);
71 gp_Pnt aPnt4(200, 200, 0);
72 TopoDS_Edge anEdge2 = BRepBuilderAPI_MakeEdge(aPnt3, aPnt4).Edge();
74 anAIS->Redisplay(Standard_True);
75 theContext->RecomputeSelectionOnly(anAIS);
77 // 4. Check the current viewer selection
78 Handle(AIS_InteractiveContext) aContext = theContext;
79 bool isFirstLinePoint = false;
80 bool isShapeSelected = false;
81 for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected()) {
82 TopoDS_Shape aShape = aContext->SelectedShape();
83 if (aShape.ShapeType() == TopAbs_VERTEX) {
84 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
85 if (!aVertex.IsNull()) {
86 gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
87 double aD_1 = aPoint.Distance(aPnt1);
88 double aD_2 = aPoint.Distance(aPnt2);
89 isFirstLinePoint = aD_1 < Precision::Confusion() || aD_2 < Precision::Confusion();
94 std::cout << "Error: The point of the first line should not be selected." << std::endl;
97 void PartSet_TestOCC::testErase_OCC(Handle_AIS_InteractiveContext theContext,
98 Handle_V3d_View theView)
101 gp_Pnt aPnt1(100, 100, 0);
102 gp_Pnt aPnt2(150, 300, 0);
103 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
105 // 2. Display shape and activate it in the local context
106 Handle(AIS_Shape) anAIS = new AIS_Shape(anEdge);
107 if (!theContext->HasOpenedContext()) {
108 theContext->ClearCurrents(false);
109 theContext->OpenLocalContext(false/*use displayed objects*/, true/*allow shape decomposition*/);
111 theContext->Display(anAIS, false);
112 theContext->Load(anAIS, -1, true/*allow decomposition*/);
113 theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_VERTEX));
114 theContext->Activate(anAIS, AIS_Shape::SelectionMode(TopAbs_EDGE));
116 // 3. Change selection in the viewer
117 // emulate rectangle selection in the viewer
118 theContext->Select(0, 0, 2500, 2500, theView);
120 theContext->Erase(anAIS);
123 void PartSet_TestOCC::createTestLine(XGUI_Workshop* theWorkshop)
127 ModuleBase_Operation* anOperation = theWorkshop->operationMgr()->currentOperation();
128 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
129 boost::shared_ptr<ModelAPI_Feature> aSketch;
133 boost::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_PluginManager::get()->rootDocument();
134 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature(
135 PartSet_OperationSketchLine::Type().c_str());
136 if (aFeature) // TODO: generate an error if feature was not created
139 boost::shared_ptr<SketchPlugin_Feature> aSketch =
140 boost::dynamic_pointer_cast<SketchPlugin_Feature>(aPreviewOp->sketch());
141 aSketch->addSub(aFeature);
143 PartSet_OperationSketchLine::setLinePoint(aFeature, 100, 100, LINE_ATTR_START);
144 PartSet_OperationSketchLine::setLinePoint(aFeature, 150, 300, LINE_ATTR_END);
146 boost::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchLine::preview(aFeature);
148 XGUI_Displayer* aDisplayer = theWorkshop->displayer();
149 aDisplayer->Redisplay(aFeature, aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
150 std::list<int> aModes;
151 aModes.push_back(TopAbs_VERTEX);
152 aModes.push_back(TopAbs_EDGE);
153 aDisplayer->ActivateInLocalContext(aFeature, aModes, true);
156 /*double aDelta = -200;
157 for (int i = 0; i < 20; i++) {
158 aDelta = aDelta - i*2;
159 PartSet_OperationSketchLine::setLinePoint(aFeature, 100+aDelta, 200+aDelta, LINE_ATTR_START);
160 PartSet_OperationSketchLine::setLinePoint(aFeature, 300+aDelta, 500+aDelta, LINE_ATTR_END);
161 boost::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchLine::preview(aFeature);
163 theWorkshop->displayer()->Redisplay(aFeature, aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), true);
166 for (int j = 0; j < 10000000; j++)
167 aVal = aVal/aVal+aVal*2;
169 //std::list<int> aModes;
171 //aModes.push_back(TopAbs_VERTEX);
172 //aModes.push_back(TopAbs_EDGE);
173 //aDisplayer->ActivateInLocalContext(aFeature, aModes, true);
174 myTestFeature = aFeature;
176 std::list<XGUI_ViewerPrs> aPrs;
177 aPrs.push_back(XGUI_ViewerPrs(myTestFeature, TopoDS_Shape()));
178 aDisplayer->SetSelected(aPrs, true);
182 void PartSet_TestOCC::changeTestLine(XGUI_Workshop* theWorkshop)
187 boost::shared_ptr<ModelAPI_Feature> aFeature = myTestFeature;
189 myTestDelta = myTestDelta - 50;
190 double aDelta = myTestDelta;
191 PartSet_OperationSketchLine::setLinePoint(aFeature, -100/*aDelta*/, -100/*aDelta*/, LINE_ATTR_START);
192 PartSet_OperationSketchLine::setLinePoint(aFeature, 200/*aDelta*2*/, 200/*aDelta*2*/, LINE_ATTR_END);
193 boost::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchLine::preview(aFeature);
195 theWorkshop->displayer()->Redisplay(aFeature, aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), true);
196 //std::list<int> aModes;
198 //aModes.push_back(TopAbs_VERTEX);
199 //aModes.push_back(TopAbs_EDGE);
200 //aDisplayer->ActivateInLocalContext(aFeature, aModes, true);
202 /*std::list<XGUI_ViewerPrs> aPrs;
203 aPrs.push_back(XGUI_ViewerPrs(myTestFeature, TopoDS_Shape()));
204 theWorkshop->displayer()->SetSelected(aPrs, true);*/
206 theWorkshop->displayer()->UpdateViewer();
209 void PartSet_TestOCC::moveMouse(Handle(AIS_InteractiveContext) theContext, Handle(V3d_View) theView)
211 theContext->MoveTo(10, 10, theView);
212 theContext->Select();