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), mySketch(theFeature), myActiveWidget(0)
51 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
55 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
57 return theId == SketchPlugin_Line::ID() || theId == SketchPlugin_Point::ID() ||
58 theId == SketchPlugin_Circle::ID() ||
59 theId == SketchPlugin_Arc::ID() ||
60 theId == SketchPlugin_ConstraintDistance::ID() ||
61 theId == SketchPlugin_ConstraintLength::ID() ||
62 theId == SketchPlugin_ConstraintRadius::ID() ||
63 theId == SketchPlugin_ConstraintParallel::ID() ||
64 theId == SketchPlugin_ConstraintPerpendicular::ID() ||
65 theId == SketchPlugin_ConstraintCoincidence::ID();
68 bool PartSet_OperationFeatureCreate::canBeCommitted() const
70 return !myActiveWidget;
73 bool PartSet_OperationFeatureCreate::isGranted(ModuleBase_IOperation* theOperation) const
75 return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
78 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(ObjectPtr theFeature) const
80 std::list<int> aModes;
81 if (theFeature != feature())
82 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
86 void PartSet_OperationFeatureCreate::initSelection(const std::list<ModuleBase_ViewerPrs>& theSelected,
87 const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
89 myPreSelection = theSelected;
92 void PartSet_OperationFeatureCreate::initFeature(FeaturePtr theFeature)
94 myInitFeature = theFeature;
98 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
103 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
104 const std::list<ModuleBase_ViewerPrs>& theSelected,
105 const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
107 if (canBeCommitted())
109 // if the point creation is finished, the next mouse release should commit the modification
110 // the next release can happens by double click in the viewer
112 restartOperation(feature()->getKind(), feature());
116 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
117 double aX = aPoint.X(), anY = aPoint.Y();
119 if (theSelected.empty()) {
120 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
123 ModuleBase_ViewerPrs aPrs = theSelected.front();
124 const TopoDS_Shape& aShape = aPrs.shape();
125 if (!aShape.IsNull()) // the point is selected
127 if (aShape.ShapeType() == TopAbs_VERTEX) {
128 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
129 if (!aVertex.IsNull()) {
130 aPoint = BRep_Tool::Pnt(aVertex);
131 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
133 PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(),
137 else if (aShape.ShapeType() == TopAbs_EDGE) // the line is selected
139 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
140 // move to selected line
141 if (feature()->getKind() == SketchPlugin_Line::ID()) {
142 //FeaturePtr aFeature = aPrs.feature();
143 //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
149 /* TODO if (!theSelected.empty()) {
150 ModuleBase_ViewerPrs aPrs = theSelected.front();
151 aFeature = aPrs.object();
153 aFeature = feature(); // for the widget distance only
155 bool isApplyed = setWidgetValue(aFeature, aX, anY);
158 emit activateNextWidget(myActiveWidget);
162 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
164 if (canBeCommitted()) {
166 restartOperation(feature()->getKind(), feature());
170 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
171 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
172 setWidgetValue(feature(), aX, anY);
177 void PartSet_OperationFeatureCreate::keyReleased(std::string theName, QKeyEvent* theEvent)
179 int aKeyType = theEvent->key();
180 // the second point should be activated by any modification in the property panel
181 if (!theName.empty())
183 //setPointSelectionMode(myFeaturePrs->getNextMode(theName), false);
185 keyReleased(theEvent->key());
188 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
190 myActiveWidget = theWidget;
191 if ((myPreSelection.size() > 0) && myActiveWidget) {
192 const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
193 ModuleBase_WidgetValueFeature aValue;
194 aValue.setObject(aPrs.object());
195 if (myActiveWidget->setValue(&aValue)) {
196 myPreSelection.remove(aPrs);
197 emit activateNextWidget(myActiveWidget);
200 if (myInitFeature && myActiveWidget) {
201 ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
202 if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
203 myInitFeature = FeaturePtr();
204 emit activateNextWidget(myActiveWidget);
209 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
212 case Qt::Key_Return: {
213 if (canBeCommitted())
216 // it start a new line creation at a free point
217 restartOperation(feature()->getKind(), FeaturePtr());
221 case Qt::Key_Escape: {
222 if (canBeCommitted())
236 void PartSet_OperationFeatureCreate::startOperation()
238 PartSet_OperationSketchBase::startOperation();
239 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
241 emit multiSelectionEnabled(false);
244 void PartSet_OperationFeatureCreate::abortOperation()
246 emit featureConstructed(feature(), FM_Hide);
247 PartSet_OperationSketchBase::abortOperation();
250 void PartSet_OperationFeatureCreate::stopOperation()
252 PartSet_OperationSketchBase::stopOperation();
253 emit multiSelectionEnabled(true);
256 void PartSet_OperationFeatureCreate::afterCommitOperation()
258 PartSet_OperationSketchBase::afterCommitOperation();
259 emit featureConstructed(feature(), FM_Deactivation);
262 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
264 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
266 boost::shared_ptr<SketchPlugin_Feature> aFeature =
267 boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
269 aFeature->addSub(aNewFeature);
271 //myFeaturePrs->init(aNewFeature);
272 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
274 emit featureConstructed(aNewFeature, FM_Activation);
280 bool PartSet_OperationFeatureCreate::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
282 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
283 aValue->setObject(theFeature);
284 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
285 bool isApplyed = myActiveWidget->setValue(aValue);
288 myIsModified = (myIsModified || isApplyed);