Salome HOME
Issue #504 - 1.17. Error user feedback when a feature fail
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Sketch.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        SketchPlugin_Sketch.cxx
4 // Created:     27 Mar 2014
5 // Author:      Mikhail PONIKAROV
6
7 #include <Config_PropManager.h>
8
9 #include <GeomAlgoAPI_CompoundBuilder.h>
10 #include <GeomAlgoAPI_FaceBuilder.h>
11
12 #include <GeomAPI_AISObject.h>
13 #include <GeomAPI_Dir.h>
14 #include <GeomAPI_PlanarEdges.h>
15
16 #include <GeomAlgoAPI_FaceBuilder.h>
17
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAlgoAPI_PointBuilder.h>
20
21 #include <ModelAPI_AttributeRefList.h>
22 #include <ModelAPI_AttributeString.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Feature.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_ResultConstruction.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Events.h>
31
32 #include <SketchPlugin_Sketch.h>
33 #include <SketchPlugin_Feature.h>
34 #include <SketchPlugin_SketchEntity.h>
35 #include <SketchPlugin_Tools.h>
36
37 #include <Events_Loop.h>
38
39 #include <memory>
40
41 #include <math.h>
42 #include <vector>
43
44 using namespace std;
45
46 SketchPlugin_Sketch::SketchPlugin_Sketch()
47 {
48 }
49
50 void SketchPlugin_Sketch::initAttributes()
51 {
52   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
53   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
54   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
55   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
56   // the selected face, base for the sketcher plane, not obligatory
57   data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
58   ModelAPI_Session::get()->validators()->registerNotObligatory(
59     getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
60   data()->addAttribute(SketchPlugin_Sketch::SOLVER_ERROR(), ModelAPI_AttributeString::typeId());
61 }
62
63 void SketchPlugin_Sketch::execute()
64 {
65   if (!data()->isValid())
66     return;
67   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
68       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
69
70   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
71       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
72   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
73       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
74   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
75       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
76
77   std::list<ObjectPtr> aFeatures = aRefList->list();
78   if (aFeatures.empty())
79     return;
80
81   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
82   std::shared_ptr<SketchPlugin_Feature> aFeature;
83   std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
84   for (; anIt != aLast; anIt++) {
85     aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
86     if (aFeature) {
87       if (!aFeature->sketch()) // on load document the back references are missed
88         aFeature->setSketch(this);
89       // do not include the external edges into the result
90       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
91         if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value())
92           continue;
93       }
94       // do not include the construction entities in the result
95       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
96         if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
97           continue;
98       }
99
100       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
101       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
102       for (; aResIter != aRes.cend(); aResIter++) {
103         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
104             ModelAPI_ResultConstruction>(*aResIter);
105         if (aConstr) {
106           std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
107           if (aShape)
108             aFeaturesPreview.push_back(aShape);
109         }
110       }
111     }
112   }
113
114   if (aFeaturesPreview.empty())
115     return;
116
117   // Collect all edges as one big wire
118   std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
119   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
120   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
121     aBigWire->addEdge(*aShapeIt);
122   }
123   aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
124   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
125   aConstr->setShape(aBigWire);
126   setResult(aConstr);
127 }
128
129 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
130 {
131   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
132   if (aNew) {
133     std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
134     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
135   }
136    // set as current also after it becomes sub to set correctly enabled for other sketch subs
137   document()->setCurrentFeature(aNew, false);
138   return aNew;
139 }
140
141 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
142 {
143   if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
144     return;
145   // to keep the persistent sub-elements indexing, do not remove elements from list,
146   // but substitute by nulls
147   reflist(SketchPlugin_Sketch::FEATURES_ID())->substitute(theFeature, ObjectPtr());
148 }
149
150 int SketchPlugin_Sketch::numberOfSubs(bool forTree) const
151 {
152   if (forTree)
153     return 0;
154   return data()->reflist(FEATURES_ID())->size(false);
155 }
156
157 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(
158   const int theIndex, bool forTree)
159 {
160   if (forTree)
161     return FeaturePtr();
162
163   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
164   FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
165   return aRes;
166 }
167
168 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
169 {
170   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
171       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
172   std::list<ObjectPtr> aFeatures = aRefList->list();
173   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
174   int aResultIndex = 1; // number of the counted (created) features, started from 1
175   int aFeatureIndex = -1; // number of the not-empty features in the list
176   for (; anIt != aFeatures.end(); anIt++) {
177     if (anIt->get()) 
178       aFeatureIndex++;
179     if (aFeatureIndex == theIndex)
180       break;
181     aResultIndex++;
182   }
183   return aResultIndex;
184 }
185
186 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
187 {
188   // check is this feature of result
189   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
190   if (!aFeature) {
191     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
192     if (aRes)
193       aFeature = document()->feature(aRes);
194   }
195   if (aFeature) {
196     return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
197   }
198   return false;
199 }
200
201
202 void SketchPlugin_Sketch::erase()
203 {
204   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
205       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
206   std::list<ObjectPtr> aFeatures = aRefList->list();
207   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
208   for (; anIt != aFeatures.end(); anIt++) {
209     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
210     if (aFeature) {
211       // subs are referenced from sketch, but must be removed for sure, so not checkings
212       document()->removeFeature(aFeature);
213     }
214   }
215   ModelAPI_CompositeFeature::erase();
216 }
217
218 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
219   if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
220     std::shared_ptr<GeomAPI_Shape> aSelection = 
221       data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
222     if (aSelection) { // update arguments due to the selection value
223       // update the sketch plane
224       std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
225       if (aPlane) {
226         double anA, aB, aC, aD;
227         aPlane->coefficients(anA, aB, aC, aD);
228
229         // calculate attributes of the sketch
230         std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
231         std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
232         std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
233         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
234         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
235         // X axis is preferable to be dirX on the sketch
236         static const double tol = 0.1; // here can not be very small value to avoid very close to X normal axis (issue 595)
237         bool isX = fabs(anA) - 1.0 < tol && fabs(aB) < tol && fabs(aC) < tol;
238         std::shared_ptr<GeomAPI_Dir> aTempDir(
239           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
240         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
241         std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
242
243         // update position of the sketch
244         std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
245           <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
246         anOrigin->setValue(anOrigPnt);
247         std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
248           data()->attribute(SketchPlugin_Sketch::NORM_ID()));
249         aNormal->setValue(aNormDir);
250         std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
251           data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
252         aDirX->setValue(aXDir);
253         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
254       }
255     }
256   } else if (theID == SketchPlugin_Sketch::NORM_ID() || theID == SketchPlugin_Sketch::DIRX_ID()) {
257     // send all sub-elements are also updated: all entities become created on different plane
258     static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
259     std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
260     std::list<ObjectPtr>::iterator aSub = aSubs.begin();
261     for(; aSub != aSubs.end(); aSub++) {
262       if (aSub->get())
263         ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
264     }
265   }
266 }
267
268 void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
269                                               SketchPlugin_Sketch* theSketch,
270                                               const std::string& theAttributeID, const int theIndex)
271 {
272   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
273     theFeature->attribute(theAttributeID));
274
275   if (!aPoint || !aPoint->isInitialized())
276     return;
277
278   std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
279   //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
280   // make a visible point
281   std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
282   std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
283                      theFeature->data(), theIndex);
284   aResult->setShape(aCenterPointShape);
285   aResult->setIsInHistory(false);
286
287   theFeature->setResult(aResult, theIndex);
288 }
289
290 FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
291                                                             SketchPlugin_Sketch* theSketch)
292 {
293   FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
294   // addFeature generates a unique name for the feature, it caches the name
295   std::string aUniqueFeatureName = aNewFeature->data()->name();
296   // all attribute values are copied\pasted to the new feature, name is not an exception
297   theFeature->data()->copyTo(aNewFeature->data());
298   // as a name for the feature, the generated unique name is set
299   aNewFeature->data()->setName(aUniqueFeatureName);
300   // text expressions could block setValue of some attributes
301   clearExpressions(aNewFeature);
302
303   return aNewFeature;
304 }
305
306 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
307 {
308   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
309
310   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
311       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
312   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
313       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
314   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
315       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
316
317   return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
318 }