Salome HOME
Merge branch 'master' into cgt/devCEA
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_SketchEntity.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_SketchEntity.h
4 // Created:     05 Mar 2015
5 // Author:      Natalia ERMOLAEVA
6
7 #ifndef SketchPlugin_SketchEntity_H_
8 #define SketchPlugin_SketchEntity_H_
9
10 #include "SketchPlugin.h"
11 #include "SketchPlugin_Feature.h"
12
13 #include <ModelAPI_CompositeFeature.h>
14 #include <GeomAPI_Shape.h>
15 #include <GeomAPI_AISObject.h>
16 #include <ModelAPI_Document.h>
17 #include <ModelAPI_AttributeSelection.h>
18 #include <ModelAPI_AttributeBoolean.h>
19 #include <GeomAPI_ICustomPrs.h>
20
21 #include <Config_PropManager.h>
22
23 /**\class SketchPlugin_SketchEntity
24  * \ingroup Plugins
25  * \brief Sketch Entity for creation of the new feature in PartSet. 
26  * This is an abstract class to give
27  * an interface to create the entity features such as line, circle, arc and point.
28  */
29 class SketchPlugin_SketchEntity : public SketchPlugin_Feature, public GeomAPI_ICustomPrs
30 {
31  public:
32   /// Reference to the construction type of the feature
33   inline static const std::string& AUXILIARY_ID()
34   {
35     static const std::string MY_AUXILIARY_ID("Auxiliary");
36     return MY_AUXILIARY_ID;
37   }
38
39   /// Reference to the external edge or vertex as a AttributeSelection
40   inline static const std::string& EXTERNAL_ID()
41   {
42     static const std::string MY_EXTERNAL_ID("External");
43     return MY_EXTERNAL_ID;
44   }
45
46   /// Reference to the copy type of the feature
47   inline static const std::string& COPY_ID()
48   {
49     static const std::string MY_COPY_ID("Copy");
50     return MY_COPY_ID;
51   }
52
53   /// Width of the auxiliary line
54   inline static const double SKETCH_LINE_WIDTH_AUXILIARY()
55   {
56     return 2;
57   }
58
59   /// Width of the line
60   inline static const double SKETCH_LINE_WIDTH()
61   {
62     return 3;
63   }
64
65   /// Style of the auxiliary line
66   inline static const int SKETCH_LINE_STYLE_AUXILIARY()
67   {
68     return  3;
69   }
70
71   /// Style of the line
72   inline static const int SKETCH_LINE_STYLE()
73   {
74     return  0;
75   }
76
77   /// Request for initialization of data model of the feature: adding all attributes
78   virtual void initAttributes();
79
80   /// Returns true of the feature is created basing on the external shape of not-this-sketch object
81   virtual bool isExternal() const
82   {
83     AttributeSelectionPtr aAttr = data()->selection(EXTERNAL_ID());
84     if (aAttr)
85       return aAttr->context().get() != NULL && !aAttr->isInvalid();
86     return false;
87   }
88
89   /// Returns true of the feature is a copy of other feature
90   virtual bool isCopy() const
91   {
92     AttributeBooleanPtr anAttr = data()->boolean(COPY_ID());
93     if(anAttr.get()) {
94       return anAttr->value();
95     }
96     return false;
97   }
98
99   /// Customize presentation of the feature
100   virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
101                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
102   {
103     /// Store previous color values of the presentation to check it after setting specific entity
104     /// color. Default color is set into presentation to have not modified isCustomized state
105     /// after default customize prs is completed.
106     std::vector<int> aPrevColor;
107     aPrevColor.resize(3);
108     thePrs->getColor(aPrevColor[0], aPrevColor[1], aPrevColor[2]);
109     if (theResult.get()) {
110       std::string aSection, aName, aDefault;
111       theResult->colorConfigInfo(aSection, aName, aDefault);
112       std::vector<int> aColor;
113       aColor = Config_PropManager::color(aSection, aName);
114       thePrs->setColor(aColor[0], aColor[1], aColor[2]);
115     }
116
117     bool isCustomized = theDefaultPrs.get() != NULL &&
118                         theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
119     int aShapeType = thePrs->getShapeType();
120     // a compound is processed like the edge because the
121     // arc feature uses the compound for presentable AIS
122     if (aShapeType != 6/*an edge*/ && aShapeType != 7/*a vertex*/ && aShapeType != 0/*compound*/)
123       return false;
124
125     // set color from preferences
126     std::vector<int> aColor;
127     std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
128                                     data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
129     bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
130     if (isConstruction) {
131       aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
132     }
133     else if (isExternal()) {
134       aColor = Config_PropManager::color("Visualization", "sketch_external_color");
135     }
136     else {
137       aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
138     }
139     if (!aColor.empty()) {
140       thePrs->setColor(aColor[0], aColor[1], aColor[2]);
141       for (int i = 0; i < 3 && !isCustomized; i++)
142         isCustomized = aColor[i] != aPrevColor[i];
143     }
144
145     if (aShapeType == 6 || aShapeType == 0) { // if this is an edge or a compound
146       if (isConstruction) {
147         isCustomized = thePrs->setWidth(SKETCH_LINE_WIDTH_AUXILIARY()) || isCustomized;
148         isCustomized = thePrs->setLineStyle(SKETCH_LINE_STYLE_AUXILIARY()) || isCustomized;
149       }
150       else {
151         isCustomized = thePrs->setWidth(SKETCH_LINE_WIDTH()) || isCustomized;
152         isCustomized = thePrs->setLineStyle(SKETCH_LINE_STYLE()) || isCustomized;
153       }
154     }
155     else if (aShapeType == 7) { // otherwise this is a vertex
156       // The width value do not have effect on the point presentation.
157       // It is defined in order to extend selection area of the object.
158       thePrs->setWidth(17);
159     //  thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
160     }
161     if(isCopy()) {
162       double aWidth = thePrs->width();
163       isCustomized = thePrs->setWidth(aWidth / 2.5) || isCustomized;
164     }
165
166     if (!theResult.get()) {
167       double aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
168       thePrs->setDeflection(aDeflection);
169     }
170     return isCustomized;
171   }
172
173 protected:
174   /// initializes mySketch
175   SketchPlugin_SketchEntity();
176
177   /// \brief Initializes attributes of derived class.
178   virtual void initDerivedClassAttributes(){};
179
180 };
181
182 #endif