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