Salome HOME
Task #3005 : To be able to create a group on a whole result
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Split.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "SketchPlugin_Split.h"
21
22 #include <Events_Message.h>
23
24 #include <GeomAPI_Dir2d.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Pnt2d.h>
27 #include <GeomAPI_XY.h>
28 #include <GeomDataAPI_Point2D.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30 #include <GeomAlgoAPI_CompoundBuilder.h>
31
32 #include <ModelAPI_AttributeBoolean.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_AttributeRefAttr.h>
35 #include <ModelAPI_AttributeReference.h>
36 #include <ModelAPI_AttributeString.h>
37 #include <ModelAPI_Events.h>
38 #include <ModelAPI_Validator.h>
39 #include <ModelAPI_Session.h>
40 #include <ModelAPI_Tools.h>
41
42 #include <ModelGeomAlgo_Shape.h>
43
44 #include <SketchPlugin_Arc.h>
45 #include <SketchPlugin_Circle.h>
46 #include <SketchPlugin_ConstraintCoincidence.h>
47 #include <SketchPlugin_ConstraintEqual.h>
48 #include <SketchPlugin_ConstraintLength.h>
49 #include <SketchPlugin_ConstraintMiddle.h>
50 #include <SketchPlugin_ConstraintMirror.h>
51 #include <SketchPlugin_ConstraintParallel.h>
52 #include <SketchPlugin_ConstraintTangent.h>
53 #include <SketchPlugin_Line.h>
54 #include <SketchPlugin_MultiRotation.h>
55 #include <SketchPlugin_MultiTranslation.h>
56 #include <SketchPlugin_Point.h>
57 #include <SketchPlugin_Tools.h>
58
59 #include <ModelGeomAlgo_Point2D.h>
60 #include <ModelAPI_EventReentrantMessage.h>
61 #include <Events_Loop.h>
62
63 #include <cmath>
64
65 //#define CREATE_CONSTRAINTS
66
67 #ifdef DEBUG_SPLIT
68 #include <iostream>
69 #endif
70
71 static const double PI = 3.141592653589793238463;
72
73 SketchPlugin_Split::SketchPlugin_Split()
74 {
75 }
76
77 void SketchPlugin_Split::initAttributes()
78 {
79   data()->addAttribute(SELECTED_OBJECT(), ModelAPI_AttributeReference::typeId());
80   data()->addAttribute(SELECTED_POINT(), GeomDataAPI_Point2D::typeId());
81
82   data()->addAttribute(PREVIEW_POINT(), GeomDataAPI_Point2D::typeId());
83   data()->addAttribute(PREVIEW_OBJECT(), ModelAPI_AttributeReference::typeId());
84
85   data()->attribute(PREVIEW_POINT())->setIsArgument(false);
86   data()->attribute(SELECTED_POINT())->setIsArgument(false);
87   data()->attribute(PREVIEW_OBJECT())->setIsArgument(false);
88
89   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_POINT());
90   ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), PREVIEW_OBJECT());
91
92   // TODO: remove
93   //data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
94   //data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
95   //data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
96 }
97
98 void SketchPlugin_Split::execute()
99 {
100   std::shared_ptr<ModelAPI_Data> aData = data();
101
102   // Check the base objects are initialized.
103   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
104                                                            data()->attribute(SELECTED_OBJECT()));
105   //ObjectPtr aBaseObject = anObjectAttr->value();
106   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
107   //                                          aData->attribute(SketchPlugin_Constraint::VALUE()));
108   if(!aBaseObjectAttr->isInitialized()) {
109     setError("Error: Base object is not initialized.");
110     return;
111   }
112   ObjectPtr aBaseObject = aBaseObjectAttr->value();
113   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
114   //  getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
115   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
116   //  getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
117   if (!aFirstPointAttrOfSplit.get() || !aFirstPointAttrOfSplit->isInitialized() ||
118       !aSecondPointAttrOfSplit.get() || !aSecondPointAttrOfSplit->isInitialized()) {
119     setError("Error: Sub-shape is not initialized.");
120     return;
121   }
122
123   /// Remove reference of this feature to feature used in preview, it is not necessary anymore
124   /// as trim will be removed after execute
125   AttributeReferencePtr aPreviewObjectAttr =
126                      std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
127                      data()->attribute(PREVIEW_OBJECT()));
128
129   ObjectPtr aPreviewObject = aPreviewObjectAttr->value();
130   AttributePoint2DPtr aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
131                                            data()->attribute(PREVIEW_POINT()));
132   std::shared_ptr<GeomAPI_Pnt2d> aPreviewPnt2d = aPoint->pnt();
133   // nullify pointer of preview attribute
134   aPreviewObjectAttr->setValue(ResultPtr());
135   bool anIsEqualPreviewAndSelected = aPreviewObject == aBaseObject;
136
137   // Wait all constraints being created, then send update events
138   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
139   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
140   if (isUpdateFlushed)
141     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
142
143   // Find feature constraints
144   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
145   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
146   std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
147
148   //std::map<FeaturePtr, IdToPointPair> aTangentFeatures;
149   std::map<FeaturePtr, IdToPointPair> aCoincidenceToFeature;
150   getConstraints(aFeaturesToDelete, aFeaturesToUpdate, /*aTangentFeatures, */
151                  aCoincidenceToFeature);
152
153   std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
154   std::list<AttributePtr> aRefsToFeature;
155   getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
156
157   std::map<AttributePtr, AttributePtr> aBasePointModifiedAttributes;
158
159 #ifdef DEBUG_SPLIT
160   std::cout << std::endl;
161   std::cout << "SketchPlugin_Split::execute()" << std::endl;
162   std::cout << std::endl;
163
164   SketchPlugin_Sketch* aSketch = sketch();
165   std::cout << "SKETCH FEATURES (before split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
166   for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
167     std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
168   }
169
170   std::cout << std::endl;
171   std::cout << "---- IN PARAMETERS ----" << std::endl;
172   std::cout << "Base feature:" << getFeatureInfo(aBaseFeature) << std::endl;
173   std::cout << std::endl;
174
175   if (!aCoincidenceToFeature.empty()) {
176     std::cout << "Coincidences to base feature[" <<
177       aCoincidenceToFeature.size() << "]: " << std::endl;
178     std::map<FeaturePtr, IdToPointPair>::const_iterator anIt = aCoincidenceToFeature.begin(),
179                                                         aLast = aCoincidenceToFeature.end();
180     for (int i = 1; anIt != aLast; anIt++, i++) {
181       FeaturePtr aFeature = (*anIt).first;
182       std::string anAttributeId = (*anIt).second.first;
183       std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = (*anIt).second.second;
184
185       std::cout << i << "-" << getFeatureInfo(aFeature) << std::endl;
186       std::cout <<     " -Attribute to correct:" << anAttributeId << std::endl;
187       std::cout <<     " -Point attribute:" <<
188         ModelGeomAlgo_Point2D::getPointAttributeInfo(aPointAttr) << std::endl;
189     }
190   }
191
192   std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
193     aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
194   std::cout << std::endl << "References to attributes of base feature [" <<
195     aBaseRefAttributes.size() << "]" << std::endl;
196   for (; aRefIt != aRefLast; aRefIt++) {
197     AttributePtr aBaseAttr = aRefIt->first;
198     std::list<AttributePtr> aRefAttributes = aRefIt->second;
199     std::string aRefsInfo;
200     std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
201                                             aRefAttrLast = aRefAttributes.end();
202     for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
203       if (!aRefsInfo.empty())
204         aRefsInfo.append(",");
205       AttributePtr aRAttr = *aRefAttrIt;
206       aRefsInfo.append(aRAttr->id());
207       FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
208       aRefsInfo.append("(" + aRFeature->name() + ") ");
209     }
210     std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
211       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
212     std::cout << aPointAttr->id().c_str() <<
213       ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
214   }
215   std::cout << std::endl;
216   std::cout << std::endl << "References to base feature [" <<
217     aRefsToFeature.size() << "]" << std::endl;
218   std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
219                                           aRefAttrLast = aRefsToFeature.end();
220   std::string aRefsInfo;
221   for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
222     if (!aRefsInfo.empty())
223       aRefsInfo.append(",");
224     AttributePtr aRAttr = *aRefAttrIt;
225     aRefsInfo.append(aRAttr->id());
226     FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
227     aRefsInfo.append("(" + aRFeature->name() + ") ");
228   }
229   std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
230
231
232   std::cout << std::endl;
233   std::cout << "---- SPLIT ----" << std::endl;
234   std::cout << std::endl;
235 #endif
236
237   keepCurrentFeature();
238
239   std::string aFeatureKind = aBaseFeature->getKind();
240   FeaturePtr aSplitFeature, anAfterFeature;
241   std::set<AttributePoint2DPtr> aFurtherCoincidences;
242   std::set<FeaturePtr> aCreatedFeatures;
243   std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
244   FeaturePtr aReplacingFeature, aNewFeature;
245   if (aFeatureKind == SketchPlugin_Line::ID())
246     aNewFeature = splitLine(aSplitFeature, aBaseFeature, anAfterFeature,
247                             aFurtherCoincidences, aCreatedFeatures, aModifiedAttributes);
248   else if (aFeatureKind == SketchPlugin_Arc::ID())
249     aNewFeature = splitArc(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences,
250                            aCreatedFeatures, aModifiedAttributes);
251
252   restoreCurrentFeature();
253
254   if (aFeatureKind == SketchPlugin_Circle::ID()) {
255     FeaturePtr aCircleFeature = aBaseFeature;
256     aReplacingFeature = splitCircle(aSplitFeature, aBaseFeature, anAfterFeature,
257                                     aFurtherCoincidences, aCreatedFeatures, aModifiedAttributes);
258
259     updateRefFeatureConstraints(getFeatureResult(aBaseFeature), aRefsToFeature);
260
261     AttributePtr aCenterAttr = aCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID());
262     aFeaturesToDelete.insert(aCircleFeature);
263     // as circle is removed, temporary fill this attribute*/
264     aBaseObjectAttr->setObject(ResultPtr());
265   }
266
267 #ifdef DEBUG_SPLIT
268   std::cout << "---- OUT PARAMETERS ----" << std::endl;
269   std::cout << "Base modified feature:" << getFeatureInfo(aBaseFeature) << std::endl;
270   std::cout << "Split feature:" << getFeatureInfo(aSplitFeature) << std::endl;
271   std::cout << "After feature:" << getFeatureInfo(anAfterFeature) << std::endl;
272   std::cout << std::endl;
273
274   std::cout << "Created features by split:[" << aCreatedFeatures.size() << "]" << std::endl;
275   std::set<FeaturePtr>::const_iterator aFIt = aCreatedFeatures.begin(),
276                                        aFLast = aCreatedFeatures.end();
277   for (; aFIt != aFLast; aFIt++) {
278     std::cout << getFeatureInfo(*aFIt) << std::endl;
279   }
280   std::cout << std::endl;
281
282   std::cout << "Attributes for further Coincidences:" << std::endl;
283   std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
284                                                 aLast = aFurtherCoincidences.end();
285   for (; anIt != aLast; anIt++) {
286     AttributePtr anAttribute = *anIt;
287     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
288     std::cout << ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute)
289               << " [" << getFeatureInfo(aFeature, false) << "]" << std::endl;
290   }
291
292   std::cout << "Modifed attributes (constraints to attributes are moved here):" << std::endl;
293   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
294     aPIt = aModifiedAttributes.begin(), aPLast = aModifiedAttributes.end();
295   std::string aResInfo;
296   for (; aPIt != aPLast; aPIt++) {
297     if (!aResInfo.empty())
298       aResInfo += "\n";
299
300     std::pair<AttributePtr, AttributePtr> aPair = *aPIt;
301
302     AttributePtr anAttr = aPair.first;
303     aResInfo.append(anAttr->id());
304     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->owner());
305     aResInfo.append("(" + aFeature->name() + ") ");
306
307     aResInfo.append("  - is modified to -  ");
308
309     anAttr = aPair.second;
310     aResInfo.append(anAttr->id());
311     aFeature = ModelAPI_Feature::feature(anAttr->owner());
312     aResInfo.append("(" + aFeature->name() + ") ");
313   }
314   std::cout << aResInfo << std::endl;
315 #endif
316
317   std::set<ResultPtr> aFeatureResults;
318   aFeatureResults.insert(getFeatureResult(aBaseFeature));
319   if (anAfterFeature.get() && anAfterFeature != aBaseFeature)
320     aFeatureResults.insert(getFeatureResult(anAfterFeature));
321
322   // coincidence to feature
323   updateCoincidenceConstraintsToFeature(aCoincidenceToFeature, aFurtherCoincidences,
324                                         aFeatureResults, aSplitFeature, aFeaturesToDelete);
325
326   updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes);
327
328   // delete constraints
329 #ifdef DEBUG_SPLIT
330   std::cout << "remove features and references:" << std::endl;
331   std::set<FeaturePtr>::const_iterator aDIt = aFeaturesToDelete.begin(),
332                                        aDLast = aFeaturesToDelete.end();
333   for (; aDIt != aDLast; aDIt++) {
334     std::cout << getFeatureInfo(*aDIt, false) << std::endl;
335     std::cout << std::endl;
336   }
337 #endif
338   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
339   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
340
341 #ifdef DEBUG_SPLIT
342   std::cout << "update features after split:" << std::endl;
343   std::set<FeaturePtr>::const_iterator anUIt = aFeaturesToUpdate.begin(),
344                                        anULast = aFeaturesToUpdate.end();
345   for (; anUIt != anULast; anUIt++) {
346     std::cout << getFeatureInfo(*anUIt, false) << std::endl;
347     std::cout << std::endl;
348   }
349 #endif
350   updateFeaturesAfterSplit(aFeaturesToUpdate);
351
352   // Send events to update the sub-features by the solver.
353   if(isUpdateFlushed) {
354     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
355   }
356
357     if (anIsEqualPreviewAndSelected) {
358     // equal preview and selected objects
359     // nothing to do if the preview and selected objects are different
360     ResultPtr aReplacingResult;
361     if (aReplacingFeature.get()) {
362       aReplacingFeature->execute(); // need it to obtain result
363       aReplacingResult = getFeatureResult(aReplacingFeature);
364     }
365     if (aReplacingResult.get()) { // base object was removed
366       aPreviewObject = aReplacingResult;
367       //aMessage->setSelectedObject(aReplacingResult);
368
369       //GeomShapePtr aSelectedShape = aReplacingResult->shape();
370       //std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
371       //                                                          aPreviewPnt2d->y());
372       //std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
373       //if (ModelGeomAlgo_Point2D::isPointOnEdge(aSelectedShape, aPreviewPnt, aProjectedPoint)) {
374         //bool aValue = true;
375       //}
376       //aBaseShape = aShape;
377
378 #ifdef DEBUG_SPLIT
379       if (!aSelectedShape.get())
380         std::cout << "Set empty selected object" << std::endl;
381       else
382         std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
383 #endif
384       //bool aValue = true;
385     }
386     else {
387       aPreviewObject = ObjectPtr();
388
389       aBaseFeature->execute(); // should recompute shapes of result to do not check obsolete one
390       aBaseObject = getFeatureResult(aBaseFeature);
391       std::shared_ptr<GeomAPI_Pnt> aPreviewPnt = sketch()->to3D(aPreviewPnt2d->x(),
392                                                                 aPreviewPnt2d->y());
393       ResultPtr aBaseResult = std::dynamic_pointer_cast<ModelAPI_Result>(aBaseObject);
394       if (aBaseResult) {
395         GeomShapePtr aShape = aBaseResult->shape();
396         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
397         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
398           aPreviewObject = aBaseResult;
399       }
400       if (!aPreviewObject.get() && aNewFeature.get()) {
401         ResultPtr aNewFeatureResult = getFeatureResult(aNewFeature);
402         if (aNewFeatureResult.get()) {
403           GeomShapePtr aShape = aNewFeatureResult->shape();
404           std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
405           if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPreviewPnt, aProjectedPoint))
406             aPreviewObject = aNewFeatureResult;
407         }
408       }
409     }
410   }
411   if (aPreviewObject.get()) {
412     std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage = std::shared_ptr
413       <ModelAPI_EventReentrantMessage>(new ModelAPI_EventReentrantMessage(
414                                            ModelAPI_EventReentrantMessage::eventId(), this));
415     aMessage->setSelectedObject(aPreviewObject);
416     Events_Loop::loop()->send(aMessage);
417   }
418
419
420 #ifdef DEBUG_SPLIT
421   std::cout << "SKETCH FEATURES (after split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
422   for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
423     std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
424   }
425 #endif
426 }
427
428 // LCOV_EXCL_START
429 std::string SketchPlugin_Split::processEvent(const std::shared_ptr<Events_Message>& theMessage)
430 {
431 #ifdef DEBUG_SPLIT
432   std::cout << "SketchPlugin_Split::processEvent:" << data()->name() << std::endl;
433 #endif
434   std::string aFilledAttributeName;
435
436   std::shared_ptr<ModelAPI_EventReentrantMessage> aMessage =
437         std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
438   if (aMessage.get()) {
439     ObjectPtr anObject = aMessage->selectedObject();
440     std::shared_ptr<GeomAPI_Pnt2d> aPoint = aMessage->clickedPoint();
441
442     if (anObject.get() && aPoint.get()) {
443       //if (myCashedShapes.find(anObject) == myCashedShapes.end())
444       //  fillObjectShapes(anObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
445       if (myCashedShapes.find(anObject) == myCashedShapes.end())
446         fillObjectShapes(anObject, sketch()->data()->owner());
447       const std::set<GeomShapePtr>& aShapes = myCashedShapes[anObject];
448       if (aShapes.size() > 1) {
449         std::shared_ptr<ModelAPI_AttributeReference> aRefSelectedAttr =
450                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
451                               data()->attribute(SELECTED_OBJECT()));
452         std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
453                               std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
454                               data()->attribute(PREVIEW_OBJECT()));
455         aRefSelectedAttr->setValue(anObject);
456         aRefPreviewAttr->setValue(anObject);
457
458         std::shared_ptr<GeomDataAPI_Point2D> aPointSelectedAttr =
459                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
460                               data()->attribute(SELECTED_POINT()));
461         std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
462                               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
463                               data()->attribute(PREVIEW_POINT()));
464         aPointSelectedAttr->setValue(aPoint);
465         aPointPreviewAttr->setValue(aPoint);
466
467         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
468
469         GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
470         if (aSelectedShape.get()) {
471           aFilledAttributeName = SELECTED_OBJECT();
472         }
473         else {
474           // #2480 - sub shape is not initialized when split sketch
475           // If restarted operation use some selection on the shape that is split and
476           // result selectiona can not participate in new split(checked shape above is null),
477           // reset filled values of selection set in this method above
478           aRefSelectedAttr->setValue(ResultPtr());
479           aRefPreviewAttr->setValue(ResultPtr());
480         }
481   #ifdef DEBUG_SPLIT
482         if (!aSelectedShape.get())
483           std::cout << "Set empty selected object" << std::endl;
484         else
485           std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
486   #endif
487       }
488     }
489   }
490   return aFilledAttributeName;
491 }
492 // LCOV_EXCL_STOP
493
494 AISObjectPtr SketchPlugin_Split::getAISObject(AISObjectPtr thePrevious)
495 {
496   /*AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
497                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
498   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
499
500   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
501                                         data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
502   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
503                                         data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
504
505   if (aBaseObjectAttr->isInitialized() && aBaseFeature.get() &&
506       aFirstPointAttrOfSplit->isInitialized() &&
507       aSecondPointAttrOfSplit->isInitialized()) {
508
509     ResultPtr aResult = getFeatureResult(aBaseFeature);
510     GeomShapePtr aBaseShape = aResult->shape();
511     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
512
513     std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
514     std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
515     aPoints.push_back(aStartPoint);
516
517     std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
518     std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
519       sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
520     aPoints.push_back(aSecondPoint);
521
522     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
523
524     GeomAlgoAPI_ShapeTools::splitShape_p(aBaseShape, aPoints, aSplitShapes);
525     std::shared_ptr<GeomAPI_Shape> aShape =
526       GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
527
528     AISObjectPtr anAIS = thePrevious;
529     if (aShape) {
530       if (!anAIS)
531         anAIS = AISObjectPtr(new GeomAPI_AISObject);
532       anAIS->createShape(aShape);
533       std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
534              aBaseFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
535
536       bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
537
538       std::vector<int> aColor;
539       double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
540       int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
541       if (isConstruction) {
542         aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
543         aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
544         aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
545       }
546       else {
547         aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
548       }
549       anAIS->setColor(aColor[0], aColor[1], aColor[2]);
550       anAIS->setWidth(aWidth + 1);
551       anAIS->setLineStyle(aLineStyle);
552     }
553     return anAIS;
554   }
555   return AISObjectPtr();*/
556 #ifdef DEBUG_SPLIT
557   std::cout << "SketchPlugin_Split::getAISObject: " << data()->name() << std::endl;
558 #endif
559
560   AISObjectPtr anAIS = thePrevious;
561
562   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
563   GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
564   if (aPreviewShape.get())
565     aShapes.push_back(aPreviewShape);
566   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
567   if (aSelectedShape.get())
568     aShapes.push_back(aSelectedShape);
569
570   if (aShapes.empty())
571     return AISObjectPtr();
572
573   GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
574   if (!aBaseShape.get())
575     return AISObjectPtr();
576
577   if (aBaseShape.get()) {
578     if (!anAIS)
579       anAIS = AISObjectPtr(new GeomAPI_AISObject);
580     anAIS->createShape(aBaseShape);
581
582     std::vector<int> aColor;
583     aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
584     double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
585     int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
586     anAIS->setColor(aColor[0], aColor[1], aColor[2]);
587     // width when there is not base object should be extened in several points
588     // in order to see this preview over highlight
589     anAIS->setWidth(aWidth+4);
590     anAIS->setLineStyle(aLineStyle);
591   }
592   else
593     anAIS = AISObjectPtr();
594   return anAIS;
595 }
596
597 //********************************************************************
598 void SketchPlugin_Split::fillObjectShapes(const ObjectPtr& theObject,
599                                           const ObjectPtr& theSketch)
600 {
601   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
602   std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> > aPointToAttributes;
603   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
604   // current feature
605   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
606   // edges on feature
607   std::set<ResultPtr> anEdgeResults;
608   ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeResults);
609   if (!anEdgeResults.empty()) {
610     GeomShapePtr aFeatureShape = (*anEdgeResults.begin())->shape();
611
612     // coincidences to the feature
613     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
614                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
615     // layed on feature coincidences to divide it on several shapes
616     std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
617     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
618         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
619     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
620         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
621     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
622         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
623     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
624     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
625     ModelGeomAlgo_Point2D::getPointsInsideShape_p(aFeatureShape, aRefAttributes, aC->pnt(),
626                                                 aX->dir(), aY, aPoints, aPointToAttributes);
627
628     if (!aPoints.empty())
629       GeomAlgoAPI_ShapeTools::splitShape_p(aFeatureShape, aPoints, aShapes);
630   }
631   myCashedShapes[theObject] = aShapes;
632   myCashedReferences[theObject] = aPointToAttributes;
633 }
634
635 GeomShapePtr SketchPlugin_Split::getSubShape(const std::string& theObjectAttributeId,
636                                              const std::string& thePointAttributeId)
637 {
638   GeomShapePtr aBaseShape;
639
640   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
641                                                        data()->attribute(theObjectAttributeId));
642   ObjectPtr aBaseObject = anObjectAttr->value();
643   if (!aBaseObject.get())
644     return aBaseShape;
645
646   // point on feature
647   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
648                                            data()->attribute(thePointAttributeId));
649   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
650   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
651                                                                anAttributePnt2d->y());
652
653 #ifdef TRIM_SHAPE
654   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
655     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
656
657   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
658   if (!aShapes.empty()) {
659     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
660     for (; anIt != aLast; anIt++) {
661       GeomShapePtr aShape = *anIt;
662       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
663       if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
664         aBaseShape = aShape;
665     }
666   }
667 #else
668   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
669     fillObjectShapes(aBaseObject, sketch()->data()->owner());
670
671   std::shared_ptr<GeomAPI_Pnt> aStartPoint;
672   std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
673   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
674   std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
675   for (; anIt != aLast; anIt++) {
676     GeomShapePtr aCurrentShape = *anIt;
677     std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
678     if (ModelGeomAlgo_Point2D::isPointOnEdge(aCurrentShape, anAttributePnt, aProjectedPoint)) {
679       if (aCurrentShape->shapeType() == GeomAPI_Shape::EDGE) {
680         std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aCurrentShape));
681         aStartPoint = anEdge->firstPoint();
682         aSecondPoint = anEdge->lastPoint();
683       }
684       break;
685     }
686   }
687
688   if (!aStartPoint.get() || !aSecondPoint.get())
689     return aBaseShape;
690
691   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
692   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
693   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject/*aBaseObjectAttr->value()*/);
694
695   //AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
696   //                                      data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
697   //AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
698   //                                      data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
699   if (anObjectAttr->isInitialized() && aBaseFeature.get() && aPointAttr->isInitialized()) {
700       //aFirstPointAttrOfSplit->isInitialized() &&
701       //aSecondPointAttrOfSplit->isInitialized()) {
702     ResultPtr aResult = getFeatureResult(aBaseFeature);
703     GeomShapePtr aResultShape = aResult->shape();
704     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
705
706     //std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
707     //std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
708     aPoints.push_back(aStartPoint);
709
710     //std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
711     //std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
712     //  sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
713     aPoints.push_back(aSecondPoint);
714
715     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
716     GeomAlgoAPI_ShapeTools::splitShape_p(aResultShape, aPoints, aSplitShapes);
717     aBaseShape = GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
718 #endif
719   }
720   return aBaseShape;
721 }
722
723 void SketchPlugin_Split::getFeaturePoints(const FeaturePtr& theFeature,
724                                                     AttributePoint2DPtr& theStartPointAttr,
725                                                     AttributePoint2DPtr& theEndPointAttr)
726 {
727   std::string aFeatureKind = theFeature->getKind();
728   std::string aStartAttributeName, anEndAttributeName;
729   if (aFeatureKind == SketchPlugin_Line::ID()) {
730     aStartAttributeName = SketchPlugin_Line::START_ID();
731     anEndAttributeName = SketchPlugin_Line::END_ID();
732   }
733   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
734     aStartAttributeName = SketchPlugin_Arc::START_ID();
735     anEndAttributeName = SketchPlugin_Arc::END_ID();
736   }
737   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
738     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
739                                          theFeature->attribute(aStartAttributeName));
740     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
741                                          theFeature->attribute(anEndAttributeName));
742   }
743 }
744
745 void SketchPlugin_Split::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
746                                     std::set<FeaturePtr>& theFeaturesToUpdate,
747                                     std::map<FeaturePtr, IdToPointPair>& theCoincidenceToFeature)
748 {
749   std::shared_ptr<ModelAPI_Data> aData = data();
750
751   // Check the base objects are initialized.
752   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
753                                                            data()->attribute(SELECTED_OBJECT()));
754   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
755   //                                          aData->attribute(SketchPlugin_Constraint::VALUE()));
756   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
757   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
758
759   std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
760   std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
761   aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
762
763   std::set<AttributePtr>::const_iterator aIt;
764   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
765     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
766     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
767     std::string aRefFeatureKind = aRefFeature->getKind();
768     if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
769         aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
770         aRefFeatureKind == SketchPlugin_MultiTranslation::ID() ||
771         aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
772       theFeaturesToDelete.insert(aRefFeature);
773     else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
774       theFeaturesToUpdate.insert(aRefFeature);
775     else if (aRefFeatureKind == SketchPlugin_ConstraintCoincidence::ID()) {
776       std::string anAttributeToBeModified;
777       AttributePoint2DPtr aCoincidentPoint;
778       AttributeRefAttrPtr anAttrA = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
779       AttributeRefAttrPtr anAttrB = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
780       bool isToFeature = false;
781       if (anAttrA->isObject() || anAttrB->isObject()) { /// coincidence to base feature
782         FeaturePtr aFeature = anAttrA->isObject() ? ModelAPI_Feature::feature(anAttrA->object())
783                                                   : FeaturePtr();
784         isToFeature = aFeature.get() && aFeature == aBaseFeature;
785         anAttributeToBeModified = anAttrA->id();
786         if (!isToFeature) {
787           aFeature = anAttrB->isObject() ? ModelAPI_Feature::feature(anAttrB->object())
788                                          : FeaturePtr();
789           isToFeature = aFeature.get() && aFeature == aBaseFeature;
790           anAttributeToBeModified = anAttrB->id();
791         }
792         if (isToFeature)
793           aCoincidentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aRefFeature);
794       }
795       if (!isToFeature) { /// coincidence to point on base feature
796         AttributePtr anAttribute;
797
798         if (!anAttrA->isObject()) {
799           AttributePtr aCurAttribute = anAttrA->attr();
800           if (aCurAttribute.get()) {
801             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
802             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
803               anAttribute = anAttrB->attr();
804               anAttributeToBeModified = anAttrA->id();
805             }
806           }
807         }
808         if (!anAttribute.get() && !anAttrB->isObject()) {
809           AttributePtr aCurAttribute = anAttrB->attr();
810           if (aCurAttribute.get()) {
811             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
812             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
813               anAttribute = anAttrA->attr();
814               anAttributeToBeModified = anAttrB->id();
815             }
816           }
817         }
818         if (anAttribute.get())
819           aCoincidentPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
820       }
821       if (aCoincidentPoint.get() && isToFeature)
822         theCoincidenceToFeature[aRefFeature] = std::make_pair(anAttributeToBeModified,
823                                                               aCoincidentPoint);
824     }
825   }
826 }
827
828 void SketchPlugin_Split::getRefAttributes(const FeaturePtr& theFeature,
829                                     std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
830                                     std::list<AttributePtr>& theRefsToFeature)
831 {
832   theRefs.clear();
833
834   std::list<AttributePtr> aPointAttributes =
835     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
836   std::set<AttributePtr> aPointAttributesSet;
837
838   std::list<AttributePtr>::const_iterator aPIt =
839     aPointAttributes.begin(), aPLast = aPointAttributes.end();
840   for (; aPIt != aPLast; aPIt++)
841     aPointAttributesSet.insert(*aPIt);
842
843   std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
844   std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
845   aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
846
847   std::set<AttributePtr>::const_iterator aIt;
848   for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
849     AttributePtr anAttr = (*aIt);
850     FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
851     if (anAttrFeature.get() != this &&
852         anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
853       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
854       if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
855         AttributePtr anAttrInRef = aRefAttr->attr();
856         if (anAttrInRef.get() &&
857             aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
858           if (theRefs.find(anAttrInRef) != theRefs.end())
859             theRefs[anAttrInRef].push_back(aRefAttr);
860           else {
861             std::list<AttributePtr> anAttrList;
862             anAttrList.push_back(aRefAttr);
863             theRefs[anAttrInRef] = anAttrList;
864           }
865         }
866       }
867       else { /// find attributes referenced to feature itself
868         theRefsToFeature.push_back(anAttr);
869       }
870     }
871   }
872 }
873
874 void SketchPlugin_Split::updateCoincidenceConstraintsToFeature(
875       const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
876       const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
877       const std::set<ResultPtr>& theFeatureResults,
878       const FeaturePtr& theSplitFeature,
879       std::set<FeaturePtr>& theFeaturesToDelete)
880 {
881   if (theCoincidenceToFeature.empty())
882     return;
883
884   // we should build coincidence constraints to end of the split feature
885   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aNewCoincidencesToSplitFeature;
886   AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
887   getFeaturePoints(theSplitFeature, aStartPointAttr, anEndPointAttr);
888   if (theFurtherCoincidences.find(aStartPointAttr) == theFurtherCoincidences.end())
889     aNewCoincidencesToSplitFeature.insert(aStartPointAttr);
890   if (theFurtherCoincidences.find(anEndPointAttr) == theFurtherCoincidences.end())
891     aNewCoincidencesToSplitFeature.insert(anEndPointAttr);
892
893   std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = theCoincidenceToFeature.begin(),
894                                                             aCLast = theCoincidenceToFeature.end();
895 #ifdef DEBUG_SPLIT
896   std::cout << std::endl;
897   std::cout << "Coincidences to feature(modified):"<< std::endl;
898 #endif
899   for (; aCIt != aCLast; aCIt++) {
900     FeaturePtr aCoincFeature = aCIt->first;
901     std::string anAttributeId = aCIt->second.first;
902     std::string aSecondAttribute = anAttributeId == SketchPlugin_Constraint::ENTITY_A() ?
903         SketchPlugin_Constraint::ENTITY_B() : SketchPlugin_Constraint::ENTITY_A();
904
905     AttributePoint2DPtr aCoincPoint = aCIt->second.second;
906     std::set<AttributePoint2DPtr>::const_iterator aFCIt = theFurtherCoincidences.begin(),
907                                                   aFCLast = theFurtherCoincidences.end();
908     std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
909     AttributePoint2DPtr aFeaturePointAttribute;
910     for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
911       AttributePoint2DPtr aFCAttribute = *aFCIt;
912       if (aCoincPnt->isEqual(aFCAttribute->pnt()))
913         aFeaturePointAttribute = aFCAttribute;
914     }
915     if (aFeaturePointAttribute.get()) {
916       // create new constraint and remove the current
917       aCoincFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
918           SketchPlugin_ConstraintCoincidence::ID(),
919           aFeaturePointAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
920       theFeaturesToDelete.insert(aCIt->first);
921       // create new coincidences to split feature points
922       std::set<AttributePoint2DPtr>::const_iterator aSFIt = aNewCoincidencesToSplitFeature.begin(),
923                                                     aSFLast = aNewCoincidencesToSplitFeature.end();
924       for (; aSFIt != aSFLast; aSFIt++) {
925         AttributePoint2DPtr aSFAttribute = *aSFIt;
926         if (aCoincPnt->isEqual(aSFAttribute->pnt())) {
927           SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
928                            SketchPlugin_ConstraintCoincidence::ID(),
929                            aSFAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
930         }
931       }
932     }
933     else {
934       /// find feature by shape intersected the point
935       ResultPtr aResultForCoincidence = *(theFeatureResults.begin());
936
937       if (theFeatureResults.size() > 1) { // try to find point on additional feature
938         ResultPtr anAddtionalResult = *(theFeatureResults.begin()++);
939         GeomShapePtr aShape = anAddtionalResult->shape();
940
941         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
942         std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
943
944         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
945         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
946           aResultForCoincidence = anAddtionalResult;
947       }
948       aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
949     }
950 #ifdef DEBUG_SPLIT
951   std::cout << " -" << getFeatureInfo(aCoincFeature) << std::endl;
952 #endif
953   }
954 }
955
956 void SketchPlugin_Split::updateRefFeatureConstraints(
957                                                   const ResultPtr& theFeatureBaseResult,
958                                                   const std::list<AttributePtr>& theRefsToFeature)
959 {
960   std::list<AttributePtr>::const_iterator anIt = theRefsToFeature.begin(),
961                                           aLast = theRefsToFeature.end();
962   for (; anIt != aLast; anIt++) {
963     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
964     if (aRefAttr.get())
965       aRefAttr->setObject(theFeatureBaseResult);
966   }
967 }
968
969 void SketchPlugin_Split::updateRefAttConstraints(
970                     const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
971                     const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes)
972 {
973 #ifdef DEBUG_SPLIT
974   std::cout << "SketchPlugin_Split::updateRefAttConstraints" << std::endl;
975 #endif
976
977   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
978     anIt = theModifiedAttributes.begin(),  aLast = theModifiedAttributes.end();
979   for (; anIt != aLast; anIt++) {
980     AttributePtr anAttribute = anIt->first;
981
982     /// not found in references
983     if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
984       continue;
985     std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
986     std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
987                                             aRLast = aRefAttributes.end();
988
989     AttributePtr aNewAttribute = anIt->second;
990     for (; aRefIt != aRLast; aRefIt++) {
991       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
992       if (aRefAttr.get()) {
993         aRefAttr->setAttr(aNewAttribute);
994 #ifdef DEBUG_SPLIT
995         FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->owner());
996         std::cout << " -" << getFeatureInfo(aFeature) << std::endl;
997 #endif
998       }
999     }
1000   }
1001 }
1002
1003 FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature,
1004                                              FeaturePtr& theBaseFeatureModified,
1005                                              FeaturePtr& theAfterFeature,
1006                                              std::set<AttributePoint2DPtr>& thePoints,
1007                                              std::set<FeaturePtr>& theCreatedFeatures,
1008                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1009 {
1010   FeaturePtr anNewFeature;
1011
1012   std::set<FeaturePtr> aCreatedFeatures;
1013   FeaturePtr aConstraintFeature;
1014   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1015
1016   SketchPlugin_Sketch* aSketch = sketch();
1017   if (!aSketch)
1018     return anNewFeature;
1019
1020   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1021                                                            data()->attribute(SELECTED_OBJECT()));
1022   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1023   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1024   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1025   std::string aFeatureKind = aBaseFeature->getKind();
1026   if (aFeatureKind != SketchPlugin_Line::ID())
1027     return anNewFeature;
1028
1029   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1030     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1031   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1032     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1033   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1034
1035   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1036   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1037     setError("Error: Feature has no start and end points.");
1038     return anNewFeature;
1039   }
1040
1041   arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
1042                       aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1043
1044 #ifdef DEBUG_SPLIT
1045   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1046   std::cout << "Start point: " <<
1047     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1048   std::cout << "1st point:   " <<
1049     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1050   std::cout << "2nd point:   " <<
1051     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1052   std::cout << "End point:   " <<
1053     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1054 #endif
1055
1056   /// create a split feature
1057   theSplitFeature =
1058     createLineFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1059   theCreatedFeatures.insert(theSplitFeature);
1060
1061   // before split feature
1062   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1063     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1064                                         theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
1065   }
1066   else {
1067     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1068     /// move end arc point to start of split
1069   }
1070
1071   // after split feature
1072   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1073     FeaturePtr aFeature;
1074     if (!theBaseFeatureModified.get()) {
1075       aFeature = aBaseFeature; ///< use base feature to store all constraints here
1076       fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), aSecondPointAttrOfSplit);
1077       aFeature->execute(); // to update result
1078     }
1079     else {
1080       aFeature = createLineFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1081       theCreatedFeatures.insert(aFeature);
1082       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1083                                              aFeature->attribute(SketchPlugin_Line::END_ID())));
1084       anNewFeature = aFeature;
1085     }
1086     aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1087                      SketchPlugin_ConstraintCoincidence::ID(),
1088                      theSplitFeature->attribute(SketchPlugin_Line::END_ID()),
1089                      aFeature->attribute(SketchPlugin_Line::START_ID()));
1090     theCreatedFeatures.insert(aConstraintFeature);
1091
1092     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1093                                 (aFeature->attribute(SketchPlugin_Line::START_ID())));
1094     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1095                                 (aFeature->attribute(SketchPlugin_Line::END_ID())));
1096
1097     if (!theBaseFeatureModified.get())
1098       theBaseFeatureModified = aFeature;
1099     else
1100       theAfterFeature = aFeature;
1101   }
1102   else {
1103     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1104                                   (theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1105     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1106                                    theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1107   }
1108   // base split, that is defined before split feature should be changed at end
1109   // (after the after feature creation). Otherwise modified value will be used in after feature
1110   // before split feature
1111   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1112     /// move end arc point to start of split
1113     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
1114                                                     aFirstPointAttrOfSplit);
1115     theBaseFeatureModified->execute(); // to update result
1116     aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1117                      SketchPlugin_ConstraintCoincidence::ID(),
1118                      theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
1119                      theSplitFeature->attribute(SketchPlugin_Line::START_ID()));
1120     theCreatedFeatures.insert(aConstraintFeature);
1121
1122     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1123                              (theBaseFeatureModified->attribute(SketchPlugin_Line::START_ID())));
1124     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1125                                (theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID())));
1126   }
1127   else
1128     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1129                                        (theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
1130
1131 #ifdef CREATE_CONSTRAINTS
1132   // additional constraints between split and base features
1133   aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1134           SketchPlugin_ConstraintParallel::ID(),
1135           getFeatureResult(aBaseFeature),
1136           getFeatureResult(theSplitFeature));
1137   theCreatedFeatures.insert(aConstraintFeature);
1138   if (theAfterFeature.get()) {
1139     aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1140           SketchPlugin_ConstraintParallel::ID(),
1141           getFeatureResult(aBaseFeature),
1142           getFeatureResult(theAfterFeature));
1143     theCreatedFeatures.insert(aConstraintFeature);
1144   }
1145 #endif
1146   return anNewFeature;
1147 }
1148
1149 FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature,
1150                                             FeaturePtr& theBaseFeatureModified,
1151                                             FeaturePtr& theAfterFeature,
1152                                             std::set<AttributePoint2DPtr>& thePoints,
1153                                             std::set<FeaturePtr>& theCreatedFeatures,
1154                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1155 {
1156   FeaturePtr anNewFeature;
1157
1158   std::set<FeaturePtr> aCreatedFeatures;
1159   FeaturePtr aConstraintFeature;
1160   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1161
1162   SketchPlugin_Sketch* aSketch = sketch();
1163   if (!aSketch)
1164     return anNewFeature;
1165
1166   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1167                                                            data()->attribute(SELECTED_OBJECT()));
1168   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1169   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1170   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1171   std::string aFeatureKind = aBaseFeature->getKind();
1172   if (aFeatureKind != SketchPlugin_Arc::ID())
1173     return anNewFeature;
1174
1175   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1176     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1177   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1178     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1179   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1180   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1181   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1182     setError("Error: Feature has no start and end points.");
1183     return anNewFeature;
1184   }
1185
1186   arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1187                      aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1188 #ifdef DEBUG_SPLIT
1189   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1190   std::cout << "Start point: " <<
1191     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1192   std::cout << "1st point:   " <<
1193     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1194   std::cout << "2nd point:   " <<
1195     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1196   std::cout << "End point:   " <<
1197     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1198 #endif
1199
1200   /// split feature
1201   theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1202   theCreatedFeatures.insert(theSplitFeature);
1203
1204   // before split feature
1205   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1206     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1207                                   theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1208   }
1209   else {
1210     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1211     /// move end arc point to start of split
1212   }
1213
1214   // after split feature
1215   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1216     FeaturePtr aFeature;
1217     if (!theBaseFeatureModified.get()) {
1218       aFeature = aBaseFeature; ///< use base feature to store all constraints here
1219       fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttrOfSplit);
1220       aFeature->execute(); // to update result
1221     }
1222     else {
1223       aFeature = createArcFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1224       theCreatedFeatures.insert(aFeature);
1225       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1226                                                   aFeature->attribute(SketchPlugin_Arc::END_ID())));
1227       anNewFeature = aFeature;
1228     }
1229     aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1230                      SketchPlugin_ConstraintCoincidence::ID(),
1231                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
1232                      aFeature->attribute(SketchPlugin_Arc::START_ID()));
1233     theCreatedFeatures.insert(aConstraintFeature);
1234
1235     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1236                                 (aFeature->attribute(SketchPlugin_Arc::START_ID())));
1237     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1238                                 (aFeature->attribute(SketchPlugin_Arc::END_ID())));
1239
1240     if (!theBaseFeatureModified.get())
1241       theBaseFeatureModified = aFeature;
1242     else
1243       theAfterFeature = aFeature;
1244   }
1245   else {
1246     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1247                                   (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1248     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1249                                    theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1250   }
1251   // base split, that is defined before split feature should be changed at end
1252   // (after the after feature creation). Otherwise modified value will be used in after feature
1253   // before split feature
1254   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1255     /// move end arc point to start of split
1256     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1257                                                     aFirstPointAttrOfSplit);
1258     theBaseFeatureModified->execute(); // to update result
1259     aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1260                      SketchPlugin_ConstraintCoincidence::ID(),
1261                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1262                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1263     theCreatedFeatures.insert(aConstraintFeature);
1264
1265     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1266                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1267     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1268                                (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1269   }
1270   else
1271     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1272                                        (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1273
1274   // additional constraints between split and base features
1275 #ifdef CREATE_CONSTRAINTS
1276   aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1277           SketchPlugin_ConstraintEqual::ID(),
1278           getFeatureResult(aBaseFeature),
1279           getFeatureResult(theSplitFeature));
1280   theCreatedFeatures.insert(aConstraintFeature);
1281   aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1282           SketchPlugin_ConstraintTangent::ID(),
1283           getFeatureResult(theSplitFeature),
1284           getFeatureResult(aBaseFeature));
1285   theCreatedFeatures.insert(aConstraintFeature);
1286   if (theAfterFeature.get()) {
1287     aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1288                 SketchPlugin_ConstraintEqual::ID(),
1289                 getFeatureResult(aBaseFeature),
1290                 getFeatureResult(theAfterFeature));
1291     theCreatedFeatures.insert(aConstraintFeature);
1292     aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1293                 SketchPlugin_ConstraintTangent::ID(),
1294                 getFeatureResult(theSplitFeature),
1295                 getFeatureResult(theAfterFeature));
1296     theCreatedFeatures.insert(aConstraintFeature);
1297   }
1298 #endif
1299   return anNewFeature;
1300 }
1301
1302 FeaturePtr SketchPlugin_Split::splitCircle(FeaturePtr& theSplitFeature,
1303                                                FeaturePtr& theBaseFeatureModified,
1304                                                FeaturePtr& theAfterFeature,
1305                                                std::set<AttributePoint2DPtr>& thePoints,
1306                                                std::set<FeaturePtr>& theCreatedFeatures,
1307                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1308 {
1309   FeaturePtr anNewFeature;
1310
1311   std::set<FeaturePtr> aCreatedFeatures;
1312   FeaturePtr aConstraintFeature;
1313   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1314
1315   SketchPlugin_Sketch* aSketch = sketch();
1316   if (!aSketch)
1317     return anNewFeature;
1318
1319   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1320                                                            data()->attribute(SELECTED_OBJECT()));
1321   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1322   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1323   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1324   std::string aFeatureKind = aBaseFeature->getKind();
1325   if (aFeatureKind != SketchPlugin_Circle::ID())
1326     return anNewFeature;
1327
1328   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1329     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1330   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1331     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1332
1333   /// split feature
1334   theSplitFeature =
1335     createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1336   bool aSplitReversed = std::dynamic_pointer_cast<SketchPlugin_Arc>(theSplitFeature)->isReversed();
1337   theCreatedFeatures.insert(theSplitFeature);
1338
1339   /// base feature is a left part of the circle
1340   theBaseFeatureModified = createArcFeature(aBaseFeature,
1341     aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1342   anNewFeature = theBaseFeatureModified;
1343   std::dynamic_pointer_cast<SketchPlugin_Arc>(
1344     theBaseFeatureModified)->setReversed(!aSplitReversed);
1345   theBaseFeatureModified->execute();
1346
1347   theModifiedAttributes.insert(
1348     std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1349                   theBaseFeatureModified->attribute(SketchPlugin_Arc::CENTER_ID())));
1350
1351   theCreatedFeatures.insert(theBaseFeatureModified);
1352
1353   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1354                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1355   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1356                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1357
1358   // additional constraints between split and base features
1359   aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1360                      SketchPlugin_ConstraintCoincidence::ID(),
1361                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1362                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()));
1363   theCreatedFeatures.insert(aConstraintFeature);
1364   aConstraintFeature = SketchPlugin_Tools::createConstraintAttrAttr(sketch(),
1365                      SketchPlugin_ConstraintCoincidence::ID(),
1366                      theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID()),
1367                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1368   theCreatedFeatures.insert(aConstraintFeature);
1369
1370 #ifdef CREATE_CONSTRAINTS
1371   aConstraintFeature = SketchPlugin_Tools::createConstraint(sketch(),
1372                      SketchPlugin_ConstraintTangent::ID(),
1373                      getFeatureResult(theSplitFeature),
1374                      getFeatureResult(theBaseFeatureModified));
1375   theCreatedFeatures.insert(aConstraintFeature);
1376 #endif
1377   return anNewFeature;
1378 }
1379
1380 void SketchPlugin_Split::arrangePointsOnLine(
1381     const AttributePoint2DPtr& theStartPointAttr,
1382     const AttributePoint2DPtr& theEndPointAttr,
1383     AttributePoint2DPtr& theFirstPointAttr,
1384     AttributePoint2DPtr& theLastPointAttr) const
1385 {
1386   // if first point is closer to last point, swap first and last values
1387   if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
1388       theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
1389     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1390     theFirstPointAttr = theLastPointAttr;
1391     theLastPointAttr = aTmpPoint;
1392   }
1393 }
1394
1395 void SketchPlugin_Split::arrangePointsOnArc(
1396     const FeaturePtr& theArc,
1397     const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
1398     const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
1399     std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
1400     std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const
1401 {
1402   static const double anAngleTol = 1.e-12;
1403
1404   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1405       theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1406   bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1407
1408   // collect directions to each point
1409   std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1410       new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1411   std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1412       new GeomAPI_Dir2d(theFirstPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1413   std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1414       new GeomAPI_Dir2d(theSecondPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1415
1416   // sort points by their angular values
1417   double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1418   double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1419   double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1420   if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1421     aFirstPtAngle += aPeriod;
1422   if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1423     aSecondPtAngle += aPeriod;
1424
1425   if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1426     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1427     theFirstPointAttr = theSecondPointAttr;
1428     theSecondPointAttr = aTmpPoint;
1429   }
1430 }
1431
1432 void SketchPlugin_Split::fillAttribute(const AttributePtr& theModifiedAttribute,
1433                                                  const AttributePtr& theSourceAttribute)
1434 {
1435   std::string anAttributeType = theModifiedAttribute->attributeType();
1436   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1437     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1438                                               theModifiedAttribute);
1439     AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1440                                               theSourceAttribute);
1441
1442     if (aModifiedAttribute.get() && aSourceAttribute.get())
1443       aModifiedAttribute->setValue(aSourceAttribute->pnt());
1444   }
1445   else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1446     AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1447                                               theModifiedAttribute);
1448     AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1449                                               theSourceAttribute);
1450
1451     if (aModifiedAttribute.get() && aSourceAttribute.get())
1452       aModifiedAttribute->setValue(aSourceAttribute->value());
1453   }
1454 }
1455
1456 FeaturePtr SketchPlugin_Split::createLineFeature(const FeaturePtr& theBaseFeature,
1457                                                            const AttributePtr& theFirstPointAttr,
1458                                                            const AttributePtr& theSecondPointAttr)
1459 {
1460   FeaturePtr aFeature;
1461   SketchPlugin_Sketch* aSketch = sketch();
1462   if (!aSketch || !theBaseFeature.get())
1463     return aFeature;
1464
1465   aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1466
1467   fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPointAttr);
1468   fillAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPointAttr);
1469
1470   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1471                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1472
1473   aFeature->execute(); // to obtain result
1474
1475   return aFeature;
1476 }
1477
1478 FeaturePtr SketchPlugin_Split::createArcFeature(const FeaturePtr& theBaseFeature,
1479                                                           const AttributePtr& theFirstPointAttr,
1480                                                           const AttributePtr& theSecondPointAttr)
1481 {
1482   FeaturePtr aFeature;
1483   SketchPlugin_Sketch* aSketch = sketch();
1484   if (!aSketch || !theBaseFeature.get())
1485     return aFeature;
1486
1487   std::string aCenterAttributeId;
1488   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1489     aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1490   else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1491     aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1492
1493   if (aCenterAttributeId.empty())
1494     return aFeature;
1495
1496   aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1497   // update fillet arc: make the arc correct for sure, so, it is not needed to process
1498   // the "attribute updated"
1499   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1500   bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1501
1502   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1503                 theBaseFeature->attribute(aCenterAttributeId));
1504   fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
1505   fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
1506
1507   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1508                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1509
1510   /// fill referersed state of created arc as it is on the base arc
1511   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1512     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1513     aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1514   }
1515   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1516   aFeature->execute(); // to obtain result
1517
1518   return aFeature;
1519 }
1520
1521 void SketchPlugin_Split::updateFeaturesAfterSplit(
1522                                                    const std::set<FeaturePtr>& theFeaturesToUpdate)
1523 {
1524   std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
1525                                        aLast = theFeaturesToUpdate.end();
1526   for (; anIt != aLast; anIt++) {
1527     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1528     std::string aRefFeatureKind = aRefFeature->getKind();
1529     if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
1530       std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
1531                               std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
1532       if (aLenghtFeature.get()) {
1533         std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
1534             ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
1535         double aValue;
1536         if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
1537           aValueAttr->setValue(aValue);
1538       }
1539     }
1540   }
1541 }
1542
1543 std::shared_ptr<ModelAPI_Result> SketchPlugin_Split::getFeatureResult(
1544                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
1545 {
1546   std::shared_ptr<ModelAPI_Result> aResult;
1547
1548   std::string aFeatureKind = theFeature->getKind();
1549   if (aFeatureKind == SketchPlugin_Line::ID())
1550     aResult = theFeature->firstResult();
1551   else if (aFeatureKind == SketchPlugin_Arc::ID())
1552     aResult = theFeature->lastResult();
1553   else if (aFeatureKind == SketchPlugin_Circle::ID())
1554     aResult = theFeature->lastResult();
1555
1556   return aResult;
1557 }
1558
1559 #ifdef _DEBUG
1560 std::set<std::shared_ptr<ModelAPI_Attribute> > SketchPlugin_Split::getEdgeAttributes(
1561                                            const std::shared_ptr<ModelAPI_Feature>& theFeature)
1562 {
1563   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
1564
1565   std::string aFeatureKind = theFeature->getKind();
1566   if (aFeatureKind == SketchPlugin_Line::ID()) {
1567     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::START_ID()));
1568     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::END_ID()));
1569   }
1570   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
1571     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::START_ID()));
1572     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::END_ID()));
1573   }
1574   else if (aFeatureKind == SketchPlugin_Circle::ID()) {
1575   }
1576
1577   return anAttributes;
1578 }
1579 #endif
1580
1581 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointAttribute
1582                                                               (const bool isFirstAttribute)
1583 {
1584   std::shared_ptr<GeomDataAPI_Point2D> anAttribute;
1585
1586   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
1587   if (!aSelectedShape.get())
1588     return anAttribute;
1589
1590   if (aSelectedShape->shapeType() != GeomAPI_Shape::EDGE)
1591     return anAttribute;
1592
1593   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1594                                                        data()->attribute(SELECTED_OBJECT()));
1595   ObjectPtr aBaseObject = anObjectAttr->value();
1596   if (!aBaseObject.get())
1597     return anAttribute;
1598
1599   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelectedShape));
1600
1601   std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
1602   std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
1603
1604   std::shared_ptr<GeomDataAPI_Point2D> aFirstPointAttr, aLastPointAttr;
1605   /// find the points in feature attributes
1606   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1607   std::list<AttributePtr> a2DPointAttributes = aBaseFeature->data()->attributes(
1608                                                     GeomDataAPI_Point2D::typeId());
1609   std::list<AttributePtr>::const_iterator anIt = a2DPointAttributes.begin(),
1610                                           aLast = a2DPointAttributes.end();
1611   for (; anIt != aLast; anIt++) {
1612     std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
1613                                   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
1614     std::shared_ptr<GeomAPI_Pnt2d> aPoint2D = anAttributePoint->pnt();
1615     std::shared_ptr<GeomAPI_Pnt> aPoint3D = sketch()->to3D(aPoint2D->x(), aPoint2D->y());
1616     if (aFirstPnt->isEqual(aPoint3D))
1617       aFirstPointAttr = anAttributePoint;
1618     else if (aLastPnt->isEqual(aPoint3D))
1619       aLastPointAttr = anAttributePoint;
1620   }
1621
1622   /// find the points in coincident features
1623   PntToAttributesMap aRefAttributes = myCashedReferences[aBaseObject];
1624   PntToAttributesMap::const_iterator
1625     aRIt = aRefAttributes.begin(), aRLast = aRefAttributes.end();
1626   for (; aRIt != aRLast; aRIt++) {
1627     std::shared_ptr<GeomDataAPI_Point2D> anAttribute = aRIt->first;
1628     std::shared_ptr<GeomAPI_Pnt> aPoint = aRIt->second;
1629     if (!aFirstPointAttr.get() && aFirstPnt->isEqual(aPoint))
1630       aFirstPointAttr = anAttribute;
1631     if (!aLastPointAttr.get() && aLastPnt->isEqual(aPoint))
1632       aLastPointAttr = anAttribute;
1633     if (aFirstPointAttr.get() && aLastPointAttr.get())
1634       break;
1635   }
1636   if (!aFirstPointAttr.get() || !aLastPointAttr)
1637     return anAttribute;
1638
1639   return isFirstAttribute ? aFirstPointAttr : aLastPointAttr;
1640 }
1641
1642 #ifdef _DEBUG
1643 std::string SketchPlugin_Split::getFeatureInfo(const std::shared_ptr<ModelAPI_Feature>& theFeature,
1644                                                const bool isUseAttributesInfo)
1645 {
1646   std::string anInfo;
1647   if (!theFeature.get()) {
1648     return "none";
1649   }
1650
1651   if (theFeature->data()->isValid())
1652     anInfo.append(theFeature->data()->name().c_str());
1653
1654   if (isUseAttributesInfo) {
1655     std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(theFeature,
1656                                                              getEdgeAttributes(theFeature));
1657     /// processing of feature with point 2d attributes, like line, arc, circle
1658     if (!aPointsInfo.empty()) {
1659       anInfo += ": ";
1660       anInfo += "\n";
1661       anInfo += aPointsInfo;
1662     }
1663     else { /// process constraint coincidence, find points in ref attr attributes
1664       std::list<AttributePtr> anAttrs = theFeature->data()->attributes(
1665                                                        ModelAPI_AttributeRefAttr::typeId());
1666       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
1667       std::string anAttributesInfo;
1668       for(; anIt != aLast; anIt++) {
1669         if (!anAttributesInfo.empty()) {
1670           anAttributesInfo.append(", ");
1671           anAttributesInfo += "\n";
1672         }
1673         AttributePtr anAttr = *anIt;
1674         std::string aValue = "not defined";
1675         std::string aType = anAttr->attributeType();
1676         if (aType == ModelAPI_AttributeRefAttr::typeId()) {
1677           std::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
1678                              std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
1679           if (aRefAttr.get()) {
1680             if (aRefAttr->isObject()) {
1681               FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1682               aValue = "<object:>" + getFeatureInfo(aFeature, false);
1683             }
1684             else {
1685               AttributePtr anAttribute = aRefAttr->attr();
1686               if (anAttribute.get()) {
1687                 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
1688                 aValue = "<attr:>" + ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute) +
1689                          " [" + getFeatureInfo(aFeature, false) + "]";
1690               }
1691             }
1692           }
1693         }
1694         anAttributesInfo.append("    " + anAttr->id() + ": " + aValue);
1695       }
1696       if (!anAttributesInfo.empty())
1697         anInfo = anInfo + "\n" + anAttributesInfo;
1698     }
1699   }
1700   return anInfo;
1701 }
1702 #endif