Salome HOME
SketchPlugin refactoring
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Trim.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_Trim_H_
22 #define SketchPlugin_Trim_H_
23
24 #include "SketchPlugin.h"
25
26 #include "GeomAPI_IPresentable.h"
27 #include <ModelAPI_IReentrant.h>
28 #include <SketchPlugin_Sketch.h>
29
30 class GeomDataAPI_Point2D;
31 class ModelAPI_Feature;
32 class ModelAPI_Result;
33 class ModelAPI_Object;
34
35 typedef std::pair<std::string, std::shared_ptr<GeomDataAPI_Point2D> > IdToPointPair;
36
37 /** \class SketchPlugin_Trim
38  *  \ingroup Plugins
39  *  \brief Feature for creation of a new constraint trimming object. Entities for split:
40  */
41 class SketchPlugin_Trim : public SketchPlugin_Feature, public GeomAPI_IPresentable,
42                           public ModelAPI_IReentrant
43 {
44  public:
45   /// Split constraint kind
46   inline static const std::string& ID()
47   {
48     static const std::string MY_TRIM_ID("SketchTrim");
49     return MY_TRIM_ID;
50   }
51   /// \brief Returns the kind of a feature
52   SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
53   {
54     static std::string MY_KIND = SketchPlugin_Trim::ID();
55     return MY_KIND;
56   }
57
58   /// The value parameter for the constraint
59   inline static const std::string& SELECTED_OBJECT()
60   {
61     static const std::string MY_SELECTED_OBJECT("SelectedObject");
62     return MY_SELECTED_OBJECT;
63   }
64
65   /// Start 2D point of the split segment
66   inline static const std::string& SELECTED_POINT()
67   {
68     static const std::string MY_SELECTED_POINT("SelectedPoint");
69     return MY_SELECTED_POINT;
70   }
71
72   /// The value parameter for the preview object
73   inline static const std::string& PREVIEW_OBJECT()
74   {
75     static const std::string MY_PREVIEW_OBJECT("PreviewObject");
76     return MY_PREVIEW_OBJECT;
77   }
78
79   /// Start 2D point of the split segment
80   inline static const std::string& PREVIEW_POINT()
81   {
82     static const std::string MY_PREVIEW_POINT("PreviewPoint");
83     return MY_PREVIEW_POINT;
84   }
85
86   /// \brief Creates a new part document if needed
87   SKETCHPLUGIN_EXPORT virtual void execute();
88
89   /// \brief Request for initialization of data model of the feature: adding all attributes
90   SKETCHPLUGIN_EXPORT virtual void initAttributes();
91
92   /// Reimplemented from ModelAPI_Feature::isMacro()
93   /// \returns true
94   SKETCHPLUGIN_EXPORT virtual bool isMacro() const { return true; }
95
96   /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false.
97   /// This is necessary to perform execute only by apply the feature
98   SKETCHPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; }
99
100   /// \brief Use plugin manager for features creation
101   SketchPlugin_Trim();
102
103   /// Returns the AIS preview
104   SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious);
105
106   /// Apply information of the message to current object. It fills selected point and object
107   virtual std::string processEvent(const std::shared_ptr<Events_Message>& theMessage);
108
109   typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
110                    std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
111                              std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
112
113   static void fillObjectShapes(const std::shared_ptr<ModelAPI_Object>& theObject,
114     const std::shared_ptr<ModelAPI_Object>& theSketch,
115     std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> >& theCashedShapes,
116     std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap>& theObjectToPoints);
117
118 private:
119   bool setCoincidenceToAttribute(const AttributePtr& theAttribute,
120             const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
121             std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
122   /// Move tangency constraint to the feature if it is geometrically closely to it
123   /// \param theAttribute an attribute of a tangent constraint feature
124   /// \param theFeature a feature that can be set into the attribute
125   bool moveTangency(const AttributePtr& theAttribute, const FeaturePtr& theFeature);
126
127   GeomShapePtr getSubShape(const std::string& theObjectAttributeId,
128                            const std::string& thePointAttributeId);
129
130   /// Returns geom point attribute of the feature bounds. It processes line or arc.
131   /// For circle feature, the result attributes are null
132   /// \param theFeature a source feature
133   /// \param theStartPointAttr an out attribute to start point
134   /// \param theStartPointAttr an out attribute to end point
135   void getFeaturePoints(const FeaturePtr& theFeature,
136                         std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
137                         std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
138
139   /// Obtains those constraints of the feature that should be modified. output maps contain
140   /// point of coincidence and attribute id to be modified after split
141   /// \param theFeaturesToDelete [out] constrains that will be deleted after split
142   /// \param theFeaturesToUpdate [out] constrains that will be updated after split
143   void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
144                       std::set<FeaturePtr>& theFeaturesToUpdate);
145
146   /// Obtains references to feature point attributes and to feature,
147   /// e.g. for feature line: 1st container is
148   ///             <1st line point, list<entity_a in distance, entity_b in parallel> >
149   ///             <2nd line point, list<> >
150   ///      for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
151   /// \param theFeature an investigated feature
152   /// \param theRefs a container of list of referenced attributes
153   void getRefAttributes(const FeaturePtr& theFeature,
154                         std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
155                         std::list<AttributePtr>& theRefsToFeature);
156
157   /// Move constraints from attribute of base feature to attribute after modification
158   /// \param theBaseRefAttributes container of references to the attributes of base feature
159   /// \param theModifiedAttributes container of attributes placed instead of base attributes
160   /// at the same place
161   void updateRefAttConstraints(
162                const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
163                const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
164                std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
165
166   /// Remove references constraints from attribute of base feature refer to the given attribute
167   /// \param theAttribute an attribute
168   /// \param theModifiedAttributes modifiable container of attributes
169   void removeReferencesToAttribute(const AttributePtr& theAttribute,
170                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes);
171
172    /// Updates line length if it exist in the list
173   /// \param theFeaturesToUpdate a constraints container
174   void updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate);
175
176   /// Make the base object is splitted by the point attributes
177   /// \param theBaseRefAttributes container of references to the attributes of base feature
178   /// \param thePoints a list of points where coincidences will be build
179   /// \param theModifiedAttributes a container of attribute on base
180   /// feature to attribute on new feature
181   /// \return new line if it was created
182   FeaturePtr trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
183                 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
184                 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
185                 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
186                 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
187
188   /// Make the base object is splitted by the point attributes
189   /// \param thePoints a list of points where coincidences will be build
190   /// \return new line if it was created
191   FeaturePtr trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
192                const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
193                std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
194                std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
195                std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
196
197   /// Make the base object is splitted by the point attributes
198   /// \param thePoints a list of points where coincidences will be build
199   FeaturePtr trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
200                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
201                   std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
202                   std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
203
204   /// Correct the first and the second point to provide condition that the first is closer to
205   /// the start point and the second point - to the last end of current segment. To rearrange
206   /// them if this condition is not satisfied.
207   /// \param theStartPointAttr a start point of a segment
208   /// \param theEndPointAttr an end point of a segment
209   /// \param theFirstPoint a start point of a segment
210   /// \param theSecondPoint an end point of a segment
211   void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
212                            const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
213                            std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
214                            std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
215
216   /// Correct the first and the second point to provide condition that the first is closer to
217   /// the start point and the second point - to the last end of current segment. To rearrange
218   /// them if this condition is not satisfied.
219   /// \param theArc an arc to be split
220   /// \param theStartPointAttr a start point of a segment
221   /// \param theEndPointAttr an end point of a segment
222   /// \param theFirstPoint a start point of a segment
223   /// \param theSecondPoint an end point of a segment
224   void arrangePointsOnArc(const FeaturePtr& theArc,
225                           const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
226                           const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
227                           std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
228                           std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
229
230   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
231   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
232   /// \param theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
233   void fillAttribute(const AttributePtr& theModifiedAttribute,
234                      const AttributePtr& theSourceAttribute);
235
236   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
237   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
238   /// \param thePoint a point value
239   void fillPointAttribute(const AttributePtr& theModifiedAttribute,
240                           const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
241
242   /// Creates a line feature filled by center of base feature and given points
243   /// \param theBaseFeature another arc feature
244   /// \param theFirstAttribute an attribute with coordinates for the start point
245   /// \param theSecondAttribute an attribute with coordinates for the end point
246   FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
247                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
248                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
249
250   /// Creates an arc feature filled by center of base feature and given points
251   /// \param theBaseFeature another arc feature
252   /// \param theFirstAttribute an attribute with coordinates for the start point
253   /// \param theSecondAttribute an attribute with coordinates for the end point
254   FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
255                               const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
256                               const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
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 private:
265   void findShapePoints(const std::string& theObjectAttributeId,
266                        const std::string& thePointAttributeId,
267                        std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
268                        std::shared_ptr<GeomAPI_Pnt>& aLastPoint);
269
270   std::shared_ptr<GeomAPI_Pnt2d> convertPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
271
272 private:
273   std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
274   std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap> myObjectToPoints;
275 };
276
277 #endif