1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Storage.cpp
4 // Created: 30 Nov 2015
5 // Author: Artem ZHIDKOV
7 #include <SketchSolver_Storage.h>
8 #include <SketchSolver_Manager.h>
10 #include <GeomDataAPI_Point2D.h>
11 #include <ModelAPI_AttributeRefAttr.h>
12 #include <ModelAPI_AttributeRefList.h>
13 #include <SketchPlugin_Arc.h>
14 #include <SketchPlugin_Circle.h>
15 #include <SketchPlugin_Line.h>
16 #include <SketchPlugin_Point.h>
17 #include <SketchPlugin_IntersectionPoint.h>
18 #include <SketchPlugin_ConstraintCoincidence.h>
19 #include <SketchPlugin_ConstraintMirror.h>
20 #include <SketchPlugin_ConstraintRigid.h>
23 /// \brief Verify two vectors of constraints are equal.
24 /// Vectors differ by the order of elements are equal.
25 static bool isEqual(const std::list<ConstraintWrapperPtr>& theCVec1,
26 const std::list<ConstraintWrapperPtr>& theCVec2);
29 void SketchSolver_Storage::addConstraint(ConstraintPtr theConstraint,
30 ConstraintWrapperPtr theSolverConstraint)
32 if (theSolverConstraint) {
33 std::list<ConstraintWrapperPtr> aConstrList(1, theSolverConstraint);
34 addConstraint(theConstraint, aConstrList);
36 addConstraint(theConstraint, std::list<ConstraintWrapperPtr>());
39 void SketchSolver_Storage::addConstraint(
40 ConstraintPtr theConstraint,
41 std::list<ConstraintWrapperPtr> theSolverConstraints)
43 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
44 aFound = myConstraintMap.find(theConstraint);
45 if (aFound == myConstraintMap.end() || !isEqual(aFound->second, theSolverConstraints))
46 setNeedToResolve(true);
48 if (theSolverConstraints.empty()) {
49 // constraint links to the empty list, add its attributes linked to the empty entities
50 std::list<AttributePtr> aRefAttrs =
51 theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
52 std::list<AttributePtr>::const_iterator anAttrIt = aRefAttrs.begin();
53 for (; anAttrIt != aRefAttrs.end(); ++anAttrIt) {
54 AttributeRefAttrPtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIt);
55 if (aRef->isObject()) {
56 FeaturePtr aFeature = ModelAPI_Feature::feature(aRef->object());
57 if (aFeature) addEntity(aFeature, EntityWrapperPtr());
59 addEntity(aRef->attr(), EntityWrapperPtr());
61 std::list<AttributePtr> aRefLists =
62 theConstraint->data()->attributes(ModelAPI_AttributeRefList::typeId());
63 for (anAttrIt = aRefLists.begin(); anAttrIt != aRefLists.end(); ++anAttrIt) {
64 AttributeRefListPtr aRef = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anAttrIt);
65 std::list<ObjectPtr> anObj = aRef->list();
66 std::list<ObjectPtr>::iterator anIt = anObj.begin();
67 for (; anIt != anObj.end(); ++anIt) {
68 FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
69 if (aFeature) addEntity(aFeature, EntityWrapperPtr());
73 else if (theSolverConstraints.front()->type() != CONSTRAINT_PT_PT_COINCIDENT) {
74 // Do not add point-point coincidence, because it is already made by setting
75 // the same parameters for both points
76 std::list<ConstraintWrapperPtr>::iterator aCIt = theSolverConstraints.begin();
77 for (; aCIt != theSolverConstraints.end(); ++aCIt)
81 if (!theSolverConstraints.empty() || aFound == myConstraintMap.end())
82 myConstraintMap[theConstraint] = theSolverConstraints;
83 // block events if necessary
84 if (myEventsBlocked && theConstraint && theConstraint->data() && theConstraint->data()->isValid())
85 theConstraint->data()->blockSendAttributeUpdated(myEventsBlocked);
88 static std::list<AttributePtr> pointAttributes(FeaturePtr theFeature)
90 std::list<AttributePtr> aPoints;
91 if (theFeature->getKind() == SketchPlugin_Arc::ID()) {
92 aPoints.push_back(theFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
93 aPoints.push_back(theFeature->attribute(SketchPlugin_Arc::START_ID()));
94 aPoints.push_back(theFeature->attribute(SketchPlugin_Arc::END_ID()));
96 else if (theFeature->getKind() == SketchPlugin_Circle::ID())
97 aPoints.push_back(theFeature->attribute(SketchPlugin_Circle::CENTER_ID()));
98 else if (theFeature->getKind() == SketchPlugin_Line::ID()) {
99 aPoints.push_back(theFeature->attribute(SketchPlugin_Line::START_ID()));
100 aPoints.push_back(theFeature->attribute(SketchPlugin_Line::END_ID()));
102 else if (theFeature->getKind() == SketchPlugin_Point::ID() ||
103 theFeature->getKind() == SketchPlugin_IntersectionPoint::ID())
104 aPoints.push_back(theFeature->attribute(SketchPlugin_Point::COORD_ID()));
108 void SketchSolver_Storage::addEntity(FeaturePtr theFeature,
109 EntityWrapperPtr theSolverEntity)
111 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFound = myFeatureMap.find(theFeature);
112 if (aFound == myFeatureMap.end() || !aFound->second ||
113 (theSolverEntity && !aFound->second->isEqual(theSolverEntity)))
114 setNeedToResolve(true); // the entity is new or modified
116 if (!theSolverEntity) {
117 // feature links to the empty entity, add its attributes
118 std::list<AttributePtr> aPntAttrs = pointAttributes(theFeature);
119 std::list<AttributePtr>::const_iterator anAttrIt = aPntAttrs.begin();
120 for (; anAttrIt != aPntAttrs.end(); ++anAttrIt)
121 addEntity(*anAttrIt, EntityWrapperPtr());
122 if (aFound == myFeatureMap.end())
123 myFeatureMap[theFeature] = theSolverEntity;
125 myFeatureMap[theFeature] = theSolverEntity;
127 // block events if necessary
128 if (myEventsBlocked && theFeature->data() && theFeature->data()->isValid())
129 theFeature->data()->blockSendAttributeUpdated(myEventsBlocked);
132 void SketchSolver_Storage::addEntity(AttributePtr theAttribute,
133 EntityWrapperPtr theSolverEntity)
135 std::map<AttributePtr, EntityWrapperPtr>::const_iterator aFound = myAttributeMap.find(theAttribute);
136 if (aFound == myAttributeMap.end() || !aFound->second ||
137 (theSolverEntity && !aFound->second->isEqual(theSolverEntity)))
138 setNeedToResolve(true); // the entity is new or modified
140 if (theSolverEntity || aFound == myAttributeMap.end())
141 myAttributeMap[theAttribute] = theSolverEntity;
142 // block events if necessary
143 if (myEventsBlocked && theAttribute->owner() &&
144 theAttribute->owner()->data() && theAttribute->owner()->data()->isValid())
145 theAttribute->owner()->data()->blockSendAttributeUpdated(myEventsBlocked);
149 static bool isCopyInMulti(std::shared_ptr<SketchPlugin_Feature> theFeature,
150 const std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >& theConstraints)
152 bool aResult = theFeature->isCopy();
154 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
155 anIt = theConstraints.begin();
156 for (; anIt != theConstraints.end() && aResult; ++anIt) {
157 if (anIt->first->getKind() != SketchPlugin_ConstraintMirror::ID())
159 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
160 anIt->first->attribute(SketchPlugin_Constraint::ENTITY_C()));
161 std::list<ObjectPtr> aMirroredList = aRefList->list();
162 std::list<ObjectPtr>::const_iterator aMIt = aMirroredList.begin();
163 for (; aMIt != aMirroredList.end() && aResult; ++aMIt) {
164 FeaturePtr aFeat = ModelAPI_Feature::feature(*aMIt);
165 if (aFeat == theFeature)
173 bool SketchSolver_Storage::update(FeaturePtr theFeature, const GroupID& theGroup)
175 bool isUpdated = false;
176 EntityWrapperPtr aRelated = entity(theFeature);
177 if (!aRelated) { // Feature is not exist, create it
178 std::list<EntityWrapperPtr> aSubs;
179 // Reserve the feature in the map of features (do not want to add several copies of it)
180 myFeatureMap[theFeature] = aRelated;
181 // Firstly, create/update its attributes
182 std::list<AttributePtr> anAttrs = pointAttributes(theFeature);
183 std::list<AttributePtr>::const_iterator anIt = anAttrs.begin();
184 for (; anIt != anAttrs.end(); ++anIt) {
185 isUpdated = update(*anIt, theGroup) || isUpdated;
186 aSubs.push_back(entity(*anIt));
188 // If the feature is a circle, add its radius as a sub
189 if (theFeature->getKind() == SketchPlugin_Circle::ID()) {
190 AttributePtr aRadius = theFeature->attribute(SketchPlugin_Circle::RADIUS_ID());
191 isUpdated = update(aRadius, theGroup) || isUpdated;
192 aSubs.push_back(entity(aRadius));
194 // If the feature if circle or arc, we need to add normal of the sketch to the list of subs
195 if (theFeature->getKind() == SketchPlugin_Arc::ID() ||
196 theFeature->getKind() == SketchPlugin_Circle::ID()) {
197 EntityWrapperPtr aNormal = getNormal();
198 if (aNormal) aSubs.push_back(aNormal);
200 // Secondly, convert feature
201 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
202 GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
203 // Check external feature
204 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
205 std::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
206 if (aSketchFeature && (aSketchFeature->isExternal() || isCopyInMulti(aSketchFeature, myConstraintMap)))
207 aGroup = GID_OUTOFGROUP;
208 aRelated = aBuilder->createFeature(theFeature, aSubs, aGroup);
211 addEntity(theFeature, aRelated);
212 } else if (theGroup != GID_UNKNOWN)
213 changeGroup(aRelated, theGroup);
214 return update(aRelated) || isUpdated;
217 bool SketchSolver_Storage::update(AttributePtr theAttribute, const GroupID& theGroup)
219 AttributePtr anAttribute = theAttribute;
220 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
222 if (aRefAttr->isObject()) {
223 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
224 return update(aFeature, theGroup);
226 anAttribute = aRefAttr->attr();
229 EntityWrapperPtr aRelated = entity(anAttribute);
230 if (!aRelated) { // Attribute is not exist, create it
231 // verify the attribute is a point of arc and add whole arc
232 if (anAttribute->owner()) {
233 FeaturePtr aFeature = ModelAPI_Feature::feature(anAttribute->owner());
234 if (aFeature->getKind() == SketchPlugin_Arc::ID() &&
235 myFeatureMap.find(aFeature) == myFeatureMap.end()) {
236 // Additional checking that all attributes are initialized
237 if (aFeature->attribute(SketchPlugin_Arc::CENTER_ID())->isInitialized() &&
238 aFeature->attribute(SketchPlugin_Arc::START_ID())->isInitialized() &&
239 aFeature->attribute(SketchPlugin_Arc::END_ID())->isInitialized()) {
240 return SketchSolver_Storage::update(aFeature);
242 myFeatureMap[aFeature] = EntityWrapperPtr();
247 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
248 GroupID aGroup = theGroup != GID_UNKNOWN ? theGroup : myGroupID;
249 // Check attribute of external features
250 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
251 std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
252 if (aSketchFeature && (aSketchFeature->isExternal() || isCopyInMulti(aSketchFeature, myConstraintMap)))
253 aGroup = GID_OUTOFGROUP;
254 aRelated = aBuilder->createAttribute(anAttribute, aGroup);
257 addEntity(anAttribute, aRelated);
258 } else if (theGroup != GID_UNKNOWN)
259 changeGroup(aRelated, theGroup);
260 return update(aRelated);
265 const std::list<ConstraintWrapperPtr>& SketchSolver_Storage::constraint(
266 const ConstraintPtr& theConstraint) const
268 static std::list<ConstraintWrapperPtr> aDummy;
270 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr>>::const_iterator
271 aFound = myConstraintMap.find(theConstraint);
272 if (aFound != myConstraintMap.end())
273 return aFound->second;
277 const EntityWrapperPtr& SketchSolver_Storage::entity(const FeaturePtr& theFeature) const
279 static EntityWrapperPtr aDummy;
281 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFound = myFeatureMap.find(theFeature);
282 if (aFound != myFeatureMap.end())
283 return aFound->second;
287 const EntityWrapperPtr& SketchSolver_Storage::entity(const AttributePtr& theAttribute) const
289 static EntityWrapperPtr aDummy;
291 std::map<AttributePtr, EntityWrapperPtr>::const_iterator
292 aFound = myAttributeMap.find(theAttribute);
293 if (aFound != myAttributeMap.end())
294 return aFound->second;
296 AttributeRefAttrPtr aRefAttr =
297 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
299 if (aRefAttr->isObject()) {
300 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
301 return entity(aFeature);
303 return entity(aRefAttr->attr());
308 bool SketchSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
310 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::iterator
311 aFound = myConstraintMap.find(theConstraint);
312 if (aFound == myConstraintMap.end())
313 return true; // no constraint, already deleted
316 std::list<ConstraintWrapperPtr> aConstrList = aFound->second;
317 myConstraintMap.erase(aFound);
318 // Remove SolveSpace constraints
319 bool isFullyRemoved = true;
320 std::list<ConstraintWrapperPtr>::iterator anIt = aConstrList.begin();
321 while (anIt != aConstrList.end()) {
323 std::list<ConstraintWrapperPtr>::iterator aRemoveIt = anIt++;
324 aConstrList.erase(aRemoveIt);
326 isFullyRemoved = false;
330 return isFullyRemoved;
333 template <class ENT_TYPE>
334 static bool isUsed(ConstraintWrapperPtr theConstraint, ENT_TYPE theEntity)
336 if (!theConstraint || !theEntity)
338 std::list<EntityWrapperPtr>::const_iterator anEntIt = theConstraint->entities().begin();
339 for (; anEntIt != theConstraint->entities().end(); ++anEntIt)
340 if ((*anEntIt)->isBase(theEntity))
345 static bool isUsed(EntityWrapperPtr theFeature, AttributePtr theSubEntity)
347 if (!theFeature || !theSubEntity)
349 std::list<EntityWrapperPtr>::const_iterator aSubIt = theFeature->subEntities().begin();
350 for (; aSubIt != theFeature->subEntities().end(); ++aSubIt)
351 if ((*aSubIt)->isBase(theSubEntity))
356 static bool isUsed(ConstraintPtr theConstraint, AttributePtr theAttribute)
358 if (!theConstraint || !theAttribute)
360 std::list<AttributePtr> anAttrList = theConstraint->data()->attributes(std::string());
361 std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
362 for (; anIt != anAttrList.end(); ++anIt) {
363 if (*anIt == theAttribute)
365 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIt);
366 if (aRefAttr && !aRefAttr->isObject() && aRefAttr->attr() == theAttribute)
372 bool SketchSolver_Storage::isUsed(FeaturePtr theFeature) const
374 if (myFeatureMap.find(theFeature) != myFeatureMap.end())
377 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
378 aCIt = myConstraintMap.begin();
379 std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
380 for (; aCIt != myConstraintMap.end(); ++aCIt)
381 for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
382 if (::isUsed(*aCWIt, theFeature))
385 std::list<AttributePtr> anAttrList = pointAttributes(theFeature);
386 std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
387 for (; anIt != anAttrList.end(); ++anIt)
393 bool SketchSolver_Storage::isUsed(AttributePtr theAttribute) const
395 AttributePtr anAttribute = theAttribute;
396 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
398 if (aRefAttr->isObject())
399 return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
401 anAttribute = aRefAttr->attr();
404 if (myAttributeMap.find(theAttribute) != myAttributeMap.end())
406 // check in constraints
407 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
408 aCIt = myConstraintMap.begin();
409 std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
410 for (; aCIt != myConstraintMap.end(); ++aCIt) {
411 for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
412 if (::isUsed(*aCWIt, anAttribute))
414 // Additional check for the Fixed constraints, which have no wrapper associated.
415 if (aCIt->first->getKind() == SketchPlugin_ConstraintRigid::ID() &&
416 ::isUsed(aCIt->first, anAttribute))
420 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
421 for (; aFIt != myFeatureMap.end(); ++aFIt)
422 if (::isUsed(aFIt->second, anAttribute))
428 bool SketchSolver_Storage::removeEntity(FeaturePtr theFeature)
430 std::map<FeaturePtr, EntityWrapperPtr>::iterator aFound = myFeatureMap.find(theFeature);
431 if (aFound == myFeatureMap.end())
432 return true; // feature not found, nothing to delete
434 EntityWrapperPtr anEntity = aFound->second;
435 myFeatureMap.erase(aFound);
437 // Check if the feature is not used by constraints, remove it
438 if (!anEntity || (!isUsed(theFeature) && remove(anEntity)))
441 // feature is not removed, revert operation
442 myFeatureMap[theFeature] = anEntity;
447 bool SketchSolver_Storage::removeEntity(AttributePtr theAttribute)
449 std::map<AttributePtr, EntityWrapperPtr>::iterator aFound = myAttributeMap.find(theAttribute);
450 if (aFound == myAttributeMap.end())
451 return true; // attribute not found, nothing to delete
453 EntityWrapperPtr anEntity = aFound->second;
454 myAttributeMap.erase(aFound);
456 // Check if the attribute is not used by constraints and features, remove it
457 if (!anEntity || (!isUsed(theAttribute) && remove(anEntity)))
460 // attribute is not removed, revert operation
461 myAttributeMap[theAttribute] = anEntity;
467 bool SketchSolver_Storage::removeCoincidence(ConstraintWrapperPtr theConstraint)
469 std::list<EntityWrapperPtr> aPoints = theConstraint->entities();
470 std::list<EntityWrapperPtr>::const_iterator aPIt;
472 CoincidentPointsMap::iterator aPtPtIt = myCoincidentPoints.begin();
473 for (; aPtPtIt != myCoincidentPoints.end(); ++aPtPtIt) {
474 for (aPIt = aPoints.begin(); aPIt != aPoints.end(); ++aPIt)
475 if (aPtPtIt->first == *aPIt ||
476 aPtPtIt->second.find(*aPIt) != aPtPtIt->second.end())
478 if (aPIt != aPoints.end())
482 if (aPtPtIt == myCoincidentPoints.end())
483 return true; // already removed
485 // Create new copies of coincident points
486 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
487 std::list<EntityWrapperPtr> aNewPoints;
488 for (aPIt = aPoints.begin(); aPIt != aPoints.end(); ++aPIt)
489 aNewPoints.push_back(aBuilder->createAttribute(
490 (*aPIt)->baseAttribute(), myGroupID, mySketchID));
492 // Find all points fallen out of group of coincident points
493 std::map<EntityWrapperPtr, EntityWrapperPtr> aNotCoinc;
494 aNotCoinc[aPtPtIt->first] = EntityWrapperPtr();
495 std::set<EntityWrapperPtr>::const_iterator aTempIt = aPtPtIt->second.begin();
496 for (; aTempIt != aPtPtIt->second.end(); ++aTempIt)
497 aNotCoinc[*aTempIt] = EntityWrapperPtr();
498 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::iterator
499 aConstrIt = myConstraintMap.begin();
500 for (; aConstrIt != myConstraintMap.end(); ++aConstrIt)
501 if (aConstrIt->first->getKind() == SketchPlugin_ConstraintCoincidence::ID()) {
502 AttributeRefAttrPtr aRefAttrA = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
503 aConstrIt->first->attribute(SketchPlugin_Constraint::ENTITY_A()));
504 AttributeRefAttrPtr aRefAttrB = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
505 aConstrIt->first->attribute(SketchPlugin_Constraint::ENTITY_B()));
506 AttributePtr anAttrA, anAttrB;
507 if (aConstrIt->first->data()->isValid()) {
508 if (!aRefAttrA || !aRefAttrB || aRefAttrA->isObject() || aRefAttrB->isObject())
510 anAttrA = aRefAttrA->attr();
511 anAttrB = aRefAttrB->attr();
513 // obtain attributes from the constraint wrapper
514 ConstraintWrapperPtr aWrapper = aConstrIt->second.front();
515 anAttrA = aWrapper->entities().front()->baseAttribute();
516 anAttrB = aWrapper->entities().back()->baseAttribute();
518 std::map<AttributePtr, EntityWrapperPtr>::iterator
519 aFound = myAttributeMap.find(anAttrA);
520 if (aFound != myAttributeMap.end())
521 aNotCoinc.erase(aFound->second);
522 aFound = myAttributeMap.find(anAttrB);
523 if (aFound != myAttributeMap.end())
524 aNotCoinc.erase(aFound->second);
526 if (aNotCoinc.empty())
528 std::list<EntityWrapperPtr>::const_iterator aNewPIt;
529 for (aPIt = aPoints.begin(), aNewPIt = aNewPoints.begin();
530 aPIt != aPoints.end(); ++aPIt, ++aNewPIt) {
531 if (aNotCoinc.find(*aPIt) != aNotCoinc.end())
532 aNotCoinc[*aPIt] = *aNewPIt;
535 // Find all features and constraints uses coincident points
536 std::map<EntityWrapperPtr, EntityWrapperPtr>::iterator aNotCIt;
537 std::set<EntityWrapperPtr> anUpdFeatures;
538 std::map<FeaturePtr, EntityWrapperPtr>::iterator aFIt = myFeatureMap.begin();
539 for (; aFIt != myFeatureMap.end(); ++aFIt) {
541 continue; // avoid not completed arcs
542 for (aNotCIt = aNotCoinc.begin(); aNotCIt != aNotCoinc.end(); ++aNotCIt) {
543 if (!aNotCIt->second || !aFIt->second->isUsed(aNotCIt->first->baseAttribute()))
545 std::list<EntityWrapperPtr> aSubs = aFIt->second->subEntities();
546 std::list<EntityWrapperPtr>::iterator aSIt = aSubs.begin();
548 for (; aSIt != aSubs.end(); ++aSIt)
549 if (*aSIt == aNotCIt->first) {
550 *aSIt = aNotCIt->second;
554 aFIt->second->setSubEntities(aSubs);
555 anUpdFeatures.insert(aFIt->second);
560 std::set<EntityWrapperPtr>::iterator anUpdIt = anUpdFeatures.begin();
561 for (; anUpdIt != anUpdFeatures.end(); ++anUpdIt)
562 update(EntityWrapperPtr(*anUpdIt));
564 // remove not coincident points
565 for (aNotCIt = aNotCoinc.begin(); aNotCIt != aNotCoinc.end(); ++aNotCIt) {
566 if (aPtPtIt->second.size() <= 1) {
567 myCoincidentPoints.erase(aPtPtIt);
570 if (aPtPtIt->first == aNotCIt->first) {
571 std::set<EntityWrapperPtr> aSlaves = aPtPtIt->second;
572 EntityWrapperPtr aNewMaster = *aSlaves.begin();
573 aSlaves.erase(aSlaves.begin());
574 myCoincidentPoints.erase(aPtPtIt);
575 myCoincidentPoints[aNewMaster] = aSlaves;
576 aPtPtIt = myCoincidentPoints.find(aNewMaster);
578 aPtPtIt->second.erase(aNotCIt->first);
583 bool SketchSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
585 bool isFullyRemoved = true;
586 std::list<EntityWrapperPtr>::const_iterator anIt = theConstraint->entities().begin();
587 for (; anIt != theConstraint->entities().end(); ++anIt) {
588 FeaturePtr aBaseFeature = (*anIt)->baseFeature();
590 isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
592 isFullyRemoved = SketchSolver_Storage::removeEntity((*anIt)->baseAttribute()) && isFullyRemoved;
594 return isFullyRemoved;
597 bool SketchSolver_Storage::remove(EntityWrapperPtr theEntity)
599 bool isFullyRemoved = true;
600 std::list<EntityWrapperPtr>::const_iterator anEntIt = theEntity->subEntities().begin();
601 for (; anEntIt != theEntity->subEntities().end(); ++anEntIt) {
602 FeaturePtr aBaseFeature = (*anEntIt)->baseFeature();
604 isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseFeature) && isFullyRemoved;
606 AttributePtr aBaseAttr = (*anEntIt)->baseAttribute();
608 isFullyRemoved = SketchSolver_Storage::removeEntity(aBaseAttr) && isFullyRemoved;
614 std::list<ParameterWrapperPtr>::const_iterator aParIt = theEntity->parameters().begin();
615 for (; aParIt != theEntity->parameters().end(); ++aParIt)
616 isFullyRemoved = remove(*aParIt) && isFullyRemoved;
617 return isFullyRemoved;
621 bool SketchSolver_Storage::isInteract(const FeaturePtr& theFeature) const
625 if (myConstraintMap.empty())
626 return true; // empty storage interacts with each feature
628 ConstraintPtr aConstraint = std::dynamic_pointer_cast<SketchPlugin_Constraint>(theFeature);
630 if (myConstraintMap.find(aConstraint) != myConstraintMap.end())
632 } else if (myFeatureMap.find(theFeature) != myFeatureMap.end())
635 std::list<AttributePtr> anAttrList = theFeature->data()->attributes(std::string());
636 std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
637 for (; anIt != anAttrList.end(); ++anIt)
638 if (isInteract(*anIt))
644 bool SketchSolver_Storage::isInteract(const AttributePtr& theAttribute) const
649 AttributeRefListPtr aRefList =
650 std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(theAttribute);
652 std::list<ObjectPtr> anObjects = aRefList->list();
653 std::list<ObjectPtr>::iterator anObjIt = anObjects.begin();
654 for (; anObjIt != anObjects.end(); ++anObjIt) {
655 FeaturePtr aFeature = ModelAPI_Feature::feature(*anObjIt);
656 if (isInteract(aFeature))
662 AttributeRefAttrPtr aRefAttr =
663 std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theAttribute);
665 return myAttributeMap.find(theAttribute) != myAttributeMap.end();
666 if (!aRefAttr->isObject())
667 return myAttributeMap.find(aRefAttr->attr()) != myAttributeMap.end();
669 FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
670 return isInteract(aFeature);
673 bool SketchSolver_Storage::isConsistent() const
675 // Check the constraints are valid
676 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
677 aCIter = myConstraintMap.begin();
678 for (; aCIter != myConstraintMap.end(); ++aCIter)
679 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
681 // Check the features are valid
682 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
683 for (; aFIter != myFeatureMap.end(); aFIter++)
684 if (!aFIter->first->data() || !aFIter->first->data()->isValid())
689 bool SketchSolver_Storage::isFixed(EntityWrapperPtr theEntity) const
691 if (theEntity->group() != myGroupID)
693 // no need additional checking for entities differ than point
694 if (theEntity->type() != ENTITY_POINT)
697 CoincidentPointsMap::const_iterator anIt = myCoincidentPoints.begin();
698 for (; anIt != myCoincidentPoints.end(); ++anIt)
699 if (anIt->first == theEntity || anIt->second.find(theEntity) != anIt->second.end()) {
700 if (anIt->first->group() != myGroupID)
702 std::set<EntityWrapperPtr>::const_iterator anEntIt = anIt->second.begin();
703 for (; anEntIt != anIt->second.end(); ++anEntIt)
704 if ((*anEntIt)->group() != myGroupID)
708 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator aCIt = myConstraintMap.begin();
709 std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
710 for (; aCIt != myConstraintMap.end(); ++aCIt) {
711 if (aCIt->second.empty())
713 aCWIt = aCIt->second.begin();
714 if ((*aCWIt)->type() != CONSTRAINT_FIXED)
716 for (; aCWIt != aCIt->second.end(); ++aCIt)
717 if ((theEntity->baseAttribute() && (*aCWIt)->isUsed(theEntity->baseAttribute())) ||
718 (theEntity->baseFeature() && (*aCWIt)->isUsed(theEntity->baseFeature())))
725 void SketchSolver_Storage::removeInvalidEntities()
727 // Remove invalid constraints
728 std::list<ConstraintPtr> anInvalidConstraints;
729 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
730 aCIter = myConstraintMap.begin();
731 for (; aCIter != myConstraintMap.end(); ++aCIter)
732 if (!aCIter->first->data() || !aCIter->first->data()->isValid())
733 anInvalidConstraints.push_back(aCIter->first);
734 std::list<ConstraintPtr>::const_iterator anInvCIt = anInvalidConstraints.begin();
735 for (; anInvCIt != anInvalidConstraints.end(); ++anInvCIt)
736 removeConstraint(*anInvCIt);
737 // Remove invalid features
738 std::list<FeaturePtr> anInvalidFeatures;
739 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
740 for (; aFIter != myFeatureMap.end(); aFIter++)
741 if (!aFIter->first->data() || !aFIter->first->data()->isValid())
742 anInvalidFeatures.push_back(aFIter->first);
743 std::list<FeaturePtr>::const_iterator anInvFIt = anInvalidFeatures.begin();
744 for (; anInvFIt != anInvalidFeatures.end(); ++anInvFIt)
745 removeEntity(*anInvFIt);
748 EntityWrapperPtr SketchSolver_Storage::getNormal() const
750 EntityWrapperPtr aSketch = sketch();
754 // Find normal entity
755 const std::list<EntityWrapperPtr>& aSketchSubs = aSketch->subEntities();
756 std::list<EntityWrapperPtr>::const_iterator aSIt = aSketchSubs.begin();
757 for (; aSIt != aSketchSubs.end(); ++aSIt)
758 if ((*aSIt)->type() == ENTITY_NORMAL)
760 return EntityWrapperPtr();
763 const EntityWrapperPtr& SketchSolver_Storage::sketch() const
765 static EntityWrapperPtr aDummySketch;
767 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
768 for (; aFIt != myFeatureMap.end(); ++aFIt)
769 if (aFIt->second && aFIt->second->type() == ENTITY_SKETCH)
771 if (aFIt == myFeatureMap.end())
776 void SketchSolver_Storage::setSketch(const EntityWrapperPtr& theSketch)
780 addEntity(FeaturePtr(), theSketch);
783 void SketchSolver_Storage::processArcs()
786 std::map<FeaturePtr, EntityWrapperPtr>::iterator aFIt = myFeatureMap.begin();
787 for (; aFIt != myFeatureMap.end(); ++aFIt)
788 if (!aFIt->second && aFIt->first->getKind() == SketchPlugin_Arc::ID()) {
789 // Additional checking the attributes are initialized
790 if (aFIt->first->attribute(SketchPlugin_Arc::CENTER_ID())->isInitialized() &&
791 aFIt->first->attribute(SketchPlugin_Arc::START_ID())->isInitialized() &&
792 aFIt->first->attribute(SketchPlugin_Arc::END_ID())->isInitialized())
799 void SketchSolver_Storage::blockEvents(bool isBlocked)
801 if (isBlocked == myEventsBlocked)
804 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
805 aCIter = myConstraintMap.begin();
806 for (; aCIter != myConstraintMap.end(); aCIter++)
807 if (aCIter->first->data() && aCIter->first->data()->isValid())
808 aCIter->first->data()->blockSendAttributeUpdated(isBlocked);
810 std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIter = myFeatureMap.begin();
811 for (; aFIter != myFeatureMap.end(); aFIter++)
812 if (aFIter->first->data() && aFIter->first->data()->isValid())
813 aFIter->first->data()->blockSendAttributeUpdated(isBlocked);
815 std::map<AttributePtr, EntityWrapperPtr>::const_iterator anAtIter = myAttributeMap.begin();
816 for (; anAtIter != myAttributeMap.end(); anAtIter++)
817 if (anAtIter->first->owner() && anAtIter->first->owner()->data() &&
818 anAtIter->first->owner()->data()->isValid())
819 anAtIter->first->owner()->data()->blockSendAttributeUpdated(isBlocked);
820 myEventsBlocked = isBlocked;
823 std::set<ObjectPtr> SketchSolver_Storage::getConflictingConstraints(SolverPtr theSolver) const
825 std::set<ObjectPtr> aConflicting;
826 std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
827 aConstrIt = myConstraintMap.begin();
828 for (; aConstrIt != myConstraintMap.end(); ++aConstrIt) {
829 std::list<ConstraintWrapperPtr>::const_iterator anIt = aConstrIt->second.begin();
830 for (; anIt != aConstrIt->second.end(); ++anIt)
831 if (theSolver->isConflicting((*anIt)->id())) {
832 aConflicting.insert(aConstrIt->first);
843 // ============== Auxiliary functions ====================================
844 bool isEqual(const std::list<ConstraintWrapperPtr>& theCVec1,
845 const std::list<ConstraintWrapperPtr>& theCVec2)
847 if (theCVec1.size() != theCVec2.size())
850 std::list<bool> aChecked(theCVec2.size(), false);
851 std::list<ConstraintWrapperPtr>::const_iterator anIt1 = theCVec1.begin();
852 for (; anIt1 != theCVec1.end(); ++anIt1) {
853 std::list<ConstraintWrapperPtr>::const_iterator anIt2 = theCVec2.begin();
854 std::list<bool>::iterator aCheckIt = aChecked.begin();
855 while (aCheckIt != aChecked.end() && *aCheckIt) {
859 for (; anIt2 != theCVec2.end(); ++anIt2, ++aCheckIt)
860 if (!(*aCheckIt) && (*anIt1)->isEqual(*anIt2)) {
864 // the same constraint is not found
865 if (anIt2 == theCVec2.end())