Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom 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
16 #include <GeomAlgoAPI_FaceBuilder.h>
17
18 #include <ModelAPI_AttributeRefList.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_ResultConstruction.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Session.h>
26
27 #include <SketchPlugin_Sketch.h>
28 #include <SketchPlugin_Feature.h>
29 #include <SketchPlugin_SketchEntity.h>
30
31 #include <memory>
32
33 #include <math.h>
34 #include <vector>
35
36 using namespace std;
37
38 SketchPlugin_Sketch::SketchPlugin_Sketch()
39 {
40 }
41
42 void SketchPlugin_Sketch::initAttributes()
43 {
44   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
45   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
46   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
47   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
48   // the selected face, base for the sketcher plane, not obligatory
49   data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
50   ModelAPI_Session::get()->validators()->registerNotObligatory(
51     getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
52 }
53
54 void SketchPlugin_Sketch::execute()
55 {
56   if (!data()->isValid())
57     return;
58   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
59       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
60
61   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
62       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
63   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
64       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
65   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
66       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
67
68   std::list<ObjectPtr> aFeatures = aRefList->list();
69   if (aFeatures.empty())
70     return;
71
72   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
73   std::shared_ptr<SketchPlugin_Feature> aFeature;
74   std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
75   for (; anIt != aLast; anIt++) {
76     aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
77     if (aFeature) {
78       if (!aFeature->sketch()) // on load document the back references are missed
79         aFeature->setSketch(this);
80       // do not include the external edges into the result
81       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
82         if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value())
83           continue;
84       }
85       // do not include the construction entities in the result
86       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
87         if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_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->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
115   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
116   aConstr->setShape(aBigWire);
117   setResult(aConstr);
118 }
119
120 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
121 {
122   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
123   if (aNew) {
124     std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
125     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
126   }
127   return aNew;
128 }
129
130 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
131 {
132   if (!data().get()) // sketch is already removed (case on undo of sketch), sync is not needed
133     return;
134   list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
135   list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
136   bool isRemoved = false;
137   bool aHasEmtpyFeature = false;
138   for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
139     std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
140     if (aFeature.get() != NULL && aFeature == theFeature) {
141       data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
142       isRemoved = true;
143     }
144     else if (aFeature.get() == NULL)
145       aHasEmtpyFeature = true;
146   }
147   // if the object is not found in the sketch sub-elements, that means that the object is removed already.
148   // Find the first empty element and remove it
149   if (!isRemoved && aHasEmtpyFeature)
150     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
151 }
152
153 int SketchPlugin_Sketch::numberOfSubs() const
154 {
155   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
156 }
157
158 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
159 {
160   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
161   return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
162 }
163
164 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
165 {
166   return subFeature(theIndex)->data()->featureId();
167 }
168
169 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
170 {
171   // check is this feature of result
172   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
173   if (!aFeature) {
174     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
175     if (aRes)
176       aFeature = document()->feature(aRes);
177   }
178   if (aFeature) {
179     list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
180     for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
181       if (*aSubIt == aFeature)
182         return true;
183     }
184   }
185   return false;
186 }
187
188
189 void SketchPlugin_Sketch::erase()
190 {
191   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
192       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
193   std::list<ObjectPtr> aFeatures = aRefList->list();
194   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
195   for (; anIt != aFeatures.end(); anIt++) {
196     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
197     if (aFeature) {
198       // subs are referenced from sketch, but must be removed for sure, so not checkings
199       document()->removeFeature(aFeature);
200     }
201   }
202   ModelAPI_CompositeFeature::erase();
203 }
204
205 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
206   if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
207     std::shared_ptr<GeomAPI_Shape> aSelection = 
208       data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
209     if (aSelection) { // update arguments due to the selection value
210       // update the sketch plane
211       std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
212       if (aPlane) {
213         double anA, aB, aC, aD;
214         aPlane->coefficients(anA, aB, aC, aD);
215
216         // calculate attributes of the sketch
217         std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
218         std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
219         std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
220         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
221         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
222         // X axis is preferable to be dirX on the sketch
223         static const double tol = 1.e-7;
224         bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
225         std::shared_ptr<GeomAPI_Dir> aTempDir(
226           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
227         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
228         std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
229
230         // update position of the sketch
231         std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
232           <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
233         anOrigin->setValue(anOrigPnt);
234         std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
235           data()->attribute(SketchPlugin_Sketch::NORM_ID()));
236         aNormal->setValue(aNormDir);
237         std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
238           data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
239         aDirX->setValue(aXDir);
240         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
241       }
242     }
243   }
244 }