- // Verify the correctness of constraint arguments
- if (aNbPoints == 2 && aNbEntities == 0)
- myType = SLVS_C_PT_PT_DISTANCE;
- else if (aNbPoints != 1 || aNbEntities != 1)
- myType = SLVS_C_UNKNOWN;
- return getType();
- }
-
- // Constraint for the given length of a line
- if (aConstraintKind.compare(SketchPlugin_ConstraintLength::ID()) == 0) {
- int aNbLines = 0;
- for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
- std::shared_ptr<ModelAPI_Attribute> anAttr =
- aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
- if (typeOfAttribute(anAttr) == LINE)
- myAttributesList[aNbLines++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
- }
- if (aNbLines == 1)
- myType = SLVS_C_PT_PT_DISTANCE;
- return getType();
- }
-
- // Constraint for two parallel/perpendicular lines
- bool isParallel = (aConstraintKind.compare(SketchPlugin_ConstraintParallel::ID()) == 0);
- bool isPerpendicular = (aConstraintKind.compare(SketchPlugin_ConstraintPerpendicular::ID()) == 0);
- if (isParallel || isPerpendicular) {
- int aNbEntities = 2; // lines in SolveSpace constraints should start from SketchPlugin_Constraint::ENTITY_C() attribute
- for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
- std::shared_ptr<ModelAPI_Attribute> anAttr =
- aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
- if (typeOfAttribute(anAttr) == LINE)
- myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
- }
- if (aNbEntities == 4)
- myType = isParallel ? SLVS_C_PARALLEL : SLVS_C_PERPENDICULAR;
- return getType();
- }
-
- // Constraint for radius of a circle or an arc of circle
- if (aConstraintKind.compare(SketchPlugin_ConstraintRadius::ID()) == 0) {
- int aNbEntities = 2; // lines in SolveSpace constraints should started from SketchPlugin_Constraint::ENTITY_C() attribute
- for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
- std::shared_ptr<ModelAPI_Attribute> anAttr =
- aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
- AttrType aType = typeOfAttribute(anAttr);
- if (aType == CIRCLE || aType == ARC)
- myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
- }
- if (aNbEntities == 3)
- myType = SLVS_C_DIAMETER;
- return getType();
- }
-
- // Constraint for fixed entity
- if (aConstraintKind.compare(SketchPlugin_ConstraintRigid::ID()) == 0) {
- // Verify that only one entity is filled
- int aNbAttrs = 0;
- for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
- std::shared_ptr<ModelAPI_Attribute> anAttr =
- aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
- AttrType aType = typeOfAttribute(anAttr);
- if (aType != UNKNOWN)
- myAttributesList[aNbAttrs++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
- }
- if (aNbAttrs == 1)
- myType = SLVS_C_WHERE_DRAGGED;
- return getType();
- }
-
- /// \todo Implement other kind of constraints
-
- return getType();
-}
-
-// ================= Auxiliary functions ==============================
-AttrType typeOfAttribute(std::shared_ptr<ModelAPI_Attribute> theAttribute)
-{
- std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrRef = std::dynamic_pointer_cast<
- ModelAPI_AttributeRefAttr>(theAttribute);
- if (!anAttrRef)
- return UNKNOWN;
-
- if (anAttrRef->isObject()) {
+ }
+ return false;
+}
+
+void SketchSolver_Constraint::update(ConstraintPtr theConstraint)
+{
+ cleanErrorMsg();
+ bool needToRebuild = (theConstraint && theConstraint != myBaseConstraint);
+ if (!needToRebuild)
+ needToRebuild = checkAttributesChanged(theConstraint);
+ if (needToRebuild) {
+ if (theConstraint && theConstraint->getKind() != myBaseConstraint->getKind())
+ return;
+ remove(myBaseConstraint);
+ if (theConstraint)
+ myBaseConstraint = theConstraint;
+ process();
+ return;
+ }
+
+ // Update all attributes
+ int aType;
+ std::map<Slvs_hEntity, Slvs_hEntity> aRelocationMap;
+ std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIter = myFeatureMap.begin();
+ for (; aFeatIter != myFeatureMap.end(); aFeatIter++) {
+ Slvs_hEntity aPrevID = aFeatIter->second;
+ aFeatIter->second = changeEntity(aFeatIter->first, aType);
+ if (aFeatIter->second != aPrevID)
+ aRelocationMap[aPrevID] = aFeatIter->second;
+ }
+ std::map<AttributePtr, Slvs_hEntity>::iterator anAttrIter = myAttributeMap.begin();
+ for (; anAttrIter != myAttributeMap.end(); anAttrIter++) {
+ Slvs_hEntity aPrevID = anAttrIter->second;
+ anAttrIter->second = changeEntity(anAttrIter->first, aType);
+ if (anAttrIter->second != aPrevID)
+ aRelocationMap[aPrevID] = anAttrIter->second;
+ }
+
+ // Value if exists
+ DataPtr aData = myBaseConstraint->data();
+ if (!aData) return;
+ AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+ myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
+ double aValue = aValueAttr ? aValueAttr->value() : 0.0;
+
+ // Update constraint
+ std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
+ for (; aCIter != mySlvsConstraints.end(); aCIter++) {
+ Slvs_Constraint aConstraint = myStorage->getConstraint(*aCIter);
+ if (aValueAttr)
+ aConstraint.valA = aValue;
+ Slvs_hEntity* aCoeffs[6] = {
+ &aConstraint.ptA, &aConstraint.ptB,
+ &aConstraint.entityA, &aConstraint.entityB,
+ &aConstraint.entityC, &aConstraint.entityD};
+ for (int i = 0; i < 6; i++) {
+ if (*(aCoeffs[i]) == SLVS_E_UNKNOWN)
+ continue;
+ 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);
+ }
+ for (size_t i = 0; i < mySlvsConstraints.size(); i++)
+ if (aRemovedConstraints.find(mySlvsConstraints[i]) != aRemovedConstraints.end()) {
+ mySlvsConstraints.erase(mySlvsConstraints.begin() + i);
+ i--;
+ }
+}
+
+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()) {