Salome HOME
1c9ae40e53441b66a14737ec0cdd42675579968b
[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   /// Apply information of the message to current object. It fills selected point and object
132   virtual std::string processEvent(const std::shared_ptr<Events_Message>& theMessage);
133
134 private:
135   /// Fulfill an internal container by shapes obtained from the parameter object
136   /// Shapes are result of split operation by points coincident to shape of the object
137   /// \param theObject a source object (will be splitted)
138   /// \param theSketch a sketch object
139   void fillObjectShapes(const ObjectPtr& theObject, const ObjectPtr& theSketch);
140
141   GeomShapePtr getSubShape(const std::string& theObjectAttributeId,
142                            const std::string& thePointAttributeId);
143   /// Returns geom point attribute of the feature bounds. It processes line or arc.
144   /// For circle feature, the result attributes are null
145   /// \param theFeature a source feature
146   /// \param theStartPointAttr an out attribute to start point
147   /// \param theStartPointAttr an out attribute to end point
148   void getFeaturePoints(const FeaturePtr& theFeature,
149                         std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
150                         std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
151
152   /// Returns cast of attribute to geometrical point if the attribute is a ref attr attribute
153   /// \param theAttribute an attribute
154   /// \param geom point 2D or NULL
155   std::shared_ptr<GeomDataAPI_Point2D> getPointOfRefAttr(const AttributePtr& theAttribute);
156
157   /// Obtains those constraints of the feature that should be modified. output maps contain
158   /// point of coincidence and attribute id to be modified after split
159   /// \param theFeaturesToDelete [out] constrains that will be deleted after split
160   /// \param theFeaturesToUpdate [out] constrains that will be updated after split
161   /// \param theTangentFeatures  [out] tangent feature to be connected to new feature
162   /// \param theCoincidenceToFeature [out] coincidence to feature to be connected to new feature
163   /// \param theCoincidenceToPoint [out] coincidence to point be connected to new feature
164   void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
165               std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToUpdate,
166               std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature);
167
168   /// Obtains references to feature point attributes and to feature,
169   /// e.g. for feature line: 1st container is
170   ///             <1st line point, list<entity_a in distance, entity_b in parallel> >
171   ///             <2nd line point, list<> >
172   ///      for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
173   /// \param theFeature an investigated feature
174   /// \param theRefs a container of list of referenced attributes
175   void getRefAttributes(const FeaturePtr& theFeature,
176                         std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
177                         std::list<AttributePtr>& theRefsToFeature);
178
179   /// Move coincidence constraint from feature to point if it is found
180   /// \param theCoincidenceToFeature coincidence to feature to be connected to new feature
181   /// \param theFurtherCoincidences a list of points where coincidences will be build
182   /// \param theFeatureResults created results after split where constaint might be connected
183   /// \param theSplitFeature feature created by split, new coincidences to points should be created
184   /// if theCoincidenceToFeature contains equal points
185   void updateCoincidenceConstraintsToFeature(
186       const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
187       const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
188       const std::set<ResultPtr>& theFeatureResults,
189       const FeaturePtr& theSplitFeature);
190
191   /// Move constraints from base feature to given feature
192   /// \param theFeature a base feature
193   /// \param theRefsToFeature list of attributes referenced to base feature
194   void updateRefFeatureConstraints(const std::shared_ptr<ModelAPI_Result>& theFeatureBaseResult,
195                                    const std::list<AttributePtr>& theRefsToFeature);
196
197   /// Move constraints from attribute of base feature to attribute after modification
198   /// \param theBaseRefAttributes container of references to the attributes of base feature
199   /// \param theModifiedAttributes container of attributes placed instead of base attributes
200   /// at the same place
201   void updateRefAttConstraints(
202                const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
203                const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes);
204
205   /// Make the base object is splitted by the point attributes
206   /// \param theSplitFeature a result split feature
207   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
208   /// \param theAfterFeature a feature between last point of split feature and the end point
209   /// \param thePoints a list of points where coincidences will be build
210   /// \param theCreatedFeatures a container of created features
211   /// \param theModifiedAttributes a container of attribute on base
212   /// feature to attribute on new feature
213   /// \return new line if it was created
214   FeaturePtr splitLine(std::shared_ptr<ModelAPI_Feature>& theSplitFeature,
215                  std::shared_ptr<ModelAPI_Feature>& theBeforeFeature,
216                  std::shared_ptr<ModelAPI_Feature>& theAfterFeature,
217                  std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
218                  std::set<std::shared_ptr<ModelAPI_Feature>>& theCreatedFeatures,
219                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
220
221   /// Make the base object is splitted by the point attributes
222   /// \param theSplitFeature a result split feature
223   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
224   /// \param theAfterFeature a feature between last point of split feature and the end point
225   /// \param thePoints a list of points where coincidences will be build
226   /// \param theCreatedFeatures a container of created features
227   /// \return new arc if it was created
228   FeaturePtr splitArc(std::shared_ptr<ModelAPI_Feature>& theSplitFeature,
229                 std::shared_ptr<ModelAPI_Feature>& theBeforeFeature,
230                 std::shared_ptr<ModelAPI_Feature>& theAfterFeature,
231                 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
232                 std::set<std::shared_ptr<ModelAPI_Feature>>& theCreatedFeatures,
233                 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
234
235   /// Make the base object is splitted by the point attributes
236   /// \param theSplitFeature a result split feature
237   /// \param theBeforeFeature a feature between start point and the 1st point of split feature
238   /// \param theAfterFeature a feature between last point of split feature and the end point
239   /// \param thePoints a list of points where coincidences will be build
240   /// \param theCreatedFeatures a container of created features
241   /// \return new arc if it was created
242   FeaturePtr splitCircle(std::shared_ptr<ModelAPI_Feature>& theSplitFeature,
243                    std::shared_ptr<ModelAPI_Feature>& theBeforeFeature,
244                    std::shared_ptr<ModelAPI_Feature>& theAfterFeature,
245                    std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
246                    std::set<std::shared_ptr<ModelAPI_Feature>>& theCreatedFeatures,
247                    std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
248
249   /// Correct the first and the second point to provide condition that the first is closer to
250   /// the start point and the second point - to the last end of current segment. To rearrange
251   /// them if this condition is not satisfied.
252   /// \param theStartPointAttr a start point of a segment
253   /// \param theEndPointAttr an end point of a segment
254   /// \param theFirstPointAttr a start point of a segment
255   /// \param theSecondPointAttr an end point of a segment
256   void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
257                            const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
258                            std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
259                            std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const;
260
261   /// Correct the first and the second point to provide condition that the first is closer to
262   /// the start point and the second point - to the last end of current segment. To rearrange
263   /// them if this condition is not satisfied.
264   /// \param theArc an arc to be split
265   /// \param theStartPointAttr a start point of a segment
266   /// \param theEndPointAttr an end point of a segment
267   /// \param theFirstPointAttr a start point of a segment
268   /// \param theSecondPointAttr an end point of a segment
269   void arrangePointsOnArc(const FeaturePtr& theArc,
270                           const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
271                           const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
272                           std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
273                           std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const;
274
275   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
276   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
277   /// \param theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
278   void fillAttribute(const AttributePtr& theModifiedAttribute,
279                      const AttributePtr& theSourceAttribute);
280
281   /// Creates a line feature filled by center of base feature and given points
282   /// \param theBaseFeature another arc feature
283   /// \param theFirstAttribute an attribute with coordinates for the start point
284   /// \param theSecondAttribute an attribute with coordinates for the end point
285   FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
286                                const AttributePtr& theFirstPointAttr,
287                                const AttributePtr& theSecondPointAttr);
288
289   /// Creates an arc feature filled by center of base feature and given points
290   /// \param theBaseFeature another arc feature
291   /// \param theFirstAttribute an attribute with coordinates for the start point
292   /// \param theSecondAttribute an attribute with coordinates for the end point
293   FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
294                               const AttributePtr& theFirstPointAttr,
295                               const AttributePtr& theSecondPointAttr);
296
297   /// Add feature coincidence constraint between given attributes
298   /// \param theConstraintId a constraint index
299   /// \param theFirstAttribute an attribute of further coincidence
300   /// \param theSecondAttribute an attribute of further coincidence
301   std::shared_ptr<ModelAPI_Feature> createConstraint(const std::string& theConstraintId,
302                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
303                         const std::shared_ptr<ModelAPI_Attribute>& theSecondAttribute);
304
305   /// Add feature coincidence constraint between given attributes
306   /// \param theConstraintId a constraint index
307   /// \param theFirstAttribute an attribute of further coincidence
308   /// \param theFirstAttribute an attribute of further coincidence
309   std::shared_ptr<ModelAPI_Feature> createConstraintForObjects(const std::string& theConstraintId,
310                         const std::shared_ptr<ModelAPI_Object>& theFirstObject,
311                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
312
313   /// Add feature coincidence constraint between given attributes
314   /// \param theFeaturesToUpdate a constraint index
315   void updateFeaturesAfterSplit(const std::set<FeaturePtr>& theFeaturesToUpdate);
316
317   /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
318   /// \param theFeature a feature
319   /// \return result object
320   std::shared_ptr<ModelAPI_Result> getFeatureResult(
321                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
322
323   /// Returns attributes of the feature, used in edge build, for arc it is end and start points
324   /// \param theFeature a feature
325   /// \return container of attributes
326   std::set<std::shared_ptr<ModelAPI_Attribute> > getEdgeAttributes(
327                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
328
329   /// Returns first attribute coincident to the first/second point of selected shape
330   /// \param isFirstAttribute true for getting the first atribute, false otherwise
331   /// \return an attribute or NULL
332   std::shared_ptr<GeomDataAPI_Point2D> getPointAttribute(const bool isFirstAttribute);
333
334 #ifdef _DEBUG
335   /// Return feature name, kind, point attribute values united in a string
336   /// \param theFeature an investigated feature
337   /// \return string value
338   std::string getFeatureInfo(const std::shared_ptr<ModelAPI_Feature>& theFeature,
339                              const bool isUseAttributesInfo = true);
340 #endif
341 private:
342   typedef std::map<std::shared_ptr<GeomDataAPI_Point2D>,
343                    std::shared_ptr<GeomAPI_Pnt> > PntToAttributesMap;
344
345   std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
346   std::map<std::shared_ptr<ModelAPI_Object>, PntToAttributesMap> myCashedReferences;
347 };
348
349 #endif