]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Tools.cpp
Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Tools.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "SketchPlugin_Tools.h"
22
23 #include "SketchPlugin_ConstraintCoincidence.h"
24 #include "SketchPlugin_ConstraintTangent.h"
25 #include "SketchPlugin_Point.h"
26 #include "SketchPlugin_SketchEntity.h"
27
28 #include <SketcherPrs_Tools.h>
29
30 #include <ModelAPI_AttributeDouble.h>
31
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Point2D.h>
34
35 namespace SketchPlugin_Tools {
36
37 void clearExpressions(AttributeDoublePtr theAttribute)
38 {
39   theAttribute->setText(std::string());
40 }
41
42 void clearExpressions(AttributePointPtr theAttribute)
43 {
44   theAttribute->setText(std::string(), std::string(), std::string());
45 }
46
47 void clearExpressions(AttributePoint2DPtr theAttribute)
48 {
49   theAttribute->setText(std::string(), std::string());
50 }
51
52 void clearExpressions(AttributePtr theAttribute)
53 {
54   // Double
55   AttributeDoublePtr anAttributeDouble =
56       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
57   if (anAttributeDouble.get())
58     clearExpressions(anAttributeDouble);
59   // Point
60   AttributePointPtr anAttributePoint =
61       std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
62   if (anAttributePoint.get())
63     clearExpressions(anAttributePoint);
64   // Point2D
65   AttributePoint2DPtr anAttributePoint2D =
66       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
67   if (anAttributePoint2D.get())
68     clearExpressions(anAttributePoint2D);
69 }
70
71 void clearExpressions(FeaturePtr theFeature)
72 {
73   if (!theFeature.get())
74     return;
75
76   std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
77   std::list<AttributePtr>::iterator anAttributeIt = anAttributes.begin();
78   for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
79     clearExpressions(*anAttributeIt);
80   }
81 }
82
83 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(const FeaturePtr theStartCoin)
84 {
85   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
86                                                           SketchPlugin_Constraint::ENTITY_A());
87   if (aPnt.get() == NULL)
88     aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
89   return aPnt;
90 }
91
92 std::set<FeaturePtr> findCoincidentConstraints(const FeaturePtr& theFeature)
93 {
94   std::set<FeaturePtr> aCoincident;
95   const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
96   std::set<AttributePtr>::const_iterator aIt;
97   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
98     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aIt)->owner());
99     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID())
100       aCoincident.insert(aConstrFeature);
101   }
102   return aCoincident;
103 }
104
105 void findCoincidences(const FeaturePtr theStartCoin,
106                       const std::string& theAttr,
107                       std::set<FeaturePtr>& theList)
108 {
109   AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
110   if(!aPnt) {
111     return;
112   }
113   FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
114   if(theList.find(aObj) == theList.end()) {
115     std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincidencePoint(theStartCoin);
116     if(aOrig.get() == NULL) {
117       return;
118     }
119     theList.insert(aObj);
120     std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(aObj);
121     std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
122     for (; aCIt != aCoincidences.end(); ++aCIt) {
123       FeaturePtr aConstrFeature = *aCIt;
124       std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(aConstrFeature);
125       if(aPnt.get() && aOrig->isEqual(aPnt)) {
126         findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A(), theList);
127         findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B(), theList);
128       }
129     }
130   }
131 }
132
133 std::set<FeaturePtr> findFeaturesCoincidentToPoint(const AttributePoint2DPtr& thePoint)
134 {
135   std::set<FeaturePtr> aCoincidentFeatures;
136
137   FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
138   aCoincidentFeatures.insert(anOwner);
139
140   std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(anOwner);
141   std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
142   for (; aCIt != aCoincidences.end(); ++aCIt) {
143     bool isPointUsedInCoincidence = false;
144     AttributeRefAttrPtr anOtherCoincidentAttr;
145     for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
146       AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
147       if (!aRefAttr)
148         continue;
149       if (!aRefAttr->isObject() && aRefAttr->attr() == thePoint)
150         isPointUsedInCoincidence = true;
151       else
152         anOtherCoincidentAttr = aRefAttr;
153     }
154
155     if (isPointUsedInCoincidence) {
156       ObjectPtr anObj;
157       if (anOtherCoincidentAttr->isObject())
158         anObj = anOtherCoincidentAttr->object();
159       else
160         anObj = anOtherCoincidentAttr->attr()->owner();
161       aCoincidentFeatures.insert(ModelAPI_Feature::feature(anObj));
162     }
163   }
164
165   return aCoincidentFeatures;
166 }
167
168 // Container for point-point coincidences.
169 // Useful to find points coincident to a given point.
170 class CoincidentPoints
171 {
172 public:
173   void addCoincidence(const AttributePoint2DPtr& thePoint1,
174                       const AttributePoint2DPtr& thePoint2 = AttributePoint2DPtr())
175   {
176     std::list< std::set<AttributePoint2DPtr> >::iterator aFound1 = find(thePoint1);
177     std::list< std::set<AttributePoint2DPtr> >::iterator aFound2 = find(thePoint2);
178     if (aFound1 == myCoincidentPoints.end()) {
179       std::set<AttributePoint2DPtr> aNewSet;
180       aNewSet.insert(thePoint1);
181       if (thePoint2)
182         aNewSet.insert(thePoint2);
183       myCoincidentPoints.push_back(aNewSet);
184     } else if (aFound2 == myCoincidentPoints.end()) {
185       if (thePoint2)
186         aFound1->insert(thePoint2);
187     } else {
188       aFound1->insert(aFound2->begin(), aFound2->end());
189       myCoincidentPoints.erase(aFound2);
190     }
191   }
192
193   std::set<AttributePoint2DPtr> coincidentPoints(const AttributePoint2DPtr& thePoint)
194   {
195     std::list< std::set<AttributePoint2DPtr> >::iterator aFound = find(thePoint);
196     if (aFound == myCoincidentPoints.end())
197       return std::set<AttributePoint2DPtr>();
198     return *aFound;
199   }
200
201 private:
202   std::list< std::set<AttributePoint2DPtr> >::iterator find(const AttributePoint2DPtr& thePoint)
203   {
204     std::list< std::set<AttributePoint2DPtr> >::iterator aSeek = myCoincidentPoints.begin();
205     for (; aSeek != myCoincidentPoints.end(); ++aSeek)
206       if (aSeek->find(thePoint) != aSeek->end())
207         return aSeek;
208     return myCoincidentPoints.end();
209   }
210
211 private:
212   std::list< std::set<AttributePoint2DPtr> > myCoincidentPoints;
213 };
214
215 std::set<AttributePoint2DPtr> findPointsCoincidentToPoint(const AttributePoint2DPtr& thePoint)
216 {
217   CoincidentPoints aCoincidentPoints;
218   AttributePoint2DPtr aPoints[2];
219
220   FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
221   std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(anOwner);
222   std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
223   for (; aCIt != aCoincidences.end(); ++aCIt) {
224     aPoints[0] = AttributePoint2DPtr();
225     aPoints[1] = AttributePoint2DPtr();
226     for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
227       AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
228       if (!aRefAttr)
229         continue;
230       if (!aRefAttr->isObject())
231         aPoints[aPtInd++] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
232     }
233
234     if (aPoints[0])
235       aCoincidentPoints.addCoincidence(aPoints[0], aPoints[1]);
236   }
237
238   return aCoincidentPoints.coincidentPoints(thePoint);
239 }
240
241 void resetAttribute(SketchPlugin_Feature* theFeature,
242                     const std::string& theId)
243 {
244   AttributePtr anAttr = theFeature->attribute(theId);
245   if(anAttr.get()) {
246     anAttr->reset();
247   }
248 }
249
250 void createConstraint(SketchPlugin_Feature* theFeature,
251                       const std::string& theId,
252                       const AttributePtr theAttr,
253                       const ObjectPtr theObject,
254                       const bool theIsCanBeTangent)
255 {
256   AttributeRefAttrPtr aRefAttr = theFeature->refattr(theId);
257   if(aRefAttr.get() && aRefAttr->isInitialized()) {
258     FeaturePtr aConstraint;
259     if(!theIsCanBeTangent) {
260       aConstraint = theFeature->sketch()
261                               ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
262     } else {
263       if(aRefAttr->isObject()) {
264         ObjectPtr anObject = aRefAttr->object();
265         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
266         if(aFeature->getKind() == SketchPlugin_Point::ID()) {
267           aConstraint = theFeature->sketch()
268                                   ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
269         } else {
270           aConstraint = theFeature->sketch()
271                                   ->addFeature(SketchPlugin_ConstraintTangent::ID());
272         }
273       } else {
274         aConstraint = theFeature->sketch()
275                                 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
276       }
277     }
278     AttributeRefAttrPtr aRefAttrA = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
279     aRefAttr->isObject() ? aRefAttrA->setObject(aRefAttr->object())
280                          : aRefAttrA->setAttr(aRefAttr->attr());
281     AttributeRefAttrPtr aRefAttrB = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
282     if(theObject.get()) {
283       aRefAttrB->setObject(theObject);
284     } else if(theAttr.get()) {
285       aRefAttrB->setAttr(theAttr);
286     }
287   }
288 }
289
290 void convertRefAttrToPointOrTangentCurve(const AttributeRefAttrPtr&      theRefAttr,
291                                          const AttributePtr&             theDefaultAttr,
292                                          std::shared_ptr<GeomAPI_Shape>& theTangentCurve,
293                                          std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
294 {
295   AttributePtr anAttr = theDefaultAttr;
296   if (theRefAttr->isObject()) {
297     FeaturePtr aTgFeature = ModelAPI_Feature::feature(theRefAttr->object());
298     if (aTgFeature) {
299       if (aTgFeature->getKind() != SketchPlugin_Point::ID()) {
300         theTangentCurve = aTgFeature->lastResult()->shape();
301         return;
302       }
303       anAttr = aTgFeature->attribute(SketchPlugin_Point::COORD_ID());
304     }
305   } else
306     anAttr = theRefAttr->attr();
307
308   thePassingPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr)->pnt();
309 }
310
311 } // namespace SketchPlugin_Tools