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 if (anEntityID == SLVS_E_UNKNOWN) {
121 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
125 // Check the entity is complex
126 Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
127 if (anEntity.point[0] != SLVS_E_UNKNOWN) {
128 for (int i = 0; i < 4 && anEntity.point[i]; i++)
129 theAttributes.push_back(anEntity.point[i]);
130 } else // simple entity
131 theAttributes.push_back(anEntityID);
134 void SketchSolver_ConstraintRigid::adjustConstraint()
136 if (myFeatureMap.empty() || (
137 myFeatureMap.begin()->first->getKind() != SketchPlugin_Arc::ID() &&
138 myFeatureMap.begin()->first->getKind() != SketchPlugin_Circle::ID()))
140 FeaturePtr aFeature = myFeatureMap.begin()->first;
142 // Search radius constraints and update them
143 Slvs_Constraint aConstraint;
144 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
145 for (; aCIter != mySlvsConstraints.end(); aCIter++) {
146 aConstraint = myStorage->getConstraint(*aCIter);
147 if (aConstraint.type != SLVS_C_DIAMETER)
149 double aRadius = 0.0;
150 if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
151 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
152 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
153 std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
154 aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
155 aRadius = aCenter->distance(aStart);
157 aRadius = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
158 aFeature->attribute(SketchPlugin_Circle::RADIUS_ID()))->value();
161 aConstraint.valA = aRadius * 2.0;
162 *aCIter = myStorage->updateConstraint(aConstraint);
167 bool SketchSolver_ConstraintRigid::remove(ConstraintPtr theConstraint)
170 if (theConstraint && theConstraint != myBaseConstraint)
172 bool isFullyRemoved = true;
173 std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
174 for (; aCIter != mySlvsConstraints.end(); aCIter++)
175 isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
177 if (isFullyRemoved) {
178 myFeatureMap.clear();
179 myAttributeMap.clear();
182 cleanRemovedEntities();
187 void SketchSolver_ConstraintRigid::fixArc(const Slvs_Entity& theArc)
189 Slvs_Constraint aConstraint;
190 Slvs_hConstraint aConstrID = myStorage->isPointFixed(theArc.point[0]);
191 int aPointsToFix = 2; // number of fixed points for the arc
192 if (aConstrID != SLVS_E_UNKNOWN)
196 FeaturePtr aFeature = myFeatureMap.begin()->first;
197 std::shared_ptr<GeomAPI_Pnt2d> aCenter = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
198 aFeature->attribute(SketchPlugin_Arc::CENTER_ID()))->pnt();
199 std::shared_ptr<GeomAPI_Pnt2d> aStart = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
200 aFeature->attribute(SketchPlugin_Arc::START_ID()))->pnt();
201 double aRadius = aCenter->distance(aStart);
203 // Update end point of the arc to be on a curve
204 std::shared_ptr<GeomAPI_Pnt2d> anEnd = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
205 aFeature->attribute(SketchPlugin_Arc::END_ID()))->pnt();
206 double aDistance = anEnd->distance(aCenter);
207 std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
208 if (aDistance < tolerance)
209 aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
211 aDir = aDir->multiplied(aRadius / aDistance);
212 double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
213 Slvs_Entity aEndPoint = myStorage->getEntity(theArc.point[2]);
214 for (int i = 0; i < 2; i++) {
215 Slvs_Param aParam = myStorage->getParameter(aEndPoint.param[i]);
217 myStorage->updateParameter(aParam);
220 for (int i = 1; aPointsToFix > 0; i++, aPointsToFix--) {
221 aConstrID = myStorage->isPointFixed(theArc.point[i]);
222 if (aConstrID != SLVS_E_UNKNOWN)
223 continue; // the coincident point is already fixed
224 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), getType(), myGroup->getWorkplaneId(),
225 0.0, theArc.point[i], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
226 aConstraint.h = myStorage->addConstraint(aConstraint);
227 mySlvsConstraints.push_back(aConstraint.h);
228 if (!myBaseConstraint)
229 myStorage->addTemporaryConstraint(aConstraint.h);
232 // Fix radius of the arc
233 bool isExists = false;
234 std::list<Slvs_Constraint> aDiamConstraints = myStorage->getConstraintsByType(SLVS_C_DIAMETER);
235 std::list<Slvs_Constraint>::iterator anIt = aDiamConstraints.begin();
236 for (; anIt != aDiamConstraints.end() && !isExists; anIt)
237 if (anIt->entityA == myFeatureMap.begin()->second)
240 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_DIAMETER, myGroup->getWorkplaneId(),
241 aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, myFeatureMap.begin()->second, SLVS_E_UNKNOWN);
242 aConstraint.h = myStorage->addConstraint(aConstraint);
243 mySlvsConstraints.push_back(aConstraint.h);
244 if (!myBaseConstraint)
245 myStorage->addTemporaryConstraint(aConstraint.h);