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>
18 #include <GeomAlgoAPI_FaceBuilder.h>
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Dir.h>
21 #include <GeomAPI_XYZ.h>
23 #include <XGUI_ViewerPrs.h>
25 #include <AIS_Shape.hxx>
26 #include <AIS_ListOfInteractive.hxx>
27 #include <AIS_InteractiveObject.hxx>
28 #include <AIS_DimensionOwner.hxx>
29 #include <AIS_LengthDimension.hxx>
30 #include <V3d_View.hxx>
36 #include <QMouseEvent>
40 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
42 : PartSet_OperationSketchBase(theId, theParent)
46 PartSet_OperationSketch::~PartSet_OperationSketch()
50 std::list<int> PartSet_OperationSketch::getSelectionModes(FeaturePtr theFeature) const
52 std::list<int> aModes;
53 if (!hasSketchPlane())
54 aModes.push_back(TopAbs_FACE);
56 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
61 void PartSet_OperationSketch::init(FeaturePtr theFeature,
62 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
63 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
66 setEditingFeature(theFeature);
69 FeaturePtr PartSet_OperationSketch::sketch() const
74 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
75 const std::list<XGUI_ViewerPrs>& theSelected,
76 const std::list<XGUI_ViewerPrs>& theHighlighted)
78 if (!hasSketchPlane()) {
79 if (!theHighlighted.empty()) {
80 XGUI_ViewerPrs aPrs = theHighlighted.front();
81 const TopoDS_Shape& aShape = aPrs.shape();
83 setSketchPlane(aShape);
87 // if shift button is pressed and there are some already selected objects, the operation should
88 // not be started. We just want to combine some selected objects.
89 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
90 if (aHasShift && theSelected.size() > 0)
93 if (theHighlighted.size() == 1) {
94 FeaturePtr aFeature = theHighlighted.front().feature();
96 std::string anOperationType = getOperationType(aFeature);
97 if (theSelected.size() > 1)
98 anOperationType = PartSet_OperationFeatureEditMulti::Type();
99 restartOperation(anOperationType, aFeature);
103 myFeatures = theHighlighted;
108 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
109 const std::list<XGUI_ViewerPrs>& theSelected,
110 const std::list<XGUI_ViewerPrs>& theHighlighted)
112 if (!hasSketchPlane()) {
115 /// TODO: OCC bug: 25034 - the highlighted list should be filled not only for AIS_Shape
116 /// but for other IO, for example constraint dimensions.
117 /// It is empty and we have to use the process mouse release to start edition operation
118 /// for these objects
119 if (theSelected.size() == 1) {
120 FeaturePtr aFeature = theSelected.front().feature();
122 restartOperation(getOperationType(aFeature), aFeature);
127 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
129 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
132 if (myFeatures.size() != 1) {
133 FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), theView, feature(),
136 restartOperation(PartSet_OperationFeatureEditMulti::Type(), aFeature);
140 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >
141 PartSet_OperationSketch::subPreview() const
143 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
145 boost::shared_ptr<SketchPlugin_Feature> aFeature;
147 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
148 if (!aData->isValid())
150 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
151 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
153 std::list<FeaturePtr > aFeatures = aRefList->list();
154 std::list<FeaturePtr >::const_iterator anIt = aFeatures.begin(),
155 aLast = aFeatures.end();
156 for (; anIt != aLast; anIt++) {
157 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
158 boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
160 aPreviewMap[aFeature] = aPreview;
165 void PartSet_OperationSketch::stopOperation()
167 PartSet_OperationSketchBase::stopOperation();
168 emit featureConstructed(feature(), FM_Hide);
169 emit closeLocalContext();
172 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
174 return hasSketchPlane();
177 void PartSet_OperationSketch::startOperation()
179 PartSet_OperationSketchBase::startOperation();
180 if (!isEditOperation())
184 bool PartSet_OperationSketch::hasSketchPlane() const
186 bool aHasPlane = false;
189 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
190 AttributeDoublePtr anAttr;
191 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
192 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
193 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
198 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
200 if (theShape.IsNull())
203 // get selected shape
204 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
205 aGShape->setImpl(new TopoDS_Shape(theShape));
207 // get plane parameters
208 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
210 // set plane parameters to feature
211 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
212 double anA, aB, aC, aD;
213 aPlane->coefficients(anA, aB, aC, aD);
215 // calculate attributes of the sketch
216 boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
217 boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
218 boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
219 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
220 boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
221 // X axis is preferable to be dirX on the sketch
222 const double tol = Precision::Confusion();
223 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
224 boost::shared_ptr<GeomAPI_Dir> aTempDir(isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
225 boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
226 boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
228 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
229 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
230 anOrigin->setValue(anOrigPnt);
231 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
232 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
233 aNormal->setValue(aNormDir);
234 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
235 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
236 aDirX->setValue(aXDir);
237 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
238 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
239 aDirY->setValue(aYDir);
240 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
244 emit featureConstructed(feature(), FM_Hide);
245 emit closeLocalContext();
246 emit planeSelected(aDir->x(), aDir->y(), aDir->z());
249 std::string PartSet_OperationSketch::getOperationType(FeaturePtr theFeature)
251 return PartSet_OperationFeatureEdit::Type();