Salome HOME
Merge remote-tracking branch 'origin/Dev_1.1.0' into Dev_1.1.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 #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   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
122   aConstr->setShape(aBigWire);
123   setResult(aConstr);
124 }
125
126 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
127 {
128   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
129   if (aNew) {
130     std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
131     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
132   }
133   return aNew;
134 }
135
136 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
137 {
138   if (!data().get()) // sketch is already removed (case on undo of sketch), sync is not needed
139     return;
140   list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
141   list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
142   bool isRemoved = false;
143   bool aHasEmtpyFeature = false;
144   for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
145     std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
146     if (aFeature.get() != NULL && aFeature == theFeature) {
147       data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
148       isRemoved = true;
149     }
150     else if (aFeature.get() == NULL)
151       aHasEmtpyFeature = true;
152   }
153   // if the object is not found in the sketch sub-elements, that means that the object is removed already.
154   // Find the first empty element and remove it
155   if (!isRemoved && aHasEmtpyFeature)
156     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
157 }
158
159 int SketchPlugin_Sketch::numberOfSubs() const
160 {
161   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
162 }
163
164 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
165 {
166   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
167   return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
168 }
169
170 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
171 {
172   return subFeature(theIndex)->data()->featureId();
173 }
174
175 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
176 {
177   // check is this feature of result
178   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
179   if (!aFeature) {
180     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
181     if (aRes)
182       aFeature = document()->feature(aRes);
183   }
184   if (aFeature) {
185     list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
186     for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
187       if (*aSubIt == aFeature)
188         return true;
189     }
190   }
191   return false;
192 }
193
194 std::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
195 {
196   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
197       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
198   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
199       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
200   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
201       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
202
203   std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
204       ->added(aY->dir()->xyz()->multiplied(theY));
205
206   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
207 }
208
209 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
210   const std::shared_ptr<GeomAPI_Pnt>& thePnt)
211 {
212   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
213       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
214   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
215       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
216   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
217       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
218   return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
219 }
220
221
222 bool SketchPlugin_Sketch::isPlaneSet()
223 {
224   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
225       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
226
227   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
228 }
229
230 std::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
231 {
232   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
233       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
234   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
235       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
236
237   if (!anOrigin || !aNorm)
238     return std::shared_ptr<GeomAPI_Pln>();
239
240   return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
241 }
242
243 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::coordinatePlane() const
244 {
245   DataPtr aData = data();
246   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
247     aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
248   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
249     aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
250   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
251     aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
252   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
253     aData->attribute(SketchPlugin_Sketch::NORM_ID()));
254
255   return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aY->dir(), aNorm->dir()));
256 }
257
258 void SketchPlugin_Sketch::erase()
259 {
260   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
261       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
262   std::list<ObjectPtr> aFeatures = aRefList->list();
263   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
264   for (; anIt != aFeatures.end(); anIt++) {
265     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
266     if (aFeature) {
267       // subs are referenced from sketch, but must be removed for sure, so not checkings
268       document()->removeFeature(aFeature);
269     }
270   }
271   ModelAPI_CompositeFeature::erase();
272 }
273
274 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
275   if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
276     std::shared_ptr<GeomAPI_Shape> aSelection = 
277       data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
278     if (aSelection) { // update arguments due to the selection value
279       // update the sketch plane
280       std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
281       if (aPlane) {
282         double anA, aB, aC, aD;
283         aPlane->coefficients(anA, aB, aC, aD);
284
285         // calculate attributes of the sketch
286         std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
287         std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
288         std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
289         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
290         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
291         // X axis is preferable to be dirX on the sketch
292         static const double tol = 1.e-7;
293         bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
294         std::shared_ptr<GeomAPI_Dir> aTempDir(
295           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
296         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
297         std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
298
299         // update position of the sketch
300         std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
301           <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
302         anOrigin->setValue(anOrigPnt);
303         std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
304           data()->attribute(SketchPlugin_Sketch::NORM_ID()));
305         aNormal->setValue(aNormDir);
306         std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
307           data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
308         aDirX->setValue(aXDir);
309         std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
310           data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
311         aDirY->setValue(aYDir);
312         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
313       }
314     }
315   }
316 }