Salome HOME
Issue #2215: Wrong cut result between a compsolid and a box
[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
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 //#define DEBUG_SPLIT
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);
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_TRIM_METHODS
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_TRIM_METHODS
427   std::cout << "SketchPlugin_Trim::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   #ifdef DEBUG_TRIM_METHODS
466         if (!aSelectedShape.get())
467           std::cout << "Set empty selected object" << std::endl;
468         else
469           std::cout << "Set shape with ShapeType: " << aSelectedShape->shapeTypeStr() << std::endl;
470   #endif
471         aFilledAttributeName = SELECTED_OBJECT();
472       }
473     }
474   }
475   return aFilledAttributeName;
476 }
477
478 AISObjectPtr SketchPlugin_Split::getAISObject(AISObjectPtr thePrevious)
479 {
480   /*AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
481                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
482   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
483
484   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
485                                         data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
486   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
487                                         data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
488
489   if (aBaseObjectAttr->isInitialized() && aBaseFeature.get() &&
490       aFirstPointAttrOfSplit->isInitialized() &&
491       aSecondPointAttrOfSplit->isInitialized()) {
492
493     ResultPtr aResult = getFeatureResult(aBaseFeature);
494     GeomShapePtr aBaseShape = aResult->shape();
495     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
496
497     std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
498     std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
499     aPoints.push_back(aStartPoint);
500
501     std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
502     std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
503       sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
504     aPoints.push_back(aSecondPoint);
505
506     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
507
508     GeomAlgoAPI_ShapeTools::splitShape_p(aBaseShape, aPoints, aSplitShapes);
509     std::shared_ptr<GeomAPI_Shape> aShape =
510       GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
511
512     AISObjectPtr anAIS = thePrevious;
513     if (aShape) {
514       if (!anAIS)
515         anAIS = AISObjectPtr(new GeomAPI_AISObject);
516       anAIS->createShape(aShape);
517       std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
518              aBaseFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
519
520       bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
521
522       std::vector<int> aColor;
523       double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
524       int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
525       if (isConstruction) {
526         aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
527         aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
528         aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
529       }
530       else {
531         aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
532       }
533       anAIS->setColor(aColor[0], aColor[1], aColor[2]);
534       anAIS->setWidth(aWidth + 1);
535       anAIS->setLineStyle(aLineStyle);
536     }
537     return anAIS;
538   }
539   return AISObjectPtr();*/
540 #ifdef DEBUG_TRIM_METHODS
541   std::cout << "SketchPlugin_Trim::getAISObject: " << data()->name() << std::endl;
542 #endif
543
544   AISObjectPtr anAIS = thePrevious;
545
546   std::list<std::shared_ptr<GeomAPI_Shape> > aShapes;
547   GeomShapePtr aPreviewShape = getSubShape(PREVIEW_OBJECT(), PREVIEW_POINT());
548   if (aPreviewShape.get())
549     aShapes.push_back(aPreviewShape);
550   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
551   if (aSelectedShape.get())
552     aShapes.push_back(aSelectedShape);
553
554   if (aShapes.empty())
555     return AISObjectPtr();
556
557   GeomShapePtr aBaseShape = GeomAlgoAPI_CompoundBuilder::compound(aShapes);
558   if (!aBaseShape.get())
559     return AISObjectPtr();
560
561   if (aBaseShape.get()) {
562     if (!anAIS)
563       anAIS = AISObjectPtr(new GeomAPI_AISObject);
564     anAIS->createShape(aBaseShape);
565
566     std::vector<int> aColor;
567     aColor = Config_PropManager::color("Visualization", "operation_remove_feature_color");
568     double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
569     int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
570     anAIS->setColor(aColor[0], aColor[1], aColor[2]);
571     // width when there is not base object should be extened in several points
572     // in order to see this preview over highlight
573     anAIS->setWidth(aWidth+4);
574     anAIS->setLineStyle(aLineStyle);
575   }
576   else
577     anAIS = AISObjectPtr();
578   return anAIS;
579 }
580
581 //********************************************************************
582 void SketchPlugin_Split::fillObjectShapes(const ObjectPtr& theObject,
583                                           const ObjectPtr& theSketch)
584 {
585   std::set<std::shared_ptr<GeomAPI_Shape> > aShapes;
586   std::map<std::shared_ptr<GeomDataAPI_Point2D>, std::shared_ptr<GeomAPI_Pnt> > aPointToAttributes;
587   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aRefAttributes;
588   // current feature
589   FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
590   // edges on feature
591   std::set<ResultPtr> anEdgeResults;
592   ModelGeomAlgo_Shape::shapesOfType(aFeature, GeomAPI_Shape::EDGE, anEdgeResults);
593   if (!anEdgeResults.empty()) {
594     GeomShapePtr aFeatureShape = (*anEdgeResults.begin())->shape();
595
596     // coincidences to the feature
597     ModelGeomAlgo_Point2D::getPointsOfReference(aFeature, SketchPlugin_ConstraintCoincidence::ID(),
598                          aRefAttributes, SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
599     // layed on feature coincidences to divide it on several shapes
600     std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
601     std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
602         aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
603     std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
604         aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
605     std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
606         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
607     std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
608     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
609     ModelGeomAlgo_Point2D::getPointsInsideShape_p(aFeatureShape, aRefAttributes, aC->pnt(),
610                                                 aX->dir(), aY, aPoints, aPointToAttributes);
611
612     GeomAlgoAPI_ShapeTools::splitShape_p(aFeatureShape, aPoints, aShapes);
613   }
614   myCashedShapes[theObject] = aShapes;
615   myCashedReferences[theObject] = aPointToAttributes;
616 }
617
618 GeomShapePtr SketchPlugin_Split::getSubShape(const std::string& theObjectAttributeId,
619                                              const std::string& thePointAttributeId)
620 {
621   GeomShapePtr aBaseShape;
622
623   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
624                                                        data()->attribute(theObjectAttributeId));
625   ObjectPtr aBaseObject = anObjectAttr->value();
626   if (!aBaseObject.get())
627     return aBaseShape;
628
629   // point on feature
630   AttributePoint2DPtr aPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
631                                            data()->attribute(thePointAttributeId));
632   std::shared_ptr<GeomAPI_Pnt2d> anAttributePnt2d = aPointAttr->pnt();
633   std::shared_ptr<GeomAPI_Pnt> anAttributePnt = sketch()->to3D(anAttributePnt2d->x(),
634                                                                anAttributePnt2d->y());
635
636 #ifdef TRIM_SHAPE
637   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
638     fillObjectShapes(aBaseObject, sketch()->data()->owner(), myCashedShapes, myObjectToPoints);
639
640   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
641   if (!aShapes.empty()) {
642     std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
643     for (; anIt != aLast; anIt++) {
644       GeomShapePtr aShape = *anIt;
645       std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
646       if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, anAttributePnt, aProjectedPoint))
647         aBaseShape = aShape;
648     }
649   }
650 #else
651   if (myCashedShapes.find(aBaseObject) == myCashedShapes.end())
652     fillObjectShapes(aBaseObject, sketch()->data()->owner());
653
654   std::shared_ptr<GeomAPI_Pnt> aStartPoint;
655   std::shared_ptr<GeomAPI_Pnt> aSecondPoint;
656   const std::set<GeomShapePtr>& aShapes = myCashedShapes[aBaseObject];
657   std::set<GeomShapePtr>::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();
658   for (; anIt != aLast; anIt++) {
659     GeomShapePtr aCurrentShape = *anIt;
660     std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
661     if (ModelGeomAlgo_Point2D::isPointOnEdge(aCurrentShape, anAttributePnt, aProjectedPoint)) {
662       if (aCurrentShape->shapeType() == GeomAPI_Shape::EDGE) {
663         std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aCurrentShape));
664         aStartPoint = anEdge->firstPoint();
665         aSecondPoint = anEdge->lastPoint();
666       }
667       break;
668     }
669   }
670
671   if (!aStartPoint.get() || !aSecondPoint.get())
672     return aBaseShape;
673
674   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
675   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
676   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject/*aBaseObjectAttr->value()*/);
677
678   //AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
679   //                                      data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
680   //AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
681   //                                      data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
682   if (anObjectAttr->isInitialized() && aBaseFeature.get() && aPointAttr->isInitialized()) {
683       //aFirstPointAttrOfSplit->isInitialized() &&
684       //aSecondPointAttrOfSplit->isInitialized()) {
685     ResultPtr aResult = getFeatureResult(aBaseFeature);
686     GeomShapePtr aResultShape = aResult->shape();
687     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
688
689     //std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
690     //std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
691     aPoints.push_back(aStartPoint);
692
693     //std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
694     //std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
695     //  sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
696     aPoints.push_back(aSecondPoint);
697
698     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
699     GeomAlgoAPI_ShapeTools::splitShape_p(aResultShape, aPoints, aSplitShapes);
700     aBaseShape = GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
701 #endif
702   }
703   return aBaseShape;
704 }
705
706 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointOfRefAttr(
707                                                       const AttributePtr& theAttribute)
708 {
709   AttributePoint2DPtr aPointAttribute;
710
711   if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
712     AttributeRefAttrPtr aRefAttr =
713       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
714     if (aRefAttr.get() && aRefAttr->isInitialized()) {
715       AttributePtr anAttribute = aRefAttr->attr();
716       if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
717         aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
718     }
719   }
720   return aPointAttribute;
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 {
880   if (theCoincidenceToFeature.empty())
881     return;
882
883   // we should build coincidence constraints to end of the split feature
884   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aNewCoincidencesToSplitFeature;
885   AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
886   getFeaturePoints(theSplitFeature, aStartPointAttr, anEndPointAttr);
887   if (theFurtherCoincidences.find(aStartPointAttr) == theFurtherCoincidences.end())
888     aNewCoincidencesToSplitFeature.insert(aStartPointAttr);
889   if (theFurtherCoincidences.find(anEndPointAttr) == theFurtherCoincidences.end())
890     aNewCoincidencesToSplitFeature.insert(anEndPointAttr);
891
892   std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = theCoincidenceToFeature.begin(),
893                                                             aCLast = theCoincidenceToFeature.end();
894 #ifdef DEBUG_SPLIT
895   std::cout << std::endl;
896   std::cout << "Coincidences to feature(modified):"<< std::endl;
897 #endif
898   for (; aCIt != aCLast; aCIt++) {
899     FeaturePtr aCoincFeature = aCIt->first;
900     std::string anAttributeId = aCIt->second.first;
901     AttributePoint2DPtr aCoincPoint = aCIt->second.second;
902     std::set<AttributePoint2DPtr>::const_iterator aFCIt = theFurtherCoincidences.begin(),
903                                                   aFCLast = theFurtherCoincidences.end();
904     std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
905     AttributePoint2DPtr aFeaturePointAttribute;
906     for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
907       AttributePoint2DPtr aFCAttribute = *aFCIt;
908       if (aCoincPnt->isEqual(aFCAttribute->pnt()))
909         aFeaturePointAttribute = aFCAttribute;
910     }
911     if (aFeaturePointAttribute.get()) {
912       aCoincFeature->refattr(anAttributeId)->setObject(ResultPtr());
913       aCoincFeature->refattr(anAttributeId)->setAttr(aFeaturePointAttribute);
914       // create new coincidences to split feature points
915       std::set<AttributePoint2DPtr>::const_iterator aSFIt = aNewCoincidencesToSplitFeature.begin(),
916                                                     aSFLast = aNewCoincidencesToSplitFeature.end();
917       for (; aSFIt != aSFLast; aSFIt++) {
918         AttributePoint2DPtr aSFAttribute = *aSFIt;
919         if (aCoincPnt->isEqual(aSFAttribute->pnt())) {
920           std::string aSecondAttribute = SketchPlugin_Constraint::ENTITY_A();
921           if (anAttributeId == SketchPlugin_Constraint::ENTITY_A())
922             aSecondAttribute = SketchPlugin_Constraint::ENTITY_B();
923           createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
924                            aSFAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
925         }
926       }
927     }
928     else {
929       /// find feature by shape intersected the point
930       ResultPtr aResultForCoincidence = *(theFeatureResults.begin());
931
932       if (theFeatureResults.size() > 1) { // try to find point on additional feature
933         ResultPtr anAddtionalResult = *(theFeatureResults.begin()++);
934         GeomShapePtr aShape = anAddtionalResult->shape();
935
936         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
937         std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
938
939         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
940         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
941           aResultForCoincidence = anAddtionalResult;
942       }
943       aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
944     }
945 #ifdef DEBUG_SPLIT
946   std::cout << " -" << getFeatureInfo(aCoincFeature) << std::endl;
947 #endif
948   }
949 }
950
951 void SketchPlugin_Split::updateRefFeatureConstraints(
952                                                   const ResultPtr& theFeatureBaseResult,
953                                                   const std::list<AttributePtr>& theRefsToFeature)
954 {
955   std::list<AttributePtr>::const_iterator anIt = theRefsToFeature.begin(),
956                                           aLast = theRefsToFeature.end();
957   for (; anIt != aLast; anIt++) {
958     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
959     if (aRefAttr.get())
960       aRefAttr->setObject(theFeatureBaseResult);
961   }
962 }
963
964 void SketchPlugin_Split::updateRefAttConstraints(
965                     const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
966                     const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes)
967 {
968 #ifdef DEBUG_SPLIT
969   std::cout << "SketchPlugin_Split::updateRefAttConstraints" << std::endl;
970 #endif
971
972   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
973     anIt = theModifiedAttributes.begin(),  aLast = theModifiedAttributes.end();
974   for (; anIt != aLast; anIt++) {
975     AttributePtr anAttribute = anIt->first;
976
977     /// not found in references
978     if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
979       continue;
980     std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
981     std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
982                                             aRLast = aRefAttributes.end();
983
984     AttributePtr aNewAttribute = anIt->second;
985     for (; aRefIt != aRLast; aRefIt++) {
986       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
987       if (aRefAttr.get()) {
988         aRefAttr->setAttr(aNewAttribute);
989 #ifdef DEBUG_SPLIT
990         FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->owner());
991         std::cout << " -" << getFeatureInfo(aFeature) << std::endl;
992 #endif
993       }
994     }
995   }
996 }
997
998 FeaturePtr SketchPlugin_Split::splitLine(FeaturePtr& theSplitFeature,
999                                              FeaturePtr& theBaseFeatureModified,
1000                                              FeaturePtr& theAfterFeature,
1001                                              std::set<AttributePoint2DPtr>& thePoints,
1002                                              std::set<FeaturePtr>& theCreatedFeatures,
1003                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1004 {
1005   FeaturePtr anNewFeature;
1006
1007   std::set<FeaturePtr> aCreatedFeatures;
1008   FeaturePtr aConstraintFeature;
1009   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1010
1011   SketchPlugin_Sketch* aSketch = sketch();
1012   if (!aSketch)
1013     return anNewFeature;
1014
1015   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1016                                                            data()->attribute(SELECTED_OBJECT()));
1017   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1018   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1019   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1020   std::string aFeatureKind = aBaseFeature->getKind();
1021   if (aFeatureKind != SketchPlugin_Line::ID())
1022     return anNewFeature;
1023
1024   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1025     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1026   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1027     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1028   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1029
1030   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1031   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1032     setError("Error: Feature has no start and end points.");
1033     return anNewFeature;
1034   }
1035
1036   arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
1037                       aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1038
1039 #ifdef DEBUG_SPLIT
1040   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1041   std::cout << "Start point: " <<
1042     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1043   std::cout << "1st point:   " <<
1044     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1045   std::cout << "2nd point:   " <<
1046     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1047   std::cout << "End point:   " <<
1048     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1049 #endif
1050
1051   /// create a split feature
1052   theSplitFeature =
1053     createLineFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1054   theCreatedFeatures.insert(theSplitFeature);
1055
1056   // before split feature
1057   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1058     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1059                                         theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
1060   }
1061   else {
1062     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1063     /// move end arc point to start of split
1064   }
1065
1066   // after split feature
1067   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1068     FeaturePtr aFeature;
1069     if (!theBaseFeatureModified.get()) {
1070       aFeature = aBaseFeature; ///< use base feature to store all constraints here
1071       fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), aSecondPointAttrOfSplit);
1072       aFeature->execute(); // to update result
1073     }
1074     else {
1075       aFeature = createLineFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1076       theCreatedFeatures.insert(aFeature);
1077       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1078                                              aFeature->attribute(SketchPlugin_Line::END_ID())));
1079       anNewFeature = aFeature;
1080     }
1081     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1082                      theSplitFeature->attribute(SketchPlugin_Line::END_ID()),
1083                      aFeature->attribute(SketchPlugin_Line::START_ID()));
1084     theCreatedFeatures.insert(aConstraintFeature);
1085
1086     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1087                                 (aFeature->attribute(SketchPlugin_Line::START_ID())));
1088     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1089                                 (aFeature->attribute(SketchPlugin_Line::END_ID())));
1090
1091     if (!theBaseFeatureModified.get())
1092       theBaseFeatureModified = aFeature;
1093     else
1094       theAfterFeature = aFeature;
1095   }
1096   else {
1097     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1098                                   (theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1099     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1100                                    theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
1101   }
1102   // base split, that is defined before split feature should be changed at end
1103   // (after the after feature creation). Otherwise modified value will be used in after feature
1104   // before split feature
1105   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1106     /// move end arc point to start of split
1107     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
1108                                                     aFirstPointAttrOfSplit);
1109     theBaseFeatureModified->execute(); // to update result
1110     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1111                      theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
1112                      theSplitFeature->attribute(SketchPlugin_Line::START_ID()));
1113     theCreatedFeatures.insert(aConstraintFeature);
1114
1115     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1116                              (theBaseFeatureModified->attribute(SketchPlugin_Line::START_ID())));
1117     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1118                                (theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID())));
1119   }
1120   else
1121     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1122                                        (theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
1123
1124 #ifdef CREATE_CONSTRAINTS
1125   // additional constraints between split and base features
1126   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
1127                                                        getFeatureResult(aBaseFeature),
1128                                                        getFeatureResult(theSplitFeature));
1129   theCreatedFeatures.insert(aConstraintFeature);
1130   if (theAfterFeature.get()) {
1131     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
1132                                                     getFeatureResult(aBaseFeature),
1133                                                     getFeatureResult(theAfterFeature));
1134     theCreatedFeatures.insert(aConstraintFeature);
1135   }
1136 #endif
1137   return anNewFeature;
1138 }
1139
1140 FeaturePtr SketchPlugin_Split::splitArc(FeaturePtr& theSplitFeature,
1141                                             FeaturePtr& theBaseFeatureModified,
1142                                             FeaturePtr& theAfterFeature,
1143                                             std::set<AttributePoint2DPtr>& thePoints,
1144                                             std::set<FeaturePtr>& theCreatedFeatures,
1145                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1146 {
1147   FeaturePtr anNewFeature;
1148
1149   std::set<FeaturePtr> aCreatedFeatures;
1150   FeaturePtr aConstraintFeature;
1151   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1152
1153   SketchPlugin_Sketch* aSketch = sketch();
1154   if (!aSketch)
1155     return anNewFeature;
1156
1157   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1158                                                            data()->attribute(SELECTED_OBJECT()));
1159   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1160   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1161   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1162   std::string aFeatureKind = aBaseFeature->getKind();
1163   if (aFeatureKind != SketchPlugin_Arc::ID())
1164     return anNewFeature;
1165
1166   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1167     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1168   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1169     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1170   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
1171   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
1172   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
1173     setError("Error: Feature has no start and end points.");
1174     return anNewFeature;
1175   }
1176
1177   arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
1178                      aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1179 #ifdef DEBUG_SPLIT
1180   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
1181   std::cout << "Start point: " <<
1182     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
1183   std::cout << "1st point:   " <<
1184     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
1185   std::cout << "2nd point:   " <<
1186     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
1187   std::cout << "End point:   " <<
1188     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
1189 #endif
1190
1191   /// split feature
1192   theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1193   theCreatedFeatures.insert(theSplitFeature);
1194
1195   // before split feature
1196   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1197     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
1198                                   theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1199   }
1200   else {
1201     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
1202     /// move end arc point to start of split
1203   }
1204
1205   // after split feature
1206   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
1207     FeaturePtr aFeature;
1208     if (!theBaseFeatureModified.get()) {
1209       aFeature = aBaseFeature; ///< use base feature to store all constraints here
1210       fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttrOfSplit);
1211       aFeature->execute(); // to update result
1212     }
1213     else {
1214       aFeature = createArcFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
1215       theCreatedFeatures.insert(aFeature);
1216       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1217                                                   aFeature->attribute(SketchPlugin_Arc::END_ID())));
1218       anNewFeature = aFeature;
1219     }
1220     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1221                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
1222                      aFeature->attribute(SketchPlugin_Arc::START_ID()));
1223     theCreatedFeatures.insert(aConstraintFeature);
1224
1225     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1226                                 (aFeature->attribute(SketchPlugin_Arc::START_ID())));
1227     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1228                                 (aFeature->attribute(SketchPlugin_Arc::END_ID())));
1229
1230     if (!theBaseFeatureModified.get())
1231       theBaseFeatureModified = aFeature;
1232     else
1233       theAfterFeature = aFeature;
1234   }
1235   else {
1236     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1237                                   (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1238     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
1239                                    theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
1240   }
1241   // base split, that is defined before split feature should be changed at end
1242   // (after the after feature creation). Otherwise modified value will be used in after feature
1243   // before split feature
1244   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
1245     /// move end arc point to start of split
1246     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1247                                                     aFirstPointAttrOfSplit);
1248     theBaseFeatureModified->execute(); // to update result
1249     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1250                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1251                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1252     theCreatedFeatures.insert(aConstraintFeature);
1253
1254     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1255                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1256     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1257                                (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1258   }
1259   else
1260     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1261                                        (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
1262
1263   // additional constraints between split and base features
1264 #ifdef CREATE_CONSTRAINTS
1265   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1266                                                        getFeatureResult(aBaseFeature),
1267                                                        getFeatureResult(theSplitFeature));
1268   theCreatedFeatures.insert(aConstraintFeature);
1269   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
1270                                                        getFeatureResult(theSplitFeature),
1271                                                        getFeatureResult(aBaseFeature));
1272   theCreatedFeatures.insert(aConstraintFeature);
1273   if (theAfterFeature.get()) {
1274     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
1275                                                     getFeatureResult(aBaseFeature),
1276                                                     getFeatureResult(theAfterFeature));
1277     theCreatedFeatures.insert(aConstraintFeature);
1278     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
1279                                                     getFeatureResult(theSplitFeature),
1280                                                     getFeatureResult(theAfterFeature));
1281     theCreatedFeatures.insert(aConstraintFeature);
1282   }
1283 #endif
1284   return anNewFeature;
1285 }
1286
1287 FeaturePtr SketchPlugin_Split::splitCircle(FeaturePtr& theSplitFeature,
1288                                                FeaturePtr& theBaseFeatureModified,
1289                                                FeaturePtr& theAfterFeature,
1290                                                std::set<AttributePoint2DPtr>& thePoints,
1291                                                std::set<FeaturePtr>& theCreatedFeatures,
1292                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
1293 {
1294   FeaturePtr anNewFeature;
1295
1296   std::set<FeaturePtr> aCreatedFeatures;
1297   FeaturePtr aConstraintFeature;
1298   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
1299
1300   SketchPlugin_Sketch* aSketch = sketch();
1301   if (!aSketch)
1302     return anNewFeature;
1303
1304   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1305                                                            data()->attribute(SELECTED_OBJECT()));
1306   //AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1307   //                                         data()->attribute(SketchPlugin_Constraint::VALUE()));
1308   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
1309   std::string aFeatureKind = aBaseFeature->getKind();
1310   if (aFeatureKind != SketchPlugin_Circle::ID())
1311     return anNewFeature;
1312
1313   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointAttribute(true);
1314     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
1315   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointAttribute(false);
1316     //getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
1317
1318   /// split feature
1319   theSplitFeature =
1320     createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1321   bool aSplitReversed = std::dynamic_pointer_cast<SketchPlugin_Arc>(theSplitFeature)->isReversed();
1322   theCreatedFeatures.insert(theSplitFeature);
1323
1324   /// base feature is a left part of the circle
1325   theBaseFeatureModified = createArcFeature(aBaseFeature,
1326     aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
1327   anNewFeature = theBaseFeatureModified;
1328   std::dynamic_pointer_cast<SketchPlugin_Arc>(
1329     theBaseFeatureModified)->setReversed(!aSplitReversed);
1330   theBaseFeatureModified->execute();
1331
1332   theModifiedAttributes.insert(
1333     std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
1334                   theBaseFeatureModified->attribute(SketchPlugin_Arc::CENTER_ID())));
1335
1336   theCreatedFeatures.insert(theBaseFeatureModified);
1337
1338   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1339                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
1340   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
1341                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
1342
1343   // additional constraints between split and base features
1344   aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1345                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1346                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()));
1347   theCreatedFeatures.insert(aConstraintFeature);
1348   aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1349                      theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID()),
1350                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1351   theCreatedFeatures.insert(aConstraintFeature);
1352
1353 #ifdef CREATE_CONSTRAINTS
1354   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
1355                                                        getFeatureResult(theSplitFeature),
1356                                                        getFeatureResult(theBaseFeatureModified));
1357   theCreatedFeatures.insert(aConstraintFeature);
1358 #endif
1359   return anNewFeature;
1360 }
1361
1362 void SketchPlugin_Split::arrangePointsOnLine(
1363     const AttributePoint2DPtr& theStartPointAttr,
1364     const AttributePoint2DPtr& theEndPointAttr,
1365     AttributePoint2DPtr& theFirstPointAttr,
1366     AttributePoint2DPtr& theLastPointAttr) const
1367 {
1368   // if first point is closer to last point, swap first and last values
1369   if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
1370       theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
1371     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1372     theFirstPointAttr = theLastPointAttr;
1373     theLastPointAttr = aTmpPoint;
1374   }
1375 }
1376
1377 void SketchPlugin_Split::arrangePointsOnArc(
1378     const FeaturePtr& theArc,
1379     const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
1380     const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
1381     std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
1382     std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const
1383 {
1384   static const double anAngleTol = 1.e-12;
1385
1386   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1387       theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1388   bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1389
1390   // collect directions to each point
1391   std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1392       new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1393   std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1394       new GeomAPI_Dir2d(theFirstPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1395   std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1396       new GeomAPI_Dir2d(theSecondPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1397
1398   // sort points by their angular values
1399   double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1400   double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1401   double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1402   if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1403     aFirstPtAngle += aPeriod;
1404   if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1405     aSecondPtAngle += aPeriod;
1406
1407   if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1408     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1409     theFirstPointAttr = theSecondPointAttr;
1410     theSecondPointAttr = aTmpPoint;
1411   }
1412 }
1413
1414 void SketchPlugin_Split::fillAttribute(const AttributePtr& theModifiedAttribute,
1415                                                  const AttributePtr& theSourceAttribute)
1416 {
1417   std::string anAttributeType = theModifiedAttribute->attributeType();
1418   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1419     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1420                                               theModifiedAttribute);
1421     AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1422                                               theSourceAttribute);
1423
1424     if (aModifiedAttribute.get() && aSourceAttribute.get())
1425       aModifiedAttribute->setValue(aSourceAttribute->pnt());
1426   }
1427   else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1428     AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1429                                               theModifiedAttribute);
1430     AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1431                                               theSourceAttribute);
1432
1433     if (aModifiedAttribute.get() && aSourceAttribute.get())
1434       aModifiedAttribute->setValue(aSourceAttribute->value());
1435   }
1436 }
1437
1438 FeaturePtr SketchPlugin_Split::createLineFeature(const FeaturePtr& theBaseFeature,
1439                                                            const AttributePtr& theFirstPointAttr,
1440                                                            const AttributePtr& theSecondPointAttr)
1441 {
1442   FeaturePtr aFeature;
1443   SketchPlugin_Sketch* aSketch = sketch();
1444   if (!aSketch || !theBaseFeature.get())
1445     return aFeature;
1446
1447   aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1448
1449   fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPointAttr);
1450   fillAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPointAttr);
1451
1452   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1453                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1454
1455   aFeature->execute(); // to obtain result
1456
1457   return aFeature;
1458 }
1459
1460 FeaturePtr SketchPlugin_Split::createArcFeature(const FeaturePtr& theBaseFeature,
1461                                                           const AttributePtr& theFirstPointAttr,
1462                                                           const AttributePtr& theSecondPointAttr)
1463 {
1464   FeaturePtr aFeature;
1465   SketchPlugin_Sketch* aSketch = sketch();
1466   if (!aSketch || !theBaseFeature.get())
1467     return aFeature;
1468
1469   std::string aCenterAttributeId;
1470   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1471     aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1472   else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1473     aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1474
1475   if (aCenterAttributeId.empty())
1476     return aFeature;
1477
1478   aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1479   // update fillet arc: make the arc correct for sure, so, it is not needed to process
1480   // the "attribute updated"
1481   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1482   bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1483
1484   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1485                 theBaseFeature->attribute(aCenterAttributeId));
1486   fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
1487   fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
1488
1489   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1490                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1491
1492   /// fill referersed state of created arc as it is on the base arc
1493   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1494     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1495     aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1496   }
1497   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1498   aFeature->execute(); // to obtain result
1499
1500   return aFeature;
1501 }
1502
1503 FeaturePtr SketchPlugin_Split::createConstraint(const std::string& theConstraintId,
1504                                                     const AttributePtr& theFirstAttribute,
1505                                                     const AttributePtr& theSecondAttribute)
1506 {
1507   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1508   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1509                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1510   aRefAttr->setAttr(theFirstAttribute);
1511
1512   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1513                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1514   aRefAttr->setAttr(theSecondAttribute);
1515
1516   return aConstraint;
1517 }
1518
1519 FeaturePtr SketchPlugin_Split::createConstraintForObjects(
1520                                                     const std::string& theConstraintId,
1521                                                     const ObjectPtr& theFirstObject,
1522                                                     const ObjectPtr& theSecondObject)
1523 {
1524   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1525   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1526                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1527   aRefAttr->setObject(theFirstObject);
1528
1529   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1530                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1531   aRefAttr->setObject(theSecondObject);
1532
1533   return aConstraint;
1534 }
1535
1536 void SketchPlugin_Split::updateFeaturesAfterSplit(
1537                                                    const std::set<FeaturePtr>& theFeaturesToUpdate)
1538 {
1539   std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
1540                                        aLast = theFeaturesToUpdate.end();
1541   for (; anIt != aLast; anIt++) {
1542     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1543     std::string aRefFeatureKind = aRefFeature->getKind();
1544     if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
1545       std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
1546                               std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
1547       if (aLenghtFeature.get()) {
1548         std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
1549             ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
1550         double aValue;
1551         if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
1552           aValueAttr->setValue(aValue);
1553       }
1554     }
1555   }
1556 }
1557
1558 std::shared_ptr<ModelAPI_Result> SketchPlugin_Split::getFeatureResult(
1559                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
1560 {
1561   std::shared_ptr<ModelAPI_Result> aResult;
1562
1563   std::string aFeatureKind = theFeature->getKind();
1564   if (aFeatureKind == SketchPlugin_Line::ID())
1565     aResult = theFeature->firstResult();
1566   else if (aFeatureKind == SketchPlugin_Arc::ID())
1567     aResult = theFeature->lastResult();
1568   else if (aFeatureKind == SketchPlugin_Circle::ID())
1569     aResult = theFeature->lastResult();
1570
1571   return aResult;
1572 }
1573
1574 std::set<std::shared_ptr<ModelAPI_Attribute> > SketchPlugin_Split::getEdgeAttributes(
1575                                            const std::shared_ptr<ModelAPI_Feature>& theFeature)
1576 {
1577   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
1578
1579   std::string aFeatureKind = theFeature->getKind();
1580   if (aFeatureKind == SketchPlugin_Line::ID()) {
1581     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::START_ID()));
1582     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::END_ID()));
1583   }
1584   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
1585     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::START_ID()));
1586     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::END_ID()));
1587   }
1588   else if (aFeatureKind == SketchPlugin_Circle::ID()) {
1589   }
1590
1591   return anAttributes;
1592 }
1593
1594 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_Split::getPointAttribute
1595                                                               (const bool isFirstAttribute)
1596 {
1597   std::shared_ptr<GeomDataAPI_Point2D> anAttribute;
1598
1599   GeomShapePtr aSelectedShape = getSubShape(SELECTED_OBJECT(), SELECTED_POINT());
1600   if (!aSelectedShape.get())
1601     return anAttribute;
1602
1603   if (aSelectedShape->shapeType() != GeomAPI_Shape::EDGE)
1604     return anAttribute;
1605
1606   AttributeReferencePtr anObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
1607                                                        data()->attribute(SELECTED_OBJECT()));
1608   ObjectPtr aBaseObject = anObjectAttr->value();
1609   if (!aBaseObject.get())
1610     return anAttribute;
1611
1612   std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aSelectedShape));
1613
1614   std::shared_ptr<GeomAPI_Pnt> aFirstPnt = anEdge->firstPoint();
1615   std::shared_ptr<GeomAPI_Pnt> aLastPnt = anEdge->lastPoint();
1616
1617   std::shared_ptr<GeomDataAPI_Point2D> aFirstPointAttr, aLastPointAttr;
1618   /// find the points in feature attributes
1619   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObject);
1620   std::list<AttributePtr> a2DPointAttributes = aBaseFeature->data()->attributes(
1621                                                     GeomDataAPI_Point2D::typeId());
1622   std::list<AttributePtr>::const_iterator anIt = a2DPointAttributes.begin(),
1623                                           aLast = a2DPointAttributes.end();
1624   for (; anIt != aLast; anIt++) {
1625     std::shared_ptr<GeomDataAPI_Point2D> anAttributePoint =
1626                                   std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
1627     std::shared_ptr<GeomAPI_Pnt2d> aPoint2D = anAttributePoint->pnt();
1628     std::shared_ptr<GeomAPI_Pnt> aPoint3D = sketch()->to3D(aPoint2D->x(), aPoint2D->y());
1629     if (aFirstPnt->isEqual(aPoint3D))
1630       aFirstPointAttr = anAttributePoint;
1631     else if (aLastPnt->isEqual(aPoint3D))
1632       aLastPointAttr = anAttributePoint;
1633   }
1634
1635   /// find the points in coincident features
1636   PntToAttributesMap aRefAttributes = myCashedReferences[aBaseObject];
1637   PntToAttributesMap::const_iterator
1638     aRIt = aRefAttributes.begin(), aRLast = aRefAttributes.end();
1639   for (; aRIt != aRLast; aRIt++) {
1640     std::shared_ptr<GeomDataAPI_Point2D> anAttribute = aRIt->first;
1641     std::shared_ptr<GeomAPI_Pnt> aPoint = aRIt->second;
1642     if (!aFirstPointAttr.get() && aFirstPnt->isEqual(aPoint))
1643       aFirstPointAttr = anAttribute;
1644     if (!aLastPointAttr.get() && aLastPnt->isEqual(aPoint))
1645       aLastPointAttr = anAttribute;
1646     if (aFirstPointAttr.get() && aLastPointAttr.get())
1647       break;
1648   }
1649   if (!aFirstPointAttr.get() || !aLastPointAttr)
1650     return anAttribute;
1651
1652   return isFirstAttribute ? aFirstPointAttr : aLastPointAttr;
1653 }
1654
1655 #ifdef _DEBUG
1656 std::string SketchPlugin_Split::getFeatureInfo(const std::shared_ptr<ModelAPI_Feature>& theFeature,
1657                                                const bool isUseAttributesInfo)
1658 {
1659   std::string anInfo;
1660   if (!theFeature.get()) {
1661     return "none";
1662   }
1663
1664   if (theFeature->data()->isValid())
1665     anInfo.append(theFeature->data()->name().c_str());
1666
1667   if (isUseAttributesInfo) {
1668     std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(theFeature,
1669                                                              getEdgeAttributes(theFeature));
1670     /// processing of feature with point 2d attributes, like line, arc, circle
1671     if (!aPointsInfo.empty()) {
1672       anInfo += ": ";
1673       anInfo += "\n";
1674       anInfo += aPointsInfo;
1675     }
1676     else { /// process constraint coincidence, find points in ref attr attributes
1677       std::list<AttributePtr> anAttrs = theFeature->data()->attributes(
1678                                                        ModelAPI_AttributeRefAttr::typeId());
1679       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
1680       std::string anAttributesInfo;
1681       for(; anIt != aLast; anIt++) {
1682         if (!anAttributesInfo.empty()) {
1683           anAttributesInfo.append(", ");
1684           anAttributesInfo += "\n";
1685         }
1686         AttributePtr anAttr = *anIt;
1687         std::string aValue = "not defined";
1688         std::string aType = anAttr->attributeType();
1689         if (aType == ModelAPI_AttributeRefAttr::typeId()) {
1690           std::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
1691                              std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
1692           if (aRefAttr.get()) {
1693             if (aRefAttr->isObject()) {
1694               FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1695               aValue = "<object:>" + getFeatureInfo(aFeature, false);
1696             }
1697             else {
1698               AttributePtr anAttribute = aRefAttr->attr();
1699               if (anAttribute.get()) {
1700                 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
1701                 aValue = "<attr:>" + ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute) +
1702                          " [" + getFeatureInfo(aFeature, false) + "]";
1703               }
1704             }
1705           }
1706         }
1707         anAttributesInfo.append("    " + anAttr->id() + ": " + aValue);
1708       }
1709       if (!anAttributesInfo.empty())
1710         anInfo = anInfo + "\n" + anAttributesInfo;
1711     }
1712   }
1713   return anInfo;
1714 }
1715 #endif