Salome HOME
#2027 Sketcher Trim feature - deselect base feature
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintSplit.h
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintSplit.h
4 // Created: 25 Aug 2016
5 // Author:  Natalia ERMOLAEVA
6
7 #ifndef SketchPlugin_ConstraintSplit_H_
8 #define SketchPlugin_ConstraintSplit_H_
9
10 #include "SketchPlugin.h"
11 #include <SketchPlugin_Sketch.h>
12 #include "SketchPlugin_ConstraintBase.h"
13
14 class GeomDataAPI_Point2D;
15 class ModelAPI_Feature;
16 class ModelAPI_Result;
17
18 typedef std::pair<std::string, std::shared_ptr<GeomDataAPI_Point2D> > IdToPointPair;
19
20 /** \class SketchPlugin_ConstraintSplit
21  *  \ingroup Plugins
22  *  \brief Feature for creation of a new constraint splitting object. Entities for split:
23  * - Linear segment by point(s) on this line
24  * - Arc by point(s) on this arc
25  * - Circle by at least 2 split-points on this circle
26  *
27  * The following constraints will be applied after split to keep the divided segments geometry:
28  * - Coincident constraints for both parts of created segments in the point of splitting
29  * - For linear segments parallel, for circles - tangent constraint, for arc - tangent and equal
30  *   constraints. In case of three segments in result two couple of constraints are created
31  *    - parallel and equal constraints: the first is between 1st and middle entity, the second is
32  *      between 1st and 3rd.
33  *    - tangency constraints: the first between 1st and 2nd, the second between 2nd and 3rd.
34  * - Constraints assigned to the feature before split operation are assigned after using rules:
35  *    - Coincident constraints are assigned to the segment where they belong to. Segment of split
36  *      has only a coincidence to the neighbor segment. All constraints used in the splitting of
37  *      this segment are moved to point of neighbor segment. If constraint was initially built
38  *      to the point of splitting segment, it stays for this point.
39  *    - Geometrical and dimensional constraints are assigned to one of result segment, not the
40  *      selected for split. In case of 3 result segments, this will be the segment nearest to the
41  *      start point of arc/line.
42  *    - Replication constraint used split feature will be deleted in the same way as it is deleted
43  *      by any of entity delete in sketch which is used in this constraint.
44  *
45  *  This constraint has three attributes:
46  *  SketchPlugin_Constraint::VALUE() contains reference object to be splitted
47  *  SketchPlugin_Constraint::ENTITY_A() and SketchPlugin_Constraint::ENTITY_B() for the points of split;
48  *
49  */
50 class SketchPlugin_ConstraintSplit : public SketchPlugin_ConstraintBase
51 {
52  public:
53   /// Split constraint kind
54   inline static const std::string& ID()
55   {
56     static const std::string MY_CONSTRAINT_SPLIT_ID("SketchConstraintSplit");
57     return MY_CONSTRAINT_SPLIT_ID;
58   }
59   /// \brief Returns the kind of a feature
60   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
61   {
62     static std::string MY_KIND = SketchPlugin_ConstraintSplit::ID();
63     return MY_KIND;
64   }
65
66   /// \brief Creates a new part document if needed
67   SKETCHPLUGIN_EXPORT virtual void execute();
68
69   /// \brief Request for initialization of data model of the feature: adding all attributes
70   SKETCHPLUGIN_EXPORT virtual void initAttributes();
71
72   /// Reimplemented from ModelAPI_Feature::isMacro().
73   /// \returns true
74   SKETCHPLUGIN_EXPORT virtual bool isMacro() const;
75
76   /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false.
77   /// This is necessary to perform execute only by apply the feature
78   SKETCHPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; }
79
80   /// \brief Use plugin manager for features creation
81   SketchPlugin_ConstraintSplit();
82
83   /// Returns the AIS preview
84   SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious);
85
86 private:
87   /// Returns geom point attribute of the feature bounds. It processes line or arc.
88   /// For circle feature, the result attributes are null
89   /// \param theFeature a source feature
90   /// \param theStartPointAttr an out attribute to start point
91   /// \param theStartPointAttr an out attribute to end point
92   void getFeaturePoints(const FeaturePtr& theFeature,
93                         std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
94                         std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
95
96   /// Returns cast of attribute to geometrical point if the attribute is a ref attr attribute
97   /// \param theAttribute an attribute
98   /// \param geom point 2D or NULL
99   std::shared_ptr<GeomDataAPI_Point2D> getPointOfRefAttr(const AttributePtr& theAttribute);
100
101   /// Obtains those constraints of the feature that should be modified. output maps contain
102   /// point of coincidence and attribute id to be modified after split
103   /// \param theFeaturesToDelete [out] constrains that will be deleted after split
104   /// \param theFeaturesToUpdate [out] constrains that will be updated after split
105   /// \param theTangentFeatures  [out] tangent feature to be connected to new feature
106   /// \param theCoincidenceToFeature [out] coincidence to feature to be connected to new feature
107   /// \param theCoincidenceToPoint [out] coincidence to point be connected to new feature
108   void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
109               std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToUpdate,
110               std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature);
111
112   /// Obtains references to feature point attributes and to feature,
113   /// e.g. for feature line: 1st container is
114   ///             <1st line point, list<entity_a in distance, entity_b in parallel> >
115   ///             <2nd line point, list<> >
116   ///      for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
117   /// \param theFeature an investigated feature
118   /// \param theRefs a container of list of referenced attributes
119   void getRefAttributes(const FeaturePtr& theFeature,
120                         std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
121                         std::list<AttributePtr>& theRefsToFeature);
122
123   /// Move coincidence constraint from feature to point if it is found
124   /// \param theCoincidenceToFeature coincidence to feature to be connected to new feature
125   /// \param theFurtherCoincidences a list of points where coincidences will be build
126   /// \param theFeatureResults created results after split where constaint might be connected
127   /// \param theSplitFeature feature created by split, new coincidences to points should be created
128   /// if theCoincidenceToFeature contains equal points
129   void updateCoincidenceConstraintsToFeature(
130       const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
131       const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
132       const std::set<ResultPtr>& theFeatureResults,
133       const FeaturePtr& theSplitFeature);
134
135   /// Move constraints from base feature to given feature
136   /// \param theFeature a base feature
137   /// \param theRefsToFeature list of attributes referenced to base feature
138   void updateRefFeatureConstraints(const std::shared_ptr<ModelAPI_Result>& theFeatureBaseResult,
139                                    const std::list<AttributePtr>& theRefsToFeature);
140
141   /// Move constraints from attribute of base feature to attribute after modification
142   /// \param theBaseRefAttributes container of references to the attributes of base feature
143   /// \param theModifiedAttributes container of attributes placed instead of base attributes
144   /// at the same place
145   void updateRefAttConstraints(
146                const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
147                const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes);
148
149   /// Make the base object is splitted by the point attributes
150   /// \param theSplitFeature a result split feature
151   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
152   /// \param theAfterFeature a feature between last point of split feature and the end point
153   /// \param thePoints a list of points where coincidences will be build
154   /// \param theCreatedFeatures a container of created features
155   /// \param theModifiedAttributes a container of attribute on base
156   /// feature to attribute on new feature
157   void splitLine(std::shared_ptr<ModelAPI_Feature>& theSplitFeature,
158                  std::shared_ptr<ModelAPI_Feature>& theBeforeFeature,
159                  std::shared_ptr<ModelAPI_Feature>& theAfterFeature,
160                  std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
161                  std::set<std::shared_ptr<ModelAPI_Feature>>& theCreatedFeatures,
162                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
163
164   /// Make the base object is splitted by the point attributes
165   /// \param theSplitFeature a result split feature
166   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
167   /// \param theAfterFeature a feature between last point of split feature and the end point
168   /// \param thePoints a list of points where coincidences will be build
169   /// \param theCreatedFeatures a container of created features
170   void splitArc(std::shared_ptr<ModelAPI_Feature>& theSplitFeature,
171                 std::shared_ptr<ModelAPI_Feature>& theBeforeFeature,
172                 std::shared_ptr<ModelAPI_Feature>& theAfterFeature,
173                 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
174                 std::set<std::shared_ptr<ModelAPI_Feature>>& theCreatedFeatures,
175                 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
176
177   /// Make the base object is splitted by the point attributes
178   /// \param theSplitFeature a result split feature
179   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
180   /// \param theAfterFeature a feature between last point of split feature and the end point
181   /// \param thePoints a list of points where coincidences will be build
182   /// \param theCreatedFeatures a container of created features
183   void splitCircle(std::shared_ptr<ModelAPI_Feature>& theSplitFeature,
184                    std::shared_ptr<ModelAPI_Feature>& theBeforeFeature,
185                    std::shared_ptr<ModelAPI_Feature>& theAfterFeature,
186                    std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
187                    std::set<std::shared_ptr<ModelAPI_Feature>>& theCreatedFeatures,
188                    std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
189
190   /// Correct the first and the second point to provide condition that the first is closer to
191   /// the start point and the second point - to the last end of current segment. To rearrange
192   /// them if this condition is not satisfied.
193   /// \param theStartPointAttr a start point of a segment
194   /// \param theEndPointAttr an end point of a segment
195   /// \param theFirstPointAttr a start point of a segment
196   /// \param theSecondPointAttr an end point of a segment
197   void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
198                            const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
199                            std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
200                            std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const;
201
202   /// Correct the first and the second point to provide condition that the first is closer to
203   /// the start point and the second point - to the last end of current segment. To rearrange
204   /// them if this condition is not satisfied.
205   /// \param theArc an arc to be split
206   /// \param theStartPointAttr a start point of a segment
207   /// \param theEndPointAttr an end point of a segment
208   /// \param theFirstPointAttr a start point of a segment
209   /// \param theSecondPointAttr an end point of a segment
210   void arrangePointsOnArc(const FeaturePtr& theArc,
211                           const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
212                           const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
213                           std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
214                           std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const;
215
216   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
217   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
218   /// \param theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
219   void fillAttribute(const AttributePtr& theModifiedAttribute,
220                      const AttributePtr& theSourceAttribute);
221
222   /// Creates a line feature filled by center of base feature and given points
223   /// \param theBaseFeature another arc feature
224   /// \param theFirstAttribute an attribute with coordinates for the start point
225   /// \param theSecondAttribute an attribute with coordinates for the end point
226   FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
227                                const AttributePtr& theFirstPointAttr,
228                                const AttributePtr& theSecondPointAttr);
229
230   /// Creates an arc feature filled by center of base feature and given points
231   /// \param theBaseFeature another arc feature
232   /// \param theFirstAttribute an attribute with coordinates for the start point
233   /// \param theSecondAttribute an attribute with coordinates for the end point
234   FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
235                               const AttributePtr& theFirstPointAttr,
236                               const AttributePtr& theSecondPointAttr);
237
238   /// Add feature coincidence constraint between given attributes
239   /// \param theConstraintId a constraint index
240   /// \param theFirstAttribute an attribute of further coincidence
241   /// \param theSecondAttribute an attribute of further coincidence
242   std::shared_ptr<ModelAPI_Feature> createConstraint(const std::string& theConstraintId,
243                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
244                         const std::shared_ptr<ModelAPI_Attribute>& theSecondAttribute);
245
246   /// Add feature coincidence constraint between given attributes
247   /// \param theConstraintId a constraint index
248   /// \param theFirstAttribute an attribute of further coincidence
249   /// \param theFirstAttribute an attribute of further coincidence
250   std::shared_ptr<ModelAPI_Feature> createConstraintForObjects(const std::string& theConstraintId,
251                         const std::shared_ptr<ModelAPI_Object>& theFirstObject,
252                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
253
254   /// Add feature coincidence constraint between given attributes
255   /// \param theFeaturesToUpdate a constraint index
256   void updateFeaturesAfterSplit(const std::set<FeaturePtr>& theFeaturesToUpdate);
257
258   /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
259   /// \param theFeature a feature
260   /// \return result object
261   std::shared_ptr<ModelAPI_Result> getFeatureResult(
262                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
263
264   /// Returns attributes of the feature, used in edge build, for arc it is end and start points
265   /// \param theFeature a feature
266   /// \return container of attributes
267   std::set<std::shared_ptr<ModelAPI_Attribute> > getEdgeAttributes(
268                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
269
270 #ifdef _DEBUG
271   /// Return feature name, kind, point attribute values united in a string
272   /// \param theFeature an investigated feature
273   /// \return string value
274   std::string getFeatureInfo(const std::shared_ptr<ModelAPI_Feature>& theFeature,
275                              const bool isUseAttributesInfo = true);
276 #endif
277 };
278
279 #endif