Salome HOME
Merge remote-tracking branch 'origin/BR_REENTRANCE_OPERATION' into origin_Dev_1.5.0
[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 <GeomDataAPI_Point.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <SketcherPrs_Tools.h>
13 #include <SketchPlugin_ConstraintCoincidence.h>
14 #include <SketchPlugin_SketchEntity.h>
15
16 namespace SketchPlugin_Tools {
17
18 void clearExpressions(AttributeDoublePtr theAttribute)
19 {
20   theAttribute->setText(std::string());
21 }
22
23 void clearExpressions(AttributePointPtr theAttribute)
24 {
25   theAttribute->setText(std::string(), std::string(), std::string());
26 }
27
28 void clearExpressions(AttributePoint2DPtr theAttribute)
29 {
30   theAttribute->setText(std::string(), std::string());
31 }
32
33 void clearExpressions(AttributePtr theAttribute)
34 {
35   // Double
36   AttributeDoublePtr anAttributeDouble = 
37       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
38   if (anAttributeDouble.get())
39     clearExpressions(anAttributeDouble);
40   // Point
41   AttributePointPtr anAttributePoint = 
42       std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
43   if (anAttributePoint.get())
44     clearExpressions(anAttributePoint);
45   // Point2D
46   AttributePoint2DPtr anAttributePoint2D = 
47       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
48   if (anAttributePoint2D.get())
49     clearExpressions(anAttributePoint2D);
50 }
51
52 void clearExpressions(FeaturePtr theFeature)
53 {
54   if (!theFeature.get())
55     return;
56
57   std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());  
58   std::list<AttributePtr>::iterator anAttributeIt = anAttributes.begin();
59   for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
60     clearExpressions(*anAttributeIt);
61   }
62 }
63
64 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(const FeaturePtr theStartCoin)
65 {
66   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), 
67                                                                     SketchPlugin_Constraint::ENTITY_A());
68   if (aPnt.get() == NULL)
69     aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
70   return aPnt;
71 }
72
73 void findCoincidences(const FeaturePtr theStartCoin,
74                       const std::string& theAttr,
75                       std::set<FeaturePtr>& theList)
76 {
77   AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
78   if(!aPnt) {
79     return;
80   }
81   FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
82   if(theList.find(aObj) == theList.end()) {
83     std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincidencePoint(theStartCoin);
84     if(aOrig.get() == NULL) {
85       return;
86     }
87     theList.insert(aObj);
88     const std::set<AttributePtr>& aRefsList = aObj->data()->refsToMe();
89     std::set<AttributePtr>::const_iterator aIt;
90     for(aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
91       std::shared_ptr<ModelAPI_Attribute> aAttr = (*aIt);
92       FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aAttr->owner());
93       if(aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
94         std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(aConstrFeature);
95         if(aPnt.get() && aOrig->isEqual(aPnt)) {
96           findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A(), theList);
97           findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B(), theList);
98         }
99       }
100     }
101   }
102 }
103
104 void updateMultiAttribute(const AttributePtr& theFirstAngleAttribute,
105                           const AttributePtr& theSecondAngleAttribute,
106                           const int& theValue,
107                           const bool toMultiply)
108 {
109   if (theValue == 0 || !theFirstAngleAttribute->isInitialized())
110     return;
111
112   AttributeDoublePtr aDoubleFirstAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
113                                                                 theFirstAngleAttribute);
114   double aValue = aDoubleFirstAttr->value();
115
116   AttributeDoublePtr aDoubleSecondAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
117                                                                 theSecondAngleAttribute);
118   if (toMultiply)
119     aDoubleSecondAttr->setValue(aValue*theValue);
120   else
121     aDoubleSecondAttr->setValue(aValue/theValue);
122 }
123
124 void updateMultiAttribute(const AttributePtr& theFirstAttribute,
125                           const AttributePtr& theSecondAttribute,
126                           const AttributePtr& theModifiedAttribute,
127                           const int& theValue,
128                           const bool toMultiply)
129 {
130   if (theValue == 0 || !theFirstAttribute->isInitialized()
131                     || !theSecondAttribute->isInitialized())
132     return;
133
134   std::shared_ptr<GeomDataAPI_Point2D> aFirstPoint = 
135           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFirstAttribute);
136   std::shared_ptr<GeomDataAPI_Point2D> aSecondPoint = 
137           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theSecondAttribute);
138   std::shared_ptr<GeomDataAPI_Point2D> aModifiedPoint = 
139           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theModifiedAttribute);
140
141   if (!aFirstPoint.get() || !aSecondPoint.get() || !aModifiedPoint.get())
142     return;
143
144   if (aFirstPoint->pnt()->isEqual(aSecondPoint->pnt()))
145     aModifiedPoint->setValue(aFirstPoint->pnt());
146   else {
147     double aDx = aSecondPoint->x() - aFirstPoint->x();
148     double aDy = aSecondPoint->y() - aFirstPoint->y();
149
150     double aX  = toMultiply ? aDx * theValue : aDx / theValue;
151     double anY = toMultiply ? aDy * theValue : aDy / theValue;
152
153     aModifiedPoint->setValue(aFirstPoint->x() + aX, aFirstPoint->y() + anY);
154   }
155 }
156
157 } // namespace SketchPlugin_Tools