]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Tools.cpp
Salome HOME
Issue #2157: Fix incorrect searching of features coincident to fillet point (special...
[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_ConstraintLength.h"
25 #include "SketchPlugin_ConstraintTangent.h"
26 #include "SketchPlugin_Line.h"
27 #include "SketchPlugin_Point.h"
28 #include "SketchPlugin_SketchEntity.h"
29
30 #include <SketcherPrs_Tools.h>
31
32 #include <ModelAPI_AttributeDouble.h>
33
34 #include <GeomAPI_Dir2d.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_XY.h>
37
38 #include <GeomDataAPI_Point.h>
39 #include <GeomDataAPI_Point2D.h>
40
41 #ifdef DEBUG_TRIM
42 #include <iostream>
43 #endif
44
45 namespace SketchPlugin_Tools {
46
47 void clearExpressions(AttributeDoublePtr theAttribute)
48 {
49   theAttribute->setText(std::string());
50 }
51
52 void clearExpressions(AttributePointPtr theAttribute)
53 {
54   theAttribute->setText(std::string(), std::string(), std::string());
55 }
56
57 void clearExpressions(AttributePoint2DPtr theAttribute)
58 {
59   theAttribute->setText(std::string(), std::string());
60 }
61
62 void clearExpressions(AttributePtr theAttribute)
63 {
64   // Double
65   AttributeDoublePtr anAttributeDouble =
66       std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
67   if (anAttributeDouble.get())
68     clearExpressions(anAttributeDouble);
69   // Point
70   AttributePointPtr anAttributePoint =
71       std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
72   if (anAttributePoint.get())
73     clearExpressions(anAttributePoint);
74   // Point2D
75   AttributePoint2DPtr anAttributePoint2D =
76       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
77   if (anAttributePoint2D.get())
78     clearExpressions(anAttributePoint2D);
79 }
80
81 void clearExpressions(FeaturePtr theFeature)
82 {
83   if (!theFeature.get())
84     return;
85
86   std::list<AttributePtr> anAttributes = theFeature->data()->attributes(std::string());
87   std::list<AttributePtr>::iterator anAttributeIt = anAttributes.begin();
88   for (; anAttributeIt != anAttributes.end(); ++anAttributeIt) {
89     clearExpressions(*anAttributeIt);
90   }
91 }
92
93 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(const FeaturePtr theStartCoin)
94 {
95   std::shared_ptr<GeomAPI_Pnt2d> aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(),
96                                                           SketchPlugin_Constraint::ENTITY_A());
97   if (aPnt.get() == NULL)
98     aPnt = SketcherPrs_Tools::getPoint(theStartCoin.get(), SketchPlugin_Constraint::ENTITY_B());
99   return aPnt;
100 }
101
102 std::set<FeaturePtr> findCoincidentConstraints(const FeaturePtr& theFeature)
103 {
104   std::set<FeaturePtr> aCoincident;
105   const std::set<AttributePtr>& aRefsList = theFeature->data()->refsToMe();
106   std::set<AttributePtr>::const_iterator aIt;
107   for (aIt = aRefsList.cbegin(); aIt != aRefsList.cend(); ++aIt) {
108     FeaturePtr aConstrFeature = std::dynamic_pointer_cast<ModelAPI_Feature>((*aIt)->owner());
109     if (aConstrFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID())
110       aCoincident.insert(aConstrFeature);
111   }
112   return aCoincident;
113 }
114
115 void findCoincidences(const FeaturePtr theStartCoin,
116                       const std::string& theAttr,
117                       std::set<FeaturePtr>& theList,
118                       const bool theIsAttrOnly)
119 {
120   AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
121   if(!aPnt) {
122     return;
123   }
124   FeaturePtr aObj = ModelAPI_Feature::feature(aPnt->object());
125   if(theList.find(aObj) == theList.end()) {
126     std::shared_ptr<GeomAPI_Pnt2d> aOrig = getCoincidencePoint(theStartCoin);
127     if(aOrig.get() == NULL) {
128       return;
129     }
130     if(!theIsAttrOnly || !aPnt->isObject()) {
131       theList.insert(aObj);
132     }
133     std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(aObj);
134     std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
135     for (; aCIt != aCoincidences.end(); ++aCIt) {
136       FeaturePtr aConstrFeature = *aCIt;
137       std::shared_ptr<GeomAPI_Pnt2d> aPnt = getCoincidencePoint(aConstrFeature);
138       if(aPnt.get() && aOrig->isEqual(aPnt)) {
139         findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_A(),
140                          theList, theIsAttrOnly);
141         findCoincidences(aConstrFeature, SketchPlugin_ConstraintCoincidence::ENTITY_B(),
142                          theList, theIsAttrOnly);
143       }
144     }
145   }
146 }
147
148 std::set<FeaturePtr> findFeaturesCoincidentToPoint(const AttributePoint2DPtr& thePoint)
149 {
150   std::set<FeaturePtr> aCoincidentFeatures;
151
152   FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
153   aCoincidentFeatures.insert(anOwner);
154
155   std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(anOwner);
156   std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
157   for (; aCIt != aCoincidences.end(); ++aCIt) {
158     bool isPointUsedInCoincidence = false;
159     AttributeRefAttrPtr anOtherCoincidentAttr;
160     for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
161       AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
162       if (!aRefAttr)
163         continue;
164       if (!aRefAttr->isObject() && aRefAttr->attr() == thePoint)
165         isPointUsedInCoincidence = true;
166       else
167         anOtherCoincidentAttr = aRefAttr;
168     }
169
170     if (isPointUsedInCoincidence) {
171       ObjectPtr anObj;
172       if (anOtherCoincidentAttr->isObject())
173         anObj = anOtherCoincidentAttr->object();
174       else
175         anObj = anOtherCoincidentAttr->attr()->owner();
176       aCoincidentFeatures.insert(ModelAPI_Feature::feature(anObj));
177     }
178   }
179
180   return aCoincidentFeatures;
181 }
182
183 // Container for point-point coincidences.
184 // Useful to find points coincident to a given point.
185 class CoincidentPoints
186 {
187 public:
188   void addCoincidence(const AttributePoint2DPtr& thePoint1,
189                       const AttributePoint2DPtr& thePoint2 = AttributePoint2DPtr())
190   {
191     std::list< std::set<AttributePoint2DPtr> >::iterator aFound1 = find(thePoint1);
192     std::list< std::set<AttributePoint2DPtr> >::iterator aFound2 = find(thePoint2);
193     if (aFound1 == myCoincidentPoints.end()) {
194       std::set<AttributePoint2DPtr> aNewSet;
195       aNewSet.insert(thePoint1);
196       if (thePoint2)
197         aNewSet.insert(thePoint2);
198       myCoincidentPoints.push_back(aNewSet);
199     } else if (aFound2 == myCoincidentPoints.end()) {
200       if (thePoint2)
201         aFound1->insert(thePoint2);
202     } else {
203       aFound1->insert(aFound2->begin(), aFound2->end());
204       myCoincidentPoints.erase(aFound2);
205     }
206   }
207
208   std::set<AttributePoint2DPtr> coincidentPoints(const AttributePoint2DPtr& thePoint)
209   {
210     collectCoincidentPoints(thePoint);
211
212     std::list< std::set<AttributePoint2DPtr> >::iterator aFound = find(thePoint);
213     if (aFound == myCoincidentPoints.end())
214       return std::set<AttributePoint2DPtr>();
215     return *aFound;
216   }
217
218 private:
219   void coincidences(const FeaturePtr& theFeature,
220                     std::set<FeaturePtr>& theCoincidences) const
221   {
222     // iterate through coincideces for the given feature
223     std::set<FeaturePtr> aCoincidences = SketchPlugin_Tools::findCoincidentConstraints(theFeature);
224     std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
225     for (; aCIt != aCoincidences.end(); ++aCIt)
226     {
227       if (theCoincidences.find(*aCIt) != theCoincidences.end())
228         continue; // already processed
229       theCoincidences.insert(*aCIt);
230       // iterate on coincident attributes
231       for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
232         AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
233         if (aRefAttr && !aRefAttr->isObject())
234         {
235           FeaturePtr anOwner = ModelAPI_Feature::feature(aRefAttr->attr()->owner());
236           if (anOwner != theFeature)
237             coincidences(anOwner, theCoincidences);
238         }
239       }
240     }
241   }
242
243   // Iteratively search points coincident to the given point
244   // (two points may be coincident through the third point)
245   void collectCoincidentPoints(const AttributePoint2DPtr& thePoint)
246   {
247     AttributePoint2DPtr aPoints[2];
248
249     FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
250     std::set<FeaturePtr> aCoincidences;
251     coincidences(anOwner, aCoincidences);
252
253     std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
254     for (; aCIt != aCoincidences.end(); ++aCIt) {
255       aPoints[0] = AttributePoint2DPtr();
256       aPoints[1] = AttributePoint2DPtr();
257       for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
258         AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
259         if (aRefAttr && !aRefAttr->isObject())
260           aPoints[aPtInd++] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
261       }
262
263       if (aPoints[0] && aPoints[1])
264         addCoincidence(aPoints[0], aPoints[1]);
265     }
266   }
267
268   std::list< std::set<AttributePoint2DPtr> >::iterator find(const AttributePoint2DPtr& thePoint)
269   {
270     std::list< std::set<AttributePoint2DPtr> >::iterator aSeek = myCoincidentPoints.begin();
271     for (; aSeek != myCoincidentPoints.end(); ++aSeek)
272       if (aSeek->find(thePoint) != aSeek->end())
273         return aSeek;
274     return myCoincidentPoints.end();
275   }
276
277 private:
278   std::list< std::set<AttributePoint2DPtr> > myCoincidentPoints;
279 };
280
281 std::set<AttributePoint2DPtr> findPointsCoincidentToPoint(const AttributePoint2DPtr& thePoint)
282 {
283   CoincidentPoints aCoincidentPoints;
284   return aCoincidentPoints.coincidentPoints(thePoint);
285 }
286
287 void resetAttribute(SketchPlugin_Feature* theFeature,
288                     const std::string& theId)
289 {
290   AttributePtr anAttr = theFeature->attribute(theId);
291   if(anAttr.get()) {
292     anAttr->reset();
293   }
294 }
295
296 void createCoincidenceOrTangency(SketchPlugin_Feature* theFeature,
297                                  const std::string& theId,
298                                  const AttributePtr theAttr,
299                                  const ObjectPtr theObject,
300                                  const bool theIsCanBeTangent)
301 {
302   AttributeRefAttrPtr aRefAttr = theFeature->refattr(theId);
303   if(aRefAttr.get() && aRefAttr->isInitialized()) {
304     FeaturePtr aConstraint;
305     if(!theIsCanBeTangent) {
306       aConstraint = theFeature->sketch()
307                               ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
308     } else {
309       if(aRefAttr->isObject()) {
310         ObjectPtr anObject = aRefAttr->object();
311         FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
312         if(aFeature->getKind() == SketchPlugin_Point::ID()) {
313           aConstraint = theFeature->sketch()
314                                   ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
315         } else {
316           aConstraint = theFeature->sketch()
317                                   ->addFeature(SketchPlugin_ConstraintTangent::ID());
318         }
319       } else {
320         aConstraint = theFeature->sketch()
321                                 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
322       }
323     }
324     AttributeRefAttrPtr aRefAttrA = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
325     aRefAttr->isObject() ? aRefAttrA->setObject(aRefAttr->object())
326                          : aRefAttrA->setAttr(aRefAttr->attr());
327     AttributeRefAttrPtr aRefAttrB = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
328     if(theObject.get()) {
329       aRefAttrB->setObject(theObject);
330     } else if(theAttr.get()) {
331       aRefAttrB->setAttr(theAttr);
332     }
333   }
334 }
335
336 void convertRefAttrToPointOrTangentCurve(const AttributeRefAttrPtr&      theRefAttr,
337                                          const AttributePtr&             theDefaultAttr,
338                                          std::shared_ptr<GeomAPI_Shape>& theTangentCurve,
339                                          std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
340 {
341   AttributePtr anAttr = theDefaultAttr;
342   if (theRefAttr->isObject()) {
343     FeaturePtr aTgFeature = ModelAPI_Feature::feature(theRefAttr->object());
344     if (aTgFeature) {
345       if (aTgFeature->getKind() != SketchPlugin_Point::ID()) {
346         theTangentCurve = aTgFeature->lastResult()->shape();
347         return;
348       }
349       anAttr = aTgFeature->attribute(SketchPlugin_Point::COORD_ID());
350     }
351   } else
352     anAttr = theRefAttr->attr();
353
354   thePassingPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr)->pnt();
355 }
356
357
358 FeaturePtr createConstraintAttrAttr(SketchPlugin_Sketch* theSketch,
359                                     const std::string& theConstraintId,
360                                     const AttributePtr& theFirstAttribute,
361                                     const AttributePtr& theSecondAttribute)
362 {
363   FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
364   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
365                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
366   aRefAttr->setAttr(theFirstAttribute);
367
368   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
369                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
370   aRefAttr->setAttr(theSecondAttribute);
371
372 #if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT)
373   std::cout << "<createConstraint to attribute> :"
374             << " first attribute - " << theFirstAttribute->id()
375             << " second attribute - " << theSecondAttribute->id()
376             << std::endl;
377 #endif
378
379   return aConstraint;
380 }
381
382 FeaturePtr createConstraintAttrObject(SketchPlugin_Sketch* theSketch,
383                                       const std::string& theConstraintId,
384                                       const AttributePtr& theFirstAttribute,
385                                       const ObjectPtr& theSecondObject)
386 {
387   FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
388   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
389                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
390   aRefAttr->setAttr(theFirstAttribute);
391
392   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
393                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
394   aRefAttr->setObject(theSecondObject);
395
396 #if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT)
397   std::cout << "<createConstraint to attribute> :"
398             << " first attribute - " << theFirstAttribute->id()
399             << " second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
400             << std::endl;
401 #endif
402
403   return aConstraint;
404 }
405
406 FeaturePtr createConstraintObjectObject(SketchPlugin_Sketch* theSketch,
407                                         const std::string& theConstraintId,
408                                         const ObjectPtr& theFirstObject,
409                                         const ObjectPtr& theSecondObject)
410 {
411   FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
412   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
413                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
414   aRefAttr->setObject(theFirstObject);
415
416   aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
417                                  aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
418   aRefAttr->setObject(theSecondObject);
419
420 #if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT)
421   std::cout << "<createConstraint to attribute> :"
422             << " first object - " << ModelAPI_Feature::feature(theFirstObject)->getKind()
423             << " second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
424             << std::endl;
425 #endif
426
427   return aConstraint;
428 }
429
430 GeomPnt2dPtr flyoutPointCoordinates(const ConstraintPtr& theConstraint)
431 {
432   // currently process Length constraints only
433   if (theConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
434     return GeomPnt2dPtr();
435
436   AttributeRefAttrPtr aLineAttr = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
437   if (!aLineAttr || !aLineAttr->isObject())
438     return GeomPnt2dPtr();
439   FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
440   if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
441     return GeomPnt2dPtr();
442
443   std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
444       aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
445   std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
446       aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
447
448   std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
449       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
450       theConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
451   std::shared_ptr<GeomAPI_Pnt2d> aFltPnt = aFlyoutAttr->pnt();
452
453   std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
454
455   double X = aStartPnt->x() + aFltPnt->x() * aLineDir->x() - aFltPnt->y() * aLineDir->y();
456   double Y = aStartPnt->y() + aFltPnt->x() * aLineDir->y() + aFltPnt->y() * aLineDir->x();
457
458   return GeomPnt2dPtr(new GeomAPI_Pnt2d(X, Y));
459 }
460
461 } // namespace SketchPlugin_Tools