Salome HOME
9a4d82f848e9d425223463ff6634d38b178fdb02
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Tools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Tools.cpp
4 // Created:     07 July 2015
5 // Author:      Sergey POKHODENKO
6
7 #include "SketchPlugin_Tools.h"
8
9 #include "SketchPlugin_ConstraintCoincidence.h"
10 #include "SketchPlugin_ConstraintTangent.h"
11 #include "SketchPlugin_Point.h"
12 #include "SketchPlugin_SketchEntity.h"
13
14 #include <SketcherPrs_Tools.h>
15
16 #include <ModelAPI_AttributeDouble.h>
17
18 #include <GeomDataAPI_Point.h>
19 #include <GeomDataAPI_Point2D.h>
20
21 namespace SketchPlugin_Tools {
22
23 void clearExpressions(AttributeDoublePtr theAttribute)
24 {
25   theAttribute->setText(std::string());
26 }
27
28 void clearExpressions(AttributePointPtr theAttribute)
29 {
30   theAttribute->setText(std::string(), std::string(), std::string());
31 }
32
33 void clearExpressions(AttributePoint2DPtr theAttribute)
34 {
35   theAttribute->setText(std::string(), std::string());
36 }
37
38 void clearExpressions(AttributePtr theAttribute)
39 {
40   // Double
41   AttributeDoublePtr anAttributeDouble =
42       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
43   if (anAttributeDouble.get())
44     clearExpressions(anAttributeDouble);
45   // Point
46   AttributePointPtr anAttributePoint =
47       std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
48   if (anAttributePoint.get())
49     clearExpressions(anAttributePoint);
50   // Point2D
51   AttributePoint2DPtr anAttributePoint2D =
52       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
53   if (anAttributePoint2D.get())
54     clearExpressions(anAttributePoint2D);
55 }
56
57 void clearExpressions(FeaturePtr theFeature)
58 {
59   if (!theFeature.get())
60     return;
61
62   std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
63   std::list<AttributePtr>::iterator anAttributeIt = anAttributes.begin();
64   for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
65     clearExpressions(*anAttributeIt);
66   }
67 }
68
69 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(const FeaturePtr theStartCoin)
70 {
71   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
72                                                           SketchPlugin_Constraint::ENTITY_A());
73   if (aPnt.get() == NULL)
74     aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
75   return aPnt;
76 }
77
78 void findCoincidences(const FeaturePtr theStartCoin,
79                       const std::string& theAttr,
80                       std::set<FeaturePtr>& theList)
81 {
82   AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
83   if(!aPnt) {
84     return;
85   }
86   FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
87   if(theList.find(aObj) == theList.end()) {
88     std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincidencePoint(theStartCoin);
89     if(aOrig.get() == NULL) {
90       return;
91     }
92     theList.insert(aObj);
93     const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
94     std::set<AttributePtr>::const_iterator aIt;
95     for(aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
96       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
97       FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
98       if(aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
99         std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(aConstrFeature);
100         if(aPnt.get() && aOrig->isEqual(aPnt)) {
101           findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A(), theList);
102           findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B(), theList);
103         }
104       }
105     }
106   }
107 }
108
109 void resetAttribute(SketchPlugin_Feature* theFeature,
110                     const std::string& theId)
111 {
112   AttributePtr anAttr = theFeature->attribute(theId);
113   if(anAttr.get()) {
114     anAttr->reset();
115   }
116 }
117
118 void createConstraint(SketchPlugin_Feature* theFeature,
119                       const std::string& theId,
120                       const AttributePtr theAttr,
121                       const ObjectPtr theObject,
122                       const bool theIsCanBeTangent)
123 {
124   AttributeRefAttrPtr aRefAttr = theFeature->refattr(theId);
125   if(aRefAttr.get() && aRefAttr->isInitialized()) {
126     FeaturePtr aConstraint;
127     if(!theIsCanBeTangent) {
128       aConstraint = theFeature->sketch()
129                               ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
130     } else {
131       if(aRefAttr->isObject()) {
132         ObjectPtr anObject = aRefAttr->object();
133         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
134         if(aFeature->getKind() == SketchPlugin_Point::ID()) {
135           aConstraint = theFeature->sketch()
136                                   ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
137         } else {
138           aConstraint = theFeature->sketch()
139                                   ->addFeature(SketchPlugin_ConstraintTangent::ID());
140         }
141       } else {
142         aConstraint = theFeature->sketch()
143                                 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
144       }
145     }
146     AttributeRefAttrPtr aRefAttrA = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
147     aRefAttr->isObject() ? aRefAttrA->setObject(aRefAttr->object())
148                          : aRefAttrA->setAttr(aRefAttr->attr());
149     AttributeRefAttrPtr aRefAttrB = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
150     if(theObject.get()) {
151       aRefAttrB->setObject(theObject);
152     } else if(theAttr.get()) {
153       aRefAttrB->setAttr(theAttr);
154     }
155   }
156 }
157 } // namespace SketchPlugin_Tools