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