]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Improve Distance constraint
authorvsv <vitaly.smetannikov@opencascade.com>
Tue, 5 Aug 2014 08:03:06 +0000 (12:03 +0400)
committervsv <vitaly.smetannikov@opencascade.com>
Tue, 5 Aug 2014 08:03:06 +0000 (12:03 +0400)
src/ModuleBase/ModuleBase_Operation.cpp
src/ModuleBase/ModuleBase_WidgetFeature.cpp
src/ModuleBase/ModuleBase_WidgetFeatureOrAttribute.cpp
src/PartSet/PartSet_OperationFeatureCreate.cpp
src/SketchPlugin/SketchPlugin_ConstraintDistance.cpp
src/SketchPlugin/SketchPlugin_Plugin.cpp
src/SketchPlugin/SketchPlugin_Validators.cpp
src/SketchPlugin/plugin-Sketch.xml

index ba44d45817af3591dde5e221d6893195b15b0e5d..44eddb349c00e32ab19c6419dbc51a5c4d797699 100644 (file)
@@ -102,12 +102,15 @@ bool ModuleBase_Operation::canBeCommitted() const
 
     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
-    const ModelAPI_Validator* aValidator = aFactory->validator(aId);
-    if (aValidator) {
+    std::list<ModelAPI_Validator*> aValidators;
+    aFactory->validators(aId, aValidators);
+    std::list<ModelAPI_Validator*>::const_iterator aIt;
+    for (aIt = aValidators.cbegin(); aIt != aValidators.cend(); ++aIt) {
       const ModuleBase_FeatureValidator* aFValidator = 
-        dynamic_cast<const ModuleBase_FeatureValidator*>(aValidator);
+        dynamic_cast<const ModuleBase_FeatureValidator*>(*aIt);
       if (aFValidator) {
-        return aFValidator->isValid(aFeature);
+        if (!aFValidator->isValid(aFeature))
+          return false;
       }
     }
     return true;
index 0246514d1bd64138fd7bcdfb2a6f73aced1c2274..e699a72de437ad568513e7e7fb2f0652fb641f07 100644 (file)
@@ -80,16 +80,20 @@ bool ModuleBase_WidgetFeature::setObject(const ObjectPtr& theObject)
   std::list<std::list<std::string> > anArguments;
   aFactory->validators(parentID(), attributeID(), aValidators, anArguments);
   std::list<ModelAPI_Validator*>::iterator aValidator = aValidators.begin();
+  bool isValid = true;
   for(; aValidator != aValidators.end(); aValidator++) {
-    if (*aValidator) {
-      const ModuleBase_ResultValidator* aResValidator = 
-        dynamic_cast<const ModuleBase_ResultValidator*>(*aValidator);
-      if (aResValidator) {
-        if (!aResValidator->isValid(theObject))
-          return false;
+    const ModuleBase_ResultValidator* aResValidator = 
+      dynamic_cast<const ModuleBase_ResultValidator*>(*aValidator);
+    if (aResValidator) {
+      isValid = false;
+      if (aResValidator->isValid(theObject)) {
+        isValid = true;
+        break;
       }
     }
   }
+  if (!isValid)
+    return false;
 
   myObject = theObject;
   myEditor->setText(theObject ? theObject->data()->name().c_str() : "");
index 10f8ba4f3112a67dbe0da36da82343d7eacfe9fe..daa23d5311179de0534d08cd9df66fe8b8df8fe6 100644 (file)
@@ -53,7 +53,7 @@ bool ModuleBase_WidgetFeatureOrAttribute::setValue(ModuleBase_WidgetValue* theVa
       if (aObject) {
         isDone = setObject(aObject);
       }
-      if (!isDone && aValuePoint) {
+      if (aValuePoint) {
         FeaturePtr aFeature = ModuleBase_Tools::feature(aObject);
         if (aFeature) {
           // find the given point in the feature attributes
@@ -89,7 +89,7 @@ bool ModuleBase_WidgetFeatureOrAttribute::storeValue(ObjectPtr theFeature) const
   ModuleBase_WidgetFeatureOrAttribute* that = (ModuleBase_WidgetFeatureOrAttribute*) this;
   if (object())
     aRef->setObject(object());
-  else if (myAttribute)
+  if (myAttribute)
     aRef->setAttr(myAttribute);
 
   aFeature->execute();
@@ -112,7 +112,7 @@ bool ModuleBase_WidgetFeatureOrAttribute::restoreValue(ObjectPtr theFeature)
     std::string aText = "";
     if (aFeature)
       aText = aFeature->data()->name().c_str();
-    else if (myAttribute)
+    if (myAttribute)
       aText = myAttribute->attributeType().c_str();
 
     editor()->setText(aText.c_str());
index 816153a00a7c58d06e7474913222d7a355bdca1d..f8b2dddf12af432060c9eb262d62d0e52654ee36 100644 (file)
@@ -271,6 +271,8 @@ FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMess
 
 bool PartSet_OperationFeatureCreate::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
 {
+  if (!myActiveWidget)
+    return false;
   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
   aValue->setObject(theFeature);
   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
index 0c499fff0e88763ed5078994ed8e41bb5889ba1a..531f09bd6a9fcd1cfdbad02fd3137c9281fad2e5 100644 (file)
@@ -5,6 +5,7 @@
 #include "SketchPlugin_ConstraintDistance.h"
 #include <SketchPlugin_Point.h>
 #include <SketchPlugin_Circle.h>
+#include <SketchPlugin_Line.h>
 
 #include <GeomAPI_Lin2d.h>
 #include <GeomAPI_Pnt2d.h>
@@ -18,11 +19,17 @@ static boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(
             DataPtr             theData,
             const std::string&  theAttribute);
 
+static boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData, const std::string& theAttribute);
+
+static boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const boost::shared_ptr<SketchPlugin_Line>& theLine, 
+                                                           const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint);
+
 
 SketchPlugin_ConstraintDistance::SketchPlugin_ConstraintDistance()
 {
 }
 
+//*************************************************************************************
 void SketchPlugin_ConstraintDistance::initAttributes()
 {
   data()->addAttribute(SketchPlugin_Constraint::VALUE(),    ModelAPI_AttributeDouble::type());
@@ -31,22 +38,39 @@ void SketchPlugin_ConstraintDistance::initAttributes()
   data()->addAttribute(SketchPlugin_Constraint::ENTITY_B(), ModelAPI_AttributeRefAttr::type());
 }
 
+//*************************************************************************************
 void SketchPlugin_ConstraintDistance::execute()
 {
   boost::shared_ptr<ModelAPI_Data> aData = data();
-
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
-  boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
-  if (aPoint_A && aPoint_B) {
-    AttributeDoublePtr anAttr_Value =
-      boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(SketchPlugin_Constraint::VALUE()));
-
-    if (!anAttr_Value->isInitialized()) {
-      anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
+  AttributeDoublePtr anAttr_Value =
+    boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+      aData->attribute(SketchPlugin_Constraint::VALUE()));
+
+  if (!anAttr_Value->isInitialized()) {
+    boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = 
+      getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
+    boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = 
+      getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
+  
+    if (aPoint_A && aPoint_B) { // both points
+        anAttr_Value->setValue(aPoint_A->pnt()->distance(aPoint_B->pnt()));
+    } else {
+      if (!aPoint_A && aPoint_B) { //Line and point
+        boost::shared_ptr<SketchPlugin_Line> aLine = 
+          getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
+        if (aLine)
+          anAttr_Value->setValue(aLine->distanceToPoint(aPoint_B->pnt()));
+      } else if (aPoint_A && !aPoint_B) { // Point and line
+        boost::shared_ptr<SketchPlugin_Line> aLine = 
+          getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
+        if(aLine)
+          anAttr_Value->setValue(aLine->distanceToPoint(aPoint_A->pnt()));
+      }
     }
   }
 }
 
+//*************************************************************************************
 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintDistance::getAISObject(
                     boost::shared_ptr<GeomAPI_AISObject> thePrevious)
 {
@@ -58,14 +82,36 @@ boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintDistance::getAISObje
   DataPtr aData = data();
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_A = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_A());
   boost::shared_ptr<GeomDataAPI_Point2D> aPoint_B = getFeaturePoint(aData, SketchPlugin_Constraint::ENTITY_B());
-  if (!aPoint_A || !aPoint_B)
+
+  boost::shared_ptr<GeomAPI_Pnt2d> aPnt_A;
+  boost::shared_ptr<GeomAPI_Pnt2d> aPnt_B;
+
+  if (aPoint_A && aPoint_B) {
+    aPnt_A = aPoint_A->pnt();
+    aPnt_B = aPoint_B->pnt();
+  } else if (!aPoint_A && aPoint_B) {
+    boost::shared_ptr<SketchPlugin_Line> aLine = 
+      getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_A());
+    if (aLine) {
+      aPnt_B = aPoint_B->pnt();
+      aPnt_A = getProjectionPoint(aLine, aPnt_B);
+    }
+  } else if (aPoint_A && !aPoint_B) {
+    boost::shared_ptr<SketchPlugin_Line> aLine = 
+      getFeatureLine(aData, SketchPlugin_Constraint::ENTITY_B());
+    if (aLine) {
+      aPnt_A = aPoint_A->pnt();
+      aPnt_B = getProjectionPoint(aLine, aPnt_A);
+    }
+  }
+  if (!aPnt_A || !aPnt_B)
     return thePrevious;
 
   boost::shared_ptr<GeomDataAPI_Point2D> aFlyOutAttr = 
     boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT()));
 
-  boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPoint_A->x(), aPoint_A->y());
-  boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPoint_B->x(), aPoint_B->y());
+  boost::shared_ptr<GeomAPI_Pnt> aPoint1 = sketch()->to3D(aPnt_A->x(), aPnt_A->y());
+  boost::shared_ptr<GeomAPI_Pnt> aPoint2 = sketch()->to3D(aPnt_B->x(), aPnt_B->y());
   boost::shared_ptr<GeomAPI_Pnt> aFlyoutPnt = aFlyOutAttr->isInitialized() ? 
                                               sketch()->to3D(aFlyOutAttr->x(), aFlyOutAttr->y()) : 
                                               boost::shared_ptr<GeomAPI_Pnt>();
@@ -82,6 +128,7 @@ boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_ConstraintDistance::getAISObje
   return anAIS;
 }
 
