]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Trim.cpp
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     std::shared_ptr<GeomAPI_Pnt2d> aPoint2D = sketch()->to2D(aPoint);
349     for (std::list<ObjectPtr>::const_iterator anObjectIt = anObjects.begin();
350       anObjectIt != anObjects.end(); anObjectIt++) {
351       ObjectPtr anObject = *anObjectIt;
352       // find an attribute on the point feature and if it is, append it into attribute list
353       // the case when the feature just touch the source feature, not intersect
354       FeaturePtr aRefFeature = ModelAPI_Feature::feature(anObject);
355       std::list<std::shared_ptr<ModelAPI_Attribute> > anAttributes =
356                               aRefFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
357       bool aFoundAttribute = false;
358       for (std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator
359            aPntAttrIt = anAttributes.begin(); aPntAttrIt != anAttributes.end(); aPntAttrIt++) {
360         std::shared_ptr<GeomDataAPI_Point2D> anAttrPoint =
361                                  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*aPntAttrIt);
362         if (anAttrPoint->pnt()->isEqual(aPoint2D)) {
363           createConstraint(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute, anAttrPoint);
364           aFoundAttribute = true;
365           break;
366         }
367       }
368       if (!aFoundAttribute)
369         createConstraintToObject(SketchPlugin_ConstraintCoincidence::ID(), aPointAttribute,
370                                  anObject);
371     }
372   }
373
374   // move constraints from base feature to replacing feature: ignore coincidences to feature
375   // if attributes of coincidence participated in split
376   ResultPtr aReplacingResult;
377   if (aReplacingFeature.get()) {
378     aReplacingFeature->execute(); // need it to obtain result
379     aReplacingResult = getFeatureResult(aReplacingFeature);
380   }
381   for(std::list<AttributePtr>::const_iterator anIt = aRefsToFeature.begin(),
382                                           aLast = aRefsToFeature.end();
383       anIt != aLast; anIt++) {
384     AttributePtr anAttribute = *anIt;
385
386     if (setCoincidenceToAttribute(anAttribute, aFurtherCoincidences))
387       continue;
388
389     // move tangency constraint to the nearest feature if possible
390     if (aNewFeature.get() && moveTangency(anAttribute, aNewFeature))
391       continue;
392
393     if (aReplacingResult.get()) {
394       AttributeRefAttrPtr aRefAttr =
395           std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
396       if (aRefAttr.get())
397         aRefAttr->setObject(aReplacingResult);
398       else {
399         AttributeReferencePtr aReferenceAttr =
400                              std::dynamic_pointer_cast<ModelAPI_AttributeReference>(anAttribute);
401         if (aReferenceAttr.get())
402           aReferenceAttr->setObject(aReplacingResult);
403       }
404     }
405   }
406
407   updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes, aFeaturesToDelete);
408
409   // Wait all constraints being created, then send update events
410   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
411   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
412   if (isUpdateFlushed)
413     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
414
415   // delete constraints
416 #ifdef DEBUG_TRIM
417   if (aFeaturesToDelete.size() > 0) {
418     std::cout << "after SPlit: removeFeaturesAndReferences: " << std::endl;
419     std::string aValue;
420     for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
421          anIt != aFeaturesToDelete.end(); anIt++) {
422       FeaturePtr aFeature = *anIt;
423       std::cout << aFeature->data()->name() << std::endl;
424     }
425   }
426 #endif
427   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
428   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
429
430   updateFeaturesAfterTrim(aFeaturesToUpdate);
431
432   // Send events to update the sub-features by the solver.
433   if(isUpdateFlushed) {
434     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
435   }
436
437   if (anIsEqualPreviewAndSelected) {
438     // equal preview and selected objects
439     // nothing to do if the preview and selected objects are different
440     if (aReplacingResult.get()) { // base object was removed
441       aPreviewObject = aReplacingResult;
442       //aMessage->setSelectedObject(aReplacingResult);
443 #ifdef DEBUG_TRIM_METHODS
444       if (!aSelectedShape.get())
445         std::cout << "Set empty selected object" << std::endl;
446       else
447         std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
448 #endif
449     }
450     else {
451       aPreviewObject = ObjectPtr();
452
453       aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
454       aBaseObject = getFeatureResult(aBaseFeature);
455       std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
456                                                                 aPreviewPnt2d->y());
457       ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
458       if (aBaseResult) {
459         GeomShapePtr aShape = aBaseResult->shape();
460         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
461         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
462           aPreviewObject = aBaseResult;
463       }
464       if (!aPreviewObject.get() && aNewFeature.get()) {
465         ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
466         if (aNewFeatureResult.get()) {
467           GeomShapePtr aShape = aNewFeatureResult->shape();
468           std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
469           if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
470             aPreviewObject = aNewFeatureResult;
471         }
472       }
473     }
474   }
475   if (aPreviewObject.get()) {
476     std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
477       <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
478                                            ModelAPI_EventReentrantMessage::eventId(), this));
479     aMessage->setSelectedObject(aPreviewObject);
480     Events_Loop::loop()->send(aMessage);
481   }
482 #ifdef DEBUG_TRIM
483   std::cout << "SketchPlugin_Trim::done" << std::endl;
484 #endif
485 }
486
487 std::string SketchPlugin_Trim::processEvent(const std::shared_ptr<Events_Message>& theMessage)
488 {
489 #ifdef DEBUG_TRIM_METHODS
490   std::cout << "SketchPlugin_Trim::processEvent:" << data()->name() << std::endl;
491 #endif
492   std::string aFilledAttributeName;
493
494   std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
495         std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
496   if (aMessage.get()) {
497     ObjectPtr anObject = aMessage->selectedObject();
498     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
499
500     if (anObject.get() && aPoint.get()) {
501       if (myCashedShapes.find(anObject) == myCashedShapes.end())
502         fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
503       const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
504       if (aShapes.size() > 1) {
505         std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
506                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
507                               data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
508         std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
509                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
510                               data()->attribute(SketchPlugin_Trim::PREVIEW_OBJECT()));
511         aRefSelectedAttr->setValue(anObject);
512         aRefPreviewAttr->setValue(anObject);
513
514         std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
515                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
516                               data()->attribute(SketchPlugin_Trim::SELECTED_POINT()));
517         std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
518                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
519                               data()->attribute(SketchPlugin_Trim::PREVIEW_POINT()));
520         aPointSelectedAttr->setValue(aPoint);
521         aPointPreviewAttr->setValue(aPoint);
522
523         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
524
525         GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
526   #ifdef DEBUG_TRIM_METHODS
527         if (!aSelectedShape.get())
528           std::cout << "Set empty selected object" << std::endl;
529         else
530           std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
531   #endif
532         aFilledAttributeName = SketchPlugin_Trim::SELECTED_OBJECT();
533       }
534     }
535   }
536   return aFilledAttributeName;
537 }
538
539 bool SketchPlugin_Trim::setCoincidenceToAttribute(const AttributePtr& theAttribute,
540                                 const std::set<AttributePoint2DPtr>& theFurtherCoincidences)
541 {
542   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
543   if (aFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
544     return false;
545
546   AttributePoint2DPtr aRefPointAttr = SketchPlugin_ConstraintCoincidence::getPoint(aFeature);
547   if (!aRefPointAttr.get())
548     return false;
549   std::shared_ptr<GeomAPI_Pnt2d> aRefPnt2d = aRefPointAttr->pnt();
550
551   std::set<AttributePoint2DPtr>::const_iterator anIt = theFurtherCoincidences.begin(),
552                                                 aLast = theFurtherCoincidences.end();
553   bool aFoundPoint = false;
554   for (; anIt != aLast && !aFoundPoint; anIt++) {
555     AttributePoint2DPtr aPointAttribute = (*anIt);
556     std::shared_ptr<GeomAPI_Pnt2d> aPoint2d = aPointAttribute->pnt();
557     if (aPoint2d->isEqual(aRefPnt2d)) {
558       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
559                                                                            theAttribute);
560       if (aRefAttr.get()) {
561         aRefAttr->setAttr(aPointAttribute);
562         aFoundPoint = true;
563       }
564     }
565   }
566   return aFoundPoint;
567 }
568
569 bool SketchPlugin_Trim::moveTangency(const AttributePtr& theAttribute,
570                                      const FeaturePtr& theFeature)
571 {
572   FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner());
573   if (aFeature->getKind() != SketchPlugin_ConstraintTangent::ID())
574     return false;
575
576   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
577                                                                            theAttribute);
578   if (!aRefAttr.get())
579     return false;
580
581   // get shape of tangent object to the current
582   std::string aTangentAttr = SketchPlugin_Constraint::ENTITY_A();
583   if (aRefAttr->id() == SketchPlugin_Constraint::ENTITY_A())
584     aTangentAttr = SketchPlugin_Constraint::ENTITY_B();
585   AttributeRefAttrPtr aTangentRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
586                                                      aFeature->attribute(aTangentAttr));
587   FeaturePtr aTangentFeature = ModelAPI_Feature::feature(aTangentRefAttr->object());
588
589   // get shape of the feature of the attribute
590   FeaturePtr anAttributeFeature = ModelAPI_Feature::feature(aRefAttr->object());
591   anAttributeFeature->execute(); // the modified value should be applyed to recompute shape
592   PointToRefsMap aPointToAttributeOrObject;
593   std::list<FeaturePtr> aFeatures;
594   aFeatures.push_back(anAttributeFeature);
595   ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
596                                                    aPointToAttributeOrObject);
597   if (!aPointToAttributeOrObject.empty())
598     return true; // the attribute feature has a point of intersection, so we do not replace it
599
600   // get shape of the feature
601   aPointToAttributeOrObject.clear();
602   aFeatures.clear();
603   aFeatures.push_back(theFeature);
604   ModelGeomAlgo_Point2D::getPointsIntersectedShape(aTangentFeature, aFeatures,
605                                                    aPointToAttributeOrObject);
606   if (!aPointToAttributeOrObject.empty()) {
607     std::set<ResultPtr> anEdgeShapes;
608     ModelGeomAlgo_Shape::shapesOfType(theFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
609     if (!anEdgeShapes.empty()) {
610       ResultPtr aResult = *anEdgeShapes.begin();
611       if (aResult.get()) {
612         aRefAttr->setObject(aResult);
613         return true; // the attribute feature has a point of intersection, so we do not replace it
614       }
615     }
616   }
617   return false;
618 }
619
620 AISObjectPtr SketchPlugin_Trim::getAISObject(AISObjectPtr thePrevious)
621 {
622 #ifdef DEBUG_TRIM_METHODS
623   std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
624 #endif
625
626   AISObjectPtr anAIS = thePrevious;
627
628   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
629   GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
630   if (aPreviewShape.get())
631     aShapes.push_back(aPreviewShape);
632   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
633   if (aSelectedShape.get())
634     aShapes.push_back(aSelectedShape);
635
636   if (aShapes.empty())
637     return AISObjectPtr();
638
639   GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
640   if (!aBaseShape.get())
641     return AISObjectPtr();
642
643   if (aBaseShape.get()) {
644     if (!anAIS)
645       anAIS = AISObjectPtr(new GeomAPI_AISObject);
646     anAIS->createShape(aBaseShape);
647
648     std::vector<int> aColor;
649     aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
650     double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
651     int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
652     anAIS->setColor(aColor[0], aColor[1], aColor[2]);
653     // width when there is not base object should be extened in several points
654     // in order to see this preview over highlight
655     anAIS->setWidth(aWidth+4);
656     anAIS->setLineStyle(aLineStyle);
657   }
658   else
659     anAIS = AISObjectPtr();
660
661   return anAIS;
662 }
663
664 GeomShapePtr SketchPlugin_Trim::getSubShape(const std::string& theObjectAttributeId,
665                                             const std::string& thePointAttributeId)
666 {
667   GeomShapePtr aBaseShape;
668
669   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
670                                                        data()->attribute(theObjectAttributeId));
671   ObjectPtr aBaseObject = anObjectAttr->value();
672   if (!aBaseObject.get())
673     return aBaseShape;
674
675   // point on feature
676   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
677                                            data()->attribute(thePointAttributeId));
678   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
679   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
680                                                                anAttributePnt2d->y());
681
682   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
683     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
684
685   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
686   if (!aShapes.empty()) {
687     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
688     for (; anIt != aLast; anIt++) {
689       GeomShapePtr aShape = *anIt;
690       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
691       if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
692         aBaseShape = aShape;
693     }
694   }
695   return aBaseShape;
696 }
697
698 void SketchPlugin_Trim::getFeaturePoints(const FeaturePtr& theFeature,
699                                          AttributePoint2DPtr& theStartPointAttr,
700                                          AttributePoint2DPtr& theEndPointAttr)
701 {
702   std::string aFeatureKind = theFeature->getKind();
703   std::string aStartAttributeName, anEndAttributeName;
704   if (aFeatureKind == SketchPlugin_Line::ID()) {
705     aStartAttributeName = SketchPlugin_Line::START_ID();
706     anEndAttributeName = SketchPlugin_Line::END_ID();
707   }
708   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
709     aStartAttributeName = SketchPlugin_Arc::START_ID();
710     anEndAttributeName = SketchPlugin_Arc::END_ID();
711   }
712   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
713     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
714                                          theFeature->attribute(aStartAttributeName));
715     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
716                                          theFeature->attribute(anEndAttributeName));
717   }
718 }
719
720 void SketchPlugin_Trim::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
721                                        std::set<FeaturePtr>& theFeaturesToUpdate)
722 {
723   std::shared_ptr<ModelAPI_Data> aData = data();
724
725   // Check the base objects are initialized.
726   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
727                                          aData->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
728   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
729   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
730
731   std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
732   std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
733   aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
734
735   std::set<AttributePtr>::const_iterator aIt;
736   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
737     std::shared_ptr<ModelAPI_Attribute> anAttr = (*aIt);
738     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
739     std::string aRefFeatureKind = aRefFeature->getKind();
740     std::string anAttributeId = anAttr->id();
741     if ((aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() &&
742          anAttributeId == SketchPlugin_ConstraintMirror::MIRROR_LIST_ID()) ||
743         (aRefFeatureKind == SketchPlugin_MultiRotation::ID() &&
744          anAttributeId == SketchPlugin_MultiRotation::ROTATION_LIST_ID()) ||
745         (aRefFeatureKind == SketchPlugin_MultiTranslation::ID() &&
746          anAttributeId == SketchPlugin_MultiTranslation::TRANSLATION_LIST_ID()) ||
747         aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
748       theFeaturesToDelete.insert(aRefFeature);
749     else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
750       theFeaturesToUpdate.insert(aRefFeature);
751   }
752 }
753
754 void SketchPlugin_Trim::getRefAttributes(const FeaturePtr& theFeature,
755                                     std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
756                                     std::list<AttributePtr>& theRefsToFeature)
757 {
758   theRefs.clear();
759
760   std::list<AttributePtr> aPointAttributes =
761     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
762   std::set<AttributePtr> aPointAttributesSet;
763
764   std::list<AttributePtr>::const_iterator aPIt =
765     aPointAttributes.begin(), aPLast = aPointAttributes.end();
766   for (; aPIt != aPLast; aPIt++)
767     aPointAttributesSet.insert(*aPIt);
768
769   std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
770   std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
771   aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
772
773   std::set<AttributePtr>::const_iterator aIt;
774   for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
775     AttributePtr anAttr = (*aIt);
776     FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
777     if (anAttrFeature.get() != this &&
778         anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
779       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
780       if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
781         AttributePtr anAttrInRef = aRefAttr->attr();
782         if (anAttrInRef.get() &&
783             aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
784           if (theRefs.find(anAttrInRef) != theRefs.end())
785             theRefs[anAttrInRef].push_back(aRefAttr);
786           else {
787             std::list<AttributePtr> anAttrList;
788             anAttrList.push_back(aRefAttr);
789             theRefs[anAttrInRef] = anAttrList;
790           }
791         }
792       }
793       else { /// find attributes referenced to feature itself
794         theRefsToFeature.push_back(anAttr);
795       }
796     }
797   }
798 }
799
800 void SketchPlugin_Trim::updateRefAttConstraints(
801                     const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
802                     const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes,
803                     std::set<FeaturePtr>& theFeaturesToDelete)
804 {
805 #ifdef DEBUG_TRIM
806   std::cout << "SketchPlugin_Trim::updateRefAttConstraints" << std::endl;
807 #endif
808
809   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
810     anIt = theModifiedAttributes.begin(),  aLast = theModifiedAttributes.end();
811   for (; anIt != aLast; anIt++) {
812     AttributePtr anAttribute = anIt->first;
813
814     /// not found in references
815     if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
816       continue;
817     std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
818     std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
819                                             aRLast = aRefAttributes.end();
820
821     AttributePtr aNewAttribute = anIt->second;
822     if (aNewAttribute.get()) {
823       for (; aRefIt != aRLast; aRefIt++) {
824         AttributeRefAttrPtr aRefAttr =
825                         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
826         if (aRefAttr.get()) {
827             aRefAttr->setAttr(aNewAttribute);
828         }
829       }
830     }
831   }
832 }
833
834 void SketchPlugin_Trim::removeReferencesToAttribute(const AttributePtr& theAttribute,
835                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes)
836 {
837   /// not found in references
838   if (theBaseRefAttributes.find(theAttribute) == theBaseRefAttributes.end())
839     return;
840
841   std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(theAttribute);
842   std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
843                                           aRLast = aRefAttributes.end();
844
845   std::set<FeaturePtr> aFeaturesToDelete;
846   for (; aRefIt != aRLast; aRefIt++) {
847     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
848     if (aRefAttr.get()) {
849       aFeaturesToDelete.insert(ModelAPI_Feature::feature(aRefAttr->owner()));
850     }
851   }
852
853 #ifdef DEBUG_TRIM
854   // delete constraints
855   if (aFeaturesToDelete.size() > 0) {
856     std::cout << "removeReferencesToAttribute: " << std::endl;
857     std::string aValue;
858     for (std::set<FeaturePtr>::const_iterator anIt = aFeaturesToDelete.begin();
859          anIt != aFeaturesToDelete.end(); anIt++) {
860       FeaturePtr aFeature = *anIt;
861       std::cout << aFeature->data()->name() << std::endl;
862     }
863   }
864 #endif
865   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
866   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
867 }
868
869 void SketchPlugin_Trim::updateFeaturesAfterTrim(const std::set<FeaturePtr>& theFeaturesToUpdate)
870 {
871   std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
872                                        aLast = theFeaturesToUpdate.end();
873   for (; anIt != aLast; anIt++) {
874     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
875     std::string aRefFeatureKind = aRefFeature->getKind();
876     if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
877       std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
878                               std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
879       if (aLenghtFeature.get()) {
880         std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
881             ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
882         double aValue;
883         if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
884           aValueAttr->setValue(aValue);
885       }
886     }
887   }
888 }
889
890 FeaturePtr SketchPlugin_Trim::trimLine(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
891                   const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
892                   std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
893                   std::set<AttributePoint2DPtr>& thePoints,
894                   std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
895 {
896   FeaturePtr anNewFeature;
897
898   // Check the base objects are initialized.
899   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
900                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
901   ObjectPtr aBaseObject = aBaseObjectAttr->value();
902   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
903
904   /// points of trim
905   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
906   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
907
908   std::shared_ptr<GeomAPI_Pnt2d> aStartFeaturePoint = aStartPointAttrOfBase->pnt();
909   std::shared_ptr<GeomAPI_Pnt2d> aLastFeaturePoint = anEndPointAttrOfBase->pnt();
910
911   std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
912   std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
913   arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
914                       aStartShapePoint, aLastShapePoint);
915 #ifdef DEBUG_TRIM
916   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
917   if (aStartShapePoint.get())
918     std::cout << "Start point: [" << aStartShapePoint->x() << ", " <<
919                                        aStartShapePoint->y() << "]" << std::endl;
920   std::cout << "1st point:   [" << aStartFeaturePoint->x() << ", " <<
921                                    aStartFeaturePoint->y() << "]" << std::endl;
922   if (aLastShapePoint.get())
923     std::cout << "2st point:   [" << aLastShapePoint->x() << ", " <<
924                                      aLastShapePoint->y() << "]" << std::endl;
925   std::cout << "End point:   [" << aLastFeaturePoint->x() << ", " <<
926                                    aLastFeaturePoint->y() << "]" << std::endl;
927 #endif
928
929   bool isStartPoint = !aStartShapePoint.get() || aStartFeaturePoint->isEqual(aStartShapePoint);
930   bool isLastPoint = !aLastShapePoint.get() || aLastFeaturePoint->isEqual(aLastShapePoint);
931   if (isStartPoint || isLastPoint) {
932     // result is one line: changed existing line
933     std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Line::START_ID()
934                                                   : SketchPlugin_Line::END_ID();
935     std::shared_ptr<GeomAPI_Pnt2d> aPoint;
936     if (aStartShapePoint.get() && aLastShapePoint.get())
937       aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
938     else
939       aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
940
941     // it is important to delete references before the feature modification because
942     // if deletion will be after the feature modification, solver returns the feature back
943     removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
944                                 theBaseRefAttributes);
945
946     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
947     //theModifiedAttributes.insert(
948     //  std::make_pair(aBaseFeature->attribute(aModifiedAttribute), AttributePtr()));
949
950     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
951                                (aBaseFeature->attribute(aModifiedAttribute)));
952   }
953   else {
954     // result is two lines: start line point - start shape point,
955     // last shape point - last line point
956     // create second line
957     anNewFeature = createLineFeature(aBaseFeature, aLastShapePoint, aLastFeaturePoint);
958     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
959                                (anNewFeature->attribute(SketchPlugin_Line::START_ID())));
960
961     std::string aModifiedAttribute = SketchPlugin_Line::END_ID();
962     theModifiedAttributes.insert(
963       std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
964                                    anNewFeature->attribute(SketchPlugin_Line::END_ID())));
965
966     // modify base arc
967     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
968
969     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
970                                (aBaseFeature->attribute(aModifiedAttribute)));
971
972     // Collinear constraint for lines
973     createConstraintForObjects(SketchPlugin_ConstraintCollinear::ID(),
974                                getFeatureResult(aBaseFeature),
975                                getFeatureResult(anNewFeature));
976
977   }
978   return anNewFeature;
979 }
980
981 FeaturePtr SketchPlugin_Trim::trimArc(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
982                  const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
983                  std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
984                  std::set<AttributePoint2DPtr>& thePoints,
985                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
986 {
987   FeaturePtr anNewFeature;
988   // Check the base objects are initialized.
989   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
990                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
991   ObjectPtr aBaseObject = aBaseObjectAttr->value();
992   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
993
994   /// points of trim
995   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
996   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
997
998   std::shared_ptr<GeomAPI_Pnt2d> aStartArcPoint = aStartPointAttrOfBase->pnt();
999   std::shared_ptr<GeomAPI_Pnt2d> aLastArcPoint = anEndPointAttrOfBase->pnt();
1000
1001   std::shared_ptr<GeomAPI_Pnt2d> aStartShapePoint = theStartShapePoint;
1002   std::shared_ptr<GeomAPI_Pnt2d> aLastShapePoint = theLastShapePoint;
1003   arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1004                      aStartShapePoint, aLastShapePoint);
1005 #ifdef DEBUG_TRIM
1006   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1007   if (aStartShapePoint.get())
1008     std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1009                                        aStartShapePoint->y() << "]" << std::endl;
1010   std::cout << "Start arc attribute point:   [" << aStartArcPoint->x() << ", " <<
1011                                    aStartArcPoint->y() << "]" << std::endl;
1012   if (aLastShapePoint.get())
1013     std::cout << "Last shape point:   [" << aLastShapePoint->x() << ", " <<
1014                                      aLastShapePoint->y() << "]" << std::endl;
1015   std::cout << "Last arc attribute point:   [" << aLastArcPoint->x() << ", " <<
1016                                    aLastArcPoint->y() << "]" << std::endl;
1017 #endif
1018
1019   bool isStartPoint = !aStartShapePoint.get() || aStartArcPoint->isEqual(aStartShapePoint);
1020   bool isLastPoint = !aLastShapePoint.get() || aLastArcPoint->isEqual(aLastShapePoint);
1021   if (isStartPoint || isLastPoint) {
1022     // result is one arc: changed existing arc
1023     std::string aModifiedAttribute = isStartPoint ? SketchPlugin_Arc::START_ID()
1024                                                   : SketchPlugin_Arc::END_ID();
1025     std::shared_ptr<GeomAPI_Pnt2d> aPoint;
1026     if (aStartShapePoint.get() && aLastShapePoint.get())
1027       aPoint = isStartPoint ? aLastShapePoint : aStartShapePoint;
1028     else
1029       aPoint = aStartShapePoint.get() ? aStartShapePoint : aLastShapePoint;
1030
1031     removeReferencesToAttribute(aBaseFeature->attribute(aModifiedAttribute),
1032                                 theBaseRefAttributes);
1033
1034     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aPoint);
1035
1036     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1037                                (aBaseFeature->attribute(aModifiedAttribute)));
1038   }
1039   else {
1040     // result is two arcs: start arc point - start shape point, last shape point - last arc point
1041     // create second arc
1042     anNewFeature = createArcFeature(aBaseFeature, aLastShapePoint, aLastArcPoint);
1043     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1044                                (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1045
1046     std::string aModifiedAttribute = SketchPlugin_Arc::END_ID();
1047     theModifiedAttributes.insert(
1048       std::make_pair(aBaseFeature->attribute(aModifiedAttribute),
1049                                    anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1050
1051     // modify base arc
1052     fillPointAttribute(aBaseFeature->attribute(aModifiedAttribute), aStartShapePoint);
1053
1054     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1055                                (aBaseFeature->attribute(aModifiedAttribute)));
1056
1057     // equal Radius constraint for arcs
1058     createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1059                                getFeatureResult(aBaseFeature),
1060                                getFeatureResult(anNewFeature));
1061     // coincident centers constraint
1062     createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1063                      aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1064                      anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
1065
1066 #ifdef DEBUG_TRIM
1067     std::cout << "Created arc on points:" << std::endl;
1068     std::cout << "Start shape point: [" << aStartShapePoint->x() << ", " <<
1069                                            aStartShapePoint->y() << "]" << std::endl;
1070 #endif
1071   }
1072   return anNewFeature;
1073 }
1074
1075 FeaturePtr SketchPlugin_Trim::trimCircle(const std::shared_ptr<GeomAPI_Pnt2d>& theStartShapePoint,
1076                                    const std::shared_ptr<GeomAPI_Pnt2d>& theLastShapePoint,
1077                                    std::set<AttributePoint2DPtr>& thePoints,
1078                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1079 {
1080   // Check the base objects are initialized.
1081   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1082                                         data()->attribute(SketchPlugin_Trim::SELECTED_OBJECT()));
1083   ObjectPtr aBaseObject = aBaseObjectAttr->value();
1084   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1085
1086   /// points of trim
1087   //AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1088   //getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1089
1090   /// trim feature
1091   FeaturePtr anNewFeature = createArcFeature(aBaseFeature, theStartShapePoint, theLastShapePoint);
1092   // arc created by trim of circle is always correct, that means that it is not inversed
1093   anNewFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(false);
1094
1095   theModifiedAttributes.insert(
1096     std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1097                    anNewFeature->attribute(SketchPlugin_Arc::CENTER_ID())));
1098
1099   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1100                              (anNewFeature->attribute(SketchPlugin_Arc::START_ID())));
1101   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1102                              (anNewFeature->attribute(SketchPlugin_Arc::END_ID())));
1103
1104   return anNewFeature;
1105 }
1106
1107 void SketchPlugin_Trim::arrangePointsOnLine(const AttributePoint2DPtr& theStartPointAttr,
1108                                             const AttributePoint2DPtr& theEndPointAttr,
1109                                             std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1110                                             std::shared_ptr<GeomAPI_Pnt2d>& theLastPoint) const
1111 {
1112   if (!theFirstPoint.get() || !theLastPoint.get())
1113     return;
1114
1115   // if first point is closer to last point, swap first and last values
1116   if (theStartPointAttr->pnt()->distance(theFirstPoint) >
1117       theStartPointAttr->pnt()->distance(theLastPoint)) {
1118     std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1119     theFirstPoint = theLastPoint;
1120     theLastPoint = aTmpPoint;
1121   }
1122 }
1123
1124 void SketchPlugin_Trim::arrangePointsOnArc(const FeaturePtr& theArc,
1125                                   const AttributePoint2DPtr& theStartPointAttr,
1126                                   const AttributePoint2DPtr& theEndPointAttr,
1127                                   std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1128                                   std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint) const
1129 {
1130   if (!theFirstPoint.get() || !theSecondPoint.get())
1131     return;
1132
1133   static const double anAngleTol = 1.e-12;
1134
1135   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1136       theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1137   bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1138
1139   // collect directions to each point
1140   std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1141       new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1142   std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1143       new GeomAPI_Dir2d(theFirstPoint->xy()->decreased(aCenter->xy())));
1144   std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1145       new GeomAPI_Dir2d(theSecondPoint->xy()->decreased(aCenter->xy())));
1146
1147   // sort points by their angular values
1148   double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1149   double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1150   double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1151   if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1152     aFirstPtAngle += aPeriod;
1153   if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1154     aSecondPtAngle += aPeriod;
1155
1156   if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1157     std::shared_ptr<GeomAPI_Pnt2d> aTmpPoint = theFirstPoint;
1158     theFirstPoint = theSecondPoint;
1159     theSecondPoint = aTmpPoint;
1160   }
1161 }
1162
1163 void SketchPlugin_Trim::fillPointAttribute(const AttributePtr& theModifiedAttribute,
1164                                            const std::shared_ptr<GeomAPI_Pnt2d>& thePoint)
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     aModifiedAttribute->setValue(thePoint);
1171
1172 #ifdef DEBUG_TRIM
1173     FeaturePtr aFeature = ModelAPI_Feature::feature(theModifiedAttribute->owner());
1174     std::cout << "<fillPointAttribute[" << aFeature->data()->name() << ": " <<
1175       theModifiedAttribute->id() <<
1176       "]> => Pnt2d - [" << thePoint->x() << ", " << thePoint->y() << "]" << std::endl;
1177 #endif
1178   }
1179 }
1180
1181
1182 void SketchPlugin_Trim::fillAttribute(const AttributePtr& theModifiedAttribute,
1183                                       const AttributePtr& theSourceAttribute)
1184 {
1185   std::string anAttributeType = theModifiedAttribute->attributeType();
1186   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1187     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1188                                               theModifiedAttribute);
1189     AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1190                                               theSourceAttribute);
1191
1192     if (aModifiedAttribute.get() && aSourceAttribute.get())
1193       aModifiedAttribute->setValue(aSourceAttribute->pnt());
1194   }
1195   else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1196     AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1197                                               theModifiedAttribute);
1198     AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1199                                               theSourceAttribute);
1200
1201     if (aModifiedAttribute.get() && aSourceAttribute.get())
1202       aModifiedAttribute->setValue(aSourceAttribute->value());
1203   }
1204   else if (anAttributeType == ModelAPI_AttributeRefAttr::typeId()) {
1205     AttributeRefAttrPtr aRefAttributeToFill = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1206                                                                              theModifiedAttribute);
1207     AttributeRefAttrPtr aSourceRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1208                                          theSourceAttribute);
1209     if (!aSourceRefAttr.get())
1210       aRefAttributeToFill->setAttr(theSourceAttribute);
1211     else {
1212       if (aSourceRefAttr->isObject())
1213         aRefAttributeToFill->setObject(aSourceRefAttr->object());
1214       else
1215         aRefAttributeToFill->setAttr(aSourceRefAttr->attr());
1216     }
1217   }
1218 }
1219
1220 FeaturePtr SketchPlugin_Trim::createLineFeature(const FeaturePtr& theBaseFeature,
1221                                         const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1222                                         const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1223 {
1224 #ifdef DEBUG_TRIM
1225   std::cout << "---- createLineFeature ---" << std::endl;
1226 #endif
1227
1228   FeaturePtr aFeature;
1229   SketchPlugin_Sketch* aSketch = sketch();
1230   if (!aSketch || !theBaseFeature.get())
1231     return aFeature;
1232
1233   aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1234
1235   fillPointAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPoint);
1236   fillPointAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPoint);
1237
1238   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1239                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1240
1241   aFeature->execute(); // to obtain result
1242
1243 #ifdef DEBUG_TRIM
1244   std::cout << "---- createLineFeature:end ---" << std::endl;
1245 #endif
1246
1247   return aFeature;
1248 }
1249
1250 FeaturePtr SketchPlugin_Trim::createArcFeature(const FeaturePtr& theBaseFeature,
1251                                                const std::shared_ptr<GeomAPI_Pnt2d>& theFirstPoint,
1252                                                const std::shared_ptr<GeomAPI_Pnt2d>& theSecondPoint)
1253 {
1254   FeaturePtr aFeature;
1255   SketchPlugin_Sketch* aSketch = sketch();
1256   if (!aSketch || !theBaseFeature.get())
1257     return aFeature;
1258
1259   std::string aCenterAttributeId;
1260   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1261     aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1262   else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1263     aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1264
1265   if (aCenterAttributeId.empty())
1266     return aFeature;
1267
1268 #ifdef DEBUG_TRIM
1269   std::cout << "---- createArcFeature ---" << std::endl;
1270 #endif
1271
1272   aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1273   // update fillet arc: make the arc correct for sure, so, it is not needed to process
1274   // the "attribute updated"
1275   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1276   bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1277
1278   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1279                 theBaseFeature->attribute(aCenterAttributeId));
1280   fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPoint);
1281   fillPointAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPoint);
1282
1283   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1284                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1285
1286   /// fill referersed state of created arc as it is on the base arc
1287   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1288     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1289     aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1290   }
1291   aFeature->execute(); // to obtain result (need to calculate arc parameters before sending Update)
1292   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1293
1294   #ifdef DEBUG_TRIM
1295   std::cout << "---- createArcFeature:end ---" << std::endl;
1296   #endif
1297
1298   return aFeature;
1299 }
1300
1301 FeaturePtr SketchPlugin_Trim::createConstraint(const std::string& theConstraintId,
1302                                                const AttributePtr& theFirstAttribute,
1303                                                const AttributePtr& theSecondAttribute)
1304 {
1305   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1306   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1307                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1308   aRefAttr->setAttr(theFirstAttribute);
1309
1310   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1311                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1312   aRefAttr->setAttr(theSecondAttribute);
1313
1314 #ifdef DEBUG_TRIM
1315   std::cout << "<createConstraint to attribute> :"
1316             << "first attribute - " << theFirstAttribute->id()
1317             << "second attribute - " << theSecondAttribute->id()
1318             << std::endl;
1319 #endif
1320
1321   return aConstraint;
1322 }
1323
1324 FeaturePtr SketchPlugin_Trim::createConstraintToObject(const std::string& theConstraintId,
1325                                                const AttributePtr& theFirstAttribute,
1326                                                const ObjectPtr& theSecondObject)
1327 {
1328   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1329   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1330                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1331   aRefAttr->setAttr(theFirstAttribute);
1332
1333   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1334                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1335   aRefAttr->setObject(theSecondObject);
1336
1337 #ifdef DEBUG_TRIM
1338   std::cout << "<createConstraint to attribute> :"
1339             << "first attribute - " << theFirstAttribute->id()
1340             << "second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
1341             << std::endl;
1342 #endif
1343
1344   return aConstraint;
1345 }
1346
1347 FeaturePtr SketchPlugin_Trim::createConstraintForObjects(
1348                                                     const std::string& theConstraintId,
1349                                                     const ObjectPtr& theFirstObject,
1350                                                     const ObjectPtr& theSecondObject)
1351 {
1352   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1353   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1354                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1355   aRefAttr->setObject(theFirstObject);
1356
1357   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1358                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1359   aRefAttr->setObject(theSecondObject);
1360
1361   return aConstraint;
1362 }
1363
1364 std::shared_ptr<ModelAPI_Result> SketchPlugin_Trim::getFeatureResult(
1365                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
1366 {
1367   std::shared_ptr<ModelAPI_Result> aResult;
1368
1369   std::string aFeatureKind = theFeature->getKind();
1370   if (aFeatureKind == SketchPlugin_Line::ID())
1371     aResult = theFeature->firstResult();
1372   else if (aFeatureKind == SketchPlugin_Arc::ID())
1373     aResult = theFeature->lastResult();
1374   else if (aFeatureKind == SketchPlugin_Circle::ID())
1375     aResult = theFeature->lastResult();
1376
1377   return aResult;
1378 }
1379
1380 //********************************************************************
1381 void SketchPlugin_Trim::fillObjectShapes(const ObjectPtr& theObject,
1382                 const ObjectPtr& theSketch,
1383                 std::map<ObjectPtr, std::set<GeomShapePtr> >& theCashedShapes,
1384                 std::map<ObjectPtr, PointToRefsMap>& theObjectToPoints)
1385 {
1386   PointToRefsMap aPointsInfo;
1387
1388   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
1389   std::map<std::shared_ptr<GeomAPI_Pnt>,
1390                            std::list< AttributePoint2DPtr > > aPointToAttributes;
1391   std::map<std::shared_ptr<GeomAPI_Pnt>,
1392                            std::list< ObjectPtr > > aPointToObjects;
1393
1394   std::set<AttributePoint2DPtr > aRefAttributes;
1395   // current feature
1396   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
1397   std::set<ResultPtr> anEdgeShapes;
1398   // edges on feature
1399   ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeShapes);
1400   if (!anEdgeShapes.empty()) {
1401     GeomShapePtr aFeatureShape = (*anEdgeShapes.begin())->shape();
1402
1403     // coincidences to the feature
1404     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
1405                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
1406     // layed on feature coincidences to divide it on several shapes
1407     std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
1408     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
1409         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
1410     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1411         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
1412     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
1413         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
1414     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
1415
1416     ModelGeomAlgo_Point2D::getPointsInsideShape(aFeatureShape, aRefAttributes, aC->pnt(),
1417                                                 aX->dir(), aY, aPointsInfo);
1418
1419     std::list<FeaturePtr> aFeatures;
1420     CompositeFeaturePtr aSketchComposite =
1421                          std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theSketch);
1422     for (int i = 0; i < aSketchComposite->numberOfSubs(); i++) {
1423       FeaturePtr aFeature = aSketchComposite->subFeature(i);
1424       if (aFeature.get())
1425         aFeatures.push_back(aFeature);
1426     }
1427     ModelGeomAlgo_Point2D::getPointsIntersectedShape(aFeature, aFeatures, aPointsInfo);
1428
1429     GeomAlgoAPI_ShapeTools::splitShape(aFeatureShape, aPointsInfo, aShapes);
1430   }
1431   theObjectToPoints[theObject] = aPointsInfo;
1432   theCashedShapes[theObject] = aShapes;
1433 }