]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Trim.h
Salome HOME
Merge branch 'master' into cgt/devCEA
[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   /// Moves the feature : Empty
107   SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY) {};
108
109   /// Apply information of the message to current object. It fills selected point and object
110   virtual std::string processEvent(const std::shared_ptr<Events_Message>& theMessage);
111
112   typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
113                    std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
114                              std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
115
116   static void fillObjectShapes(const std::shared_ptr<ModelAPI_Object>& theObject,
117     const std::shared_ptr<ModelAPI_Object>& theSketch,
118     std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> >& theCashedShapes,
119     std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap>& theObjectToPoints);
120
121 private:
122   bool setCoincidenceToAttribute(const AttributePtr& theAttribute,
123             const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences);
124   /// Move tangency constraint to the feature if it is geometrically closely to it
125   /// \param theAttribute an attribute of a tangent constraint feature
126   /// \param theFeature a feature that can be set into the attribute
127   bool moveTangency(const AttributePtr& theAttribute, const FeaturePtr& theFeature);
128
129   GeomShapePtr getSubShape(const std::string& theObjectAttributeId,
130                            const std::string& thePointAttributeId);
131
132   /// Returns geom point attribute of the feature bounds. It processes line or arc.
133   /// For circle feature, the result attributes are null
134   /// \param theFeature a source feature
135   /// \param theStartPointAttr an out attribute to start point
136   /// \param theStartPointAttr an out attribute to end point
137   void getFeaturePoints(const FeaturePtr& theFeature,
138                         std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
139                         std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
140
141   /// Obtains those constraints of the feature that should be modified. output maps contain
142   /// point of coincidence and attribute id to be modified after split
143   /// \param theFeaturesToDelete [out] constrains that will be deleted after split
144   /// \param theFeaturesToUpdate [out] constrains that will be updated after split
145   void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
146                       std::set<FeaturePtr>& theFeaturesToUpdate);
147
148   /// Obtains references to feature point attributes and to feature,
149   /// e.g. for feature line: 1st container is
150   ///             <1st line point, list<entity_a in distance, entity_b in parallel> >
151   ///             <2nd line point, list<> >
152   ///      for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
153   /// \param theFeature an investigated feature
154   /// \param theRefs a container of list of referenced attributes
155   void getRefAttributes(const FeaturePtr& theFeature,
156                         std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
157                         std::list<AttributePtr>& theRefsToFeature);
158
159   /// Move constraints from attribute of base feature to attribute after modification
160   /// \param theBaseRefAttributes container of references to the attributes of base feature
161   /// \param theModifiedAttributes container of attributes placed instead of base attributes
162   /// at the same place
163   void updateRefAttConstraints(
164                const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
165                const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
166                std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
167
168   /// Remove references constraints from attribute of base feature refer to the given attribute
169   /// \param theAttribute an attribute
170   /// \param theModifiedAttributes modifiable container of attributes
171   void removeReferencesToAttribute(const AttributePtr& theAttribute,
172                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes);
173
174    /// Updates line length if it exist in the list
175   /// \param theFeaturesToUpdate a constraints container
176   void updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate);
177
178   /// Make the base object is splitted by the point attributes
179   /// \param theBaseRefAttributes container of references to the attributes of base feature
180   /// \param thePoints a list of points where coincidences will be build
181   /// \param theModifiedAttributes a container of attribute on base
182   /// feature to attribute on new feature
183   /// \return new line if it was created
184   FeaturePtr trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
185                 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
186                 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
187                 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
188                 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
189
190   /// Make the base object is splitted by the point attributes
191   /// \param thePoints a list of points where coincidences will be build
192   /// \return new line if it was created
193   FeaturePtr trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
194                const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
195                std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
196                std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
197                std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
198
199   /// Make the base object is splitted by the point attributes
200   /// \param thePoints a list of points where coincidences will be build
201   FeaturePtr trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
202                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
203                   std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
204                   std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
205
206   /// Correct the first and the second point to provide condition that the first is closer to
207   /// the start point and the second point - to the last end of current segment. To rearrange
208   /// them if this condition is not satisfied.
209   /// \param theStartPointAttr a start point of a segment
210   /// \param theEndPointAttr an end point of a segment
211   /// \param theFirstPoint a start point of a segment
212   /// \param theSecondPoint an end point of a segment
213   void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
214                            const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
215                            std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
216                            std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
217
218   /// Correct the first and the second point to provide condition that the first is closer to
219   /// the start point and the second point - to the last end of current segment. To rearrange
220   /// them if this condition is not satisfied.
221   /// \param theArc an arc to be split
222   /// \param theStartPointAttr a start point of a segment
223   /// \param theEndPointAttr an end point of a segment
224   /// \param theFirstPoint a start point of a segment
225   /// \param theSecondPoint an end point of a segment
226   void arrangePointsOnArc(const FeaturePtr& theArc,
227                           const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
228                           const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
229                           std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
230                           std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
231
232   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
233   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
234   /// \param theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
235   void fillAttribute(const AttributePtr& theModifiedAttribute,
236                      const AttributePtr& theSourceAttribute);
237
238   /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
239   /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
240   /// \param thePoint a point value
241   void fillPointAttribute(const AttributePtr& theModifiedAttribute,
242                           const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
243
244   /// Creates a line feature filled by center of base feature and given points
245   /// \param theBaseFeature another arc feature
246   /// \param theFirstAttribute an attribute with coordinates for the start point
247   /// \param theSecondAttribute an attribute with coordinates for the end point
248   FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
249                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
250                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
251
252   /// Creates an arc feature filled by center of base feature and given points
253   /// \param theBaseFeature another arc feature
254   /// \param theFirstAttribute an attribute with coordinates for the start point
255   /// \param theSecondAttribute an attribute with coordinates for the end point
256   FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
257                               const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
258                               const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
259
260   /// Add feature coincidence constraint between given attributes
261   /// \param theConstraintId a constraint index
262   /// \param theFirstAttribute an attribute of further coincidence
263   /// \param theSecondAttribute an attribute of further coincidence
264   std::shared_ptr<ModelAPI_Feature> createConstraint(const std::string& theConstraintId,
265                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
266                         const std::shared_ptr<ModelAPI_Attribute>& theSecondAttribute);
267
268   /// Add feature coincidence constraint between given attributes
269   /// \param theConstraintId a constraint index
270   /// \param theFirstAttribute an attribute of further coincidence
271   /// \param theSecondObject an object of further coincidence
272   std::shared_ptr<ModelAPI_Feature> createConstraintToObject(const std::string& theConstraintId,
273                         const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
274                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
275
276   /// Add feature coincidence constraint between given attributes
277   /// \param theConstraintId a constraint index
278   /// \param theFirstAttribute an attribute of further coincidence
279   /// \param theFirstAttribute an attribute of further coincidence
280   std::shared_ptr<ModelAPI_Feature> createConstraintForObjects(const std::string& theConstraintId,
281                         const std::shared_ptr<ModelAPI_Object>& theFirstObject,
282                         const std::shared_ptr<ModelAPI_Object>& theSecondObject);
283
284   /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
285   /// \param theFeature a feature
286   /// \return result object
287   std::shared_ptr<ModelAPI_Result> getFeatureResult(
288                                     const std::shared_ptr<ModelAPI_Feature>& theFeature);
289
290 private:
291   void findShapePoints(const std::string& theObjectAttributeId,
292                        const std::string& thePointAttributeId,
293                        std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
294                        std::shared_ptr<GeomAPI_Pnt>& aLastPoint);
295
296   std::shared_ptr<GeomAPI_Pnt2d> convertPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
297
298 private:
299   std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
300   std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap> myObjectToPoints;
301 };
302
303 #endif