1 #include <SketchSolver_ConstraintRigid.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Group.h>
5 #include <SketchPlugin_ConstraintRigid.h>
7 SketchSolver_ConstraintRigid::SketchSolver_ConstraintRigid(FeaturePtr theFeature)
8 : SketchSolver_Constraint(),
9 myBaseFeature(theFeature)
14 void SketchSolver_ConstraintRigid::process()
17 if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroup == 0) {
18 /// TODO: Put error message here
21 if (!mySlvsConstraints.empty()) // some data is changed, update constraint
22 update(myBaseConstraint);
25 std::vector<Slvs_hEntity> anEntities;
26 getAttributes(aValue, anEntities);
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);
45 isEmpty = aConstrIter == mySlvsConstraints.end();
51 void SketchSolver_ConstraintRigid::getAttributes(
53 std::vector<Slvs_hEntity>& theAttributes)
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()));
63 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
66 anEntityID = myGroup->getAttributeId(aRefAttr);
67 if (anEntityID == SLVS_E_UNKNOWN)
68 anEntityID = changeEntity(aRefAttr, aType);
70 anEntityID = myGroup->getFeatureId(myBaseFeature);
71 if (anEntityID == SLVS_E_UNKNOWN)
72 anEntityID = changeEntity(myBaseFeature, aType);
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);
84 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
87 if (theConstraint && theConstraint != myBaseConstraint)
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;
96 myAttributeMap.clear();
99 cleanRemovedEntities();