+//*************************************************************************************
 void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
 {
   boost::shared_ptr<ModelAPI_Data> aData = data();
@@ -93,6 +140,7 @@ void SketchPlugin_ConstraintDistance::move(double theDeltaX, double theDeltaY)
   aPoint1->setValue(aPoint1->x() + theDeltaX, aPoint1->y() + theDeltaY);
 }
 
+//*************************************************************************************
 boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
                                                        const std::string& theAttribute)
 {
@@ -113,9 +161,42 @@ boost::shared_ptr<GeomDataAPI_Point2D> getFeaturePoint(DataPtr theData,
   else if (aFeature && aFeature->getKind() == SketchPlugin_Circle::ID())
     aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
                                           (aFeature->data()->attribute(SketchPlugin_Circle::CENTER_ID()));
-  else {
-    if (anAttr->attr())
+  else if (anAttr->attr()) {
       aPointAttr = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr->attr());
   }
   return aPointAttr;
 }
+
+//*************************************************************************************
+boost::shared_ptr<SketchPlugin_Line> getFeatureLine(DataPtr theData, const std::string& theAttribute)
+{
+  boost::shared_ptr<SketchPlugin_Line> aLine;
+  if (!theData)
+    return aLine;
+
+  boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
+    boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(theData->attribute(theAttribute));
+  if (anAttr) {
+    FeaturePtr aFeature = SketchPlugin_Sketch::getFeature(anAttr->object());
+    if (aFeature && aFeature->getKind() == SketchPlugin_Line::ID()) {
+      aLine = boost::dynamic_pointer_cast<SketchPlugin_Line>(aFeature);
+    }
+  }
+  return aLine;
+}
+
+//*************************************************************************************
+boost::shared_ptr<GeomAPI_Pnt2d> getProjectionPoint(const boost::shared_ptr<SketchPlugin_Line>& theLine, 
+                                                          const boost::shared_ptr<GeomAPI_Pnt2d>& thePoint)
+{
+  boost::shared_ptr<ModelAPI_Data> aData = theLine->data();
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint1 =
+        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+        aData->attribute(SketchPlugin_Line::START_ID()));
+  boost::shared_ptr<GeomDataAPI_Point2D> aPoint2 =
+        boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(
+        aData->attribute(SketchPlugin_Line::END_ID()));
+
+  GeomAPI_Lin2d aLin2d(aPoint1->x(), aPoint1->y(), aPoint2->x(), aPoint2->y());
+  return aLin2d.project(thePoint);
+}
\ No newline at end of file
index 687e2cd94d0f8055ddaa4ed18a31a0d0ee90c281..8c9de8767b5200a08ee51334b94b0baad7e5fd05 100644 (file)
@@ -24,7 +24,6 @@ SketchPlugin_Plugin::SketchPlugin_Plugin()
 {
   PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
-  aFactory->registerValidator("SketchPlugin_DistanceFeatureValidator", new SketchPlugin_DistanceFeatureValidator);
 
   // register this plugin
   ModelAPI_PluginManager::get()->registerPlugin(this);
index 7f44ef5b1ab6de9b08b80746441ea4027e56c042..161a1442c37dcf3930a90631b421c55549d2347e 100644 (file)
@@ -38,7 +38,8 @@ bool SketchPlugin_DistanceFeatureValidator::isValid(const FeaturePtr theFeature)
   if (!aRefA || !aRefB)
     return false;
 
-  FeaturePtr aFetureA = SketchPlugin_Sketch::getFeature(aRefA->object());
+  return true;
+/*  FeaturePtr aFetureA = SketchPlugin_Sketch::getFeature(aRefA->object());
   FeaturePtr aFetureB = SketchPlugin_Sketch::getFeature(aRefB->object());
   if (!aFetureA || !aFetureB)
     return false;
@@ -52,5 +53,5 @@ bool SketchPlugin_DistanceFeatureValidator::isValid(const FeaturePtr theFeature)
     return isValidType(aTypeA);
   } else
     return isValidType(aTypeA) && isValidType(aTypeB);
-  return false;
+  return false;*/
 }
