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>
14 #include <PartSet_OperationSketchBase.h>
16 #include <ModuleBase_ViewerPrs.h>
17 #include <ModelAPI_Feature.h>
19 #include <SketchPlugin_Line.h>
20 #include <ModelAPI_Document.h>
22 static double myTestDelta;
23 static ResultPtr myTestObject;
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>
34 void PartSet_TestOCC::testSelection(XGUI_Workshop* theWorkshop)
37 PartSet_TestOCC::createTestLine(theWorkshop);
38 PartSet_TestOCC::moveMouse(theWorkshop->viewer()->AISContext(),
39 theWorkshop->viewer()->activeView());
40 PartSet_TestOCC::changeTestLine(theWorkshop);
42 std::shared_ptr<GeomAPI_AISObject> anIO = theWorkshop->displayer()->getAISObject(myTestObject);
44 theWorkshop->viewer()->AISContext()->MoveTo(0, 0, theWorkshop->viewer()->activeView());
45 theWorkshop->viewer()->AISContext()->Select(0, 0, 2500, 2500,
46 theWorkshop->viewer()->activeView());
50 void PartSet_TestOCC::local_selection_change_shape(Handle_AIS_InteractiveContext theContext,
51 Handle_V3d_View theView)
54 gp_Pnt aPnt1(100, 100, 0);
55 gp_Pnt aPnt2(150, 300, 0);
56 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
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*/);
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));
69 // 3. Change selection in the viewer
70 // emulate rectangle selection in the viewer
71 theContext->Select(0, 0, 2500, 2500, theView);
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();
78 anAIS->Redisplay(Standard_True);
79 theContext->RecomputeSelectionOnly(anAIS);
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();
98 std::cout << "Error: The point of the first line should not be selected." << std::endl;
101 void PartSet_TestOCC::local_selection_erase(Handle_AIS_InteractiveContext theContext,
102 Handle_V3d_View theView)
105 gp_Pnt aPnt1(100, 100, 0);
106 gp_Pnt aPnt2(150, 300, 0);
107 TopoDS_Edge anEdge = BRepBuilderAPI_MakeEdge(aPnt1, aPnt2).Edge();
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*/);
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));
120 // 3. Change selection in the viewer
121 // emulate rectangle selection in the viewer
122 theContext->Select(0, 0, 2500, 2500, theView);
124 theContext->Erase(anAIS);
127 void PartSet_TestOCC::createTestLine(XGUI_Workshop* theWorkshop)
131 ModuleBase_Operation* anOperation = theWorkshop->operationMgr()->currentOperation();
132 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
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
142 std::shared_ptr<SketchPlugin_Feature> aSketch = std::dynamic_pointer_cast<
143 SketchPlugin_Feature>(aPreviewOp->sketch());
144 aSketch->addSub(aFeature);
146 PartSet_Tools::setFeaturePoint(aFeature, 100, 100, SketchPlugin_Line::START_ID());
147 PartSet_Tools::setFeaturePoint(aFeature, 150, 300, SketchPlugin_Line::END_ID());
149 std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
151 XGUI_Displayer* aDisplayer = theWorkshop->displayer();
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);
161 std::list<int> aModes;
162 aModes.push_back(TopAbs_VERTEX);
163 aModes.push_back(TopAbs_EDGE);
164 aDisplayer->activateInLocalContext(aFeature->firstResult(), aModes, true);
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());
173 std::shared_ptr<GeomAPI_Shape> aPreview = PartSet_OperationSketchBase::preview(aFeature);
174 Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation(
176 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), NULL);
178 aDisplayer->redisplay(aFeature, anAIS, true);
181 for (int j = 0; j < 10000000; j++)
182 aVal = aVal/aVal+aVal*2;
184 //std::list<int> aModes;
186 //aModes.push_back(TopAbs_VERTEX);
187 //aModes.push_back(TopAbs_EDGE);
188 //aDisplayer->activateInLocalContext(aFeature, aModes, true);
189 myTestObject = aFeature->firstResult();
191 QList<ObjectPtr> aFeatureList;
192 aFeatureList.append(myTestObject);
193 aDisplayer->setSelected(aFeatureList, true);
197 void PartSet_TestOCC::changeTestLine(XGUI_Workshop* theWorkshop)
202 ResultPtr aFeature = myTestObject;
204 myTestDelta = myTestDelta - 50;
205 double aDelta = myTestDelta;
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);
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;
220 //aModes.push_back(TopAbs_VERTEX);
221 //aModes.push_back(TopAbs_EDGE);
222 //aDisplayer->activateInLocalContext(aFeature, aModes, true);
224 /*QFeatureList aFeatureList;
225 aFeatureList.append(myTestObject);
226 theWorkshop->displayer()->setSelected(aFeatureList, true);*/
228 theWorkshop->displayer()->updateViewer();
231 void PartSet_TestOCC::moveMouse(Handle(AIS_InteractiveContext) theContext, Handle(V3d_View) theView)
233 theContext->MoveTo(10, 10, theView);
234 theContext->Select();