1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 #include <SketchSolver_ConstraintMovement.h>
4 #include <SketchSolver_Error.h>
5 #include <SketchSolver_Manager.h>
7 #include <SketchPlugin_Arc.h>
8 #include <SketchPlugin_Circle.h>
9 #include <SketchPlugin_Line.h>
10 #include <SketchPlugin_Point.h>
12 #include <GeomDataAPI_Point2D.h>
15 void SketchSolver_ConstraintMovement::process()
18 if (!myBaseFeature || !myStorage || myGroupID == GID_UNKNOWN) {
19 // Not enough parameters are initialized
23 ParameterWrapperPtr aValue;
24 getAttributes(aValue, myMovedEntities);
25 if (!myErrorMsg.empty() || myMovedEntities.empty()) {
26 // Nothing to move, clear the feature to avoid changing its group
27 // after removing the Movement constraint.
28 myBaseFeature = FeaturePtr();
32 std::vector<EntityWrapperPtr>::iterator anEntIt = myMovedEntities.begin();
33 for (; anEntIt != myMovedEntities.end(); ++anEntIt)
38 static std::list<EntityWrapperPtr> movedEntities(
39 EntityWrapperPtr theOld, StoragePtr theOldStorage,
40 EntityWrapperPtr theNew, StoragePtr theNewStorage)
42 bool isFullyMoved = true;
43 std::list<EntityWrapperPtr> aMoved;
44 if (theOld->isEqual(theNew))
47 std::list<EntityWrapperPtr> anOldSubs = theOld->subEntities();
48 std::list<EntityWrapperPtr> aNewSubs = theNew->subEntities();
49 std::list<EntityWrapperPtr>::const_iterator anOldIt = anOldSubs.begin();
50 std::list<EntityWrapperPtr>::const_iterator aNewIt = aNewSubs.begin();
51 for (; anOldIt != anOldSubs.end() && aNewIt != aNewSubs.end(); ++anOldIt, ++aNewIt) {
52 std::list<EntityWrapperPtr> aMovedSubs = movedEntities(
53 *anOldIt, theOldStorage, *aNewIt, theNewStorage);
54 // check only the points to be moved (because arcs in PlaneGCS have scalar subs too)
55 if ((*anOldIt)->type() == ENTITY_POINT &&
56 (aMovedSubs.size() != 1 || aMovedSubs.front() != *anOldIt))
58 aMoved.insert(aMoved.end(), aMovedSubs.begin(), aMovedSubs.end());
62 aMoved.push_back(theOld);
68 void SketchSolver_ConstraintMovement::getAttributes(
69 ParameterWrapperPtr& theValue,
70 std::vector<EntityWrapperPtr>& theAttributes)
72 // There will be build entities, according to the fixed feature, in the separate storage
73 // to check whether any point is moved
74 BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
75 StoragePtr anOtherStorage = aBuilder->createStorage(myGroupID);
76 anOtherStorage->setSketch(myStorage->sketch());
77 if (!anOtherStorage->update(myBaseFeature, myGroupID))
79 EntityWrapperPtr aNewEntity = anOtherStorage->entity(myBaseFeature);
80 EntityWrapperPtr anOldEntity = myStorage->entity(myBaseFeature);
82 std::list<EntityWrapperPtr> aMoved;
83 if (aNewEntity && anOldEntity)
84 aMoved = movedEntities(anOldEntity, myStorage, aNewEntity, anOtherStorage);
86 // get attributes moved
87 std::list<AttributePtr> anAttrList =
88 myBaseFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
89 std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
90 for (; anIt != anAttrList.end(); ++anIt) {
91 aNewEntity = anOtherStorage->entity(*anIt);
92 anOldEntity = myStorage->entity(*anIt);
93 if (!aNewEntity || !anOldEntity)
95 std::list<EntityWrapperPtr> aMovedAttr = movedEntities(
96 anOldEntity, myStorage, aNewEntity, anOtherStorage);
97 aMoved.insert(aMoved.end(), aMovedAttr.begin(), aMovedAttr.end());
100 theAttributes.clear();
101 theAttributes.insert(theAttributes.begin(), aMoved.begin(), aMoved.end());
104 bool SketchSolver_ConstraintMovement::remove()
107 // Move fixed entities back to the current group
108 std::vector<EntityWrapperPtr>::iterator aMoveIt = myMovedEntities.begin();
109 for (; aMoveIt != myMovedEntities.end(); ++aMoveIt) {
110 if ((*aMoveIt)->baseAttribute())
111 myStorage->update((*aMoveIt)->baseAttribute(), myGroupID);
112 else if ((*aMoveIt)->baseFeature())
113 myStorage->update((*aMoveIt)->baseFeature(), myGroupID);
116 // Remove base feature
118 myStorage->removeEntity(myBaseFeature);