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::keyReleased(std::string theName, QKeyEvent* theEvent)
172 int aKeyType = theEvent->key();
173 // the second point should be activated by any modification in the property panel
174 if (!theName.empty()) {
175 //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
177 keyReleased(theEvent->key());
180 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
182 myActiveWidget = theWidget;
183 if ((myPreSelection.size() > 0) && myActiveWidget) {
184 const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
185 ModuleBase_WidgetValueFeature aValue;
186 aValue.setObject(aPrs.object());
187 if (myActiveWidget->setValue(&aValue)) {
188 myPreSelection.remove(aPrs);
189 emit activateNextWidget(myActiveWidget);
192 if (myInitFeature && myActiveWidget) {
193 ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
194 if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
195 myInitFeature = FeaturePtr();
196 emit activateNextWidget(myActiveWidget);
201 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
204 case Qt::Key_Return: {
206 // it start a new line creation at a free point
207 restartOperation(feature()->getKind(), FeaturePtr());
211 case Qt::Key_Escape: {
221 void PartSet_OperationFeatureCreate::startOperation()
223 PartSet_OperationSketchBase::startOperation();
224 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
226 emit multiSelectionEnabled(false);
229 void PartSet_OperationFeatureCreate::abortOperation()
231 emit featureConstructed(feature(), FM_Hide);
232 PartSet_OperationSketchBase::abortOperation();
235 void PartSet_OperationFeatureCreate::stopOperation()
237 PartSet_OperationSketchBase::stopOperation();
238 emit multiSelectionEnabled(true);
241 void PartSet_OperationFeatureCreate::afterCommitOperation()
243 PartSet_OperationSketchBase::afterCommitOperation();
244 emit featureConstructed(feature(), FM_Deactivation);
247 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
249 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
251 boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
252 SketchPlugin_Feature>(sketch());
254 aFeature->addSub(aNewFeature);
256 //myFeaturePrs->init(aNewFeature);
257 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
259 //TODO emit featureConstructed(aNewFeature, FM_Activation);
265 bool PartSet_OperationFeatureCreate::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
269 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
270 aValue->setObject(theFeature);
271 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
272 bool isApplyed = myActiveWidget->setValue(aValue);
275 myIsModified = (myIsModified || isApplyed);