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 <AIS_InteractiveObject.hxx>
25 #include <V3d_View.hxx>
31 #include <QMouseEvent>
35 const Quantity_NameOfColor SKETCH_PLANE_COLOR = Quantity_NOC_CHOCOLATE; /// the plane edge color
36 const int SKETCH_WIDTH = 4; /// the plane edge width
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(boost::shared_ptr<ModelAPI_Feature> theFeature) const
50 std::list<int> aModes;
51 if (!hasSketchPlane())
52 aModes.push_back(TopAbs_FACE);
54 aModes = PartSet_OperationSketchBase::getSelectionModes(theFeature);
58 void PartSet_OperationSketch::init(boost::shared_ptr<ModelAPI_Feature> theFeature,
59 const std::list<XGUI_ViewerPrs>& /*theSelected*/,
60 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
62 setFeature(theFeature);
65 boost::shared_ptr<ModelAPI_Feature> PartSet_OperationSketch::sketch() const
70 void PartSet_OperationSketch::mousePressed(QMouseEvent* theEvent, Handle_V3d_View theView,
71 const std::list<XGUI_ViewerPrs>& theSelected,
72 const std::list<XGUI_ViewerPrs>& theHighlighted)
74 if (!hasSketchPlane()) {
75 if (!theHighlighted.empty()) {
76 XGUI_ViewerPrs aPrs = theHighlighted.front();
77 const TopoDS_Shape& aShape = aPrs.shape();
79 setSketchPlane(aShape);
83 // if shift button is pressed and there are some already selected objects, the operation should
84 // not be started. We just want to combine some selected objects.
85 bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
86 if (aHasShift && theSelected.size() > 0)
89 if (theHighlighted.size() == 1) {
90 boost::shared_ptr<ModelAPI_Feature> aFeature = theHighlighted.front().feature();
92 restartOperation(PartSet_OperationEditLine::Type(), aFeature);
95 myFeatures = theHighlighted;
99 void PartSet_OperationSketch::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
101 if (!hasSketchPlane() || !(theEvent->buttons() & Qt::LeftButton) || myFeatures.empty())
104 if (myFeatures.size() != 1) {
105 boost::shared_ptr<ModelAPI_Feature> aFeature = PartSet_Tools::NearestFeature(theEvent->pos(),
106 theView, feature(), myFeatures);
108 restartOperation(PartSet_OperationEditLine::Type(), aFeature);
112 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
113 PartSet_OperationSketch::subPreview() const
115 std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> > aPreviewMap;
117 boost::shared_ptr<SketchPlugin_Feature> aFeature;
119 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
120 if (!aData->isValid())
122 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
123 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(aData->attribute(SKETCH_ATTR_FEATURES));
125 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
126 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
127 aLast = aFeatures.end();
128 for (; anIt != aLast; anIt++) {
129 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
130 boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
132 aPreviewMap[aFeature] = aPreview;
137 void PartSet_OperationSketch::stopOperation()
139 PartSet_OperationSketchBase::stopOperation();
140 emit featureConstructed(feature(), FM_Hide);
141 emit closeLocalContext();
144 bool PartSet_OperationSketch::isNestedOperationsEnabled() const
146 return hasSketchPlane();
149 void PartSet_OperationSketch::correctPresentation(Handle(AIS_InteractiveObject) thePresentation)
151 Handle(AIS_Shape) anAIS = Handle(AIS_Shape)::DownCast(thePresentation);
155 anAIS->SetColor(Quantity_Color(SKETCH_PLANE_COLOR));
156 anAIS->SetWidth(SKETCH_WIDTH);
160 void PartSet_OperationSketch::startOperation()
163 setFeature(createFeature());
168 bool PartSet_OperationSketch::hasSketchPlane() const
170 bool aHasPlane = false;
173 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
174 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
175 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
176 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
177 aHasPlane = aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
182 void PartSet_OperationSketch::setSketchPlane(const TopoDS_Shape& theShape)
184 if (theShape.IsNull())
187 // get selected shape
188 boost::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
189 aGShape->setImpl(new TopoDS_Shape(theShape));
191 // get plane parameters
192 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
194 // set plane parameters to feature
195 boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
196 double anA, aB, aC, aD;
197 aPlane->coefficients(anA, aB, aC, aD);
199 boost::shared_ptr<ModelAPI_AttributeDouble> anAttr;
200 // temporary solution for main planes only
201 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
202 boost::dynamic_pointer_cast<GeomDataAPI_Point>(aData->attribute(SKETCH_ATTR_ORIGIN));
203 anOrigin->setValue(0, 0, 0);
204 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
205 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_NORM));
206 aNormal->setValue(anA, aB, aC);
207 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
208 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRX));
209 aDirX->setValue(aB, aC, anA);
210 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
211 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(aData->attribute(SKETCH_ATTR_DIRY));
212 aDirY->setValue(aC, anA, aB);
213 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
217 emit featureConstructed(feature(), FM_Hide);
218 emit closeLocalContext();
219 emit planeSelected(aDir->x(), aDir->y(), aDir->z());