]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintSplit.cpp
Salome HOME
Revert change for MacroArc attribute names
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintSplit.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintSplit.cpp
4 // Created: 25 Aug 2016
5 // Author:  Natalia ERMOLAEVA
6
7 #include "SketchPlugin_ConstraintSplit.h"
8
9 #include <GeomAPI_Dir2d.h>
10 #include <GeomAPI_Pnt2d.h>
11 #include <GeomAPI_XY.h>
12 #include <GeomDataAPI_Point2D.h>
13 #include <GeomAlgoAPI_ShapeTools.h>
14
15 #include <ModelAPI_AttributeReference.h>
16 #include <ModelAPI_AttributeString.h>
17 #include <ModelAPI_AttributeRefAttr.h>
18 #include <ModelAPI_Tools.h>
19 #include <ModelAPI_AttributeBoolean.h>
20
21 #include <ModelAPI_Validator.h>
22 #include <ModelAPI_Session.h>
23 #include <ModelAPI_AttributeDouble.h>
24
25 #include <SketchPlugin_Line.h>
26 #include <SketchPlugin_Arc.h>
27 #include <SketchPlugin_Circle.h>
28 #include <SketchPlugin_ConstraintCoincidence.h>
29 #include <SketchPlugin_ConstraintEqual.h>
30 #include <SketchPlugin_ConstraintParallel.h>
31 #include <SketchPlugin_ConstraintTangent.h>
32 #include <SketchPlugin_ConstraintLength.h>
33 #include <SketchPlugin_ConstraintMirror.h>
34 #include <SketchPlugin_MultiRotation.h>
35 #include <SketchPlugin_MultiTranslation.h>
36 #include <SketchPlugin_ConstraintMiddle.h>
37
38 #include <ModelAPI_Events.h>
39 #include <SketchPlugin_Line.h>
40 #include <SketchPlugin_Arc.h>
41 #include <SketchPlugin_Circle.h>
42
43 #include <ModelGeomAlgo_Point2D.h>
44 #include <Events_Loop.h>
45
46 #include <cmath>
47
48 //#define CREATE_CONSTRAINTS
49
50 //#define DEBUG_SPLIT
51 #ifdef DEBUG_SPLIT
52 #include <iostream>
53 #endif
54
55 static const double PI = 3.141592653589793238463;
56
57 SketchPlugin_ConstraintSplit::SketchPlugin_ConstraintSplit()
58 {
59 }
60
61 void SketchPlugin_ConstraintSplit::initAttributes()
62 {
63   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
64   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
65   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
66 }
67
68 void SketchPlugin_ConstraintSplit::execute()
69 {
70   std::shared_ptr<ModelAPI_Data> aData = data();
71
72   // Check the base objects are initialized.
73   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
74                                             aData->attribute(SketchPlugin_Constraint::VALUE()));
75   if(!aBaseObjectAttr->isInitialized()) {
76     setError("Error: Base object is not initialized.");
77     return;
78   }
79   AttributePoint2DPtr aFirstPointAttrOfSplit =
80     getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
81   AttributePoint2DPtr aSecondPointAttrOfSplit =
82     getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
83   if (!aFirstPointAttrOfSplit.get() || !aFirstPointAttrOfSplit->isInitialized() ||
84       !aSecondPointAttrOfSplit.get() || !aSecondPointAttrOfSplit->isInitialized()) {
85     setError("Error: Sub-shape is not initialized.");
86     return;
87   }
88
89   // Wait all constraints being created, then send update events
90   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
91   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
92   if (isUpdateFlushed)
93     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
94
95
96   // Find feature constraints
97   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
98   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
99   std::set<FeaturePtr> aFeaturesToDelete, aFeaturesToUpdate;
100
101   //std::map<FeaturePtr, IdToPointPair> aTangentFeatures;
102   std::map<FeaturePtr, IdToPointPair> aCoincidenceToFeature;
103   getConstraints(aFeaturesToDelete, aFeaturesToUpdate, /*aTangentFeatures, */
104                  aCoincidenceToFeature);
105
106   std::map<AttributePtr, std::list<AttributePtr> > aBaseRefAttributes;
107   std::list<AttributePtr> aRefsToFeature;
108   getRefAttributes(aBaseFeature, aBaseRefAttributes, aRefsToFeature);
109
110   std::map<AttributePtr, AttributePtr> aBasePointModifiedAttributes;
111
112 #ifdef DEBUG_SPLIT
113   std::cout << std::endl;
114   std::cout << "SketchPlugin_ConstraintSplit::execute()" << std::endl;
115   std::cout << std::endl;
116
117   SketchPlugin_Sketch* aSketch = sketch();
118   std::cout << "SKETCH FEATURES (before split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
119   for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
120     std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
121   }
122
123   std::cout << std::endl;
124   std::cout << "---- IN PARAMETERS ----" << std::endl;
125   std::cout << "Base feature:" << getFeatureInfo(aBaseFeature) << std::endl;
126   std::cout << std::endl;
127
128   if (!aCoincidenceToFeature.empty()) {
129     std::cout << "Coincidences to base feature[" <<
130       aCoincidenceToFeature.size() << "]: " << std::endl;
131     std::map<FeaturePtr, IdToPointPair>::const_iterator anIt = aCoincidenceToFeature.begin(),
132                                                         aLast = aCoincidenceToFeature.end();
133     for (int i = 1; anIt != aLast; anIt++, i++) {
134       FeaturePtr aFeature = (*anIt).first;
135       std::string anAttributeId = (*anIt).second.first;
136       std::shared_ptr<GeomDataAPI_Point2D> aPointAttr = (*anIt).second.second;
137
138       std::cout << i << "-" << getFeatureInfo(aFeature) << std::endl;
139       std::cout <<     " -Attribute to correct:" << anAttributeId << std::endl;
140       std::cout <<     " -Point attribute:" <<
141         ModelGeomAlgo_Point2D::getPointAttributeInfo(aPointAttr) << std::endl;
142     }
143   }
144
145   std::map<AttributePtr, std::list<AttributePtr> >::const_iterator
146     aRefIt = aBaseRefAttributes.begin(), aRefLast = aBaseRefAttributes.end();
147   std::cout << std::endl << "References to attributes of base feature [" <<
148     aBaseRefAttributes.size() << "]" << std::endl;
149   for (; aRefIt != aRefLast; aRefIt++) {
150     AttributePtr aBaseAttr = aRefIt->first;
151     std::list<AttributePtr> aRefAttributes = aRefIt->second;
152     std::string aRefsInfo;
153     std::list<AttributePtr>::const_iterator aRefAttrIt = aRefAttributes.begin(),
154                                             aRefAttrLast = aRefAttributes.end();
155     for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
156       if (!aRefsInfo.empty())
157         aRefsInfo.append(",");
158       AttributePtr aRAttr = *aRefAttrIt;
159       aRefsInfo.append(aRAttr->id());
160       FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
161       aRefsInfo.append("(" + aRFeature->name() + ") ");
162     }
163     std::shared_ptr<GeomDataAPI_Point2D> aPointAttr =
164       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aBaseAttr);
165     std::cout << aPointAttr->id().c_str() <<
166       ": " << "[" << aRefAttributes.size() << "] " << aRefsInfo << std::endl;
167   }
168   std::cout << std::endl;
169   std::cout << std::endl << "References to base feature [" <<
170     aRefsToFeature.size() << "]" << std::endl;
171   std::list<AttributePtr>::const_iterator aRefAttrIt = aRefsToFeature.begin(),
172                                           aRefAttrLast = aRefsToFeature.end();
173   std::string aRefsInfo;
174   for (; aRefAttrIt != aRefAttrLast; aRefAttrIt++) {
175     if (!aRefsInfo.empty())
176       aRefsInfo.append(",");
177     AttributePtr aRAttr = *aRefAttrIt;
178     aRefsInfo.append(aRAttr->id());
179     FeaturePtr aRFeature = ModelAPI_Feature::feature(aRAttr->owner());
180     aRefsInfo.append("(" + aRFeature->name() + ") ");
181   }
182   std::cout << "[" << aRefsToFeature.size() << "] " << aRefsInfo << std::endl;
183
184
185   std::cout << std::endl;
186   std::cout << "---- SPLIT ----" << std::endl;
187   std::cout << std::endl;
188 #endif
189
190   std::string aFeatureKind = aBaseFeature->getKind();
191   FeaturePtr aSplitFeature, anAfterFeature;
192   std::set<AttributePoint2DPtr> aFurtherCoincidences;
193   std::set<FeaturePtr> aCreatedFeatures;
194   std::set<std::pair<AttributePtr, AttributePtr>> aModifiedAttributes;
195   if (aFeatureKind == SketchPlugin_Line::ID())
196     splitLine(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences, aCreatedFeatures,
197               aModifiedAttributes);
198   else if (aFeatureKind == SketchPlugin_Arc::ID())
199     splitArc(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences, aCreatedFeatures,
200              aModifiedAttributes);
201   if (aFeatureKind == SketchPlugin_Circle::ID()) {
202     FeaturePtr aCircleFeature = aBaseFeature;
203     splitCircle(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences,
204       aCreatedFeatures, aModifiedAttributes);
205
206     updateRefFeatureConstraints(getFeatureResult(aBaseFeature), aRefsToFeature);
207
208     AttributePtr aCenterAttr = aCircleFeature->attribute(SketchPlugin_Circle::CENTER_ID());
209     aFeaturesToDelete.insert(aCircleFeature);
210     // as circle is removed, temporary fill this attribute*/
211     aBaseObjectAttr->setObject(ResultPtr());
212   }
213
214 #ifdef DEBUG_SPLIT
215   std::cout << "---- OUT PARAMETERS ----" << std::endl;
216   std::cout << "Base modified feature:" << getFeatureInfo(aBaseFeature) << std::endl;
217   std::cout << "Split feature:" << getFeatureInfo(aSplitFeature) << std::endl;
218   std::cout << "After feature:" << getFeatureInfo(anAfterFeature) << std::endl;
219   std::cout << std::endl;
220
221   std::cout << "Created features by split:[" << aCreatedFeatures.size() << "]" << std::endl;
222   std::set<FeaturePtr>::const_iterator aFIt = aCreatedFeatures.begin(),
223                                        aFLast = aCreatedFeatures.end();
224   for (; aFIt != aFLast; aFIt++) {
225     std::cout << getFeatureInfo(*aFIt) << std::endl;
226   }
227   std::cout << std::endl;
228
229   std::cout << "Attributes for further Coincidences:" << std::endl;
230   std::set<AttributePoint2DPtr>::const_iterator anIt = aFurtherCoincidences.begin(),
231                                                 aLast = aFurtherCoincidences.end();
232   for (; anIt != aLast; anIt++) {
233     AttributePtr anAttribute = *anIt;
234     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
235     std::cout << ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute)
236               << " [" << getFeatureInfo(aFeature, false) << "]" << std::endl;
237   }
238
239   std::cout << "Modifed attributes (constraints to attributes are moved here):" << std::endl;
240   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
241     aPIt = aModifiedAttributes.begin(), aPLast = aModifiedAttributes.end();
242   std::string aResInfo;
243   for (; aPIt != aPLast; aPIt++) {
244     if (!aResInfo.empty())
245       aResInfo += "\n";
246
247     std::pair<AttributePtr, AttributePtr> aPair = *aPIt;
248
249     AttributePtr anAttr = aPair.first;
250     aResInfo.append(anAttr->id());
251     FeaturePtr aFeature = ModelAPI_Feature::feature(anAttr->owner());
252     aResInfo.append("(" + aFeature->name() + ") ");
253
254     aResInfo.append("  - is modified to -  ");
255
256     anAttr = aPair.second;
257     aResInfo.append(anAttr->id());
258     aFeature = ModelAPI_Feature::feature(anAttr->owner());
259     aResInfo.append("(" + aFeature->name() + ") ");
260   }
261   std::cout << aResInfo << std::endl;
262 #endif
263
264   std::set<ResultPtr> aFeatureResults;
265   aFeatureResults.insert(getFeatureResult(aBaseFeature));
266   if (anAfterFeature.get() && anAfterFeature != aBaseFeature)
267     aFeatureResults.insert(getFeatureResult(anAfterFeature));
268
269   // coincidence to feature
270   updateCoincidenceConstraintsToFeature(aCoincidenceToFeature, aFurtherCoincidences,
271                                         aFeatureResults, aSplitFeature);
272
273   updateRefAttConstraints(aBaseRefAttributes, aModifiedAttributes);
274
275   // delete constraints
276 #ifdef DEBUG_SPLIT
277   std::cout << "remove features and references:" << std::endl;
278   std::set<FeaturePtr>::const_iterator aDIt = aFeaturesToDelete.begin(),
279                                        aDLast = aFeaturesToDelete.end();
280   for (; aDIt != aDLast; aDIt++) {
281     std::cout << getFeatureInfo(*aDIt, false) << std::endl;
282     std::cout << std::endl;
283   }
284 #endif
285   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
286   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_DELETED));
287
288 #ifdef DEBUG_SPLIT
289   std::cout << "update features after split:" << std::endl;
290   std::set<FeaturePtr>::const_iterator anUIt = aFeaturesToUpdate.begin(),
291                                        anULast = aFeaturesToUpdate.end();
292   for (; anUIt != anULast; anUIt++) {
293     std::cout << getFeatureInfo(*anUIt, false) << std::endl;
294     std::cout << std::endl;
295   }
296 #endif
297   updateFeaturesAfterSplit(aFeaturesToUpdate);
298
299   // Send events to update the sub-features by the solver.
300   if(isUpdateFlushed) {
301     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
302   }
303
304 #ifdef DEBUG_SPLIT
305   std::cout << "SKETCH FEATURES (after split) [" << aSketch->numberOfSubs() << "]:" << std::endl;
306   for (int i = 0, aNbSubs = aSketch->numberOfSubs(); i < aNbSubs; i++) {
307     std::cout << getFeatureInfo(aSketch->subFeature(i), false) << std::endl;
308   }
309 #endif
310 }
311
312 bool SketchPlugin_ConstraintSplit::isMacro() const
313 {
314   return true;
315 }
316
317 AISObjectPtr SketchPlugin_ConstraintSplit::getAISObject(AISObjectPtr thePrevious)
318 {
319   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
320                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
321   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
322
323   AttributePoint2DPtr aFirstPointAttrOfSplit = getPointOfRefAttr(
324                                         data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
325   AttributePoint2DPtr aSecondPointAttrOfSplit = getPointOfRefAttr(
326                                         data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
327
328   if (aBaseObjectAttr->isInitialized() && aBaseFeature.get() &&
329       aFirstPointAttrOfSplit->isInitialized() &&
330       aSecondPointAttrOfSplit->isInitialized()) {
331
332     ResultPtr aResult = getFeatureResult(aBaseFeature);
333     GeomShapePtr aBaseShape = aResult->shape();
334     std::list<std::shared_ptr<GeomAPI_Pnt> > aPoints;
335
336     std::shared_ptr<GeomAPI_Pnt2d> aStartPnt2d = aFirstPointAttrOfSplit->pnt();
337     std::shared_ptr<GeomAPI_Pnt> aStartPoint = sketch()->to3D(aStartPnt2d->x(), aStartPnt2d->y());
338     aPoints.push_back(aStartPoint);
339
340     std::shared_ptr<GeomAPI_Pnt2d> aSecondPnt2d = aSecondPointAttrOfSplit->pnt();
341     std::shared_ptr<GeomAPI_Pnt> aSecondPoint =
342       sketch()->to3D(aSecondPnt2d->x(), aSecondPnt2d->y());
343     aPoints.push_back(aSecondPoint);
344
345     std::set<std::shared_ptr<GeomAPI_Shape> > aSplitShapes;
346
347     GeomAlgoAPI_ShapeTools::splitShape_p(aBaseShape, aPoints, aSplitShapes);
348     std::shared_ptr<GeomAPI_Shape> aShape =
349       GeomAlgoAPI_ShapeTools::findShape(aPoints, aSplitShapes);
350
351     AISObjectPtr anAIS = thePrevious;
352     if (aShape) {
353       if (!anAIS)
354         anAIS = AISObjectPtr(new GeomAPI_AISObject);
355       anAIS->createShape(aShape);
356       std::shared_ptr<ModelAPI_AttributeBoolean> anAuxiliaryAttr =
357              aBaseFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID());
358
359       bool isConstruction = anAuxiliaryAttr.get() != NULL && anAuxiliaryAttr->value();
360
361       std::vector<int> aColor;
362       double aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH();
363       int aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE();
364       if (isConstruction) {
365         aColor = Config_PropManager::color("Visualization", "sketch_auxiliary_color");
366         aWidth = SketchPlugin_SketchEntity::SKETCH_LINE_WIDTH_AUXILIARY();
367         aLineStyle = SketchPlugin_SketchEntity::SKETCH_LINE_STYLE_AUXILIARY();
368       }
369       else {
370         aColor = Config_PropManager::color("Visualization", "sketch_entity_color");
371       }
372       anAIS->setColor(aColor[0], aColor[1], aColor[2]);
373       anAIS->setWidth(aWidth + 1);
374       anAIS->setLineStyle(aLineStyle);
375     }
376     return anAIS;
377   }
378   return AISObjectPtr();
379 }
380
381 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_ConstraintSplit::getPointOfRefAttr(
382                                                       const AttributePtr& theAttribute)
383 {
384   AttributePoint2DPtr aPointAttribute;
385
386   if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
387     AttributeRefAttrPtr aRefAttr =
388       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
389     if (aRefAttr.get() && aRefAttr->isInitialized()) {
390       AttributePtr anAttribute = aRefAttr->attr();
391       if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
392         aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
393     }
394   }
395   return aPointAttribute;
396 }
397
398 void SketchPlugin_ConstraintSplit::getFeaturePoints(const FeaturePtr& theFeature,
399                                                     AttributePoint2DPtr& theStartPointAttr,
400                                                     AttributePoint2DPtr& theEndPointAttr)
401 {
402   std::string aFeatureKind = theFeature->getKind();
403   std::string aStartAttributeName, anEndAttributeName;
404   if (aFeatureKind == SketchPlugin_Line::ID()) {
405     aStartAttributeName = SketchPlugin_Line::START_ID();
406     anEndAttributeName = SketchPlugin_Line::END_ID();
407   }
408   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
409     aStartAttributeName = SketchPlugin_Arc::START_ID();
410     anEndAttributeName = SketchPlugin_Arc::END_ID();
411   }
412   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
413     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
414                                          theFeature->attribute(aStartAttributeName));
415     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
416                                          theFeature->attribute(anEndAttributeName));
417   }
418 }
419
420 void SketchPlugin_ConstraintSplit::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
421                                       std::set<FeaturePtr>& theFeaturesToUpdate,
422                                       std::map<FeaturePtr, IdToPointPair>& theCoincidenceToFeature)
423 {
424   std::shared_ptr<ModelAPI_Data> aData = data();
425
426   // Check the base objects are initialized.
427   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
428                                             aData->attribute(SketchPlugin_Constraint::VALUE()));
429   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
430   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
431
432   std::set<AttributePtr> aRefsList = aBaseFeatureResult->data()->refsToMe();
433   std::set<AttributePtr> aFRefsList = aBaseFeature->data()->refsToMe();
434   aRefsList.insert(aFRefsList.begin(), aFRefsList.end());
435
436   std::set<AttributePtr>::const_iterator aIt;
437   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
438     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
439     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
440     std::string aRefFeatureKind = aRefFeature->getKind();
441     if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
442         aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
443         aRefFeatureKind == SketchPlugin_MultiTranslation::ID() ||
444         aRefFeatureKind == SketchPlugin_ConstraintMiddle::ID())
445       theFeaturesToDelete.insert(aRefFeature);
446     else if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID())
447       theFeaturesToUpdate.insert(aRefFeature);
448     else if (aRefFeatureKind == SketchPlugin_ConstraintCoincidence::ID()) {
449       std::string anAttributeToBeModified;
450       AttributePoint2DPtr aCoincidentPoint;
451       AttributeRefAttrPtr anAttrA = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
452       AttributeRefAttrPtr anAttrB = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
453       bool isToFeature = false;
454       if (anAttrA->isObject() || anAttrB->isObject()) { /// coincidence to base feature
455         FeaturePtr aFeature = anAttrA->isObject() ? ModelAPI_Feature::feature(anAttrA->object())
456                                                   : FeaturePtr();
457         isToFeature = aFeature.get() && aFeature == aBaseFeature;
458         anAttributeToBeModified = anAttrA->id();
459         if (!isToFeature) {
460           aFeature = anAttrB->isObject() ? ModelAPI_Feature::feature(anAttrB->object())
461                                          : FeaturePtr();
462           isToFeature = aFeature.get() && aFeature == aBaseFeature;
463           anAttributeToBeModified = anAttrB->id();
464         }
465         if (isToFeature)
466           aCoincidentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aRefFeature);
467       }
468       if (!isToFeature) { /// coincidence to point on base feature
469         AttributePtr anAttribute;
470
471         if (!anAttrA->isObject()) {
472           AttributePtr aCurAttribute = anAttrA->attr();
473           if (aCurAttribute.get()) {
474             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
475             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
476               anAttribute = anAttrB->attr();
477               anAttributeToBeModified = anAttrA->id();
478             }
479           }
480         }
481         if (!anAttribute.get() && !anAttrB->isObject()) {
482           AttributePtr aCurAttribute = anAttrB->attr();
483           if (aCurAttribute.get()) {
484             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
485             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
486               anAttribute = anAttrA->attr();
487               anAttributeToBeModified = anAttrB->id();
488             }
489           }
490         }
491         if (anAttribute.get())
492           aCoincidentPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
493       }
494       if (aCoincidentPoint.get() && isToFeature)
495         theCoincidenceToFeature[aRefFeature] = std::make_pair(anAttributeToBeModified,
496                                                               aCoincidentPoint);
497     }
498   }
499 }
500
501 void SketchPlugin_ConstraintSplit::getRefAttributes(const FeaturePtr& theFeature,
502                                     std::map<AttributePtr, std::list<AttributePtr> >& theRefs,
503                                     std::list<AttributePtr>& theRefsToFeature)
504 {
505   theRefs.clear();
506
507   std::list<AttributePtr> aPointAttributes =
508     theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
509   std::set<AttributePtr> aPointAttributesSet;
510
511   std::list<AttributePtr>::const_iterator aPIt =
512     aPointAttributes.begin(), aPLast = aPointAttributes.end();
513   for (; aPIt != aPLast; aPIt++)
514     aPointAttributesSet.insert(*aPIt);
515
516   std::set<AttributePtr> aRefsAttributes = getFeatureResult(theFeature)->data()->refsToMe();
517   std::set<AttributePtr> aFRefsList = theFeature->data()->refsToMe();
518   aRefsAttributes.insert(aFRefsList.begin(), aFRefsList.end());
519
520   std::set<AttributePtr>::const_iterator aIt;
521   for (aIt = aRefsAttributes.cbegin(); aIt != aRefsAttributes.cend(); ++aIt) {
522     AttributePtr anAttr = (*aIt);
523     FeaturePtr anAttrFeature = ModelAPI_Feature::feature(anAttr->owner());
524     if (anAttrFeature.get() != this &&
525         anAttr.get() && anAttr->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
526       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
527       if (!aRefAttr->isObject()) { /// find attributes referenced to feature point attributes
528         AttributePtr anAttrInRef = aRefAttr->attr();
529         if (anAttrInRef.get() &&
530             aPointAttributesSet.find(anAttrInRef) != aPointAttributesSet.end()) {
531           if (theRefs.find(anAttrInRef) != theRefs.end())
532             theRefs[anAttrInRef].push_back(aRefAttr);
533           else {
534             std::list<AttributePtr> anAttrList;
535             anAttrList.push_back(aRefAttr);
536             theRefs[anAttrInRef] = anAttrList;
537           }
538         }
539       }
540       else { /// find attributes referenced to feature itself
541         theRefsToFeature.push_back(anAttr);
542       }
543     }
544   }
545 }
546
547 void SketchPlugin_ConstraintSplit::updateCoincidenceConstraintsToFeature(
548       const std::map<std::shared_ptr<ModelAPI_Feature>, IdToPointPair>& theCoincidenceToFeature,
549       const std::set<std::shared_ptr<GeomDataAPI_Point2D> >& theFurtherCoincidences,
550       const std::set<ResultPtr>& theFeatureResults,
551       const FeaturePtr& theSplitFeature)
552 {
553   if (theCoincidenceToFeature.empty())
554     return;
555
556   // we should build coincidence constraints to end of the split feature
557   std::set<std::shared_ptr<GeomDataAPI_Point2D> > aNewCoincidencesToSplitFeature;
558   AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
559   getFeaturePoints(theSplitFeature, aStartPointAttr, anEndPointAttr);
560   if (theFurtherCoincidences.find(aStartPointAttr) == theFurtherCoincidences.end())
561     aNewCoincidencesToSplitFeature.insert(aStartPointAttr);
562   if (theFurtherCoincidences.find(anEndPointAttr) == theFurtherCoincidences.end())
563     aNewCoincidencesToSplitFeature.insert(anEndPointAttr);
564
565   std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = theCoincidenceToFeature.begin(),
566                                                             aCLast = theCoincidenceToFeature.end();
567 #ifdef DEBUG_SPLIT
568   std::cout << std::endl;
569   std::cout << "Coincidences to feature(modified):"<< std::endl;
570 #endif
571   for (; aCIt != aCLast; aCIt++) {
572     FeaturePtr aCoincFeature = aCIt->first;
573     std::string anAttributeId = aCIt->second.first;
574     AttributePoint2DPtr aCoincPoint = aCIt->second.second;
575     std::set<AttributePoint2DPtr>::const_iterator aFCIt = theFurtherCoincidences.begin(),
576                                                   aFCLast = theFurtherCoincidences.end();
577     std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
578     AttributePoint2DPtr aFeaturePointAttribute;
579     for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
580       AttributePoint2DPtr aFCAttribute = *aFCIt;
581       if (aCoincPnt->isEqual(aFCAttribute->pnt()))
582         aFeaturePointAttribute = aFCAttribute;
583     }
584     if (aFeaturePointAttribute.get()) {
585       aCoincFeature->refattr(anAttributeId)->setObject(ResultPtr());
586       aCoincFeature->refattr(anAttributeId)->setAttr(aFeaturePointAttribute);
587       // create new coincidences to split feature points
588       std::set<AttributePoint2DPtr>::const_iterator aSFIt = aNewCoincidencesToSplitFeature.begin(),
589                                                     aSFLast = aNewCoincidencesToSplitFeature.end();
590       for (; aSFIt != aSFLast; aSFIt++) {
591         AttributePoint2DPtr aSFAttribute = *aSFIt;
592         if (aCoincPnt->isEqual(aSFAttribute->pnt())) {
593           std::string aSecondAttribute = SketchPlugin_Constraint::ENTITY_A();
594           if (anAttributeId == SketchPlugin_Constraint::ENTITY_A())
595             aSecondAttribute = SketchPlugin_Constraint::ENTITY_B();
596           createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
597                            aSFAttribute, aCoincFeature->refattr(aSecondAttribute)->attr());
598         }
599       }
600     }
601     else {
602       /// find feature by shape intersected the point
603       ResultPtr aResultForCoincidence = *(theFeatureResults.begin());
604
605       if (theFeatureResults.size() > 1) { // try to find point on additional feature
606         ResultPtr anAddtionalResult = *(theFeatureResults.begin()++);
607         GeomShapePtr aShape = anAddtionalResult->shape();
608
609         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
610         std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
611
612         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
613         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
614           aResultForCoincidence = anAddtionalResult;
615       }
616       aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
617     }
618 #ifdef DEBUG_SPLIT
619   std::cout << " -" << getFeatureInfo(aCoincFeature) << std::endl;
620 #endif
621   }
622 }
623
624 void SketchPlugin_ConstraintSplit::updateRefFeatureConstraints(
625                                                   const ResultPtr& theFeatureBaseResult,
626                                                   const std::list<AttributePtr>& theRefsToFeature)
627 {
628   std::list<AttributePtr>::const_iterator anIt = theRefsToFeature.begin(),
629                                           aLast = theRefsToFeature.end();
630   for (; anIt != aLast; anIt++) {
631     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
632     if (aRefAttr.get())
633       aRefAttr->setObject(theFeatureBaseResult);
634   }
635 }
636
637 void SketchPlugin_ConstraintSplit::updateRefAttConstraints(
638                     const std::map<AttributePtr, std::list<AttributePtr> >& theBaseRefAttributes,
639                     const std::set<std::pair<AttributePtr, AttributePtr> >& theModifiedAttributes)
640 {
641 #ifdef DEBUG_SPLIT
642   std::cout << "SketchPlugin_ConstraintSplit::updateRefAttConstraints" << std::endl;
643 #endif
644
645   std::set<std::pair<AttributePtr, AttributePtr> >::const_iterator
646     anIt = theModifiedAttributes.begin(),  aLast = theModifiedAttributes.end();
647   for (; anIt != aLast; anIt++) {
648     AttributePtr anAttribute = anIt->first;
649
650     /// not found in references
651     if (theBaseRefAttributes.find(anAttribute) == theBaseRefAttributes.end())
652       continue;
653     std::list<AttributePtr> aRefAttributes = theBaseRefAttributes.at(anAttribute);
654     std::list<AttributePtr>::const_iterator aRefIt = aRefAttributes.begin(),
655                                             aRLast = aRefAttributes.end();
656
657     AttributePtr aNewAttribute = anIt->second;
658     for (; aRefIt != aRLast; aRefIt++) {
659       AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
660       if (aRefAttr.get()) {
661         aRefAttr->setAttr(aNewAttribute);
662 #ifdef DEBUG_SPLIT
663         FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->owner());
664         std::cout << " -" << getFeatureInfo(aFeature) << std::endl;
665 #endif
666       }
667     }
668   }
669 }
670
671 void SketchPlugin_ConstraintSplit::splitLine(FeaturePtr& theSplitFeature,
672                                              FeaturePtr& theBaseFeatureModified,
673                                              FeaturePtr& theAfterFeature,
674                                              std::set<AttributePoint2DPtr>& thePoints,
675                                              std::set<FeaturePtr>& theCreatedFeatures,
676                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
677 {
678   std::set<FeaturePtr> aCreatedFeatures;
679   FeaturePtr aConstraintFeature;
680   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
681
682   SketchPlugin_Sketch* aSketch = sketch();
683   if (!aSketch)
684     return;
685
686   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
687                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
688   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
689   std::string aFeatureKind = aBaseFeature->getKind();
690   if (aFeatureKind != SketchPlugin_Line::ID())
691     return;
692
693   AttributePoint2DPtr aFirstPointAttrOfSplit =
694     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
695   AttributePoint2DPtr aSecondPointAttrOfSplit =
696     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
697   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
698
699   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
700   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
701     setError("Error: Feature has no start and end points.");
702     return;
703   }
704
705   arrangePointsOnLine(aStartPointAttrOfBase, anEndPointAttrOfBase,
706                       aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
707
708 #ifdef DEBUG_SPLIT
709   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
710   std::cout << "Start point: " <<
711     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
712   std::cout << "1st point:   " <<
713     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
714   std::cout << "2nd point:   " <<
715     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
716   std::cout << "End point:   " <<
717     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
718 #endif
719
720   /// create a split feature
721   theSplitFeature =
722     createLineFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
723   theCreatedFeatures.insert(theSplitFeature);
724
725   // before split feature
726   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
727     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
728                                         theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
729   }
730   else {
731     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
732     /// move end arc point to start of split
733   }
734
735   // after split feature
736   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
737     FeaturePtr aFeature;
738     if (!theBaseFeatureModified.get()) {
739       aFeature = aBaseFeature; ///< use base feature to store all constraints here
740       fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), aSecondPointAttrOfSplit);
741       aFeature->execute(); // to update result
742     }
743     else {
744       aFeature = createLineFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
745       theCreatedFeatures.insert(aFeature);
746       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
747                                              aFeature->attribute(SketchPlugin_Line::END_ID())));
748     }
749     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
750                      theSplitFeature->attribute(SketchPlugin_Line::END_ID()),
751                      aFeature->attribute(SketchPlugin_Line::START_ID()));
752     theCreatedFeatures.insert(aConstraintFeature);
753
754     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
755                                 (aFeature->attribute(SketchPlugin_Line::START_ID())));
756     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
757                                 (aFeature->attribute(SketchPlugin_Line::END_ID())));
758
759     if (!theBaseFeatureModified.get())
760       theBaseFeatureModified = aFeature;
761     else
762       theAfterFeature = aFeature;
763   }
764   else {
765     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
766                                   (theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
767     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
768                                    theSplitFeature->attribute(SketchPlugin_Line::END_ID())));
769   }
770   // base split, that is defined before split feature should be changed at end
771   // (after the after feature creation). Otherwise modified value will be used in after feature
772   // before split feature
773   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
774     /// move end arc point to start of split
775     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
776                                                     aFirstPointAttrOfSplit);
777     theBaseFeatureModified->execute(); // to update result
778     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
779                      theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID()),
780                      theSplitFeature->attribute(SketchPlugin_Line::START_ID()));
781     theCreatedFeatures.insert(aConstraintFeature);
782
783     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
784                              (theBaseFeatureModified->attribute(SketchPlugin_Line::START_ID())));
785     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
786                                (theBaseFeatureModified->attribute(SketchPlugin_Line::END_ID())));
787   }
788   else
789     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
790                                        (theSplitFeature->attribute(SketchPlugin_Line::START_ID())));
791
792 #ifdef CREATE_CONSTRAINTS
793   // additional constraints between split and base features
794   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
795                                                        getFeatureResult(aBaseFeature),
796                                                        getFeatureResult(theSplitFeature));
797   theCreatedFeatures.insert(aConstraintFeature);
798   if (theAfterFeature.get()) {
799     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintParallel::ID(),
800                                                     getFeatureResult(aBaseFeature),
801                                                     getFeatureResult(theAfterFeature));
802     theCreatedFeatures.insert(aConstraintFeature);
803   }
804 #endif
805 }
806
807 void SketchPlugin_ConstraintSplit::splitArc(FeaturePtr& theSplitFeature,
808                                             FeaturePtr& theBaseFeatureModified,
809                                             FeaturePtr& theAfterFeature,
810                                             std::set<AttributePoint2DPtr>& thePoints,
811                                             std::set<FeaturePtr>& theCreatedFeatures,
812                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
813 {
814   std::set<FeaturePtr> aCreatedFeatures;
815   FeaturePtr aConstraintFeature;
816   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
817
818   SketchPlugin_Sketch* aSketch = sketch();
819   if (!aSketch)
820     return;
821
822   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
823                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
824   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
825   std::string aFeatureKind = aBaseFeature->getKind();
826   if (aFeatureKind != SketchPlugin_Arc::ID())
827     return;
828
829   AttributePoint2DPtr aFirstPointAttrOfSplit =
830     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
831   AttributePoint2DPtr aSecondPointAttrOfSplit =
832     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
833   AttributePoint2DPtr aStartPointAttrOfBase, anEndPointAttrOfBase;
834   getFeaturePoints(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase);
835   if (!aStartPointAttrOfBase.get() && !anEndPointAttrOfBase.get()) {
836     setError("Error: Feature has no start and end points.");
837     return;
838   }
839
840   arrangePointsOnArc(aBaseFeature, aStartPointAttrOfBase, anEndPointAttrOfBase,
841                      aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
842 #ifdef DEBUG_SPLIT
843   std::cout << "Arranged points (to build split between 1st and 2nd points:" << std::endl;
844   std::cout << "Start point: " <<
845     ModelGeomAlgo_Point2D::getPointAttributeInfo(aStartPointAttrOfBase) << std::endl;
846   std::cout << "1st point:   " <<
847     ModelGeomAlgo_Point2D::getPointAttributeInfo(aFirstPointAttrOfSplit) << std::endl;
848   std::cout << "2nd point:   " <<
849     ModelGeomAlgo_Point2D::getPointAttributeInfo(aSecondPointAttrOfSplit) << std::endl;
850   std::cout << "End point:   " <<
851     ModelGeomAlgo_Point2D::getPointAttributeInfo(anEndPointAttrOfBase) << std::endl;
852 #endif
853
854   /// split feature
855   theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
856   theCreatedFeatures.insert(theSplitFeature);
857
858   // before split feature
859   if (aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
860     theModifiedAttributes.insert(std::make_pair(aStartPointAttrOfBase,
861                                   theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
862   }
863   else {
864     theBaseFeatureModified = aBaseFeature; ///< use base feature to store all constraints here
865     /// move end arc point to start of split
866   }
867
868   // after split feature
869   if (!aSecondPointAttrOfSplit->pnt()->isEqual(anEndPointAttrOfBase->pnt())) {
870     FeaturePtr aFeature;
871     if (!theBaseFeatureModified.get()) {
872       aFeature = aBaseFeature; ///< use base feature to store all constraints here
873       fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttrOfSplit);
874       aFeature->execute(); // to update result
875     }
876     else {
877       aFeature = createArcFeature(aBaseFeature, aSecondPointAttrOfSplit, anEndPointAttrOfBase);
878       theCreatedFeatures.insert(aFeature);
879       theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
880                                                   aFeature->attribute(SketchPlugin_Arc::END_ID())));
881     }
882     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
883                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
884                      aFeature->attribute(SketchPlugin_Arc::START_ID()));
885     theCreatedFeatures.insert(aConstraintFeature);
886
887     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
888                                 (aFeature->attribute(SketchPlugin_Arc::START_ID())));
889     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
890                                 (aFeature->attribute(SketchPlugin_Arc::END_ID())));
891
892     if (!theBaseFeatureModified.get())
893       theBaseFeatureModified = aFeature;
894     else
895       theAfterFeature = aFeature;
896   }
897   else {
898     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
899                                   (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
900     theModifiedAttributes.insert(std::make_pair(anEndPointAttrOfBase,
901                                    theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
902   }
903   // base split, that is defined before split feature should be changed at end
904   // (after the after feature creation). Otherwise modified value will be used in after feature
905   // before split feature
906   if (!aStartPointAttrOfBase->pnt()->isEqual(aFirstPointAttrOfSplit->pnt())) {
907     /// move end arc point to start of split
908     fillAttribute(theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
909                                                     aFirstPointAttrOfSplit);
910     theBaseFeatureModified->execute(); // to update result
911     aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
912                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
913                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
914     theCreatedFeatures.insert(aConstraintFeature);
915
916     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
917                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
918     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
919                                (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
920   }
921   else
922     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
923                                        (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
924
925   // additional constraints between split and base features
926 #ifdef CREATE_CONSTRAINTS
927   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
928                                                        getFeatureResult(aBaseFeature),
929                                                        getFeatureResult(theSplitFeature));
930   theCreatedFeatures.insert(aConstraintFeature);
931   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
932                                                        getFeatureResult(theSplitFeature),
933                                                        getFeatureResult(aBaseFeature));
934   theCreatedFeatures.insert(aConstraintFeature);
935   if (theAfterFeature.get()) {
936     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(),
937                                                     getFeatureResult(aBaseFeature),
938                                                     getFeatureResult(theAfterFeature));
939     theCreatedFeatures.insert(aConstraintFeature);
940     aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
941                                                     getFeatureResult(theSplitFeature),
942                                                     getFeatureResult(theAfterFeature));
943     theCreatedFeatures.insert(aConstraintFeature);
944   }
945 #endif
946 }
947
948 void SketchPlugin_ConstraintSplit::splitCircle(FeaturePtr& theSplitFeature,
949                                                FeaturePtr& theBaseFeatureModified,
950                                                FeaturePtr& theAfterFeature,
951                                                std::set<AttributePoint2DPtr>& thePoints,
952                                                std::set<FeaturePtr>& theCreatedFeatures,
953                  std::set<std::pair<AttributePtr, AttributePtr>>& theModifiedAttributes)
954 {
955   std::set<FeaturePtr> aCreatedFeatures;
956   FeaturePtr aConstraintFeature;
957   theBaseFeatureModified = FeaturePtr(); // it will contain modified base feature
958
959   SketchPlugin_Sketch* aSketch = sketch();
960   if (!aSketch)
961     return;
962
963   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
964                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
965   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
966   std::string aFeatureKind = aBaseFeature->getKind();
967   if (aFeatureKind != SketchPlugin_Circle::ID())
968     return;
969
970   AttributePoint2DPtr aFirstPointAttrOfSplit =
971     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
972   AttributePoint2DPtr aSecondPointAttrOfSplit =
973     getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
974
975   /// split feature
976   theSplitFeature =
977     createArcFeature(aBaseFeature, aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
978   bool aSplitReversed = std::dynamic_pointer_cast<SketchPlugin_Arc>(theSplitFeature)->isReversed();
979   theCreatedFeatures.insert(theSplitFeature);
980
981   /// base feature is a left part of the circle
982   theBaseFeatureModified = createArcFeature(aBaseFeature,
983     aFirstPointAttrOfSplit, aSecondPointAttrOfSplit);
984   std::dynamic_pointer_cast<SketchPlugin_Arc>(
985     theBaseFeatureModified)->setReversed(!aSplitReversed);
986   theBaseFeatureModified->execute();
987
988   theModifiedAttributes.insert(
989     std::make_pair(aBaseFeature->attribute(SketchPlugin_Circle::CENTER_ID()),
990                   theBaseFeatureModified->attribute(SketchPlugin_Arc::CENTER_ID())));
991
992   theCreatedFeatures.insert(theBaseFeatureModified);
993
994   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
995                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID())));
996   thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
997                              (theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID())));
998
999   // additional constraints between split and base features
1000   aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1001                      theBaseFeatureModified->attribute(SketchPlugin_Arc::END_ID()),
1002                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()));
1003   theCreatedFeatures.insert(aConstraintFeature);
1004   aConstraintFeature = createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
1005                      theBaseFeatureModified->attribute(SketchPlugin_Arc::START_ID()),
1006                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
1007   theCreatedFeatures.insert(aConstraintFeature);
1008
1009 #ifdef CREATE_CONSTRAINTS
1010   aConstraintFeature = createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(),
1011                                                        getFeatureResult(theSplitFeature),
1012                                                        getFeatureResult(theBaseFeatureModified));
1013   theCreatedFeatures.insert(aConstraintFeature);
1014 #endif
1015 }
1016
1017 void SketchPlugin_ConstraintSplit::arrangePointsOnLine(
1018     const AttributePoint2DPtr& theStartPointAttr,
1019     const AttributePoint2DPtr& theEndPointAttr,
1020     AttributePoint2DPtr& theFirstPointAttr,
1021     AttributePoint2DPtr& theLastPointAttr) const
1022 {
1023   // if first point is closer to last point, swap first and last values
1024   if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
1025       theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
1026     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1027     theFirstPointAttr = theLastPointAttr;
1028     theLastPointAttr = aTmpPoint;
1029   }
1030 }
1031
1032 void SketchPlugin_ConstraintSplit::arrangePointsOnArc(
1033     const FeaturePtr& theArc,
1034     const std::shared_ptr<GeomDataAPI_Point2D>& theStartPointAttr,
1035     const std::shared_ptr<GeomDataAPI_Point2D>& theEndPointAttr,
1036     std::shared_ptr<GeomDataAPI_Point2D>& theFirstPointAttr,
1037     std::shared_ptr<GeomDataAPI_Point2D>& theSecondPointAttr) const
1038 {
1039   static const double anAngleTol = 1.e-12;
1040
1041   std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1042       theArc->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
1043   bool isReversed = theArc->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1044
1045   // collect directions to each point
1046   std::shared_ptr<GeomAPI_Dir2d> aStartDir(
1047       new GeomAPI_Dir2d(theStartPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1048   std::shared_ptr<GeomAPI_Dir2d> aFirstPtDir(
1049       new GeomAPI_Dir2d(theFirstPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1050   std::shared_ptr<GeomAPI_Dir2d> aSecondPtDir(
1051       new GeomAPI_Dir2d(theSecondPointAttr->pnt()->xy()->decreased(aCenter->xy())));
1052
1053   // sort points by their angular values
1054   double aFirstPtAngle = aStartDir->angle(aFirstPtDir);
1055   double aSecondPtAngle = aStartDir->angle(aSecondPtDir);
1056   double aPeriod = isReversed ? -2.0 * PI : 2.0 * PI;
1057   if (fabs(aFirstPtAngle) > anAngleTol && isReversed == (aFirstPtAngle > 0.))
1058     aFirstPtAngle += aPeriod;
1059   if (fabs(aSecondPtAngle) > anAngleTol && isReversed == (aSecondPtAngle > 0.))
1060     aSecondPtAngle += aPeriod;
1061
1062   if (fabs(aFirstPtAngle) > fabs(aSecondPtAngle)) {
1063     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
1064     theFirstPointAttr = theSecondPointAttr;
1065     theSecondPointAttr = aTmpPoint;
1066   }
1067 }
1068
1069 void SketchPlugin_ConstraintSplit::fillAttribute(const AttributePtr& theModifiedAttribute,
1070                                                  const AttributePtr& theSourceAttribute)
1071 {
1072   std::string anAttributeType = theModifiedAttribute->attributeType();
1073   if (anAttributeType == GeomDataAPI_Point2D::typeId()) {
1074     AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1075                                               theModifiedAttribute);
1076     AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
1077                                               theSourceAttribute);
1078
1079     if (aModifiedAttribute.get() && aSourceAttribute.get())
1080       aModifiedAttribute->setValue(aSourceAttribute->pnt());
1081   }
1082   else if (anAttributeType == ModelAPI_AttributeBoolean::typeId()) {
1083     AttributeBooleanPtr aModifiedAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1084                                               theModifiedAttribute);
1085     AttributeBooleanPtr aSourceAttribute = std::dynamic_pointer_cast<ModelAPI_AttributeBoolean>(
1086                                               theSourceAttribute);
1087
1088     if (aModifiedAttribute.get() && aSourceAttribute.get())
1089       aModifiedAttribute->setValue(aSourceAttribute->value());
1090   }
1091 }
1092
1093 FeaturePtr SketchPlugin_ConstraintSplit::createLineFeature(const FeaturePtr& theBaseFeature,
1094                                                            const AttributePtr& theFirstPointAttr,
1095                                                            const AttributePtr& theSecondPointAttr)
1096 {
1097   FeaturePtr aFeature;
1098   SketchPlugin_Sketch* aSketch = sketch();
1099   if (!aSketch || !theBaseFeature.get())
1100     return aFeature;
1101
1102   aFeature = aSketch->addFeature(SketchPlugin_Line::ID());
1103
1104   fillAttribute(aFeature->attribute(SketchPlugin_Line::START_ID()), theFirstPointAttr);
1105   fillAttribute(aFeature->attribute(SketchPlugin_Line::END_ID()), theSecondPointAttr);
1106
1107   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1108                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1109
1110   aFeature->execute(); // to obtain result
1111
1112   return aFeature;
1113 }
1114
1115 FeaturePtr SketchPlugin_ConstraintSplit::createArcFeature(const FeaturePtr& theBaseFeature,
1116                                                           const AttributePtr& theFirstPointAttr,
1117                                                           const AttributePtr& theSecondPointAttr)
1118 {
1119   FeaturePtr aFeature;
1120   SketchPlugin_Sketch* aSketch = sketch();
1121   if (!aSketch || !theBaseFeature.get())
1122     return aFeature;
1123
1124   std::string aCenterAttributeId;
1125   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID())
1126     aCenterAttributeId = SketchPlugin_Arc::CENTER_ID();
1127   else if (theBaseFeature->getKind() == SketchPlugin_Circle::ID())
1128     aCenterAttributeId = SketchPlugin_Circle::CENTER_ID();
1129
1130   if (aCenterAttributeId.empty())
1131     return aFeature;
1132
1133   aFeature = aSketch->addFeature(SketchPlugin_Arc::ID());
1134   // update fillet arc: make the arc correct for sure, so, it is not needed to process
1135   // the "attribute updated"
1136   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
1137   bool aWasBlocked = aFeature->data()->blockSendAttributeUpdated(true);
1138
1139   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
1140                 theBaseFeature->attribute(aCenterAttributeId));
1141   fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
1142   fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
1143
1144   fillAttribute(aFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()),
1145                 theBaseFeature->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID()));
1146
1147   /// fill referersed state of created arc as it is on the base arc
1148   if (theBaseFeature->getKind() == SketchPlugin_Arc::ID()) {
1149     bool aReversed = theBaseFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->value();
1150     aFeature->boolean(SketchPlugin_Arc::REVERSED_ID())->setValue(aReversed);
1151   }
1152   aFeature->data()->blockSendAttributeUpdated(aWasBlocked);
1153   aFeature->execute(); // to obtain result
1154
1155   return aFeature;
1156 }
1157
1158 FeaturePtr SketchPlugin_ConstraintSplit::createConstraint(const std::string& theConstraintId,
1159                                                     const AttributePtr& theFirstAttribute,
1160                                                     const AttributePtr& theSecondAttribute)
1161 {
1162   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1163   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1164                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1165   aRefAttr->setAttr(theFirstAttribute);
1166
1167   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1168                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1169   aRefAttr->setAttr(theSecondAttribute);
1170
1171   return aConstraint;
1172 }
1173
1174 FeaturePtr SketchPlugin_ConstraintSplit::createConstraintForObjects(
1175                                                     const std::string& theConstraintId,
1176                                                     const ObjectPtr& theFirstObject,
1177                                                     const ObjectPtr& theSecondObject)
1178 {
1179   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
1180   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1181                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
1182   aRefAttr->setObject(theFirstObject);
1183
1184   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
1185                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
1186   aRefAttr->setObject(theSecondObject);
1187
1188   return aConstraint;
1189 }
1190
1191 void SketchPlugin_ConstraintSplit::updateFeaturesAfterSplit(
1192                                                    const std::set<FeaturePtr>& theFeaturesToUpdate)
1193 {
1194   std::set<FeaturePtr>::const_iterator anIt = theFeaturesToUpdate.begin(),
1195                                        aLast = theFeaturesToUpdate.end();
1196   for (; anIt != aLast; anIt++) {
1197     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
1198     std::string aRefFeatureKind = aRefFeature->getKind();
1199     if (aRefFeatureKind == SketchPlugin_ConstraintLength::ID()) {
1200       std::shared_ptr<SketchPlugin_ConstraintLength> aLenghtFeature =
1201                               std::dynamic_pointer_cast<SketchPlugin_ConstraintLength>(*anIt);
1202       if (aLenghtFeature.get()) {
1203         std::shared_ptr<ModelAPI_AttributeDouble> aValueAttr = std::dynamic_pointer_cast<
1204             ModelAPI_AttributeDouble>(aLenghtFeature->attribute(SketchPlugin_Constraint::VALUE()));
1205         double aValue;
1206         if (aLenghtFeature->computeLenghtValue(aValue) && aValueAttr.get())
1207           aValueAttr->setValue(aValue);
1208       }
1209     }
1210   }
1211 }
1212
1213 std::shared_ptr<ModelAPI_Result> SketchPlugin_ConstraintSplit::getFeatureResult(
1214                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
1215 {
1216   std::shared_ptr<ModelAPI_Result> aResult;
1217
1218   std::string aFeatureKind = theFeature->getKind();
1219   if (aFeatureKind == SketchPlugin_Line::ID())
1220     aResult = theFeature->firstResult();
1221   else if (aFeatureKind == SketchPlugin_Arc::ID())
1222     aResult = theFeature->lastResult();
1223   else if (aFeatureKind == SketchPlugin_Circle::ID())
1224     aResult = theFeature->lastResult();
1225
1226   return aResult;
1227 }
1228
1229 std::set<std::shared_ptr<ModelAPI_Attribute> > SketchPlugin_ConstraintSplit::getEdgeAttributes(
1230                                            const std::shared_ptr<ModelAPI_Feature>& theFeature)
1231 {
1232   std::set<std::shared_ptr<ModelAPI_Attribute> > anAttributes;
1233
1234   std::string aFeatureKind = theFeature->getKind();
1235   if (aFeatureKind == SketchPlugin_Line::ID()) {
1236     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::START_ID()));
1237     anAttributes.insert(theFeature->attribute(SketchPlugin_Line::END_ID()));
1238   }
1239   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
1240     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::START_ID()));
1241     anAttributes.insert(theFeature->attribute(SketchPlugin_Arc::END_ID()));
1242   }
1243   else if (aFeatureKind == SketchPlugin_Circle::ID()) {
1244   }
1245
1246   return anAttributes;
1247 }
1248
1249 #ifdef _DEBUG
1250 std::string SketchPlugin_ConstraintSplit::getFeatureInfo(
1251                                                const std::shared_ptr<ModelAPI_Feature>& theFeature,
1252                                                const bool isUseAttributesInfo)
1253 {
1254   std::string anInfo;
1255   if (!theFeature.get()) {
1256     return "none";
1257   }
1258
1259   if (theFeature->data()->isValid())
1260     anInfo.append(theFeature->data()->name().c_str());
1261
1262   if (isUseAttributesInfo) {
1263     std::string aPointsInfo = ModelGeomAlgo_Point2D::getPontAttributesInfo(theFeature,
1264                                                              getEdgeAttributes(theFeature));
1265     /// processing of feature with point 2d attributes, like line, arc, circle
1266     if (!aPointsInfo.empty()) {
1267       anInfo += ": ";
1268       anInfo += "\n";
1269       anInfo += aPointsInfo;
1270     }
1271     else { /// process constraint coincidence, find points in ref attr attributes
1272       std::list<AttributePtr> anAttrs = theFeature->data()->attributes(
1273                                                        ModelAPI_AttributeRefAttr::typeId());
1274       std::list<AttributePtr>::const_iterator anIt = anAttrs.begin(), aLast = anAttrs.end();
1275       std::string anAttributesInfo;
1276       for(; anIt != aLast; anIt++) {
1277         if (!anAttributesInfo.empty()) {
1278           anAttributesInfo.append(", ");
1279           anAttributesInfo += "\n";
1280         }
1281         AttributePtr anAttr = *anIt;
1282         std::string aValue = "not defined";
1283         std::string aType = anAttr->attributeType();
1284         if (aType == ModelAPI_AttributeRefAttr::typeId()) {
1285           std::shared_ptr<ModelAPI_AttributeRefAttr> aRefAttr =
1286                              std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
1287           if (aRefAttr.get()) {
1288             if (aRefAttr->isObject()) {
1289               FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
1290               aValue = "<object:>" + getFeatureInfo(aFeature, false);
1291             }
1292             else {
1293               AttributePtr anAttribute = aRefAttr->attr();
1294               if (anAttribute.get()) {
1295                 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
1296                 aValue = "<attr:>" + ModelGeomAlgo_Point2D::getPointAttributeInfo(anAttribute) +
1297                          " [" + getFeatureInfo(aFeature, false) + "]";
1298               }
1299             }
1300           }
1301         }
1302         anAttributesInfo.append("    " + anAttr->id() + ": " + aValue);
1303       }
1304       if (!anAttributesInfo.empty())
1305         anInfo = anInfo + "\n" + anAttributesInfo;
1306     }
1307   }
1308   return anInfo;
1309 }
1310 #endif