Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[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   /// Width of the auxiliary line
69   inline static const double SKETCH_LINE_WIDTH_AUXILIARY()
70   {
71     return 2;
72   }
73
74   /// Width of the line
75   inline static const double SKETCH_LINE_WIDTH()
76   {
77     return 3;
78   }
79
80   /// Style of the auxiliary line
81   inline static const int SKETCH_LINE_STYLE_AUXILIARY()
82   {
83     return  3;
84   }
85
86   /// Style of the line
87   inline static const int SKETCH_LINE_STYLE()
88   {
89     return  0;
90   }
91
92   /// Request for initialization of data model of the feature: adding all attributes
93   virtual void initAttributes();
94
95   /// Returns true of the feature is created basing on the external shape of not-this-sketch object
96   virtual bool isExternal() const
97   {
98     AttributeSelectionPtr aAttr = data()->selection(EXTERNAL_ID());
99     if (aAttr)
100       return aAttr->context().get() != NULL && !aAttr->isInvalid();
101     return false;
102   }
103
104   /// Returns true of the feature is a copy of other feature
105   virtual bool isCopy() const
106   {
107     AttributeBooleanPtr anAttr = data()->boolean(COPY_ID());
108     if(anAttr.get()) {
109       return anAttr->value();
110     }
111     return false;
112   }
113
114 // LCOV_EXCL_START
115   /// Returns \c true if the result is marked as auxiliary
116   virtual bool isAuxiliary(ResultPtr theResult)
117   { return false; }
118
119   /// Customize presentation of the feature
120   virtual bool customisePresentation(ResultPtr theResult, AISObjectPtr thePrs,
121                                      std::shared_ptr<GeomAPI_ICustomPrs> theDefaultPrs)
122   {
123     /// Store previous color values of the presentation to check it after setting specific entity
124     /// color. Default color is set into presentation to have not modified isCustomized state
125     /// after default customize prs is completed.
126     std::vector<int> aPrevColor;
127     aPrevColor.resize(3);
128     thePrs->getColor(aPrevColor[0], aPrevColor[1], aPrevColor[2]);
129     if (theResult.get()) {
130       std::string aSection, aName, aDefault;
131       theResult->colorConfigInfo(aSection, aName, aDefault);
132       std::vector<int> aColor;
133       aColor = Config_PropManager::color(aSection, aName);
134       thePrs->setColor(aColor[0], aColor[1], aColor[2]);
135     }
136
137     bool isCustomized = theDefaultPrs.get() != NULL &&
138                         theDefaultPrs->customisePresentation(theResult, thePrs, theDefaultPrs);
139     int aShapeType = thePrs->getShapeType();
140     // a compound is processed like the edge because the
141     // arc feature uses the compound for presentable AIS
142     if (aShapeType != 6/*an edge*/ && aShapeType != 7/*a vertex*/ && aShapeType != 0/*compound*/)
143       return false;
144
145     // set color from preferences
146     std::vector<int> aColor;
147     std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
148                                     data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
149     bool isConstruction = (anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value())
150                        || isAuxiliary(theResult);
151     if (isConstruction) {
152       aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
153     }
154     else if (isExternal()) {
155       aColor = Config_PropManager::color("Visualization", "sketch_external_color");
156     }
157     else {
158       aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
159     }
160     if (!aColor.empty()) {
161       if (theResult.get() && ModelAPI_Session::get()->isOperation()) {
162         AttributeIntArrayPtr aColorAttr = theResult->data()->intArray(ModelAPI_Result::COLOR_ID());
163         aColorAttr->setSize(3);
164         // Set the color attribute in order do not use default colors in the perasentation object
165         for (int i = 0; i < 3; i++)
166           aColorAttr->setValue(i, aColor[i]);
167       }
168       thePrs->setColor(aColor[0], aColor[1], aColor[2]);
169       for (int i = 0; i < 3 && !isCustomized; i++)
170         isCustomized = aColor[i] != aPrevColor[i];
171     }
172
173     if (aShapeType == 6 || aShapeType == 0) { // if this is an edge or a compound
174       if (isConstruction) {
175         isCustomized = thePrs->setWidth(SKETCH_LINE_WIDTH_AUXILIARY()) || isCustomized;
176         isCustomized = thePrs->setLineStyle(SKETCH_LINE_STYLE_AUXILIARY()) || isCustomized;
177       }
178       else {
179         isCustomized = thePrs->setWidth(SKETCH_LINE_WIDTH()) || isCustomized;
180         isCustomized = thePrs->setLineStyle(SKETCH_LINE_STYLE()) || isCustomized;
181       }
182     }
183     else if (aShapeType == 7) { // otherwise this is a vertex
184       // The width value do not have effect on the point presentation.
185       // It is defined in order to extend selection area of the object.
186       thePrs->setWidth(17);
187     //  thePrs->setPointMarker(1, 1.); // Set point as a '+' symbol
188     }
189     if(isCopy()) {
190       double aWidth = thePrs->width();
191       isCustomized = thePrs->setWidth(aWidth / 2.5) || isCustomized;
192     }
193
194     if (!theResult.get()) {
195       double aDeflection = Config_PropManager::real("Visualization", "construction_deflection");
196       thePrs->setDeflection(aDeflection);
197     }
198     return isCustomized;
199   }
200 // LCOV_EXCL_STOP
201
202 protected:
203   /// initializes mySketch
204   SketchPlugin_SketchEntity();
205
206   /// \brief Initializes attributes of derived class.
207   virtual void initDerivedClassAttributes(){};
208
209 };
210
211 #endif