#include <ModelAPI_AttributeRefList.h>
#include <ModelAPI_Data.h>
#include <Model_Events.h>
+
#include <SketchPlugin_Constraint.h>
#include <SketchPlugin_ConstraintCoincidence.h>
#include <SketchPlugin_Line.h>
#include <math.h>
#include <assert.h>
+#include <set>
+
/// Tolerance for value of parameters
const double tolerance = 1.e-10;
void SketchSolver_ConstraintManager::processEvent(const Events_Message* theMessage)
{
if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED) ||
- theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED))
+ theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED) ||
+ theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_MOVED))
{
- const Model_FeatureUpdatedMessage* aUpdateMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
+ const Model_FeatureUpdatedMessage* anUpdateMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
+ std::set< boost::shared_ptr<ModelAPI_Feature> > aFeatures = anUpdateMsg->features();
- // Only sketches and constraints can be added by Create event
- const std::string& aFeatureKind = aUpdateMsg->feature()->getKind();
- if (aFeatureKind.compare("Sketch") == 0)
- {
- boost::shared_ptr<SketchPlugin_Feature> aSketch =
- boost::dynamic_pointer_cast<SketchPlugin_Feature>(aUpdateMsg->feature());
- changeWorkplane(aSketch);
- return ;
- }
- boost::shared_ptr<SketchPlugin_Constraint> aConstraint =
- boost::dynamic_pointer_cast<SketchPlugin_Constraint>(aUpdateMsg->feature());
- if (aConstraint)
- changeConstraint(aConstraint);
- else
+ bool isModifiedEvt = theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_MOVED);
+ if (!isModifiedEvt)
{
- // Sketch plugin features can be only updated
- boost::shared_ptr<SketchPlugin_Feature> aFeature =
- boost::dynamic_pointer_cast<SketchPlugin_Feature>(aUpdateMsg->feature());
- if (aFeature)
- updateEntity(aFeature);
+ std::set< boost::shared_ptr<ModelAPI_Feature> >::iterator aFeatIter;
+ for (aFeatIter = aFeatures.begin(); aFeatIter != aFeatures.end(); aFeatIter++)
+ {
+ // Only sketches and constraints can be added by Create event
+ const std::string& aFeatureKind = (*aFeatIter)->getKind();
+ if (aFeatureKind.compare("Sketch") == 0)
+ {
+ boost::shared_ptr<SketchPlugin_Feature> aSketch =
+ boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
+ changeWorkplane(aSketch);
+ return ;
+ }
+ boost::shared_ptr<SketchPlugin_Constraint> aConstraint =
+ boost::dynamic_pointer_cast<SketchPlugin_Constraint>(*aFeatIter);
+ if (aConstraint)
+ changeConstraint(aConstraint);
+ else
+ {
+ // Sketch plugin features can be only updated
+ boost::shared_ptr<SketchPlugin_Feature> aFeature =
+ boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aFeatIter);
+ if (aFeature)
+ updateEntity(aFeature);
+ }
+ }
}
+
+ // Solve the set of constraints
+ resolveConstraints(isModifiedEvt);
}
else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED))
{
const Model_FeatureDeletedMessage* aDeleteMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
-
- if (aDeleteMsg->group().compare("Sketch") == 0)
+ const std::set<std::string>& aFeatureGroups = aDeleteMsg->groups();
+
+ // Find "Sketch" in groups. The constraint groups should be updated when an object removed from Sketch
+ std::set<std::string>::const_iterator aFGrIter;
+ for (aFGrIter = aFeatureGroups.begin(); aFGrIter != aFeatureGroups.end(); aFGrIter++)
+ if (aFGrIter->compare("Sketch") == 0)
+ break;
+
+ if (aFGrIter != aFeatureGroups.end())
{
std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter = myGroups.begin();
while (aGroupIter != myGroups.end())
}
}
}
- else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_MOVED))
- {
- // Solve the set of constraints
- resolveConstraints();
- }
}
bool SketchSolver_ConstraintManager::changeWorkplane(boost::shared_ptr<SketchPlugin_Feature> theSketch)
boost::shared_ptr<SketchPlugin_Feature> SketchSolver_ConstraintManager::findWorkplaneForConstraint(
boost::shared_ptr<SketchPlugin_Constraint> theConstraint) const
{
+ // Already verified workplanes
+ std::set< boost::shared_ptr<SketchPlugin_Feature> > aVerified;
+
std::vector<SketchSolver_ConstraintGroup*>::const_iterator aGroupIter;
for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
{
boost::shared_ptr<SketchPlugin_Feature> aWP = (*aGroupIter)->getWorkplane();
+ if (aVerified.find(aWP) != aVerified.end())
+ continue;
+
boost::shared_ptr<ModelAPI_AttributeRefList> aWPFeatures =
boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aWP->data()->attribute(SKETCH_ATTR_FEATURES));
std::list< boost::shared_ptr<ModelAPI_Feature> > aFeaturesList = aWPFeatures->list();
for (anIter = aFeaturesList.begin(); anIter != aFeaturesList.end(); anIter++)
if (*anIter == theConstraint)
return aWP; // workplane is found
+ aVerified.insert(aWP);
}
return boost::shared_ptr<SketchPlugin_Feature>();
}
-void SketchSolver_ConstraintManager::resolveConstraints()
+void SketchSolver_ConstraintManager::resolveConstraints(const bool needEvent)
{
std::vector<SketchSolver_ConstraintGroup*>::iterator aGroupIter;
for (aGroupIter = myGroups.begin(); aGroupIter != myGroups.end(); aGroupIter++)
(*aGroupIter)->resolveConstraints();
+
+ // Features may be updated => send events
+ if (needEvent)
+ Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
}
if (myWorkplane.h != SLVS_E_UNKNOWN && myConstraints.empty())
return true;
- /// \todo Should be implemented
+ // Go through constraint entities and verify if some of them already in the group
+ for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
+ {
+ boost::shared_ptr<ModelAPI_AttributeRefAttr> aCAttrRef =
+ boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(
+ theConstraint->data()->attribute(CONSTRAINT_ATTRIBUTES[i])
+ );
+ if (!aCAttrRef) continue;
+ if (myEntityMap.find(aCAttrRef->attr()) != myEntityMap.end())
+ return true;
+ }
+
+ // Entities did not found
return false;
}
void updateEntity(boost::shared_ptr<SketchPlugin_Feature> theFeature);
/** \brief Goes through the list of groups and solve the constraints
+ * \param[in] needEvent shows that some features are probably updated and Update event should be thrown
*/
- void resolveConstraints();
+ void resolveConstraints(const bool needEvent);
private:
class SketchSolver_ConstraintGroup;
// Created object event *******************
if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) {
const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
- FeaturePtr aFeature = aUpdMsg->feature();
- DocumentPtr aDoc = aFeature->document();
-
- if (aDoc == myDocument) { // If root objects
- if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
- // Add a new part
- int aStart = myPartModels.size() + 1;
- XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
- aModel->setPartId(myPartModels.count());
- myPartModels.append(aModel);
- insertRow(aStart, partFolderNode());
- } else { // Update top groups (other except parts
- QModelIndex aIndex = myModel->findParent(aFeature);
- int aStart = myModel->rowCount(aIndex) - 1;
- aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
- insertRow(aStart, aIndex);
- }
- } else { // if sub-objects of first level nodes
- XGUI_PartModel* aPartModel = 0;
- QList<XGUI_PartModel*>::const_iterator aIt;
- for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
- if ((*aIt)->hasDocument(aDoc)) {
- aPartModel = (*aIt);
- break;
+ std::set<FeaturePtr> aFeatures = aUpdMsg->features();
+
+ std::set<FeaturePtr>::const_iterator aIt;
+ for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
+ FeaturePtr aFeature = (*aIt);
+ DocumentPtr aDoc = aFeature->document();
+ if (aDoc == myDocument) { // If root objects
+ if (aFeature->getGroup().compare(PARTS_GROUP) == 0) { // Update only Parts group
+ // Add a new part
+ int aStart = myPartModels.size() + 1;
+ XGUI_PartDataModel* aModel = new XGUI_PartDataModel(myDocument, this);
+ aModel->setPartId(myPartModels.count());
+ myPartModels.append(aModel);
+ insertRow(aStart, partFolderNode());
+ } else { // Update top groups (other except parts
+ QModelIndex aIndex = myModel->findParent(aFeature);
+ int aStart = myModel->rowCount(aIndex) - 1;
+ aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
+ insertRow(aStart, aIndex);
+ }
+ } else { // if sub-objects of first level nodes
+ XGUI_PartModel* aPartModel = 0;
+ QList<XGUI_PartModel*>::const_iterator aIt;
+ for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
+ if ((*aIt)->hasDocument(aDoc)) {
+ aPartModel = (*aIt);
+ break;
+ }
+ }
+ if (aPartModel) {
+ QModelIndex aIndex = aPartModel->findParent(aFeature);
+ int aStart = aPartModel->rowCount(aIndex) - 1;
+ aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
+ insertRow(aStart, aIndex);
}
- }
- if (aPartModel) {
- QModelIndex aIndex = aPartModel->findParent(aFeature);
- int aStart = aPartModel->rowCount(aIndex) - 1;
- aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
- insertRow(aStart, aIndex);
}
}
-
// Deleted object event ***********************
} else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_DELETED)) {
const Model_FeatureDeletedMessage* aUpdMsg = dynamic_cast<const Model_FeatureDeletedMessage*>(theMessage);
DocumentPtr aDoc = aUpdMsg->document();
-
- if (aDoc == myDocument) { // If root objects
- if (aUpdMsg->group().compare(PARTS_GROUP) == 0) { // Updsate only Parts group
- int aStart = myPartModels.size() - 1;
- removeSubModel(aStart);
- removeRow(aStart, partFolderNode());
- } else { // Update top groups (other except parts
- QModelIndex aIndex = myModel->findGroup(aUpdMsg->group());
- int aStart = myModel->rowCount(aIndex);
- aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
- removeRow(aStart, aIndex);
- }
- } else {
- XGUI_PartModel* aPartModel = 0;
- QList<XGUI_PartModel*>::const_iterator aIt;
- for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
- if ((*aIt)->hasDocument(aDoc)) {
- aPartModel = (*aIt);
- break;
+ std::set<std::string> aGroups = aUpdMsg->groups();
+
+ std::set<std::string>::const_iterator aIt;
+ for (aIt = aGroups.begin(); aIt != aGroups.end(); ++aIt) {
+ std::string aGroup = (*aIt);
+ if (aDoc == myDocument) { // If root objects
+ if (aGroup.compare(PARTS_GROUP) == 0) { // Updsate only Parts group
+ int aStart = myPartModels.size() - 1;
+ removeSubModel(aStart);
+ removeRow(aStart, partFolderNode());
+ } else { // Update top groups (other except parts
+ QModelIndex aIndex = myModel->findGroup(aGroup);
+ int aStart = myModel->rowCount(aIndex);
+ aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
+ removeRow(aStart, aIndex);
+ }
+ } else {
+ XGUI_PartModel* aPartModel = 0;
+ QList<XGUI_PartModel*>::const_iterator aIt;
+ for (aIt = myPartModels.constBegin(); aIt != myPartModels.constEnd(); ++aIt) {
+ if ((*aIt)->hasDocument(aDoc)) {
+ aPartModel = (*aIt);
+ break;
+ }
+ }
+ if (aPartModel) {
+ QModelIndex aIndex = aPartModel->findGroup(aGroup);
+ int aStart = aPartModel->rowCount(aIndex);
+ aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
+ removeRow(aStart, aIndex);
}
- }
- if (aPartModel) {
- QModelIndex aIndex = aPartModel->findGroup(aUpdMsg->group());
- int aStart = aPartModel->rowCount(aIndex);
- aIndex = createIndex(aIndex.row(), aIndex.column(), (void*)getModelIndex(aIndex));
- removeRow(aStart, aIndex);
}
}
-
// Deleted object event ***********************
} else if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_UPDATED)) {
//const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
// Process creation of Part
if (theMessage->eventID() == Events_Loop::loop()->eventByName(EVENT_FEATURE_CREATED)) {
const Model_FeatureUpdatedMessage* aUpdMsg = dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
- FeaturePtr aFeature = aUpdMsg->feature();
- if (aFeature->getKind() == "Part") {
+ std::set<FeaturePtr> aFeatures = aUpdMsg->features();
+
+ std::set<FeaturePtr>::const_iterator aIt;
+ bool aHasPart = false;
+ for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
+ FeaturePtr aFeature = (*aIt);
+ if (aFeature->getKind() == "Part") {
+ aHasPart = true;
+ break;
+ }
+ }
+ if (aHasPart) {
//The created part will be created in Object Browser later and we have to activate it
// only when it is created everywere
QTimer::singleShot(50, this, SLOT(activateLastPart()));
{
const Model_FeatureUpdatedMessage* anUpdateMsg =
dynamic_cast<const Model_FeatureUpdatedMessage*>(theMessage);
- FeaturePtr aNewFeature = anUpdateMsg->feature();
+ std::set<FeaturePtr> aFeatures = anUpdateMsg->features();
+
FeaturePtr aCurrentFeature = myOperationMgr->currentOperation()->feature();
- if(aNewFeature == aCurrentFeature) {
- myPropertyPanel->updateContentWidget(aCurrentFeature);
+ std::set<FeaturePtr>::const_iterator aIt;
+ for (aIt = aFeatures.begin(); aIt != aFeatures.end(); ++aIt) {
+ FeaturePtr aNewFeature = (*aIt);
+ if(aNewFeature == aCurrentFeature) {
+ myPropertyPanel->updateContentWidget(aCurrentFeature);
+ break;
+ }
}
}
//An operation passed by message. Start it, process and commit.