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_ViewerProxy.h>
11 #include <PartSet_Tools.h>
12 #include <PartSet_OperationSketchBase.h>
14 #include <ModuleBase_ViewerPrs.h>
15 #include <ModelAPI_Feature.h>
17 #include <SketchPlugin_Line.h>
18 #include <ModelAPI_Document.h>
20 static double myTestDelta;
21 static ResultPtr myTestObject;
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>
32 void PartSet_TestOCC::testSelection(XGUI_Workshop* theWorkshop)
35 PartSet_TestOCC::createTestLine(theWorkshop);
36 PartSet_TestOCC::moveMouse(theWorkshop->viewer()->AISContext(),
37 theWorkshop->viewer()->activeView());
38 PartSet_TestOCC::changeTestLine(theWorkshop);
40 std::shared_ptr<GeomAPI_AISObject> anIO = theWorkshop->displayer()->getAISObject(myTestObject);
42 theWorkshop->viewer()->AISContext()->MoveTo(0, 0, theWorkshop->viewer()->activeView());
43 theWorkshop->viewer()->AISContext()->Select(0, 0, 2500, 2500,
44 theWorkshop->viewer()->activeView());
48 void PartSet_TestOCC::local_selection_change_shape(Handle_AIS_InteractiveContext theContext,
49 Handle_V3d_View theView)
52 gp_Pnt aPnt1(100, 100, 0);
53 gp_Pnt aPnt2(150, 300, 0);
54 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
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*/);
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));
67 // 3. Change selection in the viewer
68 // emulate rectangle selection in the viewer
69 theContext->Select(0, 0, 2500, 2500, theView);
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();
76 anAIS->Redisplay(Standard_True);
77 theContext->RecomputeSelectionOnly(anAIS);
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();
96 std::cout << "Error: The point of the first line should not be selected." << std::endl;
99 void PartSet_TestOCC::local_selection_erase(Handle_AIS_InteractiveContext theContext,
100 Handle_V3d_View theView)
103 gp_Pnt aPnt1(100, 100, 0);
104 gp_Pnt aPnt2(150, 300, 0);
105 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
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*/);
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));
118 // 3. Change selection in the viewer
119 // emulate rectangle selection in the viewer
120 theContext->Select(0, 0, 2500, 2500, theView);
122 theContext->Erase(anAIS);
125 void PartSet_TestOCC::createTestLine(XGUI_Workshop* theWorkshop)
129 ModuleBase_Operation* anOperation = theWorkshop->operationMgr()->currentOperation();
130 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
135 std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->moduleDocument();
136 FeaturePtr aFeature = aDoc->addFeature(SketchPlugin_Line::ID());
137 if (aFeature) // TODO: generate an error if feature was not created
140 std::shared_ptr<SketchPlugin_Feature> aSketch = std::dynamic_pointer_cast<
141 SketchPlugin_Feature>(aPreviewOp->sketch());
142 aSketch->addSub(aFeature);
144 PartSet_Tools::setFeaturePoint(aFeature, 100, 100, SketchPlugin_Line::START_ID());
145 PartSet_Tools::setFeaturePoint(aFeature, 150, 300, SketchPlugin_Line::END_ID());
147 std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
149 XGUI_Displayer* aDisplayer = theWorkshop->displayer();
151 std::shared_ptr<GeomAPI_AISObject> aPrevAIS;
152 std::shared_ptr<SketchPlugin_Feature> aSPFeature = std::dynamic_pointer_cast<
153 SketchPlugin_Feature>(aFeature);
154 //std::shared_ptr<GeomAPI_AISObject> anAIS = aSPFeature->getAISObject(aPrevAIS);
155 //if (!anAIS->empty())
156 aDisplayer->display(aFeature, false);
157 //aDisplayer->redisplay(aFeature->firstResult(), anAIS, false);
159 std::list<int> aModes;
160 aModes.push_back(TopAbs_VERTEX);
161 aModes.push_back(TopAbs_EDGE);
162 aDisplayer->activateInLocalContext(aFeature->firstResult(), aModes, true);
165 /*double aDelta = -200;
166 for (int i = 0; i < 20; i++) {
167 aDelta = aDelta - i*2;
168 PartSet_Tools::setFeaturePoint(aFeature, 100+aDelta, 200+aDelta, SketchPlugin_Line::START_ID());
169 PartSet_Tools::setFeaturePoint(aFeature, 300+aDelta, 500+aDelta, SketchPlugin_Line::END_ID());
171 std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
172 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
174 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), NULL);
176 aDisplayer->redisplay(aFeature, anAIS, true);
179 for (int j = 0; j < 10000000; j++)
180 aVal = aVal/aVal+aVal*2;
182 //std::list<int> aModes;
184 //aModes.push_back(TopAbs_VERTEX);
185 //aModes.push_back(TopAbs_EDGE);
186 //aDisplayer->activateInLocalContext(aFeature, aModes, true);
187 myTestObject = aFeature->firstResult();
189 QList<ObjectPtr> aFeatureList;
190 aFeatureList.append(myTestObject);
191 aDisplayer->setSelected(aFeatureList, true);
195 void PartSet_TestOCC::changeTestLine(XGUI_Workshop* theWorkshop)
200 ResultPtr aFeature = myTestObject;
202 myTestDelta = myTestDelta - 50;
203 double aDelta = myTestDelta;
205 //PartSet_Tools::setFeaturePoint(aFeature, -100/*aDelta*/, -100/*aDelta*/, LINE_ATTR_START);
206 //PartSet_Tools::setFeaturePoint(aFeature, 200/*aDelta*2*/, 200/*aDelta*2*/, LINE_ATTR_END);
207 //std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
209 std::shared_ptr<GeomAPI_AISObject> aPrevAIS;
210 std::shared_ptr<SketchPlugin_Feature> aSPFeature = std::dynamic_pointer_cast<
211 SketchPlugin_Feature>(aFeature);
212 //std::shared_ptr<GeomAPI_AISObject> anAIS = aSPFeature->getAISObject(aPrevAIS);
213 //if (!anAIS->empty())
214 theWorkshop->displayer()->display(aFeature, true);
215 // theWorkshop->displayer()->redisplay(aFeature, anAIS, true);
216 //std::list<int> aModes;
218 //aModes.push_back(TopAbs_VERTEX);
219 //aModes.push_back(TopAbs_EDGE);
220 //aDisplayer->activateInLocalContext(aFeature, aModes, true);
222 /*QFeatureList aFeatureList;
223 aFeatureList.append(myTestObject);
224 theWorkshop->displayer()->setSelected(aFeatureList, true);*/
226 theWorkshop->displayer()->updateViewer();
229 void PartSet_TestOCC::moveMouse(Handle(AIS_InteractiveContext) theContext, Handle(V3d_View) theView)
231 theContext->MoveTo(10, 10, theView);
232 theContext->Select();