index 96ccaefce297951e05f809ba48798859bdb558f6..ed01bb4ca12cc34889bdba4c878842ae1ef10e61 100644 (file)
         <label title="Select point and another feature (point or point on line) between which to calculate distance" tooltip="Select point and another feature (point or point on line) between which to calculate distance"/>
         <feature_or_attribute_selector id="ConstraintEntityA" label="First object" tooltip="Select an point in the viewer">
           <validator id="ModuleBase_ResultPointValidator"/>
+          <validator id="ModuleBase_ResultLineValidator"/>
         </feature_or_attribute_selector>
         <feature_or_attribute_selector id="ConstraintEntityB" label="Last object" tooltip="Select an point in the viewer">
           <validator id="ModuleBase_ResultPointValidator"/>
+          <validator id="ModuleBase_ResultLineValidator"/>
         </feature_or_attribute_selector>
         <point_selector id="ConstraintFlyoutValuePnt" internal="1"/>
         <doublevalue_editor label="Value" tooltip="Constraint value" id="ConstraintValue"/>
         <validator id="PartSet_DistanceValidator"/>
-        <validator id="SketchPlugin_DistanceFeatureValidator"/> 
       </feature>
       
       <feature id="SketchConstraintLength" title="Length" tooltip="Create constraint for the given length of a line segment">