--- /dev/null
+// File: PartSet_OperationConstraint.h
+// Created: 20 Apr 2014
+// Author: Natalia ERMOLAEVA
+
+#include <PartSet_OperationConstraint.h>
+
+#include <PartSet_Tools.h>
+#include <PartSet_OperationSketch.h>
+
+#include <SketchPlugin_Feature.h>
+#include <SketchPlugin_Sketch.h>
+
+#include <GeomDataAPI_Point2D.h>
+
+#include <ModuleBase_OperationDescription.h>
+
+#include <ModelAPI_Data.h>
+#include <ModelAPI_Document.h>
+#include <ModelAPI_AttributeRefAttr.h>
+#include <ModelAPI_AttributeRefList.h>
+
+#include <SketchPlugin_Constraint.h>
+
+#include <Geom_Line.hxx>
+#include <gp_Lin.hxx>
+
+#include <XGUI_ViewerPrs.h>
+#include <XGUI_Constants.h>
+
+#include <SketchPlugin_Line.h>
+
+#include <V3d_View.hxx>
+#include <TopoDS_Vertex.hxx>
+#include <TopoDS.hxx>
+#include <BRep_Tool.hxx>
+
+#ifdef _DEBUG
+#include <QDebug>
+#endif
+
+#include <QMouseEvent>
+
+using namespace std;
+
+PartSet_OperationConstraint::PartSet_OperationConstraint(const QString& theId,
+ QObject* theParent,
+ boost::shared_ptr<ModelAPI_Feature> theFeature)
+: PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
+{
+}
+
+PartSet_OperationConstraint::~PartSet_OperationConstraint()
+{
+}
+
+bool PartSet_OperationConstraint::isGranted(ModuleBase_IOperation* theOperation) const
+{
+ return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
+}
+
+void PartSet_OperationConstraint::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
+ const std::list<XGUI_ViewerPrs>& /*theSelected*/,
+ const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
+{
+ //if (!theFeature || theFeature->getKind() != "SketchLine")
+ // return;
+ // use the last point of the previous feature as the first of the new one
+ //boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
+ //myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
+}
+
+boost::shared_ptr<ModelAPI_Feature> PartSet_OperationConstraint::sketch() const
+{
+ return mySketch;
+}
+
+void PartSet_OperationConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
+ const std::list<XGUI_ViewerPrs>& theSelected,
+ const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
+{
+ /*if (myPointSelectionMode == SM_DonePoint)
+ {
+ // if the point creation is finished, the next mouse release should commit the modification
+ // the next release can happens by double click in the viewer
+ commit();
+ restartOperation(PartSet_OperationConstraint::Type(), feature());
+ return;
+ }
+
+ double aX, anY;
+
+ bool isFoundPoint = false;
+ gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
+ if (theSelected.empty()) {
+ PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
+ isFoundPoint = true;
+ }
+ else {
+ XGUI_ViewerPrs aPrs = theSelected.front();
+ const TopoDS_Shape& aShape = aPrs.shape();
+ if (!aShape.IsNull()) // the point is selected
+ {
+ if (aShape.ShapeType() == TopAbs_VERTEX) {
+ const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
+ if (!aVertex.IsNull()) {
+ aPoint = BRep_Tool::Pnt(aVertex);
+ PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
+ isFoundPoint = true;
+
+ setConstraints(aX, anY);
+ }
+ }
+ else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
+ {
+ boost::shared_ptr<ModelAPI_Feature> 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::ConvertTo2D(aPoint, sketch(), theView, X1, Y1);
+
+ switch (myPointSelectionMode) {
+ case SM_FirstPoint:
+ PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
+ break;
+ case SM_SecondPoint: {
+ getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
+ PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
+ }
+ break;
+ default:
+ break;
+ }
+ isFoundPoint = true;
+ }
+ }
+ }
+ }
+
+ switch (myPointSelectionMode)
+ {
+ case SM_FirstPoint: {
+ setLinePoint(feature(), aX, anY, LINE_ATTR_START);
+ setLinePoint(feature(), aX, anY, LINE_ATTR_END);
+ flushUpdated();
+
+ //setPointSelectionMode(SM_SecondPoint);
+ }
+ break;
+ case SM_SecondPoint: {
+ setLinePoint(feature(), aX, anY, LINE_ATTR_END);
+ flushUpdated();
+
+ //setPointSelectionMode(SM_DonePoint);
+ }
+ break;
+ default:
+ break;
+ }
+*/
+}
+
+void PartSet_OperationConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
+{
+/* switch (myPointSelectionMode)
+ {
+ case SM_FirstPoint: {
+ double aX, anY;
+ gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
+ PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
+ setLinePoint(feature(), aX, anY, LINE_ATTR_START);
+ setLinePoint(feature(), aX, anY, LINE_ATTR_END);
+ flushUpdated();
+ emit focusActivated(LINE_ATTR_START);
+ }
+ break;
+ case SM_SecondPoint:
+ {
+ gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
+ setLinePoint(aPoint, theView, LINE_ATTR_END);
+ flushUpdated();
+ emit focusActivated(LINE_ATTR_END);
+ }
+ break;
+ case SM_DonePoint:
+ {
+ commit();
+ restartOperation(PartSet_OperationConstraint::Type(), feature());
+ }
+ default:
+ break;
+ }*/
+}
+
+void PartSet_OperationConstraint::startOperation()
+{
+ PartSet_OperationSketchBase::startOperation();
+ //setPointSelectionMode(!myInitPoint ? SM_FirstPoint : SM_SecondPoint);
+
+ emit multiSelectionEnabled(false);
+}
+
+void PartSet_OperationConstraint::abortOperation()
+{
+ emit featureConstructed(feature(), FM_Hide);
+ PartSet_OperationSketchBase::abortOperation();
+}
+
+void PartSet_OperationConstraint::stopOperation()
+{
+ PartSet_OperationSketchBase::stopOperation();
+ emit multiSelectionEnabled(true);
+}
+
+void PartSet_OperationConstraint::afterCommitOperation()
+{
+ PartSet_OperationSketchBase::afterCommitOperation();
+ emit featureConstructed(feature(), FM_Deactivation);
+}
+
+boost::shared_ptr<ModelAPI_Feature> PartSet_OperationConstraint::createFeature(const bool theFlushMessage)
+{
+ boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature(false);
+ if (sketch()) {
+ boost::shared_ptr<SketchPlugin_Feature> aFeature =
+ boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
+
+ aFeature->addSub(aNewFeature);
+ }
+ /*if (myInitPoint) {
+ setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
+ setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
+
+ boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
+ boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
+ (aData->attribute(LINE_ATTR_START));
+ createConstraint(myInitPoint, aPoint);
+ }*/
+
+ emit featureConstructed(aNewFeature, FM_Activation);
+ if (theFlushMessage)
+ flushCreated();
+ return aNewFeature;
+}
--- /dev/null
+// File: PartSet_OperationConstraint.h
+// Created: 20 Apr 2014
+// Author: Natalia ERMOLAEVA
+
+#ifndef PartSet_OperationConstraint_H
+#define PartSet_OperationConstraint_H
+
+#include "PartSet.h"
+
+#include <PartSet_OperationSketchBase.h>
+#include <QObject>
+
+/*!
+ \class PartSet_OperationConstraint
+ * \brief The operation for the sketch constraint feature creation
+*/
+class PARTSET_EXPORT PartSet_OperationConstraint : public PartSet_OperationSketchBase
+{
+ Q_OBJECT
+
+public:
+ /// Returns the operation type key
+ static std::string Type() { return "SketchConstraintDistance"; }
+
+public:
+ /// Constructor
+ /// \param theId the feature identifier
+ /// \param theParent the operation parent
+ /// \param theFeature the parent feature
+ PartSet_OperationConstraint(const QString& theId, QObject* theParent,
+ boost::shared_ptr<ModelAPI_Feature> theSketchFeature);
+ /// Destructor
+ virtual ~PartSet_OperationConstraint();
+
+ /// Returns that this operator can be started above already running one.
+ /// The runned operation should be the sketch feature modified operation
+ /// \param theOperation the previous running operation
+ virtual bool isGranted(ModuleBase_IOperation* theOperation) const;
+
+ /// Initializes some fields accorging to the feature
+ /// \param theSelected the list of selected presentations
+ /// \param theHighlighted the list of highlighted presentations
+ virtual void init(boost::shared_ptr<ModelAPI_Feature> theFeature,
+ const std::list<XGUI_ViewerPrs>& theSelected,
+ const std::list<XGUI_ViewerPrs>& theHighlighted);
+
+ /// Returns the operation sketch feature
+ /// \returns the sketch instance
+ virtual boost::shared_ptr<ModelAPI_Feature> sketch() const;
+
+ /// Gives the current selected objects to be processed by the operation
+ /// \param theEvent the mouse event
+ /// \param theView a viewer to have the viewer the eye position
+ /// \param theSelected the list of selected presentations
+ /// \param theHighlighted the list of highlighted presentations
+ virtual void mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
+ const std::list<XGUI_ViewerPrs>& theSelected,
+ const std::list<XGUI_ViewerPrs>& theHighlighted);
+ /// Gives the current mouse point in the viewer
+ /// \param thePoint a point clicked in the viewer
+ /// \param theEvent the mouse event
+ virtual void mouseMoved(QMouseEvent* theEvent, Handle_V3d_View theView);
+
+protected:
+ /// \brief Virtual method called when operation is started
+ /// Virtual method called when operation started (see start() method for more description)
+ /// After the parent operation body perform, set sketch feature to the created line feature
+ virtual void startOperation();
+
+ /// Virtual method called when operation aborted (see abort() method for more description)
+ /// Before the feature is aborted, it should be hidden from the viewer
+ virtual void abortOperation();
+
+ /// Virtual method called when operation stopped - committed or aborted.
+ /// Restore the multi selection state
+ virtual void stopOperation();
+
+ /// Virtual method called after operation committed (see commit() method for more description)
+ virtual void afterCommitOperation();
+
+ /// Creates an operation new feature
+ /// In addition to the default realization it appends the created line feature to
+ /// the sketch feature
+ /// \param theFlushMessage the flag whether the create message should be flushed
+ /// \returns the created feature
+ virtual boost::shared_ptr<ModelAPI_Feature> createFeature(const bool theFlushMessage = true);
+
+private:
+ boost::shared_ptr<ModelAPI_Feature> mySketch; ///< the sketch feature
+};
+
+#endif