Salome HOME
Remove DirY from Sketch attributes
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
index 41182b67466ec120220231ecb90a898b894fb989..f7c4ab57f0c6fde0261324b1410766d0acf8a20b 100644 (file)
@@ -20,6 +20,7 @@
 #include <GeomAPI_Pnt2d.h>
 #include <GeomAPI_Pnt.h>
 #include <GeomAPI_Edge.h>
+#include <GeomAPI_Vertex.h>
 
 #include <GeomAPI_Dir.h>
 #include <GeomAPI_XYZ.h>
@@ -35,6 +36,7 @@
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Line.h>
+#include <SketchPlugin_Point.h>
 
 #include <ModuleBase_ViewerPrs.h>
 
@@ -97,8 +99,9 @@ Handle(V3d_View) theView,
 
   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> anY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
+  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+      aData->attribute(SketchPlugin_Sketch::NORM_ID()));
+  std::shared_ptr<GeomAPI_XYZ> anY = aNorm->xyz()->cross(aX->xyz());
 
   gp_Pnt anOriginPnt(anOrigin->x(), anOrigin->y(), anOrigin->z());
   gp_Vec aVec(anOriginPnt, thePoint);
@@ -136,13 +139,14 @@ std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const
       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
+  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+      aData->attribute(SketchPlugin_Sketch::NORM_ID()));
+  std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
 
   std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = 
     std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
 
-  return aPnt2d->to3D(aC->pnt(), aX->dir(), aY->dir());
+  return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
 }
 
 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
@@ -306,6 +310,35 @@ void PartSet_Tools::createConstraint(CompositeFeaturePtr theSketch,
     aFeature->execute();
 }
 
+std::shared_ptr<GeomDataAPI_Point2D> PartSet_Tools::
+  findAttributePoint(CompositeFeaturePtr theSketch, double theX, double theY,
+  double theTolerance, const QList<FeaturePtr>& theIgnore)
+{
+  std::shared_ptr<GeomAPI_Pnt2d> aClickedPoint = std::shared_ptr<GeomAPI_Pnt2d>(
+      new GeomAPI_Pnt2d(theX, theY));
+
+  std::list<std::shared_ptr<ModelAPI_Attribute> > anAttiributes;
+  for (int i = 0; i < theSketch->numberOfSubs(); i++) {
+    FeaturePtr aFeature = theSketch->subFeature(i);
+    if (!theIgnore.contains(aFeature)) {
+      anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
+
+      std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt;
+      for (anIt = anAttiributes.cbegin(); anIt != anAttiributes.cend(); ++anIt) {
+        std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
+          std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
+        double x = aCurPoint->x();
+        double y = aCurPoint->y();
+        if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < theTolerance)) {
+          return aCurPoint;
+        }
+      }
+    }
+  }
+  return std::shared_ptr<GeomDataAPI_Point2D>();
+}
+
+
 void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr theFeature,
                                    const std::string& theAttribute, double theClickedX,
                                    double theClickedY)
