Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Sketch.cpp
1 // File:        SketchPlugin_Sketch.cxx
2 // Created:     27 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Config_PropManager.h>
6
7 #include <GeomAlgoAPI_CompoundBuilder.h>
8 #include <GeomAlgoAPI_FaceBuilder.h>
9
10 #include <GeomAPI_AISObject.h>
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_XYZ.h>
14
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomDataAPI_Point.h>
17 #include <GeomAlgoAPI_FaceBuilder.h>
18
19 #include <ModelAPI_AttributeRefList.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Object.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_Validator.h>
26 #include <ModelAPI_Session.h>
27
28 #include <SketchPlugin_Sketch.h>
29 #include <SketchPlugin_Feature.h>
30
31 #include <boost/smart_ptr/shared_ptr.hpp>
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::type());
45   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
46   data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
47   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
48   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
49   // the selected face, base for the sketcher plane, not obligatory
50   data()->addAttribute(SketchPlugin_Feature::EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
51   ModelAPI_Session::get()->validators()->registerNotObligatory(
52     getKind(), SketchPlugin_Feature::EXTERNAL_ID());
53 }
54
55 void SketchPlugin_Sketch::execute()
56 {
57   if (!data()->isValid())
58     return;
59   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
60       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
61
62   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
63       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
64   boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
65       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
66   boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
67       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
68   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::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   boost::shared_ptr<SketchPlugin_Feature> aFeature;
77   std::list<boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
78   for (; anIt != aLast; anIt++) {
79     aFeature = boost::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_Feature::EXTERNAL_ID())) {
85         if (aFeature->data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value())
86           continue;
87       }
88
89       const std::list<boost::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
90       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
91       for (; aResIter != aRes.cend(); aResIter++) {
92         boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = boost::dynamic_pointer_cast<
93             ModelAPI_ResultConstruction>(*aResIter);
94         if (aConstr) {
95           boost::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
96           if (aShape)
97             aFeaturesPreview.push_back(aShape);
98         }
99       }
100     }
101   }
102
103   if (aFeaturesPreview.empty())
104     return;
105
106   // Collect all edges as one big wire
107   boost::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
108   std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
109   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
110     aBigWire->addEdge(*aShapeIt);
111   }
112   aBigWire->setOrigin(anOrigin->pnt());
113   aBigWire->setDirX(aDirX->dir());
114   aBigWire->setDirY(aDirY->dir());
115   aBigWire->setNorm(aNorm->dir());
116
117 //  GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
118 //                                         aFeaturesPreview, aLoops, aWires);
119   boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
120   aConstr->setShape(aBigWire);
121   setResult(aConstr);
122 }
123
124 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
125 {
126   boost::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
127   if (aNew) {
128     boost::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
129     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
130   }
131   return aNew;
132 }
133
134 int SketchPlugin_Sketch::numberOfSubs() const
135 {
136   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
137 }
138
139 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
140 {
141   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
142   return boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
143 }
144
145 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
146 {
147   return subFeature(theIndex)->data()->featureId();
148 }
149
150 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
151 {
152   // check is this feature of result
153   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
154   if (!aFeature) {
155     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
156     if (aRes)
157       aFeature = document()->feature(aRes);
158   }
159   if (aFeature) {
160     list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
161     for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
162       if (*aSubIt == aFeature)
163         return true;
164     }
165   }
166   return false;
167 }
168
169 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
170 {
171   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
172       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
173   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
174       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
175   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
176       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
177
178   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
179       ->added(aY->dir()->xyz()->multiplied(theY));
180
181   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
182 }
183
184 boost::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
185   const boost::shared_ptr<GeomAPI_Pnt>& thePnt)
186 {
187   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
188       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
189   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
190       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
191   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
192       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
193   return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
194 }
195
196
197 bool SketchPlugin_Sketch::isPlaneSet()
198 {
199   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
200       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
201
202   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
203 }
204
205 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
206 {
207   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
208       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
209   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
210       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
211
212   if (!anOrigin || !aNorm)
213     return boost::shared_ptr<GeomAPI_Pln>();
214
215   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
216 }
217
218 void addPlane(double theX, double theY, double theZ,
219               std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
220 {
221   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
222   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
223   double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
224   boost::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
225                                                                            aSize);
226   theShapes.push_back(aFace);
227 }
228
229 AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
230 {
231   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
232       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
233
234   if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
235     AISObjectPtr aAIS = thePrevious;
236     if (!aAIS) {
237       std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
238
239       addPlane(1, 0, 0, aFaces);  // YZ plane
240       addPlane(0, 1, 0, aFaces);  // XZ plane
241       addPlane(0, 0, 1, aFaces);  // XY plane
242       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
243       aAIS = AISObjectPtr(new GeomAPI_AISObject());
244       aAIS->createShape(aCompound);
245
246       std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
247       SKETCH_PLANE_COLOR);
248       aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
249
250       aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
251       SKETCH_WIDTH));
252     }
253     return aAIS;
254   }
255   return AISObjectPtr();
256 }
257
258 void SketchPlugin_Sketch::erase()
259 {
260   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
261       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
262   std::list<ObjectPtr> aFeatures = aRefList->list();
263   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
264   for (; anIt != aFeatures.end(); anIt++) {
265     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
266     if (aFeature) {
267        // subs are referenced from sketch, but must be removed for sure, so not checkings
268       document()->removeFeature(aFeature, false);
269     }
270   }
271   ModelAPI_CompositeFeature::erase();
272 }
273
274 void SketchPlugin_Sketch::attributeChanged() {
275   static bool kIsUpdated = false; // to avoid infinitive cycle on attrubtes change
276   static bool kIsAttrChanged = false;
277   boost::shared_ptr<GeomAPI_Shape> aSelection = 
278     data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value();
279   if (aSelection && !kIsUpdated) { // update arguments due to the selection value
280     kIsUpdated = true;
281     // update the sketch plane
282     boost::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       boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
289       boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
290       boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
291       aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
292       boost::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       boost::shared_ptr<GeomAPI_Dir> aTempDir(
297         isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
298       boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
299       boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
300
301       kIsAttrChanged = false; // track the attributes were really changed during the plane update
302       boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast
303         <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
304       anOrigin->setValue(anOrigPnt);
305       boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
306         data()->attribute(SketchPlugin_Sketch::NORM_ID()));
307       aNormal->setValue(aNormDir);
308       boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
309         data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
310       aDirX->setValue(aXDir);
311       boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
312         data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
313       aDirY->setValue(aYDir);
314       boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
315
316       if (kIsAttrChanged) {
317         /* now it is in updater
318         // the plane was changed, so reexecute sub-elements to update shapes (located in new plane)
319         ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
320         list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
321         for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
322           boost::shared_ptr<SketchPlugin_Feature> aFeature = 
323             boost::dynamic_pointer_cast<SketchPlugin_Feature>(*aSubIt);
324           if (aFeature && aFactory->validate(aFeature)) {
325             aFeature->execute();
326           }
327         }
328         */
329         kIsAttrChanged = false;
330       }
331     }
332     kIsUpdated = false;
333   } else if (kIsUpdated) { // other attributes are updated during the selection comupation
334     kIsAttrChanged = true;
335   }
336 }