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