1 // File: PartSet_OperationSketch.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationSketch.h>
7 #include <PartSet_OperationFeatureEdit.h>
8 #include <PartSet_Tools.h>
10 #include <SketchPlugin_Sketch.h>
11 #include <SketchPlugin_ConstraintLength.h>
13 #include <ModelAPI_Data.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeRefList.h>
16 #include <ModelAPI_Events.h>
18 #include <GeomAlgoAPI_FaceBuilder.h>
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Dir.h>
21 #include <GeomAPI_XYZ.h>
23 #include <ModuleBase_ViewerPrs.h>
24 #include <ModuleBase_ISelection.h>
25 #include <ModuleBase_IViewer.h>
26 #include <Events_Loop.h>
28 #include <AIS_Shape.hxx>
29 #include <AIS_ListOfInteractive.hxx>
30 #include <AIS_InteractiveObject.hxx>
31 #include <AIS_DimensionOwner.hxx>
32 #include <AIS_LengthDimension.hxx>
33 #include <V3d_View.hxx>
39 #include <QMouseEvent>
43 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId, QObject* theParent)
44 : PartSet_OperationSketchBase(theId, theParent)
48 PartSet_OperationSketch::~PartSet_OperationSketch()
52 CompositeFeaturePtr PartSet_OperationSketch::sketch() const
54 return boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
57 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer, ModuleBase_ISelection* theSelection)
59 if (hasSketchPlane()) {
60 // if shift button is pressed and there are some already selected objects, the operation should
61 // not be started. We just want to combine some selected objects.
62 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
63 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
64 QList<ModuleBase_ViewerPrs> aHighlighted = theSelection->getHighlighted();
65 //if (aHasShift && (aSelected.size() > 0)) {
66 foreach(ModuleBase_ViewerPrs aPrs, aHighlighted)
67 aSelected.append(aPrs);
69 //if (aHasShift && aSelected.size() > 0)
72 // there should be a start of operation, which uses the pre-highlighted objects,
73 // the selected ones are collected here and are processed by a mouse move
74 if (aHighlighted.size() == 1) {
75 //if (aSelected.size() > 0) {
76 ObjectPtr aFeature = aSelected.first().object();
78 std::string anOperationType = PartSet_OperationFeatureEdit::Type();
79 restartOperation(anOperationType, aFeature);
83 myFeatures = aHighlighted;
85 //myFeatures = aSelected;
91 void PartSet_OperationSketch::selectionChanged(ModuleBase_ISelection* theSelection)
96 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
97 if (!aSelected.empty()) {
98 ModuleBase_ViewerPrs aPrs = aSelected.first();
99 // We have to select a plane before any operation
100 TopoDS_Shape aShape = aPrs.shape();
101 if (!aShape.IsNull()) {
102 boost::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
104 emit featureConstructed(feature(), FM_Hide);
105 // If selection is not a sketcher presentation then it has to be stored as
107 if (feature() != aPrs.object()) {
108 //boost::shared_ptr<SketchPlugin_Sketch> aSketch =
109 // boost::dynamic_pointer_cast<SketchPlugin_Sketch>(feature());
110 DataPtr aData = feature()->data();
111 AttributeSelectionPtr aSelAttr =
112 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
113 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
115 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
117 GeomShapePtr aShapePtr(new GeomAPI_Shape());
118 aShapePtr->setImpl(new TopoDS_Shape(aShape));
119 aSelAttr->setValue(aRes, aShapePtr);
123 // Turn viewer to the plane
124 emit planeSelected(aDir->x(), aDir->y(), aDir->z());
132 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
133 ModuleBase_ISelection* theSelection)
135 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
136 if (hasSketchPlane()) {
137 /// TODO: OCC bug: 25034 - the highlighted list should be filled not only for AIS_Shape
138 /// but for other IO, for example constraint dimensions.
139 /// It is empty and we have to use the process mouse release to start edition operation
140 /// for these objects
141 if (aSelected.size() == 1) {
142 ObjectPtr aObject = aSelected.first().object();
144 restartOperation(PartSet_OperationFeatureEdit::Type(), aObject);
150 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
152 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
155 if (myFeatures.size() != 1) {
156 Handle(V3d_View) aView = theViewer->activeView();
157 FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), aView, feature(),
160 restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
164 std::list<FeaturePtr> PartSet_OperationSketch::subFeatures() const
166 std::list<FeaturePtr> aFeaList;
167 FeaturePtr aFeature = feature();
171 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
172 if (!aData->isValid())
173 return std::list<FeaturePtr>();
174 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
175 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
177 std::list<ObjectPtr> aList = aRefList->list();
178 std::list<ObjectPtr>::iterator aIt;
179 for (aIt = aList.begin(); aIt != aList.end(); ++aIt) {
180 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*aIt);
182 aFeaList.push_back(aFeature);
187 void PartSet_OperationSketch::stopOperation()
189 PartSet_OperationSketchBase::stopOperation();
190 emit featureConstructed(feature(), FM_Hide);
193 void PartSet_OperationSketch::afterCommitOperation()
195 FeaturePtr aFeature = feature();
196 std::list<ResultPtr> aResults = aFeature->results();
197 std::list<ResultPtr>::const_iterator aIt;
198 Events_ID anEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW);
199 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
200 ModelAPI_EventCreator::get()->sendUpdated(*aIt, anEvent);
202 Events_Loop::loop()->flush(anEvent);
205 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
207 return hasSketchPlane();
210 void PartSet_OperationSketch::startOperation()
212 PartSet_OperationSketchBase::startOperation();
213 if (!isEditOperation())
217 bool PartSet_OperationSketch::hasSketchPlane() const
219 bool aHasPlane = false;
222 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
223 AttributeDoublePtr anAttr;
224 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
225 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
226 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
231 boost::shared_ptr<GeomAPI_Dir> PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
233 if (theShape.IsNull())
234 return boost::shared_ptr<GeomAPI_Dir>();
236 // get selected shape
237 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
238 aGShape->setImpl(new TopoDS_Shape(theShape));
240 // get plane parameters
241 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
243 // set plane parameters to feature
244 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
245 double anA, aB, aC, aD;
246 aPlane->coefficients(anA, aB, aC, aD);
248 // calculate attributes of the sketch
249 boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
250 boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
251 boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
252 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
253 boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
254 // X axis is preferable to be dirX on the sketch
255 const double tol = Precision::Confusion();
256 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
257 boost::shared_ptr<GeomAPI_Dir> aTempDir(
258 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
259 boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
260 boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
262 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
263 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
264 anOrigin->setValue(anOrigPnt);
265 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
266 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
267 aNormal->setValue(aNormDir);
268 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
269 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
270 aDirX->setValue(aXDir);
271 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
272 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
273 aDirY->setValue(aYDir);
274 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
279 bool PartSet_OperationSketch::isGranted(ModuleBase_Operation* theOperation) const
281 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
282 return aPreviewOp != NULL;