Salome HOME
refs #80 - Sketch base GUI: create/draw point, circle and arc
[modules/shaper.git] / src / PartSet / PartSet_OperationConstraint.cpp
1 // File:        PartSet_OperationConstraint.h
2 // Created:     20 Apr 2014
3 // Author:      Natalia ERMOLAEVA
4
5 #include <PartSet_OperationConstraint.h>
6
7 #include <PartSet_Tools.h>
8 #include <PartSet_OperationSketch.h>
9
10 #include <SketchPlugin_Feature.h>
11 #include <SketchPlugin_Sketch.h>
12
13 #include <GeomDataAPI_Point2D.h>
14
15 #include <ModuleBase_OperationDescription.h>
16
17 #include <ModelAPI_Data.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_AttributeRefAttr.h>
20 #include <ModelAPI_AttributeRefList.h>
21 #include <ModelAPI_AttributeDouble.h>
22
23 #include <SketchPlugin_Constraint.h>
24
25 #include <Geom_Line.hxx>
26 #include <gp_Lin.hxx>
27
28 #include <XGUI_ViewerPrs.h>
29 #include <XGUI_Constants.h>
30
31 #include <SketchPlugin_Line.h>
32
33 #include <V3d_View.hxx>
34 #include <TopoDS_Vertex.hxx>
35 #include <TopoDS.hxx>
36 #include <BRep_Tool.hxx>
37
38 #ifdef _DEBUG
39 #include <QDebug>
40 #endif
41
42 #include <QMouseEvent>
43
44 using namespace std;
45
46 PartSet_OperationConstraint::PartSet_OperationConstraint(const QString& theId,
47                                                   QObject* theParent,
48                                               FeaturePtr theFeature)
49 : PartSet_OperationSketchBase(theId, theParent), mySketch(theFeature)
50 {
51 }
52
53 PartSet_OperationConstraint::~PartSet_OperationConstraint()
54 {
55 }
56
57 bool PartSet_OperationConstraint::isGranted(ModuleBase_IOperation* theOperation) const
58 {
59   return theOperation->getDescription()->operationId().toStdString() == PartSet_OperationSketch::Type();
60 }
61
62 void PartSet_OperationConstraint::init(FeaturePtr theFeature,
63                                        const std::list<XGUI_ViewerPrs>& /*theSelected*/,
64                                        const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
65 {
66   //if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
67   //  return;
68   // use the last point of the previous feature as the first of the new one
69   //boost::shared_ptr<ModelAPI_Data> aData = theFeature->data();
70   //myInitPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(LINE_ATTR_END));
71 }
72
73 FeaturePtr PartSet_OperationConstraint::sketch() const
74 {
75   return mySketch;
76 }
77
78 void PartSet_OperationConstraint::mouseReleased(QMouseEvent* theEvent, Handle(V3d_View) theView,
79                                                 const std::list<XGUI_ViewerPrs>& theSelected,
80                                                 const std::list<XGUI_ViewerPrs>& /*theHighlighted*/)
81 {
82   if (theSelected.empty()) {
83   }
84   else {
85     XGUI_ViewerPrs aPrs = theSelected.front();
86     FeaturePtr aFeature = aPrs.feature();
87
88     setFeature(aFeature);
89     setValue(120);
90     flushUpdated();
91   }
92 }
93
94 void PartSet_OperationConstraint::mouseMoved(QMouseEvent* theEvent, Handle(V3d_View) theView)
95 {
96 }
97
98 void PartSet_OperationConstraint::startOperation()
99 {
100   PartSet_OperationSketchBase::startOperation();
101   //setPointSelectionMode(!myInitPoint ? SM_FirstPoint : SM_SecondPoint);
102
103   emit multiSelectionEnabled(false);
104 }
105
106 void PartSet_OperationConstraint::abortOperation()
107 {
108   emit featureConstructed(feature(), FM_Hide);
109   PartSet_OperationSketchBase::abortOperation();
110 }
111
112 void PartSet_OperationConstraint::stopOperation()
113 {
114   PartSet_OperationSketchBase::stopOperation();
115   emit multiSelectionEnabled(true);
116 }
117
118 void PartSet_OperationConstraint::afterCommitOperation()
119 {
120   PartSet_OperationSketchBase::afterCommitOperation();  
121   emit featureConstructed(feature(), FM_Deactivation);
122 }
123
124 FeaturePtr PartSet_OperationConstraint::createFeature(const bool theFlushMessage)
125 {
126   FeaturePtr aNewFeature = ModuleBase_Operation::createFeature(false);
127   if (sketch()) {
128     boost::shared_ptr<SketchPlugin_Feature> aFeature = 
129                            boost::dynamic_pointer_cast<SketchPlugin_Feature>(sketch());
130
131     aFeature->addSub(aNewFeature);
132   }
133   /*if (myInitPoint) {
134     setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_START);
135     setLinePoint(aNewFeature, myInitPoint->x(), myInitPoint->y(), LINE_ATTR_END);
136
137     boost::shared_ptr<ModelAPI_Data> aData = aNewFeature->data();
138     boost::shared_ptr<GeomDataAPI_Point2D> aPoint = boost::dynamic_pointer_cast<GeomDataAPI_Point2D>
139                                                                 (aData->attribute(LINE_ATTR_START));
140     PartSet_Tools::createConstraint(myInitPoint, aPoint);
141   }*/
142
143   emit featureConstructed(aNewFeature, FM_Activation);
144   if (theFlushMessage)
145     flushCreated();
146   return aNewFeature;
147 }
148
149 void PartSet_OperationConstraint::setFeature(FeaturePtr theFeature)
150 {
151   if (!theFeature || theFeature->getKind() != SKETCH_LINE_KIND)
152     return;
153
154   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
155
156   boost::shared_ptr<ModelAPI_AttributeRefAttr> anAttr = 
157           boost::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(aData->attribute(CONSTRAINT_ATTR_ENTITY_A));
158   anAttr->setFeature(theFeature);
159 }
160
161 void PartSet_OperationConstraint::setValue(const double theValue)
162 {
163   boost::shared_ptr<ModelAPI_Data> aData = feature()->data();
164
165   AttributeDoublePtr anAttr = 
166           boost::dynamic_pointer_cast<ModelAPI_AttributeDouble>(aData->attribute(CONSTRAINT_ATTR_VALUE));
167   anAttr->setValue(theValue);
168
169 }