Salome HOME
Remove extra files
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMovement.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include <SketchSolver_ConstraintMovement.h>
4 #include <SketchSolver_Error.h>
5 #include <SketchSolver_Manager.h>
6
7 #include <SketchPlugin_Arc.h>
8 #include <SketchPlugin_Circle.h>
9 #include <SketchPlugin_Line.h>
10 #include <SketchPlugin_Point.h>
11
12 #include <GeomDataAPI_Point2D.h>
13
14
15 void SketchSolver_ConstraintMovement::process()
16 {
17   cleanErrorMsg();
18   if (!myBaseFeature || !myStorage || myGroupID == GID_UNKNOWN) {
19     // Not enough parameters are initialized
20     return;
21   }
22
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();
29     return;
30   }
31
32   std::vector<EntityWrapperPtr>::iterator anEntIt = myMovedEntities.begin();
33   for (; anEntIt != myMovedEntities.end(); ++anEntIt)
34     fixFeature(*anEntIt);
35 }
36
37
38 static std::list<EntityWrapperPtr> movedEntities(
39     EntityWrapperPtr theOld, StoragePtr theOldStorage,
40     EntityWrapperPtr theNew, StoragePtr theNewStorage)
41 {
42   bool isFullyMoved = true;
43   std::list<EntityWrapperPtr> aMoved;
44   if (theOld->isEqual(theNew))
45     return aMoved;
46
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))
57       isFullyMoved = false;
58     aMoved.insert(aMoved.end(), aMovedSubs.begin(), aMovedSubs.end());
59   }
60   if (isFullyMoved) {
61     aMoved.clear();
62     aMoved.push_back(theOld);
63   }
64   return aMoved;
65 }
66
67
68 void SketchSolver_ConstraintMovement::getAttributes(
69     ParameterWrapperPtr& theValue,
70     std::vector<EntityWrapperPtr>& theAttributes)
71 {
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))
78     return;
79   EntityWrapperPtr aNewEntity = anOtherStorage->entity(myBaseFeature);
80   EntityWrapperPtr anOldEntity = myStorage->entity(myBaseFeature);
81
82   std::list<EntityWrapperPtr> aMoved;
83   if (aNewEntity && anOldEntity)
84     aMoved = movedEntities(anOldEntity, myStorage, aNewEntity, anOtherStorage);
85   else {
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)
94         continue;
95       std::list<EntityWrapperPtr> aMovedAttr = movedEntities(
96           anOldEntity, myStorage, aNewEntity, anOtherStorage);
97       aMoved.insert(aMoved.end(), aMovedAttr.begin(), aMovedAttr.end());
98     }
99   }
100   theAttributes.clear();
101   theAttributes.insert(theAttributes.begin(), aMoved.begin(), aMoved.end());
102 }
103
104 bool SketchSolver_ConstraintMovement::remove()
105 {
106   cleanErrorMsg();
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);
114   }
115
116   // Remove base feature
117   if (myBaseFeature)
118     myStorage->removeEntity(myBaseFeature);
119   return true;
120 }