1 // File: PartSet_OperationFeatureCreate.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationFeatureCreate.h>
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Point.h>
12 #include <SketchPlugin_Line.h>
13 #include <SketchPlugin_Circle.h>
14 #include <SketchPlugin_Arc.h>
15 #include <SketchPlugin_ConstraintDistance.h>
16 #include <SketchPlugin_ConstraintLength.h>
17 #include <SketchPlugin_ConstraintRadius.h>
18 #include <SketchPlugin_ConstraintParallel.h>
19 #include <SketchPlugin_ConstraintPerpendicular.h>
20 #include <SketchPlugin_ConstraintCoincidence.h>
22 #include <GeomAPI_Pnt2d.h>
24 #include <ModuleBase_OperationDescription.h>
25 #include <ModuleBase_WidgetPoint2D.h>
26 #include <ModuleBase_WidgetValueFeature.h>
27 #include <ModuleBase_ViewerPrs.h>
29 #include <XGUI_Constants.h>
31 #include <V3d_View.hxx>
32 #include <TopoDS_Vertex.hxx>
34 #include <BRep_Tool.hxx>
40 #include <QMouseEvent>
44 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
46 FeaturePtr theFeature)
47 : PartSet_OperationSketchBase(theId, theParent),
53 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
57 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
59 return theId == SketchPlugin_Line::ID() || theId == SketchPlugin_Point::ID()
60 || theId == SketchPlugin_Circle::ID() || theId == SketchPlugin_Arc::ID()
61 || theId == SketchPlugin_ConstraintDistance::ID()
62 || theId == SketchPlugin_ConstraintLength::ID()
63 || theId == SketchPlugin_ConstraintRadius::ID()
64 || theId == SketchPlugin_ConstraintParallel::ID()
65 || theId == SketchPlugin_ConstraintPerpendicular::ID()
66 || theId == SketchPlugin_ConstraintCoincidence::ID();
69 bool PartSet_OperationFeatureCreate::canBeCommitted() const
71 if (PartSet_OperationSketchBase::canBeCommitted())
72 return !myActiveWidget;
76 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(ObjectPtr theFeature) const
78 std::list<int> aModes;
79 if (theFeature != feature())
80 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
84 void PartSet_OperationFeatureCreate::initSelection(
85 const std::list<ModuleBase_ViewerPrs>& theSelected,
86 const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
88 myPreSelection = theSelected;
91 void PartSet_OperationFeatureCreate::initFeature(FeaturePtr theFeature)
93 myInitFeature = theFeature;
96 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
101 void PartSet_OperationFeatureCreate::mouseReleased(
102 QMouseEvent* theEvent, Handle(V3d_View) theView,
103 const std::list<ModuleBase_ViewerPrs>& theSelected,
104 const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
107 // if the point creation is finished, the next mouse release should commit the modification
108 // the next release can happens by double click in the viewer
109 restartOperation(feature()->getKind(), feature());
113 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
114 double aX = aPoint.X(), anY = aPoint.Y();
116 if (theSelected.empty()) {
117 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
119 ModuleBase_ViewerPrs aPrs = theSelected.front();
120 const TopoDS_Shape& aShape = aPrs.shape();
121 if (!aShape.IsNull()) // the point is selected
123 if (aShape.ShapeType() == TopAbs_VERTEX) {
124 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
125 if (!aVertex.IsNull()) {
126 aPoint = BRep_Tool::Pnt(aVertex);
127 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
129 PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(), aX,
132 } else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
134 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
135 // move to selected line
136 if (feature()->getKind() == SketchPlugin_Line::ID()) {
137 //FeaturePtr aFeature = aPrs.feature();
138 //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
144 if (!theSelected.empty()) {
145 ModuleBase_ViewerPrs aPrs = theSelected.front();
146 aFeature = aPrs.object();
148 aFeature = feature(); // for the widget distance only
150 bool isApplyed = setWidgetValue(aFeature, aX, anY);
153 emit activateNextWidget(myActiveWidget);
157 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
160 restartOperation(feature()->getKind(), feature());
163 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
164 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
165 setWidgetValue(feature(), aX, anY);
170 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
172 myActiveWidget = theWidget;
173 if ((myPreSelection.size() > 0) && myActiveWidget) {
174 const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
175 ModuleBase_WidgetValueFeature aValue;
176 aValue.setObject(aPrs.object());
177 if (myActiveWidget->setValue(&aValue)) {
178 myPreSelection.remove(aPrs);
179 emit activateNextWidget(myActiveWidget);
182 if (myInitFeature && myActiveWidget) {
183 ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
184 if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
185 myInitFeature = FeaturePtr();
186 emit activateNextWidget(myActiveWidget);
191 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
195 case Qt::Key_Enter: {
197 // it start a new line creation at a free point
198 restartOperation(feature()->getKind());
202 case Qt::Key_Escape: {
213 void PartSet_OperationFeatureCreate::startOperation()
215 PartSet_OperationSketchBase::startOperation();
216 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
218 emit multiSelectionEnabled(false);
221 void PartSet_OperationFeatureCreate::abortOperation()
223 emit featureConstructed(feature(), FM_Hide);
224 PartSet_OperationSketchBase::abortOperation();
227 void PartSet_OperationFeatureCreate::stopOperation()
229 PartSet_OperationSketchBase::stopOperation();
230 emit multiSelectionEnabled(true);
233 void PartSet_OperationFeatureCreate::afterCommitOperation()
235 PartSet_OperationSketchBase::afterCommitOperation();
236 emit featureConstructed(feature(), FM_Deactivation);
239 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
241 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
243 boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
244 SketchPlugin_Feature>(sketch());
246 aFeature->addSub(aNewFeature);
248 //myFeaturePrs->init(aNewFeature);
249 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
251 //TODO emit featureConstructed(aNewFeature, FM_Activation);
257 bool PartSet_OperationFeatureCreate::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
261 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
262 aValue->setObject(theFeature);
263 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
264 bool isApplyed = myActiveWidget->setValue(aValue);
267 myIsModified = (myIsModified || isApplyed);