@@ -330,16 +363,20 @@ void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr the
       new GeomAPI_Pnt2d(theClickedX, theClickedY));
   for (; anIt != aLast; anIt++) {
     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
+    if (theFeature == aFeature)
+      continue;
     // find the given point in the feature attributes
     anAttiributes = aFeature->data()->attributes(GeomDataAPI_Point2D::type());
     std::list<std::shared_ptr<ModelAPI_Attribute> >::const_iterator anIt = anAttiributes.begin(),
         aLast = anAttiributes.end();
     std::shared_ptr<GeomDataAPI_Point2D> aFPoint;
     for (; anIt != aLast && !aFPoint; anIt++) {
-      std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = std::dynamic_pointer_cast<
-          GeomDataAPI_Point2D>(*anIt);
-      if (aCurPoint && aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())
+      std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
+        std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
+      if (aCurPoint && (aCurPoint->pnt()->distance(aClickedPoint) < Precision::Confusion())) {
         aFPoint = aCurPoint;
+        break;
+      }
     }
     if (aFPoint)
       PartSet_Tools::createConstraint(theSketch, aFPoint, aFeaturePoint);
@@ -351,23 +388,25 @@ std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theS
   std::shared_ptr<GeomAPI_Pln> aPlane;
 
   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
-  std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
-      aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      aData->attribute(SketchPlugin_Sketch::NORM_ID()));
-  if (aNormal && anOrigin) {
-    double adX = aNormal->x();
-    double adY = aNormal->y();
-    double adZ = aNormal->z();
-
-    if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
-      double aX = anOrigin->x();
-      double aY = anOrigin->y();
-      double aZ = anOrigin->z();
-      gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
-      double aA, aB, aC, aD;
-      aPln.Coefficients(aA, aB, aC, aD);
-      aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
+  if (aData) {
+    std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
+        aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+    std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+        aData->attribute(SketchPlugin_Sketch::NORM_ID()));
+    if (aNormal && anOrigin) {
+      double adX = aNormal->x();
+      double adY = aNormal->y();
+      double adZ = aNormal->z();
+
+      if ( (adX != 0) || (adY != 0) || (adZ != 0) ) { // Plane is valid
+        double aX = anOrigin->x();
+        double aY = anOrigin->y();
+        double aZ = anOrigin->z();
+        gp_Pln aPln(gp_Pnt(aX, aY, aZ), gp_Dir(adX, adY, adZ));
+        double aA, aB, aC, aD;
+        aPln.Coefficients(aA, aB, aC, aD);
+        aPlane = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aA, aB, aC, aD));
+      }
     }
   }
   return aPlane;
@@ -380,78 +419,103 @@ std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::point3D(std::shared_ptr<GeomAPI_Pnt2
   if (!theSketch || !thePoint2D)
     return aPoint;
 
+  DataPtr aData = theSketch->data();
   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
-      theSketch->data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
+      aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      theSketch->data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
-  std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
-      theSketch->data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
-
-  return thePoint2D->to3D(aC->pnt(), aX->dir(), aY->dir());
-}
+      aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
+  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
+      aData->attribute(SketchPlugin_Sketch::NORM_ID()));
+  std::shared_ptr<GeomAPI_Dir> aY(new GeomAPI_Dir(aNorm->dir()->cross(aX->dir())));
 
-bool PartSet_Tools::isConstraintFeature(const std::string& theKind)
-{
-  return theKind == SketchPlugin_ConstraintDistance::ID()
-      || theKind == SketchPlugin_ConstraintLength::ID()
-      || theKind == SketchPlugin_ConstraintRadius::ID()
-      || theKind == SketchPlugin_ConstraintRigid::ID();
+  return thePoint2D->to3D(aC->pnt(), aX->dir(), aY);
 }
 
