Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Feature.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Feature.h
4 // Created:     27 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #ifndef SketchPlugin_Feature_H_
8 #define SketchPlugin_Feature_H_
9
10 #include "SketchPlugin.h"
11 #include <ModelAPI_CompositeFeature.h>
12 #include <GeomAPI_Shape.h>
13 #include <GeomAPI_AISObject.h>
14 #include <ModelAPI_Document.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_AttributeBoolean.h>
17 #include <GeomAPI_ICustomPrs.h>
18
19 #include <Config_PropManager.h>
20
21 #define SKETCH_EDGE_COLOR "#ff0000"
22 #define SKETCH_POINT_COLOR "#ff0000"
23 #define SKETCH_EXTERNAL_EDGE_COLOR "#00ff00"
24 #define SKETCH_CONSTRUCTION_COLOR "#000000"
25
26 class SketchPlugin_Sketch;
27 class GeomAPI_Pnt2d;
28 class Handle_AIS_InteractiveObject;
29
30 /**\class SketchPlugin_Feature
31  * \ingroup Plugins
32  * \brief Feature for creation of the new feature in PartSet. This is an abstract class to give
33  * an interface to create the sketch feature preview.
34  */
35 class SketchPlugin_Feature : public ModelAPI_Feature, public GeomAPI_ICustomPrs
36 {
37  public:
38   /// Reference to the construction type of the feature
39   inline static const std::string& CONSTRUCTION_ID()
40   {
41     static const std::string MY_CONSTRUCTION_ID("Construction");
42     return MY_CONSTRUCTION_ID;
43   }
44
45   /// Reference to the external edge or vertex as a AttributeSelection
46   inline static const std::string& EXTERNAL_ID()
47   {
48     static const std::string MY_EXTERNAL_ID("External");
49     return MY_EXTERNAL_ID;
50   }
51
52   /// Returns true if this feature must be displayed in the history (top level of Part tree)
53   SKETCHPLUGIN_EXPORT virtual bool isInHistory()
54   {
55     return false;
56   }
57
58   /// Moves the feature
59   /// \param theDeltaX the delta for X coordinate is moved
60   /// \param theDeltaY the delta for Y coordinate is moved
61   SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY) = 0;
62
63   /// Return the distance between the feature and the point
64   /// \param thePoint the point
65   virtual double distanceToPoint(const std::shared_ptr<GeomAPI_Pnt2d>& thePoint) = 0;
66
67   /// Construction result is allways recomuted on the fly
68   SKETCHPLUGIN_EXPORT virtual bool isPersistentResult() {return false;}
69
70   /// Returns true is sketch element is under the rigid constraint
71   SKETCHPLUGIN_EXPORT virtual bool isFixed() {return false;}
72
73   /// Returns true of the feature is created basing on the external shape of not-this-sketch object
74   inline bool isExternal() const
75   {
76     AttributeSelectionPtr aAttr = data()->selection(EXTERNAL_ID());
77     if (aAttr)
78       return aAttr->context().get() != NULL;
79     return false;
80   }
81
82   /// Customize presentation of the feature
83   virtual void customisePresentation(AISObjectPtr thePrs)
84   {
85     std::vector<int> aRGB;
86   
87     int aShapeType = thePrs->getShapeType();
88     if (aShapeType != 6/*an edge*/ && aShapeType != 7/*a vertex*/)
89       return;
90
91     std::shared_ptr<ModelAPI_AttributeBoolean> aConstructionAttr =
92                                    data()->boolean(SketchPlugin_Feature::CONSTRUCTION_ID());
93     bool isConstruction = aConstructionAttr.get() != NULL && aConstructionAttr->value();
94     if (aShapeType == 6) { // if this is an edge
95       if (isConstruction) {
96         thePrs->setWidth(1);
97         thePrs->setLineStyle(3);
98         aRGB = Config_PropManager::color("Visualization", "sketch_construction_color",
99                                          SKETCH_CONSTRUCTION_COLOR);
100       }
101       else {
102         thePrs->setWidth(3);
103         thePrs->setLineStyle(0);
104         if (isExternal()) {
105           // Set color from preferences
106           aRGB = Config_PropManager::color("Visualization", "sketch_external_color",
107                                            SKETCH_EXTERNAL_EDGE_COLOR);
108         }
109         else {
110           // Set color from preferences
111           aRGB = Config_PropManager::color("Visualization", "sketch_edge_color",
112                                            SKETCH_EDGE_COLOR);
113         }
114       }
115     }
116     else if (aShapeType == 7) { // otherwise this is a vertex
117       //  thePrs->setPointMarker(6, 2.);
118       // Set color from preferences
119       aRGB = Config_PropManager::color("Visualization", "sketch_point_color",
120                                        SKETCH_POINT_COLOR);
121     }
122
123     if (!aRGB.empty())
124       thePrs->setColor(aRGB[0], aRGB[1], aRGB[2]);
125   }
126
127   /// Returns the sketch of this feature
128   SketchPlugin_Sketch* sketch();
129 protected:
130   /// Sets the higher-level feature for the sub-feature (sketch for line)
131   void setSketch(SketchPlugin_Sketch* theSketch)
132   {
133     mySketch = theSketch;
134   }
135   /// initializes mySketch
136   SketchPlugin_Feature();
137
138   friend class SketchPlugin_Sketch;
139
140  private:
141   std::shared_ptr<GeomAPI_Shape> myPreview;  ///< the preview shape
142   SketchPlugin_Sketch* mySketch;  /// sketch that contains this feature
143 };
144
145 #endif