Salome HOME
466024127b2af00ea9d0a95bf4ad70db9b187f26
[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 #include <GeomAPI_XYZ.h>
16
17 #include <GeomDataAPI_Dir.h>
18 #include <GeomDataAPI_Point.h>
19 #include <GeomAlgoAPI_FaceBuilder.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
30 #include <SketchPlugin_Sketch.h>
31 #include <SketchPlugin_Feature.h>
32 #include <SketchPlugin_SketchEntity.h>
33
34 #include <memory>
35
36 #include <math.h>
37 #include <vector>
38
39 using namespace std;
40
41 SketchPlugin_Sketch::SketchPlugin_Sketch()
42 {
43 }
44
45 void SketchPlugin_Sketch::initAttributes()
46 {
47   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
48   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
49   data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
50   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
51   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
52   // the selected face, base for the sketcher plane, not obligatory
53   data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
54   ModelAPI_Session::get()->validators()->registerNotObligatory(
55     getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
56 }
57
58 void SketchPlugin_Sketch::execute()
59 {
60   if (!data()->isValid())
61     return;
62   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
63       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
64
65   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
66       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
67   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
68       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
69   std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
70       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
71   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
72       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
73
74   std::list<ObjectPtr> aFeatures = aRefList->list();
75   if (aFeatures.empty())
76     return;
77
78   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
79   std::shared_ptr<SketchPlugin_Feature> aFeature;
80   std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
81   for (; anIt != aLast; anIt++) {
82     aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
83     if (aFeature) {
84       if (!aFeature->sketch()) // on load document the back references are missed
85         aFeature->setSketch(this);
86       // do not include the external edges into the result
87       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
88         if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value())
89           continue;
90       }
91       // do not include the construction entities in the result
92       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
93         if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
94           continue;
95       }
96
97       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
98       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
99       for (; aResIter != aRes.cend(); aResIter++) {
100         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
101             ModelAPI_ResultConstruction>(*aResIter);
102         if (aConstr) {
103           std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
104           if (aShape)
105             aFeaturesPreview.push_back(aShape);
106         }
107       }
108     }
109   }
110
111   if (aFeaturesPreview.empty())
112     return;
113
114   // Collect all edges as one big wire
115   std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
116   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
117   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
118     aBigWire->addEdge(*aShapeIt);
119   }
120   aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir());
121
122 //  GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
123 //                                         aFeaturesPreview, aLoops, aWires);
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);
132   if (aNew) {
133     std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
134     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
135   }
136   return aNew;
137 }
138
139 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
140 {
141   if (!data().get()) // sketch is already removed (case on undo of sketch), sync is not needed
142     return;
143   list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
144   list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
145   bool isRemoved = false;
146   bool aHasEmtpyFeature = false;
147   for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
148     std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
149     if (aFeature.get() != NULL && aFeature == theFeature) {
150       data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
151       isRemoved = true;
152     }
153     else if (aFeature.get() == NULL)
154       aHasEmtpyFeature = true;
155   }
156   // if the object is not found in the sketch sub-elements, that means that the object is removed already.
157   // Find the first empty element and remove it
158   if (!isRemoved && aHasEmtpyFeature)
159     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
160 }
161
162 int SketchPlugin_Sketch::numberOfSubs() const
163 {
164   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
165 }
166
167 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
168 {
169   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
170   return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
171 }
172
173 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
174 {
175   return subFeature(theIndex)->data()->featureId();
176 }
177
178 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
179 {
180   // check is this feature of result
181   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
182   if (!aFeature) {
183     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
184     if (aRes)
185       aFeature = document()->feature(aRes);
186   }
187   if (aFeature) {
188     list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
189     for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
190       if (*aSubIt == aFeature)
191         return true;
192     }
193   }
194   return false;
195 }
196
197 std::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
198 {
199   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
200       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
201   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
202       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
203   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
204       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
205
206   std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
207       ->added(aY->dir()->xyz()->multiplied(theY));
208
209   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
210 }
211
212 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
213   const std::shared_ptr<GeomAPI_Pnt>& thePnt)
214 {
215   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
216       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
217   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
218       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
219   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
220       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
221   return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
222 }
223
224
225 bool SketchPlugin_Sketch::isPlaneSet()
226 {
227   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
228       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
229
230   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
231 }
232
233 std::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
234 {
235   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
236       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
237   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
238       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
239
240   if (!anOrigin || !aNorm)
241     return std::shared_ptr<GeomAPI_Pln>();
242
243   return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
244 }
245
246 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::coordinatePlane() const
247 {
248   DataPtr aData = data();
249   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
250     aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
251   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
252     aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
253   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
254     aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
255   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
256     aData->attribute(SketchPlugin_Sketch::NORM_ID()));
257
258   return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aY->dir(), aNorm->dir()));
259 }
260
261 void SketchPlugin_Sketch::erase()
262 {
263   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
264       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
265   std::list<ObjectPtr> aFeatures = aRefList->list();
266   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
267   for (; anIt != aFeatures.end(); anIt++) {
268     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
269     if (aFeature) {
270       // subs are referenced from sketch, but must be removed for sure, so not checkings
271       document()->removeFeature(aFeature);
272     }
273   }
274   ModelAPI_CompositeFeature::erase();
275 }
276
277 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
278   if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
279     std::shared_ptr<GeomAPI_Shape> aSelection = 
280       data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
281     if (aSelection) { // update arguments due to the selection value
282       // update the sketch plane
283       std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
284       if (aPlane) {
285         double anA, aB, aC, aD;
286         aPlane->coefficients(anA, aB, aC, aD);
287
288         // calculate attributes of the sketch
289         std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
290         std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
291         std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
292         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
293         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
294         // X axis is preferable to be dirX on the sketch
295         static const double tol = 1.e-7;
296         bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
297         std::shared_ptr<GeomAPI_Dir> aTempDir(
298           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
299         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
300         std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
301
302         // update position of the sketch
303         std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
304           <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
305         anOrigin->setValue(anOrigPnt);
306         std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
307           data()->attribute(SketchPlugin_Sketch::NORM_ID()));
308         aNormal->setValue(aNormDir);
309         std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
310           data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
311         aDirX->setValue(aXDir);
312         std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
313           data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
314         aDirY->setValue(aYDir);
315         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
316       }
317     }
318   }
319 }