]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_ConstraintSplit.cpp
Salome HOME
1b59de5358d20f23d0bf495597ec40b6fd6cd685
[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
21 #include <SketchPlugin_Line.h>
22 #include <SketchPlugin_Arc.h>
23 #include <SketchPlugin_Circle.h>
24 #include <SketchPlugin_ConstraintCoincidence.h>
25 #include <SketchPlugin_ConstraintEqual.h>
26 #include <SketchPlugin_ConstraintParallel.h>
27 #include <SketchPlugin_ConstraintTangent.h>
28
29 //#include <ModelAPI_Data.h>
30 #include <ModelAPI_Events.h>
31 //#include <ModelAPI_Session.h>
32 //#include <ModelAPI_Validator.h>
33 //
34 //#include <SketchPlugin_Arc.h>
35 //#include <SketchPlugin_Line.h>
36 //#include <SketchPlugin_Point.h>
37 //#include <SketchPlugin_Sketch.h>
38 //#include <SketchPlugin_ConstraintCoincidence.h>
39 //#include <SketchPlugin_ConstraintTangent.h>
40 //#include <SketchPlugin_ConstraintRadius.h>
41 //#include <SketchPlugin_Tools.h>
42 //
43 #include <Events_Loop.h>
44 //
45 //#include <math.h>
46 //
47 //const double tolerance = 1.e-7;
48 //const double paramTolerance = 1.e-4;
49
50 ///// \brief Attract specified point on theNewArc to the attribute of theFeature
51 //static void recalculateAttributes(FeaturePtr theNewArc, const std::string& theNewArcAttribute,
52 //  FeaturePtr theFeature, const std::string& theFeatureAttribute);
53 //
54 ///// \brief Calculates center of fillet arc and coordinates of tangency points
55 //static void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
56 //                                  double theRadius, bool theNotInversed[2],
57 //                                  std::shared_ptr<GeomAPI_XY>& theCenter,
58 //                                  std::shared_ptr<GeomAPI_XY>& theTangentA,
59 //                                  std::shared_ptr<GeomAPI_XY>& theTangentB);
60 //
61 ///// Get point on 1/3 length of edge from fillet point
62 //static void getPointOnEdge(const FeaturePtr theFeature,
63 //                           const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
64 //                           std::shared_ptr<GeomAPI_Pnt2d>& thePoint);
65 //
66 ///// Get distance from point to feature
67 //static double getProjectionDistance(const FeaturePtr theFeature,
68 //                             const std::shared_ptr<GeomAPI_Pnt2d> thePoint);
69 //
70 ///// Get coincide edges for fillet
71 //static std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence);
72
73 SketchPlugin_ConstraintSplit::SketchPlugin_ConstraintSplit()
74 //: myListOfPointsChangedInCode(false),
75 //  myRadiusChangedByUser(false),
76 //  myRadiusChangedInCode(false),
77 //  myRadiusInitialized(false)
78 {
79 }
80
81 void SketchPlugin_ConstraintSplit::initAttributes()
82 {
83   data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeReference::typeId());
84   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
85   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
86
87   //data()->addAttribute(SketchPlugin_Constraint::VALUE(), ModelAPI_AttributeDouble::typeId());
88   //data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttrList::typeId());
89 }
90
91 void SketchPlugin_ConstraintSplit::execute()
92 {
93   std::shared_ptr<ModelAPI_Data> aData = data();
94
95   // Check the base objects are initialized.
96   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
97                                             aData->attribute(SketchPlugin_Constraint::VALUE()));
98   if(!aBaseObjectAttr->isInitialized()) {
99     setError("Error: Base object is not initialized.");
100     return;
101   }
102   AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
103   AttributePoint2DPtr aSecondPointAttr = getPointOfRefAttr(aData->attribute(SketchPlugin_Constraint::ENTITY_B()));
104   if (!aFirstPointAttr.get() || !aFirstPointAttr->isInitialized() ||
105       !aSecondPointAttr.get() || !aSecondPointAttr->isInitialized()) {
106     setError("Error: Sub-shape is not initialized.");
107     return;
108   }
109
110   // Wait all constraints being created, then send update events
111   static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
112   bool isUpdateFlushed = Events_Loop::loop()->isFlushed(anUpdateEvent);
113   if (isUpdateFlushed)
114     Events_Loop::loop()->setFlushed(anUpdateEvent, false);
115
116
117   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
118   std::string aFeatureKind = aBaseFeature->getKind();
119   FeaturePtr aSplitFeature, aBeforeFeature, anAfterFeature;
120   /*if (aFeatureKind == SketchPlugin_Line::ID())
121     splitLine(aSplitFeature, anOtherFeatures);
122   else*/ if (aFeatureKind == SketchPlugin_Arc::ID())
123     splitArc(aSplitFeature, aBeforeFeature, anAfterFeature);
124   /*if (aFeatureKind == SketchPlugin_Circle::ID())
125     splitCircle(aSplitFeature, anOtherFeatures);
126   FeaturePtr aSplitFeature;
127   std::set<FeaturePtr> anOtherFeatures;*/
128
129   // Send events to update the sub-features by the solver.
130   if(isUpdateFlushed) {
131     Events_Loop::loop()->setFlushed(anUpdateEvent, true);
132   }
133 }
134
135 void SketchPlugin_ConstraintSplit::attributeChanged(const std::string& theID)
136 {
137 /*  if(theID == SketchPlugin_Constraint::ENTITY_A()) {
138     if(myListOfPointsChangedInCode) {
139       return;
140     }
141
142     // Clear results.
143     clearResults();
144
145     // Clear list of new points.
146     myNewPoints.clear();
147
148     // Get list of points for fillets and current radius.
149     AttributeRefAttrListPtr aRefListOfFilletPoints = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttrList>(
150       data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
151     AttributeDoublePtr aRadiusAttribute = real(VALUE());
152     int aListSize = aRefListOfFilletPoints->size();
153     if(aListSize == 0 && !myRadiusChangedByUser) {
154       // If list is empty reset radius to zero (if it was not changed by user).
155       myRadiusChangedInCode = true;
156       aRadiusAttribute->setValue(0);
157       myRadiusChangedInCode = false;
158       return;
159     }
160
161     // Iterate over points to get base lines an calculate radius for fillets.
162     double aMinimumRadius = 0;
163     std::list<std::pair<ObjectPtr, AttributePtr>> aSelectedPointsList = aRefListOfFilletPoints->list();
164     std::list<std::pair<ObjectPtr, AttributePtr>>::iterator anIter = aSelectedPointsList.begin();
165     std::set<AttributePtr> aPointsToSkeep;
166     for(int anIndex = 0; anIndex < aListSize; anIndex++, anIter++) {
167       AttributePtr aFilletPointAttr = (*anIter).second;
168       std::shared_ptr<GeomDataAPI_Point2D> aFilletPoint2D =
169         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aFilletPointAttr);
170       if(!aFilletPoint2D.get()) {
171         myNewPoints.clear();
172         setError("Error: One of the selected points is invalid.");
173         return;
174       }
175
176       // If point or coincident point is already in list remove it from attribute.
177       if(aPointsToSkeep.find(aFilletPointAttr) != aPointsToSkeep.end()) {
178         myListOfPointsChangedInCode = true;
179         aRefListOfFilletPoints->remove(aFilletPointAttr);
180         myListOfPointsChangedInCode = false;
181         continue;
182       }
183
184       // Obtain constraint coincidence for the fillet point.
185       FeaturePtr aConstraintCoincidence;
186       const std::set<AttributePtr>& aRefsList = aFilletPointAttr->owner()->data()->refsToMe();
187       for(std::set<AttributePtr>::const_iterator anIt = aRefsList.cbegin(); anIt != aRefsList.cend(); ++anIt) {
188         std::shared_ptr<ModelAPI_Attribute> anAttr = (*anIt);
189         FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anAttr->owner());
190         if(aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
191           AttributeRefAttrPtr anAttrRefA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
192             aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_A()));
193           AttributeRefAttrPtr anAttrRefB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
194             aConstrFeature->attribute(SketchPlugin_ConstraintCoincidence::ENTITY_B()));
195           if(anAttrRefA.get()) {
196             AttributePtr anAttrA = anAttrRefA->attr();
197             if(aFilletPointAttr == anAttrA) {
198               aConstraintCoincidence = aConstrFeature;
199               break;
200             }
201           }
202           if(anAttrRefB.get()) {
203             AttributePtr anAttrB = anAttrRefB->attr();
204             if(aFilletPointAttr == anAttrB) {
205               aConstraintCoincidence = aConstrFeature;
206               break;
207             }
208           }
209         }
210       }
211
212       if(!aConstraintCoincidence.get()) {
213         myNewPoints.clear();
214         setError("Error: No coincident edges at one of the selected points.");
215         return;
216       }
217
218       // Get coincides from constraint.
219       std::set<FeaturePtr> aCoincides;
220
221
222       SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
223                                            SketchPlugin_ConstraintCoincidence::ENTITY_A(),
224                                            aCoincides);
225       SketchPlugin_Tools::findCoincidences(aConstraintCoincidence,
226                                            SketchPlugin_ConstraintCoincidence::ENTITY_B(),
227                                            aCoincides);
228
229       // Remove points from set of coincides. Also get all attributes which is equal to this point to exclude it.
230       std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
231       std::set<FeaturePtr> aNewSetOfCoincides;
232       for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
233         std::string aFeatureKind = (*anIt)->getKind();
234         if(aFeatureKind == SketchPlugin_Point::ID()) {
235           AttributePtr anAttr = (*anIt)->attribute(SketchPlugin_Point::COORD_ID());
236           std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
237             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
238           if(aPoint2D.get() && aFilletPnt2d->isEqual(aPoint2D->pnt())) {
239             aPointsToSkeep.insert(anAttr);
240           }
241         } else if(aFeatureKind == SketchPlugin_Line::ID()) {
242           AttributePtr anAttrStart = (*anIt)->attribute(SketchPlugin_Line::START_ID());
243           std::shared_ptr<GeomDataAPI_Point2D> aPointStart2D =
244             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrStart);
245           if(aPointStart2D.get() && aFilletPnt2d->isEqual(aPointStart2D->pnt())) {
246             aPointsToSkeep.insert(anAttrStart);
247           }
248           AttributePtr anAttrEnd = (*anIt)->attribute(SketchPlugin_Line::END_ID());
249           std::shared_ptr<GeomDataAPI_Point2D> aPointEnd2D =
250             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrEnd);
251           if(aPointEnd2D.get() && aFilletPnt2d->isEqual(aPointEnd2D->pnt())) {
252             aPointsToSkeep.insert(anAttrEnd);
253           }
254           aNewSetOfCoincides.insert(*anIt);
255         } else if(aFeatureKind == SketchPlugin_Arc::ID() ) {
256           AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
257           std::shared_ptr<GeomDataAPI_Point2D> aPointCenter2D =
258             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrCenter);
259           if(aPointCenter2D.get() && aFilletPnt2d->isEqual(aPointCenter2D->pnt())) {
260             aPointsToSkeep.insert(anAttrCenter);
261             continue;
262           }
263           AttributePtr anAttrStart = (*anIt)->attribute(SketchPlugin_Arc::START_ID());
264           std::shared_ptr<GeomDataAPI_Point2D> aPointStart2D =
265             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrStart);
266           if(aPointStart2D.get() && aFilletPnt2d->isEqual(aPointStart2D->pnt())) {
267             aPointsToSkeep.insert(anAttrStart);
268           }
269           AttributePtr anAttrEnd = (*anIt)->attribute(SketchPlugin_Arc::END_ID());
270           std::shared_ptr<GeomDataAPI_Point2D> aPointEnd2D =
271             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrEnd);
272           if(aPointEnd2D.get() && aFilletPnt2d->isEqual(aPointEnd2D->pnt())) {
273             aPointsToSkeep.insert(anAttrEnd);
274           }
275           aNewSetOfCoincides.insert(*anIt);
276         }
277       }
278       aCoincides = aNewSetOfCoincides;
279
280       // If we still have more than two coincides remove auxilary entities from set of coincides.
281       if(aCoincides.size() > 2) {
282         aNewSetOfCoincides.clear();
283         for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
284           if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
285             aNewSetOfCoincides.insert(*anIt);
286           }
287         }
288         aCoincides = aNewSetOfCoincides;
289       }
290
291       if(aCoincides.size() != 2) {
292         myNewPoints.clear();
293         setError("Error: One of the selected points does not have two suitable edges for fillet.");
294         return;
295       }
296
297       // Store base point for fillet.
298       aPointsToSkeep.insert(aFilletPointAttr);
299       myNewPoints.insert(aFilletPointAttr);
300
301       // Get base lines for fillet.
302       FeaturePtr anOldFeatureA, anOldFeatureB;
303       std::set<FeaturePtr>::iterator aLinesIt = aCoincides.begin();
304       anOldFeatureA = *aLinesIt++;
305       anOldFeatureB = *aLinesIt;
306
307       // Getting radius value if it was not changed by user.
308       if(!myRadiusChangedByUser) {
309         // Getting points located at 1/3 of edge length from fillet point.
310         std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt2d = aFilletPoint2D->pnt();
311         std::shared_ptr<GeomAPI_Pnt2d> aPntA, aPntB;
312         getPointOnEdge(anOldFeatureA, aFilletPnt2d, aPntA);
313         getPointOnEdge(anOldFeatureB, aFilletPnt2d, aPntB);
314
315         /// Getting distances.
316         double aDistanceA = getProjectionDistance(anOldFeatureB, aPntA);
317         double aDistanceB = getProjectionDistance(anOldFeatureA, aPntB);
318         double aRadius = aDistanceA < aDistanceB ? aDistanceA / 2.0 : aDistanceB / 2.0;
319         aMinimumRadius = aMinimumRadius == 0 ? aRadius : aRadius < aMinimumRadius ? aRadius : aMinimumRadius;
320       }
321     }
322
323     // Set new default radius if it was not changed by user.
324     if(!myRadiusChangedByUser) {
325       myRadiusChangedInCode = true;
326       aRadiusAttribute->setValue(aMinimumRadius);
327       myRadiusChangedInCode = false;
328     }
329
330   } else if(theID == SketchPlugin_Constraint::VALUE()) {
331     if(myRadiusInitialized && !myRadiusChangedInCode) {
332       myRadiusChangedByUser = true;
333     }
334     if(!myRadiusInitialized) {
335       myRadiusInitialized = true;
336     }
337   }
338 */
339 }
340
341 //AISObjectPtr SketchPlugin_ConstraintSplit::getAISObject(AISObjectPtr thePrevious)
342 //{
343 //  if (!sketch())
344 //    return thePrevious;
345 //
346 //  AISObjectPtr anAIS = thePrevious;
347 //  /// TODO: Equal constraint presentation should be put here
348 //  return anAIS;
349 //}
350
351 bool SketchPlugin_ConstraintSplit::isMacro() const
352 {
353   return true;
354 }
355
356 //void SketchPlugin_ConstraintSplit::clearResults()
357 //{
358 ///*  // Clear auxiliary flag on initial objects.
359 //  for(std::map<AttributePtr, FilletFeatures>::iterator aPointsIter = myPointFeaturesMap.begin();
360 //      aPointsIter != myPointFeaturesMap.end();) {
361 //    const FilletFeatures& aFilletFeatures = aPointsIter->second;
362 //    std::list<std::pair<FeaturePtr, bool>>::const_iterator aFeatureIt;
363 //    for(aFeatureIt = aFilletFeatures.baseEdgesState.cbegin();
364 //        aFeatureIt != aFilletFeatures.baseEdgesState.cend();
365 //        ++aFeatureIt) {
366 //      aFeatureIt->first->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->setValue(aFeatureIt->second);
367 //    }
368 //    ++aPointsIter;
369 //  }
370 //
371 //  // And remove all produced features.
372 //  DocumentPtr aDoc = sketch()->document();
373 //  for(std::map<AttributePtr, FilletFeatures>::iterator aPointsIter = myPointFeaturesMap.begin();
374 //      aPointsIter != myPointFeaturesMap.end();) {
375 //    // Remove all produced constraints.
376 //    const FilletFeatures& aFilletFeatures = aPointsIter->second;
377 //    std::list<FeaturePtr>::const_iterator aFeatureIt;
378 //    for(aFeatureIt = aFilletFeatures.resultConstraints.cbegin();
379 //        aFeatureIt != aFilletFeatures.resultConstraints.cend();
380 //        ++aFeatureIt) {
381 //      aDoc->removeFeature(*aFeatureIt);
382 //    }
383 //
384 //    // Remove all result edges.
385 //    for(aFeatureIt = aFilletFeatures.resultEdges.cbegin();
386 //        aFeatureIt != aFilletFeatures.resultEdges.cend();
387 //        ++aFeatureIt) {
388 //      aDoc->removeFeature(*aFeatureIt);
389 //    }
390 //
391 //    // Remove point from map.
392 //    myPointFeaturesMap.erase(aPointsIter++);
393 //  }
394 //*/
395 //};
396 //
397 //
398 //// =========   Auxiliary functions   =================
399 //void recalculateAttributes(FeaturePtr theNewArc,  const std::string& theNewArcAttribute,
400 //                           FeaturePtr theFeature, const std::string& theFeatureAttribute)
401 //{
402 //  std::shared_ptr<GeomAPI_Pnt2d> anArcPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
403 //      theNewArc->attribute(theNewArcAttribute))->pnt();
404 //  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
405 //      theFeature->attribute(theFeatureAttribute))->setValue(anArcPoint->x(), anArcPoint->y());
406 //}
407
408 /*/// \brief Find intersections of lines shifted along normal direction
409 void possibleFilletCenterLineLine(
410     std::shared_ptr<GeomAPI_XY> thePointA, std::shared_ptr<GeomAPI_Dir2d> theDirA,
411     std::shared_ptr<GeomAPI_XY> thePointB, std::shared_ptr<GeomAPI_Dir2d> theDirB,
412     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
413 {
414   std::shared_ptr<GeomAPI_Dir2d> aDirAT(new GeomAPI_Dir2d(-theDirA->y(), theDirA->x()));
415   std::shared_ptr<GeomAPI_Dir2d> aDirBT(new GeomAPI_Dir2d(-theDirB->y(), theDirB->x()));
416   std::shared_ptr<GeomAPI_XY> aPntA, aPntB;
417   double aDet = theDirA->cross(theDirB);
418   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
419     aPntA = thePointA->added(aDirAT->xy()->multiplied(aStepA * theRadius));
420     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
421       aPntB = thePointB->added(aDirBT->xy()->multiplied(aStepB * theRadius));
422       double aVX = aDirAT->xy()->dot(aPntA);
423       double aVY = aDirBT->xy()->dot(aPntB);
424       std::shared_ptr<GeomAPI_XY> aPoint(new GeomAPI_XY(
425           (theDirB->x() * aVX - theDirA->x() * aVY) / aDet,
426           (theDirB->y() * aVX - theDirA->y() * aVY) / aDet));
427       theCenters.push_back(aPoint);
428     }
429   }
430 }
431
432 /// \brief Find intersections of line shifted along normal direction in both sides
433 ///        and a circle with extended radius
434 void possibleFilletCenterLineArc(
435     std::shared_ptr<GeomAPI_XY> theStartLine, std::shared_ptr<GeomAPI_Dir2d> theDirLine,
436     std::shared_ptr<GeomAPI_XY> theCenterArc, double theRadiusArc,
437     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
438 {
439   std::shared_ptr<GeomAPI_Dir2d> aDirT(new GeomAPI_Dir2d(-theDirLine->y(), theDirLine->x()));
440   std::shared_ptr<GeomAPI_XY> aPnt;
441   double aDirNorm2 = theDirLine->dot(theDirLine);
442   double aRad = 0.0;
443   double aDirX = theDirLine->x();
444   double aDirX2 = theDirLine->x() * theDirLine->x();
445   double aDirY2 = theDirLine->y() * theDirLine->y();
446   double aDirXY = theDirLine->x() * theDirLine->y();
447   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
448     aPnt = theStartLine->added(aDirT->xy()->multiplied(aStepA * theRadius));
449     double aCoeff = aDirT->xy()->dot(aPnt->decreased(theCenterArc));
450     double aCoeff2 = aCoeff * aCoeff;
451     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
452       aRad = theRadiusArc + aStepB * theRadius;
453       double aD = aRad * aRad * aDirNorm2 - aCoeff2;
454       if (aD < 0.0)
455         continue;
456       double aDs = sqrt(aD);
457       double x1 = theCenterArc->x() + (aCoeff * aDirT->x() - aDirT->y() * aDs) / aDirNorm2;
458       double x2 = theCenterArc->x() + (aCoeff * aDirT->x() + aDirT->y() * aDs) / aDirNorm2;
459       double y1 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
460           aDirXY * (aPnt->x() - theCenterArc->x()) - theDirLine->y() * aDs) / aDirNorm2;
461       double y2 = (aDirX2 * aPnt->y() + aDirY2 * theCenterArc->y() -
462           aDirXY * (aPnt->x() - theCenterArc->x()) + theDirLine->y() * aDs) / aDirNorm2;
463
464       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
465       theCenters.push_back(aPoint1);
466       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
467       theCenters.push_back(aPoint2);
468     }
469   }
470 }
471
472 /// \brief Find intersections of two circles with extended radii
473 void possibleFilletCenterArcArc(
474     std::shared_ptr<GeomAPI_XY> theCenterA, double theRadiusA,
475     std::shared_ptr<GeomAPI_XY> theCenterB, double theRadiusB,
476     double theRadius, std::list< std::shared_ptr<GeomAPI_XY> >& theCenters)
477 {
478   std::shared_ptr<GeomAPI_XY> aCenterDir = theCenterB->decreased(theCenterA);
479   double aCenterDist2 = aCenterDir->dot(aCenterDir);
480   double aCenterDist = sqrt(aCenterDist2);
481
482   double aRadA, aRadB;
483   for (double aStepA = -1.0; aStepA <= 1.0; aStepA += 2.0) {
484     aRadA = theRadiusA + aStepA * theRadius;
485     for (double aStepB = -1.0; aStepB <= 1.0; aStepB += 2.0) {
486       aRadB = theRadiusB + aStepB * theRadius;
487       if (aRadA + aRadB < aCenterDist || fabs(aRadA - aRadB) > aCenterDist)
488         continue; // there is no intersections
489
490       double aMedDist = (aRadA * aRadA - aRadB * aRadB + aCenterDist2) / (2.0 * aCenterDist);
491       double aHeight = sqrt(aRadA * aRadA - aMedDist * aMedDist);
492
493       double x1 = theCenterA->x() + (aMedDist * aCenterDir->x() + aCenterDir->y() * aHeight) / aCenterDist;
494       double y1 = theCenterA->y() + (aMedDist * aCenterDir->y() - aCenterDir->x() * aHeight) / aCenterDist;
495
496       double x2 = theCenterA->x() + (aMedDist * aCenterDir->x() - aCenterDir->y() * aHeight) / aCenterDist;
497       double y2 = theCenterA->y() + (aMedDist * aCenterDir->y() + aCenterDir->x() * aHeight) / aCenterDist;
498
499       std::shared_ptr<GeomAPI_XY> aPoint1(new GeomAPI_XY(x1, y1));
500       theCenters.push_back(aPoint1);
501       std::shared_ptr<GeomAPI_XY> aPoint2(new GeomAPI_XY(x2, y2));
502       theCenters.push_back(aPoint2);
503     }
504   }
505 }
506
507 void calculateFilletCenter(FeaturePtr theFeatureA, FeaturePtr theFeatureB,
508                            double theRadius, bool theNotInversed[2],
509                            std::shared_ptr<GeomAPI_XY>& theCenter,
510                            std::shared_ptr<GeomAPI_XY>& theTangentA,
511                            std::shared_ptr<GeomAPI_XY>& theTangentB)
512 {
513   static const int aNbFeatures = 2;
514   FeaturePtr aFeature[aNbFeatures] = {theFeatureA, theFeatureB};
515   std::shared_ptr<GeomAPI_XY> aStart[aNbFeatures], aEnd[aNbFeatures], aCenter[aNbFeatures];
516   std::shared_ptr<GeomDataAPI_Point2D> aStartPoint, aEndPoint;
517
518   for (int i = 0; i < aNbFeatures; i++) {
519     if (aFeature[i]->getKind() == SketchPlugin_Line::ID()) {
520       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
521           aFeature[i]->attribute(SketchPlugin_Line::START_ID()));
522       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
523           aFeature[i]->attribute(SketchPlugin_Line::END_ID()));
524     } else if (aFeature[i]->getKind() == SketchPlugin_Arc::ID()) {
525       aStartPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
526           aFeature[i]->attribute(SketchPlugin_Arc::START_ID()));
527       aEndPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
528           aFeature[i]->attribute(SketchPlugin_Arc::END_ID()));
529       aCenter[i] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
530           aFeature[i]->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt()->xy();
531     } else
532       return;
533     aStart[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
534         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()) :
535         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()));
536     aEnd[i] = std::shared_ptr<GeomAPI_XY>(theNotInversed[i] ?
537         new GeomAPI_XY(aEndPoint->x(), aEndPoint->y()) :
538         new GeomAPI_XY(aStartPoint->x(), aStartPoint->y()));
539   }
540
541   if (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
542       theFeatureB->getKind() == SketchPlugin_Line::ID()) {
543     std::shared_ptr<GeomAPI_Dir2d> aDir[2];
544     std::shared_ptr<GeomAPI_Dir2d> aDirT[2];
545     for (int i = 0; i < aNbFeatures; i++) {
546       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(aEnd[i]->decreased(aStart[i])));
547       aDirT[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(-aDir[i]->y(), aDir[i]->x()));
548     }
549
550     // get and filter possible centers
551     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
552     possibleFilletCenterLineLine(aStart[0], aDir[0], aStart[1], aDir[1], theRadius, aSuspectCenters);
553     double aDot = 0.0;
554     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
555     for (; anIt != aSuspectCenters.end(); anIt++) {
556       aDot = aDirT[0]->xy()->dot(aStart[0]->decreased(*anIt));
557       theTangentA = (*anIt)->added(aDirT[0]->xy()->multiplied(aDot));
558       if (theTangentA->decreased(aStart[0])->dot(aDir[0]->xy()) < 0.0)
559         continue; // incorrect position
560       aDot = aDirT[1]->xy()->dot(aStart[1]->decreased(*anIt));
561       theTangentB = (*anIt)->added(aDirT[1]->xy()->multiplied(aDot));
562       if (theTangentB->decreased(aStart[1])->dot(aDir[1]->xy()) < 0.0)
563         continue; // incorrect position
564       // the center is found, stop searching
565       theCenter = *anIt;
566       return;
567     }
568   } else if ((theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
569       theFeatureB->getKind() == SketchPlugin_Line::ID()) || 
570       (theFeatureA->getKind() == SketchPlugin_Line::ID() &&
571       theFeatureB->getKind() == SketchPlugin_Arc::ID())) {
572     int aLineInd = theFeatureA->getKind() == SketchPlugin_Line::ID() ? 0 : 1;
573     double anArcRadius = aStart[1-aLineInd]->distance(aCenter[1-aLineInd]);
574     std::shared_ptr<GeomAPI_Dir2d> aDirLine = std::shared_ptr<GeomAPI_Dir2d>(
575         new GeomAPI_Dir2d(aEnd[aLineInd]->decreased(aStart[aLineInd])));
576     std::shared_ptr<GeomAPI_Dir2d> aDirT = std::shared_ptr<GeomAPI_Dir2d>(
577         new GeomAPI_Dir2d(-aDirLine->y(), aDirLine->x()));
578
579     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir = std::shared_ptr<GeomAPI_Dir2d>(
580         new GeomAPI_Dir2d(aStart[1-aLineInd]->decreased(aCenter[1-aLineInd])));
581     std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
582         new GeomAPI_Dir2d(aEnd[1-aLineInd]->decreased(aCenter[1-aLineInd])));
583     double anArcAngle = aEndArcDir->angle(aStartArcDir);
584
585     // get possible centers and filter them
586     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
587     possibleFilletCenterLineArc(aStart[aLineInd], aDirLine, aCenter[1-aLineInd], anArcRadius, theRadius, aSuspectCenters);
588     double aDot = 0.0;
589     // the line is forward into the arc
590     double innerArc = aCenter[1-aLineInd]->decreased(aStart[aLineInd])->dot(aDirLine->xy());
591     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
592     // The possible centers are ranged by their positions.
593     // If the point is not satisfy one of criteria, the weight is decreased with penalty.
594     int aBestWeight = 0;
595     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
596     for (; anIt != aSuspectCenters.end(); anIt++) {
597       int aWeight = 2;
598       aDot = aDirT->xy()->dot(aStart[aLineInd]->decreased(*anIt));
599       aLineTgPoint = (*anIt)->added(aDirT->xy()->multiplied(aDot));
600       // Check the point is placed on the correct arc (penalty if false)
601       if (aCenter[1-aLineInd]->distance(*anIt) * innerArc > anArcRadius * innerArc)
602         aWeight -= 1;
603       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
604           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1-aLineInd])));
605       double aCurAngle = aCurDir->angle(aStartArcDir);
606       if (anArcAngle < 0.0) aCurAngle *= -1.0;
607       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle))
608         continue;
609       if (aWeight > aBestWeight)
610         aBestWeight = aWeight;
611       else if (aWeight < aBestWeight ||
612                aStart[aLineInd]->distance(*anIt) >
613                aStart[aLineInd]->distance(theCenter)) // <-- take closer point
614         continue;
615       // the center is found, stop searching
616       theCenter = *anIt;
617       anArcTgPoint = aCenter[1-aLineInd]->added(aCurDir->xy()->multiplied(anArcRadius));
618       if (theFeatureA->getKind() == SketchPlugin_Line::ID()) {
619         theTangentA = aLineTgPoint;
620         theTangentB = anArcTgPoint;
621       } else {
622         theTangentA = anArcTgPoint;
623         theTangentB = aLineTgPoint;
624       }
625       //return;
626     }
627   } else if (theFeatureA->getKind() == SketchPlugin_Arc::ID() &&
628       theFeatureB->getKind() == SketchPlugin_Arc::ID()) {
629     double anArcRadius[aNbFeatures];
630     double anArcAngle[aNbFeatures];
631     std::shared_ptr<GeomAPI_Dir2d> aStartArcDir[aNbFeatures];
632     for (int i = 0; i < aNbFeatures; i++) {
633       anArcRadius[i] = aStart[i]->distance(aCenter[i]);
634       aStartArcDir[i] = std::shared_ptr<GeomAPI_Dir2d>(
635           new GeomAPI_Dir2d(aStart[i]->decreased(aCenter[i])));
636       std::shared_ptr<GeomAPI_Dir2d> aEndArcDir = std::shared_ptr<GeomAPI_Dir2d>(
637           new GeomAPI_Dir2d(aEnd[i]->decreased(aCenter[i])));
638       anArcAngle[i] = aEndArcDir->angle(aStartArcDir[i]);
639     }
640
641     // get and filter possible centers
642     std::list< std::shared_ptr<GeomAPI_XY> > aSuspectCenters;
643     possibleFilletCenterArcArc(aCenter[0], anArcRadius[0], aCenter[1], anArcRadius[1], theRadius, aSuspectCenters);
644     double aDot = 0.0;
645     std::shared_ptr<GeomAPI_XY> aLineTgPoint, anArcTgPoint;
646     std::list< std::shared_ptr<GeomAPI_XY> >::iterator anIt = aSuspectCenters.begin();
647     for (; anIt != aSuspectCenters.end(); anIt++) {
648       std::shared_ptr<GeomAPI_Dir2d> aCurDir = std::shared_ptr<GeomAPI_Dir2d>(
649           new GeomAPI_Dir2d((*anIt)->decreased(aCenter[0])));
650       double aCurAngle = aCurDir->angle(aStartArcDir[0]);
651       if (anArcAngle[0] < 0.0) aCurAngle *= -1.0;
652       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[0]))
653         continue; // incorrect position
654       theTangentA = aCenter[0]->added(aCurDir->xy()->multiplied(anArcRadius[0]));
655
656       aCurDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d((*anIt)->decreased(aCenter[1])));
657       aCurAngle = aCurDir->angle(aStartArcDir[1]);
658       if (anArcAngle[1] < 0.0) aCurAngle *= -1.0;
659       if (aCurAngle < 0.0 || aCurAngle > fabs(anArcAngle[1]))
660         continue; // incorrect position
661       theTangentB = aCenter[1]->added(aCurDir->xy()->multiplied(anArcRadius[1]));
662
663       // the center is found, stop searching
664       theCenter = *anIt;
665       return;
666     }
667   }
668 }
669
670 void getPointOnEdge(const FeaturePtr theFeature,
671                     const std::shared_ptr<GeomAPI_Pnt2d> theFilletPoint,
672                     std::shared_ptr<GeomAPI_Pnt2d>& thePoint) {
673   if(theFeature->getKind() == SketchPlugin_Line::ID()) {
674     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
675       theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
676     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
677       theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
678     if(aPntStart->distance(theFilletPoint) > 1.e-7) {
679       aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
680         theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
681       aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
682         theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
683     }
684     thePoint.reset( new GeomAPI_Pnt2d(aPntStart->xy()->added( aPntEnd->xy()->decreased( aPntStart->xy() )->multiplied(1.0 / 3.0) ) ) );
685   } else {
686     std::shared_ptr<GeomAPI_Pnt2d> aPntTemp;
687     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
688       theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
689     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
690       theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
691     if(theFeature->attribute(SketchPlugin_Arc::INVERSED_ID())) {
692       aPntTemp = aPntStart;
693       aPntStart = aPntEnd;
694       aPntEnd = aPntTemp;
695     }
696     std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
697       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
698     std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
699     double aStartParameter(0), anEndParameter(0);
700     aCirc->parameter(aPntStart, paramTolerance, aStartParameter);
701     aCirc->parameter(aPntEnd, paramTolerance, anEndParameter);
702     if(aPntStart->distance(theFilletPoint) > tolerance) {
703       double aTmpParameter = aStartParameter;
704       aStartParameter = anEndParameter;
705       anEndParameter = aTmpParameter;
706     }
707     double aPntParameter = aStartParameter + (anEndParameter - aStartParameter) / 3.0;
708     aCirc->D0(aPntParameter, thePoint);
709   }
710 }
711
712 double getProjectionDistance(const FeaturePtr theFeature,
713                              const std::shared_ptr<GeomAPI_Pnt2d> thePoint)
714 {
715   std::shared_ptr<GeomAPI_Pnt2d> aProjectPnt;
716   if(theFeature->getKind() == SketchPlugin_Line::ID()) {
717     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
718       theFeature->attribute(SketchPlugin_Line::START_ID()))->pnt();
719     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
720       theFeature->attribute(SketchPlugin_Line::END_ID()))->pnt();
721     std::shared_ptr<GeomAPI_Lin2d> aLin(new GeomAPI_Lin2d(aPntStart, aPntEnd));
722     aProjectPnt = aLin->project(thePoint);
723   } else {
724     std::shared_ptr<GeomAPI_Pnt2d> aPntStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
725       theFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
726     std::shared_ptr<GeomAPI_Pnt2d> aPntEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
727       theFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
728     std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
729       theFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
730     std::shared_ptr<GeomAPI_Circ2d> aCirc(new GeomAPI_Circ2d(aCenterPnt, aPntStart));
731     aProjectPnt = aCirc->project(thePoint);
732   }
733   if(aProjectPnt.get()) {
734     return aProjectPnt->distance(thePoint);
735   }
736   return -1;
737 }
738
739 std::set<FeaturePtr> getCoincides(const FeaturePtr& theConstraintCoincidence)
740 {
741   std::set<FeaturePtr> aCoincides;
742
743   std::shared_ptr<GeomAPI_Pnt2d> aFilletPnt = SketchPlugin_Tools::getCoincidencePoint(theConstraintCoincidence);
744
745   SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
746                                        SketchPlugin_ConstraintCoincidence::ENTITY_A(),
747                                        aCoincides);
748   SketchPlugin_Tools::findCoincidences(theConstraintCoincidence,
749                                        SketchPlugin_ConstraintCoincidence::ENTITY_B(),
750                                        aCoincides);
751
752   // Remove points from set of coincides.
753   std::set<FeaturePtr> aNewSetOfCoincides;
754   for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
755     if((*anIt)->getKind() == SketchPlugin_Line::ID()) {
756       aNewSetOfCoincides.insert(*anIt);
757     } else if((*anIt)->getKind() == SketchPlugin_Arc::ID()) {
758       AttributePtr anAttrCenter = (*anIt)->attribute(SketchPlugin_Arc::CENTER_ID());
759       std::shared_ptr<GeomDataAPI_Point2D> aPointCenter2D =
760         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrCenter);
761       if(aPointCenter2D.get() && aFilletPnt->isEqual(aPointCenter2D->pnt())) {
762         continue;
763       }
764       aNewSetOfCoincides.insert(*anIt);
765     }
766   }
767   aCoincides = aNewSetOfCoincides;
768
769   // If we still have more than two coincides remove auxilary entities from set of coincides.
770   if(aCoincides.size() > 2) {
771     aNewSetOfCoincides.clear();
772     for(std::set<FeaturePtr>::iterator anIt = aCoincides.begin(); anIt != aCoincides.end(); ++anIt) {
773       if(!(*anIt)->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value()) {
774         aNewSetOfCoincides.insert(*anIt);
775       }
776     }
777     aCoincides = aNewSetOfCoincides;
778   }
779
780   return aCoincides;
781 }
782 */
783
784 std::shared_ptr<GeomDataAPI_Point2D> SketchPlugin_ConstraintSplit::getPointOfRefAttr(
785                                                                   const AttributePtr& theAttribute)
786 {
787   AttributePoint2DPtr aPointAttribute;
788
789   if (theAttribute->attributeType() == ModelAPI_AttributeRefAttr::typeId()) {
790     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
791     if (aRefAttr.get() && aRefAttr->isInitialized()) {
792       AttributePtr anAttribute = aRefAttr->attr();
793       if (anAttribute.get() && anAttribute->attributeType() == GeomDataAPI_Point2D::typeId())
794         aPointAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttribute);
795     }
796   }
797   return aPointAttribute;
798 }
799
800 void SketchPlugin_ConstraintSplit::getFeaturePoints(AttributePoint2DPtr& theStartPointAttr,
801                                                     AttributePoint2DPtr& theEndPointAttr)
802 {
803   AttributePoint2DPtr aPointAttribute;
804
805   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
806                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
807   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
808
809   std::string aFeatureKind = aBaseFeature->getKind();
810   std::string aStartAttributeName, anEndAttributeName;
811   if (aFeatureKind == SketchPlugin_Line::ID()) {
812     aStartAttributeName = SketchPlugin_Line::START_ID();
813     anEndAttributeName = SketchPlugin_Line::END_ID();
814   }
815   else if (aFeatureKind == SketchPlugin_Arc::ID()) {
816     aStartAttributeName = SketchPlugin_Arc::START_ID();
817     anEndAttributeName = SketchPlugin_Arc::END_ID();
818   }
819   if (!aStartAttributeName.empty() && !anEndAttributeName.empty()) {
820     theStartPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
821                                                          aBaseFeature->attribute(aStartAttributeName));
822     theEndPointAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
823                                                          aBaseFeature->attribute(anEndAttributeName));
824   }
825 }
826
827 void SketchPlugin_ConstraintSplit::splitArc(FeaturePtr& theSplitFeature,
828                                             FeaturePtr& theBeforeFeature,
829                                             FeaturePtr& theAfterFeature)
830 {
831   SketchPlugin_Sketch* aSketch = sketch();
832   if (!aSketch)
833     return;
834
835   AttributeReferencePtr aBaseObjectAttr = std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
836                                            data()->attribute(SketchPlugin_Constraint::VALUE()));
837   FeaturePtr aBaseFeature = ModelAPI_Feature::feature(aBaseObjectAttr->value());
838   std::string aFeatureKind = aBaseFeature->getKind();
839   if (aFeatureKind != SketchPlugin_Arc::ID())
840     return;
841
842   AttributePoint2DPtr aFirstPointAttr = getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_A()));
843   AttributePoint2DPtr aSecondPointAttr = getPointOfRefAttr(data()->attribute(SketchPlugin_Constraint::ENTITY_B()));
844   AttributePoint2DPtr aStartPointAttr, anEndPointAttr;
845   getFeaturePoints(aStartPointAttr, anEndPointAttr);
846   if (!aStartPointAttr.get() && !anEndPointAttr.get()) {
847     setError("Error: Feature has no start and end points.");
848     return;
849   }
850
851   arrangePoints(aStartPointAttr, anEndPointAttr, aFirstPointAttr, aSecondPointAttr);
852
853   /// split feature
854   theSplitFeature = aSketch->addFeature(aFeatureKind);
855   theSplitFeature->string(SketchPlugin_Arc::ARC_TYPE())->setValue(
856                 SketchPlugin_Arc::ARC_TYPE_CENTER_START_END());
857   fillAttribute(theSplitFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
858                 aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
859   fillAttribute(theSplitFeature->attribute(SketchPlugin_Arc::START_ID()), aFirstPointAttr);
860   fillAttribute(theSplitFeature->attribute(SketchPlugin_Arc::END_ID()), aSecondPointAttr);
861   theSplitFeature->execute(); /// to use result of the feature in constraint
862
863   if (!aStartPointAttr->pnt()->isEqual(aFirstPointAttr->pnt())) {
864     theBeforeFeature = aBaseFeature; ///< use base feature to store all constraints here
865     /// move end arc point to start of split
866     fillAttribute(theBeforeFeature->attribute(SketchPlugin_Arc::END_ID()), aFirstPointAttr);
867     createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
868                      theBeforeFeature->attribute(SketchPlugin_Arc::END_ID()),
869                      theSplitFeature->attribute(SketchPlugin_Arc::START_ID()));
870   }
871
872   if (!aSecondPointAttr->pnt()->isEqual(anEndPointAttr->pnt())) {
873     if (!theBeforeFeature) {
874       theAfterFeature = aBaseFeature; ///< use base feature to store all constraints here
875       fillAttribute(theBeforeFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttr);
876     }
877     else {
878       theAfterFeature = aSketch->addFeature(aFeatureKind);
879       theAfterFeature->string(SketchPlugin_Arc::ARC_TYPE())->setValue(
880                     SketchPlugin_Arc::ARC_TYPE_CENTER_START_END());
881       fillAttribute(theAfterFeature->attribute(SketchPlugin_Arc::CENTER_ID()),
882                     aBaseFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
883       fillAttribute(theAfterFeature->attribute(SketchPlugin_Arc::START_ID()), aSecondPointAttr);
884       fillAttribute(theAfterFeature->attribute(SketchPlugin_Arc::END_ID()), anEndPointAttr);
885       theAfterFeature->execute(); /// to use result of the feature in constraint
886     }
887     createConstraint(SketchPlugin_ConstraintCoincidence::ID(),
888                      theSplitFeature->attribute(SketchPlugin_Arc::END_ID()),
889                      theAfterFeature->attribute(SketchPlugin_Arc::START_ID()));
890   }
891
892   // additional constraints between split and base features
893   createConstraint(SketchPlugin_ConstraintEqual::ID(), getFeatureResult(aBaseFeature),
894                                                        getFeatureResult(theSplitFeature));
895   createConstraint(SketchPlugin_ConstraintTangent::ID(), getFeatureResult(theSplitFeature),
896                                                          getFeatureResult(aBaseFeature));
897   if (theAfterFeature.get()) {
898     createConstraint(SketchPlugin_ConstraintEqual::ID(), getFeatureResult(aBaseFeature),
899                                                          getFeatureResult(theAfterFeature));
900     createConstraint(SketchPlugin_ConstraintTangent::ID(), getFeatureResult(theSplitFeature),
901                                                          getFeatureResult(theAfterFeature));
902   }
903 }
904
905 void SketchPlugin_ConstraintSplit::arrangePoints(const AttributePoint2DPtr& theStartPointAttr,
906                                                  const AttributePoint2DPtr& theEndPointAttr,
907                                                  AttributePoint2DPtr& theFirstPointAttr,
908                                                  AttributePoint2DPtr& theLastPointAttr)
909 {
910   /// if first point is closer to last point, wrap first and last values
911   if (theStartPointAttr->pnt()->distance(theFirstPointAttr->pnt()) >
912       theEndPointAttr->pnt()->distance(theLastPointAttr->pnt())) {
913     AttributePoint2DPtr aTmpPoint = theFirstPointAttr;
914     theFirstPointAttr = theLastPointAttr;
915     theLastPointAttr = aTmpPoint;
916   }
917 }
918
919 void SketchPlugin_ConstraintSplit::fillAttribute(const AttributePtr& theModifiedAttribute,
920                                                  const AttributePtr& theSourceAttribute)
921 {
922   AttributePoint2DPtr aModifiedAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
923                                             theModifiedAttribute);
924   AttributePoint2DPtr aSourceAttribute = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
925                                             theSourceAttribute);
926
927   if (aModifiedAttribute.get() && aSourceAttribute.get())
928     aSourceAttribute->setValue(aModifiedAttribute->pnt());
929 }
930
931 void SketchPlugin_ConstraintSplit::createConstraint(const std::string& theConstraintId,
932                                                     const AttributePtr& theFirstAttribute,
933                                                     const AttributePtr& theSecondAttribute)
934 {
935   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
936   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
937                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
938   aRefAttr->setAttr(theFirstAttribute);
939
940   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
941                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
942   aRefAttr->setAttr(theSecondAttribute);
943 }
944
945 void SketchPlugin_ConstraintSplit::createConstraint(const std::string& theConstraintId,
946                                                     const ObjectPtr& theFirstObject,
947                                                     const ObjectPtr& theSecondObject)
948 {
949   FeaturePtr aConstraint = sketch()->addFeature(theConstraintId);
950   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
951                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
952   aRefAttr->setObject(theFirstObject);
953
954   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
955                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
956   aRefAttr->setObject(theSecondObject);
957 }
958
959 std::shared_ptr<ModelAPI_Object> SketchPlugin_ConstraintSplit::getFeatureResult(
960                                     const std::shared_ptr<ModelAPI_Feature>& theFeature)
961 {
962   std::shared_ptr<ModelAPI_Object> aResult;
963
964   std::string aFeatureKind = theFeature->getKind();
965   if (aFeatureKind == SketchPlugin_Line::ID())
966     aResult = theFeature->firstResult();
967   else if (aFeatureKind == SketchPlugin_Arc::ID())
968     aResult = theFeature->lastResult();
969   else if (aFeatureKind == SketchPlugin_Circle::ID())
970     aResult = theFeature->lastResult();
971
972   return aResult;
973 }
974