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_ConstraintTangent.h"
25 #include "SketchPlugin_Point.h"
26 #include "SketchPlugin_SketchEntity.h"
28 #include <SketcherPrs_Tools.h>
30 #include <ModelAPI_AttributeDouble.h>
32 #include <GeomDataAPI_Point.h>
33 #include <GeomDataAPI_Point2D.h>
35 namespace SketchPlugin_Tools {
37 void clearExpressions(AttributeDoublePtr theAttribute)
39 theAttribute->setText(std::string());
42 void clearExpressions(AttributePointPtr theAttribute)
44 theAttribute->setText(std::string(), std::string(), std::string());
47 void clearExpressions(AttributePoint2DPtr theAttribute)
49 theAttribute->setText(std::string(), std::string());
52 void clearExpressions(AttributePtr theAttribute)
55 AttributeDoublePtr anAttributeDouble =
56 std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theAttribute);
57 if (anAttributeDouble.get())
58 clearExpressions(anAttributeDouble);
60 AttributePointPtr anAttributePoint =
61 std::dynamic_pointer_cast<GeomDataAPI_Point>(theAttribute);
62 if (anAttributePoint.get())
63 clearExpressions(anAttributePoint);
65 AttributePoint2DPtr anAttributePoint2D =
66 std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theAttribute);
67 if (anAttributePoint2D.get())
68 clearExpressions(anAttributePoint2D);
71 void clearExpressions(FeaturePtr theFeature)
73 if (!theFeature.get())
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);
83 std::shared_ptr<GeomAPI_Pnt2d> getCoincidencePoint(const FeaturePtr theStartCoin)
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());
92 std::set<FeaturePtr> findCoincidentConstraints(const FeaturePtr& theFeature)
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);
105 void findCoincidences(const FeaturePtr theStartCoin,
106 const std::string& theAttr,
107 std::set<FeaturePtr>& theList)
109 AttributeRefAttrPtr aPnt = theStartCoin->refattr(theAttr);
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) {
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);
133 std::set<FeaturePtr> findFeaturesCoincidentToPoint(const AttributePoint2DPtr& thePoint)
135 std::set<FeaturePtr> aCoincidentFeatures;
137 FeaturePtr anOwner = ModelAPI_Feature::feature(thePoint->owner());
138 aCoincidentFeatures.insert(anOwner);
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));
149 if (!aRefAttr->isObject() && aRefAttr->attr() == thePoint)
150 isPointUsedInCoincidence = true;
152 anOtherCoincidentAttr = aRefAttr;
155 if (isPointUsedInCoincidence) {
157 if (anOtherCoincidentAttr->isObject())
158 anObj = anOtherCoincidentAttr->object();
160 anObj = anOtherCoincidentAttr->attr()->owner();
161 aCoincidentFeatures.insert(ModelAPI_Feature::feature(anObj));
165 return aCoincidentFeatures;
168 // Container for point-point coincidences.
169 // Useful to find points coincident to a given point.
170 class CoincidentPoints
173 void addCoincidence(const AttributePoint2DPtr& thePoint1,
174 const AttributePoint2DPtr& thePoint2 = AttributePoint2DPtr())
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);
182 aNewSet.insert(thePoint2);
183 myCoincidentPoints.push_back(aNewSet);
184 } else if (aFound2 == myCoincidentPoints.end()) {
186 aFound1->insert(thePoint2);
188 aFound1->insert(aFound2->begin(), aFound2->end());
189 myCoincidentPoints.erase(aFound2);
193 std::set<AttributePoint2DPtr> coincidentPoints(const AttributePoint2DPtr& thePoint)
195 std::list< std::set<AttributePoint2DPtr> >::iterator aFound = find(thePoint);
196 if (aFound == myCoincidentPoints.end())
197 return std::set<AttributePoint2DPtr>();
202 std::list< std::set<AttributePoint2DPtr> >::iterator find(const AttributePoint2DPtr& thePoint)
204 std::list< std::set<AttributePoint2DPtr> >::iterator aSeek = myCoincidentPoints.begin();
205 for (; aSeek != myCoincidentPoints.end(); ++aSeek)
206 if (aSeek->find(thePoint) != aSeek->end())
208 return myCoincidentPoints.end();
212 std::list< std::set<AttributePoint2DPtr> > myCoincidentPoints;
215 std::set<AttributePoint2DPtr> findPointsCoincidentToPoint(const AttributePoint2DPtr& thePoint)
217 CoincidentPoints aCoincidentPoints;
218 AttributePoint2DPtr aPoints[2];
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));
230 if (!aRefAttr->isObject())
231 aPoints[aPtInd++] = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aRefAttr->attr());
235 aCoincidentPoints.addCoincidence(aPoints[0], aPoints[1]);
238 return aCoincidentPoints.coincidentPoints(thePoint);
241 void resetAttribute(SketchPlugin_Feature* theFeature,
242 const std::string& theId)
244 AttributePtr anAttr = theFeature->attribute(theId);
250 void createConstraint(SketchPlugin_Feature* theFeature,
251 const std::string& theId,
252 const AttributePtr theAttr,
253 const ObjectPtr theObject,
254 const bool theIsCanBeTangent)
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());
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());
270 aConstraint = theFeature->sketch()
271 ->addFeature(SketchPlugin_ConstraintTangent::ID());
274 aConstraint = theFeature->sketch()
275 ->addFeature(SketchPlugin_ConstraintCoincidence::ID());
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);
290 void convertRefAttrToPointOrTangentCurve(const AttributeRefAttrPtr& theRefAttr,
291 const AttributePtr& theDefaultAttr,
292 std::shared_ptr<GeomAPI_Shape>& theTangentCurve,
293 std::shared_ptr<GeomAPI_Pnt2d>& thePassingPoint)
295 AttributePtr anAttr = theDefaultAttr;
296 if (theRefAttr->isObject()) {
297 FeaturePtr aTgFeature = ModelAPI_Feature::feature(theRefAttr->object());
299 if (aTgFeature->getKind() != SketchPlugin_Point::ID()) {
300 theTangentCurve = aTgFeature->lastResult()->shape();
303 anAttr = aTgFeature->attribute(SketchPlugin_Point::COORD_ID());
306 anAttr = theRefAttr->attr();
308 thePassingPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr)->pnt();
311 } // namespace SketchPlugin_Tools