Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_SketchEntity.h
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
18 //
19
20 #ifndef SketchPlugin_SketchEntity_H_
21 #define SketchPlugin_SketchEntity_H_
22
23 #include "SketchPlugin.h"
24 #include "SketchPlugin_Feature.h"
25
26 #include <ModelAPI_CompositeFeature.h>
27 #include <GeomAPI_Shape.h>
28 #include <GeomAPI_AISObject.h>
29 #include <ModelAPI_Document.h>
30 #include <ModelAPI_AttributeSelection.h>
31 #include <ModelAPI_AttributeBoolean.h>
32 #include <GeomAPI_ICustomPrs.h>
33
34 #include <Config_PropManager.h>
35
36 /**\class SketchPlugin_SketchEntity
37  * \ingroup Plugins
38  * \brief Sketch Entity for creation of the new feature in PartSet. 
39  * This is an abstract class to give
40  * an interface to create the entity features such as line, circle, arc and point.
41  */
42 class SketchPlugin_SketchEntity : public SketchPlugin_Feature, public GeomAPI_ICustomPrs
43 {
44  public:
45   /// Reference to the construction type of the feature
46   inline static const std::string& AUXILIARY_ID()
47   {
48     static const std::string MY_AUXILIARY_ID("Auxiliary");
49     return MY_AUXILIARY_ID;
50   }
51
52   /// Reference to the external edge or vertex as a AttributeSelection
53   inline static const std::string& EXTERNAL_ID()
54   {
55     static const std::string MY_EXTERNAL_ID("External");
56     return MY_EXTERNAL_ID;
57   }
58
59   /// Reference to the copy type of the feature
60   inline static const std::string& COPY_ID()
61   {
62     static const std::string MY_COPY_ID("Copy");
63     return MY_COPY_ID;
64   }
65
66   /// Width of the auxiliary line
67   inline static const double SKETCH_LINE_WIDTH_AUXILIARY()
68   {
69     return 2;
70   }
71
72   /// Width of the line
73   inline static const double SKETCH_LINE_WIDTH()
74   {
75     return 3;
76   }
77
78   /// Style of the auxiliary line
79   inline static const int SKETCH_LINE_STYLE_AUXILIARY()
80   {
81     return  3;
82   }
83
84   /// Style of the line
85   inline static const int SKETCH_LINE_STYLE()
86   {
87     return  0;
88   }
89
90   /// Request for initialization of data model of the feature: adding all attributes
91   virtual void initAttributes();
92
93   /// Returns true of the feature is created basing on the external shape of not-this-sketch object
94   virtual bool isExternal() const
95   {
96     AttributeSelectionPtr aAttr = data()->selection(EXTERNAL_ID());
97     if (aAttr)
98       return aAttr->context().get() != NULL && !aAttr->isInvalid();
99     return false;
100   }
101
102   /// Returns true of the feature is a copy of other feature
103   virtual bool isCopy() const
104   {
105     AttributeBooleanPtr anAttr = data()->boolean(COPY_ID());
106     if(anAttr.get()) {
107       return anAttr->value();
108     }
109     return false;
110   }
111
112   /// Customize presentation of the feature
113   virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
114                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
115   {
116     /// Store previous color values of the presentation to check it after setting specific entity
117     /// color. Default color is set into presentation to have not modified isCustomized state
118     /// after default customize prs is completed.
119     std::vector<int> aPrevColor;
120     aPrevColor.resize(3);
121     thePrs->getColor(aPrevColor[0], aPrevColor[1], aPrevColor[2]);
122     if (theResult.get()) {
123       std::string aSection, aName, aDefault;
124       theResult->colorConfigInfo(aSection, aName, aDefault);
125       std::vector<int> aColor;
126       aColor = Config_PropManager::color(aSection, aName);
127       thePrs->setColor(aColor[0], aColor[1], aColor[2]);
128     }
129
130     bool isCustomized = theDefaultPrs.get() != NULL &&
131                         theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
132     int aShapeType = thePrs->getShapeType();
133     // a compound is processed like the edge because the
134     // arc feature uses the compound for presentable AIS
135     if (aShapeType != 6/*an edge*/ && aShapeType != 7/*a vertex*/ && aShapeType != 0/*compound*/)
136       return false;
137
138     // set color from preferences
139     std::vector<int> aColor;
140     std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
141                                     data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
142     bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
143     if (isConstruction) {
144       aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
145     }
146     else if (isExternal()) {
147       aColor = Config_PropManager::color("Visualization", "sketch_external_color");
148     }
149     else {
150       aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
151     }
152     if (!aColor.empty()) {
153       thePrs->setColor(aColor[0], aColor[1], aColor[2]);
154       for (int i = 0; i < 3 && !isCustomized; i++)
155         isCustomized = aColor[i] != aPrevColor[i];
156     }
157
158     if (aShapeType == 6 || aShapeType == 0) { // if this is an edge or a compound
159       if (isConstruction) {
160         isCustomized = thePrs->setWidth(SKETCH_LINE_WIDTH_AUXILIARY()) || isCustomized;
161         isCustomized = thePrs->setLineStyle(SKETCH_LINE_STYLE_AUXILIARY()) || isCustomized;
162       }
163       else {
164         isCustomized = thePrs->setWidth(SKETCH_LINE_WIDTH()) || isCustomized;
165         isCustomized = thePrs->setLineStyle(SKETCH_LINE_STYLE()) || isCustomized;
166       }
167     }
168     else if (aShapeType == 7) { // otherwise this is a vertex
169       // The width value do not have effect on the point presentation.
170       // It is defined in order to extend selection area of the object.
171       thePrs->setWidth(17);
172     //  thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
173     }
174     if(isCopy()) {
175       double aWidth = thePrs->width();
176       isCustomized = thePrs->setWidth(aWidth / 2.5) || isCustomized;
177     }
178
179     if (!theResult.get()) {
180       double aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
181       thePrs->setDeflection(aDeflection);
182     }
183     return isCustomized;
184   }
185
186 protected:
187   /// initializes mySketch
188   SketchPlugin_SketchEntity();
189
190   /// \brief Initializes attributes of derived class.
191   virtual void initDerivedClassAttributes(){};
192
193 };
194
195 #endif