1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_TestOCC.h
4 // Created: 28 Apr 2014
5 // Author: Natalia ERMOLAEVA
7 #include <PartSet_TestOCC.h>
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>
15 #include <ModuleBase_ViewerPrs.h>
16 #include <ModelAPI_Feature.h>
18 #include <SketchPlugin_Line.h>
19 #include <ModelAPI_Document.h>
21 static double myTestDelta;
22 static ResultPtr myTestObject;
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>
33 void PartSet_TestOCC::testSelection(XGUI_Workshop* theWorkshop)
36 PartSet_TestOCC::createTestLine(theWorkshop);
37 PartSet_TestOCC::moveMouse(theWorkshop->viewer()->AISContext(),
38 theWorkshop->viewer()->activeView());
39 PartSet_TestOCC::changeTestLine(theWorkshop);
41 std::shared_ptr<GeomAPI_AISObject> anIO = theWorkshop->displayer()->getAISObject(myTestObject);
43 theWorkshop->viewer()->AISContext()->MoveTo(0, 0, theWorkshop->viewer()->activeView());
44 theWorkshop->viewer()->AISContext()->Select(0, 0, 2500, 2500,
45 theWorkshop->viewer()->activeView());
49 void PartSet_TestOCC::local_selection_change_shape(Handle_AIS_InteractiveContext theContext,
50 Handle_V3d_View theView)
53 gp_Pnt aPnt1(100, 100, 0);
54 gp_Pnt aPnt2(150, 300, 0);
55 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
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*/);
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));
68 // 3. Change selection in the viewer
69 // emulate rectangle selection in the viewer
70 theContext->Select(0, 0, 2500, 2500, theView);
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();
77 anAIS->Redisplay(Standard_True);
78 theContext->RecomputeSelectionOnly(anAIS);
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();
97 std::cout << "Error: The point of the first line should not be selected." << std::endl;
100 void PartSet_TestOCC::local_selection_erase(Handle_AIS_InteractiveContext theContext,
101 Handle_V3d_View theView)
104 gp_Pnt aPnt1(100, 100, 0);
105 gp_Pnt aPnt2(150, 300, 0);
106 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
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*/);
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));
119 // 3. Change selection in the viewer
120 // emulate rectangle selection in the viewer
121 theContext->Select(0, 0, 2500, 2500, theView);
123 theContext->Erase(anAIS);
126 void PartSet_TestOCC::createTestLine(XGUI_Workshop* theWorkshop)
130 ModuleBase_Operation* anOperation = theWorkshop->operationMgr()->currentOperation();
131 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
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
141 std::shared_ptr<SketchPlugin_Feature> aSketch = std::dynamic_pointer_cast<
142 SketchPlugin_Feature>(aPreviewOp->sketch());
143 aSketch->addSub(aFeature);
145 PartSet_Tools::setFeaturePoint(aFeature, 100, 100, SketchPlugin_Line::START_ID());
146 PartSet_Tools::setFeaturePoint(aFeature, 150, 300, SketchPlugin_Line::END_ID());
148 std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
150 XGUI_Displayer* aDisplayer = theWorkshop->displayer();
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);
160 std::list<int> aModes;
161 aModes.push_back(TopAbs_VERTEX);
162 aModes.push_back(TopAbs_EDGE);
163 aDisplayer->activateInLocalContext(aFeature->firstResult(), aModes, true);
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());
172 std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
173 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
175 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), NULL);
177 aDisplayer->redisplay(aFeature, anAIS, true);
180 for (int j = 0; j < 10000000; j++)
181 aVal = aVal/aVal+aVal*2;
183 //std::list<int> aModes;
185 //aModes.push_back(TopAbs_VERTEX);
186 //aModes.push_back(TopAbs_EDGE);
187 //aDisplayer->activateInLocalContext(aFeature, aModes, true);
188 myTestObject = aFeature->firstResult();
190 QList<ObjectPtr> aFeatureList;
191 aFeatureList.append(myTestObject);
192 aDisplayer->setSelected(aFeatureList, true);
196 void PartSet_TestOCC::changeTestLine(XGUI_Workshop* theWorkshop)
201 ResultPtr aFeature = myTestObject;
203 myTestDelta = myTestDelta - 50;
204 double aDelta = myTestDelta;
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);
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;
219 //aModes.push_back(TopAbs_VERTEX);
220 //aModes.push_back(TopAbs_EDGE);
221 //aDisplayer->activateInLocalContext(aFeature, aModes, true);
223 /*QFeatureList aFeatureList;
224 aFeatureList.append(myTestObject);
225 theWorkshop->displayer()->setSelected(aFeatureList, true);*/
227 theWorkshop->displayer()->updateViewer();
230 void PartSet_TestOCC::moveMouse(Handle(AIS_InteractiveContext) theContext, Handle(V3d_View) theView)
232 theContext->MoveTo(10, 10, theView);
233 theContext->Select();