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 <ModuleBase_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>
41 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
43 : PartSet_OperationSketchBase(theId, theParent)
47 PartSet_OperationSketch::~PartSet_OperationSketch()
51 std::list<int> PartSet_OperationSketch::getSelectionModes(FeaturePtr theFeature) const
53 std::list<int> aModes;
54 if (!hasSketchPlane())
55 aModes.push_back(TopAbs_FACE);
57 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
63 /// Initializes the operation with previously created feature. It is used in sequental operations
64 void PartSet_OperationSketch::initFeature(FeaturePtr theFeature)
67 setEditingFeature(theFeature);
70 FeaturePtr PartSet_OperationSketch::sketch() const
75 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
76 const std::list<ModuleBase_ViewerPrs>& theSelected,
77 const std::list<ModuleBase_ViewerPrs>& theHighlighted)
79 if (hasSketchPlane()){
80 // if shift button is pressed and there are some already selected objects, the operation should
81 // not be started. We just want to combine some selected objects.
82 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
83 if (aHasShift && theSelected.size() > 0)
86 if (theHighlighted.size() == 1) {
87 ObjectPtr aFeature = theHighlighted.front().object();
89 std::string anOperationType = (theSelected.size() > 1)?
90 PartSet_OperationFeatureEditMulti::Type() :
91 PartSet_OperationFeatureEdit::Type();
92 restartOperation(anOperationType, aFeature);
96 myFeatures = theHighlighted;
101 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
102 const std::list<ModuleBase_ViewerPrs>& theSelected,
103 const std::list<ModuleBase_ViewerPrs>& theHighlighted)
105 if (hasSketchPlane()) {
106 /// TODO: OCC bug: 25034 - the highlighted list should be filled not only for AIS_Shape
107 /// but for other IO, for example constraint dimensions.
108 /// It is empty and we have to use the process mouse release to start edition operation
109 /// for these objects
110 if (theSelected.size() == 1) {
111 ObjectPtr aFeature = theSelected.front().object();
113 restartOperation(PartSet_OperationFeatureEdit::Type(), aFeature);
118 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
120 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
123 if (myFeatures.size() != 1) {
124 FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), theView, feature(),
127 restartOperation(PartSet_OperationFeatureEditMulti::Type(), aFeature);
131 std::list<FeaturePtr> PartSet_OperationSketch::subFeatures() const
133 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
134 if (!aData->isValid())
135 return std::list<FeaturePtr>();
136 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
137 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SketchPlugin_Sketch::FEATURES_ID()));
139 std::list<ObjectPtr> aList = aRefList->list();
140 std::list<ObjectPtr>::iterator aIt;
141 std::list<FeaturePtr> aFeaList;
142 for (aIt = aList.begin(); aIt != aList.end(); ++aIt) {
143 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*aIt);
145 aFeaList.push_back(aFeature);
150 void PartSet_OperationSketch::stopOperation()
152 PartSet_OperationSketchBase::stopOperation();
153 emit featureConstructed(feature(), FM_Hide);
154 emit closeLocalContext();
157 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
159 return hasSketchPlane();
163 void PartSet_OperationSketch::startOperation()
165 PartSet_OperationSketchBase::startOperation();
166 if (!isEditOperation())
170 bool PartSet_OperationSketch::hasSketchPlane() const
172 bool aHasPlane = false;
175 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
176 AttributeDoublePtr anAttr;
177 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
178 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
179 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
184 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
186 if (theShape.IsNull())
189 // get selected shape
190 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
191 aGShape->setImpl(new TopoDS_Shape(theShape));
193 // get plane parameters
194 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
196 // set plane parameters to feature
197 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
198 double anA, aB, aC, aD;
199 aPlane->coefficients(anA, aB, aC, aD);
201 // calculate attributes of the sketch
202 boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
203 boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
204 boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
205 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
206 boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
207 // X axis is preferable to be dirX on the sketch
208 const double tol = Precision::Confusion();
209 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
210 boost::shared_ptr<GeomAPI_Dir> aTempDir(isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
211 boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
212 boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
214 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
215 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
216 anOrigin->setValue(anOrigPnt);
217 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
218 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::NORM_ID()));
219 aNormal->setValue(aNormDir);
220 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
221 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
222 aDirX->setValue(aXDir);
223 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
224 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
225 aDirY->setValue(aYDir);
226 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
230 emit featureConstructed(feature(), FM_Hide);
231 emit closeLocalContext();
232 emit planeSelected(aDir->x(), aDir->y(), aDir->z());