From 135a6de5327328c770b09086c00f673a4ff7a851 Mon Sep 17 00:00:00 2001 From: vsv Date: Thu, 27 Nov 2014 18:45:28 +0300 Subject: [PATCH] Provide sub-shapes selection in sketcher --- .../ModuleBase_WidgetShapeSelector.cpp | 6 +-- .../ModuleBase_WidgetShapeSelector.h | 3 +- src/PartSet/CMakeLists.txt | 16 +----- src/PartSet/PartSet_Module.cpp | 26 +++++++++- src/PartSet/PartSet_Tools.cpp | 49 +++++++++++++++---- src/PartSet/PartSet_Tools.h | 17 ++++++- src/PartSet/PartSet_WidgetShapeSelector.cpp | 38 ++++++++++++++ src/PartSet/PartSet_WidgetShapeSelector.h | 35 +++++++++++++ src/SketchPlugin/plugin-Sketch.xml | 8 +-- 9 files changed, 162 insertions(+), 36 deletions(-) create mode 100644 src/PartSet/PartSet_WidgetShapeSelector.cpp create mode 100644 src/PartSet/PartSet_WidgetShapeSelector.h diff --git a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp index e7899b5d2..211363d16 100644 --- a/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp +++ b/src/ModuleBase/ModuleBase_WidgetShapeSelector.cpp @@ -133,9 +133,9 @@ bool ModuleBase_WidgetShapeSelector::storeValue() const ResultPtr aBody = std::dynamic_pointer_cast(mySelectedObject); if (aBody) { AttributePtr aAttr = aData->attribute(attributeID()); - std::shared_ptr aSelectAttr = - std::dynamic_pointer_cast(aAttr); + AttributeSelectionPtr aSelectAttr = + std::dynamic_pointer_cast(aAttr); if (aSelectAttr) aSelectAttr->setValue(aBody, myShape); updateObject(myFeature); @@ -171,7 +171,7 @@ bool ModuleBase_WidgetShapeSelector::restoreValue() DataPtr aData = myFeature->data(); bool isBlocked = this->blockSignals(true); if (myUseSubShapes) { - std::shared_ptr aSelect = aData->selection(attributeID()); + AttributeSelectionPtr aSelect = aData->selection(attributeID()); if (aSelect) { mySelectedObject = aSelect->context(); myShape = aSelect->value(); diff --git a/src/ModuleBase/ModuleBase_WidgetShapeSelector.h b/src/ModuleBase/ModuleBase_WidgetShapeSelector.h index d0b04a6c6..4cf0a4d2e 100644 --- a/src/ModuleBase/ModuleBase_WidgetShapeSelector.h +++ b/src/ModuleBase/ModuleBase_WidgetShapeSelector.h @@ -35,7 +35,6 @@ Q_OBJECT virtual ~ModuleBase_WidgetShapeSelector(); /// Saves the internal parameters to the given feature - /// \param theObject a model feature to be changed virtual bool storeValue() const; virtual bool restoreValue(); @@ -98,7 +97,7 @@ Q_OBJECT void setObject(ObjectPtr theObj, std::shared_ptr theShape = std::shared_ptr()); //----------- Class members ------------- - private: + protected: QWidget* myContainer; QLabel* myLabel; QLineEdit* myTextLine; diff --git a/src/PartSet/CMakeLists.txt b/src/PartSet/CMakeLists.txt index bdfcb5360..a6c9c34e8 100644 --- a/src/PartSet/CMakeLists.txt +++ b/src/PartSet/CMakeLists.txt @@ -6,36 +6,24 @@ SET(PROJECT_HEADERS PartSet.h PartSet_Constants.h PartSet_EditLine.h -# PartSet_Listener.h PartSet_Module.h -# PartSet_OperationFeatureCreate.h -# PartSet_OperationFeatureEdit.h -# PartSet_OperationSketchBase.h -# PartSet_OperationSketch.h -# PartSet_OperationFeatureBase.h PartSet_Tools.h PartSet_WidgetSketchLabel.h PartSet_Validators.h PartSet_WidgetPoint2d.h PartSet_WidgetPoint2dDistance.h -# PartSet_Operation.h + PartSet_WidgetShapeSelector.h ) SET(PROJECT_SOURCES PartSet_EditLine.cpp -# PartSet_Listener.cpp PartSet_Module.cpp -# PartSet_OperationFeatureCreate.cpp -# PartSet_OperationFeatureEdit.cpp -# PartSet_OperationSketchBase.cpp -# PartSet_OperationSketch.cpp -# PartSet_OperationFeatureBase.cpp PartSet_Tools.cpp PartSet_WidgetSketchLabel.cpp PartSet_Validators.cpp PartSet_WidgetPoint2d.cpp PartSet_WidgetPoint2dDistance.cpp -# PartSet_Operation.cpp + PartSet_WidgetShapeSelector.cpp ) SET(PROJECT_RESOURCES diff --git a/src/PartSet/PartSet_Module.cpp b/src/PartSet/PartSet_Module.cpp index 7204c0e12..e115736c3 100644 --- a/src/PartSet/PartSet_Module.cpp +++ b/src/PartSet/PartSet_Module.cpp @@ -9,7 +9,7 @@ #include "PartSet_Tools.h" #include "PartSet_WidgetPoint2D.h" #include "PartSet_WidgetPoint2dDistance.h" -//#include "PartSet_Operation.h" +#include "PartSet_WidgetShapeSelector.h" #include #include @@ -46,6 +46,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include //#include //#include @@ -399,11 +405,16 @@ void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* t QStringList PartSet_Module::sketchOperationIdList() const { QStringList aIds; - aIds << SketchPlugin_Sketch::ID().c_str(); aIds << SketchPlugin_Line::ID().c_str(); aIds << SketchPlugin_Point::ID().c_str(); aIds << SketchPlugin_Arc::ID().c_str(); aIds << SketchPlugin_Circle::ID().c_str(); + aIds << SketchPlugin_ConstraintLength::ID().c_str(); + aIds << SketchPlugin_ConstraintDistance::ID().c_str(); + aIds << SketchPlugin_ConstraintRigid::ID().c_str(); + aIds << SketchPlugin_ConstraintRadius::ID().c_str(); + aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str(); + aIds << SketchPlugin_ConstraintParallel::ID().c_str(); return aIds; } @@ -858,6 +869,7 @@ QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* aWgt->setWorkshop(aWorkshop); theModelWidgets.append(aWgt); return aWgt->getControl(); + } else if (theType == "sketch-2dpoint_selector") { PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId); aWgt->setWorkshop(aWorkshop); @@ -868,6 +880,7 @@ QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theModelWidgets.append(aWgt); return aWgt->getControl(); + } if (theType == "point2ddistance") { PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId); aWgt->setWorkshop(aWorkshop); @@ -875,6 +888,15 @@ QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theModelWidgets.append(aWgt); return aWgt->getControl(); + + } if (theType == "sketch_shape_selector") { + PartSet_WidgetShapeSelector* aWgt = + new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId); + aWgt->setSketcher(myCurrentSketch); + + theModelWidgets.append(aWgt); + return aWgt->getControl(); + }else return 0; } diff --git a/src/PartSet/PartSet_Tools.cpp b/src/PartSet/PartSet_Tools.cpp index dbb12d393..fdc4e25c8 100644 --- a/src/PartSet/PartSet_Tools.cpp +++ b/src/PartSet/PartSet_Tools.cpp @@ -126,12 +126,8 @@ 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 PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr theSketch) { - if (!theSketch) - return; - std::shared_ptr aData = theSketch->data(); std::shared_ptr aC = std::dynamic_pointer_cast( @@ -141,11 +137,10 @@ void PartSet_Tools::convertTo3D(const double theX, const double theY, FeaturePtr std::shared_ptr aY = std::dynamic_pointer_cast( aData->attribute(SketchPlugin_Sketch::DIRY_ID())); - std::shared_ptr aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX)) - ->added(aY->dir()->xyz()->multiplied(theY)); + std::shared_ptr aPnt2d = + std::shared_ptr(new GeomAPI_Pnt2d(theX, theY)); - std::shared_ptr aPoint = std::shared_ptr(new GeomAPI_Pnt(aSum)); - thePoint = gp_Pnt(aPoint->x(), aPoint->y(), aPoint->z()); + return aPnt2d->to3D(aC->pnt(), aX->dir(), aY->dir()); } ObjectPtr PartSet_Tools::nearestFeature(QPoint thePoint, Handle_V3d_View theView, @@ -535,3 +530,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 aValue = std::shared_ptr( + new GeomAPI_Pnt(aPoint.X(), aPoint.Y(), aPoint.Z())); + + // find the given point in the feature attributes + std::list anAttiributes = + aFeature->data()->attributes(GeomDataAPI_Point2D::type()); + std::list::const_iterator anIt = anAttiributes.begin(), + aLast = anAttiributes.end(); + for (; anIt != aLast && !anAttribute; anIt++) { + std::shared_ptr aCurPoint = + std::dynamic_pointer_cast(*anIt); + + std::shared_ptr aPnt = convertTo3D(aCurPoint->x(), aCurPoint->y(), theSketch); + if (aPnt && (aPnt->distance(aValue) < Precision::Confusion())) { + anAttribute = aCurPoint; + break; + } + } + } + } + } + return anAttribute; +} diff --git a/src/PartSet/PartSet_Tools.h b/src/PartSet/PartSet_Tools.h index 53c98a47b..522f0363d 100644 --- a/src/PartSet/PartSet_Tools.h +++ b/src/PartSet/PartSet_Tools.h @@ -14,6 +14,10 @@ #include #include +#include +#include + +#include #include @@ -52,8 +56,7 @@ class PARTSET_EXPORT PartSet_Tools /// \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, - gp_Pnt& thePoint); + static std::shared_ptr convertTo3D(const double theX, const double theY, FeaturePtr theSketch); /// Returns an object that is under the mouse point. Firstly it checks the highlighting, /// if it exists, the first object is returned. Secondly, there is an iteration on @@ -161,6 +164,16 @@ class PARTSET_EXPORT PartSet_Tools /// \param theY the output vertical coordinate of the point static bool hasVertexShape(const ModuleBase_ViewerPrs& thePrs, FeaturePtr theSketch, Handle_V3d_View theView, double& theX, double& theY); + + + /** + * Find attribute of object which corresponds to the given shape + * \param theObj - an object + * \param theShape - a Shape + * \param theSketch - a Sketch to get a plane of converting to 2d + */ + static AttributePtr findAttributeBy2dPoint(ObjectPtr theObj, const TopoDS_Shape theShape, FeaturePtr theSketch); + protected: /// Returns an object that is under the mouse point. Firstly it checks the highlighting, /// if it exists, the first object is returned. Secondly, there is an iteration on diff --git a/src/PartSet/PartSet_WidgetShapeSelector.cpp b/src/PartSet/PartSet_WidgetShapeSelector.cpp new file mode 100644 index 000000000..6036af1d4 --- /dev/null +++ b/src/PartSet/PartSet_WidgetShapeSelector.cpp @@ -0,0 +1,38 @@ +// File: PartSet_WidgetShapeSelector.cpp +// Created: 27 Nov 2014 +// Author: Vitaly Smetannikov + +#include "PartSet_WidgetShapeSelector.h" + +#include +#include + + +bool PartSet_WidgetShapeSelector::storeValue() const +{ + if (!mySelectedObject) + return false; + + FeaturePtr aSelectedFeature = ModelAPI_Feature::feature(mySelectedObject); + if (aSelectedFeature == myFeature) // In order to avoid selection of the same object + return false; + + DataPtr aData = myFeature->data(); + if (myUseSubShapes && myShape) { + AttributePtr aAttr = aData->attribute(attributeID()); + AttributeRefAttrPtr aRefAttr = + std::dynamic_pointer_cast(aAttr); + if (aRefAttr) { + TopoDS_Shape aShape = myShape->impl(); + AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(mySelectedObject, aShape, mySketch); + if (mySelectedObject) + aRefAttr->setObject(mySelectedObject); + if (aPntAttr) + aRefAttr->setAttr(aPntAttr); + + updateObject(myFeature); + return true; + } + } + return ModuleBase_WidgetShapeSelector::storeValue(); +} diff --git a/src/PartSet/PartSet_WidgetShapeSelector.h b/src/PartSet/PartSet_WidgetShapeSelector.h new file mode 100644 index 000000000..a216830ae --- /dev/null +++ b/src/PartSet/PartSet_WidgetShapeSelector.h @@ -0,0 +1,35 @@ +// File: PartSet_WidgetShapeSelector.h +// Created: 27 Nov 2014 +// Author: Vitaly Smetannikov + + +#ifndef PartSet_WidgetShapeSelector_H +#define PartSet_WidgetShapeSelector_H + +#include "PartSet.h" + +#include + +#include + +class PARTSET_EXPORT PartSet_WidgetShapeSelector: public ModuleBase_WidgetShapeSelector +{ +Q_OBJECT + public: + PartSet_WidgetShapeSelector(QWidget* theParent, ModuleBase_IWorkshop* theWorkshop, + const Config_WidgetAPI* theData, const std::string& theParentId) + : ModuleBase_WidgetShapeSelector(theParent, theWorkshop, theData, theParentId) {} + + virtual ~PartSet_WidgetShapeSelector() {} + + /// Saves the internal parameters to the given feature + virtual bool storeValue() const; + + void setSketcher(CompositeFeaturePtr theSketch) { mySketch = theSketch; } + CompositeFeaturePtr sketch() const { return mySketch; } + +private: + CompositeFeaturePtr mySketch; +}; + +#endif \ No newline at end of file diff --git a/src/SketchPlugin/plugin-Sketch.xml b/src/SketchPlugin/plugin-Sketch.xml index d0881383b..4395e92fc 100644 --- a/src/SketchPlugin/plugin-Sketch.xml +++ b/src/SketchPlugin/plugin-Sketch.xml @@ -30,10 +30,10 @@