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), myIsEditMode(false)
40 PartSet_OperationSketch::~PartSet_OperationSketch()
44 std::list<int> PartSet_OperationSketch::getSelectionModes(boost::shared_ptr<ModelAPI_Feature> theFeature) const
46 std::list<int> aModes;
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)
62 myFeatures = theSelected;
65 void PartSet_OperationSketch::mouseReleased(QMouseEvent* theEvent, Handle_V3d_View theView,
66 const std::list<XGUI_ViewerPrs>& theSelected)
68 if (theSelected.empty())
72 XGUI_ViewerPrs aPrs = theSelected.front();
73 const TopoDS_Shape& aShape = aPrs.shape();
74 if (!aShape.IsNull()) {
75 setSketchPlane(aShape);
80 if (theSelected.size() == 1) {
81 boost::shared_ptr<ModelAPI_Feature> aFeature = theSelected.front().feature();
83 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
89 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
91 if (!myIsEditMode || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
94 if (myFeatures.size() != 1) {
95 boost::shared_ptr<ModelAPI_Feature> aFeature = PartSet_Tools::NearestFeature(theEvent->pos(),
96 theView, feature(), myFeatures);
98 emit launchOperation(PartSet_OperationEditLine::Type(), aFeature);
102 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
103 PartSet_OperationSketch::subPreview() const
105 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
107 boost::shared_ptr<SketchPlugin_Feature> aFeature;
109 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
110 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
111 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
113 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
114 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
115 aLast = aFeatures.end();
116 for (; anIt != aLast; anIt++) {
117 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
118 boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
120 aPreviewMap[aFeature] = aPreview;
125 void PartSet_OperationSketch::stopOperation()
127 PartSet_OperationSketchBase::stopOperation();
128 emit featureConstructed(feature(), FM_Hide);
129 emit closeLocalContext();
132 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
134 if (theShape.IsNull())
137 // get selected shape
138 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
139 aGShape->setImpl(new TopoDS_Shape(theShape));
141 // get plane parameters
142 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
144 // set plane parameters to feature
145 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
146 double anA, aB, aC, aD;
147 aPlane->coefficients(anA, aB, aC, aD);
149 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
151 aData->real(SKETCH_ATTR_PLANE_A)->setValue(anA);
152 aData->real(SKETCH_ATTR_PLANE_B)->setValue(aB);
153 aData->real(SKETCH_ATTR_PLANE_C)->setValue(aC);
154 aData->real(SKETCH_ATTR_PLANE_D)->setValue(aD);
156 // temporary solution for main planes only
157 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
158 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
159 anOrigin->setValue(0, 0, 0);
160 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
161 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
162 aNormal->setValue(anA, aB, aC);
163 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
164 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
165 aDirX->setValue(aB, aC, anA);
166 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
167 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
168 aDirY->setValue(aC, anA, aB);
169 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
170 emit featureConstructed(feature(), FM_Hide);
171 emit closeLocalContext();
172 emit planeSelected(aDir->x(), aDir->y(), aDir->z());