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