1 #include <SketchSolver_ConstraintMultiTranslation.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
5 #include <SketchPlugin_Arc.h>
6 #include <SketchPlugin_MultiTranslation.h>
8 #include <ModelAPI_AttributeDouble.h>
9 #include <ModelAPI_AttributeInteger.h>
10 #include <ModelAPI_AttributeRefAttr.h>
11 #include <ModelAPI_AttributeRefList.h>
12 #include <ModelAPI_ResultConstruction.h>
13 #include <ModelAPI_Data.h>
15 #include <GeomAPI_Dir2d.h>
16 #include <GeomAPI_XY.h>
21 void SketchSolver_ConstraintMultiTranslation::getAttributes(
22 Slvs_hEntity& theStartPoint, Slvs_hEntity& theEndPoint,
23 std::vector< std::vector<Slvs_hEntity> >& thePoints,
24 std::vector< std::vector<Slvs_hEntity> >& theEntities)
26 DataPtr aData = myBaseConstraint->data();
27 AttributePtr aStartPointAttr = aData->attribute(SketchPlugin_MultiTranslation::START_POINT_ID());
28 AttributePtr aEndPointAttr = aData->attribute(SketchPlugin_MultiTranslation::END_POINT_ID());
29 if (!aStartPointAttr || !aStartPointAttr->isInitialized() ||
30 !aEndPointAttr || !aEndPointAttr->isInitialized()) {
31 myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
35 int aType = SLVS_E_UNKNOWN; // type of created entity
36 Slvs_hEntity anEntityID = myGroup->getAttributeId(aStartPointAttr);
37 if (anEntityID == SLVS_E_UNKNOWN)
38 anEntityID = changeEntity(aStartPointAttr, aType);
39 theStartPoint = anEntityID;
40 anEntityID = myGroup->getAttributeId(aEndPointAttr);
41 if (anEntityID == SLVS_E_UNKNOWN)
42 anEntityID = changeEntity(aEndPointAttr, aType);
43 theEndPoint = anEntityID;
45 // Lists of objects and number of copies
46 AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
47 aData->attribute(SketchPlugin_Constraint::ENTITY_A()));
48 myNumberOfObjects = anInitialRefList->size();
49 myNumberOfCopies = (size_t) aData->integer(SketchPlugin_MultiTranslation::NUMBER_OF_COPIES_ID())->value();
50 AttributeRefListPtr aRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
51 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B()));
53 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
57 // Obtain all points of initial features and store them into separate lists
58 // containing their translated copies.
59 // Also all circles and arc collected too, because they will be constrained by equal radii.
61 ResultConstructionPtr aRC;
62 static const size_t MAX_POINTS = 3;
63 std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
64 std::vector<Slvs_hEntity> anEntities; // list of translated entities
65 std::list<ObjectPtr> anObjectList = aRefList->list();
66 std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
67 while (anObjectIter != anObjectList.end()) {
68 for (size_t i = 0; i < MAX_POINTS; i++)
72 for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
73 aFeature = ModelAPI_Feature::feature(*anObjectIter);
76 anEntityID = changeEntity(aFeature, aType);
77 anEntities.push_back(anEntityID);
78 Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
80 case SLVS_E_POINT_IN_2D:
81 case SLVS_E_POINT_IN_3D:
82 aPoints[0].push_back(anEntityID);
84 case SLVS_E_LINE_SEGMENT:
85 aPoints[0].push_back(anEntity.point[0]); // start point of line
86 aPoints[1].push_back(anEntity.point[1]); // end point of line
89 aPoints[0].push_back(anEntity.point[0]); // center of circle
91 case SLVS_E_ARC_OF_CIRCLE:
92 aPoints[0].push_back(anEntity.point[0]); // center of arc
93 aPoints[1].push_back(anEntity.point[1]); // start point of arc
94 aPoints[2].push_back(anEntity.point[2]); // end point of arc
97 myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
102 for (size_t i = 0; i < MAX_POINTS; ++i)
103 if (!aPoints[i].empty())
104 thePoints.push_back(aPoints[i]);
105 if (!anEntities.empty())
106 theEntities.push_back(anEntities);
110 void SketchSolver_ConstraintMultiTranslation::process()
113 if (!myBaseConstraint || !myStorage || myGroup == 0) {
114 /// TODO: Put error message here
117 if (!mySlvsConstraints.empty()) // some data is changed, update constraint
118 update(myBaseConstraint);
120 Slvs_hEntity aStartPoint, aEndPoint;
121 std::vector<std::vector<Slvs_hEntity> > anEntitiesAndCopies;
122 getAttributes(aStartPoint, aEndPoint, myPointsAndCopies, anEntitiesAndCopies);
123 if (!myErrorMsg.empty())
126 // Create translation line
127 if (myTranslationLine == SLVS_E_UNKNOWN) {
128 Slvs_Entity aTranslationLine = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, myGroup->getId(),
129 myGroup->getWorkplaneId(), aStartPoint, aEndPoint);
130 aTranslationLine.h = myStorage->addEntity(aTranslationLine);
131 myTranslationLine = aTranslationLine.h;
133 Slvs_Entity aTranslationLine = myStorage->getEntity(myTranslationLine);
134 if (aTranslationLine.point[0] != aStartPoint || aTranslationLine.point[1] != aEndPoint) {
135 aTranslationLine.point[0] = aStartPoint;
136 aTranslationLine.point[1] = aEndPoint;
137 myStorage->updateEntity(aTranslationLine);
141 // Set all objects unchanged (only initial object may be changed by user)
142 myCircsAndCopies.clear();
143 std::vector<std::vector<Slvs_hEntity> >::const_iterator anEntIt = anEntitiesAndCopies.begin();
144 std::vector<Slvs_hEntity>::const_iterator aCpIt;
145 for (; anEntIt != anEntitiesAndCopies.end(); ++anEntIt) {
146 std::vector<Slvs_hEntity> aCircs;
147 for (aCpIt = anEntIt->begin(); aCpIt != anEntIt->end(); ++aCpIt) {
148 const Slvs_Entity& anEntity = myStorage->getEntity(*aCpIt);
149 std::vector<Slvs_hConstraint> aNewConstr;
150 if (anEntity.type == SLVS_E_CIRCLE) {
151 aCircs.push_back(anEntity.distance);
152 // for circles we fix only center
153 aNewConstr = myStorage->fixEntity(anEntity.point[0]);
155 aNewConstr = myStorage->fixEntity(*aCpIt);
156 mySlvsConstraints.insert(mySlvsConstraints.end(), aNewConstr.begin(), aNewConstr.end());
160 myCircsAndCopies.push_back(aCircs);
166 void SketchSolver_ConstraintMultiTranslation::update(ConstraintPtr theConstraint)
169 if (!theConstraint || theConstraint == myBaseConstraint) {
170 AttributeRefListPtr anInitialRefList = std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
171 myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A()));
172 AttributeIntegerPtr aNbCopies = myBaseConstraint->integer(SketchPlugin_MultiTranslation::NUMBER_OF_COPIES_ID());
173 if (anInitialRefList->size() != myNumberOfObjects ||
174 (size_t)aNbCopies->value() != myNumberOfCopies) {
175 remove(myBaseConstraint);
180 SketchSolver_Constraint::update();
183 bool SketchSolver_ConstraintMultiTranslation::remove(ConstraintPtr theConstraint)
186 if (theConstraint && theConstraint != myBaseConstraint)
188 bool isFullyRemoved = true;
189 std::vector<Slvs_hEntity>::iterator aCIter = mySlvsConstraints.begin();
190 for (; aCIter != mySlvsConstraints.end(); aCIter++)
191 isFullyRemoved = myStorage->removeConstraint(*aCIter) && isFullyRemoved;
192 mySlvsConstraints.clear();
194 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
195 for (; aFeatIt != myFeatureMap.end(); aFeatIt++)
196 myStorage->removeEntity(aFeatIt->second);
198 std::map<FeaturePtr, Slvs_hEntity> aFeatureMapCopy = myFeatureMap;
200 if (isFullyRemoved) {
201 myFeatureMap.clear();
202 myAttributeMap.clear();
205 cleanRemovedEntities();
207 // Restore initial features
208 std::map<FeaturePtr, Slvs_hEntity>::iterator aFIter = aFeatureMapCopy.begin();
209 for (; aFIter != aFeatureMapCopy.end(); ++aFIter)
211 if (myFeatureMap.find(aFIter->first) != myFeatureMap.end())
212 continue; // the feature was not removed
213 Slvs_hEntity anEntity = myGroup->getFeatureId(aFIter->first);
214 if (anEntity != SLVS_E_UNKNOWN)
215 myFeatureMap[aFIter->first] = anEntity;
221 void SketchSolver_ConstraintMultiTranslation::addFeature(FeaturePtr theFeature)
223 SketchSolver_Constraint::addFeature(theFeature);
225 std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.find(theFeature);
226 if (aFeatIt == myFeatureMap.end())
229 // store list of points of the feature
230 const Slvs_Entity& theEntity = myStorage->getEntity(aFeatIt->second);
231 for (int i = 0; i < 4; i++)
232 if (theEntity.point[i] != SLVS_E_UNKNOWN)
233 myPointsJustUpdated.insert(theEntity.point[i]);
236 void SketchSolver_ConstraintMultiTranslation::adjustConstraint()
238 Slvs_Entity aTranslationLine = myStorage->getEntity(myTranslationLine);
239 Slvs_hConstraint aFixed; // temporary variable
240 // Set the translation line unchanged during constraint recalculation
241 for (int i = 0; i < 2; i++) {
242 if (myStorage->isPointFixed(aTranslationLine.point[i], aFixed, true))
244 Slvs_Constraint aConstraint = Slvs_MakeConstraint(
245 SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
246 aTranslationLine.point[i], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
247 aConstraint.h = myStorage->addConstraint(aConstraint);
248 myStorage->addTemporaryConstraint(aConstraint.h);
251 // Check if the distance between point is 0, no need to resolve constraints (just wait another values)
253 for (int i = 0; i < 2; i++) {
254 Slvs_Entity aPnt = myStorage->getEntity(aTranslationLine.point[i]);
255 aXY[2*i] = myStorage->getParameter(aPnt.param[0]).val;
256 aXY[2*i+1] = myStorage->getParameter(aPnt.param[1]).val;
258 double aDelta[2] = {aXY[2] - aXY[0], aXY[3] - aXY[1]};
259 if (fabs(aDelta[0]) + fabs(aDelta[1]) < tolerance) {
260 myStorage->setNeedToResolve(false);
264 std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
265 std::list<Slvs_Constraint>::const_iterator aCoIt;
269 // Update positions of all points to satisfy distances
270 std::vector< std::vector<Slvs_hEntity> >::const_iterator aPointsIter = myPointsAndCopies.begin();
271 std::vector<Slvs_hEntity>::const_iterator aCopyIter;
272 for (; aPointsIter != myPointsAndCopies.end(); ++aPointsIter) {
273 aCopyIter = aPointsIter->begin();
274 const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
275 for (int i = 0; i < 2; i++)
276 aCoord[i] = myStorage->getParameter(anInitial.param[i]).val;
278 // if the point is coincident with another one which is temporary fixed (moved by user),
279 // we will update its position correspondingly
280 Slvs_hConstraint aFixed;
281 for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt) {
282 if ((aCoIt->ptA == anInitial.h && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
283 (aCoIt->ptB == anInitial.h && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
284 Slvs_hEntity anOtherId = aCoIt->ptA == anInitial.h ? aCoIt->ptB : aCoIt->ptA;
285 if (!myStorage->isTemporary(aFixed) &&
286 myPointsJustUpdated.find(anOtherId) == myPointsJustUpdated.end())
287 continue; // nothing to change
289 const Slvs_Entity& anOtherPnt = myStorage->getEntity(anOtherId);
290 for (int i = 0; i < 2; i++) {
291 Slvs_Param anInitParam = myStorage->getParameter(anInitial.param[i]);
292 const Slvs_Param& anOtherParam = myStorage->getParameter(anOtherPnt.param[i]);
293 anInitParam.val = anOtherParam.val;
294 myStorage->updateParameter(anInitParam);
295 aCoord[i] = anOtherParam.val;
300 // update copied points
301 aCopyIter = aPointsIter->begin();
302 for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
304 aCoord[0] += aDelta[0];
305 aCoord[1] += aDelta[1];
307 const Slvs_Entity& aTarget = myStorage->getEntity(*aCopyIter);
308 for (int i = 0; i < 2; i++) {
309 Slvs_Param aParam = myStorage->getParameter(aTarget.param[i]);
310 aParam.val = aCoord[i];
311 myStorage->updateParameter(aParam);
316 for (aPointsIter = myCircsAndCopies.begin(); aPointsIter != myCircsAndCopies.end(); ++aPointsIter) {
317 aCopyIter = aPointsIter->begin();
318 const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
319 const Slvs_Param& anInitRad = myStorage->getParameter(anInitial.param[0]);
320 for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
321 const Slvs_Entity& aCopy = myStorage->getEntity(*aCopyIter);
322 Slvs_Param aCopyRad = myStorage->getParameter(aCopy.param[0]);
323 aCopyRad.val = anInitRad.val;
324 myStorage->updateParameter(aCopyRad);
328 myPointsJustUpdated.clear();