Salome HOME
Fix compilation error on Linux. Part II.
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
1 #include <SketchSolver_Constraint.h>
2 #include <SketchSolver_Group.h>
3 #include <SketchSolver_Error.h>
4 #include <SketchSolver_Manager.h>
5
6 #include <SketchPlugin_Arc.h>
7 #include <SketchPlugin_Circle.h>
8 #include <SketchPlugin_Line.h>
9 #include <SketchPlugin_Point.h>
10
11 #include <SketchPlugin_ConstraintAngle.h>
12 #include <SketchPlugin_ConstraintCoincidence.h>
13 #include <SketchPlugin_ConstraintDistance.h>
14 #include <SketchPlugin_ConstraintEqual.h>
15 #include <SketchPlugin_ConstraintHorizontal.h>
16 #include <SketchPlugin_ConstraintLength.h>
17 #include <SketchPlugin_ConstraintMirror.h>
18 #include <SketchPlugin_ConstraintParallel.h>
19 #include <SketchPlugin_ConstraintPerpendicular.h>
20 #include <SketchPlugin_ConstraintRadius.h>
21 #include <SketchPlugin_ConstraintRigid.h>
22 #include <SketchPlugin_ConstraintTangent.h>
23 #include <SketchPlugin_ConstraintVertical.h>
24
25 #include <GeomAPI_Dir2d.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomDataAPI_Point2D.h>
28 #include <ModelAPI_AttributeDouble.h>
29 #include <ModelAPI_ResultConstruction.h>
30
31 #include <math.h>
32
33 SketchSolver_Constraint::SketchSolver_Constraint(
34     ConstraintPtr  theConstraint)
35   : myBaseConstraint(theConstraint),
36     myGroupID(GID_UNKNOWN),
37     myType(CONSTRAINT_UNKNOWN)
38 {
39 }
40
41 void SketchSolver_Constraint::process(StoragePtr theStorage,
42                                       const GroupID& theGroupID,
43                                       const EntityID& theSketchID)
44 {
45   myStorage = theStorage;
46   myGroupID = theGroupID;
47   mySketchID = theSketchID;
48   // Process constraint according to its type
49   process();
50 }
51
52
53 SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
54 {
55   const std::string& aType = theConstraint->getKind();
56   if (aType == SketchPlugin_ConstraintCoincidence::ID())
57     return CONSTRAINT_COINCIDENCE;
58   else if (aType == SketchPlugin_ConstraintRigid::ID())
59     return CONSTRAINT_FIXED;
60   else if (aType == SketchPlugin_ConstraintHorizontal::ID())
61     return CONSTRAINT_HORIZONTAL;
62   else if (aType == SketchPlugin_ConstraintVertical::ID())
63     return CONSTRAINT_VERTICAL;
64   else if (aType == SketchPlugin_ConstraintAngle::ID())
65     return CONSTRAINT_ANGLE;
66   else if (aType == SketchPlugin_ConstraintDistance::ID())
67     return CONSTRAINT_DISTANCE;
68   else if (aType == SketchPlugin_ConstraintEqual::ID())
69     return CONSTRAINT_EQUAL;
70   else if (aType == SketchPlugin_ConstraintLength::ID())
71     return CONSTRAINT_PT_PT_DISTANCE;
72   else if (aType == SketchPlugin_ConstraintMirror::ID())
73     return CONSTRAINT_SYMMETRIC;
74   else if (aType == SketchPlugin_ConstraintParallel::ID())
75     return CONSTRAINT_PARALLEL;
76   else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
77     return CONSTRAINT_PERPENDICULAR;
78   else if (aType == SketchPlugin_ConstraintRadius::ID())
79     return CONSTRAINT_RADIUS;
80   else if (aType == SketchPlugin_ConstraintTangent::ID())
81     return CONSTRAINT_TANGENT;
82   return CONSTRAINT_UNKNOWN;
83 }
84
85 void SketchSolver_Constraint::process()
86 {
87   cleanErrorMsg();
88   if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
89     // Not enough parameters are assigned
90     return;
91   }
92 ////  if (!myConstraints.empty()) // some data is changed, update constraint
93 ////    update();
94
95   SketchSolver_ConstraintType aConstrType = getType();
96   double aValue;
97   std::vector<EntityWrapperPtr> anAttributes;
98   getAttributes(aValue, anAttributes);
99   if (!myErrorMsg.empty())
100     return;
101   if (anAttributes.empty()) {
102     myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
103     return;
104   }
105   if (aConstrType == CONSTRAINT_UNKNOWN)
106     aConstrType = getType();
107
108   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
109   std::list<ConstraintWrapperPtr> aNewConstraints = aBuilder->createConstraint(
110       myBaseConstraint, myGroupID, mySketchID, aConstrType,
111       aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
112   myStorage->addConstraint(myBaseConstraint, aNewConstraints);
113
114   adjustConstraint();
115 }
116
117 ////bool SketchSolver_Constraint::checkAttributesChanged(ConstraintPtr theConstraint)
118 ////{
119 ////  std::set<Slvs_hEntity> aCurAttrs; // list of currently used attributes
120 ////  std::vector<Slvs_hConstraint>::const_iterator aConstrIter = mySlvsConstraints.begin();
121 ////  for (; aConstrIter != mySlvsConstraints.end(); aConstrIter++) {
122 ////    Slvs_Constraint aConstr = myStorage->getConstraint(*aConstrIter);
123 ////    if (aConstr.ptA != SLVS_E_UNKNOWN) aCurAttrs.insert(aConstr.ptA);
124 ////    if (aConstr.ptB != SLVS_E_UNKNOWN) aCurAttrs.insert(aConstr.ptB);
125 ////    if (aConstr.entityA != SLVS_E_UNKNOWN) aCurAttrs.insert(aConstr.entityA);
126 ////    if (aConstr.entityB != SLVS_E_UNKNOWN) aCurAttrs.insert(aConstr.entityB);
127 ////    if (aConstr.entityC != SLVS_E_UNKNOWN) aCurAttrs.insert(aConstr.entityC);
128 ////    if (aConstr.entityD != SLVS_E_UNKNOWN) aCurAttrs.insert(aConstr.entityD);
129 ////  }
130 ////  // Check the attrbutes of constraint are changed
131 ////  ConstraintPtr aConstraint = theConstraint ? theConstraint : myBaseConstraint;
132 ////  std::list<AttributePtr> anAttrList = aConstraint->data()->attributes(std::string());
133 ////  std::list<AttributePtr>::iterator anAttrIter = anAttrList.begin();
134 ////  for (; anAttrIter != anAttrList.end(); anAttrIter++) {
135 ////    AttributeRefAttrPtr aRefAttr =
136 ////        std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anAttrIter);
137 ////    if (aRefAttr) {
138 ////      if (aRefAttr->isObject()) {
139 ////        FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
140 ////        std::map<FeaturePtr, Slvs_hEntity>::iterator aFIt = myFeatureMap.find(aFeature);
141 ////        if (aFeature) {
142 ////          if (aFIt == myFeatureMap.end())
143 ////            return true;
144 ////          // Additional check the points of entity
145 ////          if (aCurAttrs.find(aFIt->second) == aCurAttrs.end()) {
146 ////            Slvs_Entity anEntity = myStorage->getEntity(aFIt->second);
147 ////            bool isFound = false;
148 ////            for (int i = 0; i < 4 && !isFound; i++)
149 ////              if (anEntity.point[i] != SLVS_E_UNKNOWN && 
150 ////                  aCurAttrs.find(anEntity.point[i]) != aCurAttrs.end())
151 ////                isFound = true;
152 ////            if (!isFound)
153 ////              return true;
154 ////          }
155 ////        }
156 ////      } else if (aRefAttr->attr()) {
157 ////        std::map<AttributePtr, Slvs_hEntity>::iterator anAIt = myAttributeMap.find(aRefAttr->attr());
158 ////        if (anAIt == myAttributeMap.end() || aCurAttrs.find(anAIt->second) == aCurAttrs.end())
159 ////          return true;
160 ////      }
161 ////    }
162 ////    AttributeRefListPtr aRefList =
163 ////        std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anAttrIter);
164 ////    if (aRefList) {
165 ////      std::list<ObjectPtr> anItems = aRefList->list();
166 ////      std::list<ObjectPtr>::iterator anIt = anItems.begin();
167 ////      for (; anIt != anItems.end(); anIt++) {
168 ////        FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
169 ////        if (aFeature && myFeatureMap.find(aFeature) == myFeatureMap.end())
170 ////          return true;
171 ////      }
172 ////    }
173 ////  }
174 ////  return false;
175 ////}
176
177 void SketchSolver_Constraint::update()
178 {
179   cleanErrorMsg();
180
181   std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
182   AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
183       myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
184   if (aValueAttr) {
185     std::list<ConstraintWrapperPtr>::iterator aWIt = aWrapper.begin();
186     for (; aWIt != aWrapper.end(); ++aWIt)
187       (*aWIt)->setValue(aValueAttr->value());
188   }
189   myStorage->addConstraint(myBaseConstraint, aWrapper);
190
191   adjustConstraint();
192 }
193
194 bool SketchSolver_Constraint::remove()
195 {
196   cleanErrorMsg();
197   return myStorage->removeConstraint(myBaseConstraint);
198 }
199
200 void SketchSolver_Constraint::getAttributes(
201     double& theValue,
202     std::vector<EntityWrapperPtr>& theAttributes)
203 {
204   static const int anInitNbOfAttr = 4;
205   theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
206
207   DataPtr aData = myBaseConstraint->data();
208   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
209
210   myType = TYPE(myBaseConstraint);
211
212   AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
213       aData->attribute(SketchPlugin_Constraint::VALUE()));
214   theValue = aValueAttr ? aValueAttr->value() : 0.0;
215
216   int aPtInd = 0; // index of first point in the list of attributes
217   int aEntInd = 2; // index of first entity in the list of attributes
218   std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
219   std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
220   for (; anIter != aConstrAttrs.end(); anIter++) {
221     AttributeRefAttrPtr aRefAttr =
222         std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
223     if (!aRefAttr || !aRefAttr->isInitialized()) {
224       myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
225       return;
226     }
227
228     myStorage->update(*anIter, myGroupID);
229     EntityWrapperPtr anEntity = myStorage->entity(*anIter);
230
231     SketchSolver_EntityType aType = anEntity->type();
232     if (aType == ENTITY_UNKNOWN)
233       continue;
234     else if (aType == ENTITY_POINT)
235       theAttributes[aPtInd++] = anEntity; // the point is created
236     else { // another entity (not a point) is created
237       if (aEntInd < anInitNbOfAttr)
238         theAttributes[aEntInd] = anEntity;
239       else
240         theAttributes.push_back(anEntity);
241       aEntInd++;
242     }
243   }
244 }
245
246 bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
247 {
248   const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
249   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
250   for (; aCIt != aCList.end(); ++aCIt)
251     if ((*aCIt)->isUsed(theFeature))
252       return true;
253
254   std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
255   std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
256   for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
257     if (isUsed(*anAttrIt))
258       return true;
259
260   return false;
261 }
262
263 bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
264 {
265   AttributePtr anAttribute = theAttribute;
266   AttributeRefAttrPtr aRefAttr =
267       std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
268   if (aRefAttr) {
269     if (aRefAttr->isObject())
270       return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
271     else
272       anAttribute = aRefAttr->attr();
273   }
274
275   const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
276   std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
277   for (; aCIt != aCList.end(); ++aCIt)
278     if ((*aCIt)->isUsed(theAttribute))
279       return true;
280   return false;
281 }
282
283 ////Slvs_hEntity SketchSolver_Constraint::changeEntity(AttributeRefAttrPtr theAttribute, int& theType)
284 ////{
285 ////  // Convert the object of the attribute to the feature
286 ////  FeaturePtr aFeature;
287 ////  if (theAttribute->isObject() && theAttribute->object()) {
288 ////    ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
289 ////        theAttribute->object());
290 ////    if (!aRC) {
291 ////      myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
292 ////      return SLVS_E_UNKNOWN;
293 ////    }
294 ////    std::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
295 ////    aFeature = aDoc->feature(aRC);
296 ////
297 ////    return changeEntity(aFeature, theType);
298 ////  }
299 ////
300 ////  return changeEntity(theAttribute->attr(), theType);
301 ////}
302 ////
303 ////Slvs_hEntity SketchSolver_Constraint::changeEntity(AttributePtr theEntity, int& theType)
304 ////{
305 ////  Slvs_hEntity aResult = SLVS_E_UNKNOWN;
306 ////  if (!theEntity || !isInitialized(theEntity)) {
307 ////    myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
308 ////    return SLVS_E_UNKNOWN;
309 ////  }
310 ////
311 ////  // If the entity is already in the group, try to find it
312 ////  std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator anEntIter =
313 ////      myAttributeMap.find(theEntity);
314 ////  Slvs_Entity aCurrentEntity;
315 ////  aCurrentEntity.h = SLVS_E_UNKNOWN;
316 ////  if (anEntIter != myAttributeMap.end())
317 ////    aCurrentEntity = myStorage->getEntity(anEntIter->second);
318 ////  else {
319 ////    aResult = myGroup->getAttributeId(theEntity);
320 ////    if (aResult != SLVS_E_UNKNOWN) {
321 ////      Slvs_Entity anEnt = myStorage->getEntity(aResult);
322 ////      theType = anEnt.type;
323 ////      myAttributeMap[theEntity] = aResult;
324 ////      return aResult;
325 ////    }
326 ////  }
327 ////
328 ////  Slvs_hGroup aGroupID = myGroup->getId();
329 ////  // do not update entity from another group
330 ////  if (aCurrentEntity.h != SLVS_E_UNKNOWN && aGroupID != aCurrentEntity.group)
331 ////    return aCurrentEntity.h;
332 ////
333 ////  // Point in 3D
334 ////  std::shared_ptr<GeomDataAPI_Point> aPoint =
335 ////      std::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
336 ////  if (aPoint) {
337 ////    double aXYZ[3] = {aPoint->x(), aPoint->y(), aPoint->z()};
338 ////    Slvs_hParam aParams[3];
339 ////    for (int i = 0; i < 3; i++) {
340 ////      Slvs_Param aPar = aCurrentEntity.h != SLVS_E_UNKNOWN ?
341 ////          myStorage->getParameter(aCurrentEntity.param[i]) :
342 ////          Slvs_MakeParam(SLVS_E_UNKNOWN, aGroupID, 0.0);
343 ////      aPar.val = aXYZ[i];
344 ////      aParams[i] = myStorage->addParameter(aPar);
345 ////    }
346 ////
347 ////    if (aCurrentEntity.h == SLVS_E_UNKNOWN) // New entity
348 ////      aCurrentEntity = Slvs_MakePoint3d(SLVS_E_UNKNOWN, aGroupID, aParams[0], aParams[1], aParams[2]);
349 ////    else { // update entity data
350 ////      for (int i = 0; i < 3; i++)
351 ////        aCurrentEntity.param[i] = aParams[i];
352 ////    }
353 ////    aResult = myStorage->addEntity(aCurrentEntity);
354 ////  } else {
355 ////    // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
356 ////    Slvs_hEntity aWorkplaneID = myGroup->getWorkplaneId();
357 ////    if (aWorkplaneID == SLVS_E_UNKNOWN)
358 ////      return SLVS_E_UNKNOWN;
359 ////
360 ////    // Point in 2D
361 ////    std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
362 ////        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
363 ////    if (aPoint2D) {
364 ////      double aXY[2] = {aPoint2D->x(), aPoint2D->y()};
365 ////      Slvs_hParam aParams[2];
366 ////      for (int i = 0; i < 2; i++) {
367 ////        Slvs_Param aPar = aCurrentEntity.h != SLVS_E_UNKNOWN ?
368 ////            myStorage->getParameter(aCurrentEntity.param[i]) :
369 ////            Slvs_MakeParam(SLVS_E_UNKNOWN, aGroupID, 0.0);
370 ////        aPar.val = aXY[i];
371 ////        aParams[i] = myStorage->addParameter(aPar);
372 ////      }
373 ////
374 ////      if (aCurrentEntity.h == SLVS_E_UNKNOWN) // New entity
375 ////        aCurrentEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID, aParams[0], aParams[1]);
376 ////      else { // update entity data
377 ////        for (int i = 0; i < 2; i++)
378 ////          aCurrentEntity.param[i] = aParams[i];
379 ////      }
380 ////      aResult = myStorage->addEntity(aCurrentEntity);
381 ////    } else {
382 ////      // Scalar value (used for the distance entities)
383 ////      AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(theEntity);
384 ////      if (aScalar) {
385 ////        Slvs_Param aParam = aCurrentEntity.h != SLVS_E_UNKNOWN ?
386 ////            myStorage->getParameter(aCurrentEntity.param[0]) :
387 ////            Slvs_MakeParam(SLVS_E_UNKNOWN, aGroupID, 0.0);
388 ////        aParam.val = aScalar->value();
389 ////        Slvs_hParam aValue = myStorage->addParameter(aParam);
390 ////
391 ////        if (aCurrentEntity.h == SLVS_E_UNKNOWN) // New entity
392 ////          aCurrentEntity = Slvs_MakeDistance(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID, aValue);
393 ////        else
394 ////          aCurrentEntity.param[0] = aValue;
395 ////        aResult = myStorage->addEntity(aCurrentEntity);
396 ////      }
397 ////    }
398 ////  }
399 ////
400 ////  myAttributeMap[theEntity] = aResult;
401 ////  theType = aCurrentEntity.type;
402 ////  return aResult;
403 ////}
404 ////
405 ////Slvs_hEntity SketchSolver_Constraint::changeEntity(FeaturePtr theEntity, int& theType)
406 ////{
407 ////  Slvs_hEntity aResult = SLVS_E_UNKNOWN;
408 ////  if (!theEntity || !theEntity->data() || !theEntity->data()->isValid())
409 ////    return SLVS_E_UNKNOWN;
410 ////  // If the entity is already in the group, try to find it
411 ////  std::map<FeaturePtr, Slvs_hEntity>::const_iterator anEntIter = myFeatureMap.find(theEntity);
412 ////  Slvs_Entity aCurrentEntity;
413 ////  aCurrentEntity.h = SLVS_E_UNKNOWN;
414 ////  if (anEntIter != myFeatureMap.end())
415 ////    aCurrentEntity = myStorage->getEntity(anEntIter->second);
416 ////  else {
417 ////    aResult = myGroup->getFeatureId(theEntity);
418 ////    if (aResult != SLVS_E_UNKNOWN) {
419 ////      Slvs_Entity anEnt = myStorage->getEntity(aResult);
420 ////      theType = anEnt.type;
421 ////      myFeatureMap[theEntity] = aResult;
422 ////      return aResult;
423 ////    }
424 ////  }
425 ////
426 ////  Slvs_hGroup aGroupID = myGroup->getId();
427 ////  // do not update entity from another group
428 ////  if (aCurrentEntity.h != SLVS_E_UNKNOWN && aGroupID != aCurrentEntity.group)
429 ////    return aCurrentEntity.h;
430 ////
431 ////  Slvs_hEntity aWorkplaneID = myGroup->getWorkplaneId();
432 ////  DataPtr aData = theEntity->data();
433 ////
434 ////  // SketchPlugin features
435 ////  const std::string& aFeatureKind = theEntity->getKind();
436 ////  AttributePtr anAttribute;
437 ////  int anAttrType;
438 ////  // Line
439 ////  if (aFeatureKind == SketchPlugin_Line::ID()) {
440 ////    anAttribute = aData->attribute(SketchPlugin_Line::START_ID());
441 ////    if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
442 ////    Slvs_hEntity aStart = changeEntity(anAttribute, anAttrType);
443 ////
444 ////    anAttribute = aData->attribute(SketchPlugin_Line::END_ID());
445 ////    if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
446 ////    Slvs_hEntity aEnd = changeEntity(anAttribute, anAttrType);
447 ////
448 ////    if (aCurrentEntity.h == SLVS_E_UNKNOWN) // New entity
449 ////      aCurrentEntity = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID, aStart, aEnd);
450 ////    else {
451 ////      aCurrentEntity.point[0] = aStart;
452 ////      aCurrentEntity.point[1] = aEnd;
453 ////    }
454 ////    aResult = myStorage->addEntity(aCurrentEntity);
455 ////  }
456 ////  // Circle
457 ////  else if (aFeatureKind == SketchPlugin_Circle::ID()) {
458 ////    anAttribute = aData->attribute(SketchPlugin_Circle::CENTER_ID());
459 ////    if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
460 ////    Slvs_hEntity aCenter = changeEntity(anAttribute, anAttrType);
461 ////
462 ////    anAttribute = aData->attribute(SketchPlugin_Circle::RADIUS_ID());
463 ////    if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
464 ////    Slvs_hEntity aRadius = changeEntity(anAttribute, anAttrType);
465 ////
466 ////    if (aCurrentEntity.h == SLVS_E_UNKNOWN) { // New entity
467 ////      Slvs_Entity aWorkplane = myStorage->getEntity(aWorkplaneID);
468 ////      aCurrentEntity = Slvs_MakeCircle(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID,
469 ////                                        aCenter, aWorkplane.normal, aRadius);
470 ////    } else {
471 ////      aCurrentEntity.point[0] = aCenter;
472 ////      aCurrentEntity.distance = aRadius;
473 ////    }
474 ////    aResult = myStorage->addEntity(aCurrentEntity);
475 ////  }
476 ////  // Arc
477 ////  else if (aFeatureKind == SketchPlugin_Arc::ID()) {
478 ////    anAttribute = aData->attribute(SketchPlugin_Arc::CENTER_ID());
479 ////    if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
480 ////    Slvs_hEntity aCenter = changeEntity(anAttribute, anAttrType);
481 ////
482 ////    anAttribute = aData->attribute(SketchPlugin_Arc::START_ID());
483 ////    if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
484 ////    Slvs_hEntity aStart = changeEntity(anAttribute, anAttrType);
485 ////
486 ////    anAttribute = aData->attribute(SketchPlugin_Arc::END_ID());
487 ////    if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
488 ////    Slvs_hEntity aEnd = changeEntity(anAttribute, anAttrType);
489 ////
490 ////    if (aCurrentEntity.h == SLVS_E_UNKNOWN) { // New entity
491 ////      Slvs_Entity aWorkplane = myStorage->getEntity(aWorkplaneID);
492 ////      aCurrentEntity = Slvs_MakeArcOfCircle(SLVS_E_UNKNOWN, aGroupID, aWorkplaneID,
493 ////                                            aWorkplane.normal, aCenter, aStart, aEnd);
494 ////    } else {
495 ////      aCurrentEntity.point[0] = aCenter;
496 ////      aCurrentEntity.point[1] = aStart;
497 ////      aCurrentEntity.point[2] = aEnd;
498 ////    }
499 ////    aResult = myStorage->addEntity(aCurrentEntity);
500 ////  }
501 ////  // Point (it has low probability to be an attribute of constraint, so it is checked at the end)
502 ////  else if (aFeatureKind == SketchPlugin_Point::ID()) {
503 ////    anAttribute = aData->attribute(SketchPlugin_Point::COORD_ID());
504 ////    if (!isInitialized(anAttribute)) return SLVS_E_UNKNOWN;
505 ////    // Both the sketch point and its attribute (coordinates) link to the same SolveSpace point identifier
506 ////    aResult = changeEntity(anAttribute, anAttrType);
507 ////    aCurrentEntity.type = SLVS_E_POINT_IN_3D;
508 ////  }
509 ////
510 ////  if (aResult != SLVS_E_UNKNOWN) {
511 ////    myFeatureMap[theEntity] = aResult;
512 ////    theType = aCurrentEntity.type;
513 ////  }
514 ////  return aResult;
515 ////}
516 ////
517 ////bool SketchSolver_Constraint::hasConstraint(ConstraintPtr theConstraint) const
518 ////{
519 ////  std::list<ConstraintPtr>::const_iterator anIt = myConstraints.begin();
520 ////  for (; anIt != myConstraints.end(); ++anIt)
521 ////    if (*anIt == theConstraint)
522 ////      return true;
523 ////  return false;
524 ////}
525 ////
526 ////void SketchSolver_Constraint::refresh()
527 ////{
528 ////  cleanErrorMsg();
529 ////  std::map<AttributePtr, Slvs_hEntity>::iterator anAttrIter = myAttributeMap.begin();
530 ////  while (anAttrIter != myAttributeMap.end()) {
531 ////    std::shared_ptr<GeomDataAPI_Point> aPoint =
532 ////        std::dynamic_pointer_cast<GeomDataAPI_Point>(anAttrIter->first);
533 ////    Slvs_Entity anEntity = myStorage->getEntity(anAttrIter->second);
534 ////    if (anEntity.h == SLVS_E_UNKNOWN) {
535 ////      std::map<AttributePtr, Slvs_hEntity>::iterator aTmpIter = anAttrIter++;
536 ////      myAttributeMap.erase(aTmpIter);
537 ////      continue;
538 ////    }
539 ////    if (aPoint) {
540 ////      double aXYZ[3];
541 ////      for (int i = 0; i < 3; i++) {
542 ////        Slvs_Param aPar = myStorage->getParameter(anEntity.param[i]);
543 ////        aXYZ[i] = aPar.val;
544 ////      }
545 ////      if (fabs(aPoint->x() - aXYZ[0]) > tolerance ||
546 ////          fabs(aPoint->y() - aXYZ[1]) > tolerance ||
547 ////          fabs(aPoint->z() - aXYZ[2]) > tolerance)
548 ////        aPoint->setValue(aXYZ[0], aXYZ[1], aXYZ[2]);
549 ////    } else {
550 ////      // Point in 2D
551 ////      std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
552 ////          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttrIter->first);
553 ////      if (aPoint2D) {
554 ////        double aXY[2];
555 ////        for (int i = 0; i < 2; i++) {
556 ////          Slvs_Param aPar = myStorage->getParameter(anEntity.param[i]);
557 ////          aXY[i] = aPar.val;
558 ////        }
559 ////        if (fabs(aPoint2D->x() - aXY[0]) > tolerance ||
560 ////            fabs(aPoint2D->y() - aXY[1]) > tolerance)
561 ////          aPoint2D->setValue(aXY[0], aXY[1]);
562 ////      } else {
563 ////        // Scalar value (used for the distance entities)
564 ////        AttributeDoublePtr aScalar =
565 ////            std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttrIter->first);
566 ////        if (aScalar) {
567 ////          Slvs_Param aPar = myStorage->getParameter(anEntity.param[0]);
568 ////          if (fabs(aScalar->value() - aPar.val) > tolerance)
569 ////            aScalar->setValue(aPar.val);
570 ////        }
571 ////      }
572 ////    }
573 ////    anAttrIter++;
574 ////  }
575 ////
576 ////  std::map<AttributePtr, Slvs_hParam>::iterator aValIter = myValueMap.begin();
577 ////  for (; aValIter != myValueMap.end(); aValIter++) {
578 ////    AttributeDoublePtr aScalar =
579 ////        std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttrIter->first);
580 ////    if (aScalar) {
581 ////      Slvs_Param aPar = myStorage->getParameter(anAttrIter->second);
582 ////      aScalar->setValue(aPar.val);
583 ////    }
584 ////  }
585 ////}
586
587 ////bool SketchSolver_Constraint::isInitialized(AttributePtr theAttribute)
588 ////{
589 ////  if (theAttribute->isInitialized())
590 ////    return true;
591 ////  myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
592 ////  return false;
593 ////}
594 ////
595 ////
596 ////void SketchSolver_Constraint::calculateMiddlePoint(
597 ////    const Slvs_Entity& theEntity, double theCoeff, double& theX, double& theY) const
598 ////{
599 ////  if (theEntity.type == SLVS_E_LINE_SEGMENT) {
600 ////    double aStartEndXY[2][2];
601 ////    Slvs_Entity aPoint;
602 ////    for (int i = 0; i < 2; i++) {
603 ////      aPoint = myStorage->getEntity(theEntity.point[i]);
604 ////      for (int j = 0; j < 2; j++)
605 ////        aStartEndXY[i][j] = myStorage->getParameter(aPoint.param[j]).val;
606 ////    }
607 ////    theX = (1.0 - theCoeff) * aStartEndXY[0][0] + theCoeff * aStartEndXY[1][0];
608 ////    theY = (1.0 - theCoeff) * aStartEndXY[0][1] + theCoeff * aStartEndXY[1][1];
609 ////  } else if (theEntity.type == SLVS_E_ARC_OF_CIRCLE) {
610 ////    double anArcPoint[3][2];
611 ////    Slvs_Entity aPoint;
612 ////    for (int i = 0; i < 3; i++) {
613 ////      aPoint = myStorage->getEntity(theEntity.point[i]);
614 ////      for (int j = 0; j < 2; j++)
615 ////        anArcPoint[i][j] = myStorage->getParameter(aPoint.param[j]).val;
616 ////    }
617 ////    // project last point of arc on the arc
618 ////    double x = anArcPoint[1][0] - anArcPoint[0][0];
619 ////    double y = anArcPoint[1][1] - anArcPoint[0][1];
620 ////    double aRad = sqrt(x*x + y*y);
621 ////    x = anArcPoint[2][0] - anArcPoint[0][0];
622 ////    y = anArcPoint[2][1] - anArcPoint[0][1];
623 ////    double aNorm = sqrt(x*x + y*y);
624 ////    if (aNorm >= tolerance) {
625 ////      anArcPoint[2][0] = x * aRad / aNorm;
626 ////      anArcPoint[2][1] = y * aRad / aNorm;
627 ////    }
628 ////    anArcPoint[1][0] -= anArcPoint[0][0];
629 ////    anArcPoint[1][1] -= anArcPoint[0][1];
630 ////    if (theCoeff < tolerance) {
631 ////      theX = anArcPoint[0][0] + anArcPoint[1][0];
632 ////      theY = anArcPoint[0][1] + anArcPoint[1][1];
633 ////      return;
634 ////    } else if (1 - theCoeff < tolerance) {
635 ////      theX = anArcPoint[0][0] + anArcPoint[2][0];
636 ////      theY = anArcPoint[0][1] + anArcPoint[2][1];
637 ////      return;
638 ////    }
639 ////
640 ////    std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
641 ////    std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
642 ////    double anAngle = aStartDir->angle(aEndDir);
643 ////    if (anAngle < 0)
644 ////      anAngle += 2.0 * PI;
645 ////    anAngle *= theCoeff;
646 ////    double aCos = cos(anAngle);
647 ////    double aSin = sin(anAngle);
648 ////    theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
649 ////    theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
650 ////  }
651 ////}
652
653 void SketchSolver_Constraint::makeTemporary() const
654 {
655   myStorage->setTemporary(myBaseConstraint);
656 }
657