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