Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Split.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_Split_H_
21 #define SketchPlugin_Split_H_
22
23 #include "SketchPlugin.h"
24
25 #include "GeomAPI_IPresentable.h"
26 #include <ModelAPI_IReentrant.h>
27
28 #include <SketchPlugin_Sketch.h>
29
30 class GeomDataAPI_Point2D;
31 class ModelAPI_Feature;
32 class ModelAPI_Result;
33
34 typedef std::pair<std::string, std::shared_ptr<GeomDataAPI_Point2D> > IdToPointPair;
35
36 /** \class SketchPlugin_Split
37  *  \ingroup Plugins
38  *  \brief Feature for creation of a new constraint splitting object. Entities for split:
39  * - Linear segment by point(s) on this line
40  * - Arc by point(s) on this arc
41  * - Circle by at least 2 split-points on this circle
42  *
43  * The following constraints will be applied after split to keep the divided segments geometry:
44  * - Coincident constraints for both parts of created segments in the point of splitting
45  * - For linear segments parallel, for circles - tangent constraint, for arc - tangent and equal
46  *   constraints. In case of three segments in result two couple of constraints are created
47  *    - parallel and equal constraints: the first is between 1st and middle entity, the second is
48  *      between 1st and 3rd.
49  *    - tangency constraints: the first between 1st and 2nd, the second between 2nd and 3rd.
50  * - Constraints assigned to the feature before split operation are assigned after using rules:
51  *    - Coincident constraints are assigned to the segment where they belong to. Segment of split
52  *      has only a coincidence to the neighbor segment. All constraints used in the splitting of
53  *      this segment are moved to point of neighbor segment. If constraint was initially built
54  *      to the point of splitting segment, it stays for this point.
55  *    - Geometrical and dimensional constraints are assigned to one of result segment, not the
56  *      selected for split. In case of 3 result segments, this will be the segment nearest to the
57  *      start point of arc/line.
58  *    - Replication constraint used split feature will be deleted in the same way as it is deleted
59  *      by any of entity delete in sketch which is used in this constraint.
60  *
61  *  This constraint has three attributes:
62  *  SketchPlugin_Constraint::VALUE() contains reference object to be splitted
63  *  SketchPlugin_Constraint::ENTITY_A() and SketchPlugin_Constraint::ENTITY_B() for the points of split;
64  *
65  */
66 class SketchPlugin_Split : public SketchPlugin_Feature, public GeomAPI_IPresentable,
67                            public ModelAPI_IReentrant
68 {
69  public:
70   /// Split constraint kind
71   inline static const std::string& ID()
72   {
73     static const std::string MY_SPLIT_ID("SketchSplit");
74     return MY_SPLIT_ID;
75   }
76   /// \brief Returns the kind of a feature
77   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
78   {
79     static std::string MY_KIND = SketchPlugin_Split::ID();
80     return MY_KIND;
81   }
82   /// The value parameter for the constraint
83   inline static const std::string& SELECTED_OBJECT()
84   {
85     static const std::string MY_SELECTED_OBJECT("SelectedObject");
86     return MY_SELECTED_OBJECT;
87   }
88
89   /// Start 2D point of the split segment
90   inline static const std::string& SELECTED_POINT()
91   {
92     static const std::string MY_SELECTED_POINT("SelectedPoint");
93     return MY_SELECTED_POINT;
94   }
95
96   /// The value parameter for the preview object
97   inline static const std::string& PREVIEW_OBJECT()
98   {
99     static const std::string MY_PREVIEW_OBJECT("PreviewObject");
100     return MY_PREVIEW_OBJECT;
101   }
102
103   /// Start 2D point of the split segment
104   inline static const std::string& PREVIEW_POINT()
105   {
106     static const std::string MY_PREVIEW_POINT("PreviewPoint");
107     return MY_PREVIEW_POINT;
108   }
109
110   /// \brief Creates a new part document if needed
111   SKETCHPLUGIN_EXPORT virtual void execute();
112
113   /// \brief Request for initialization of data model of the feature: adding all attributes
114   SKETCHPLUGIN_EXPORT virtual void initAttributes();
115
116   /// Reimplemented from ModelAPI_Feature::isMacro().
117   /// \returns true
118   SKETCHPLUGIN_EXPORT virtual bool isMacro() const { return true; }
119
120   /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false.
121   /// This is necessary to perform execute only by apply the feature
122   SKETCHPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; }
123
124   /// \brief Use plugin manager for features creation
125   SketchPlugin_Split();
126
127   /// Returns the AIS preview
128   SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious);
129
130   /// Apply information of the message to current object. It fills selected point and object
131   virtual std::string processEvent(const std::shared_ptr<Events_Message>& theMessage);
132
133 private:
134   /// Fulfill an internal container by shapes obtained from the parameter object
135   /// Shapes are result of split operation by points coincident to shape of the object
136   /// \param theObject a source object (will be splitted)
137   /// \param theSketch a sketch object
138   void fillObjectShapes(const ObjectPtr& theObject, const ObjectPtr& theSketch);
139
140   GeomShapePtr getSubShape(const std::string& theObjectAttributeId,
141                            const std::string& thePointAttributeId);
142   /// Returns geom point attribute of the feature bounds. It processes line or arc.
143   /// For circle feature, the result attributes are null
144   /// \param theFeature a source feature
145   /// \param theStartPointAttr an out attribute to start point
146   /// \param theStartPointAttr an out attribute to end point
147   void getFeaturePoints(const FeaturePtr& theFeature,
148                         std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
149                         std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
150
151   /// Obtains those constraints of the feature that should be modified. output maps contain
152   /// point of coincidence and attribute id to be modified after split
153   /// \param theFeaturesToDelete [out] constrains that will be deleted after split
154   /// \param theFeaturesToUpdate [out] constrains that will be updated after split
155   /// \param theTangentFeatures  [out] tangent feature to be connected to new feature
156   /// \param theCoincidenceToFeature [out] coincidence to feature to be connected to new feature
157   /// \param theCoincidenceToPoint [out] coincidence to point be connected to new feature
158   void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
159               std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToUpdate,
160               std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature);
161
162   /// Obtains references to feature point attributes and to feature,
163   /// e.g. for feature line: 1st container is
164   ///             <1st line point, list<entity_a in distance, entity_b in parallel> >
165   ///             <2nd line point, list<> >
166   ///      for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
167   /// \param theFeature an investigated feature
168   /// \param theRefs a container of list of referenced attributes
169   void getRefAttributes(const FeaturePtr& theFeature,
170                         std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
171                         std::list<AttributePtr>& theRefsToFeature);
172
173   /// Move coincidence constraint from feature to point if it is found
174   /// \param theCoincidenceToFeature coincidence to feature to be connected to new feature
175   /// \param theFurtherCoincidences a list of points where coincidences will be build
176   /// \param theFeatureResults created results after split where constaint might be connected
177   /// \param theSplitFeature feature created by split, new coincidences to points should be created
178   /// \param theFeaturesToDelete the list of removed features (will be updated here by
179   ///                            the coincidences to be removed)
180   /// if theCoincidenceToFeature contains equal points
181   void updateCoincidenceConstraintsToFeature(
182       const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
183       const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
184       const std::set<ResultPtr>& theFeatureResults,
185       const FeaturePtr& theSplitFeature,
186       std::set<FeaturePtr>& theFeaturesToDelete);
187
188   /// Move constraints from base feature to given feature
189   /// \param theFeature a base feature
190   /// \param theRefsToFeature list of attributes referenced to base feature
191   void updateRefFeatureConstraints(const std::shared_ptr<ModelAPI_Result>& theFeatureBaseResult,
192                                    const std::list<AttributePtr>& theRefsToFeature);
193
194   /// Move constraints from attribute of base feature to attribute after modification
195   /// \param theBaseRefAttributes container of references to the attributes of base feature
196   /// \param theModifiedAttributes container of attributes placed instead of base attributes
197   /// at the same place
198   void updateRefAttConstraints(
199                const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
200                const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes);
201
202   /// Make the base object is splitted by the point attributes
203   /// \param theSplitFeature a result split feature
204   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
205   /// \param theAfterFeature a feature between last point of split feature and the end point
206   /// \param thePoints a list of points where coincidences will be build
207   /// \param theCreatedFeatures a container of created features
208   /// \param theModifiedAttributes a container of attribute on base
209   /// feature to attribute on new feature
210   /// \return new line if it was created
211   FeaturePtr splitLine(std::shared_ptr<ModelAPI_Feature>& theSplitFeature,
212                  std::shared_ptr<ModelAPI_Feature>& theBeforeFeature,
213                  std::shared_ptr<ModelAPI_Feature>& theAfterFeature,
214                  std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
215                  std::set<std::shared_ptr<ModelAPI_Feature>>& theCreatedFeatures,
216                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
217
218   /// Make the base object is splitted by the point attributes
219   /// \param theSplitFeature a result split feature
220   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
221   /// \param theAfterFeature a feature between last point of split feature and the end point
222   /// \param thePoints a list of points where coincidences will be build
223   /// \param theCreatedFeatures a container of created features
224   /// \return new arc if it was created
225   FeaturePtr splitArc(std::shared_ptr<ModelAPI_Feature>& theSplitFeature,
226                 std::shared_ptr<ModelAPI_Feature>& theBeforeFeature,
227                 std::shared_ptr<ModelAPI_Feature>& theAfterFeature,
228                 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
229                 std::set<std::shared_ptr<ModelAPI_Feature>>& theCreatedFeatures,
230                 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
231
232   /// Make the base object is splitted by the point attributes
233   /// \param theSplitFeature a result split feature
234   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
235   /// \param theAfterFeature a feature between last point of split feature and the end point
236   /// \param thePoints a list of points where coincidences will be build
237   /// \param theCreatedFeatures a container of created features
238   /// \return new arc if it was created
239   FeaturePtr splitCircle(std::shared_ptr<ModelAPI_Feature>& theSplitFeature,
240                    std::shared_ptr<ModelAPI_Feature>& theBeforeFeature,
241                    std::shared_ptr<ModelAPI_Feature>& theAfterFeature,
242                    std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
243                    std::set<std::shared_ptr<ModelAPI_Feature>>& theCreatedFeatures,
244                    std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
245
246   /// Correct the first and the second point to provide condition that the first is closer to
247   /// the start point and the second point - to the last end of current segment. To rearrange
248   /// them if this condition is not satisfied.
249   /// \param theStartPointAttr a start point of a segment
250   /// \param theEndPointAttr an end point of a segment
251   /// \param theFirstPointAttr a start point of a segment
252   /// \param theSecondPointAttr an end point of a segment
253   void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
254                            const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
255                            std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
256                            std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const;
257
258   /// Correct the first and the second point to provide condition that the first is closer to
259   /// the start point and the second point - to the last end of current segment. To rearrange
260   /// them if this condition is not satisfied.
261   /// \param theArc an arc to be split
262   /// \param theStartPointAttr a start point of a segment
263   /// \param theEndPointAttr an end point of a segment
264   /// \param theFirstPointAttr a start point of a segment
265   /// \param theSecondPointAttr an end point of a segment
266   void arrangePointsOnArc(const FeaturePtr& theArc,
267                           const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
268                           const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
269                           std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
270                           std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const;
271
272   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
273   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
274   /// \param theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
275   void fillAttribute(const AttributePtr& theModifiedAttribute,
276                      const AttributePtr& theSourceAttribute);
277
278   /// Creates a line feature filled by center of base feature and given points
279   /// \param theBaseFeature another arc feature
280   /// \param theFirstAttribute an attribute with coordinates for the start point
281   /// \param theSecondAttribute an attribute with coordinates for the end point
282   FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
283                                const AttributePtr& theFirstPointAttr,
284                                const AttributePtr& theSecondPointAttr);
285
286   /// Creates an arc feature filled by center of base feature and given points
287   /// \param theBaseFeature another arc feature
288   /// \param theFirstAttribute an attribute with coordinates for the start point
289   /// \param theSecondAttribute an attribute with coordinates for the end point
290   FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
291                               const AttributePtr& theFirstPointAttr,
292                               const AttributePtr& theSecondPointAttr);
293
294   /// Add feature coincidence constraint between given attributes
295   /// \param theFeaturesToUpdate a constraint index
296   void updateFeaturesAfterSplit(const std::set<FeaturePtr>& theFeaturesToUpdate);
297
298   /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
299   /// \param theFeature a feature
300   /// \return result object
301   std::shared_ptr<ModelAPI_Result> getFeatureResult(
302                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
303
304   /// Returns attributes of the feature, used in edge build, for arc it is end and start points
305   /// \param theFeature a feature
306   /// \return container of attributes
307   std::set<std::shared_ptr<ModelAPI_Attribute> > getEdgeAttributes(
308                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
309
310   /// Returns first attribute coincident to the first/second point of selected shape
311   /// \param isFirstAttribute true for getting the first atribute, false otherwise
312   /// \return an attribute or NULL
313   std::shared_ptr<GeomDataAPI_Point2D> getPointAttribute(const bool isFirstAttribute);
314
315 #ifdef _DEBUG
316   /// Return feature name, kind, point attribute values united in a string
317   /// \param theFeature an investigated feature
318   /// \return string value
319   std::string getFeatureInfo(const std::shared_ptr<ModelAPI_Feature>& theFeature,
320                              const bool isUseAttributesInfo = true);
321 #endif
322 private:
323   typedef std::map<std::shared_ptr<GeomDataAPI_Point2D>,
324                    std::shared_ptr<GeomAPI_Pnt> > PntToAttributesMap;
325
326   std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
327   std::map<std::shared_ptr<ModelAPI_Object>, PntToAttributesMap> myCashedReferences;
328 };
329
330 #endif