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 void PartSet_OperationSketch::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
55 const std::list<XGUI_ViewerPrs>& thePresentations)
57 setFeature(theFeature);
60 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketch::sketch() const
65 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
66 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
67 const std::list<XGUI_ViewerPrs>& theHighlighted)
69 if (!hasSketchPlane()) {
70 if (!theHighlighted.empty()) {
71 XGUI_ViewerPrs aPrs = theHighlighted.front();
72 const TopoDS_Shape& aShape = aPrs.shape();
74 setSketchPlane(aShape);
78 if (theHighlighted.size() == 1) {
79 boost::shared_ptr<ModelAPI_Feature> aFeature = theHighlighted.front().feature();
81 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
84 myFeatures = theHighlighted;
88 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
90 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
93 if (myFeatures.size() != 1) {
94 boost::shared_ptr<ModelAPI_Feature> aFeature = PartSet_Tools::NearestFeature(theEvent->pos(),
95 theView, feature(), myFeatures);
97 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
101 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
102 PartSet_OperationSketch::subPreview() const
104 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
106 boost::shared_ptr<SketchPlugin_Feature> aFeature;
108 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
109 if (!aData->isValid())
111 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
112 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
114 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
115 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
116 aLast = aFeatures.end();
117 for (; anIt != aLast; anIt++) {
118 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
119 boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
121 aPreviewMap[aFeature] = aPreview;
126 void PartSet_OperationSketch::stopOperation()
128 PartSet_OperationSketchBase::stopOperation();
129 emit featureConstructed(feature(), FM_Hide);
130 emit closeLocalContext();
133 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
135 return hasSketchPlane();
138 bool PartSet_OperationSketch::hasSketchPlane() const
140 bool aHasPlane = false;
143 // set plane parameters to feature
144 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
146 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
147 // temporary solution for main planes only
148 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
149 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
150 double aX = aNormal->x(), anY = aNormal->y(), aZ = aNormal->z();
152 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
157 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
159 if (theShape.IsNull())
162 // get selected shape
163 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
164 aGShape->setImpl(new TopoDS_Shape(theShape));
166 // get plane parameters
167 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
169 // set plane parameters to feature
170 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
171 double anA, aB, aC, aD;
172 aPlane->coefficients(anA, aB, aC, aD);
174 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
175 // temporary solution for main planes only
176 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
177 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
178 anOrigin->setValue(0, 0, 0);
179 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
180 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
181 aNormal->setValue(anA, aB, aC);
182 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
183 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
184 aDirX->setValue(aB, aC, anA);
185 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
186 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
187 aDirY->setValue(aC, anA, aB);
188 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
192 emit featureConstructed(feature(), FM_Hide);
193 emit closeLocalContext();
194 emit planeSelected(aDir->x(), aDir->y(), aDir->z());