Salome HOME
a3cbc24b5f0c49377501a2cd1939c6096558a4ca
[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
91       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
92       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
93       for (; aResIter != aRes.cend(); aResIter++) {
94         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
95             ModelAPI_ResultConstruction>(*aResIter);
96         if (aConstr) {
97           std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
98           if (aShape)
99             aFeaturesPreview.push_back(aShape);
100         }
101       }
102     }
103   }
104
105   if (aFeaturesPreview.empty())
106     return;
107
108   // Collect all edges as one big wire
109   std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
110   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
111   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
112     aBigWire->addEdge(*aShapeIt);
113   }
114   aBigWire->setOrigin(anOrigin->pnt());
115   aBigWire->setDirX(aDirX->dir());
116   aBigWire->setDirY(aDirY->dir());
117   aBigWire->setNorm(aNorm->dir());
118
119 //  GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
120 //                                         aFeaturesPreview, aLoops, aWires);
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(ModelAPI_Feature* theFeature)
137 {
138   list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
139   list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
140   bool isRemoved = 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.get() == theFeature) {
144       data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
145       isRemoved = true;
146     }
147   }
148   // if the object is not found in the sketch sub-elements, that means that the object is removed already.
149   // Find the first empty element and remove it
150   if (!isRemoved)
151     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(NULL);
152 }
153
154 int SketchPlugin_Sketch::numberOfSubs() const
155 {
156   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
157 }
158
159 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
160 {
161   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
162   return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
163 }
164
165 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
166 {
167   return subFeature(theIndex)->data()->featureId();
168 }
169
170 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
171 {
172   // check is this feature of result
173   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
174   if (!aFeature) {
175     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
176     if (aRes)
177       aFeature = document()->feature(aRes);
178   }
179   if (aFeature) {
180     list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
181     for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
182       if (*aSubIt == aFeature)
183         return true;
184     }
185   }
186   return false;
187 }
188
189 std::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
190 {
191   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
192       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
193   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
194       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
195   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
196       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
197
198   std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
199       ->added(aY->dir()->xyz()->multiplied(theY));
200
201   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
202 }
203
204 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
205   const std::shared_ptr<GeomAPI_Pnt>& thePnt)
206 {
207   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
208       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
209   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
210       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
211   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
212       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
213   return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
214 }
215
216
217 bool SketchPlugin_Sketch::isPlaneSet()
218 {
219   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
220       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
221
222   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
223 }
224
225 std::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
226 {
227   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
228       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
229   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
230       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
231
232   if (!anOrigin || !aNorm)
233     return std::shared_ptr<GeomAPI_Pln>();
234
235   return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
236 }
237
238 //void addPlane(double theX, double theY, double theZ,
239 //              std::list<std::shared_ptr<GeomAPI_Shape> >& theShapes)
240 //{
241 //  std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
242 //  std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
243 //  double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
244 //  std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
245 //                                                                           aSize);
246 //  theShapes.push_back(aFace);
247 //}
248
249 //AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
250 //{
251 //  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
252 //      data()->attribute(SketchPlugin_Sketch::NORM_ID()));
253 //
254 //  if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
255 //    AISObjectPtr aAIS = thePrevious;
256 //    if (!aAIS) {
257 //      std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
258 //
259 //      addPlane(1, 0, 0, aFaces);  // YZ plane
260 //      addPlane(0, 1, 0, aFaces);  // XZ plane
261 //      addPlane(0, 0, 1, aFaces);  // XY plane
262 //      std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
263 //      aAIS = AISObjectPtr(new GeomAPI_AISObject());
264 //      aAIS->createShape(aCompound);
265 //
266 //      std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
267 //      SKETCH_PLANE_COLOR);
268 //      aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
269 //
270 //      aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
271 //      SKETCH_WIDTH));
272 //    }
273 //    return aAIS;
274 //  }
275 //  return AISObjectPtr();
276 //}
277
278 void SketchPlugin_Sketch::erase()
279 {
280   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
281       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
282   std::list<ObjectPtr> aFeatures = aRefList->list();
283   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
284   for (; anIt != aFeatures.end(); anIt++) {
285     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
286     if (aFeature) {
287       // subs are referenced from sketch, but must be removed for sure, so not checkings
288       document()->removeFeature(aFeature);
289     }
290   }
291   ModelAPI_CompositeFeature::erase();
292 }
293
294 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
295   if (theID == SketchPlugin_Feature::EXTERNAL_ID()) {
296     std::shared_ptr<GeomAPI_Shape> aSelection = 
297       data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value();
298     if (aSelection) { // update arguments due to the selection value
299       // update the sketch plane
300       std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
301       if (aPlane) {
302         double anA, aB, aC, aD;
303         aPlane->coefficients(anA, aB, aC, aD);
304
305         // calculate attributes of the sketch
306         std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
307         std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
308         std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
309         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
310         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
311         // X axis is preferable to be dirX on the sketch
312         static const double tol = 1.e-7;
313         bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
314         std::shared_ptr<GeomAPI_Dir> aTempDir(
315           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
316         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
317         std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
318
319         // update position of the sketch
320         std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
321           <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
322         anOrigin->setValue(anOrigPnt);
323         std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
324           data()->attribute(SketchPlugin_Sketch::NORM_ID()));
325         aNormal->setValue(aNormDir);
326         std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
327           data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
328         aDirX->setValue(aXDir);
329         std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
330           data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
331         aDirY->setValue(aYDir);
332         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
333       }
334     }
335   }
336 }