]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintRigid.cpp
Salome HOME
02ba819a85d89f6ed58d31db10daf17ad11f9a2f
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintRigid.cpp
1 #include <SketchSolver_ConstraintRigid.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Group.h>
4
5 #include <SketchPlugin_ConstraintRigid.h>
6
7 SketchSolver_ConstraintRigid::SketchSolver_ConstraintRigid(FeaturePtr theFeature)
8   : SketchSolver_Constraint(),
9     myBaseFeature(theFeature)
10 {
11   process();
12 }
13
14 void SketchSolver_ConstraintRigid::process()
15 {
16   cleanErrorMsg();
17   if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroup == 0) {
18     /// TODO: Put error message here
19     return;
20   }
21   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
22     update(myBaseConstraint);
23
24   double aValue;
25   std::vector<Slvs_hEntity> anEntities;
26   getAttributes(aValue, anEntities);
27
28   Slvs_Constraint aConstraint;
29   std::vector<Slvs_hConstraint>::iterator aConstrIter = mySlvsConstraints.begin();
30   bool isEmpty = aConstrIter == mySlvsConstraints.end();
31   std::vector<Slvs_hEntity>::const_iterator anEntIter = anEntities.begin();
32   for (; anEntIter != anEntities.end(); anEntIter++) {
33     if (isEmpty) { // create new constraint
34       aConstraint = Slvs_MakeConstraint(SLVS_C_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
35           aValue, *anEntIter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
36       aConstraint.h = myStorage->addConstraint(aConstraint);
37       mySlvsConstraints.push_back(aConstraint.h);
38       if (!myBaseConstraint)
39         myStorage->addTemporaryConstraint(aConstraint.h);
40     } else { // update already existent constraint
41       aConstraint = myStorage->getConstraint(*aConstrIter);
42       aConstraint.ptA = *anEntIter;
43       myStorage->addConstraint(aConstraint);
44       aConstrIter++;
45       isEmpty = aConstrIter == mySlvsConstraints.end();
46     }
47   }
48 }
49
50
51 void SketchSolver_ConstraintRigid::getAttributes(
52     double& theValue,
53     std::vector<Slvs_hEntity>& theAttributes)
54 {
55   theValue = 0.0;
56   int aType = SLVS_E_UNKNOWN; // type of created entity
57   Slvs_hEntity anEntityID = SLVS_E_UNKNOWN;
58   if (myBaseConstraint) {
59     // Get the attribute of constraint
60     AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
61         myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
62     if (!aRefAttr) {
63       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
64       return;
65     }
66     anEntityID = myGroup->getAttributeId(aRefAttr);
67     if (anEntityID == SLVS_E_UNKNOWN)
68       anEntityID = changeEntity(aRefAttr, aType);
69   } else {
70     anEntityID = myGroup->getFeatureId(myBaseFeature);
71     if (anEntityID == SLVS_E_UNKNOWN)
72       anEntityID = changeEntity(myBaseFeature, aType);
73   }
74
75   // Check the entity is complex
76   Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
77   if (anEntity.point[0] != SLVS_E_UNKNOWN) {
78     for (int i = 0; i < 4 && anEntity.point[i]; i++)
79       theAttributes.push_back(anEntity.point[i]);
80   } else // simple entity
81     theAttributes.push_back(anEntityID);
82 }
83
84 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
85 {
86   cleanErrorMsg();
87   if (theConstraint && theConstraint != myBaseConstraint)
88     return false;
89   bool isFullyRemoved = true;
90   std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
91   for (; aCIter != mySlvsConstraints.end(); aCIter++)
92     isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
93
94   if (isFullyRemoved) {
95     myFeatureMap.clear();
96     myAttributeMap.clear();
97     myValueMap.clear();
98   } else
99     cleanRemovedEntities();
100   return true;
101 }
102