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));
134 void PartSet_OperationSketch::mouseDoubleClick(QMouseEvent* theEvent, Handle_V3d_View theView,
135 const std::list<XGUI_ViewerPrs>& theSelected,
136 const std::list<XGUI_ViewerPrs>& theHighlighted)
139 if (!hasSketchPlane())
141 if (!theSelected.empty()) {
142 XGUI_ViewerPrs aPrs = theSelected.front();
143 if (!aPrs.owner().IsNull()) {
144 Handle(AIS_DimensionOwner) anOwner = Handle(AIS_DimensionOwner)::DownCast(aPrs.owner());
145 if (!anOwner.IsNull() && anOwner->SelectionMode() == AIS_DSM_Text) {
146 Handle(SelectMgr_SelectableObject) anObject = anOwner->Selectable();
148 if (!anObject.IsNull()) {
149 Handle(AIS_LengthDimension) aLenDim = Handle(AIS_LengthDimension)::DownCast(anObject);
150 if (!aLenDim.IsNull())
151 aValue = aLenDim->GetValue();
154 QLineEdit* aLine = new QLineEdit();
155 QPoint aViewPos = theEvent->globalPos();
156 QPoint aLinePos(aViewPos.x(), aViewPos.y());
157 aLine->move(aLinePos);
158 aLine->setText(QString::number(aValue));
165 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
167 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
170 if (myFeatures.size() != 1) {
171 FeaturePtr aFeature = PartSet_Tools::nearestFeature(theEvent->pos(), theView, feature(),
174 restartOperation(PartSet_OperationEditFeature::Type(), aFeature);
178 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >
179 PartSet_OperationSketch::subPreview() const
181 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
183 boost::shared_ptr<SketchPlugin_Feature> aFeature;
185 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
186 if (!aData->isValid())
188 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
189 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
191 std::list<FeaturePtr > aFeatures = aRefList->list();
192 std::list<FeaturePtr >::const_iterator anIt = aFeatures.begin(),
193 aLast = aFeatures.end();
194 for (; anIt != aLast; anIt++) {
195 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
196 boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
198 aPreviewMap[aFeature] = aPreview;
203 void PartSet_OperationSketch::stopOperation()
205 PartSet_OperationSketchBase::stopOperation();
206 emit featureConstructed(feature(), FM_Hide);
207 emit closeLocalContext();
210 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
212 return hasSketchPlane();
215 void PartSet_OperationSketch::startOperation()
218 setFeature(createFeature());
223 bool PartSet_OperationSketch::hasSketchPlane() const
225 bool aHasPlane = false;
228 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
229 AttributeDoublePtr anAttr;
230 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
231 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
232 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
237 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
239 if (theShape.IsNull())
242 // get selected shape
243 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
244 aGShape->setImpl(new TopoDS_Shape(theShape));
246 // get plane parameters
247 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
249 // set plane parameters to feature
250 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
251 double anA, aB, aC, aD;
252 aPlane->coefficients(anA, aB, aC, aD);
254 // calculate attributes of the sketch
255 boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
256 boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
257 boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
258 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
259 boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
260 // X axis is preferable to be dirX on the sketch
261 const double tol = Precision::Confusion();
262 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
263 boost::shared_ptr<GeomAPI_Dir> aTempDir(isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
264 boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
265 boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
267 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
268 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
269 anOrigin->setValue(anOrigPnt);
270 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
271 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
272 aNormal->setValue(aNormDir);
273 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
274 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
275 aDirX->setValue(aXDir);
276 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
277 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
278 aDirY->setValue(aYDir);
279 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
283 emit featureConstructed(feature(), FM_Hide);
284 emit closeLocalContext();
285 emit planeSelected(aDir->x(), aDir->y(), aDir->z());