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