Salome HOME
Move generation of AIS presentation into SketchPlugin
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Feature.cpp
1 #include "SketchPlugin_Feature.h"
2 #include "SketchPlugin_Sketch.h"
3 #include <ModelAPI_Document.h>
4 #include <ModelAPI_Data.h>
5 #include <ModelAPI_Object.h>
6 #include <ModelAPI_AttributeRefList.h>
7
8 #include <AIS_InteractiveObject.hxx>
9 #include <AIS_Shape.hxx>
10 #include <TopoDS_Shape.hxx>
11
12 SketchPlugin_Feature::SketchPlugin_Feature()
13 {
14   mySketch = 0;
15 }
16
17 void SketchPlugin_Feature::setPreview(const boost::shared_ptr<GeomAPI_Shape>& theShape)
18 {
19   myPreview = theShape;
20 }
21
22 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Feature::getPreview() const
23 {
24   return myPreview;
25 }
26
27 SketchPlugin_Sketch* SketchPlugin_Feature::sketch()
28 {
29   if (!mySketch) {
30     // find sketch that references to this feature
31     int aSketches = document()->size("Construction");
32     for(int a = 0; a < aSketches && !mySketch; a++) {
33       boost::shared_ptr<SketchPlugin_Sketch> aSketch = boost::
34         dynamic_pointer_cast<SketchPlugin_Sketch>(document()->feature("Construction", a, true));
35       if (aSketch) {
36         std::list<FeaturePtr > aList = 
37           aSketch->data()->reflist(SKETCH_ATTR_FEATURES)->list();
38         std::list<FeaturePtr >::iterator aSub = aList.begin();
39         for(; aSub != aList.end(); aSub++) {
40           if ((*aSub)->data()->isEqual(data())) {
41             mySketch = aSketch.get();
42             break;
43           }
44         }
45       }
46     }
47   }
48   return mySketch;
49 }
50
51 Handle(AIS_InteractiveObject) SketchPlugin_Feature::getAISShape(Handle(AIS_InteractiveObject) thePrevious)
52 {
53   boost::shared_ptr<GeomAPI_Shape> aPreview = preview();
54
55   Handle(AIS_InteractiveObject) anAIS = thePrevious;
56   const TopoDS_Shape& aShape = aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape();
57   if (!anAIS.IsNull())
58   {
59     Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAIS);
60     if (!aShapeAIS.IsNull()) {
61       // if the AIS object is displayed in the opened local context in some mode, additional
62       // AIS sub objects are created there. They should be rebuild for correct selecting.
63       // It is possible to correct it by closing local context before the shape set and opening
64       // after. Another workaround to thrown down the selection and reselecting the AIS.
65       // If there was a problem here, try the first solution with close/open local context.
66
67       aShapeAIS->Set(aShape);
68       aShapeAIS->Redisplay(Standard_True);
69     }
70   }
71   else
72   {
73     anAIS = new AIS_Shape(aShape);
74   }
75   return anAIS;
76 }