Salome HOME
4e6cdfd62a6f7d5f0d5135cb61ba451ff4c9cb49
[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: 17 Jul 2016
5 // Author:  Natalia ERMOLAEVA
6
7 #include "SketchPlugin_ConstraintSplit.h"
8
9 //#include <GeomAPI_Circ2d.h>
10 //#include <GeomAPI_Dir2d.h>
11 //#include <GeomAPI_Lin2d.h>
12 #include <GeomAPI_Pnt2d.h>
13 //#include <GeomAPI_XY.h>
14 #include <GeomDataAPI_Point2D.h>
15 //#include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeReference.h>
17 #include <ModelAPI_AttributeString.h>
18 //#include <ModelAPI_AttributeRefList.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_Tools.h>
21
22 #include <ModelAPI_Validator.h>
23 #include <ModelAPI_Session.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_ConstraintMirror.h>
33 #include <SketchPlugin_MultiRotation.h>
34 #include <SketchPlugin_MultiTranslation.h>
35
36 //#include <ModelAPI_Data.h>
37 #include <ModelAPI_Events.h>
38 //#include <ModelAPI_Session.h>
39 //#include <ModelAPI_Validator.h>
40 //
41 #include <SketchPlugin_Line.h>
42 #include <SketchPlugin_Arc.h>
43 #include <SketchPlugin_Circle.h>
44 //#include <SketchPlugin_Point.h>
45 //#include <SketchPlugin_Sketch.h>
46 //#include <SketchPlugin_Tools.h>
47 //
48
49 #include <ModelGeomAlgo_Point2D.h>
50 #include <Events_Loop.h>
51 //
52 //#include <math.h>
53 //
54 //const double tolerance = 1.e-7;
55 //const double paramTolerance = 1.e-4;
56
57 ///// \brief Attract specified point on theNewArc to the attribute of theFeature
58 //static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
59 //  FeaturePtr theFeature, const std::string& theFeatureAttribute);
60 //
61 ///// \brief Calculates center of fillet arc and coordinates of tangency points
62 //static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
63 //                                  double theRadius, bool theNotInversed[2],
64 //                                  std::shared_ptr<GeomAPI_XY>& theCenter,
65 //                                  std::shared_ptr<GeomAPI_XY>& theTangentA,
66 //                                  std::shared_ptr<GeomAPI_XY>& theTangentB);
67 //
68 ///// Get point on 1/3 length of edge from fillet point
69 //static void getPointOnEdge(const FeaturePtr theFeature,
70 //                           const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
71 //                           std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
72 //
73 ///// Get distance from point to feature
74 //static double getProjectionDistance(const FeaturePtr theFeature,
75 //                             const std::shared_ptr<GeomAPI_Pnt2d> thePoint);
76 //
77 ///// Get coincide edges for fillet
78 //static std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence);
79
80 SketchPlugin_ConstraintSplit::SketchPlugin_ConstraintSplit()
81 //: myListOfPointsChangedInCode(false),
82 //  myRadiusChangedByUser(false),
83 //  myRadiusChangedInCode(false),
84 //  myRadiusInitialized(false)
85 {
86 }
87
88 void SketchPlugin_ConstraintSplit::initAttributes()
89 {
90   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
91   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
92   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
93
94   //data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
95   //data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttrList::typeId());
96 }
97
98 void SketchPlugin_ConstraintSplit::execute()
99 {
100   std::shared_ptr<ModelAPI_Data> aData = data();
101
102   // Check the base objects are initialized.
103   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
104                                             aData->attribute(SketchPlugin_Constraint::VALUE()));
105   if(!aBaseObjectAttr->isInitialized()) {
106     setError("Error: Base object is not initialized.");
107     return;
108   }
109   AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
110   AttributePoint2DPtr aSecondPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
111   if (!aFirstPointAttr.get() || !aFirstPointAttr->isInitialized() ||
112       !aSecondPointAttr.get() || !aSecondPointAttr->isInitialized()) {
113     setError("Error: Sub-shape is not initialized.");
114     return;
115   }
116
117   // Wait all constraints being created, then send update events
118   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
119   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
120   if (isUpdateFlushed)
121     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
122
123
124   // Find feature constraints
125   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
126   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
127
128   std::set<FeaturePtr> aFeaturesToDelete;
129   std::map<FeaturePtr, IdToPointPair> aTangentFeatures;
130   std::map<FeaturePtr, IdToPointPair> aCoincidenceToFeature;
131   std::map<FeaturePtr, IdToPointPair> aCoincidenceToPoint;
132   getConstraints(aFeaturesToDelete, aTangentFeatures, aCoincidenceToFeature, aCoincidenceToPoint);
133
134
135   std::string aFeatureKind = aBaseFeature->getKind();
136   FeaturePtr aSplitFeature, anAfterFeature;
137   std::set<AttributePoint2DPtr> aFurtherCoincidences;
138   /*if (aFeatureKind == SketchPlugin_Line::ID())
139     splitLine(aSplitFeature, anOtherFeatures);
140   else*/ if (aFeatureKind == SketchPlugin_Arc::ID())
141     splitArc(aSplitFeature, aBaseFeature, anAfterFeature, aFurtherCoincidences);
142   /*if (aFeatureKind == SketchPlugin_Circle::ID())
143     splitCircle(aSplitFeature, anOtherFeatures);
144   FeaturePtr aSplitFeature;
145   std::set<FeaturePtr> anOtherFeatures;*/
146
147   //setConstraints(aSplitFeature, aBaseFeature, anAfterFeature, aFeaturesToDelete, aTangentFeatures,
148   //               aCoincidenceToFeature, aCoincidenceToPoint);
149   if (true) {
150   std::set<ResultPtr> aFeatureResults;
151   aFeatureResults.insert(getFeatureResult(aBaseFeature));
152   if (anAfterFeature.get() && anAfterFeature != aBaseFeature)
153     aFeatureResults.insert(getFeatureResult(anAfterFeature));
154
155   // coincidence to feature
156   std::map<FeaturePtr, IdToPointPair>::const_iterator aCIt = aCoincidenceToFeature.begin(),
157                                                             aCLast = aCoincidenceToFeature.end();
158   for (; aCIt != aCLast; aCIt++) {
159     FeaturePtr aCoincFeature = aCIt->first;
160     std::string anAttributeId = aCIt->second.first;
161     AttributePoint2DPtr aCoincPoint = aCIt->second.second;
162     std::set<AttributePoint2DPtr>::const_iterator aFCIt = aFurtherCoincidences.begin(),
163                                                   aFCLast = aFurtherCoincidences.end();
164     std::shared_ptr<GeomAPI_Pnt2d> aCoincPnt = aCoincPoint->pnt();
165     AttributePoint2DPtr aFeaturePointAttribute;
166     for (; aFCIt != aFCLast && !aFeaturePointAttribute.get(); aFCIt++) {
167       AttributePoint2DPtr aFCAttribute = *aFCIt;
168       if (aCoincPnt->isEqual(aFCAttribute->pnt()))
169         aFeaturePointAttribute = aFCAttribute;
170     }
171     if (aFeaturePointAttribute.get()) {
172       aCoincFeature->refattr(anAttributeId)->setAttr(aFeaturePointAttribute);
173     }
174     else {
175       /// find feature by shape intersected the point
176       ResultPtr aResultForCoincidence = *(aFeatureResults.begin());
177
178       if (aFeatureResults.size() > 1) { // try to find point on additional feature
179         ResultPtr anAddtionalResult = *(aFeatureResults.begin()++);
180         GeomShapePtr aShape = anAddtionalResult->shape();
181
182         std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = aCoincPoint->pnt();
183         std::shared_ptr<GeomAPI_Pnt> aPoint = sketch()->to3D(aPnt2d->x(), aPnt2d->y());
184
185         std::shared_ptr<GeomAPI_Pnt> aProjectedPoint;
186         if (ModelGeomAlgo_Point2D::isPointOnEdge(aShape, aPoint, aProjectedPoint))
187           aResultForCoincidence = anAddtionalResult;
188       }
189       aCoincFeature->refattr(anAttributeId)->setObject(aResultForCoincidence);
190     }
191   }
192   // coincidence to points
193   // TODO
194   // tangency
195   // TODO
196   }
197   // delete constraints
198   ModelAPI_Tools::removeFeaturesAndReferences(aFeaturesToDelete);
199
200   // Send events to update the sub-features by the solver.
201   if(isUpdateFlushed) {
202     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
203   }
204 }
205
206 bool SketchPlugin_ConstraintSplit::isMacro() const
207 {
208   return true;
209 }
210
211 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_ConstraintSplit::getPointOfRefAttr(
212                                                                   const AttributePtr& theAttribute)
213 {
214   AttributePoint2DPtr aPointAttribute;
215
216   if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
217     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
218     if (aRefAttr.get() && aRefAttr->isInitialized()) {
219       AttributePtr anAttribute = aRefAttr->attr();
220       if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
221         aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
222     }
223   }
224   return aPointAttribute;
225 }
226
227 void SketchPlugin_ConstraintSplit::getFeaturePoints(AttributePoint2DPtr& theStartPointAttr,
228                                                     AttributePoint2DPtr& theEndPointAttr)
229 {
230   AttributePoint2DPtr aPointAttribute;
231
232   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
233                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
234   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
235
236   std::string aFeatureKind = aBaseFeature->getKind();
237   std::string aStartAttributeName, anEndAttributeName;
238   if (aFeatureKind == SketchPlugin_Line::ID()) {
239     aStartAttributeName = SketchPlugin_Line::START_ID();
240     anEndAttributeName = SketchPlugin_Line::END_ID();
241   }
242   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
243     aStartAttributeName = SketchPlugin_Arc::START_ID();
244     anEndAttributeName = SketchPlugin_Arc::END_ID();
245   }
246   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
247     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
248                                                          aBaseFeature->attribute(aStartAttributeName));
249     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
250                                                          aBaseFeature->attribute(anEndAttributeName));
251   }
252 }
253
254 void SketchPlugin_ConstraintSplit::getConstraints(std::set<FeaturePtr>& theFeaturesToDelete,
255                                       std::map<FeaturePtr, IdToPointPair>& theTangentFeatures,
256                                       std::map<FeaturePtr, IdToPointPair>& theCoincidenceToFeature,
257                                       std::map<FeaturePtr, IdToPointPair>& theCoincidenceToPoint)
258 {
259   std::shared_ptr<ModelAPI_Data> aData = data();
260
261   // Check the base objects are initialized.
262   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
263                                             aData->attribute(SketchPlugin_Constraint::VALUE()));
264   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
265   ResultPtr aBaseFeatureResult = getFeatureResult(aBaseFeature);
266
267   const std::set<AttributePtr>& aRefsList = aBaseFeatureResult->data()->refsToMe();
268   std::set<AttributePtr>::const_iterator aIt;
269   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
270     std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
271     FeaturePtr aRefFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
272     std::string aRefFeatureKind = aRefFeature->getKind();
273     if (aRefFeatureKind == SketchPlugin_ConstraintMirror::ID() ||
274         aRefFeatureKind == SketchPlugin_MultiRotation::ID() ||
275         aRefFeatureKind == SketchPlugin_MultiTranslation::ID())
276       theFeaturesToDelete.insert(aRefFeature);
277     else if (aRefFeatureKind == SketchPlugin_ConstraintTangent::ID()) {
278       if (aBaseFeature->getKind() == SketchPlugin_Circle::ID()) /// TEMPORARY limitaion
279         theFeaturesToDelete.insert(aRefFeature); /// until tangency between arc and line is implemented
280       else {
281         std::string anAttributeToBeModified;
282         AttributePoint2DPtr aTangentPoint;
283         ObjectPtr aResult1 = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A())->object();
284         ObjectPtr aResult2 = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B())->object();
285         if (aResult1.get() && aResult2.get()) {
286           FeaturePtr aCoincidenceFeature = SketchPlugin_ConstraintCoincidence::findCoincidenceFeature
287                                                                     (ModelAPI_Feature::feature(aResult1),
288                                                                      ModelAPI_Feature::feature(aResult2));
289           aTangentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aCoincidenceFeature);
290         }
291         if (aTangentPoint.get()) {
292           FeaturePtr aFeature1 = ModelAPI_Feature::feature(aResult1);
293           std::string anAttributeToBeModified = aFeature1 == aBaseFeature
294                        ? SketchPlugin_Constraint::ENTITY_B() : SketchPlugin_Constraint::ENTITY_A();
295           theTangentFeatures[aRefFeature] = std::make_pair(anAttributeToBeModified, aTangentPoint);
296         }
297         else
298           theFeaturesToDelete.insert(aRefFeature); /// there is not coincident point between tangent constraint
299       }
300     }
301     else if (aRefFeatureKind == SketchPlugin_ConstraintCoincidence::ID()) {
302       std::string anAttributeToBeModified;
303       AttributePoint2DPtr aCoincidentPoint;
304       AttributeRefAttrPtr anAttrA = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_A());
305       AttributeRefAttrPtr anAttrB = aRefFeature->refattr(SketchPlugin_Constraint::ENTITY_B());
306       bool isToFeature = false;
307       if (anAttrA->isObject() || anAttrB->isObject()) { /// coincidence to base feature
308         FeaturePtr aFeature = anAttrA->isObject() ? ModelAPI_Feature::feature(anAttrA->object())
309                                                   : FeaturePtr();
310         isToFeature = aFeature.get() && aFeature == aBaseFeature;
311         anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_B();
312         if (!isToFeature) {
313           aFeature = anAttrB->isObject() ? ModelAPI_Feature::feature(anAttrB->object())
314                                          : FeaturePtr();
315           isToFeature = aFeature.get() && aFeature == aBaseFeature;
316           anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_A();
317         }
318         if (isToFeature)
319           aCoincidentPoint = SketchPlugin_ConstraintCoincidence::getPoint(aRefFeature);
320       }
321       if (!isToFeature) { /// coincidence to point on base feature
322         AttributePtr anAttribute;
323         if (!anAttrA->isObject()) {
324           AttributePtr aCurAttribute = anAttrA->attr();
325           if (aCurAttribute.get()) {
326             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
327             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
328               anAttribute = anAttrA->attr();
329               anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_A();
330             }
331           }
332         }
333         if (!anAttribute.get() && !anAttrB->isObject()) {
334           AttributePtr aCurAttribute = anAttrB->attr();
335           if (aCurAttribute.get()) {
336             FeaturePtr aCurFeature = ModelAPI_Feature::feature(aCurAttribute->owner());
337             if (aCurFeature.get() && aCurFeature == aBaseFeature) {
338               anAttribute = anAttrB->attr();
339               anAttributeToBeModified = SketchPlugin_Constraint::ENTITY_B();
340             }
341           }
342         }
343         if (anAttribute.get())
344           aCoincidentPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
345       }
346       if (aCoincidentPoint.get()) {
347         if (isToFeature)
348           theCoincidenceToFeature[aRefFeature] = std::make_pair(anAttributeToBeModified,
349                                                                 aCoincidentPoint);
350         else
351           theCoincidenceToPoint[aRefFeature] = std::make_pair(anAttributeToBeModified,
352                                                               aCoincidentPoint);
353       }
354       else
355         theFeaturesToDelete.insert(aRefFeature); /// this case should not happen
356     }
357   }
358 }
359
360 /*void SketchPlugin_ConstraintSplit::setConstraints(const FeaturePtr& theSplitFeature,
361                                                   const FeaturePtr& theBaseFeature,
362                                                   const FeaturePtr& theAfterFeature,
363     const std::set<std::shared_ptr<ModelAPI_Feature>>& theFeaturesToDelete,
364     const std::map<std::shared_ptr<ModelAPI_Feature>, std::shared_ptr<GeomDataAPI_Point2D> >& theTangentFeatures,
365     const std::map<std::shared_ptr<ModelAPI_Feature>, std::shared_ptr<GeomDataAPI_Point2D> >& theCoincidenceToFeature,
366     const std::map<std::shared_ptr<ModelAPI_Feature>, std::shared_ptr<GeomDataAPI_Point2D> >& theCoincidenceToPoint)
367 {
368   // coincidence to feature
369
370
371   // coincidence to points
372
373   // tangency
374
375   // delete constraints
376
377 }*/
378
379 void SketchPlugin_ConstraintSplit::splitArc(FeaturePtr& theSplitFeature,
380                                             FeaturePtr& theBaseFeature,
381                                             FeaturePtr& theAfterFeature,
382                                             std::set<AttributePoint2DPtr>& thePoints)
383 {
384   SketchPlugin_Sketch* aSketch = sketch();
385   if (!aSketch)
386     return;
387
388   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
389                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
390   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
391   std::string aFeatureKind = aBaseFeature->getKind();
392   if (aFeatureKind != SketchPlugin_Arc::ID())
393     return;
394
395   AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
396   AttributePoint2DPtr aSecondPointAttr = getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
397   AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
398   getFeaturePoints(aStartPointAttr, anEndPointAttr);
399   if (!aStartPointAttr.get() && !anEndPointAttr.get()) {
400     setError("Error: Feature has no start and end points.");
401     return;
402   }
403
404   arrangePoints(aStartPointAttr, anEndPointAttr, aFirstPointAttr, aSecondPointAttr);
405
406   /// split feature
407   theSplitFeature = createArcFeature(aBaseFeature, aFirstPointAttr, aSecondPointAttr);
408
409   // before split feature
410   if (!aStartPointAttr->pnt()->isEqual(aFirstPointAttr->pnt())) {
411     theBaseFeature = aBaseFeature; ///< use base feature to store all constraints here
412     /// move end arc point to start of split
413   }
414
415   // after split feature
416   if (!aSecondPointAttr->pnt()->isEqual(anEndPointAttr->pnt())) {
417     FeaturePtr aFeature;
418     if (!theBaseFeature.get()) {
419       aFeature = aBaseFeature; ///< use base feature to store all constraints here
420       fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttr);
421       aFeature->execute(); // to update result
422     }
423     else
424       aFeature = createArcFeature(aBaseFeature, aSecondPointAttr, anEndPointAttr);
425
426     createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
427                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
428                      aFeature->attribute(SketchPlugin_Arc::START_ID()));
429     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
430                                 (theBaseFeature->attribute(SketchPlugin_Arc::START_ID())));
431     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
432                                 (theBaseFeature->attribute(SketchPlugin_Arc::END_ID())));
433
434     if (!theBaseFeature.get())
435       theBaseFeature = aFeature;
436     else
437       theAfterFeature = aFeature;
438   }
439   else
440     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
441                                   (theSplitFeature->attribute(SketchPlugin_Arc::END_ID())));
442
443   // base split, that is defined before split feature should be changed at end
444   // (after the after feature creation). Otherwise modified value will be used in after feature
445   // before split feature
446   if (!aStartPointAttr->pnt()->isEqual(aFirstPointAttr->pnt())) {
447     /// move end arc point to start of split
448     fillAttribute(theBaseFeature->attribute(SketchPlugin_Arc::END_ID()), aFirstPointAttr);
449     theBaseFeature->execute(); // to update result
450     createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
451                      theBaseFeature->attribute(SketchPlugin_Arc::END_ID()),
452                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
453     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
454                                         (theBaseFeature->attribute(SketchPlugin_Arc::START_ID())));
455     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
456                                         (theBaseFeature->attribute(SketchPlugin_Arc::END_ID())));
457   }
458   else
459     thePoints.insert(std::dynamic_pointer_cast<GeomDataAPI_Point2D>
460                                        (theSplitFeature->attribute(SketchPlugin_Arc::START_ID())));
461
462   // additional constraints between split and base features
463   createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(), getFeatureResult(aBaseFeature),
464                                                        getFeatureResult(theSplitFeature));
465   createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(), getFeatureResult(theSplitFeature),
466                                                          getFeatureResult(aBaseFeature));
467   if (theAfterFeature.get()) {
468     createConstraintForObjects(SketchPlugin_ConstraintEqual::ID(), getFeatureResult(aBaseFeature),
469                                                          getFeatureResult(theAfterFeature));
470     createConstraintForObjects(SketchPlugin_ConstraintTangent::ID(), getFeatureResult(theSplitFeature),
471                                                          getFeatureResult(theAfterFeature));
472   }
473 }
474
475 void SketchPlugin_ConstraintSplit::arrangePoints(const AttributePoint2DPtr& theStartPointAttr,
476                                                  const AttributePoint2DPtr& theEndPointAttr,
477                                                  AttributePoint2DPtr& theFirstPointAttr,
478                                                  AttributePoint2DPtr& theLastPointAttr)
479 {
480   /// if first point is closer to last point, wrap first and last values
481   if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
482       theStartPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
483     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
484     theFirstPointAttr = theLastPointAttr;
485     theLastPointAttr = aTmpPoint;
486   }
487 }
488
489 void SketchPlugin_ConstraintSplit::fillAttribute(const AttributePtr& theModifiedAttribute,
490                                                  const AttributePtr& theSourceAttribute)
491 {
492   AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
493                                             theModifiedAttribute);
494   AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
495                                             theSourceAttribute);
496
497   if (aModifiedAttribute.get() && aSourceAttribute.get())
498     aModifiedAttribute->setValue(aSourceAttribute->pnt());
499 }
500
501 FeaturePtr SketchPlugin_ConstraintSplit::createArcFeature(const FeaturePtr& theBaseFeature,
502                                                           const AttributePtr& theFirstPointAttr,
503                                                           const AttributePtr& theSecondPointAttr)
504 {
505   FeaturePtr aFeature;
506   SketchPlugin_Sketch* aSketch = sketch();
507   if (!aSketch || !theBaseFeature.get())
508     return aFeature;
509
510   aFeature = aSketch->addFeature(theBaseFeature->getKind());
511   // update fillet arc: make the arc correct for sure, so, it is not needed to process the "attribute updated"
512   // by arc; moreover, it may cause cyclicity in hte mechanism of updater
513   aFeature->data()->blockSendAttributeUpdated(true);
514
515   aFeature->string(SketchPlugin_Arc::ARC_TYPE())->setValue(
516                 SketchPlugin_Arc::ARC_TYPE_CENTER_START_END());
517   fillAttribute(aFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
518                 theBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
519   fillAttribute(aFeature->attribute(SketchPlugin_Arc::START_ID()), theFirstPointAttr);
520   fillAttribute(aFeature->attribute(SketchPlugin_Arc::END_ID()), theSecondPointAttr);
521   aFeature->data()->blockSendAttributeUpdated(false);
522   aFeature->execute(); // to obtain result
523
524   return aFeature;
525 }
526
527 void SketchPlugin_ConstraintSplit::createConstraint(const std::string& theConstraintId,
528                                                     const AttributePtr& theFirstAttribute,
529                                                     const AttributePtr& theSecondAttribute)
530 {
531   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
532   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
533                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
534   aRefAttr->setAttr(theFirstAttribute);
535
536   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
537                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
538   aRefAttr->setAttr(theSecondAttribute);
539 }
540
541 void SketchPlugin_ConstraintSplit::createConstraintForObjects(const std::string& theConstraintId,
542                                                     const ObjectPtr& theFirstObject,
543                                                     const ObjectPtr& theSecondObject)
544 {
545   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
546   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
547                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
548   aRefAttr->setObject(theFirstObject);
549
550   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
551                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
552   aRefAttr->setObject(theSecondObject);
553 }
554
555 std::shared_ptr<ModelAPI_Result> SketchPlugin_ConstraintSplit::getFeatureResult(
556                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
557 {
558   std::shared_ptr<ModelAPI_Result> aResult;
559
560   std::string aFeatureKind = theFeature->getKind();
561   if (aFeatureKind == SketchPlugin_Line::ID())
562     aResult = theFeature->firstResult();
563   else if (aFeatureKind == SketchPlugin_Arc::ID())
564     aResult = theFeature->lastResult();
565   else if (aFeatureKind == SketchPlugin_Circle::ID())
566     aResult = theFeature->lastResult();
567
568   return aResult;
569 }
570