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