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 // commented: the next code is commented because the nearestFeature check the highlighting
66 // and selection inside
67 //if (aHasShift && (aSelected.size() > 0)) {
68 // foreach(ModuleBase_ViewerPrs aPrs, aHighlighted)
69 // aSelected.append(aPrs);
71 //if (aHasShift && aSelected.size() > 0)
74 // there should be a start of operation, which uses the pre-highlighted objects,
75 // the selected ones are collected here and are processed by a mouse move
76 //if (aHighlighted.size() == 1) {
77 //if (aSelected.size() > 0) {
78 // ObjectPtr aFeature = aSelected.first().object();
81 Handle(V3d_View) aView = theViewer->activeView();
82 ObjectPtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), aView, feature(),
83 aSelected, aHighlighted);
85 restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
89 // myFeatures = aHighlighted;
91 //myFeatures = aSelected;
96 void PartSet_OperationSketch::selectionChanged(ModuleBase_ISelection* theSelection)
101 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
102 if (!aSelected.empty()) {
103 ModuleBase_ViewerPrs aPrs = aSelected.first();
104 // We have to select a plane before any operation
105 TopoDS_Shape aShape = aPrs.shape();
106 if (!aShape.IsNull()) {
107 boost::shared_ptr<GeomAPI_Dir> aDir = setSketchPlane(aShape);
109 emit featureConstructed(feature(), FM_Hide);
110 // If selection is not a sketcher presentation then it has to be stored as
112 if (feature() != aPrs.object()) {
113 //boost::shared_ptr<SketchPlugin_Sketch> aSketch =
114 // boost::dynamic_pointer_cast<SketchPlugin_Sketch>(feature());
115 DataPtr aData = feature()->data();
116 AttributeSelectionPtr aSelAttr =
117 boost::dynamic_pointer_cast<ModelAPI_AttributeSelection>
118 (aData->attribute(SketchPlugin_Feature::EXTERNAL_ID()));
120 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
122 GeomShapePtr aShapePtr(new GeomAPI_Shape());
123 aShapePtr->setImpl(new TopoDS_Shape(aShape));
124 aSelAttr->setValue(aRes, aShapePtr);
128 // Turn viewer to the plane
129 emit planeSelected(aDir->x(), aDir->y(), aDir->z());
137 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
138 ModuleBase_ISelection* theSelection)
140 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
141 if (hasSketchPlane()) {
142 /// TODO: OCC bug: 25034 - the highlighted list should be filled not only for AIS_Shape
143 /// but for other IO, for example constraint dimensions.
144 /// It is empty and we have to use the process mouse release to start edition operation
145 /// for these objects
146 if (aSelected.size() == 1) {
147 ObjectPtr aObject = aSelected.first().object();
149 restartOperation(PartSet_OperationFeatureEdit::Type(), aObject);
155 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
157 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
160 // myFeatures are not filled in the previous realization, so, this code is just commented
161 // because has no effect
162 /*if (myFeatures.size() != 1) {
163 Handle(V3d_View) aView = theViewer->activeView();
164 ObjectPtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), aView, feature(),
167 restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
171 std::list<FeaturePtr> PartSet_OperationSketch::subFeatures() const
173 std::list<FeaturePtr> aFeaList;
174 FeaturePtr aFeature = feature();
178 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
179 if (!aData->isValid())
180 return std::list<FeaturePtr>();
181 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
182 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
184 std::list<ObjectPtr> aList = aRefList->list();
185 std::list<ObjectPtr>::iterator aIt;
186 for (aIt = aList.begin(); aIt != aList.end(); ++aIt) {
187 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*aIt);
189 aFeaList.push_back(aFeature);
194 void PartSet_OperationSketch::stopOperation()
196 PartSet_OperationSketchBase::stopOperation();
197 emit featureConstructed(feature(), FM_Hide);
200 void PartSet_OperationSketch::afterCommitOperation()
202 FeaturePtr aFeature = feature();
203 std::list<ResultPtr> aResults = aFeature->results();
204 std::list<ResultPtr>::const_iterator aIt;
205 Events_ID anEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW);
206 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
207 ModelAPI_EventCreator::get()->sendUpdated(*aIt, anEvent);
209 Events_Loop::loop()->flush(anEvent);
212 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
214 return hasSketchPlane();
217 void PartSet_OperationSketch::startOperation()
219 PartSet_OperationSketchBase::startOperation();
220 if (!isEditOperation())
224 bool PartSet_OperationSketch::hasSketchPlane() const
226 bool aHasPlane = false;
229 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
230 AttributeDoublePtr anAttr;
231 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
232 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
233 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
238 boost::shared_ptr<GeomAPI_Dir> PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
240 if (theShape.IsNull())
241 return boost::shared_ptr<GeomAPI_Dir>();
243 // get selected shape
244 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
245 aGShape->setImpl(new TopoDS_Shape(theShape));
247 // get plane parameters
248 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
250 // set plane parameters to feature
251 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
252 double anA, aB, aC, aD;
253 aPlane->coefficients(anA, aB, aC, aD);
255 // calculate attributes of the sketch
256 boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
257 boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
258 boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
259 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
260 boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
261 // X axis is preferable to be dirX on the sketch
262 const double tol = Precision::Confusion();
263 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
264 boost::shared_ptr<GeomAPI_Dir> aTempDir(
265 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
266 boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
267 boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
269 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
270 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
271 anOrigin->setValue(anOrigPnt);
272 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
273 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
274 aNormal->setValue(aNormDir);
275 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
276 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
277 aDirX->setValue(aXDir);
278 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
279 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
280 aDirY->setValue(aYDir);
281 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
286 bool PartSet_OperationSketch::isGranted(ModuleBase_Operation* theOperation) const
288 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
289 return aPreviewOp != NULL;