Salome HOME
COPY attribute in SketchEntity now persistent
[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
27 /**\class SketchPlugin_SketchEntity
28  * \ingroup Plugins
29  * \brief Sketch Entity for creation of the new feature in PartSet. This is an abstract class to give
30  * an interface to create the entity features such as line, circle, arc and point.
31  */
32 class SketchPlugin_SketchEntity : public SketchPlugin_Feature, public GeomAPI_ICustomPrs
33 {
34  public:
35   /// Reference to the construction type of the feature
36   inline static const std::string& AUXILIARY_ID()
37   {
38     static const std::string MY_AUXILIARY_ID("Auxiliary");
39     return MY_AUXILIARY_ID;
40   }
41
42   /// Reference to the external edge or vertex as a AttributeSelection
43   inline static const std::string& EXTERNAL_ID()
44   {
45     static const std::string MY_EXTERNAL_ID("External");
46     return MY_EXTERNAL_ID;
47   }
48
49   /// Reference to the copy type of the feature
50   inline static const std::string& COPY_ID()
51   {
52     static const std::string MY_COPY_ID("Copy");
53     return MY_COPY_ID;
54   }
55
56   /// Request for initialization of data model of the feature: adding all attributes
57   virtual void initAttributes();
58
59   /// Returns true of the feature is created basing on the external shape of not-this-sketch object
60   virtual bool isExternal() const
61   {
62     AttributeSelectionPtr aAttr = data()->selection(EXTERNAL_ID());
63     if (aAttr)
64       return aAttr->context().get() != NULL;
65     return false;
66   }
67
68   /// Returns true of the feature is a copy of other feature
69   virtual bool isCopy() const
70   {
71     AttributeBooleanPtr anAttr = data()->boolean(COPY_ID());
72     if(anAttr.get()) {
73       return anAttr->value();
74     }
75     return false;
76   }
77
78   /// Customize presentation of the feature
79   virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
80                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
81   {
82     bool isCustomized = false;
83     int aShapeType = thePrs->getShapeType();
84     // a compound is processed like the edge because the arc feature uses the compound for presentable AIS
85     if (aShapeType != 6/*an edge*/ && aShapeType != 7/*a vertex*/ && aShapeType != 0/*compound*/)
86       return false;
87
88     // set color from preferences
89     std::vector<int> aColor;
90     std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
91                                     data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
92     bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
93     if (isConstruction) {
94       aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color",
95                                          SKETCH_AUXILIARY_COLOR);
96     }
97     else if (isExternal()) {
98       aColor = Config_PropManager::color("Visualization", "sketch_external_color",
99                                         SKETCH_EXTERNAL_COLOR);
100     }
101     else {
102       aColor = Config_PropManager::color("Visualization", "sketch_entity_color",
103                                           SKETCH_ENTITY_COLOR);
104     }
105     if (!aColor.empty())
106       isCustomized = thePrs->setColor(aColor[0], aColor[1], aColor[2]) || isCustomized;
107
108     if (aShapeType == 6 || aShapeType == 0) { // if this is an edge or a compound
109       if (isConstruction) {
110         isCustomized = thePrs->setWidth(1) || isCustomized;
111         isCustomized = thePrs->setLineStyle(3) || isCustomized;
112       }
113       else {
114         isCustomized = thePrs->setWidth(3) || isCustomized;
115         isCustomized = thePrs->setLineStyle(0) || isCustomized;
116       }
117     }
118     else if (aShapeType == 7) { // otherwise this is a vertex
119       // The width value do not have effect on the point presentation.
120       // It is defined in order to extend selection area of the object.
121       thePrs->setWidth(17);
122     //  thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
123     }
124     if(isCopy()) {
125       double aWidth = thePrs->width();
126       isCustomized = thePrs->setWidth(aWidth / 2.5) || isCustomized;
127     }
128     return isCustomized;
129   }
130
131 protected:
132   /// initializes mySketch
133   SketchPlugin_SketchEntity();
134
135   /// \brief Initializes attributes of derived class.
136   virtual void initDerivedClassAttributes(){};
137
138 };
139
140 #endif