From: dbv Date: Thu, 7 Jul 2016 09:31:40 +0000 (+0300) Subject: Added option to create Construction Point by intersection of two lines. X-Git-Tag: V_2.5.0~235 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=14b2f19dcba31d259343982c208daea43b407dc5;p=modules%2Fshaper.git Added option to create Construction Point by intersection of two lines. --- diff --git a/src/ConstructionPlugin/CMakeLists.txt b/src/ConstructionPlugin/CMakeLists.txt index 4b5ca9ddb..f31b6e820 100644 --- a/src/ConstructionPlugin/CMakeLists.txt +++ b/src/ConstructionPlugin/CMakeLists.txt @@ -9,6 +9,7 @@ SET(PROJECT_HEADERS ConstructionPlugin_Point.h ConstructionPlugin_Axis.h ConstructionPlugin_Plane.h + ConstructionPlugin_Validators.h ) SET(PROJECT_SOURCES @@ -16,6 +17,7 @@ SET(PROJECT_SOURCES ConstructionPlugin_Point.cpp ConstructionPlugin_Axis.cpp ConstructionPlugin_Plane.cpp + ConstructionPlugin_Validators.cpp ) SET(XML_RESOURCES @@ -51,6 +53,7 @@ INCLUDE_DIRECTORIES( ../ModelAPI ../GeomAPI ../GeomAlgoAPI + ../Events ) diff --git a/src/ConstructionPlugin/ConstructionPlugin_Plugin.cpp b/src/ConstructionPlugin/ConstructionPlugin_Plugin.cpp index c60c022b4..63d7308d2 100644 --- a/src/ConstructionPlugin/ConstructionPlugin_Plugin.cpp +++ b/src/ConstructionPlugin/ConstructionPlugin_Plugin.cpp @@ -4,6 +4,7 @@ #include "ConstructionPlugin_Point.h" #include "ConstructionPlugin_Axis.h" #include "ConstructionPlugin_Plane.h" +#include "ConstructionPlugin_Validators.h" #include @@ -17,6 +18,11 @@ static ConstructionPlugin_Plugin* MY_CONSTRUCTION_INSTANCE = new ConstructionPlu ConstructionPlugin_Plugin::ConstructionPlugin_Plugin() { + SessionPtr aMgr = ModelAPI_Session::get(); + ModelAPI_ValidatorsFactory* aFactory = aMgr->validators(); + aFactory->registerValidator("ConstructionPlugin_ValidatorPointLines", + new ConstructionPlugin_ValidatorPointLines()); + // register this plugin ModelAPI_Session::get()->registerPlugin(this); diff --git a/src/ConstructionPlugin/ConstructionPlugin_Point.cpp b/src/ConstructionPlugin/ConstructionPlugin_Point.cpp index 380393db7..22f91d732 100644 --- a/src/ConstructionPlugin/ConstructionPlugin_Point.cpp +++ b/src/ConstructionPlugin/ConstructionPlugin_Point.cpp @@ -46,6 +46,9 @@ void ConstructionPlugin_Point::initAttributes() data()->addAttribute(POINT(), ModelAPI_AttributeSelection::typeId()); data()->addAttribute(PLANE(), ModelAPI_AttributeSelection::typeId()); + + data()->addAttribute(FIRST_LINE(), ModelAPI_AttributeSelection::typeId()); + data()->addAttribute(SECOND_LINE(), ModelAPI_AttributeSelection::typeId()); } //================================================================================================== @@ -60,6 +63,8 @@ void ConstructionPlugin_Point::execute() aShape = createByDistanceOnEdge(); } else if(aCreationMethod == CREATION_METHOD_BY_PROJECTION()) { aShape = createByProjection(); + } else if(aCreationMethod == CREATION_METHOD_BY_LINES_INTERSECTION()) { + aShape = createByIntersection(); } if(aShape.get()) { @@ -130,3 +135,25 @@ std::shared_ptr ConstructionPlugin_Point::createByProjection() return GeomAlgoAPI_PointBuilder::vertexByProjection(aVertex, aFace); } + +//================================================================================================== +std::shared_ptr ConstructionPlugin_Point::createByIntersection() +{ + // Get first line. + AttributeSelectionPtr aFirstLineSelection= selection(FIRST_LINE()); + GeomShapePtr aFirstLineShape = aFirstLineSelection->value(); + if(!aFirstLineShape.get()) { + aFirstLineShape = aFirstLineSelection->context()->shape(); + } + std::shared_ptr aFirstEdge(new GeomAPI_Edge(aFirstLineShape)); + + // Get first line. + AttributeSelectionPtr aSecondLineSelection= selection(SECOND_LINE()); + GeomShapePtr aSecondLineShape = aSecondLineSelection->value(); + if(!aSecondLineShape.get()) { + aSecondLineShape = aSecondLineSelection->context()->shape(); + } + std::shared_ptr aSecondEdge(new GeomAPI_Edge(aSecondLineShape)); + + return GeomAlgoAPI_PointBuilder::vertexByIntersection(aFirstEdge, aSecondEdge); +} diff --git a/src/ConstructionPlugin/ConstructionPlugin_Point.h b/src/ConstructionPlugin/ConstructionPlugin_Point.h index 65d17aef2..b5619e7be 100644 --- a/src/ConstructionPlugin/ConstructionPlugin_Point.h +++ b/src/ConstructionPlugin/ConstructionPlugin_Point.h @@ -59,6 +59,13 @@ public: return MY_CREATION_METHOD_ID; } + /// Attribute name for creation method. + inline static const std::string& CREATION_METHOD_BY_LINES_INTERSECTION() + { + static const std::string MY_CREATION_METHOD_ID("by_lines_intersection"); + return MY_CREATION_METHOD_ID; + } + /// Attribute name for X coordinate. inline static const std::string& X() { @@ -122,6 +129,20 @@ public: return ATTR_ID; } + /// Attribute name for seleted first line. + inline static const std::string& FIRST_LINE() + { + static const std::string ATTR_ID("first_line"); + return ATTR_ID; + } + + /// Attribute name for seleted second line. + inline static const std::string& SECOND_LINE() + { + static const std::string ATTR_ID("second_line"); + return ATTR_ID; + } + /// Creates a new part document if needed. CONSTRUCTIONPLUGIN_EXPORT virtual void execute(); @@ -142,6 +163,7 @@ private: std::shared_ptr createByXYZ(); std::shared_ptr createByDistanceOnEdge(); std::shared_ptr createByProjection(); + std::shared_ptr createByIntersection(); }; diff --git a/src/ConstructionPlugin/ConstructionPlugin_Validators.cpp b/src/ConstructionPlugin/ConstructionPlugin_Validators.cpp new file mode 100644 index 000000000..098485d0a --- /dev/null +++ b/src/ConstructionPlugin/ConstructionPlugin_Validators.cpp @@ -0,0 +1,70 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> + +// File: ConstructionPlugin_Validators.cpp +// Created: 04 July 2016 +// Author: Dmitry Bobylev + +#include "ConstructionPlugin_Validators.h" + +#include +#include + +#include + +#include + +//================================================================================================== +bool ConstructionPlugin_ValidatorPointLines::isValid(const AttributePtr& theAttribute, + const std::list& theArguments, + Events_InfoMessage& theError) const +{ + FeaturePtr aFeature = ModelAPI_Feature::feature(theAttribute->owner()); + + AttributeSelectionPtr aLineAttribute1 = std::dynamic_pointer_cast(theAttribute); + AttributeSelectionPtr aLineAttribute2 = aFeature->selection(theArguments.front()); + + GeomShapePtr aLineShape1 = aLineAttribute1->value(); + ResultPtr aContext1 = aLineAttribute1->context(); + if(!aContext1.get()) { + theError = "One of the attribute not initialized."; + return false; + } + if(!aLineShape1.get()) { + aLineShape1 = aContext1->shape(); + } + if(!aLineShape1->isEdge()) { + theError = "One of the selected shapes not an edge."; + return false; + } + + GeomShapePtr aLineShape2 = aLineAttribute2->value(); + ResultPtr aContext2 = aLineAttribute2->context(); + if(!aContext2.get()) { + return true; + } + if(!aLineShape2.get()) { + aLineShape2 = aContext2->shape(); + } + if(!aLineShape2->isEdge()) { + theError = "One of the selected shapes not an edge."; + return false; + } + + std::shared_ptr aLineEdge1(new GeomAPI_Edge(aLineShape1)); + std::shared_ptr aLineEdge2(new GeomAPI_Edge(aLineShape2)); + + std::shared_ptr aLine1 = aLineEdge1->line(); + std::shared_ptr aLine2 = aLineEdge2->line(); + + if(!aLine1->isCoplanar(aLine2)) { + theError = "Selected lines not coplanar."; + return false; + } + + if(aLine1->isParallel(aLine2)) { + theError = "Selected lines are parallel."; + return false; + } + + return true; +} diff --git a/src/ConstructionPlugin/ConstructionPlugin_Validators.h b/src/ConstructionPlugin/ConstructionPlugin_Validators.h new file mode 100644 index 000000000..85dc4a431 --- /dev/null +++ b/src/ConstructionPlugin/ConstructionPlugin_Validators.h @@ -0,0 +1,27 @@ +// Copyright (C) 2014-20xx CEA/DEN, EDF R&D --> + +// File: ConstructionPlugin_Validators.h +// Created: 04 July 2016 +// Author: Dmitry Bobylev + +#ifndef ConstructionPlugin_Validators_H_ +#define ConstructionPlugin_Validators_H_ + +#include + +/// \class ConstructionPlugin_ValidatorPointLines +/// \ingroup Validators +/// \brief A validator for selection lines for point by intersection.. +class ConstructionPlugin_ValidatorPointLines: public ModelAPI_AttributeValidator +{ +public: + //! \return True if the attribute is valid. + //! \param[in] theAttribute the checked attribute. + //! \param[in] theArguments arguments of the attribute. + //! \param[out] theError error message. + virtual bool isValid(const AttributePtr& theAttribute, + const std::list& theArguments, + Events_InfoMessage& theError) const; +}; + +#endif \ No newline at end of file diff --git a/src/ConstructionPlugin/icons/point_by_lines_intersection_32x32.png b/src/ConstructionPlugin/icons/point_by_lines_intersection_32x32.png new file mode 100644 index 000000000..d51c6ff42 Binary files /dev/null and b/src/ConstructionPlugin/icons/point_by_lines_intersection_32x32.png differ diff --git a/src/ConstructionPlugin/icons/point_by_projection_32x32.png b/src/ConstructionPlugin/icons/point_by_projection_32x32.png index e5a5510ea..91900fe80 100644 Binary files a/src/ConstructionPlugin/icons/point_by_projection_32x32.png and b/src/ConstructionPlugin/icons/point_by_projection_32x32.png differ diff --git a/src/ConstructionPlugin/point_widget.xml b/src/ConstructionPlugin/point_widget.xml index 230aac45a..c489daaad 100644 --- a/src/ConstructionPlugin/point_widget.xml +++ b/src/ConstructionPlugin/point_widget.xml @@ -67,5 +67,26 @@ + + + + + + + + + + diff --git a/src/GeomAPI/GeomAPI_Lin.cpp b/src/GeomAPI/GeomAPI_Lin.cpp index a9fe074d0..b76131aa3 100644 --- a/src/GeomAPI/GeomAPI_Lin.cpp +++ b/src/GeomAPI/GeomAPI_Lin.cpp @@ -105,3 +105,16 @@ const std::shared_ptr GeomAPI_Lin::project( return std::shared_ptr(new GeomAPI_Pnt(aResult.X(), aResult.Y(), aResult.Z())); } +bool GeomAPI_Lin::isParallel(const std::shared_ptr theLin) const +{ + return MY_LIN->Direction().IsParallel(theLin->impl().Direction(), Precision::Confusion()) == Standard_True; +} + +bool GeomAPI_Lin::isCoplanar(const std::shared_ptr theLin) const +{ + if(MY_LIN->SquareDistance(theLin->impl()) > Precision::Confusion()) { + return false; + } + + return true; +} diff --git a/src/GeomAPI/GeomAPI_Lin.h b/src/GeomAPI/GeomAPI_Lin.h index 6f6710984..c62215bbc 100644 --- a/src/GeomAPI/GeomAPI_Lin.h +++ b/src/GeomAPI/GeomAPI_Lin.h @@ -53,6 +53,14 @@ class GeomAPI_Lin : public GeomAPI_Interface GEOMAPI_EXPORT const std::shared_ptr project( const std::shared_ptr& thePoint) const; + + /// \return true if lines are parallel. + GEOMAPI_EXPORT + bool isParallel(const std::shared_ptr theLin) const; + + /// \return true if lines are coplanar. + GEOMAPI_EXPORT + bool isCoplanar(const std::shared_ptr theLin) const; }; #endif diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_PointBuilder.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_PointBuilder.cpp index b5db2869b..1c7e51bb7 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_PointBuilder.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_PointBuilder.cpp @@ -148,32 +148,16 @@ std::shared_ptr GeomAlgoAPI_PointBuilder::vertexByIntersection( return aVertex; } - gp_Lin aLin1 = theEdge1->line()->impl(); - gp_Lin aLin2 = theEdge2->line()->impl(); + std::shared_ptr aLin1 = theEdge1->line(); + std::shared_ptr aLin2 = theEdge2->line(); - if(aLin1.Distance(aLin2) > Precision::Confusion()) { - return aVertex; - } - - Handle(Geom_Line) aLine1 = new Geom_Line(aLin1); - Handle(Geom_Line) aLine2 = new Geom_Line(aLin2); - - GeomAPI_ExtremaCurveCurve anExtrema(aLine1, aLine2); + std::shared_ptr aPnt = aLin1->intersect(aLin2); - Standard_Integer aNbExtrema = anExtrema.NbExtrema(); - - if(aNbExtrema == 0) { + if(!aPnt.get()) { return aVertex; } - gp_Pnt aPnt1, aPnt2; - for(Standard_Integer anIndex = 1; anIndex <= aNbExtrema; ++anIndex) { - if(anExtrema.Distance(anIndex) <= Precision::Confusion()) { - anExtrema.Points(anIndex, aPnt1, aPnt2); - } - } - - aVertex.reset(new GeomAPI_Vertex(aPnt1.X(), aPnt1.Y(), aPnt1.Z())); + aVertex.reset(new GeomAPI_Vertex(aPnt->x(), aPnt->y(), aPnt->z())); return aVertex; }