Salome HOME
Issue #2155 Trim removes multi-rotation constraint, undo leads to wrong DOF
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Trim.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_Trim.cpp
4 // Created: 22 Feb 2017
5 // Author:  Natalia ERMOLAEVA
6
7 #include "SketchPlugin_Trim.h"
8
9 #include <GeomAPI_Dir2d.h>
10 #include <GeomAPI_Edge.h>
11 #include <GeomAPI_Pnt2d.h>
12 #include <GeomAPI_XY.h>
13 #include <GeomDataAPI_Point2D.h>
14 #include <GeomAlgoAPI_ShapeTools.h>
15 #include <GeomAlgoAPI_CompoundBuilder.h>
16
17 #include <ModelAPI_AttributeReference.h>
18 #include <ModelAPI_AttributeString.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_Tools.h>
21 #include <ModelAPI_AttributeBoolean.h>
22
23 #include <ModelAPI_Validator.h>
24 #include <ModelAPI_Session.h>
25 #include <ModelAPI_AttributeDouble.h>
26
27 #include <ModelGeomAlgo_Shape.h>
28
29 #include <SketchPlugin_Arc.h>
30 #include <SketchPlugin_ConstraintMiddle.h>
31 #include <SketchPlugin_Circle.h>
32 #include <SketchPlugin_ConstraintCoincidence.h>
33 #include <SketchPlugin_ConstraintEqual.h>
34 //#include <SketchPlugin_ConstraintParallel.h>
35 #include <SketchPlugin_ConstraintTangent.h>
36 #include <SketchPlugin_ConstraintLength.h>
37 #include <SketchPlugin_ConstraintMirror.h>
38 #include <SketchPlugin_ConstraintCollinear.h>
39 #include <SketchPlugin_Line.h>
40 #include <SketchPlugin_MultiRotation.h>
41 #include <SketchPlugin_MultiTranslation.h>
42 #include <SketchPlugin_Point.h>
43
44 #include <ModelAPI_EventReentrantMessage.h>
45
46 #include <ModelAPI_Events.h>
47 #include <SketchPlugin_Line.h>
48 #include <SketchPlugin_Arc.h>
49 #include <SketchPlugin_Circle.h>
50
51 #include <ModelGeomAlgo_Point2D.h>
52 #include <Events_Loop.h>
53
54 #include <cmath>
55
56 //#define DEBUG_TRIM_METHODS
57 //#define DEBUG_TRIM
58
59 #ifdef DEBUG_TRIM
60 #include <iostream>
61 #endif
62
63 #ifdef DEBUG_TRIM_METHODS
64 #include <iostream>
65 #endif
66
67 static const double PI = 3.141592653589793238463;
68
69 static const std::string OPERATION_HIGHLIGHT_COLOR() { return "128, 0, 0"; }
70 static const std::string OPERATION_REMOVE_FEATURE_COLOR() { return "255, 174, 201"; }
71
72 SketchPlugin_Trim::SketchPlugin_Trim()
73 {
74 }
75
76 void SketchPlugin_Trim::initAttributes()
77 {
78   data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
79   data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
80
81   data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
82   data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
83
84   data()->attribute(PREVIEW_POINT())->setIsArgument(false);
85   data()->attribute(SELECTED_POINT())->setIsArgument(false);
86   data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
87
88   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
89   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
90 }
91
92 void SketchPlugin_Trim::findShapePoints(const std::string& theObjectAttributeId,
93                                         const std::string& thePointAttributeId,
94                                         std::shared_ptr<GeomAPI_Pnt>& aStartPoint,
95                                         std::shared_ptr<GeomAPI_Pnt>& aLastPoint)
96 {
97   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
98                                             data()->attribute(theObjectAttributeId));
99   ObjectPtr aBaseObject = aBaseObjectAttr->value();
100
101   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
102                                               data()->attribute(thePointAttributeId));
103   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPoint->pnt();
104   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
105                                                                anAttributePnt2d->y());
106
107   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
108     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
109
110   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
111   if (!aShapes.empty()) {
112     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
113     for (; anIt != aLast; anIt++) {
114       GeomShapePtr aBaseShape = *anIt;
115       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
116       if (ModelGeomAlgo_Point2D::isPointOnEdge(aBaseShape, anAttributePnt, aProjectedPoint)) {
117
118         if (aBaseShape->shapeType() == GeomAPI_Shape::EDGE) {
119           std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aBaseShape));
120           //GeomAPI_Shape::Orientation anOrientation = anEdge->orientation();
121           //if (anOrientation == GeomAPI_Shape::REVERSED) {
122             aStartPoint = anEdge->lastPoint();
123             aLastPoint = anEdge->firstPoint();
124           //}
125           //else {
126             //aStartPoint = anEdge->firstPoint();
127             //aLastPoint = anEdge->lastPoint();
128           //}
129         }
130       }
131     }
132   }
133 #ifdef DEBUG_TRIM
134   std::cout << "<findShapePoints> => "
135     << std::endl << "Attribute point: "
136     << anAttributePnt->x() << ", " << anAttributePnt->y() << ", " << anAttributePnt->z() << "]"
137     << std::endl << "Start Point: ["
138     << aStartPoint->x() << ", " << aStartPoint->y() << ", " << aStartPoint->z() << "]"
139     << std::endl << "Last Point: ["
140     << aLastPoint->x() << ", " << aLastPoint->y() << ", " << aLastPoint->z() << "]"
141     << std::endl;
142 #endif
143 }
144
145 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Trim::convertPoint(
146                                                    const std::shared_ptr<GeomAPI_Pnt>& thePoint)
147 {
148   std::shared_ptr<GeomAPI_Pnt2d> aPoint;
149   if (!thePoint.get())
150     return aPoint;
151
152   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
153                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
154   ObjectPtr aBaseObject = aBaseObjectAttr->value();
155   if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
156     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
157
158   bool aFound = false;
159   const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
160   for (PointToRefsMap::const_iterator aPointIt = aRefsMap.begin();
161        aPointIt != aRefsMap.end() && !aFound; aPointIt++) {
162     if (aPointIt->first->isEqual(thePoint)) {
163       const std::pair<std::list<AttributePoint2DPtr >,
164                std::list<ObjectPtr > >& anInfo = aPointIt->second;
165       const std::list<AttributePoint2DPtr >& anAttributes = anInfo.first;
166       if (!anAttributes.empty()) {
167         aPoint = anAttributes.front()->pnt();
168         aFound = true;
169       }
170       else {
171         aPoint = sketch()->to2D(thePoint);
172         aFound = true;
173       }
174     }
175   }
176   if (!aFound) {
177     // returns an end of the shape to define direction of split if feature's attribute
178     // participates
179     aPoint = sketch()->to2D(thePoint);
180   }
181   return aPoint;
182 }
183
184 void SketchPlugin_Trim::execute()
185 {
186 #ifdef DEBUG_TRIM_METHODS
187   std::cout << "SketchPlugin_Trim::execute: " << data()->name() << std::endl;
188 #endif
189
190   SketchPlugin_Sketch* aSketch = sketch();
191   if (!aSketch) {
192     setError("Error: Sketch object is empty.");
193     return;
194   }
195
196   // Check the base objects are initialized.
197   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
198                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
199   if(!aBaseObjectAttr->isInitialized()) {
200     setError("Error: Base object is not initialized.");
201     return;
202   }
203   ObjectPtr aBaseObject = aBaseObjectAttr->value();
204   if (!aBaseObject.get()) {
205     setError("Error: Base object is not initialized.");
206     return;
207   }
208   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
209
210   /// Remove reference of this feature to feature used in preview, it is not necessary anymore
211   /// as trim will be removed after execute
212   AttributeReferencePtr aPreviewObjectAttr =
213                      std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
214                      data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
215
216   ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
217   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
218                                            data()->attribute(PREVIEW_POINT()));
219   std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
220   // nullify pointer of preview attribute
221   aPreviewObjectAttr->setValue(ResultPtr());
222
223   bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
224
225   /// points of trim
226   std::shared_ptr<GeomAPI_Pnt> aStartShapePoint, aLastShapePoint;
227 #ifdef DEBUG_TRIM
228   std::cout << " Base Feature: " << aBaseFeature->data()->name() << std::endl;
229 #endif
230   findShapePoints(SELECTED_OBJECT(), SELECTED_POINT(), aStartShapePoint, aLastShapePoint);
231
232   std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint2d = convertPoint(aStartShapePoint);
233   std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint2d = convertPoint(aLastShapePoint);
234   /// find features that should be deleted (e.g. Middle Point) or updated (e.g. Length)
235   std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
236   getConstraints(aFeaturesToDelete, aFeaturesToUpdate);
237   // find references(attributes and features) to the base feature
238   std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
239   std::list<AttributePtr> aRefsToFeature;
240   getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
241 #ifdef DEBUG_TRIM
242   std::cout << "---- getRefAttributes ----" << std::endl;
243   std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
244     aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
245   std::cout << std::endl << "References to attributes of base feature [" <<
246     aBaseRefAttributes.size() << "]" << std::endl;
247   for (; aRefIt != aRefLast; aRefIt++) {
248     AttributePtr aBaseAttr = aRefIt->first;
249     std::list<AttributePtr> aRefAttributes = aRefIt->second;
250     std::string aRefsInfo;
251     std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
252                                             aRefAttrLast = aRefAttributes.end();
253     for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
254       if (!aRefsInfo.empty())
255         aRefsInfo.append(",");
256       AttributePtr aRAttr = *aRefAttrIt;
257       aRefsInfo.append(aRAttr->id());
258       FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
259       aRefsInfo.append("(" + aRFeature->name() + ") ");
260     }
261     std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
262       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
263     std::cout << aPointAttr->id().c_str() <<
264       ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
265   }
266   std::cout << std::endl;
267   std::cout << std::endl << "References to base feature [" <<
268     aRefsToFeature.size() << "]" << std::endl;
269   std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
270                                           aRefAttrLast = aRefsToFeature.end();
271   std::string aRefsInfo;
272   for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
273     if (!aRefsInfo.empty())
274       aRefsInfo.append(",");
275     AttributePtr aRAttr = *aRefAttrIt;
276     aRefsInfo.append(aRAttr->id());
277     FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
278     aRefsInfo.append("(" + aRFeature->name() + ") ");
279   }
280   std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
281   std::cout << "---- getRefAttributes:end ----" << std::endl;
282 #endif
283   std::set<AttributePoint2DPtr> aFurtherCoincidences;
284   std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
285   const std::string& aKind = aBaseFeature->getKind();
286   FeaturePtr aReplacingFeature, aNewFeature;
287   if (aKind == SketchPlugin_Circle::ID()) {
288     aReplacingFeature = trimCircle(aStartShapePoint2d, aLastShapePoint2d,
289                aFurtherCoincidences, aModifiedAttributes);
290
291     aFeaturesToDelete.insert(aBaseFeature);
292     // as circle is removed, erase it from dependencies(arguments) of this feature
293     // otherwise Trim feature will be removed with the circle before
294     // this operation is finished
295     aBaseObjectAttr->setObject(ResultPtr());
296   }
297   else if (aKind == SketchPlugin_Line::ID()) {
298     aNewFeature = trimLine(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
299                            aFurtherCoincidences, aModifiedAttributes);
300   }
301   else if (aKind == SketchPlugin_Arc::ID()) {
302     aNewFeature = trimArc(aStartShapePoint2d, aLastShapePoint2d, aBaseRefAttributes,
303                           aFurtherCoincidences, aModifiedAttributes);
304   }
305
306   // constraints to end points of trim feature
307   if (myObjectToPoints.find(aBaseObject) == myObjectToPoints.end())
308     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
309
310   // create coincidence to objects, intersected the base object
311   const PointToRefsMap& aRefsMap = myObjectToPoints.at(aBaseObject);
312   for (std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
313                                                      aLast = aFurtherCoincidences.end();
314        anIt != aLast; anIt++) {
315     AttributePoint2DPtr aPointAttribute = (*anIt);
316     std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
317
318 #ifdef DEBUG_TRIM
319     std::cout << "<compare Points> => " << std::endl
320             << "aPoint2d: [" << aPoint2d->x() << ", " << aPoint2d->y() << "]" << std::endl;
321     if (aStartShapePoint2d.get())
322       std::cout << "Start Point: [" << aStartShapePoint2d->x() << ", " << aStartShapePoint2d->y()
323                 << "]" << std::endl;
324     if (aLastShapePoint2d.get())
325       std::cout << "Last Point: [" << aLastShapePoint2d->x() << ", " << aLastShapePoint2d->y()
326                 << "]" << std::endl;
327 #endif
328
329     std::shared_ptr<GeomAPI_Pnt> aPoint;
330     if (aStartShapePoint2d.get() && aPoint2d->isEqual(aStartShapePoint2d))
331       aPoint = aStartShapePoint;
332     else if (aLastShapePoint2d.get() && aPoint2d->isEqual(aLastShapePoint2d))
333       aPoint = aLastShapePoint;
334
335     if (!aPoint.get())
336       continue;
337
338     std::pair<std::list<AttributePoint2DPtr >, std::list<ObjectPtr > > anInfo;
339     for (PointToRefsMap::const_iterator aRefIt = aRefsMap.begin(); aRefIt != aRefsMap.end();
340          aRefIt++)
341     {
342       if (aRefIt->first->isEqual(aPoint)) {
343         anInfo = aRefIt->second;
344         break;
345       }
346     }
347     const std::list<ObjectPtr>& anObjects = anInfo.second;
348     for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
349       anObjectIt != anObjects.end(); anObjectIt++) {
350       createConstraintToObject(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute,
351                                *anObjectIt);
352     }
353   }
354
355   // move constraints from base feature to replacing feature: ignore coincidences to feature
356   // if attributes of coincidence participated in split
357   ResultPtr aReplacingResult;
358   if (aReplacingFeature.get()) {
359     aReplacingFeature->execute(); // need it to obtain result
360     aReplacingResult = getFeatureResult(aReplacingFeature);
361   }
362   for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
363                                           aLast = aRefsToFeature.end();
364       anIt != aLast; anIt++) {
365     AttributePtr anAttribute = *anIt;
366
367     if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences))
368       continue;
369
370     // move tangency constraint to the nearest feature if possible
371     if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
372       continue;
373
374     if (aReplacingResult.get()) {
375       AttributeRefAttrPtr aRefAttr =
376           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
377       if (aRefAttr.get())
378         aRefAttr->setObject(aReplacingResult);
379       else {
380         AttributeReferencePtr aReferenceAttr =
381                              std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
382         if (aReferenceAttr.get())
383           aReferenceAttr->setObject(aReplacingResult);
384       }
385     }
386   }
387
388   updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
389
390   // Wait all constraints being created, then send update events
391   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
392   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
393   if (isUpdateFlushed)
394     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
395
396   // delete constraints
397 #ifdef DEBUG_TRIM
398   if (aFeaturesToDelete.size() > 0) {
399     std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
400     std::string aValue;
401     for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
402          anIt != aFeaturesToDelete.end(); anIt++) {
403       FeaturePtr aFeature = *anIt;
404       std::cout << aFeature->data()->name() << std::endl;
405     }
406   }
407 #endif
408   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
409   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
410
411   updateFeaturesAfterTrim(aFeaturesToUpdate);
412
413   // Send events to update the sub-features by the solver.
414   if(isUpdateFlushed) {
415     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
416   }
417
418   if (anIsEqualPreviewAndSelected) {
419     // equal preview and selected objects
420     // nothing to do if the preview and selected objects are different
421     if (aReplacingResult.get()) { // base object was removed
422       aPreviewObject = aReplacingResult;
423       //aMessage->setSelectedObject(aReplacingResult);
424 #ifdef DEBUG_TRIM_METHODS
425       if (!aSelectedShape.get())
426         std::cout << "Set empty selected object" << std::endl;
427       else
428         std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
429 #endif
430     }
431     else {
432       aPreviewObject = ObjectPtr();
433
434       aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
435       aBaseObject = getFeatureResult(aBaseFeature);
436       std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
437                                                                 aPreviewPnt2d->y());
438       ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
439       if (aBaseResult) {
440         GeomShapePtr aShape = aBaseResult->shape();
441         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
442         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
443           aPreviewObject = aBaseResult;
444       }
445       if (!aPreviewObject.get() && aNewFeature.get()) {
446         ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
447         if (aNewFeatureResult.get()) {
448           GeomShapePtr aShape = aNewFeatureResult->shape();
449           std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
450           if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
451             aPreviewObject = aNewFeatureResult;
452         }
453       }
454     }
455   }
456   if (aPreviewObject.get()) {
457     std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
458       <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
459                                            ModelAPI_EventReentrantMessage::eventId(), this));
460     aMessage->setSelectedObject(aPreviewObject);
461     Events_Loop::loop()->send(aMessage);
462   }
463 #ifdef DEBUG_TRIM
464   std::cout << "SketchPlugin_Trim::done" << std::endl;
465 #endif
466 }
467
468 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
469 {
470 #ifdef DEBUG_TRIM_METHODS
471   std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
472 #endif
473   std::string aFilledAttributeName;
474
475   std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
476         std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
477   if (aMessage.get()) {
478     ObjectPtr anObject = aMessage->selectedObject();
479     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
480
481     if (anObject.get() && aPoint.get()) {
482       if (myCashedShapes.find(anObject) == myCashedShapes.end())
483         fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
484       const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
485       if (aShapes.size() > 1) {
486         std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
487                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
488                               data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
489         std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
490                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
491                               data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
492         aRefSelectedAttr->setValue(anObject);
493         aRefPreviewAttr->setValue(anObject);
494
495         std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
496                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
497                               data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
498         std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
499                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
500                               data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
501         aPointSelectedAttr->setValue(aPoint);
502         aPointPreviewAttr->setValue(aPoint);
503
504         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
505
506         GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
507   #ifdef DEBUG_TRIM_METHODS
508         if (!aSelectedShape.get())
509           std::cout << "Set empty selected object" << std::endl;
510         else
511           std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
512   #endif
513         aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
514       }
515     }
516   }
517   return aFilledAttributeName;
518 }
519
520 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
521                                 const std::set<AttributePoint2DPtr>& theFurtherCoincidences)
522 {
523   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
524   if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
525     return false;
526
527   AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
528   if (!aRefPointAttr.get())
529     return false;
530   std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
531
532   std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
533                                                 aLast = theFurtherCoincidences.end();
534   bool aFoundPoint = false;
535   for (; anIt != aLast && !aFoundPoint; anIt++) {
536     AttributePoint2DPtr aPointAttribute = (*anIt);
537     std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
538     if (aPoint2d->isEqual(aRefPnt2d)) {
539       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
540                                                                            theAttribute);
541       if (aRefAttr.get()) {
542         aRefAttr->setAttr(aPointAttribute);
543         aFoundPoint = true;
544       }
545     }
546   }
547   return aFoundPoint;
548 }
549
550 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
551                                      const FeaturePtr& theFeature)
552 {
553   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
554   if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
555     return false;
556
557   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
558                                                                            theAttribute);
559   if (!aRefAttr.get())
560     return false;
561
562   // get shape of tangent object to the current
563   std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
564   if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
565     aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
566   AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
567                                                      aFeature->attribute(aTangentAttr));
568   FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
569
570   // get shape of the feature of the attribute
571   FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
572   anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
573   PointToRefsMap aPointToAttributeOrObject;
574   std::list<FeaturePtr> aFeatures;
575   aFeatures.push_back(anAttributeFeature);
576   ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
577                                                    aPointToAttributeOrObject);
578   if (!aPointToAttributeOrObject.empty())
579     return true; // the attribute feature has a point of intersection, so we do not replace it
580
581   // get shape of the feature
582   aPointToAttributeOrObject.clear();
583   aFeatures.clear();
584   aFeatures.push_back(theFeature);
585   ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
586                                                    aPointToAttributeOrObject);
587   if (!aPointToAttributeOrObject.empty()) {
588     std::set<ResultPtr> anEdgeShapes;
589     ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
590     if (!anEdgeShapes.empty()) {
591       ResultPtr aResult = *anEdgeShapes.begin();
592       if (aResult.get()) {
593         aRefAttr->setObject(aResult);
594         return true; // the attribute feature has a point of intersection, so we do not replace it
595       }
596     }
597   }
598   return false;
599 }
600
601 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
602 {
603 #ifdef DEBUG_TRIM_METHODS
604   std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
605 #endif
606
607   AISObjectPtr anAIS = thePrevious;
608
609   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
610   GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
611   if (aPreviewShape.get())
612     aShapes.push_back(aPreviewShape);
613   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
614   if (aSelectedShape.get())
615     aShapes.push_back(aSelectedShape);
616
617   if (aShapes.empty())
618     return AISObjectPtr();
619
620   GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
621   if (!aBaseShape.get())
622     return AISObjectPtr();
623
624   if (aBaseShape.get()) {
625     if (!anAIS)
626       anAIS = AISObjectPtr(new GeomAPI_AISObject);
627     anAIS->createShape(aBaseShape);
628
629     std::vector<int> aColor;
630     aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
631     double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
632     int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
633     anAIS->setColor(aColor[0], aColor[1], aColor[2]);
634     // width when there is not base object should be extened in several points
635     // in order to see this preview over highlight
636     anAIS->setWidth(aWidth+4);
637     anAIS->setLineStyle(aLineStyle);
638   }
639   else
640     anAIS = AISObjectPtr();
641
642   return anAIS;
643 }
644
645 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
646                                             const std::string& thePointAttributeId)
647 {
648   GeomShapePtr aBaseShape;
649
650   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
651                                                        data()->attribute(theObjectAttributeId));
652   ObjectPtr aBaseObject = anObjectAttr->value();
653   if (!aBaseObject.get())
654     return aBaseShape;
655
656   // point on feature
657   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
658                                            data()->attribute(thePointAttributeId));
659   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
660   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
661                                                                anAttributePnt2d->y());
662
663   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
664     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
665
666   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
667   if (!aShapes.empty()) {
668     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
669     for (; anIt != aLast; anIt++) {
670       GeomShapePtr aShape = *anIt;
671       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
672       if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
673         aBaseShape = aShape;
674     }
675   }
676   return aBaseShape;
677 }
678
679 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
680                                          AttributePoint2DPtr& theStartPointAttr,
681                                          AttributePoint2DPtr& theEndPointAttr)
682 {
683   std::string aFeatureKind = theFeature->getKind();
684   std::string aStartAttributeName, anEndAttributeName;
685   if (aFeatureKind == SketchPlugin_Line::ID()) {
686     aStartAttributeName = SketchPlugin_Line::START_ID();
687     anEndAttributeName = SketchPlugin_Line::END_ID();
688   }
689   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
690     aStartAttributeName = SketchPlugin_Arc::START_ID();
691     anEndAttributeName = SketchPlugin_Arc::END_ID();
692   }
693   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
694     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
695                                          theFeature->attribute(aStartAttributeName));
696     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
697                                          theFeature->attribute(anEndAttributeName));
698   }
699 }
700
701 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
702                                        std::set<FeaturePtr>& theFeaturesToUpdate)
703 {
704   std::shared_ptr<ModelAPI_Data> aData = data();
705
706   // Check the base objects are initialized.
707   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
708                                          aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
709   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
710   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
711
712   std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
713   std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
714   aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
715
716   std::set<AttributePtr>::const_iterator aIt;
717   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
718     std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
719     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
720     std::string aRefFeatureKind = aRefFeature->getKind();
721     std::string anAttributeId = anAttr->id();
722     if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
723          anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
724         (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
725          anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
726         (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
727          anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
728         aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
729       theFeaturesToDelete.insert(aRefFeature);
730     else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
731       theFeaturesToUpdate.insert(aRefFeature);
732   }
733 }
734
735 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
736                                     std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
737                                     std::list<AttributePtr>& theRefsToFeature)
738 {
739   theRefs.clear();
740
741   std::list<AttributePtr> aPointAttributes =
742     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
743   std::set<AttributePtr> aPointAttributesSet;
744
745   std::list<AttributePtr>::const_iterator aPIt =
746     aPointAttributes.begin(), aPLast = aPointAttributes.end();
747   for (; aPIt != aPLast; aPIt++)
748     aPointAttributesSet.insert(*aPIt);
749
750   std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
751   std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
752   aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
753
754   std::set<AttributePtr>::const_iterator aIt;
755   for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
756     AttributePtr anAttr = (*aIt);
757     FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
758     if (anAttrFeature.get() != this &&
759         anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
760       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
761       if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
762         AttributePtr anAttrInRef = aRefAttr->attr();
763         if (anAttrInRef.get() &&
764             aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
765           if (theRefs.find(anAttrInRef) != theRefs.end())
766             theRefs[anAttrInRef].push_back(aRefAttr);
767           else {
768             std::list<AttributePtr> anAttrList;
769             anAttrList.push_back(aRefAttr);
770             theRefs[anAttrInRef] = anAttrList;
771           }
772         }
773       }
774       else { /// find attributes referenced to feature itself
775         theRefsToFeature.push_back(anAttr);
776       }
777     }
778   }
779 }
780
781 void SketchPlugin_Trim::updateRefAttConstraints(
782                     const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
783                     const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
784                     std::set<FeaturePtr>& theFeaturesToDelete)
785 {
786 #ifdef DEBUG_TRIM
787   std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
788 #endif
789
790   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
791     anIt = theModifiedAttributes.begin(),  aLast = theModifiedAttributes.end();
792   for (; anIt != aLast; anIt++) {
793     AttributePtr anAttribute = anIt->first;
794
795     /// not found in references
796     if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
797       continue;
798     std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
799     std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
800                                             aRLast = aRefAttributes.end();
801
802     AttributePtr aNewAttribute = anIt->second;
803     if (aNewAttribute.get()) {
804       for (; aRefIt != aRLast; aRefIt++) {
805         AttributeRefAttrPtr aRefAttr =
806                         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
807         if (aRefAttr.get()) {
808             aRefAttr->setAttr(aNewAttribute);
809         }
810       }
811     }
812   }
813 }
814
815 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
816                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
817 {
818   /// not found in references
819   if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
820     return;
821
822   std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
823   std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
824                                           aRLast = aRefAttributes.end();
825
826   std::set<FeaturePtr> aFeaturesToDelete;
827   for (; aRefIt != aRLast; aRefIt++) {
828     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
829     if (aRefAttr.get()) {
830       aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
831     }
832   }
833
834 #ifdef DEBUG_TRIM
835   // delete constraints
836   if (aFeaturesToDelete.size() > 0) {
837     std::cout << "removeReferencesToAttribute: " << std::endl;
838     std::string aValue;
839     for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
840          anIt != aFeaturesToDelete.end(); anIt++) {
841       FeaturePtr aFeature = *anIt;
842       std::cout << aFeature->data()->name() << std::endl;
843     }
844   }
845 #endif
846   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
847   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
848 }
849
850 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
851 {
852   std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
853                                        aLast = theFeaturesToUpdate.end();
854   for (; anIt != aLast; anIt++) {
855     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
856     std::string aRefFeatureKind = aRefFeature->getKind();
857     if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
858       std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
859                               std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
860       if (aLenghtFeature.get()) {
861         std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
862             ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
863         double aValue;
864         if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
865           aValueAttr->setValue(aValue);
866       }
867     }
868   }
869 }
870
871 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
872                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
873                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
874                   std::set<AttributePoint2DPtr>& thePoints,
875                   std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
876 {
877   FeaturePtr anNewFeature;
878
879   // Check the base objects are initialized.
880   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
881                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
882   ObjectPtr aBaseObject = aBaseObjectAttr->value();
883   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
884
885   /// points of trim
886   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
887   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
888
889   std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
890   std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
891
892   std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
893   std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
894   arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
895                       aStartShapePoint, aLastShapePoint);
896 #ifdef DEBUG_TRIM
897   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
898   if (aStartShapePoint.get())
899     std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
900                                        aStartShapePoint->y() << "]" << std::endl;
901   std::cout << "1st point:   [" << aStartFeaturePoint->x() << ", " <<
902                                    aStartFeaturePoint->y() << "]" << std::endl;
903   if (aLastShapePoint.get())
904     std::cout << "2st point:   [" << aLastShapePoint->x() << ", " <<
905                                      aLastShapePoint->y() << "]" << std::endl;
906   std::cout << "End point:   [" << aLastFeaturePoint->x() << ", " <<
907                                    aLastFeaturePoint->y() << "]" << std::endl;
908 #endif
909
910   bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
911   bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
912   if (isStartPoint || isLastPoint) {
913     // result is one line: changed existing line
914     std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
915                                                   : SketchPlugin_Line::END_ID();
916     std::shared_ptr<GeomAPI_Pnt2d> aPoint;
917     if (aStartShapePoint.get() && aLastShapePoint.get())
918       aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
919     else
920       aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
921
922     // it is important to delete references before the feature modification because
923     // if deletion will be after the feature modification, solver returns the feature back
924     removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
925                                 theBaseRefAttributes);
926
927     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
928     //theModifiedAttributes.insert(
929     //  std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
930
931     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
932                                (aBaseFeature->attribute(aModifiedAttribute)));
933   }
934   else {
935     // result is two lines: start line point - start shape point,
936     // last shape point - last line point
937     // create second line
938     anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
939     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
940                                (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
941
942     std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
943     theModifiedAttributes.insert(
944       std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
945                                    anNewFeature->attribute(SketchPlugin_Line::END_ID())));
946
947     // modify base arc
948     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
949
950     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
951                                (aBaseFeature->attribute(aModifiedAttribute)));
952
953     // Collinear constraint for lines
954     createConstraintForObjects(SketchPlugin_ConstraintCollinear::ID(),
955                                getFeatureResult(aBaseFeature),
956                                getFeatureResult(anNewFeature));
957
958   }
959   return anNewFeature;
960 }
961
962 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
963                  const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
964                  std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
965                  std::set<AttributePoint2DPtr>& thePoints,
966                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
967 {
968   FeaturePtr anNewFeature;
969   // Check the base objects are initialized.
970   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
971                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
972   ObjectPtr aBaseObject = aBaseObjectAttr->value();
973   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
974
975   /// points of trim
976   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
977   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
978
979   std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
980   std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
981
982   std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
983   std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
984   arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
985                      aStartShapePoint, aLastShapePoint);
986 #ifdef DEBUG_TRIM
987   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
988   if (aStartShapePoint.get())
989     std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
990                                        aStartShapePoint->y() << "]" << std::endl;
991   std::cout << "Start arc attribute point:   [" << aStartArcPoint->x() << ", " <<
992                                    aStartArcPoint->y() << "]" << std::endl;
993   if (aLastShapePoint.get())
994     std::cout << "Last shape point:   [" << aLastShapePoint->x() << ", " <<
995                                      aLastShapePoint->y() << "]" << std::endl;
996   std::cout << "Last arc attribute point:   [" << aLastArcPoint->x() << ", " <<
997                                    aLastArcPoint->y() << "]" << std::endl;
998 #endif
999
1000   bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1001   bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1002   if (isStartPoint || isLastPoint) {
1003     // result is one arc: changed existing arc
1004     std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1005                                                   : SketchPlugin_Arc::END_ID();
1006     std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1007     if (aStartShapePoint.get() && aLastShapePoint.get())
1008       aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1009     else
1010       aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1011
1012     removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1013                                 theBaseRefAttributes);
1014
1015     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1016
1017     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1018                                (aBaseFeature->attribute(aModifiedAttribute)));
1019   }
1020   else {
1021     // result is two arcs: start arc point - start shape point, last shape point - last arc point
1022     // create second arc
1023     anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1024     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1025                                (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1026
1027     std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1028     theModifiedAttributes.insert(
1029       std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1030                                    anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1031
1032     // modify base arc
1033     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1034
1035     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1036                                (aBaseFeature->attribute(aModifiedAttribute)));
1037
1038     // equal Radius constraint for arcs
1039     createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1040                                getFeatureResult(aBaseFeature),
1041                                getFeatureResult(anNewFeature));
1042     // coincident centers constraint
1043     createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1044                      aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1045                      anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1046
1047 #ifdef DEBUG_TRIM
1048     std::cout << "Created arc on points:" << std::endl;
1049     std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1050                                            aStartShapePoint->y() << "]" << std::endl;
1051 #endif
1052   }
1053   return anNewFeature;
1054 }
1055
1056 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1057                                    const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1058                                    std::set<AttributePoint2DPtr>& thePoints,
1059                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1060 {
1061   // Check the base objects are initialized.
1062   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1063                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1064   ObjectPtr aBaseObject = aBaseObjectAttr->value();
1065   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1066
1067   /// points of trim
1068   //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1069   //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1070
1071   /// trim feature
1072   FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1073   // arc created by trim of circle is always correct, that means that it is not inversed
1074   anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1075
1076   theModifiedAttributes.insert(
1077     std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1078                    anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1079
1080   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1081                              (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1082   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1083                              (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1084
1085   return anNewFeature;
1086 }
1087
1088 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1089                                             const AttributePoint2DPtr& theEndPointAttr,
1090                                             std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1091                                             std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1092 {
1093   if (!theFirstPoint.get() || !theLastPoint.get())
1094     return;
1095
1096   // if first point is closer to last point, swap first and last values
1097   if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1098       theStartPointAttr->pnt()->distance(theLastPoint)) {
1099     std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1100     theFirstPoint = theLastPoint;
1101     theLastPoint = aTmpPoint;
1102   }
1103 }
1104
1105 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1106                                   const AttributePoint2DPtr& theStartPointAttr,
1107                                   const AttributePoint2DPtr& theEndPointAttr,
1108                                   std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1109                                   std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1110 {
1111   if (!theFirstPoint.get() || !theSecondPoint.get())
1112     return;
1113
1114   static const double anAngleTol = 1.e-12;
1115
1116   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1117       theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1118   bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1119
1120   // collect directions to each point
1121   std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1122       new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1123   std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1124       new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1125   std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1126       new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1127
1128   // sort points by their angular values
1129   double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1130   double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1131   double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1132   if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1133     aFirstPtAngle += aPeriod;
1134   if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1135     aSecondPtAngle += aPeriod;
1136
1137   if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1138     std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1139     theFirstPoint = theSecondPoint;
1140     theSecondPoint = aTmpPoint;
1141   }
1142 }
1143
1144 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1145                                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
1146 {
1147   std::string anAttributeType = theModifiedAttribute->attributeType();
1148   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1149     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1150                                               theModifiedAttribute);
1151     aModifiedAttribute->setValue(thePoint);
1152
1153 #ifdef DEBUG_TRIM
1154     FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1155     std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1156       theModifiedAttribute->id() <<
1157       "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1158 #endif
1159   }
1160 }
1161
1162
1163 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1164                                       const AttributePtr& theSourceAttribute)
1165 {
1166   std::string anAttributeType = theModifiedAttribute->attributeType();
1167   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1168     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1169                                               theModifiedAttribute);
1170     AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1171                                               theSourceAttribute);
1172
1173     if (aModifiedAttribute.get() && aSourceAttribute.get())
1174       aModifiedAttribute->setValue(aSourceAttribute->pnt());
1175   }
1176   else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1177     AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1178                                               theModifiedAttribute);
1179     AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1180                                               theSourceAttribute);
1181
1182     if (aModifiedAttribute.get() && aSourceAttribute.get())
1183       aModifiedAttribute->setValue(aSourceAttribute->value());
1184   }
1185   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
1186     AttributeRefAttrPtr aRefAttributeToFill = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1187                                                                              theModifiedAttribute);
1188     AttributeRefAttrPtr aSourceRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1189                                          theSourceAttribute);
1190     if (!aSourceRefAttr.get())
1191       aRefAttributeToFill->setAttr(theSourceAttribute);
1192     else {
1193       if (aSourceRefAttr->isObject())
1194         aRefAttributeToFill->setObject(aSourceRefAttr->object());
1195       else
1196         aRefAttributeToFill->setAttr(aSourceRefAttr->attr());
1197     }
1198   }
1199 }
1200
1201 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1202                                         const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1203                                         const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1204 {
1205 #ifdef DEBUG_TRIM
1206   std::cout << "---- createLineFeature ---" << std::endl;
1207 #endif
1208
1209   FeaturePtr aFeature;
1210   SketchPlugin_Sketch* aSketch = sketch();
1211   if (!aSketch || !theBaseFeature.get())
1212     return aFeature;
1213
1214   aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1215
1216   fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1217   fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1218
1219   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1220                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1221
1222   aFeature->execute(); // to obtain result
1223
1224 #ifdef DEBUG_TRIM
1225   std::cout << "---- createLineFeature:end ---" << std::endl;
1226 #endif
1227
1228   return aFeature;
1229 }
1230
1231 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1232                                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1233                                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1234 {
1235   FeaturePtr aFeature;
1236   SketchPlugin_Sketch* aSketch = sketch();
1237   if (!aSketch || !theBaseFeature.get())
1238     return aFeature;
1239
1240   std::string aCenterAttributeId;
1241   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1242     aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1243   else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1244     aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1245
1246   if (aCenterAttributeId.empty())
1247     return aFeature;
1248
1249 #ifdef DEBUG_TRIM
1250   std::cout << "---- createArcFeature ---" << std::endl;
1251 #endif
1252
1253   aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1254   // update fillet arc: make the arc correct for sure, so, it is not needed to process
1255   // the "attribute updated"
1256   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1257   bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1258
1259   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1260                 theBaseFeature->attribute(aCenterAttributeId));
1261   fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1262   fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1263
1264   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1265                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1266
1267   /// fill referersed state of created arc as it is on the base arc
1268   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1269     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1270     aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1271   }
1272   aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1273   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1274
1275   #ifdef DEBUG_TRIM
1276   std::cout << "---- createArcFeature:end ---" << std::endl;
1277   #endif
1278
1279   return aFeature;
1280 }
1281
1282 FeaturePtr SketchPlugin_Trim::createConstraint(const std::string& theConstraintId,
1283                                                const AttributePtr& theFirstAttribute,
1284                                                const AttributePtr& theSecondAttribute)
1285 {
1286   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1287   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1288                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1289   aRefAttr->setAttr(theFirstAttribute);
1290
1291   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1292                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1293   aRefAttr->setAttr(theSecondAttribute);
1294
1295 #ifdef DEBUG_TRIM
1296   std::cout << "<createConstraint to attribute> :"
1297             << "first attribute - " << theFirstAttribute->id()
1298             << "second attribute - " << theSecondAttribute->id()
1299             << std::endl;
1300 #endif
1301
1302   return aConstraint;
1303 }
1304
1305 FeaturePtr SketchPlugin_Trim::createConstraintToObject(const std::string& theConstraintId,
1306                                                const AttributePtr& theFirstAttribute,
1307                                                const ObjectPtr& theSecondObject)
1308 {
1309   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1310   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1311                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1312   aRefAttr->setAttr(theFirstAttribute);
1313
1314   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1315                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1316   aRefAttr->setObject(theSecondObject);
1317
1318 #ifdef DEBUG_TRIM
1319   std::cout << "<createConstraint to attribute> :"
1320             << "first attribute - " << theFirstAttribute->id()
1321             << "second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
1322             << std::endl;
1323 #endif
1324
1325   return aConstraint;
1326 }
1327
1328 FeaturePtr SketchPlugin_Trim::createConstraintForObjects(
1329                                                     const std::string& theConstraintId,
1330                                                     const ObjectPtr& theFirstObject,
1331                                                     const ObjectPtr& theSecondObject)
1332 {
1333   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1334   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1335                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1336   aRefAttr->setObject(theFirstObject);
1337
1338   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1339                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1340   aRefAttr->setObject(theSecondObject);
1341
1342   return aConstraint;
1343 }
1344
1345 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1346                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
1347 {
1348   std::shared_ptr<ModelAPI_Result> aResult;
1349
1350   std::string aFeatureKind = theFeature->getKind();
1351   if (aFeatureKind == SketchPlugin_Line::ID())
1352     aResult = theFeature->firstResult();
1353   else if (aFeatureKind == SketchPlugin_Arc::ID())
1354     aResult = theFeature->lastResult();
1355   else if (aFeatureKind == SketchPlugin_Circle::ID())
1356     aResult = theFeature->lastResult();
1357
1358   return aResult;
1359 }
1360
1361 //********************************************************************
1362 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1363                 const ObjectPtr& theSketch,
1364                 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1365                 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1366 {
1367   PointToRefsMap aPointsInfo;
1368
1369   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1370   std::map<std::shared_ptr<GeomAPI_Pnt>,
1371                            std::list< AttributePoint2DPtr > > aPointToAttributes;
1372   std::map<std::shared_ptr<GeomAPI_Pnt>,
1373                            std::list< ObjectPtr > > aPointToObjects;
1374
1375   std::set<AttributePoint2DPtr > aRefAttributes;
1376   // current feature
1377   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1378   std::set<ResultPtr> anEdgeShapes;
1379   // edges on feature
1380   ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1381   if (!anEdgeShapes.empty()) {
1382     GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1383
1384     // coincidences to the feature
1385     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1386                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1387     // layed on feature coincidences to divide it on several shapes
1388     std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1389     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1390         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1391     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1392         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1393     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1394         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1395     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1396
1397     ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1398                                                 aX->dir(), aY, aPointsInfo);
1399
1400     std::list<FeaturePtr> aFeatures;
1401     CompositeFeaturePtr aSketchComposite =
1402                          std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1403     for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1404       FeaturePtr aFeature = aSketchComposite->subFeature(i);
1405       if (aFeature.get())
1406         aFeatures.push_back(aFeature);
1407     }
1408     ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1409
1410     GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1411   }
1412   theObjectToPoints[theObject] = aPointsInfo;
1413   theCashedShapes[theObject] = aShapes;
1414 }