From c2b387f9978e4a306ad51d2e7f8e417416898f66 Mon Sep 17 00:00:00 2001 From: nds Date: Fri, 6 Jun 2014 16:13:41 +0400 Subject: [PATCH] refs #80 - Sketch base GUI: create/draw point, circle and arc Circle final creation. --- src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.cpp | 19 ++++++++ src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.h | 6 +++ src/PartSet/PartSet_FeatureCirclePrs.cpp | 49 +++++++-------------- src/PartSet/PartSet_FeatureCirclePrs.h | 4 -- src/PartSet/PartSet_FeaturePointPrs.cpp | 4 -- src/PartSet/PartSet_FeaturePointPrs.h | 4 -- src/PartSet/PartSet_FeaturePrs.h | 2 +- src/PartSet/PartSet_Tools.cpp | 13 ++++++ src/PartSet/PartSet_Tools.h | 11 ++++- src/SketchPlugin/SketchPlugin_Circle.cpp | 37 ++++++++++++++++ src/SketchPlugin/SketchPlugin_Point.cpp | 17 +++---- 11 files changed, 110 insertions(+), 56 deletions(-) diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.cpp index 2cc821e39..57afd7a69 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.cpp @@ -10,6 +10,9 @@ #include #include +#include +#include + boost::shared_ptr GeomAlgoAPI_EdgeBuilder::line( boost::shared_ptr theStart, boost::shared_ptr theEnd) { @@ -26,3 +29,19 @@ boost::shared_ptr GeomAlgoAPI_EdgeBuilder::line( aRes->setImpl(new TopoDS_Shape(anEdge)); return aRes; } + +boost::shared_ptr GeomAlgoAPI_EdgeBuilder::lineCircle( + boost::shared_ptr theCenter, + boost::shared_ptr theNormal, double theRadius) +{ + const gp_Pnt& aCenter = theCenter->impl(); + const gp_Dir& aDir = theNormal->impl(); + + gp_Circ aCircle(gp_Ax2(aCenter, aDir), theRadius); + + BRepBuilderAPI_MakeEdge anEdgeBuilder(aCircle); + boost::shared_ptr aRes(new GeomAPI_Shape); + TopoDS_Edge anEdge = anEdgeBuilder.Edge(); + aRes->setImpl(new TopoDS_Shape(anEdge)); + return aRes; +} diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.h b/src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.h index cd505a861..b7f527230 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_EdgeBuilder.h @@ -8,6 +8,7 @@ #include #include #include +#include #include /**\class GeomAlgoAPI_EdgeBuilder @@ -21,6 +22,11 @@ public: /// Creates linear edge by two points static boost::shared_ptr line( boost::shared_ptr theStart, boost::shared_ptr theEnd); + + /// Creates linear edge in a form of a circle by a point and a circle radius + static boost::shared_ptr lineCircle( + boost::shared_ptr theCenter, + boost::shared_ptr theNormal, double theRadius); }; #endif diff --git a/src/PartSet/PartSet_FeatureCirclePrs.cpp b/src/PartSet/PartSet_FeatureCirclePrs.cpp index 48342d9a6..d825fdd58 100644 --- a/src/PartSet/PartSet_FeatureCirclePrs.cpp +++ b/src/PartSet/PartSet_FeatureCirclePrs.cpp @@ -7,11 +7,10 @@ #include #include -#include -#include -#include +#include #include +#include #include #include @@ -27,38 +26,25 @@ PartSet_FeatureCirclePrs::PartSet_FeatureCirclePrs(FeaturePtr theSketch) { } -void PartSet_FeatureCirclePrs::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_FeatureCirclePrs::setPoint(double theX, double theY, - const PartSet_SelectionMode& theMode) + 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); + PartSet_Tools::setFeaturePoint(feature(), theX, theY, CIRCLE_ATTR_CENTER); aMode = SM_SecondPoint; } break; case SM_SecondPoint: { - PartSet_Tools::setFeaturePoint(feature(), theX, theY, LINE_ATTR_END); + boost::shared_ptr aData = feature()->data(); + boost::shared_ptr aPoint = boost::dynamic_pointer_cast + (aData->attribute(CIRCLE_ATTR_CENTER)); + boost::shared_ptr aCoordPoint(new GeomAPI_Pnt2d(theX, theY)); + double aRadius = aCoordPoint->distance(aPoint->pnt()); + PartSet_Tools::setFeatureValue(feature(), aRadius, CIRCLE_ATTR_RADIUS); + aMode = SM_DonePoint; } break; @@ -74,10 +60,10 @@ std::string PartSet_FeatureCirclePrs::getAttribute(const PartSet_SelectionMode& switch (theMode) { case SM_FirstPoint: - aAttribute = LINE_ATTR_START; + aAttribute = CIRCLE_ATTR_CENTER; break; case SM_SecondPoint: - aAttribute = LINE_ATTR_END; + aAttribute = CIRCLE_ATTR_RADIUS; break; default: break; @@ -89,9 +75,9 @@ PartSet_SelectionMode PartSet_FeatureCirclePrs::getNextMode(const std::string& t { PartSet_SelectionMode aMode; - if (theAttribute == LINE_ATTR_START) + if (theAttribute == CIRCLE_ATTR_CENTER) aMode = SM_SecondPoint; - else if (theAttribute == LINE_ATTR_END) + else if (theAttribute == CIRCLE_ATTR_RADIUS) aMode = SM_DonePoint; return aMode; } @@ -103,10 +89,7 @@ boost::shared_ptr PartSet_FeatureCirclePrs::featurePoint switch (theMode) { case SM_FirstPoint: - aPointArg = LINE_ATTR_START; - break; - case SM_SecondPoint: - aPointArg = LINE_ATTR_END; + aPointArg = CIRCLE_ATTR_CENTER; break; default: break; diff --git a/src/PartSet/PartSet_FeatureCirclePrs.h b/src/PartSet/PartSet_FeatureCirclePrs.h index 1e4e68000..ef4b8497d 100644 --- a/src/PartSet/PartSet_FeatureCirclePrs.h +++ b/src/PartSet/PartSet_FeatureCirclePrs.h @@ -45,10 +45,6 @@ public: 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); diff --git a/src/PartSet/PartSet_FeaturePointPrs.cpp b/src/PartSet/PartSet_FeaturePointPrs.cpp index ac695dada..ee6bc8140 100644 --- a/src/PartSet/PartSet_FeaturePointPrs.cpp +++ b/src/PartSet/PartSet_FeaturePointPrs.cpp @@ -25,10 +25,6 @@ PartSet_FeaturePointPrs::PartSet_FeaturePointPrs(FeaturePtr theSketch) { } -void PartSet_FeaturePointPrs::initFeature(FeaturePtr theFeature) -{ -} - PartSet_SelectionMode PartSet_FeaturePointPrs::setPoint(double theX, double theY, const PartSet_SelectionMode& theMode) { diff --git a/src/PartSet/PartSet_FeaturePointPrs.h b/src/PartSet/PartSet_FeaturePointPrs.h index d06b3c734..fa9f73deb 100644 --- a/src/PartSet/PartSet_FeaturePointPrs.h +++ b/src/PartSet/PartSet_FeaturePointPrs.h @@ -45,10 +45,6 @@ public: 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); diff --git a/src/PartSet/PartSet_FeaturePrs.h b/src/PartSet/PartSet_FeaturePrs.h index 4a602d224..bf965ed8d 100644 --- a/src/PartSet/PartSet_FeaturePrs.h +++ b/src/PartSet/PartSet_FeaturePrs.h @@ -66,7 +66,7 @@ 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) = 0; + 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 diff --git a/src/PartSet/PartSet_Tools.cpp b/src/PartSet/PartSet_Tools.cpp index 3e8c921b2..36dd3fd0c 100644 --- a/src/PartSet/PartSet_Tools.cpp +++ b/src/PartSet/PartSet_Tools.cpp @@ -239,6 +239,18 @@ void PartSet_Tools::setFeaturePoint(FeaturePtr theFeature, double theX, double t aPoint->setValue(theX, theY); } +void PartSet_Tools::setFeatureValue(FeaturePtr theFeature, double theValue, + const std::string& theAttribute) +{ + if (!theFeature) + return; + boost::shared_ptr aData = theFeature->data(); + boost::shared_ptr anAttribute = + boost::dynamic_pointer_cast(aData->attribute(theAttribute)); + if (anAttribute) + anAttribute->setValue(theValue); +} + void PartSet_Tools::createConstraint(FeaturePtr theSketch, boost::shared_ptr thePoint1, boost::shared_ptr thePoint2) @@ -305,5 +317,6 @@ boost::shared_ptr PartSet_Tools::findPoint(FeaturePtr theFe 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 ac55abbb2..5cb8e9c7f 100644 --- a/src/PartSet/PartSet_Tools.h +++ b/src/PartSet/PartSet_Tools.h @@ -91,9 +91,16 @@ public: /// \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); + static void setFeaturePoint(FeaturePtr theFeature, double theX, double theY, + const std::string& theAttribute); - /// Creates a constraint on two points + /// \brief Save the double to the feature. If the attribute is double, it is filled. + /// \param theFeature the feature + /// \param theValue the horizontal coordinate + /// \param theAttribute the feature attribute + static void setFeatureValue(FeaturePtr theFeature, double theX, 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, diff --git a/src/SketchPlugin/SketchPlugin_Circle.cpp b/src/SketchPlugin/SketchPlugin_Circle.cpp index a083b1845..af26e9230 100644 --- a/src/SketchPlugin/SketchPlugin_Circle.cpp +++ b/src/SketchPlugin/SketchPlugin_Circle.cpp @@ -6,6 +6,11 @@ #include "SketchPlugin_Sketch.h" #include #include +#include +#include +#include +#include + #include SketchPlugin_Circle::SketchPlugin_Circle() @@ -25,6 +30,38 @@ void SketchPlugin_Circle::execute() const boost::shared_ptr& SketchPlugin_Circle::preview() { + SketchPlugin_Sketch* aSketch = sketch(); + if (aSketch) { + // compute a circle point in 3D view + boost::shared_ptr aCenterAttr = + boost::dynamic_pointer_cast(data()->attribute(CIRCLE_ATTR_CENTER)); + boost::shared_ptr aCenter(aSketch->to3D(aCenterAttr->x(), aCenterAttr->y())); + + // compute the circle radius + boost::shared_ptr aRadiusAttr = + boost::dynamic_pointer_cast(data()->attribute(CIRCLE_ATTR_RADIUS)); + double aRadius = aRadiusAttr->value(); + + std::list > aShapes; + // make a visible point + boost::shared_ptr aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter); + aShapes.push_back(aCenterPointShape); + + // make a visible circle + boost::shared_ptr aNDir = + boost::dynamic_pointer_cast(aSketch->data()->attribute(SKETCH_ATTR_NORM)); + bool aHasPlane = aNDir && !(aNDir->x() == 0 && aNDir->y() == 0 && aNDir->z() == 0); + if (aHasPlane) { + boost::shared_ptr aNormal(new GeomAPI_Dir(aNDir->x(), aNDir->y(), aNDir->z())); + + boost::shared_ptr aCircleShape = + GeomAlgoAPI_EdgeBuilder::lineCircle(aCenter, aNormal, aRadius); + aShapes.push_back(aCircleShape); + + boost::shared_ptr aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes); + setPreview(aCompound); + } + } /// \todo Implement preview for the circle return getPreview(); } diff --git a/src/SketchPlugin/SketchPlugin_Point.cpp b/src/SketchPlugin/SketchPlugin_Point.cpp index 63cf747a2..6df2b536b 100644 --- a/src/SketchPlugin/SketchPlugin_Point.cpp +++ b/src/SketchPlugin/SketchPlugin_Point.cpp @@ -26,13 +26,14 @@ void SketchPlugin_Point::execute() const boost::shared_ptr& SketchPlugin_Point::preview() { SketchPlugin_Sketch* aSketch = sketch(); - // compute a point in 3D view - boost::shared_ptr aPoint = - boost::dynamic_pointer_cast(data()->attribute(POINT_ATTR_COORD)); - boost::shared_ptr aPoint3D(aSketch->to3D(aPoint->x(), aPoint->y())); - // make a visible point - boost::shared_ptr aPointShape = GeomAlgoAPI_PointBuilder::point(aPoint3D); - setPreview(aPointShape); - + if (aSketch) { + // compute a point in 3D view + boost::shared_ptr aPoint = + boost::dynamic_pointer_cast(data()->attribute(POINT_ATTR_COORD)); + boost::shared_ptr aPoint3D(aSketch->to3D(aPoint->x(), aPoint->y())); + // make a visible point + boost::shared_ptr aPointShape = GeomAlgoAPI_PointBuilder::point(aPoint3D); + setPreview(aPointShape); + } return getPreview(); } -- 2.39.2