1 // File: PartSet_OperationSketchLine.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationSketchLine.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>
29 #include <SketchPlugin_Line.h>
31 #include <V3d_View.hxx>
32 #include <TopoDS_Vertex.hxx>
34 #include <BRep_Tool.hxx>
40 #include <QMouseEvent>
44 PartSet_OperationSketchLine::PartSet_OperationSketchLine(const QString& theId,
46 boost::shared_ptr<ModelAPI_Feature> theFeature)
47 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature),
48 myPointSelectionMode(SM_FirstPoint)
52 PartSet_OperationSketchLine::~PartSet_OperationSketchLine()
56 bool PartSet_OperationSketchLine::isGranted(ModuleBase_IOperation* theOperation) const
58 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
61 std::list<int> PartSet_OperationSketchLine::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
63 std::list<int> aModes;
64 if (theFeature != feature())
65 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
69 void PartSet_OperationSketchLine::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
70 const std::list<XGUI_ViewerPrs>& /*thePresentations*/)
72 if (!theFeature || theFeature->getKind() != "SketchLine")
74 // use the last point of the previous feature as the first of the new one
75 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
76 myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
79 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::sketch() const
84 void PartSet_OperationSketchLine::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
85 const std::list<XGUI_ViewerPrs>& theSelected)
89 bool isFoundPoint = false;
90 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
91 if (theSelected.empty()) {
92 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
96 XGUI_ViewerPrs aPrs = theSelected.front();
97 const TopoDS_Shape& aShape = aPrs.shape();
98 if (!aShape.IsNull()) // the point is selected
100 if (aShape.ShapeType() == TopAbs_VERTEX) {
101 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
102 if (!aVertex.IsNull()) {
103 aPoint = BRep_Tool::Pnt(aVertex);
104 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
107 setConstraints(aX, anY);
110 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
112 boost::shared_ptr<ModelAPI_Feature> aFeature = aPrs.feature();
114 double X0, X1, X2, X3;
115 double Y0, Y1, Y2, Y3;
116 getLinePoint(aFeature, LINE_ATTR_START, X2, Y2);
117 getLinePoint(aFeature, LINE_ATTR_END, X3, Y3);
118 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, X1, Y1);
120 switch (myPointSelectionMode) {
122 PartSet_Tools::ProjectPointOnLine(X2, Y2, X3, Y3, X1, Y1, aX, anY);
124 case SM_SecondPoint: {
125 getLinePoint(feature(), LINE_ATTR_START, X0, Y0);
126 PartSet_Tools::IntersectLines(X0, Y0, X1, Y1, X2, Y2, X3, Y3, aX, anY);
140 switch (myPointSelectionMode)
142 case SM_FirstPoint: {
143 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
144 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
145 myPointSelectionMode = SM_SecondPoint;
148 case SM_SecondPoint: {
149 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
150 myPointSelectionMode = SM_DonePoint;
158 void PartSet_OperationSketchLine::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
160 switch (myPointSelectionMode)
162 case SM_FirstPoint: {
164 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
165 PartSet_Tools::ConvertTo2D(aPoint, sketch(), theView, aX, anY);
166 setLinePoint(feature(), aX, anY, LINE_ATTR_START);
167 setLinePoint(feature(), aX, anY, LINE_ATTR_END);
172 gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(theEvent->pos(), theView);
173 setLinePoint(aPoint, theView, LINE_ATTR_END);
179 emit featureConstructed(feature(), FM_Deactivation);
180 emit launchOperation(PartSet_OperationSketchLine::Type(), feature());
187 void PartSet_OperationSketchLine::keyReleased(const int theKey)
190 case Qt::Key_Return: {
191 if (myPointSelectionMode == SM_DonePoint)
194 emit featureConstructed(feature(), FM_Deactivation);
198 emit launchOperation(PartSet_OperationSketchLine::Type(), boost::shared_ptr<ModelAPI_Feature>());
202 PartSet_OperationSketchBase::keyReleased(theKey);
207 void PartSet_OperationSketchLine::startOperation()
209 PartSet_OperationSketchBase::startOperation();
210 myPointSelectionMode = !myInitPoint ? SM_FirstPoint : SM_SecondPoint;
211 emit multiSelectionEnabled(false);
214 void PartSet_OperationSketchLine::abortOperation()
216 emit featureConstructed(feature(), FM_Abort);
217 PartSet_OperationSketchBase::abortOperation();
220 void PartSet_OperationSketchLine::stopOperation()
222 PartSet_OperationSketchBase::stopOperation();
223 emit multiSelectionEnabled(true);
226 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketchLine::createFeature()
228 boost::shared_ptr<ModelAPI_Feature> aNewFeature = ModuleBase_Operation::createFeature();
230 boost::shared_ptr<SketchPlugin_Feature> aFeature =
231 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
233 aFeature->addSub(aNewFeature);
236 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
237 setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
239 boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
240 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
241 (aData->attribute(LINE_ATTR_START));
242 createConstraint(myInitPoint, aPoint);
245 emit featureConstructed(aNewFeature, FM_Activation);
249 void PartSet_OperationSketchLine::createConstraint(boost::shared_ptr<GeomDataAPI_Point2D> thePoint1,
250 boost::shared_ptr<GeomDataAPI_Point2D> thePoint2)
252 boost::shared_ptr<ModelAPI_Document> aDoc = document();
253 boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature("SketchConstraintCoincidence");
256 boost::shared_ptr<SketchPlugin_Feature> aSketch =
257 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
258 aSketch->addSub(aFeature);
261 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
263 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef1 =
264 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
265 aRef1->setAttr(thePoint1);
267 boost::shared_ptr<ModelAPI_AttributeRefAttr> aRef2 =
268 boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_B));
269 aRef2->setAttr(thePoint2);
271 if (aFeature) // TODO: generate an error if feature was not created
275 void PartSet_OperationSketchLine::setConstraints(double theX, double theY)
277 std::string aPointArg;
278 switch (myPointSelectionMode)
281 aPointArg = LINE_ATTR_START;
284 aPointArg = LINE_ATTR_END;
288 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
289 boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
290 (aData->attribute(aPointArg));
291 aData = sketch()->data();
292 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
293 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
295 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
296 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
297 aLast = aFeatures.end();
298 for (; anIt != aLast; anIt++) {
299 boost::shared_ptr<ModelAPI_Feature> aFeature = *anIt;
300 boost::shared_ptr<GeomDataAPI_Point2D> aFPoint = findLinePoint(aFeature, theX, theY);
302 createConstraint(aFPoint, aPoint);
306 void PartSet_OperationSketchLine::getLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
307 const std::string& theAttribute,
308 double& theX, double& theY)
310 if (!theFeature || theFeature->getKind() != "SketchLine")
312 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
313 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
314 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
319 boost::shared_ptr<GeomDataAPI_Point2D> PartSet_OperationSketchLine::findLinePoint(
320 boost::shared_ptr<ModelAPI_Feature> theFeature,
321 double theX, double theY)
323 boost::shared_ptr<GeomDataAPI_Point2D> aPoint2D;
324 if (!theFeature || theFeature->getKind() != "SketchLine")
326 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
328 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
329 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_START));
330 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
333 aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
334 if (fabs(aPoint->x() - theX) < Precision::Confusion() && fabs(aPoint->y() - theY) < Precision::Confusion() )
340 void PartSet_OperationSketchLine::setLinePoint(boost::shared_ptr<ModelAPI_Feature> theFeature,
341 double theX, double theY,
342 const std::string& theAttribute)
346 boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
347 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
348 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
349 aPoint->setValue(theX, theY);
352 void PartSet_OperationSketchLine::setLinePoint(const gp_Pnt& thePoint,
353 Handle(V3d_View) theView,
354 const std::string& theAttribute)
357 PartSet_Tools::ConvertTo2D(thePoint, sketch(), theView, aX, anY);
358 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
359 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
360 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(theAttribute));
361 aPoint->setValue(aX, anY);