Salome HOME
Remove DirY from Sketch attributes
[modules/shaper.git] / src / PartSet / PartSet_Tools.cpp
index c97b6c68f2b55fd192de55fdc44544f80539c36f..f7c4ab57f0c6fde0261324b1410766d0acf8a20b 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 // File:        PartSet_Tools.h
 // Created:     28 Apr 2014
 // Author:      Natalia ERMOLAEVA
@@ -18,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>
@@ -33,6 +36,7 @@
 #include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Arc.h>
 #include <SketchPlugin_Line.h>
+#include <SketchPlugin_Point.h>
 
 #include <ModuleBase_ViewerPrs.h>
 
@@ -95,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);
@@ -126,26 +131,22 @@ Handle(V3d_View) theView,
   theY = aVec.X() * anY->x() + aVec.Y() * anY->y() + aVec.Z() * anY->z();
 }
 
-void PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch,
-                                gp_Pnt& thePoint)
+std::shared_ptr<GeomAPI_Pnt> PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch)
 {
-  if (!theSketch)
-    return;
-
   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
 
   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
       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_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
-      ->added(aY->dir()->xyz()->multiplied(theY));
+  std::shared_ptr<GeomAPI_Pnt2d> aPnt2d = 
+    std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY));
 
-  std::shared_ptr<GeomAPI_Pnt> aPoint = std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
-  thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z());
+  return aPnt2d->to3D(aC->pnt(), aX->dir(), aY);
 }
 
 ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView,
@@ -309,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)
@@ -333,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);
@@ -352,19 +386,29 @@ void PartSet_Tools::setConstraints(CompositeFeaturePtr theSketch, FeaturePtr the
 std::shared_ptr<GeomAPI_Pln> PartSet_Tools::sketchPlane(CompositeFeaturePtr theSketch)
 {
   std::shared_ptr<GeomAPI_Pln> aPlane;
-  double aA, aB, aC, aD;
 
   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()));
-  aA = aNormal->x();
-  aB = aNormal->y();
-  aC = aNormal->z();
-  aD = 0;
-
-  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;
 }
 
@@ -375,97 +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 ModuleBase_ViewerPrs& thePrs, CompositeFeaturePtr theSketch)
+ResultPtr PartSet_Tools::createFixedObjectByExternal(const TopoDS_Shape& theShape, 
+                                                 const ObjectPtr& theObject, 
+                                                 CompositeFeaturePtr theSketch)
 {
-  TopoDS_Shape aShape = thePrs.shape();
-  if (aShape.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(aShape));
-  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(aShape), aStart, aEnd);
-  GeomAdaptor_Curve aAdaptor(aCurve);
-  if (aAdaptor.GetType() == GeomAbs_Line) {
-    // Create line
-    aMyFeature = theSketch->addFeature(SketchPlugin_Line::ID());
-
-    //DataPtr aData = myFeature->data();
-    //std::shared_ptr<GeomDataAPI_Point2D> anEndAttr = 
-    //  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
-
-    //double aX, aY;
-    //gp_Pnt Pnt1 = aAdaptor.Value(aStart);
-    //convertTo2D(Pnt1, theSketch, aNullView, aX, aY);
-    //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::START_ID());
-
-    //gp_Pnt Pnt2 = aAdaptor.Value(aEnd);
-    //convertTo2D(Pnt2, theSketch, aNullView, aX, aY);
-    //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Line::END_ID());
-  } else if (aAdaptor.GetType() == GeomAbs_Circle) {
-    if (aAdaptor.IsClosed()) {
-      // Create circle
-      aMyFeature = theSketch->addFeature(SketchPlugin_Circle::ID());
-      //gp_Circ aCirc = aAdaptor.Circle();
-      //gp_Pnt aCenter = aCirc.Location();
-
-      //double aX, aY;
-      //convertTo2D(aCenter, theSketch, aNullView, aX, aY);
-      //setFeaturePoint(myFeature, aX, aY, SketchPlugin_Circle::CENTER_ID());
-      //setFeatureValue(myFeature, aCirc.Radius(), SketchPlugin_Circle::RADIUS_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>(thePrs.object());
-    if (anAttr && aRes) {
-      std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
-      anEdge->setImpl(new TopoDS_Shape(aShape));
+      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();
@@ -508,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)
 {
@@ -527,3 +606,39 @@ bool PartSet_Tools::hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePt
 
   return aHasVertex;
 }
+
+AttributePtr PartSet_Tools::findAttributeBy2dPoint(ObjectPtr theObj, 
+                                                   const TopoDS_Shape theShape, 
+                                                   FeaturePtr theSketch)
+{
+
+  AttributePtr anAttribute;
+  FeaturePtr aFeature = ModelAPI_Feature::feature(theObj);
+  if (aFeature) {
+    if (theShape.ShapeType() == TopAbs_VERTEX) {
+      const TopoDS_Vertex& aVertex = TopoDS::Vertex(theShape);
+      if (!aVertex.IsNull())  {
+        gp_Pnt aPoint = BRep_Tool::Pnt(aVertex);
+        std::shared_ptr<GeomAPI_Pnt> aValue = std::shared_ptr<GeomAPI_Pnt>(
+            new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z()));
+
+        // find the given point in the feature attributes
+        std::list<AttributePtr> anAttiributes = 
+          aFeature->data()->attributes(GeomDataAPI_Point2D::type());
+        std::list<AttributePtr>::const_iterator anIt = anAttiributes.begin(), 
+                                                aLast = anAttiributes.end();
+        for (; anIt != aLast && !anAttribute; anIt++) {
+          std::shared_ptr<GeomDataAPI_Point2D> aCurPoint = 
+            std::dynamic_pointer_cast<GeomDataAPI_Point2D>(*anIt);
+
+          std::shared_ptr<GeomAPI_Pnt> aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch);
+          if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) {
+            anAttribute = aCurPoint;
+            break;
+          }
+        }
+      }
+    }
+  }
+  return anAttribute;
+}