1 // File: PartSet_OperationSketch.h
2 // Created: 20 Apr 2014
3 // Author: Natalia ERMOLAEVA
5 #include <PartSet_OperationSketch.h>
7 #include <PartSet_OperationEditLine.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>
20 #include <XGUI_ViewerPrs.h>
22 #include <AIS_Shape.hxx>
23 #include <AIS_ListOfInteractive.hxx>
24 #include <V3d_View.hxx>
30 #include <QMouseEvent>
34 PartSet_OperationSketch::PartSet_OperationSketch(const QString& theId,
36 : PartSet_OperationSketchBase(theId, theParent)
40 PartSet_OperationSketch::~PartSet_OperationSketch()
44 std::list<int> PartSet_OperationSketch::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
46 std::list<int> aModes;
47 if (!hasSketchPlane())
48 aModes.push_back(TopAbs_FACE);
50 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
54 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketch::sketch() const
59 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
60 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
61 const std::list<XGUI_ViewerPrs>& theHighlighted)
63 if (!hasSketchPlane()) {
64 XGUI_ViewerPrs aPrs = theHighlighted.front();
65 const TopoDS_Shape& aShape = aPrs.shape();
67 setSketchPlane(aShape);
70 if (theHighlighted.size() == 1) {
71 boost::shared_ptr<ModelAPI_Feature> aFeature = theHighlighted.front().feature();
73 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
76 myFeatures = theHighlighted;
80 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
82 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
85 if (myFeatures.size() != 1) {
86 boost::shared_ptr<ModelAPI_Feature> aFeature = PartSet_Tools::NearestFeature(theEvent->pos(),
87 theView, feature(), myFeatures);
89 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
93 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
94 PartSet_OperationSketch::subPreview() const
96 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
98 boost::shared_ptr<SketchPlugin_Feature> aFeature;
100 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
101 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
102 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
104 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
105 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
106 aLast = aFeatures.end();
107 for (; anIt != aLast; anIt++) {
108 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
111 boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
113 aPreviewMap[aFeature] = aPreview;
118 void PartSet_OperationSketch::stopOperation()
120 PartSet_OperationSketchBase::stopOperation();
121 emit featureConstructed(feature(), FM_Hide);
122 emit closeLocalContext();
125 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
127 return hasSketchPlane();
130 bool PartSet_OperationSketch::hasSketchPlane() const
132 bool aHasPlane = false;
135 // set plane parameters to feature
136 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
138 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
139 // temporary solution for main planes only
140 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
141 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
142 double aX = aNormal->x(), anY = aNormal->y(), aZ = aNormal->z();
144 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
149 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
151 if (theShape.IsNull())
154 // get selected shape
155 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
156 aGShape->setImpl(new TopoDS_Shape(theShape));
158 // get plane parameters
159 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
161 // set plane parameters to feature
162 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
163 double anA, aB, aC, aD;
164 aPlane->coefficients(anA, aB, aC, aD);
166 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
167 // temporary solution for main planes only
168 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
169 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
170 anOrigin->setValue(0, 0, 0);
171 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
172 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
173 aNormal->setValue(anA, aB, aC);
174 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
175 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
176 aDirX->setValue(aB, aC, anA);
177 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
178 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
179 aDirY->setValue(aC, anA, aB);
180 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
184 emit featureConstructed(feature(), FM_Hide);
185 emit closeLocalContext();
186 emit planeSelected(aDir->x(), aDir->y(), aDir->z());