1 // File: PartSet_OperationSketch.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationSketch.h>
7 #include <PartSet_OperationEditFeature.h>
8 #include <PartSet_Tools.h>
10 #include <SketchPlugin_Sketch.h>
12 #include <ModelAPI_Data.h>
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_AttributeRefList.h>
16 #include <GeomAlgoAPI_FaceBuilder.h>
17 #include <GeomDataAPI_Point.h>
18 #include <GeomDataAPI_Dir.h>
19 #include <GeomAPI_XYZ.h>
21 #include <XGUI_ViewerPrs.h>
23 #include <AIS_Shape.hxx>
24 #include <AIS_ListOfInteractive.hxx>
25 #include <AIS_InteractiveObject.hxx>
26 #include <AIS_DimensionOwner.hxx>
27 #include <AIS_LengthDimension.hxx>
28 #include <V3d_View.hxx>
34 #include <QMouseEvent>
38 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
40 : PartSet_OperationSketchBase(theId, theParent)
44 PartSet_OperationSketch::~PartSet_OperationSketch()
48 std::list<int> PartSet_OperationSketch::getSelectionModes(FeaturePtr theFeature) const
50 std::list<int> aModes;
51 if (!hasSketchPlane())
52 aModes.push_back(TopAbs_FACE);
54 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
59 void PartSet_OperationSketch::init(FeaturePtr theFeature,
60 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
61 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
63 setFeature(theFeature);
66 FeaturePtr PartSet_OperationSketch::sketch() const
71 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
72 const std::list<XGUI_ViewerPrs>& theSelected,
73 const std::list<XGUI_ViewerPrs>& theHighlighted)
75 if (!hasSketchPlane()) {
76 if (!theHighlighted.empty()) {
77 XGUI_ViewerPrs aPrs = theHighlighted.front();
78 const TopoDS_Shape& aShape = aPrs.shape();
80 setSketchPlane(aShape);
84 // if shift button is pressed and there are some already selected objects, the operation should
85 // not be started. We just want to combine some selected objects.
86 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
87 if (aHasShift && theSelected.size() > 0)
90 if (theHighlighted.size() == 1) {
91 FeaturePtr aFeature = theHighlighted.front().feature();
93 restartOperation(PartSet_OperationEditFeature::Type(), aFeature);
96 myFeatures = theHighlighted;
101 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
102 const std::list<XGUI_ViewerPrs>& theSelected,
103 const std::list<XGUI_ViewerPrs>& theHighlighted)
105 if (!hasSketchPlane()) {
108 if (!theSelected.empty()) {
109 XGUI_ViewerPrs aPrs = theSelected.front();
110 if (!aPrs.owner().IsNull()) {
111 Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(aPrs.owner());
112 if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
113 Handle(SelectMgr_SelectableObject) anObject = anOwner->Selectable();
115 if (!anObject.IsNull()) {
116 Handle(AIS_LengthDimension) aLenDim = Handle(AIS_LengthDimension)::DownCast(anObject);
117 if (!aLenDim.IsNull())
118 aValue = aLenDim->GetValue();
121 QLineEdit* aLine = new QLineEdit();
122 QPoint aViewPos = theEvent->globalPos();
123 QPoint aLinePos(aViewPos.x(), aViewPos.y());
124 aLine->move(aLinePos);
125 aLine->setText(QString::number(aValue));
133 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
135 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
138 if (myFeatures.size() != 1) {
139 FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(),
140 theView, feature(), myFeatures);
142 restartOperation(PartSet_OperationEditFeature::Type(), aFeature);
146 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >
147 PartSet_OperationSketch::subPreview() const
149 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
151 boost::shared_ptr<SketchPlugin_Feature> aFeature;
153 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
154 if (!aData->isValid())
156 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
157 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
159 std::list<FeaturePtr > aFeatures = aRefList->list();
160 std::list<FeaturePtr >::const_iterator anIt = aFeatures.begin(),
161 aLast = aFeatures.end();
162 for (; anIt != aLast; anIt++) {
163 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
164 boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
166 aPreviewMap[aFeature] = aPreview;
171 void PartSet_OperationSketch::stopOperation()
173 PartSet_OperationSketchBase::stopOperation();
174 emit featureConstructed(feature(), FM_Hide);
175 emit closeLocalContext();
178 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
180 return hasSketchPlane();
183 void PartSet_OperationSketch::startOperation()
186 setFeature(createFeature());
191 bool PartSet_OperationSketch::hasSketchPlane() const
193 bool aHasPlane = false;
196 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
197 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
198 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
199 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
200 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
205 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
207 if (theShape.IsNull())
210 // get selected shape
211 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
212 aGShape->setImpl(new TopoDS_Shape(theShape));
214 // get plane parameters
215 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
217 // set plane parameters to feature
218 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
219 double anA, aB, aC, aD;
220 aPlane->coefficients(anA, aB, aC, aD);
222 // calculate attributes of the sketch
223 boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
224 boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
225 boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
226 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
227 boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
228 // X axis is preferable to be dirX on the sketch
229 const double tol = Precision::Confusion();
230 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
231 boost::shared_ptr<GeomAPI_Dir> aTempDir(isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
232 boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
233 boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
235 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
236 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
237 anOrigin->setValue(anOrigPnt);
238 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
239 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
240 aNormal->setValue(aNormDir);
241 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
242 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
243 aDirX->setValue(aXDir);
244 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
245 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
246 aDirY->setValue(aYDir);
247 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
251 emit featureConstructed(feature(), FM_Hide);
252 emit closeLocalContext();
253 emit planeSelected(aDir->x(), aDir->y(), aDir->z());