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