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