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_OperationFeatureEditMulti.h>
9 #include <PartSet_Tools.h>
11 #include <SketchPlugin_Sketch.h>
12 #include <SketchPlugin_ConstraintLength.h>
14 #include <ModelAPI_Data.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeRefList.h>
17 #include <ModelAPI_Events.h>
19 #include <GeomAlgoAPI_FaceBuilder.h>
20 #include <GeomDataAPI_Point.h>
21 #include <GeomDataAPI_Dir.h>
22 #include <GeomAPI_XYZ.h>
24 #include <ModuleBase_ViewerPrs.h>
25 #include <ModuleBase_ISelection.h>
26 #include <ModuleBase_IViewer.h>
27 #include <Events_Loop.h>
29 #include <AIS_Shape.hxx>
30 #include <AIS_ListOfInteractive.hxx>
31 #include <AIS_InteractiveObject.hxx>
32 #include <AIS_DimensionOwner.hxx>
33 #include <AIS_LengthDimension.hxx>
34 #include <V3d_View.hxx>
40 #include <QMouseEvent>
44 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId, QObject* theParent)
45 : PartSet_OperationSketchBase(theId, theParent)
49 PartSet_OperationSketch::~PartSet_OperationSketch()
53 CompositeFeaturePtr PartSet_OperationSketch::sketch() const
55 return boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(feature());
58 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer, ModuleBase_ISelection* theSelection)
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 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
65 QList<ModuleBase_ViewerPrs> aHighlighted = theSelection->getHighlighted();
66 //if (aHasShift && (aSelected.size() > 0)) {
67 foreach(ModuleBase_ViewerPrs aPrs, aHighlighted)
68 aSelected.append(aPrs);
70 //if (aHasShift && aSelected.size() > 0)
73 if (aSelected.size() > 0) {
74 ObjectPtr aFeature = aSelected.first().object();
76 std::string anOperationType =
77 (aSelected.size() > 1) ?
78 PartSet_OperationFeatureEditMulti::Type() : PartSet_OperationFeatureEdit::Type();
79 //theViewer->enableSelection(false);
80 restartOperation(anOperationType, aFeature);
83 //myFeatures = aSelected;
89 void PartSet_OperationSketch::selectionChanged(ModuleBase_ISelection* theSelection)
94 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
95 if (!aSelected.empty()) {
96 ModuleBase_ViewerPrs aPrs = aSelected.first();
97 const TopoDS_Shape& aShape = aPrs.shape();
99 setSketchPlane(aShape);
104 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer,
105 ModuleBase_ISelection* theSelection)
107 QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
108 if (hasSketchPlane()) {
109 /// TODO: OCC bug: 25034 - the highlighted list should be filled not only for AIS_Shape
110 /// but for other IO, for example constraint dimensions.
111 /// It is empty and we have to use the process mouse release to start edition operation
112 /// for these objects
113 if (aSelected.size() == 1) {
114 ObjectPtr aObject = aSelected.first().object();
116 restartOperation(PartSet_OperationFeatureEdit::Type(), aObject);
122 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, ModuleBase_IViewer* theViewer)
124 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
127 if (myFeatures.size() != 1) {
128 Handle(V3d_View) aView = theViewer->activeView();
129 FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), aView, feature(),
132 restartOperation(PartSet_OperationFeatureEditMulti::Type(), aFeature);
136 std::list<FeaturePtr> PartSet_OperationSketch::subFeatures() const
138 std::list<FeaturePtr> aFeaList;
139 FeaturePtr aFeature = feature();
143 boost::shared_ptr<ModelAPI_Data> aData = aFeature->data();
144 if (!aData->isValid())
145 return std::list<FeaturePtr>();
146 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
147 ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
149 std::list<ObjectPtr> aList = aRefList->list();
150 std::list<ObjectPtr>::iterator aIt;
151 for (aIt = aList.begin(); aIt != aList.end(); ++aIt) {
152 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*aIt);
154 aFeaList.push_back(aFeature);
159 void PartSet_OperationSketch::stopOperation()
161 PartSet_OperationSketchBase::stopOperation();
162 emit featureConstructed(feature(), FM_Hide);
165 void PartSet_OperationSketch::afterCommitOperation()
167 FeaturePtr aFeature = feature();
168 std::list<ResultPtr> aResults = aFeature->results();
169 std::list<ResultPtr>::const_iterator aIt;
170 Events_ID anEvent = Events_Loop::loop()->eventByName(EVENT_OBJECT_TOSHOW);
171 for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
172 ModelAPI_EventCreator::get()->sendUpdated(*aIt, anEvent);
174 Events_Loop::loop()->flush(anEvent);
177 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
179 return hasSketchPlane();
182 void PartSet_OperationSketch::startOperation()
184 PartSet_OperationSketchBase::startOperation();
185 if (!isEditOperation())
189 bool PartSet_OperationSketch::hasSketchPlane() const
191 bool aHasPlane = false;
194 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
195 AttributeDoublePtr anAttr;
196 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
197 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
198 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
203 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
205 if (theShape.IsNull())
208 // get selected shape
209 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
210 aGShape->setImpl(new TopoDS_Shape(theShape));
212 // get plane parameters
213 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
215 // set plane parameters to feature
216 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
217 double anA, aB, aC, aD;
218 aPlane->coefficients(anA, aB, aC, aD);
220 // calculate attributes of the sketch
221 boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
222 boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
223 boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
224 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
225 boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
226 // X axis is preferable to be dirX on the sketch
227 const double tol = Precision::Confusion();
228 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
229 boost::shared_ptr<GeomAPI_Dir> aTempDir(
230 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
231 boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
232 boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
234 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
235 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
236 anOrigin->setValue(anOrigPnt);
237 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
238 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
239 aNormal->setValue(aNormDir);
240 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
241 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
242 aDirX->setValue(aXDir);
243 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
244 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
245 aDirY->setValue(aYDir);
246 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
250 emit featureConstructed(feature(), FM_Hide);
251 emit planeSelected(aDir->x(), aDir->y(), aDir->z());
255 bool PartSet_OperationSketch::isGranted(ModuleBase_Operation* theOperation) const
257 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
258 return aPreviewOp != NULL;