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