1 #include <SketchSolver_ConstraintRigid.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Group.h>
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_Circle.h>
7 #include <SketchPlugin_ConstraintRigid.h>
9 #include <GeomAPI_Pnt2d.h>
10 #include <GeomAPI_XY.h>
11 #include <GeomDataAPI_Point2D.h>
12 #include <ModelAPI_AttributeDouble.h>
14 SketchSolver_ConstraintRigid::SketchSolver_ConstraintRigid(FeaturePtr theFeature)
15 : SketchSolver_Constraint(),
16 myBaseFeature(theFeature)
21 void SketchSolver_ConstraintRigid::process()
24 if ((!myBaseConstraint && !myBaseFeature) || !myStorage || myGroup == 0) {
25 /// TODO: Put error message here
28 if (!mySlvsConstraints.empty()) // some data is changed, update constraint
29 update(myBaseConstraint);
32 std::vector<Slvs_hEntity> anEntities;
33 getAttributes(aValue, anEntities);
34 if (!myErrorMsg.empty())
37 Slvs_Constraint aConstraint;
38 std::vector<Slvs_hConstraint>::iterator aConstrIter = mySlvsConstraints.begin();
39 bool isEmpty = aConstrIter == mySlvsConstraints.end();
41 // Check the fixed entity is an arc
43 if (!myFeatureMap.empty() && myFeatureMap.begin()->first->getKind() == SketchPlugin_Arc::ID()) {
44 Slvs_Entity anArc = myStorage->getEntity(myFeatureMap.begin()->second);
50 std::vector<Slvs_hEntity>::const_iterator anEntIter = anEntities.begin();
51 for (; anEntIter != anEntities.end(); anEntIter++) {
52 if (*anEntIter == SLVS_E_UNKNOWN)
54 Slvs_hConstraint aConstrID = myStorage->isPointFixed(*anEntIter);
55 bool isForceUpdate = (aConstrID != SLVS_E_UNKNOWN && !myBaseConstraint);
56 if (isEmpty && !isForceUpdate) { // create new constraint
57 if (aConstrID != SLVS_E_UNKNOWN)
58 continue; // the coincident point is already fixed
59 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
60 aValue, *anEntIter, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
61 aConstraint.h = myStorage->addConstraint(aConstraint);
62 mySlvsConstraints.push_back(aConstraint.h);
63 if (!myBaseConstraint)
64 myStorage->addTemporaryConstraint(aConstraint.h);
65 } else { // update already existent constraint
66 if (aConstrID == SLVS_E_UNKNOWN || myBaseConstraint)
67 aConstrID = *aConstrIter;
68 aConstraint = myStorage->getConstraint(aConstrID);
69 aConstraint.ptA = *anEntIter;
70 myStorage->addConstraint(aConstraint);
71 if (!myBaseConstraint)
72 myStorage->addTemporaryConstraint(aConstraint.h);
75 isEmpty = aConstrIter == mySlvsConstraints.end();
80 if (!myFeatureMap.empty() && myFeatureMap.begin()->first->getKind() == SketchPlugin_Circle::ID()) {
81 // Fix radius of a circle
82 AttributeDoublePtr aRadiusAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
83 myFeatureMap.begin()->first->attribute(SketchPlugin_Circle::RADIUS_ID()));
84 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER, myGroup->getWorkplaneId(),
85 aRadiusAttr->value() * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
86 aConstraint.h = myStorage->addConstraint(aConstraint);
87 mySlvsConstraints.push_back(aConstraint.h);
88 if (!myBaseConstraint)
89 myStorage->addTemporaryConstraint(aConstraint.h);
94 void SketchSolver_ConstraintRigid::getAttributes(
96 std::vector<Slvs_hEntity>& theAttributes)
99 int aType = SLVS_E_UNKNOWN; // type of created entity
100 Slvs_hEntity anEntityID = SLVS_E_UNKNOWN;
101 if (myBaseConstraint) {
102 // Get the attribute of constraint
103 AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
104 myBaseConstraint->attribute(SketchPlugin_ConstraintRigid::ENTITY_A()));
105 if (!aRefAttr || !aRefAttr->isInitialized()) {
106 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
109 anEntityID = myGroup->getAttributeId(aRefAttr);
110 if (anEntityID == SLVS_E_UNKNOWN)
111 anEntityID = changeEntity(aRefAttr, aType);
113 anEntityID = myGroup->getFeatureId(myBaseFeature);
114 if (anEntityID == SLVS_E_UNKNOWN)
115 anEntityID = changeEntity(myBaseFeature, aType);
117 myFeatureMap[myBaseFeature] = anEntityID;
120 // Check the entity is complex
121 Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
122 if (anEntity.point[0] != SLVS_E_UNKNOWN) {
123 for (int i = 0; i < 4 && anEntity.point[i]; i++)
124 theAttributes.push_back(anEntity.point[i]);
125 } else // simple entity
126 theAttributes.push_back(anEntityID);
129 void SketchSolver_ConstraintRigid::adjustConstraint()
131 if (myFeatureMap.empty() || (
132 myFeatureMap.begin()->first->getKind() != SketchPlugin_Arc::ID() &&
133 myFeatureMap.begin()->first->getKind() != SketchPlugin_Circle::ID()))
135 FeaturePtr aFeature = myFeatureMap.begin()->first;
137 // Search radius constraints and update them
138 Slvs_Constraint aConstraint;
139 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
140 for (; aCIter != mySlvsConstraints.end(); aCIter++) {
141 aConstraint = myStorage->getConstraint(*aCIter);
142 if (aConstraint.type != SLVS_C_DIAMETER)
144 double aRadius = 0.0;
145 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
146 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
147 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
148 std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
149 aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
150 aRadius = aCenter->distance(aStart);
152 aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
153 aFeature->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
156 aConstraint.valA = aRadius * 2.0;
157 *aCIter = myStorage->updateConstraint(aConstraint);
162 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
165 if (theConstraint && theConstraint != myBaseConstraint)
167 bool isFullyRemoved = true;
168 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
169 for (; aCIter != mySlvsConstraints.end(); aCIter++)
170 isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
172 if (isFullyRemoved) {
173 myFeatureMap.clear();
174 myAttributeMap.clear();
177 cleanRemovedEntities();
182 void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
184 Slvs_Constraint aConstraint;
185 Slvs_hConstraint aConstrID = myStorage->isPointFixed(theArc.point[0]);
186 int aPointsToFix = 2; // number of fixed points for the arc
187 if (aConstrID != SLVS_E_UNKNOWN)
191 FeaturePtr aFeature = myFeatureMap.begin()->first;
192 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
193 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
194 std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
195 aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
196 double aRadius = aCenter->distance(aStart);
198 // Update end point of the arc to be on a curve
199 std::shared_ptr<GeomAPI_Pnt2d> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200 aFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
201 double aDistance = anEnd->distance(aCenter);
202 std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
203 if (aDistance < tolerance)
204 aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
206 aDir = aDir->multiplied(aRadius / aDistance);
207 double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
208 Slvs_Entity aEndPoint = myStorage->getEntity(theArc.point[2]);
209 for (int i = 0; i < 2; i++) {
210 Slvs_Param aParam = myStorage->getParameter(aEndPoint.param[i]);
212 myStorage->updateParameter(aParam);
215 for (int i = 1; aPointsToFix > 0; i++, aPointsToFix--) {
216 aConstrID = myStorage->isPointFixed(theArc.point[i]);
217 if (aConstrID != SLVS_E_UNKNOWN)
218 continue; // the coincident point is already fixed
219 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
220 0.0, theArc.point[i], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
221 aConstraint.h = myStorage->addConstraint(aConstraint);
222 mySlvsConstraints.push_back(aConstraint.h);
223 if (!myBaseConstraint)
224 myStorage->addTemporaryConstraint(aConstraint.h);
227 // Fix radius of the arc
228 bool isExists = false;
229 std::list<Slvs_Constraint> aDiamConstraints = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
230 std::list<Slvs_Constraint>::iterator anIt = aDiamConstraints.begin();
231 for (; anIt != aDiamConstraints.end() && !isExists; anIt)
232 if (anIt->entityA == myFeatureMap.begin()->second)
235 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER, myGroup->getWorkplaneId(),
236 aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
237 aConstraint.h = myStorage->addConstraint(aConstraint);
238 mySlvsConstraints.push_back(aConstraint.h);
239 if (!myBaseConstraint)
240 myStorage->addTemporaryConstraint(aConstraint.h);