-ResultPtr PartSet_Tools::createFixedObjectByEdge(const TopoDS_Shape& theShape, 
+ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape, 
                                                  const ObjectPtr& theObject, 
                                                  CompositeFeaturePtr theSketch)
 {
-  if (theShape.ShapeType() != TopAbs_EDGE)
-    return ResultPtr();
-
-  // Check that we already have such external edge
-  std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
-  aInEdge->setImpl(new TopoDS_Shape(theShape));
-  ResultPtr aResult = findExternalEdge(theSketch, aInEdge);
-  if (aResult)
-    return aResult;
-
-  // If not found then we have to create new
-  Standard_Real aStart, aEnd;
-  Handle(V3d_View) aNullView;
-  FeaturePtr aMyFeature;
-
-  Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
-  GeomAdaptor_Curve aAdaptor(aCurve);
-  if (aAdaptor.GetType() == GeomAbs_Line) {
-    // Create line
-    aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
-  } else if (aAdaptor.GetType() == GeomAbs_Circle) {
-    if (aAdaptor.IsClosed()) {
-      // Create circle
-      aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
-    } else {
-      // Create arc
-      aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
+  if (theShape.ShapeType() == TopAbs_EDGE) {
+    // Check that we already have such external edge
+    std::shared_ptr<GeomAPI_Edge> aInEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge());
+    aInEdge->setImpl(new TopoDS_Shape(theShape));
+    ResultPtr aResult = findExternalEdge(theSketch, aInEdge);
+    if (aResult)
+      return aResult;
+
+    // If not found then we have to create new
+    Standard_Real aStart, aEnd;
+    Handle(V3d_View) aNullView;
+    FeaturePtr aMyFeature;
+
+    Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(theShape), aStart, aEnd);
+    GeomAdaptor_Curve aAdaptor(aCurve);
+    if (aAdaptor.GetType() == GeomAbs_Line) {
+      // Create line
+      aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
+    } else if (aAdaptor.GetType() == GeomAbs_Circle) {
+      if (aAdaptor.IsClosed()) {
+        // Create circle
+        aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
+      } else {
+        // Create arc
+        aMyFeature = theSketch->addFeature(SketchPlugin_Arc::ID());
+      }
     }
-  }
-  if (aMyFeature) {
-    DataPtr aData = aMyFeature->data();
-    AttributeSelectionPtr anAttr = 
-      std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
-      (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
+    if (aMyFeature) {
+      DataPtr aData = aMyFeature->data();
+      AttributeSelectionPtr anAttr = 
+        std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+        (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
 
-    ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
-    if (anAttr && aRes) {
-      std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
-      anEdge->setImpl(new TopoDS_Shape(theShape));
+      ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+      if (anAttr && aRes) {
+        std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
+        anEdge->setImpl(new TopoDS_Shape(theShape));
 
-      anAttr->setValue(aRes, anEdge);
+        anAttr->setValue(aRes, anEdge);
 
-      aMyFeature->execute();
+        aMyFeature->execute();
 
-      // fix this edge
-      FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
-      aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
-        setObject(aMyFeature->lastResult());
+        // fix this edge
+        FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
+        aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
+          setObject(aMyFeature->lastResult());
 
-      return aMyFeature->lastResult();
+        return aMyFeature->lastResult();
+      }
+    }
+  }
+  if (theShape.ShapeType() == TopAbs_VERTEX) {
+    std::shared_ptr<GeomAPI_Vertex> aInVert = std::shared_ptr<GeomAPI_Vertex>(new GeomAPI_Vertex());
+    aInVert->setImpl(new TopoDS_Shape(theShape));
+    ResultPtr aResult = findExternalVertex(theSketch, aInVert);
+    if (aResult)
+      return aResult;
+
+    FeaturePtr aMyFeature = theSketch->addFeature(SketchPlugin_Point::ID());
+
+    if (aMyFeature) {
+      DataPtr aData = aMyFeature->data();
+      AttributeSelectionPtr anAttr = 
+        std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
+        (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
+
+      ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
+      if (anAttr && aRes) {
+        std::shared_ptr<GeomAPI_Shape> aVert(new GeomAPI_Shape);
+        aVert->setImpl(new TopoDS_Shape(theShape));
+
+        anAttr->setValue(aRes, aVert);
+        aMyFeature->execute();
+
+        // fix this edge
+        FeaturePtr aFix = theSketch->addFeature(SketchPlugin_ConstraintRigid::ID());
+        aFix->data()->refattr(SketchPlugin_Constraint::ENTITY_A())->
+          setObject(aMyFeature->lastResult());
+
+        return aMyFeature->lastResult();
+      }
     }
   }
   return ResultPtr();
@@ -494,6 +558,35 @@ ResultPtr PartSet_Tools::findExternalEdge(CompositeFeaturePtr theSketch, std::sh
   return ResultPtr();
 }
 
+
+ResultPtr PartSet_Tools::findExternalVertex(CompositeFeaturePtr theSketch, std::shared_ptr<GeomAPI_Vertex> theVert)
+{
+  for (int i = 0; i < theSketch->numberOfSubs(); i++) {
+    FeaturePtr aFeature = theSketch->subFeature(i);
+    std::shared_ptr<SketchPlugin_Feature> aSketchFea = 
+      std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
+    if (aSketchFea) {
+      if (aSketchFea->isExternal()) {
+        std::list<ResultPtr> aResults = aSketchFea->results();
+        std::list<ResultPtr>::const_iterator aIt;
+        for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
+          ResultConstructionPtr aRes = 
+            std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aIt);
+          if (aRes) {
+            std::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
+            if (aShape) {
+              if (theVert->isEqual(aShape))
+                return aRes;
+            }
+          }
+        }
+      }
+    }
+  }
+  return ResultPtr();
+}
+
+
 bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch,
                                    Handle_V3d_View theView, double& theX, double& theY)
 {