Salome HOME
Issue #2157: Create fillet : special behavior with origin
[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                       const bool theIsAttrOnly)
109 {
110   AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
111   if(!aPnt) {
112     return;
113   }
114   FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
115   if(theList.find(aObj) == theList.end()) {
116     std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincidencePoint(theStartCoin);
117     if(aOrig.get() == NULL) {
118       return;
119     }
120     if(!theIsAttrOnly || !aPnt->isObject()) {
121       theList.insert(aObj);
122     }
123     std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(aObj);
124     std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
125     for (; aCIt != aCoincidences.end(); ++aCIt) {
126       FeaturePtr aConstrFeature = *aCIt;
127       std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(aConstrFeature);
128       if(aPnt.get() && aOrig->isEqual(aPnt)) {
129         findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A(), theList, theIsAttrOnly);
130         findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B(), theList, theIsAttrOnly);
131       }
132     }
133   }
134 }
135
136 std::set<FeaturePtr> findFeaturesCoincidentToPoint(const AttributePoint2DPtr& thePoint)
137 {
138   std::set<FeaturePtr> aCoincidentFeatures;
139
140   FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
141   aCoincidentFeatures.insert(anOwner);
142
143   std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(anOwner);
144   std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
145   for (; aCIt != aCoincidences.end(); ++aCIt) {
146     bool isPointUsedInCoincidence = false;
147     AttributeRefAttrPtr anOtherCoincidentAttr;
148     for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
149       AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
150       if (!aRefAttr)
151         continue;
152       if (!aRefAttr->isObject() && aRefAttr->attr() == thePoint)
153         isPointUsedInCoincidence = true;
154       else
155         anOtherCoincidentAttr = aRefAttr;
156     }
157
158     if (isPointUsedInCoincidence) {
159       ObjectPtr anObj;
160       if (anOtherCoincidentAttr->isObject())
161         anObj = anOtherCoincidentAttr->object();
162       else
163         anObj = anOtherCoincidentAttr->attr()->owner();
164       aCoincidentFeatures.insert(ModelAPI_Feature::feature(anObj));
165     }
166   }
167
168   return aCoincidentFeatures;
169 }
170
171 // Container for point-point coincidences.
172 // Useful to find points coincident to a given point.
173 class CoincidentPoints
174 {
175 public:
176   void addCoincidence(const AttributePoint2DPtr& thePoint1,
177                       const AttributePoint2DPtr& thePoint2 = AttributePoint2DPtr())
178   {
179     std::list< std::set<AttributePoint2DPtr> >::iterator aFound1 = find(thePoint1);
180     std::list< std::set<AttributePoint2DPtr> >::iterator aFound2 = find(thePoint2);
181     if (aFound1 == myCoincidentPoints.end()) {
182       std::set<AttributePoint2DPtr> aNewSet;
183       aNewSet.insert(thePoint1);
184       if (thePoint2)
185         aNewSet.insert(thePoint2);
186       myCoincidentPoints.push_back(aNewSet);
187     } else if (aFound2 == myCoincidentPoints.end()) {
188       if (thePoint2)
189         aFound1->insert(thePoint2);
190     } else {
191       aFound1->insert(aFound2->begin(), aFound2->end());
192       myCoincidentPoints.erase(aFound2);
193     }
194   }
195
196   std::set<AttributePoint2DPtr> coincidentPoints(const AttributePoint2DPtr& thePoint)
197   {
198     std::list< std::set<AttributePoint2DPtr> >::iterator aFound = find(thePoint);
199     if (aFound == myCoincidentPoints.end())
200       return std::set<AttributePoint2DPtr>();
201     return *aFound;
202   }
203
204 private:
205   std::list< std::set<AttributePoint2DPtr> >::iterator find(const AttributePoint2DPtr& thePoint)
206   {
207     std::list< std::set<AttributePoint2DPtr> >::iterator aSeek = myCoincidentPoints.begin();
208     for (; aSeek != myCoincidentPoints.end(); ++aSeek)
209       if (aSeek->find(thePoint) != aSeek->end())
210         return aSeek;
211     return myCoincidentPoints.end();
212   }
213
214 private:
215   std::list< std::set<AttributePoint2DPtr> > myCoincidentPoints;
216 };
217
218 std::set<AttributePoint2DPtr> findPointsCoincidentToPoint(const AttributePoint2DPtr& thePoint)
219 {
220   CoincidentPoints aCoincidentPoints;
221   AttributePoint2DPtr aPoints[2];
222
223   FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
224   std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(anOwner);
225   std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
226   for (; aCIt != aCoincidences.end(); ++aCIt) {
227     aPoints[0] = AttributePoint2DPtr();
228     aPoints[1] = AttributePoint2DPtr();
229     for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
230       AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
231       if (!aRefAttr)
232         continue;
233       if (!aRefAttr->isObject())
234         aPoints[aPtInd++] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
235     }
236
237     if (aPoints[0])
238       aCoincidentPoints.addCoincidence(aPoints[0], aPoints[1]);
239   }
240
241   return aCoincidentPoints.coincidentPoints(thePoint);
242 }
243
244 void resetAttribute(SketchPlugin_Feature* theFeature,
245                     const std::string& theId)
246 {
247   AttributePtr anAttr = theFeature->attribute(theId);
248   if(anAttr.get()) {
249     anAttr->reset();
250   }
251 }
252
253 void createConstraint(SketchPlugin_Feature* theFeature,
254                       const std::string& theId,
255                       const AttributePtr theAttr,
256                       const ObjectPtr theObject,
257                       const bool theIsCanBeTangent)
258 {
259   AttributeRefAttrPtr aRefAttr = theFeature->refattr(theId);
260   if(aRefAttr.get() && aRefAttr->isInitialized()) {
261     FeaturePtr aConstraint;
262     if(!theIsCanBeTangent) {
263       aConstraint = theFeature->sketch()
264                               ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
265     } else {
266       if(aRefAttr->isObject()) {
267         ObjectPtr anObject = aRefAttr->object();
268         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
269         if(aFeature->getKind() == SketchPlugin_Point::ID()) {
270           aConstraint = theFeature->sketch()
271                                   ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
272         } else {
273           aConstraint = theFeature->sketch()
274                                   ->addFeature(SketchPlugin_ConstraintTangent::ID());
275         }
276       } else {
277         aConstraint = theFeature->sketch()
278                                 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
279       }
280     }
281     AttributeRefAttrPtr aRefAttrA = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
282     aRefAttr->isObject() ? aRefAttrA->setObject(aRefAttr->object())
283                          : aRefAttrA->setAttr(aRefAttr->attr());
284     AttributeRefAttrPtr aRefAttrB = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
285     if(theObject.get()) {
286       aRefAttrB->setObject(theObject);
287     } else if(theAttr.get()) {
288       aRefAttrB->setAttr(theAttr);
289     }
290   }
291 }
292
293 void convertRefAttrToPointOrTangentCurve(const AttributeRefAttrPtr&      theRefAttr,
294                                          const AttributePtr&             theDefaultAttr,
295                                          std::shared_ptr<GeomAPI_Shape>& theTangentCurve,
296                                          std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
297 {
298   AttributePtr anAttr = theDefaultAttr;
299   if (theRefAttr->isObject()) {
300     FeaturePtr aTgFeature = ModelAPI_Feature::feature(theRefAttr->object());
301     if (aTgFeature) {
302       if (aTgFeature->getKind() != SketchPlugin_Point::ID()) {
303         theTangentCurve = aTgFeature->lastResult()->shape();
304         return;
305       }
306       anAttr = aTgFeature->attribute(SketchPlugin_Point::COORD_ID());
307     }
308   } else
309     anAttr = theRefAttr->attr();
310
311   thePassingPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr)->pnt();
312 }
313
314 } // namespace SketchPlugin_Tools