1 // File: PartSet_OperationConstraint.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationConstraint.h>
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Sketch.h>
13 #include <GeomDataAPI_Point2D.h>
15 #include <ModuleBase_OperationDescription.h>
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeRefList.h>
22 #include <SketchPlugin_Constraint.h>
24 #include <Geom_Line.hxx>
27 #include <XGUI_ViewerPrs.h>
28 #include <XGUI_Constants.h>
30 #include <SketchPlugin_Line.h>
32 #include <V3d_View.hxx>
33 #include <TopoDS_Vertex.hxx>
35 #include <BRep_Tool.hxx>
41 #include <QMouseEvent>
45 PartSet_OperationConstraint::PartSet_OperationConstraint(const QString& theId,
47 boost::shared_ptr<ModelAPI_Feature> theFeature)
48 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
52 PartSet_OperationConstraint::~PartSet_OperationConstraint()
56 bool PartSet_OperationConstraint::isGranted(ModuleBase_IOperation* theOperation) const
58 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
61 void PartSet_OperationConstraint::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
62 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
63 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
65 //if (!theFeature || theFeature->getKind() != "SketchLine")
67 // use the last point of the previous feature as the first of the new one
68 //boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
69 //myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
72 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationConstraint::sketch() const
77 void PartSet_OperationConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
78 const std::list<XGUI_ViewerPrs>& theSelected,
79 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
81 /*if (myPointSelectionMode == SM_DonePoint)
83 // if the point creation is finished, the next mouse release should commit the modification
84 // the next release can happens by double click in the viewer
86 restartOperation(PartSet_OperationConstraint::Type(), feature());
92 bool isFoundPoint = false;
93 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
94 if (theSelected.empty()) {
95 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
99 XGUI_ViewerPrs aPrs = theSelected.front();
100 const TopoDS_Shape& aShape = aPrs.shape();
101 if (!aShape.IsNull()) // the point is selected
103 if (aShape.ShapeType() == TopAbs_VERTEX) {
104 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
105 if (!aVertex.IsNull()) {
106 aPoint = BRep_Tool::Pnt(aVertex);
107 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
110 setConstraints(aX, anY);
113 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
115 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
117 double X0, X1, X2, X3;
118 double Y0, Y1, Y2, Y3;
119 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
120 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
121 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, X1, Y1);
123 switch (myPointSelectionMode) {
125 PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
127 case SM_SecondPoint: {
128 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
129 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
141 switch (myPointSelectionMode)
143 case SM_FirstPoint: {
144 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
145 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
148 //setPointSelectionMode(SM_SecondPoint);
151 case SM_SecondPoint: {
152 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
155 //setPointSelectionMode(SM_DonePoint);
164 void PartSet_OperationConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
166 /* switch (myPointSelectionMode)
168 case SM_FirstPoint: {
170 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
171 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
172 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
173 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
175 emit focusActivated(LINE_ATTR_START);
180 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
181 setLinePoint(aPoint, theView, LINE_ATTR_END);
183 emit focusActivated(LINE_ATTR_END);
189 restartOperation(PartSet_OperationConstraint::Type(), feature());
196 void PartSet_OperationConstraint::startOperation()
198 PartSet_OperationSketchBase::startOperation();
199 //setPointSelectionMode(!myInitPoint ? SM_FirstPoint : SM_SecondPoint);
201 emit multiSelectionEnabled(false);
204 void PartSet_OperationConstraint::abortOperation()
206 emit featureConstructed(feature(), FM_Hide);
207 PartSet_OperationSketchBase::abortOperation();
210 void PartSet_OperationConstraint::stopOperation()
212 PartSet_OperationSketchBase::stopOperation();
213 emit multiSelectionEnabled(true);
216 void PartSet_OperationConstraint::afterCommitOperation()
218 PartSet_OperationSketchBase::afterCommitOperation();
219 emit featureConstructed(feature(), FM_Deactivation);
222 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationConstraint::createFeature(const bool theFlushMessage)
224 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature(false);
226 boost::shared_ptr<SketchPlugin_Feature> aFeature =
227 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
229 aFeature->addSub(aNewFeature);
232 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
233 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
235 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
236 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
237 (aData->attribute(LINE_ATTR_START));
238 createConstraint(myInitPoint, aPoint);
241 emit featureConstructed(aNewFeature, FM_Activation);