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>
41 #include <QMouseEvent>
45 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
47 FeaturePtr theFeature)
48 : PartSet_OperationSketchBase(theId, theParent),
54 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
58 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
60 return theId == SketchPlugin_Line::ID() || theId == SketchPlugin_Point::ID()
61 || theId == SketchPlugin_Circle::ID() || theId == SketchPlugin_Arc::ID()
62 || theId == SketchPlugin_ConstraintDistance::ID()
63 || theId == SketchPlugin_ConstraintLength::ID()
64 || theId == SketchPlugin_ConstraintRadius::ID()
65 || theId == SketchPlugin_ConstraintParallel::ID()
66 || theId == SketchPlugin_ConstraintPerpendicular::ID()
67 || theId == SketchPlugin_ConstraintCoincidence::ID();
70 bool PartSet_OperationFeatureCreate::canBeCommitted() const
72 if (PartSet_OperationSketchBase::canBeCommitted())
73 return !myActiveWidget;
77 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(ObjectPtr theFeature) const
79 std::list<int> aModes;
80 if (theFeature != feature())
81 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
85 void PartSet_OperationFeatureCreate::initSelection(
86 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;
97 FeaturePtr PartSet_OperationFeatureCreate::sketch() const
102 void PartSet_OperationFeatureCreate::mouseReleased(
103 QMouseEvent* theEvent, Handle(V3d_View) theView,
104 const std::list<ModuleBase_ViewerPrs>& theSelected,
105 const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
107 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
108 double aX = aPoint.X(), anY = aPoint.Y();
109 bool isClosedContour = false;
111 if (theSelected.empty()) {
112 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
114 ModuleBase_ViewerPrs aPrs = theSelected.front();
115 const TopoDS_Shape& aShape = aPrs.shape();
116 if (!aShape.IsNull())
118 if (aShape.ShapeType() == TopAbs_VERTEX) { // a point is selected
119 const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
120 if (!aVertex.IsNull()) {
121 aPoint = BRep_Tool::Pnt(aVertex);
122 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
123 PartSet_Tools::setConstraints(sketch(), feature(), myActiveWidget->attributeID(), aX, anY);
124 isClosedContour = true;
126 } else if (aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
127 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
128 // move to selected line
129 if (feature()->getKind() == SketchPlugin_Line::ID()) {
130 //FeaturePtr aFeature = aPrs.feature();
131 //projectPointOnLine(aFeature, myPointSelectionMode, aPoint, theView, aX, anY);
137 if (!theSelected.empty()) {
138 ModuleBase_ViewerPrs aPrs = theSelected.front();
139 aFeature = aPrs.object();
141 aFeature = feature(); // for the widget distance only
143 bool isApplyed = setWidgetValue(aFeature, aX, anY);
146 emit activateNextWidget(myActiveWidget);
149 if (commit() && !isClosedContour) {
150 // if the point creation is finished, the next mouse release should commit the modification
151 // the next release can happens by double click in the viewer
152 restartOperation(feature()->getKind(), feature());
157 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
160 gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
161 PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
162 setWidgetValue(feature(), aX, anY);
166 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
168 myActiveWidget = theWidget;
169 if ((myPreSelection.size() > 0) && myActiveWidget) {
170 const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
171 ModuleBase_WidgetValueFeature aValue;
172 aValue.setObject(aPrs.object());
173 if (myActiveWidget->setValue(&aValue)) {
174 myPreSelection.remove(aPrs);
175 emit activateNextWidget(myActiveWidget);
178 if (myInitFeature && myActiveWidget) {
179 ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(myActiveWidget);
180 if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
181 myInitFeature = FeaturePtr();
182 emit activateNextWidget(myActiveWidget);
187 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
191 case Qt::Key_Enter: {
193 // it start a new line creation at a free point
194 restartOperation(feature()->getKind());
203 void PartSet_OperationFeatureCreate::activateNextToCurrentWidget()
205 emit activateNextWidget(myActiveWidget);
208 void PartSet_OperationFeatureCreate::startOperation()
210 PartSet_OperationSketchBase::startOperation();
211 //setPointSelectionMode(!myInitFeature ? SM_FirstPoint : SM_SecondPoint);
213 emit multiSelectionEnabled(false);
216 void PartSet_OperationFeatureCreate::abortOperation()
218 emit featureConstructed(feature(), FM_Hide);
219 PartSet_OperationSketchBase::abortOperation();
222 void PartSet_OperationFeatureCreate::stopOperation()
224 PartSet_OperationSketchBase::stopOperation();
225 emit multiSelectionEnabled(true);
228 void PartSet_OperationFeatureCreate::afterCommitOperation()
230 PartSet_OperationSketchBase::afterCommitOperation();
231 emit featureConstructed(feature(), FM_Deactivation);
234 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
236 FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
238 boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
239 SketchPlugin_Feature>(sketch());
241 aFeature->addSub(aNewFeature);
243 //myFeaturePrs->init(aNewFeature);
244 //myFeaturePrs->setFeature(myInitFeature, SM_FirstPoint);
246 //TODO emit featureConstructed(aNewFeature, FM_Activation);
252 bool PartSet_OperationFeatureCreate::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
256 ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
257 aValue->setObject(theFeature);
258 aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
259 bool isApplyed = myActiveWidget->setValue(aValue);
262 myIsModified = (myIsModified || isApplyed);