From 8d3f5dacb45470ba28570f19b956ddedc6213346 Mon Sep 17 00:00:00 2001 From: nds Date: Thu, 5 Jun 2014 15:37:24 +0400 Subject: [PATCH] refs #80 - Sketch base GUI: create/draw point, circle and arc Move all line specific information to FeatureLinePrs. Prepare FeaturePointPrs for the point feature. --- src/PartSet/CMakeLists.txt | 4 + src/PartSet/PartSet_FeatureLinePrs.cpp | 118 ++++++++++++ src/PartSet/PartSet_FeatureLinePrs.h | 57 ++++++ src/PartSet/PartSet_FeaturePointPrs.cpp | 31 +++ src/PartSet/PartSet_FeaturePointPrs.h | 30 +++ src/PartSet/PartSet_FeaturePrs.cpp | 180 ++---------------- src/PartSet/PartSet_FeaturePrs.h | 58 ++---- src/PartSet/PartSet_OperationConstraint.cpp | 8 +- .../PartSet_OperationCreateFeature.cpp | 18 +- src/PartSet/PartSet_TestOCC.cpp | 13 +- src/PartSet/PartSet_Tools.cpp | 83 ++++++++ src/PartSet/PartSet_Tools.h | 44 ++++- 12 files changed, 413 insertions(+), 231 deletions(-) create mode 100644 src/PartSet/PartSet_FeatureLinePrs.cpp create mode 100644 src/PartSet/PartSet_FeatureLinePrs.h create mode 100644 src/PartSet/PartSet_FeaturePointPrs.cpp create mode 100644 src/PartSet/PartSet_FeaturePointPrs.h diff --git a/src/PartSet/CMakeLists.txt b/src/PartSet/CMakeLists.txt index fe69c3399..f8629de87 100644 --- a/src/PartSet/CMakeLists.txt +++ b/src/PartSet/CMakeLists.txt @@ -6,6 +6,8 @@ SET(PROJECT_HEADERS PartSet.h PartSet_Constants.h PartSet_FeaturePrs.h + PartSet_FeatureLinePrs.h + PartSet_FeaturePointPrs.h PartSet_Listener.h PartSet_Module.h PartSet_OperationConstraint.h @@ -20,6 +22,8 @@ SET(PROJECT_HEADERS SET(PROJECT_SOURCES PartSet_FeaturePrs.cpp + PartSet_FeatureLinePrs.cpp + PartSet_FeaturePointPrs.cpp PartSet_Listener.cpp PartSet_Module.cpp PartSet_OperationConstraint.cpp diff --git a/src/PartSet/PartSet_FeatureLinePrs.cpp b/src/PartSet/PartSet_FeatureLinePrs.cpp new file mode 100644 index 000000000..5e67aaeee --- /dev/null +++ b/src/PartSet/PartSet_FeatureLinePrs.cpp @@ -0,0 +1,118 @@ +// File: PartSet_FeaturePrs.h +// Created: 04 Jun 2014 +// Author: Natalia ERMOLAEVA + +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +using namespace std; + +PartSet_FeatureLinePrs::PartSet_FeatureLinePrs(FeaturePtr theSketch) +: PartSet_FeaturePrs(theSketch) +{ +} + +void PartSet_FeatureLinePrs::initFeature(FeaturePtr theFeature) +{ + if (feature() && theFeature) + { + // use the last point of the previous feature as the first of the new one + boost::shared_ptr aData = theFeature->data(); + boost::shared_ptr anInitPoint = boost::dynamic_pointer_cast + (aData->attribute(LINE_ATTR_END)); + PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_START); + PartSet_Tools::setFeaturePoint(feature(), anInitPoint->x(), anInitPoint->y(), LINE_ATTR_END); + + aData = feature()->data(); + boost::shared_ptr aPoint = boost::dynamic_pointer_cast + (aData->attribute(LINE_ATTR_START)); + PartSet_Tools::createConstraint(sketch(), anInitPoint, aPoint); + } +} + +PartSet_SelectionMode PartSet_FeatureLinePrs::setPoint(double theX, double theY, + const PartSet_SelectionMode& theMode) +{ + PartSet_SelectionMode aMode = theMode; + switch (theMode) + { + case SM_FirstPoint: { + PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_START); + PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END); + aMode = SM_SecondPoint; + } + break; + case SM_SecondPoint: { + PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END); + aMode = SM_DonePoint; + } + break; + default: + break; + } + return aMode; +} + +std::string PartSet_FeatureLinePrs::getAttribute(const PartSet_SelectionMode& theMode) const +{ + std::string aAttribute; + switch (theMode) + { + case SM_FirstPoint: + aAttribute = LINE_ATTR_START; + break; + case SM_SecondPoint: + aAttribute = LINE_ATTR_END; + break; + default: + break; + } + return aAttribute; +} + +PartSet_SelectionMode PartSet_FeatureLinePrs::getNextMode(const std::string& theAttribute) const +{ + PartSet_SelectionMode aMode; + + if (theAttribute == LINE_ATTR_START) + aMode = SM_SecondPoint; + else if (theAttribute == LINE_ATTR_END) + aMode = SM_DonePoint; + return aMode; +} + +boost::shared_ptr PartSet_FeatureLinePrs::featurePoint + (const PartSet_SelectionMode& theMode) +{ + std::string aPointArg; + switch (theMode) + { + case SM_FirstPoint: + aPointArg = LINE_ATTR_START; + break; + case SM_SecondPoint: + aPointArg = LINE_ATTR_END; + break; + default: + break; + } + boost::shared_ptr aData = feature()->data(); + boost::shared_ptr aPoint = boost::dynamic_pointer_cast + (aData->attribute(aPointArg)); + return aPoint; +} diff --git a/src/PartSet/PartSet_FeatureLinePrs.h b/src/PartSet/PartSet_FeatureLinePrs.h new file mode 100644 index 000000000..a2ff5bba0 --- /dev/null +++ b/src/PartSet/PartSet_FeatureLinePrs.h @@ -0,0 +1,57 @@ +// File: PartSet_FeatureLinePrs.h +// Created: 04 Jun 2014 +// Author: Natalia ERMOLAEVA + +#ifndef PartSet_FeatureLinePrs_H +#define PartSet_FeatureLinePrs_H + +#include "PartSet.h" + +#include "PartSet_FeaturePrs.h" +#include "PartSet_Constants.h" + +class GeomDataAPI_Point2D; + +/*! + \class PartSet_FeatureLinePrs + * \brief The abstract class to define the specific feature manipulation. It is created for + * the feature create operation to move out the feature properties set and use one operation + * for any type of features. +*/ +class PARTSET_EXPORT PartSet_FeatureLinePrs : public PartSet_FeaturePrs +{ +public: + /// Constructor + /// \param theSketch the sketch feature + PartSet_FeatureLinePrs(FeaturePtr theSketch); + /// Destructor + virtual ~PartSet_FeatureLinePrs() {}; + + /// Sets the point to the feature in an attribute depending on the selection mode + /// \param theX the 2D point horizontal coordinate + /// \param theY the 2D point vertical coordinate + /// \param theMode the selection mode + /// \return the new selection mode + virtual PartSet_SelectionMode setPoint(double theX, double theY, + const PartSet_SelectionMode& theMode); + + /// Returns the feature attribute name for the selection mode + /// \param theMode the current operation selection mode. The feature attribute depends on the mode + virtual std::string getAttribute(const PartSet_SelectionMode& theMode) const; + + /// Returns the next selection mode after the attribute + /// \param theAttribute the feature attribute name + /// \return next attribute selection mode + virtual PartSet_SelectionMode getNextMode(const std::string& theAttribute) const; + +protected: + /// Initializes current feature by the given + /// \param theSourceFeature the feature, which attributes are used to initialize the current feature + virtual void initFeature(FeaturePtr theSourceFeature); + + /// Returns the feature point in the selection mode position. + /// \param theMode the current operation selection mode. The feature attribute depends on the mode + virtual boost::shared_ptr featurePoint(const PartSet_SelectionMode& theMode); +}; + +#endif diff --git a/src/PartSet/PartSet_FeaturePointPrs.cpp b/src/PartSet/PartSet_FeaturePointPrs.cpp new file mode 100644 index 000000000..18db26a09 --- /dev/null +++ b/src/PartSet/PartSet_FeaturePointPrs.cpp @@ -0,0 +1,31 @@ +// File: PartSet_FeaturePointPrs.h +// Created: 04 Jun 2014 +// Author: Natalia ERMOLAEVA + +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +#include + +using namespace std; + +PartSet_FeaturePointPrs::PartSet_FeaturePointPrs(FeaturePtr theFeature) +{ +} + +PartSet_FeaturePointPrs::~PartSet_FeaturePointPrs() +{ +} diff --git a/src/PartSet/PartSet_FeaturePointPrs.h b/src/PartSet/PartSet_FeaturePointPrs.h new file mode 100644 index 000000000..b43de80ed --- /dev/null +++ b/src/PartSet/PartSet_FeaturePointPrs.h @@ -0,0 +1,30 @@ +// File: PartSet_FeaturePointPrs.h +// Created: 04 Jun 2014 +// Author: Natalia ERMOLAEVA + +#ifndef PartSet_FeaturePointPrs_H +#define PartSet_FeaturePointPrs_H + +#include "PartSet.h" + +#include "PartSet_Constants.h" + +class GeomDataAPI_Point2D; + +/*! + \class PartSet_FeaturePointPrs + * \brief The abstract class to define the specific feature manipulation. It is created for + * the feature create operation to move out the feature properties set and use one operation + * for any type of features. +*/ +class PARTSET_EXPORT PartSet_FeaturePointPrs +{ +public: + /// Constructor + /// \param theSketch the sketch feature + PartSet_FeaturePointPrs(FeaturePtr theSketch); + /// Destructor + virtual ~PartSet_FeaturePointPrs(); +}; + +#endif diff --git a/src/PartSet/PartSet_FeaturePrs.cpp b/src/PartSet/PartSet_FeaturePrs.cpp index 434dd94e4..4712baf3a 100644 --- a/src/PartSet/PartSet_FeaturePrs.cpp +++ b/src/PartSet/PartSet_FeaturePrs.cpp @@ -3,12 +3,10 @@ // Author: Natalia ERMOLAEVA #include +#include #include #include -#include -#include -#include #include @@ -33,192 +31,40 @@ PartSet_FeaturePrs::~PartSet_FeaturePrs() void PartSet_FeaturePrs::init(FeaturePtr theFeature, FeaturePtr theSourceFeature) { myFeature = theFeature; - if (theSourceFeature) - { - // use the last point of the previous feature as the first of the new one - boost::shared_ptr aData = theSourceFeature->data(); - boost::shared_ptr anInitPoint = boost::dynamic_pointer_cast - (aData->attribute(LINE_ATTR_END)); - setLinePoint(theFeature, anInitPoint->x(), anInitPoint->y(), LINE_ATTR_START); - setLinePoint(theFeature, anInitPoint->x(), anInitPoint->y(), LINE_ATTR_END); - - aData = theFeature->data(); - boost::shared_ptr aPoint = boost::dynamic_pointer_cast - (aData->attribute(LINE_ATTR_START)); - createConstraint(anInitPoint, aPoint); + if (theSourceFeature) { + initFeature(theSourceFeature); } } -boost::shared_ptr PartSet_FeaturePrs::document() const -{ - return ModelAPI_PluginManager::get()->rootDocument(); -} - FeaturePtr PartSet_FeaturePrs::sketch() const { return mySketch; } -PartSet_SelectionMode PartSet_FeaturePrs::setPoint(double theX, double theY, - const PartSet_SelectionMode& theMode) -{ - PartSet_SelectionMode aMode = theMode; - switch (theMode) - { - case SM_FirstPoint: { - setLinePoint(feature(), theX, theY, LINE_ATTR_START); - setLinePoint(feature(), theX, theY, LINE_ATTR_END); - aMode = SM_SecondPoint; - } - break; - case SM_SecondPoint: { - setLinePoint(feature(), theX, theY, LINE_ATTR_END); - aMode = SM_DonePoint; - } - break; - default: - break; - } - return aMode; -} - FeaturePtr PartSet_FeaturePrs::feature() const { return myFeature; } -void PartSet_FeaturePrs::createConstraint(boost::shared_ptr thePoint1, - boost::shared_ptr thePoint2) -{ - boost::shared_ptr aDoc = document(); - FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND); - - if (sketch()) { - boost::shared_ptr aSketch = - boost::dynamic_pointer_cast(sketch()); - aSketch->addSub(aFeature); - } - - boost::shared_ptr aData = aFeature->data(); - - boost::shared_ptr aRef1 = - boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_ENTITY_A)); - aRef1->setAttr(thePoint1); - - boost::shared_ptr aRef2 = - boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_ENTITY_B)); - aRef2->setAttr(thePoint2); - - if (aFeature) // TODO: generate an error if feature was not created - aFeature->execute(); -} - void PartSet_FeaturePrs::setConstraints(double theX, double theY, const PartSet_SelectionMode& theMode) { - std::string aPointArg; - switch (theMode) - { - case SM_FirstPoint: - aPointArg = LINE_ATTR_START; - break; - case SM_SecondPoint: - aPointArg = LINE_ATTR_END; - break; - default: - break; - } + // find a feature point by the selection mode + boost::shared_ptr aPoint = featurePoint(theMode); - FeaturePtr aSkFeature = feature(); - - boost::shared_ptr aData = feature()->data(); - boost::shared_ptr aPoint = boost::dynamic_pointer_cast - (aData->attribute(aPointArg)); - aData = sketch()->data(); + // get all sketch features. If the point with the given coordinates belong to any sketch feature, + // the constraint is created between the feature point and the found sketch point + boost::shared_ptr aData = sketch()->data(); boost::shared_ptr aRefList = boost::dynamic_pointer_cast(aData->attribute(SKETCH_ATTR_FEATURES)); std::list aFeatures = aRefList->list(); - std::list::const_iterator anIt = aFeatures.begin(), - aLast = aFeatures.end(); - for (; anIt != aLast; anIt++) { + std::list::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end(); + for (; anIt != aLast; anIt++) + { FeaturePtr aFeature = *anIt; - boost::shared_ptr aFPoint = findLinePoint(aFeature, theX, theY); + boost::shared_ptr aFPoint = PartSet_Tools::findPoint(aFeature, theX, theY); if (aFPoint) - createConstraint(aFPoint, aPoint); + PartSet_Tools::createConstraint(sketch(), aFPoint, aPoint); } } - -std::string PartSet_FeaturePrs::getAttribute(const PartSet_SelectionMode& theMode) const -{ - std::string aAttribute; - switch (theMode) - { - case SM_FirstPoint: - aAttribute = LINE_ATTR_START; - break; - case SM_SecondPoint: - aAttribute = LINE_ATTR_END; - break; - default: - break; - } - return aAttribute; -} - -PartSet_SelectionMode PartSet_FeaturePrs::getNextMode(const std::string& theAttribute) const -{ - PartSet_SelectionMode aMode; - - if (theAttribute == LINE_ATTR_START) - aMode = SM_SecondPoint; - else if (theAttribute == LINE_ATTR_END) - aMode = SM_DonePoint; - return aMode; -} - -void PartSet_FeaturePrs::getLinePoint(FeaturePtr theFeature, - const std::string& theAttribute, - double& theX, double& theY) -{ - if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND) - return; - boost::shared_ptr aData = theFeature->data(); - boost::shared_ptr aPoint = - boost::dynamic_pointer_cast(aData->attribute(theAttribute)); - theX = aPoint->x(); - theY = aPoint->y(); -} - -boost::shared_ptr PartSet_FeaturePrs::findLinePoint( - FeaturePtr theFeature, - double theX, double theY) -{ - boost::shared_ptr aPoint2D; - if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND) - return aPoint2D; - boost::shared_ptr aData = theFeature->data(); - - boost::shared_ptr aPoint = - boost::dynamic_pointer_cast(aData->attribute(LINE_ATTR_START)); - if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() ) - aPoint2D = aPoint; - else { - aPoint = boost::dynamic_pointer_cast(aData->attribute(LINE_ATTR_END)); - if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() ) - aPoint2D = aPoint; - } - return aPoint2D; -} - -void PartSet_FeaturePrs::setLinePoint(FeaturePtr theFeature, - double theX, double theY, - const std::string& theAttribute) -{ - if (!theFeature) - return; - boost::shared_ptr aData = theFeature->data(); - boost::shared_ptr aPoint = - boost::dynamic_pointer_cast(aData->attribute(theAttribute)); - aPoint->setValue(theX, theY); -} diff --git a/src/PartSet/PartSet_FeaturePrs.h b/src/PartSet/PartSet_FeaturePrs.h index 0a285f522..4a602d224 100644 --- a/src/PartSet/PartSet_FeaturePrs.h +++ b/src/PartSet/PartSet_FeaturePrs.h @@ -30,7 +30,7 @@ public: /// Saves the fiature as the presentation internal feature /// \param theFeature the presentation feature /// \param theSourceFeature the feature, which attributes are used to initialize the feature - virtual void init(FeaturePtr theFeature, FeaturePtr theSourceFeature); + void init(FeaturePtr theFeature, FeaturePtr theSourceFeature); /// Returns the operation sketch feature /// \returns the sketch instance @@ -41,58 +41,36 @@ public: /// \param theY the 2D point vertical coordinate /// \param theMode the selection mode /// \return the new selection mode - PartSet_SelectionMode setPoint(double theX, double theY, const PartSet_SelectionMode& theMode); - - /// Creates constrains of the current - /// \param theX the horizontal coordnate of the point - /// \param theY the vertical coordnate of the point - /// \param theMode the current operation selection mode. The feature attribute depends on the mode - void setConstraints(double theX, double theY, const PartSet_SelectionMode& theMode); + virtual PartSet_SelectionMode setPoint(double theX, double theY, + const PartSet_SelectionMode& theMode) = 0; /// Returns the feature attribute name for the selection mode /// \param theMode the current operation selection mode. The feature attribute depends on the mode - std::string getAttribute(const PartSet_SelectionMode& theMode) const; + virtual std::string getAttribute(const PartSet_SelectionMode& theMode) const = 0; /// Returns the next selection mode after the attribute /// \param theAttribute the feature attribute name /// \return next attribute selection mode - PartSet_SelectionMode getNextMode(const std::string& theAttribute) const; + virtual PartSet_SelectionMode getNextMode(const std::string& theAttribute) const = 0; - /// \brief Save the point to the line. - /// \param theFeature the line feature - /// \param theX the horizontal coordinate - /// \param theY the vertical coordinate - /// \param theAttribute the start or end attribute of the line - static void setLinePoint(FeaturePtr, double theX, double theY, - const std::string& theAttribute); + /// Creates constrains of the current + /// \param theX the horizontal coordnate of the point + /// \param theY the vertical coordnate of the point + /// \param theMode the current operation selection mode. The feature attribute depends on the mode + void setConstraints(double theX, double theY, const PartSet_SelectionMode& theMode); protected: - /// Returns pointer to the root document. - boost::shared_ptr document() const; - - /// Returns the operation feature + /// Returns the operation feature /// \return the feature FeaturePtr feature() const; - /// Creates a constraint on two points - /// \param thePoint1 the first point - /// \param thePoint1 the second point - void createConstraint(boost::shared_ptr thePoint1, - boost::shared_ptr thePoint2); - - /// \brief Get the line point 2d coordinates. - /// \param theFeature the line feature - /// \param theAttribute the start or end attribute of the line - /// \param theX the horizontal coordinate - /// \param theY the vertical coordinate - void getLinePoint(FeaturePtr theFeature, const std::string& theAttribute, - double& theX, double& theY); - /// Find a point in the line with given coordinates - /// \param theFeature the line feature - /// \param theX the horizontal point coordinate - /// \param theY the vertical point coordinate - boost::shared_ptr findLinePoint(FeaturePtr theFeature, - double theX, double theY); + /// Initializes current feature by the given + /// \param theSourceFeature the feature, which attributes are used to initialize the current feature + virtual void initFeature(FeaturePtr theSourceFeature) = 0; + + /// Returns the feature point in the selection mode position. + /// \param theMode the current operation selection mode. The feature attribute depends on the mode + virtual boost::shared_ptr featurePoint(const PartSet_SelectionMode& theMode) = 0; private: FeaturePtr mySketch; ///< the sketch of the feature diff --git a/src/PartSet/PartSet_OperationConstraint.cpp b/src/PartSet/PartSet_OperationConstraint.cpp index 6c65c0eac..447f7a4d3 100644 --- a/src/PartSet/PartSet_OperationConstraint.cpp +++ b/src/PartSet/PartSet_OperationConstraint.cpp @@ -124,8 +124,8 @@ void PartSet_OperationConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3 if (aFeature) { double X0, X1, X2, X3; double Y0, Y1, Y2, Y3; - getLinePoint(aFeature, LINE_ATTR_START, X2, Y2); - getLinePoint(aFeature, LINE_ATTR_END, X3, Y3); + PartSet_Tools::getLinePoint(aFeature, LINE_ATTR_START, X2, Y2); + PartSet_Tools::getLinePoint(aFeature, LINE_ATTR_END, X3, Y3); PartSet_Tools::convertTo2D(aPoint, sketch(), theView, X1, Y1); switch (myPointSelectionMode) { @@ -133,7 +133,7 @@ void PartSet_OperationConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3 PartSet_Tools::projectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY); break; case SM_SecondPoint: { - getLinePoint(feature(), LINE_ATTR_START, X0, Y0); + PartSet_Tools::getLinePoint(feature(), LINE_ATTR_START, X0, Y0); PartSet_Tools::intersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY); } break; @@ -242,7 +242,7 @@ FeaturePtr PartSet_OperationConstraint::createFeature(const bool theFlushMessage boost::shared_ptr aData = aNewFeature->data(); boost::shared_ptr aPoint = boost::dynamic_pointer_cast (aData->attribute(LINE_ATTR_START)); - createConstraint(myInitPoint, aPoint); + PartSet_Tools::createConstraint(myInitPoint, aPoint); }*/ emit featureConstructed(aNewFeature, FM_Activation); diff --git a/src/PartSet/PartSet_OperationCreateFeature.cpp b/src/PartSet/PartSet_OperationCreateFeature.cpp index 1d9044592..60a5ded04 100644 --- a/src/PartSet/PartSet_OperationCreateFeature.cpp +++ b/src/PartSet/PartSet_OperationCreateFeature.cpp @@ -6,7 +6,7 @@ #include #include -#include +#include #include @@ -34,7 +34,7 @@ PartSet_OperationCreateFeature::PartSet_OperationCreateFeature(const QString& th : PartSet_OperationSketchBase(theId, theParent), myPointSelectionMode(SM_FirstPoint) { - myFeaturePrs = new PartSet_FeaturePrs(theFeature); + myFeaturePrs = new PartSet_FeatureLinePrs(theFeature); } PartSet_OperationCreateFeature::~PartSet_OperationCreateFeature() @@ -110,14 +110,17 @@ void PartSet_OperationCreateFeature::mouseReleased(QMouseEvent* theEvent, Handle myFeaturePrs->setConstraints(aX, anY, myPointSelectionMode); } } - /*else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected + else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected { + PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY); + isFoundPoint = true; + /* FeaturePtr aFeature = aPrs.feature(); if (aFeature) { double X0, X1, X2, X3; double Y0, Y1, Y2, Y3; - getLinePoint(aFeature, LINE_ATTR_START, X2, Y2); - getLinePoint(aFeature, LINE_ATTR_END, X3, Y3); + PartSet_Tools::getLinePoint(aFeature, LINE_ATTR_START, X2, Y2); + PartSet_Tools::getLinePoint(aFeature, LINE_ATTR_END, X3, Y3); PartSet_Tools::convertTo2D(aPoint, sketch(), theView, X1, Y1); switch (myPointSelectionMode) { @@ -125,7 +128,7 @@ void PartSet_OperationCreateFeature::mouseReleased(QMouseEvent* theEvent, Handle PartSet_Tools::projectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY); break; case SM_SecondPoint: { - getLinePoint(feature(), LINE_ATTR_START, X0, Y0); + PartSet_Tools::getLinePoint(feature(), LINE_ATTR_START, X0, Y0); PartSet_Tools::intersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY); } break; @@ -134,7 +137,8 @@ void PartSet_OperationCreateFeature::mouseReleased(QMouseEvent* theEvent, Handle } isFoundPoint = true; } - }*/ + */ + } } } diff --git a/src/PartSet/PartSet_TestOCC.cpp b/src/PartSet/PartSet_TestOCC.cpp index ef0ea9a75..9de469570 100644 --- a/src/PartSet/PartSet_TestOCC.cpp +++ b/src/PartSet/PartSet_TestOCC.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -141,8 +142,8 @@ void PartSet_TestOCC::createTestLine(XGUI_Workshop* theWorkshop) boost::dynamic_pointer_cast(aPreviewOp->sketch()); aSketch->addSub(aFeature); - PartSet_FeaturePrs::setLinePoint(aFeature, 100, 100, LINE_ATTR_START); - PartSet_FeaturePrs::setLinePoint(aFeature, 150, 300, LINE_ATTR_END); + PartSet_Tools::setFeaturePoint(aFeature, 100, 100, LINE_ATTR_START); + PartSet_Tools::setFeaturePoint(aFeature, 150, 300, LINE_ATTR_END); boost::shared_ptr aPreview = PartSet_OperationSketchBase::preview(aFeature); @@ -163,8 +164,8 @@ void PartSet_TestOCC::createTestLine(XGUI_Workshop* theWorkshop) /*double aDelta = -200; for (int i = 0; i < 20; i++) { aDelta = aDelta - i*2; - PartSet_FeaturePrs::setLinePoint(aFeature, 100+aDelta, 200+aDelta, LINE_ATTR_START); - PartSet_FeaturePrs::setLinePoint(aFeature, 300+aDelta, 500+aDelta, LINE_ATTR_END); + PartSet_Tools::setFeaturePoint(aFeature, 100+aDelta, 200+aDelta, LINE_ATTR_START); + PartSet_Tools::setFeaturePoint(aFeature, 300+aDelta, 500+aDelta, LINE_ATTR_END); boost::shared_ptr aPreview = PartSet_OperationSketchBase::preview(aFeature); Handle(AIS_InteractiveObject) anAIS = PartSet_Presentation::createPresentation( @@ -199,8 +200,8 @@ void PartSet_TestOCC::changeTestLine(XGUI_Workshop* theWorkshop) myTestDelta = myTestDelta - 50; double aDelta = myTestDelta; - PartSet_FeaturePrs::setLinePoint(aFeature, -100/*aDelta*/, -100/*aDelta*/, LINE_ATTR_START); - PartSet_FeaturePrs::setLinePoint(aFeature, 200/*aDelta*2*/, 200/*aDelta*2*/, LINE_ATTR_END); + PartSet_Tools::setFeaturePoint(aFeature, -100/*aDelta*/, -100/*aDelta*/, LINE_ATTR_START); + PartSet_Tools::setFeaturePoint(aFeature, 200/*aDelta*2*/, 200/*aDelta*2*/, LINE_ATTR_END); boost::shared_ptr aPreview = PartSet_OperationSketchBase::preview(aFeature); Handle(AIS_InteractiveObject) aPrevAIS; diff --git a/src/PartSet/PartSet_Tools.cpp b/src/PartSet/PartSet_Tools.cpp index ae51d7b20..265a3b26b 100644 --- a/src/PartSet/PartSet_Tools.cpp +++ b/src/PartSet/PartSet_Tools.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -14,8 +15,11 @@ #include #include +#include #include #include +#include +#include #include @@ -216,3 +220,82 @@ double PartSet_Tools::distanceToPoint(FeaturePtr theFeature, return aDelta; } + +boost::shared_ptr PartSet_Tools::document() +{ + return ModelAPI_PluginManager::get()->rootDocument(); +} + +void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double theY, + const std::string& theAttribute) +{ + if (!theFeature) + return; + boost::shared_ptr aData = theFeature->data(); + boost::shared_ptr aPoint = + boost::dynamic_pointer_cast(aData->attribute(theAttribute)); + if (aPoint) + aPoint->setValue(theX, theY); +} + +void PartSet_Tools::createConstraint(FeaturePtr theSketch, + boost::shared_ptr thePoint1, + boost::shared_ptr thePoint2) +{ + boost::shared_ptr aDoc = document(); + FeaturePtr aFeature = aDoc->addFeature(SKETCH_CONSTRAINT_COINCIDENCE_KIND); + + if (theSketch) { + boost::shared_ptr aSketch = + boost::dynamic_pointer_cast(theSketch); + aSketch->addSub(aFeature); + } + + boost::shared_ptr aData = aFeature->data(); + + boost::shared_ptr aRef1 = + boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_ENTITY_A)); + aRef1->setAttr(thePoint1); + + boost::shared_ptr aRef2 = + boost::dynamic_pointer_cast(aData->attribute(CONSTRAINT_ATTR_ENTITY_B)); + aRef2->setAttr(thePoint2); + + if (aFeature) // TODO: generate an error if feature was not created + aFeature->execute(); +} + +void PartSet_Tools::getLinePoint(FeaturePtr theFeature, const std::string& theAttribute, + double& theX, double& theY) +{ + if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND) + return; + boost::shared_ptr aData = theFeature->data(); + boost::shared_ptr aPoint = + boost::dynamic_pointer_cast(aData->attribute(theAttribute)); + theX = aPoint->x(); + theY = aPoint->y(); +} + +boost::shared_ptr PartSet_Tools::findPoint(FeaturePtr theFeature, + double theX, double theY) +{ + boost::shared_ptr aPoint2D; + if (!theFeature) + return aPoint2D; + + boost::shared_ptr aData = theFeature->data(); + if (theFeature->getKind() == SKETCH_LINE_KIND) + { + boost::shared_ptr aPoint = + boost::dynamic_pointer_cast(aData->attribute(LINE_ATTR_START)); + if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() ) + aPoint2D = aPoint; + else { + aPoint = boost::dynamic_pointer_cast(aData->attribute(LINE_ATTR_END)); + if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() ) + aPoint2D = aPoint; + } + } + return aPoint2D; +} diff --git a/src/PartSet/PartSet_Tools.h b/src/PartSet/PartSet_Tools.h index c8fa30660..ac55abbb2 100644 --- a/src/PartSet/PartSet_Tools.h +++ b/src/PartSet/PartSet_Tools.h @@ -19,6 +19,7 @@ class Handle_V3d_View; class XGUI_ViewerPrs; +class GeomDataAPI_Point2D; /*! \class PartSet_Tools @@ -45,8 +46,7 @@ public: /// \param theY the Y coordinate /// \param theSketch the sketch feature /// \param thePoint the 3D point in the viewer - static void convertTo3D(const double theX, const double theY, - FeaturePtr theSketch, + static void convertTo3D(const double theX, const double theY, FeaturePtr theSketch, gp_Pnt& thePoint); /// Returns the point of intersection of the two lines, the first is (v0, v1), the second is (v2, v3), @@ -80,16 +80,46 @@ public: /// \param theView a 3D view /// \param theSketch the sketch feature /// \param theFeatures the list of selected presentations - static FeaturePtr nearestFeature(QPoint thePoint, Handle_V3d_View theView, - FeaturePtr theSketch, - const std::list& theFeatures); + static FeaturePtr nearestFeature(QPoint thePoint, Handle_V3d_View theView, FeaturePtr theSketch, + const std::list& theFeatures); + + /// Returns pointer to the root document. + static boost::shared_ptr document(); + + /// \brief Save the point to the feature. If the attribute is 2D geometry point, it is filled. + /// \param theFeature the feature + /// \param theX the horizontal coordinate + /// \param theY the vertical coordinate + /// \param theAttribute the feature attribute + static void setFeaturePoint(FeaturePtr, double theX, double theY, const std::string& theAttribute); + + /// Creates a constraint on two points + /// \param thePoint1 the first point + /// \param thePoint1 the second point + static void createConstraint(FeaturePtr theSketch, + boost::shared_ptr thePoint1, + boost::shared_ptr thePoint2); + + /// \brief Get the line point 2d coordinates. + /// \param theFeature the line feature + /// \param theAttribute the start or end attribute of the line + /// \param theX the horizontal coordinate + /// \param theY the vertical coordinate + static void getLinePoint(FeaturePtr theFeature, const std::string& theAttribute, + double& theX, double& theY); + /// Find a point in the line with given coordinates + /// \param theFeature the line feature + /// \param theX the horizontal point coordinate + /// \param theY the vertical point coordinate + static boost::shared_ptr findPoint(FeaturePtr theFeature, double theX, + double theY); + private: /// Return the distance between the feature and the point /// \param theFeature feature object /// \param theX the horizontal coordinate of the point /// \param theX the vertical coordinate of the point - static double distanceToPoint(FeaturePtr theFeature, - double theX, double theY); + static double distanceToPoint(FeaturePtr theFeature, double theX, double theY); }; #endif -- 2.39.2