]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_OperationFeatureCreate.cpp
Salome HOME
New constraint "Rigid" was added
[modules/shaper.git] / src / PartSet / PartSet_OperationFeatureCreate.cpp
1 // File:        PartSet_OperationFeatureCreate.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationFeatureCreate.h>
6
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
9
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>
21 #include <SketchPlugin_ConstraintRigid.h>
22
23 #include <GeomAPI_Pnt2d.h>
24
25 #include <ModuleBase_OperationDescription.h>
26 #include <ModuleBase_WidgetPoint2D.h>
27 #include <ModuleBase_WidgetValueFeature.h>
28 #include <ModuleBase_ViewerPrs.h>
29 #include <ModuleBase_IPropertyPanel.h>
30
31 #include <XGUI_Constants.h>
32
33 #include <V3d_View.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopoDS.hxx>
36 #include <BRep_Tool.hxx>
37
38 #ifdef _DEBUG
39 #include <QDebug>
40 #include <iostream>
41 #endif
42
43 #include <QMouseEvent>
44
45 using namespace std;
46
47 PartSet_OperationFeatureCreate::PartSet_OperationFeatureCreate(const QString& theId,
48                                                                QObject* theParent,
49                                                                FeaturePtr theFeature)
50     : PartSet_OperationFeatureBase(theId, theParent, theFeature)
51 {
52 }
53
54 PartSet_OperationFeatureCreate::~PartSet_OperationFeatureCreate()
55 {
56 }
57
58 bool PartSet_OperationFeatureCreate::canProcessKind(const std::string& theId)
59 {
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()
68       || theId == SketchPlugin_ConstraintRigid::ID();
69 }
70
71 bool PartSet_OperationFeatureCreate::canBeCommitted() const
72 {
73   if (PartSet_OperationSketchBase::canBeCommitted()) {
74     //if(myActiveWidget && !myActiveWidget->isComputedDefault()) {
75     return isValid();
76   }
77   return false;
78 }
79
80 std::list<int> PartSet_OperationFeatureCreate::getSelectionModes(ObjectPtr theFeature) const
81 {
82   std::list<int> aModes;
83   if (theFeature != feature())
84     aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
85   return aModes;
86 }
87
88 void PartSet_OperationFeatureCreate::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
89 {
90     double aX, anY;
91     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
92     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
93     setWidgetValue(feature(), aX, anY);
94     flushUpdated();
95 }
96
97 void PartSet_OperationFeatureCreate::keyReleased(const int theKey)
98 {
99   switch (theKey) {
100     case Qt::Key_Return:
101     case Qt::Key_Enter: {
102         // it start a new line creation at a free point
103       if(isValid())
104         restartOperation(feature()->getKind());
105     }
106       break;
107     default:
108       break;
109   }
110 }
111
112 void PartSet_OperationFeatureCreate::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
113                                                  const std::list<ModuleBase_ViewerPrs>& theSelected,
114                                                  const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
115 {
116   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), theView);
117   double aX = aPoint.X(), anY = aPoint.Y();
118   bool isClosedContour = false;
119
120   if (theSelected.empty()) {
121     PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
122   } else {
123     ModuleBase_ViewerPrs aPrs = theSelected.front();
124     const TopoDS_Shape& aShape = aPrs.shape();
125     if (!aShape.IsNull()) {
126       if (aShape.ShapeType() == TopAbs_VERTEX) { // a point is selected
127         const TopoDS_Vertex& aVertex = TopoDS::Vertex(aShape);
128         if (!aVertex.IsNull()) {
129           aPoint = BRep_Tool::Pnt(aVertex);
130           PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
131           ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
132           PartSet_Tools::setConstraints(sketch(), feature(), aActiveWgt->attributeID(), aX, anY);
133           isClosedContour = true;
134         }
135       } else if (aShape.ShapeType() == TopAbs_EDGE) { // a line is selected
136         PartSet_Tools::convertTo2D(aPoint, sketch(), theView, aX, anY);
137       }
138     }
139   }
140   ObjectPtr aFeature;
141   if (!theSelected.empty()) {
142     ModuleBase_ViewerPrs aPrs = theSelected.front();
143     aFeature = aPrs.object();
144   } else {
145     aFeature = feature();  // for the widget distance only
146   }
147
148   bool isApplyed = setWidgetValue(aFeature, aX, anY);
149   if (isApplyed) {
150     flushUpdated();
151     myPropertyPanel->activateNextWidget();
152   }
153
154   if (!myPropertyPanel->activeWidget()) {
155     if(commit() && !isClosedContour) {
156       // if the point creation is finished, the next mouse release should commit the modification
157       // the next release can happens by double click in the viewer
158       restartOperation(feature()->getKind(), feature());
159     }
160   }
161 }
162
163 void PartSet_OperationFeatureCreate::startOperation()
164 {
165   PartSet_OperationSketchBase::startOperation();
166   emit multiSelectionEnabled(false);
167 }
168
169 void PartSet_OperationFeatureCreate::abortOperation()
170 {
171   emit featureConstructed(feature(), FM_Hide);
172   PartSet_OperationSketchBase::abortOperation();
173 }
174
175 void PartSet_OperationFeatureCreate::stopOperation()
176 {
177   PartSet_OperationSketchBase::stopOperation();
178   emit multiSelectionEnabled(true);
179 }
180
181 void PartSet_OperationFeatureCreate::afterCommitOperation()
182 {
183   PartSet_OperationSketchBase::afterCommitOperation();
184   emit featureConstructed(feature(), FM_Deactivation);
185 }
186
187 FeaturePtr PartSet_OperationFeatureCreate::createFeature(const bool theFlushMessage)
188 {
189   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
190   if (sketch()) {
191     boost::shared_ptr<SketchPlugin_Feature> aFeature = boost::dynamic_pointer_cast<
192         SketchPlugin_Feature>(sketch());
193
194     aFeature->addSub(aNewFeature);
195   }
196
197   if (theFlushMessage)
198     flushCreated();
199   return aNewFeature;
200 }
201
202
203 void PartSet_OperationFeatureCreate::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
204 {
205   PartSet_OperationFeatureBase::onWidgetActivated(theWidget);
206   if (myInitFeature && theWidget) {
207     ModuleBase_WidgetPoint2D* aWgt = dynamic_cast<ModuleBase_WidgetPoint2D*>(theWidget);
208     if (aWgt && aWgt->initFromPrevious(myInitFeature)) {
209       myInitFeature = FeaturePtr();
210       if (myPropertyPanel)
211         myPropertyPanel->activateNextWidget();
212     }
213   }
214 }