Salome HOME
Unit tests fixes related to python swigged lists iteration
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_ConstraintCoincidence.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:    SketchPlugin_ConstraintCoincidence.cpp
4 // Created: 08 May 2014
5 // Author:  Artem ZHIDKOV
6
7 #include "SketchPlugin_ConstraintCoincidence.h"
8
9 #include <SketchPlugin_Point.h>
10 #include <SketchPlugin_Line.h>
11 #include <SketchPlugin_Arc.h>
12 #include <SketchPlugin_Circle.h>
13
14 #include <ModelGeomAlgo_Point2D.h>
15 #include <GeomDataAPI_Point2D.h>
16
17 #include <SketcherPrs_Factory.h>
18
19 SketchPlugin_ConstraintCoincidence::SketchPlugin_ConstraintCoincidence()
20 {
21 }
22
23 void SketchPlugin_ConstraintCoincidence::initAttributes()
24 {
25   data()->addAttribute(SketchPlugin_Constraint::ENTITY_A(), ModelAPI_AttributeRefAttr::typeId());
26   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::typeId());
27 }
28
29 void SketchPlugin_ConstraintCoincidence::execute()
30 {
31 }
32
33 AISObjectPtr SketchPlugin_ConstraintCoincidence::getAISObject(AISObjectPtr thePrevious)
34 {
35   if (!sketch())
36     return thePrevious;
37
38   AISObjectPtr anAIS = SketcherPrs_Factory::coincidentConstraint(this, sketch()->coordinatePlane(),
39                                                                  thePrevious);
40   return anAIS;
41 }
42
43 FeaturePtr SketchPlugin_ConstraintCoincidence::findCoincidenceFeature(const FeaturePtr& theFeature1,
44                                                                       const FeaturePtr& theFeature2)
45 {
46   FeaturePtr aResultFeature;
47
48   std::list<AttributePtr> anAttrList;
49   if (theFeature1->getKind() == SketchPlugin_Circle::ID() ||
50       theFeature2->getKind() == SketchPlugin_Circle::ID())
51     return aResultFeature;
52
53   if (theFeature2->getKind() == SketchPlugin_Line::ID()) {
54     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::START_ID()));
55     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Line::END_ID()));
56   } else if (theFeature2->getKind() == SketchPlugin_Arc::ID()) {
57     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::START_ID()));
58     anAttrList.push_back(theFeature2->attribute(SketchPlugin_Arc::END_ID()));
59   }
60
61   const std::set<AttributePtr>& aRefsList = theFeature1->data()->refsToMe();
62   std::set<AttributePtr>::const_iterator aRefIt = aRefsList.begin();
63   for (; aRefIt != aRefsList.end() && !aResultFeature.get(); ++aRefIt) {
64     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aRefIt)->owner());
65     if (aConstrFeature->getKind() != SketchPlugin_ConstraintCoincidence::ID())
66       continue;
67     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*aRefIt);
68     AttributePtr anAttr = aRefAttr->attr();
69     if (anAttr->id() == SketchPlugin_Arc::CENTER_ID())
70       continue;
71
72     anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_A());
73     if (anAttr == *aRefIt)
74       anAttr = aConstrFeature->attribute(SketchPlugin_Constraint::ENTITY_B());
75
76     aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
77     if (!aRefAttr)
78       continue;
79     anAttr = aRefAttr->attr();
80     for (std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
81          anIt != anAttrList.end() && !aResultFeature.get(); ++anIt)
82       if (*anIt == anAttr)
83         aResultFeature = aConstrFeature;
84   }
85   return aResultFeature;
86 }
87
88 AttributePoint2DPtr SketchPlugin_ConstraintCoincidence::getPoint(const FeaturePtr& theFeature)
89 {
90   AttributePoint2DPtr aPoint = ModelGeomAlgo_Point2D::getPointOfRefAttr(theFeature.get(),
91                                                          SketchPlugin_Constraint::ENTITY_A(),
92                                  SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
93   if (!aPoint.get())
94     aPoint = ModelGeomAlgo_Point2D::getPointOfRefAttr(theFeature.get(),
95                                                       SketchPlugin_Constraint::ENTITY_B(),
96                                  SketchPlugin_Point::ID(), SketchPlugin_Point::COORD_ID());
97   return aPoint;
98 }