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