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