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>
21 #include <ModelAPI_AttributeDouble.h>
23 #include <SketchPlugin_Constraint.h>
25 #include <Geom_Line.hxx>
28 #include <XGUI_ViewerPrs.h>
29 #include <XGUI_Constants.h>
31 #include <SketchPlugin_Line.h>
33 #include <V3d_View.hxx>
34 #include <TopoDS_Vertex.hxx>
36 #include <BRep_Tool.hxx>
42 #include <QMouseEvent>
46 PartSet_OperationConstraint::PartSet_OperationConstraint(const QString& theId,
48 FeaturePtr theFeature)
49 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
53 PartSet_OperationConstraint::~PartSet_OperationConstraint()
57 bool PartSet_OperationConstraint::isGranted(ModuleBase_IOperation* theOperation) const
59 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
62 void PartSet_OperationConstraint::init(FeaturePtr theFeature,
63 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
64 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
66 //if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
68 // use the last point of the previous feature as the first of the new one
69 //boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
70 //myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
73 FeaturePtr PartSet_OperationConstraint::sketch() const
78 void PartSet_OperationConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
79 const std::list<XGUI_ViewerPrs>& theSelected,
80 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
82 /*if (myPointSelectionMode == SM_DonePoint)
84 // if the point creation is finished, the next mouse release should commit the modification
85 // the next release can happens by double click in the viewer
87 restartOperation(PartSet_OperationConstraint::Type(), feature());
93 bool isFoundPoint = false;
94 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
96 if (theSelected.empty()) {
97 //PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
98 //isFoundPoint = true;
101 XGUI_ViewerPrs aPrs = theSelected.front();
102 FeaturePtr aFeature = aPrs.feature();
104 setFeature(aFeature);
108 /*const TopoDS_Shape& aShape = aPrs.shape();
109 if (!aShape.IsNull()) // the point is selected
111 if (aShape.ShapeType() == TopAbs_VERTEX) {
112 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
113 if (!aVertex.IsNull()) {
114 aPoint = BRep_Tool::Pnt(aVertex);
115 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
118 setConstraints(aX, anY);
121 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
123 FeaturePtr aFeature = aPrs.feature();
125 double X0, X1, X2, X3;
126 double Y0, Y1, Y2, Y3;
127 PartSet_Tools::getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
128 PartSet_Tools::getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
129 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, X1, Y1);
131 switch (myPointSelectionMode) {
133 PartSet_Tools::projectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
135 case SM_SecondPoint: {
136 PartSet_Tools::getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
137 PartSet_Tools::intersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
149 /*switch (myPointSelectionMode)
151 case SM_FirstPoint: {
152 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
153 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
156 //setPointSelectionMode(SM_SecondPoint);
159 case SM_SecondPoint: {
160 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
163 //setPointSelectionMode(SM_DonePoint);
171 void PartSet_OperationConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
173 /* switch (myPointSelectionMode)
175 case SM_FirstPoint: {
177 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
178 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
179 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
180 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
182 emit focusActivated(LINE_ATTR_START);
187 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
188 setLinePoint(aPoint, theView, LINE_ATTR_END);
190 emit focusActivated(LINE_ATTR_END);
196 restartOperation(PartSet_OperationConstraint::Type(), feature());
203 void PartSet_OperationConstraint::startOperation()
205 PartSet_OperationSketchBase::startOperation();
206 //setPointSelectionMode(!myInitPoint ? SM_FirstPoint : SM_SecondPoint);
208 emit multiSelectionEnabled(false);
211 void PartSet_OperationConstraint::abortOperation()
213 emit featureConstructed(feature(), FM_Hide);
214 PartSet_OperationSketchBase::abortOperation();
217 void PartSet_OperationConstraint::stopOperation()
219 PartSet_OperationSketchBase::stopOperation();
220 emit multiSelectionEnabled(true);
223 void PartSet_OperationConstraint::afterCommitOperation()
225 PartSet_OperationSketchBase::afterCommitOperation();
226 emit featureConstructed(feature(), FM_Deactivation);
229 FeaturePtr PartSet_OperationConstraint::createFeature(const bool theFlushMessage)
231 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
233 boost::shared_ptr<SketchPlugin_Feature> aFeature =
234 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
236 aFeature->addSub(aNewFeature);
239 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
240 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
242 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
243 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
244 (aData->attribute(LINE_ATTR_START));
245 PartSet_Tools::createConstraint(myInitPoint, aPoint);
248 emit featureConstructed(aNewFeature, FM_Activation);
254 void PartSet_OperationConstraint::setFeature(FeaturePtr theFeature)
256 if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
259 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
261 boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr =
262 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
263 anAttr->setFeature(theFeature);
266 void PartSet_OperationConstraint::setValue(const double theValue)
268 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
270 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr =
271 boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
272 anAttr->setValue(theValue);