1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "SketchPlugin_Tools.h"
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"
30 #include <SketcherPrs_Tools.h>
32 #include <ModelAPI_AttributeDouble.h>
34 #include <GeomAPI_Dir2d.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_XY.h>
38 #include <GeomDataAPI_Point.h>
39 #include <GeomDataAPI_Point2D.h>
45 namespace SketchPlugin_Tools {
47 void clearExpressions(AttributeDoublePtr theAttribute)
49 theAttribute->setText(std::string());
52 void clearExpressions(AttributePointPtr theAttribute)
54 theAttribute->setText(std::string(), std::string(), std::string());
57 void clearExpressions(AttributePoint2DPtr theAttribute)
59 theAttribute->setText(std::string(), std::string());
62 void clearExpressions(AttributePtr theAttribute)
65 AttributeDoublePtr anAttributeDouble =
66 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
67 if (anAttributeDouble.get())
68 clearExpressions(anAttributeDouble);
70 AttributePointPtr anAttributePoint =
71 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
72 if (anAttributePoint.get())
73 clearExpressions(anAttributePoint);
75 AttributePoint2DPtr anAttributePoint2D =
76 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
77 if (anAttributePoint2D.get())
78 clearExpressions(anAttributePoint2D);
81 void clearExpressions(FeaturePtr theFeature)
83 if (!theFeature.get())
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);
93 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(const FeaturePtr theStartCoin)
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());
102 std::set<FeaturePtr> findCoincidentConstraints(const FeaturePtr& theFeature)
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);
115 void findCoincidences(const FeaturePtr theStartCoin,
116 const std::string& theAttr,
117 std::set<FeaturePtr>& theList,
118 const bool theIsAttrOnly)
120 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
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) {
130 if(!theIsAttrOnly || !aPnt->isObject()) {
131 theList.insert(aObj);
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);
148 std::set<FeaturePtr> findFeaturesCoincidentToPoint(const AttributePoint2DPtr& thePoint)
150 std::set<FeaturePtr> aCoincidentFeatures;
152 FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
153 aCoincidentFeatures.insert(anOwner);
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));
164 if (!aRefAttr->isObject() && aRefAttr->attr() == thePoint)
165 isPointUsedInCoincidence = true;
167 anOtherCoincidentAttr = aRefAttr;
170 if (isPointUsedInCoincidence) {
172 if (anOtherCoincidentAttr->isObject())
173 anObj = anOtherCoincidentAttr->object();
175 anObj = anOtherCoincidentAttr->attr()->owner();
176 aCoincidentFeatures.insert(ModelAPI_Feature::feature(anObj));
180 return aCoincidentFeatures;
183 // Container for point-point coincidences.
184 // Useful to find points coincident to a given point.
185 class CoincidentPoints
188 void addCoincidence(const AttributePoint2DPtr& thePoint1,
189 const AttributePoint2DPtr& thePoint2 = AttributePoint2DPtr())
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);
197 aNewSet.insert(thePoint2);
198 myCoincidentPoints.push_back(aNewSet);
199 } else if (aFound2 == myCoincidentPoints.end()) {
201 aFound1->insert(thePoint2);
203 aFound1->insert(aFound2->begin(), aFound2->end());
204 myCoincidentPoints.erase(aFound2);
208 std::set<AttributePoint2DPtr> coincidentPoints(const AttributePoint2DPtr& thePoint)
210 std::list< std::set<AttributePoint2DPtr> >::iterator aFound = find(thePoint);
211 if (aFound == myCoincidentPoints.end())
212 return std::set<AttributePoint2DPtr>();
217 std::list< std::set<AttributePoint2DPtr> >::iterator find(const AttributePoint2DPtr& thePoint)
219 std::list< std::set<AttributePoint2DPtr> >::iterator aSeek = myCoincidentPoints.begin();
220 for (; aSeek != myCoincidentPoints.end(); ++aSeek)
221 if (aSeek->find(thePoint) != aSeek->end())
223 return myCoincidentPoints.end();
227 std::list< std::set<AttributePoint2DPtr> > myCoincidentPoints;
230 std::set<AttributePoint2DPtr> findPointsCoincidentToPoint(const AttributePoint2DPtr& thePoint)
232 CoincidentPoints aCoincidentPoints;
233 AttributePoint2DPtr aPoints[2];
235 FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
236 std::set<FeaturePtr> aCoincidences = findCoincidentConstraints(anOwner);
237 std::set<FeaturePtr>::const_iterator aCIt = aCoincidences.begin();
238 for (; aCIt != aCoincidences.end(); ++aCIt) {
239 aPoints[0] = AttributePoint2DPtr();
240 aPoints[1] = AttributePoint2DPtr();
241 for (int i = 0, aPtInd = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
242 AttributeRefAttrPtr aRefAttr = (*aCIt)->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
245 if (!aRefAttr->isObject())
246 aPoints[aPtInd++] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
250 aCoincidentPoints.addCoincidence(aPoints[0], aPoints[1]);
253 return aCoincidentPoints.coincidentPoints(thePoint);
256 void resetAttribute(SketchPlugin_Feature* theFeature,
257 const std::string& theId)
259 AttributePtr anAttr = theFeature->attribute(theId);
265 void createCoincidenceOrTangency(SketchPlugin_Feature* theFeature,
266 const std::string& theId,
267 const AttributePtr theAttr,
268 const ObjectPtr theObject,
269 const bool theIsCanBeTangent)
271 AttributeRefAttrPtr aRefAttr = theFeature->refattr(theId);
272 if(aRefAttr.get() && aRefAttr->isInitialized()) {
273 FeaturePtr aConstraint;
274 if(!theIsCanBeTangent) {
275 aConstraint = theFeature->sketch()
276 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
278 if(aRefAttr->isObject()) {
279 ObjectPtr anObject = aRefAttr->object();
280 FeaturePtr aFeature = ModelAPI_Feature::feature(anObject);
281 if(aFeature->getKind() == SketchPlugin_Point::ID()) {
282 aConstraint = theFeature->sketch()
283 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
285 aConstraint = theFeature->sketch()
286 ->addFeature(SketchPlugin_ConstraintTangent::ID());
289 aConstraint = theFeature->sketch()
290 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
293 AttributeRefAttrPtr aRefAttrA = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
294 aRefAttr->isObject() ? aRefAttrA->setObject(aRefAttr->object())
295 : aRefAttrA->setAttr(aRefAttr->attr());
296 AttributeRefAttrPtr aRefAttrB = aConstraint->refattr(SketchPlugin_Constraint::ENTITY_B());
297 if(theObject.get()) {
298 aRefAttrB->setObject(theObject);
299 } else if(theAttr.get()) {
300 aRefAttrB->setAttr(theAttr);
305 void convertRefAttrToPointOrTangentCurve(const AttributeRefAttrPtr& theRefAttr,
306 const AttributePtr& theDefaultAttr,
307 std::shared_ptr<GeomAPI_Shape>& theTangentCurve,
308 std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
310 AttributePtr anAttr = theDefaultAttr;
311 if (theRefAttr->isObject()) {
312 FeaturePtr aTgFeature = ModelAPI_Feature::feature(theRefAttr->object());
314 if (aTgFeature->getKind() != SketchPlugin_Point::ID()) {
315 theTangentCurve = aTgFeature->lastResult()->shape();
318 anAttr = aTgFeature->attribute(SketchPlugin_Point::COORD_ID());
321 anAttr = theRefAttr->attr();
323 thePassingPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr)->pnt();
327 FeaturePtr createConstraintAttrAttr(SketchPlugin_Sketch* theSketch,
328 const std::string& theConstraintId,
329 const AttributePtr& theFirstAttribute,
330 const AttributePtr& theSecondAttribute)
332 FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
333 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
334 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
335 aRefAttr->setAttr(theFirstAttribute);
337 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
338 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
339 aRefAttr->setAttr(theSecondAttribute);
341 #if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT)
342 std::cout << "<createConstraint to attribute> :"
343 << " first attribute - " << theFirstAttribute->id()
344 << " second attribute - " << theSecondAttribute->id()
351 FeaturePtr createConstraintAttrObject(SketchPlugin_Sketch* theSketch,
352 const std::string& theConstraintId,
353 const AttributePtr& theFirstAttribute,
354 const ObjectPtr& theSecondObject)
356 FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
357 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
358 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
359 aRefAttr->setAttr(theFirstAttribute);
361 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
362 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
363 aRefAttr->setObject(theSecondObject);
365 #if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT)
366 std::cout << "<createConstraint to attribute> :"
367 << " first attribute - " << theFirstAttribute->id()
368 << " second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
375 FeaturePtr createConstraintObjectObject(SketchPlugin_Sketch* theSketch,
376 const std::string& theConstraintId,
377 const ObjectPtr& theFirstObject,
378 const ObjectPtr& theSecondObject)
380 FeaturePtr aConstraint = theSketch->addFeature(theConstraintId);
381 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
382 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
383 aRefAttr->setObject(theFirstObject);
385 aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
386 aConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
387 aRefAttr->setObject(theSecondObject);
389 #if defined(DEBUG_TRIM) || defined(DEBUG_SPLIT)
390 std::cout << "<createConstraint to attribute> :"
391 << " first object - " << ModelAPI_Feature::feature(theFirstObject)->getKind()
392 << " second object - " << ModelAPI_Feature::feature(theSecondObject)->getKind()
399 GeomPnt2dPtr flyoutPointCoordinates(const ConstraintPtr& theConstraint)
401 // currently process Length constraints only
402 if (theConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
403 return GeomPnt2dPtr();
405 AttributeRefAttrPtr aLineAttr = theConstraint->refattr(SketchPlugin_Constraint::ENTITY_A());
406 if (!aLineAttr || !aLineAttr->isObject())
407 return GeomPnt2dPtr();
408 FeaturePtr aLine = ModelAPI_Feature::feature(aLineAttr->object());
409 if (!aLine || aLine->getKind() != SketchPlugin_Line::ID())
410 return GeomPnt2dPtr();
412 std::shared_ptr<GeomAPI_XY> aStartPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
413 aLine->attribute(SketchPlugin_Line::START_ID()))->pnt()->xy();
414 std::shared_ptr<GeomAPI_XY> aEndPnt = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
415 aLine->attribute(SketchPlugin_Line::END_ID()))->pnt()->xy();
417 std::shared_ptr<GeomDataAPI_Point2D> aFlyoutAttr =
418 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
419 theConstraint->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
420 std::shared_ptr<GeomAPI_Pnt2d> aFltPnt = aFlyoutAttr->pnt();
422 std::shared_ptr<GeomAPI_Dir2d> aLineDir(new GeomAPI_Dir2d(aEndPnt->decreased(aStartPnt)));
424 double X = aStartPnt->x() + aFltPnt->x() * aLineDir->x() - aFltPnt->y() * aLineDir->y();
425 double Y = aStartPnt->y() + aFltPnt->x() * aLineDir->y() + aFltPnt->y() * aLineDir->x();
427 return GeomPnt2dPtr(new GeomAPI_Pnt2d(X, Y));
430 } // namespace SketchPlugin_Tools