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