Salome HOME
Construction elements are auxiliary entities:
[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     bool isConstruction = data()->boolean(SketchPlugin_Feature::CONSTRUCTION_ID())->value();
92     if (aShapeType == 6) { // if this is an edge
93       if (isConstruction) {
94         thePrs->setWidth(1);
95         aRGB = Config_PropManager::color("Visualization", "sketch_construction_color",
96                                          SKETCH_CONSTRUCTION_COLOR);
97       }
98       else {
99         thePrs->setWidth(3);
100         if (isExternal()) {
101           // Set color from preferences
102           aRGB = Config_PropManager::color("Visualization", "sketch_external_color",
103                                            SKETCH_EXTERNAL_EDGE_COLOR);
104         }
105         else {
106           // Set color from preferences
107           aRGB = Config_PropManager::color("Visualization", "sketch_edge_color",
108                                            SKETCH_EDGE_COLOR);
109         }
110       }
111     }
112     else if (aShapeType == 7) { // otherwise this is a vertex
113       //  thePrs->setPointMarker(6, 2.);
114       // Set color from preferences
115       aRGB = Config_PropManager::color("Visualization", "sketch_point_color",
116                                        SKETCH_POINT_COLOR);
117     }
118
119     if (!aRGB.empty())
120       thePrs->setColor(aRGB[0], aRGB[1], aRGB[2]);
121   }
122
123   /// Returns the sketch of this feature
124   SketchPlugin_Sketch* sketch();
125 protected:
126   /// Sets the higher-level feature for the sub-feature (sketch for line)
127   void setSketch(SketchPlugin_Sketch* theSketch)
128   {
129     mySketch = theSketch;
130   }
131   /// initializes mySketch
132   SketchPlugin_Feature();
133
134   friend class SketchPlugin_Sketch;
135
136  private:
137   std::shared_ptr<GeomAPI_Shape> myPreview;  ///< the preview shape
138   SketchPlugin_Sketch* mySketch;  /// sketch that contains this feature
139 };
140
141 #endif