1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #ifndef SketchPlugin_Trim_H_
21 #define SketchPlugin_Trim_H_
23 #include "SketchPlugin.h"
25 #include "GeomAPI_IPresentable.h"
26 #include <ModelAPI_IReentrant.h>
27 #include <SketchPlugin_Sketch.h>
29 class GeomDataAPI_Point2D;
30 class ModelAPI_Feature;
31 class ModelAPI_Result;
32 class ModelAPI_Object;
34 typedef std::pair<std::string, std::shared_ptr<GeomDataAPI_Point2D> > IdToPointPair;
36 /** \class SketchPlugin_Trim
38 * \brief Feature for creation of a new constraint trimming object. Entities for split:
40 class SketchPlugin_Trim : public SketchPlugin_Feature, public GeomAPI_IPresentable,
41 public ModelAPI_IReentrant
44 /// Split constraint kind
45 inline static const std::string& ID()
47 static const std::string MY_TRIM_ID("SketchTrim");
50 /// \brief Returns the kind of a feature
51 SKETCHPLUGIN_EXPORT virtual const std::string& getKind()
53 static std::string MY_KIND = SketchPlugin_Trim::ID();
57 /// The value parameter for the constraint
58 inline static const std::string& SELECTED_OBJECT()
60 static const std::string MY_SELECTED_OBJECT("SelectedObject");
61 return MY_SELECTED_OBJECT;
64 /// Start 2D point of the split segment
65 inline static const std::string& SELECTED_POINT()
67 static const std::string MY_SELECTED_POINT("SelectedPoint");
68 return MY_SELECTED_POINT;
71 /// The value parameter for the preview object
72 inline static const std::string& PREVIEW_OBJECT()
74 static const std::string MY_PREVIEW_OBJECT("PreviewObject");
75 return MY_PREVIEW_OBJECT;
78 /// Start 2D point of the split segment
79 inline static const std::string& PREVIEW_POINT()
81 static const std::string MY_PREVIEW_POINT("PreviewPoint");
82 return MY_PREVIEW_POINT;
85 /// \brief Creates a new part document if needed
86 SKETCHPLUGIN_EXPORT virtual void execute();
88 /// \brief Request for initialization of data model of the feature: adding all attributes
89 SKETCHPLUGIN_EXPORT virtual void initAttributes();
91 /// Reimplemented from ModelAPI_Feature::isMacro()
93 SKETCHPLUGIN_EXPORT virtual bool isMacro() const { return true; }
95 /// Reimplemented from ModelAPI_Feature::isPreviewNeeded(). Returns false.
96 /// This is necessary to perform execute only by apply the feature
97 SKETCHPLUGIN_EXPORT virtual bool isPreviewNeeded() const { return false; }
99 /// \brief Use plugin manager for features creation
102 /// Returns the AIS preview
103 SKETCHPLUGIN_EXPORT virtual AISObjectPtr getAISObject(AISObjectPtr thePrevious);
105 /// Moves the feature : Empty
106 SKETCHPLUGIN_EXPORT virtual void move(const double theDeltaX, const double theDeltaY) {};
108 /// Apply information of the message to current object. It fills selected point and object
109 virtual std::string processEvent(const std::shared_ptr<Events_Message>& theMessage);
111 typedef std::map<std::shared_ptr<GeomAPI_Pnt>,
112 std::pair<std::list<std::shared_ptr<GeomDataAPI_Point2D> >,
113 std::list<std::shared_ptr<ModelAPI_Object> > > > PointToRefsMap;
115 static void fillObjectShapes(const std::shared_ptr<ModelAPI_Object>& theObject,
116 const std::shared_ptr<ModelAPI_Object>& theSketch,
117 std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> >& theCashedShapes,
118 std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap>& theObjectToPoints);
121 bool setCoincidenceToAttribute(const AttributePtr& theAttribute,
122 const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences);
123 /// Move tangency constraint to the feature if it is geometrically closely to it
124 /// \param theAttribute an attribute of a tangent constraint feature
125 /// \param theFeature a feature that can be set into the attribute
126 bool moveTangency(const AttributePtr& theAttribute, const FeaturePtr& theFeature);
128 GeomShapePtr getSubShape(const std::string& theObjectAttributeId,
129 const std::string& thePointAttributeId);
131 /// Returns geom point attribute of the feature bounds. It processes line or arc.
132 /// For circle feature, the result attributes are null
133 /// \param theFeature a source feature
134 /// \param theStartPointAttr an out attribute to start point
135 /// \param theStartPointAttr an out attribute to end point
136 void getFeaturePoints(const FeaturePtr& theFeature,
137 std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
138 std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr);
140 /// Obtains those constraints of the feature that should be modified. output maps contain
141 /// point of coincidence and attribute id to be modified after split
142 /// \param theFeaturesToDelete [out] constrains that will be deleted after split
143 /// \param theFeaturesToUpdate [out] constrains that will be updated after split
144 void getConstraints(std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
145 std::set<FeaturePtr>& theFeaturesToUpdate);
147 /// Obtains references to feature point attributes and to feature,
148 /// e.g. for feature line: 1st container is
149 /// <1st line point, list<entity_a in distance, entity_b in parallel> >
150 /// <2nd line point, list<> >
151 /// for feature circle 2nd container is <entity_a in Radius, entity_b in equal, ...>
152 /// \param theFeature an investigated feature
153 /// \param theRefs a container of list of referenced attributes
154 void getRefAttributes(const FeaturePtr& theFeature,
155 std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
156 std::list<AttributePtr>& theRefsToFeature);
158 /// Move constraints from attribute of base feature to attribute after modification
159 /// \param theBaseRefAttributes container of references to the attributes of base feature
160 /// \param theModifiedAttributes container of attributes placed instead of base attributes
161 /// at the same place
162 void updateRefAttConstraints(
163 const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
164 const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
165 std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete);
167 /// Remove references constraints from attribute of base feature refer to the given attribute
168 /// \param theAttribute an attribute
169 /// \param theModifiedAttributes modifiable container of attributes
170 void removeReferencesToAttribute(const AttributePtr& theAttribute,
171 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes);
173 /// Updates line length if it exist in the list
174 /// \param theFeaturesToUpdate a constraints container
175 void updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate);
177 /// Make the base object is splitted by the point attributes
178 /// \param theBaseRefAttributes container of references to the attributes of base feature
179 /// \param thePoints a list of points where coincidences will be build
180 /// \param theModifiedAttributes a container of attribute on base
181 /// feature to attribute on new feature
182 /// \return new line if it was created
183 FeaturePtr trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
184 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
185 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
186 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
187 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
189 /// Make the base object is splitted by the point attributes
190 /// \param thePoints a list of points where coincidences will be build
191 /// \return new line if it was created
192 FeaturePtr trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
193 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
194 std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
195 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
196 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
198 /// Make the base object is splitted by the point attributes
199 /// \param thePoints a list of points where coincidences will be build
200 FeaturePtr trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
201 const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
202 std::set<std::shared_ptr<GeomDataAPI_Point2D> >& thePoints,
203 std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes);
205 /// Correct the first and the second point to provide condition that the first is closer to
206 /// the start point and the second point - to the last end of current segment. To rearrange
207 /// them if this condition is not satisfied.
208 /// \param theStartPointAttr a start point of a segment
209 /// \param theEndPointAttr an end point of a segment
210 /// \param theFirstPoint a start point of a segment
211 /// \param theSecondPoint an end point of a segment
212 void arrangePointsOnLine(const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
213 const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
214 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
215 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
217 /// Correct the first and the second point to provide condition that the first is closer to
218 /// the start point and the second point - to the last end of current segment. To rearrange
219 /// them if this condition is not satisfied.
220 /// \param theArc an arc to be split
221 /// \param theStartPointAttr a start point of a segment
222 /// \param theEndPointAttr an end point of a segment
223 /// \param theFirstPoint a start point of a segment
224 /// \param theSecondPoint an end point of a segment
225 void arrangePointsOnArc(const FeaturePtr& theArc,
226 const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
227 const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
228 std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
229 std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const;
231 /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
232 /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
233 /// \param theSourceAttribute an attribute of GeomDataAPI_Point2D to obtain data
234 void fillAttribute(const AttributePtr& theModifiedAttribute,
235 const AttributePtr& theSourceAttribute);
237 /// Fill attribute by value of another attribute. It processes only Point 2D attributes.
238 /// \param theModifiedAttribute an attribute of GeomDataAPI_Point2D on feature to be modified
239 /// \param thePoint a point value
240 void fillPointAttribute(const AttributePtr& theModifiedAttribute,
241 const std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
243 /// Creates a line feature filled by center of base feature and given points
244 /// \param theBaseFeature another arc feature
245 /// \param theFirstAttribute an attribute with coordinates for the start point
246 /// \param theSecondAttribute an attribute with coordinates for the end point
247 FeaturePtr createLineFeature(const FeaturePtr& theBaseFeature,
248 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
249 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
251 /// Creates an arc feature filled by center of base feature and given points
252 /// \param theBaseFeature another arc feature
253 /// \param theFirstAttribute an attribute with coordinates for the start point
254 /// \param theSecondAttribute an attribute with coordinates for the end point
255 FeaturePtr createArcFeature(const FeaturePtr& theBaseFeature,
256 const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
257 const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint);
259 /// Add feature coincidence constraint between given attributes
260 /// \param theConstraintId a constraint index
261 /// \param theFirstAttribute an attribute of further coincidence
262 /// \param theSecondAttribute an attribute of further coincidence
263 std::shared_ptr<ModelAPI_Feature> createConstraint(const std::string& theConstraintId,
264 const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
265 const std::shared_ptr<ModelAPI_Attribute>& theSecondAttribute);
267 /// Add feature coincidence constraint between given attributes
268 /// \param theConstraintId a constraint index
269 /// \param theFirstAttribute an attribute of further coincidence
270 /// \param theSecondObject an object of further coincidence
271 std::shared_ptr<ModelAPI_Feature> createConstraintToObject(const std::string& theConstraintId,
272 const std::shared_ptr<ModelAPI_Attribute>& theFirstAttribute,
273 const std::shared_ptr<ModelAPI_Object>& theSecondObject);
275 /// Add feature coincidence constraint between given attributes
276 /// \param theConstraintId a constraint index
277 /// \param theFirstAttribute an attribute of further coincidence
278 /// \param theFirstAttribute an attribute of further coincidence
279 std::shared_ptr<ModelAPI_Feature> createConstraintForObjects(const std::string& theConstraintId,
280 const std::shared_ptr<ModelAPI_Object>& theFirstObject,
281 const std::shared_ptr<ModelAPI_Object>& theSecondObject);
283 /// Result result of the feature to build constraint with. For arc, circle it is an edge result.
284 /// \param theFeature a feature
285 /// \return result object
286 std::shared_ptr<ModelAPI_Result> getFeatureResult(
287 const std::shared_ptr<ModelAPI_Feature>& theFeature);
290 void findShapePoints(const std::string& theObjectAttributeId,
291 const std::string& thePointAttributeId,
292 std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
293 std::shared_ptr<GeomAPI_Pnt>& aLastPoint);
295 std::shared_ptr<GeomAPI_Pnt2d> convertPoint(const std::shared_ptr<GeomAPI_Pnt>& thePoint);
298 std::map<std::shared_ptr<ModelAPI_Object>, std::set<GeomShapePtr> > myCashedShapes;
299 std::map<std::shared_ptr<ModelAPI_Object>, PointToRefsMap> myObjectToPoints;