Salome HOME
Composite Feature implementation. Sketch now is Composite.
[modules/shaper.git] / src / PartSet / PartSet_OperationSketch.cpp
1 // File:        PartSet_OperationSketch.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationSketch.h>
6
7 #include <PartSet_OperationFeatureEdit.h>
8 #include <PartSet_OperationFeatureEditMulti.h>
9 #include <PartSet_Tools.h>
10
11 #include <SketchPlugin_Sketch.h>
12 #include <SketchPlugin_ConstraintLength.h>
13
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_Events.h>
18
19 #include <GeomAlgoAPI_FaceBuilder.h>
20 #include <GeomDataAPI_Point.h>
21 #include <GeomDataAPI_Dir.h>
22 #include <GeomAPI_XYZ.h>
23
24 #include <ModuleBase_ViewerPrs.h>
25 #include <Events_Loop.h>
26
27 #include <AIS_Shape.hxx>
28 #include <AIS_ListOfInteractive.hxx>
29 #include <AIS_InteractiveObject.hxx>
30 #include <AIS_DimensionOwner.hxx>
31 #include <AIS_LengthDimension.hxx>
32 #include <V3d_View.hxx>
33
34 #ifdef _DEBUG
35 #include <QDebug>
36 #endif
37
38 #include <QMouseEvent>
39
40 using namespace std;
41
42 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId, QObject* theParent)
43     : PartSet_OperationSketchBase(theId, theParent)
44 {
45 }
46
47 PartSet_OperationSketch::~PartSet_OperationSketch()
48 {
49 }
50
51 CompositeFeaturePtr PartSet_OperationSketch::sketch() const
52 {
53   return boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
54 }
55
56 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
57                                            const std::list<ModuleBase_ViewerPrs>& theSelected,
58                                            const std::list<ModuleBase_ViewerPrs>& theHighlighted)
59 {
60   if (hasSketchPlane()) {
61     // if shift button is pressed and there are some already selected objects, the operation should
62     // not be started. We just want to combine some selected objects.
63     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
64     if (aHasShift && theSelected.size() > 0)
65       return;
66
67     if (theHighlighted.size() == 1) {
68       ObjectPtr aFeature = theHighlighted.front().object();
69       if (aFeature) {
70         std::string anOperationType =
71             (theSelected.size() > 1) ?
72                 PartSet_OperationFeatureEditMulti::Type() : PartSet_OperationFeatureEdit::Type();
73         restartOperation(anOperationType, aFeature);
74       }
75     } else
76       myFeatures = theHighlighted;
77
78   } else {
79     if (!theHighlighted.empty()) {
80       ModuleBase_ViewerPrs aPrs = theHighlighted.front();
81       const TopoDS_Shape& aShape = aPrs.shape();
82       if (!aShape.IsNull())
83         setSketchPlane(aShape);
84     }
85   }
86 }
87
88 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
89                                             const std::list<ModuleBase_ViewerPrs>& theSelected,
90                                             const std::list<ModuleBase_ViewerPrs>& theHighlighted)
91 {
92   if (hasSketchPlane()) {
93     /// TODO: OCC bug: 25034 - the highlighted list should be filled not only for AIS_Shape
94     /// but for other IO, for example constraint dimensions.
95     /// It is empty and we have to use the process mouse release to start edition operation
96     /// for these objects
97     if (theSelected.size() == 1) {
98       ObjectPtr aObject = theSelected.front().object();
99       if (aObject) {
100         restartOperation(PartSet_OperationFeatureEdit::Type(), aObject);
101       }
102     }
103   }
104 }
105
106 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
107 {
108   if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
109     return;
110
111   if (myFeatures.size() != 1) {
112     FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), theView, feature(),
113                                                         myFeatures);
114     if (aFeature)
115       restartOperation(PartSet_OperationFeatureEditMulti::Type(), aFeature);
116   }
117 }
118
119 std::list<FeaturePtr> PartSet_OperationSketch::subFeatures() const
120 {
121   std::list<FeaturePtr> aFeaList;
122   FeaturePtr aFeature = feature();
123   if (!aFeature)
124     return aFeaList;
125
126   boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
127   if (!aData->isValid())
128     return std::list<FeaturePtr>();
129   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
130       ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
131
132   std::list<ObjectPtr> aList = aRefList->list();
133   std::list<ObjectPtr>::iterator aIt;
134   for (aIt = aList.begin(); aIt != aList.end(); ++aIt) {
135     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*aIt);
136     if (aFeature)
137       aFeaList.push_back(aFeature);
138   }
139   return aFeaList;
140 }
141
142 void PartSet_OperationSketch::stopOperation()
143 {
144   PartSet_OperationSketchBase::stopOperation();
145   emit featureConstructed(feature(), FM_Hide);
146 }
147
148 void PartSet_OperationSketch::afterCommitOperation()
149 {
150   FeaturePtr aFeature = feature();
151   std::list<ResultPtr> aResults = aFeature->results();
152   std::list<ResultPtr>::const_iterator aIt;
153   Events_ID anEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW);
154   for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
155     ModelAPI_EventCreator::get()->sendUpdated(*aIt, anEvent);
156   }
157   Events_Loop::loop()->flush(anEvent);
158 }
159
160 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
161 {
162   return hasSketchPlane();
163 }
164
165 void PartSet_OperationSketch::startOperation()
166 {
167   PartSet_OperationSketchBase::startOperation();
168   if (!isEditOperation())
169     emit fitAllView();
170 }
171
172 bool PartSet_OperationSketch::hasSketchPlane() const
173 {
174   bool aHasPlane = false;
175
176   if (feature()) {
177     boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
178     AttributeDoublePtr anAttr;
179     boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
180         aData->attribute(SketchPlugin_Sketch::NORM_ID()));
181     aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
182   }
183   return aHasPlane;
184 }
185
186 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
187 {
188   if (theShape.IsNull())
189     return;
190
191   // get selected shape
192   boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
193   aGShape->setImpl(new TopoDS_Shape(theShape));
194
195   // get plane parameters
196   boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
197
198   // set plane parameters to feature
199   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
200   double anA, aB, aC, aD;
201   aPlane->coefficients(anA, aB, aC, aD);
202
203   // calculate attributes of the sketch
204   boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
205   boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
206   boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
207   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
208   boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
209   // X axis is preferable to be dirX on the sketch
210   const double tol = Precision::Confusion();
211   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
212   boost::shared_ptr<GeomAPI_Dir> aTempDir(
213       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
214   boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
215   boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
216
217   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
218       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
219   anOrigin->setValue(anOrigPnt);
220   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
221       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
222   aNormal->setValue(aNormDir);
223   boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
224       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
225   aDirX->setValue(aXDir);
226   boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
227       aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
228   aDirY->setValue(aYDir);
229   boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
230
231   flushUpdated();
232
233   emit featureConstructed(feature(), FM_Hide);
234   emit planeSelected(aDir->x(), aDir->y(), aDir->z());
235 }
236
237
238 bool PartSet_OperationSketch::isGranted(ModuleBase_Operation* theOperation) const
239 {
240   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
241   return aPreviewOp != NULL;
242 }
243