+ std::map<Slvs_hEntity, Slvs_hEntity>::iterator aFound = aRelocationMap.find(*(aCoeffs[i]));
+ if (aFound != aRelocationMap.end())
+ *(aCoeffs[i]) = aFound->second;
+ }
+ *aCIter = myStorage->addConstraint(aConstraint);
+ }
+ adjustConstraint();
+}
+
+bool SketchSolver_Constraint::remove(ConstraintPtr theConstraint)
+{
+ cleanErrorMsg();
+ if (theConstraint && theConstraint != myBaseConstraint)
+ return false;
+ if (mySlvsConstraints.empty())
+ return true;
+ bool isFullyRemoved = myStorage->removeConstraint(mySlvsConstraints.front());
+ if (isFullyRemoved) {
+ myFeatureMap.clear();
+ myAttributeMap.clear();
+ myValueMap.clear();
+ } else
+ cleanRemovedEntities();
+ mySlvsConstraints.clear();
+ return true;
+}
+
+void SketchSolver_Constraint::cleanRemovedEntities()
+{
+ std::set<Slvs_hParam> aRemovedParams;
+ std::set<Slvs_hEntity> aRemovedEntities;
+ std::set<Slvs_hConstraint> aRemovedConstraints;
+ myStorage->getRemoved(aRemovedParams, aRemovedEntities, aRemovedConstraints);
+ std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.begin();
+ while (aFeatIt != myFeatureMap.end()) {
+ if (aRemovedEntities.find(aFeatIt->second) == aRemovedEntities.end()) {
+ aFeatIt++;
+ continue;
+ }
+ std::map<FeaturePtr, Slvs_hEntity>::iterator aTmpIter = aFeatIt++;
+ myFeatureMap.erase(aTmpIter);
+ }
+ std::map<AttributePtr, Slvs_hEntity>::iterator anAttrIt = myAttributeMap.begin();
+ while (anAttrIt != myAttributeMap.end()) {
+ if (aRemovedEntities.find(anAttrIt->second) == aRemovedEntities.end()) {
+ anAttrIt++;
+ continue;
+ }
+ std::map<AttributePtr, Slvs_hEntity>::iterator aTmpIter = anAttrIt++;
+ myAttributeMap.erase(aTmpIter);
+ }
+ std::map<AttributePtr, Slvs_hParam>::iterator aValIt = myValueMap.begin();
+ while (aValIt != myValueMap.end()) {
+ if (aRemovedParams.find(aValIt->second) == aRemovedParams.end()) {
+ aValIt++;
+ continue;
+ }
+ std::map<AttributePtr, Slvs_hParam>::iterator aTmpIter = aValIt++;
+ myValueMap.erase(aTmpIter);
+ }
+}
+
+void SketchSolver_Constraint::getAttributes(
+ double& theValue,
+ std::vector<Slvs_hEntity>& theAttributes)
+{
+ static const int anInitNbOfAttr = 4;
+ theAttributes.assign(anInitNbOfAttr, SLVS_E_UNKNOWN);
+
+ DataPtr aData = myBaseConstraint->data();
+
+ AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ aData->attribute(SketchPlugin_Constraint::VALUE()));
+ theValue = aValueAttr ? aValueAttr->value() : 0.0;
+
+ int aPtInd = 0; // index of first point in the list of attributes
+ int aEntInd = 2; // index of first antity in the list of attributes
+ std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
+ std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
+ for (; anIter != aConstrAttrs.end(); anIter++) {
+ AttributeRefAttrPtr aRefAttr =
+ std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
+ if (!aRefAttr || !aRefAttr->isInitialized()) {
+ myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
+ return;
+ }
+
+ int aType = SLVS_E_UNKNOWN; // type of created entity
+ Slvs_hEntity anEntity = myGroup->getAttributeId(aRefAttr);
+ if (anEntity == SLVS_E_UNKNOWN)
+ anEntity = changeEntity(aRefAttr, aType);
+ else {
+ Slvs_Entity anEnt = myStorage->getEntity(anEntity);
+ aType = anEnt.type;
+ }
+
+ if (aType == SLVS_E_UNKNOWN)
+ continue;
+ else if (aType == SLVS_E_POINT_IN_2D || aType == SLVS_E_POINT_IN_3D)
+ theAttributes[aPtInd++] = anEntity; // the point is created
+ else { // another entity (not a point) is created
+ if (aEntInd < anInitNbOfAttr)
+ theAttributes[aEntInd] = anEntity;
+ else
+ theAttributes.push_back(anEntity);
+ aEntInd++;
+ }
+ }
+}
+
+Slvs_hEntity SketchSolver_Constraint::changeEntity(AttributeRefAttrPtr theAttribute, int& theType)
+{
+ // Convert the object of the attribute to the feature
+ FeaturePtr aFeature;
+ if (theAttribute->isObject() && theAttribute->object()) {
+ ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
+ theAttribute->object());
+ if (!aRC) {
+ myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
+ return SLVS_E_UNKNOWN;
+ }
+ std::shared_ptr<ModelAPI_Document> aDoc = aRC->document();
+ aFeature = aDoc->feature(aRC);
+
+ return changeEntity(aFeature, theType);
+ }
+
+ return changeEntity(theAttribute->attr(), theType);
+}
+
+Slvs_hEntity SketchSolver_Constraint::changeEntity(AttributePtr theEntity, int& theType)
+{
+ Slvs_hEntity aResult = SLVS_E_UNKNOWN;
+ if (!theEntity || !isInitialized(theEntity))
+ return SLVS_E_UNKNOWN;
+
+ // If the entity is already in the group, try to find it
+ std::map<std::shared_ptr<ModelAPI_Attribute>, Slvs_hEntity>::const_iterator anEntIter =
+ myAttributeMap.find(theEntity);
+ Slvs_Entity aCurrentEntity;
+ aCurrentEntity.h = SLVS_E_UNKNOWN;
+ if (anEntIter != myAttributeMap.end())
+ aCurrentEntity = myStorage->getEntity(anEntIter->second);
+ else {
+ aResult = myGroup->getAttributeId(theEntity);
+ if (aResult != SLVS_E_UNKNOWN) {
+ Slvs_Entity anEnt = myStorage->getEntity(aResult);
+ theType = anEnt.type;
+ myAttributeMap[theEntity] = aResult;
+ return aResult;
+ }
+ }
+
+ Slvs_hGroup aGroupID = myGroup->getId();
+ // Point in 3D
+ std::shared_ptr<GeomDataAPI_Point> aPoint =
+ std::dynamic_pointer_cast<GeomDataAPI_Point>(theEntity);
+ if (aPoint) {
+ double aXYZ[3] = {aPoint->x(), aPoint->y(), aPoint->z()};
+ Slvs_hParam aParams[3];
+ for (int i = 0; i < 3; i++) {
+ Slvs_Param aPar = aCurrentEntity.h != SLVS_E_UNKNOWN ?
+ myStorage->getParameter(aCurrentEntity.param[i]) :
+ Slvs_MakeParam(SLVS_E_UNKNOWN, aGroupID, 0.0);
+ aPar.val = aXYZ[i];
+ aParams[i] = myStorage->addParameter(aPar);
+ }
+
+ if (aCurrentEntity.h == SLVS_E_UNKNOWN) // New entity
+ aCurrentEntity = Slvs_MakePoint3d(SLVS_E_UNKNOWN, aGroupID, aParams[0], aParams[1], aParams[2]);
+ else { // update entity data
+ for (int i = 0; i < 3; i++)
+ aCurrentEntity.param[i] = aParams[i];
+ }
+ aResult = myStorage->addEntity(aCurrentEntity);
+ } else {
+ // All entities except 3D points are created on workplane. So, if there is no workplane yet, then error
+ Slvs_hEntity aWorkplaneID = myGroup->getWorkplaneId();
+ if (aWorkplaneID == SLVS_E_UNKNOWN)
+ return SLVS_E_UNKNOWN;
+
+ // Point in 2D
+ std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
+ std::dynamic_pointer_cast<GeomDataAPI_Point2D>(theEntity);
+ if (aPoint2D) {
+ double aXY[2] = {aPoint2D->x(), aPoint2D->y()};
+ Slvs_hParam aParams[2];
+ for (int i = 0; i < 2; i++) {
+ Slvs_Param aPar = aCurrentEntity.h != SLVS_E_UNKNOWN ?
+ myStorage->getParameter(aCurrentEntity.param[i]) :
+ Slvs_MakeParam(SLVS_E_UNKNOWN, aGroupID, 0.0);
+ aPar.val = aXY[i];
+ aParams[i] = myStorage->addParameter(aPar);