Salome HOME
14a9d4897d4d97f91d3f0c82772cee289d623ed4
[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 <vector>
34
35 using namespace std;
36
37 SketchPlugin_Sketch::SketchPlugin_Sketch()
38 {
39 }
40
41 void SketchPlugin_Sketch::initAttributes()
42 {
43   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
44   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
45   data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
46   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
47   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
48   // the selected face, base for the sketcher plane, not obligatory
49   data()->addAttribute(SketchPlugin_Feature::EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
50   ModelAPI_Session::get()->validators()->registerNotObligatory(
51     getKind(), SketchPlugin_Feature::EXTERNAL_ID());
52 }
53
54 void SketchPlugin_Sketch::execute()
55 {
56   if (!data()->isValid())
57     return;
58   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
59       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
60
61   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
62       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
63   boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
64       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
65   boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
66       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
67   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
68       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
69
70   std::list<ObjectPtr> aFeatures = aRefList->list();
71   if (aFeatures.empty())
72     return;
73
74   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
75   boost::shared_ptr<SketchPlugin_Feature> aFeature;
76   std::list<boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
77   for (; anIt != aLast; anIt++) {
78     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
79     if (aFeature) {
80       // do not include the external edges into the result
81       if (aFeature->data()->attribute(SketchPlugin_Feature::EXTERNAL_ID())) {
82         if (aFeature->data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value())
83           continue;
84       }
85
86       const std::list<boost::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
87       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
88       for (; aResIter != aRes.cend(); aResIter++) {
89         boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = boost::dynamic_pointer_cast<
90             ModelAPI_ResultConstruction>(*aResIter);
91         if (aConstr) {
92           boost::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
93           if (aShape)
94             aFeaturesPreview.push_back(aShape);
95         }
96       }
97     }
98   }
99
100   if (aFeaturesPreview.empty())
101     return;
102
103   // Collect all edges as one big wire
104   boost::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
105   std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
106   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
107     aBigWire->addEdge(*aShapeIt);
108   }
109   aBigWire->setOrigin(anOrigin->pnt());
110   aBigWire->setDirX(aDirX->dir());
111   aBigWire->setDirY(aDirY->dir());
112   aBigWire->setNorm(aNorm->dir());
113
114 //  GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
115 //                                         aFeaturesPreview, aLoops, aWires);
116   boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
117   aConstr->setShape(aBigWire);
118   setResult(aConstr);
119 }
120
121 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
122 {
123   boost::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
124   if (aNew) {
125     boost::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
126     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
127   }
128   return aNew;
129 }
130
131 int SketchPlugin_Sketch::numberOfSubs() const
132 {
133   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
134 }
135
136 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
137 {
138   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
139   return boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
140 }
141
142 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
143 {
144   return subFeature(theIndex)->data()->featureId();
145 }
146
147 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
148 {
149   // check is this feature of result
150   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
151   if (!aFeature) {
152     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
153     if (aRes)
154       aFeature = document()->feature(aRes);
155   }
156   if (aFeature) {
157     list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
158     for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
159       if (*aSubIt == aFeature)
160         return true;
161     }
162   }
163   return false;
164 }
165
166 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
167 {
168   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
169       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
170   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
171       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
172   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
173       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
174
175   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
176       ->added(aY->dir()->xyz()->multiplied(theY));
177
178   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
179 }
180
181 boost::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
182   const boost::shared_ptr<GeomAPI_Pnt>& thePnt)
183 {
184   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
185       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
186   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
187       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
188   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
189       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
190   return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
191 }
192
193
194 bool SketchPlugin_Sketch::isPlaneSet()
195 {
196   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
197       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
198
199   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
200 }
201
202 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
203 {
204   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
205       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
206   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
207       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
208
209   if (!anOrigin || !aNorm)
210     return boost::shared_ptr<GeomAPI_Pln>();
211
212   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
213 }
214
215 void addPlane(double theX, double theY, double theZ,
216               std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
217 {
218   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
219   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
220   double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
221   boost::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
222                                                                            aSize);
223   theShapes.push_back(aFace);
224 }
225
226 AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
227 {
228   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
229       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
230
231   if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
232     AISObjectPtr aAIS = thePrevious;
233     if (!aAIS) {
234       std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
235
236       addPlane(1, 0, 0, aFaces);  // YZ plane
237       addPlane(0, 1, 0, aFaces);  // XZ plane
238       addPlane(0, 0, 1, aFaces);  // XY plane
239       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
240       aAIS = AISObjectPtr(new GeomAPI_AISObject());
241       aAIS->createShape(aCompound);
242
243       std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
244       SKETCH_PLANE_COLOR);
245       aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
246
247       aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
248       SKETCH_WIDTH));
249     }
250     return aAIS;
251   }
252   return AISObjectPtr();
253 }
254
255 void SketchPlugin_Sketch::erase()
256 {
257   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
258       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
259   std::list<ObjectPtr> aFeatures = aRefList->list();
260   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
261   for (; anIt != aFeatures.end(); anIt++) {
262     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
263     if (aFeature) {
264        // subs are referenced from sketch, but must be removed for sure, so not checkings
265       document()->removeFeature(aFeature, false);
266     }
267   }
268   ModelAPI_CompositeFeature::erase();
269 }
270
271 void SketchPlugin_Sketch::attributeChanged() {
272   static bool myIsUpdated = false; // to avoid infinitive cycle on attrubtes change
273   boost::shared_ptr<GeomAPI_Shape> aSelection = 
274     data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value();
275   if (aSelection && !myIsUpdated) { // update arguments due to the selection value
276     myIsUpdated = true;
277     // update the sketch plane
278     boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
279     if (aPlane) {
280       double anA, aB, aC, aD;
281       aPlane->coefficients(anA, aB, aC, aD);
282
283       // calculate attributes of the sketch
284       boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
285       boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
286       boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
287       aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
288       boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
289       // X axis is preferable to be dirX on the sketch
290       static const double tol = 1.e-7;
291       bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
292       boost::shared_ptr<GeomAPI_Dir> aTempDir(
293         isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
294       boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
295       boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
296
297       boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
298         data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
299       anOrigin->setValue(anOrigPnt);
300       boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
301         data()->attribute(SketchPlugin_Sketch::NORM_ID()));
302       aNormal->setValue(aNormDir);
303       boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
304         data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
305       aDirX->setValue(aXDir);
306       boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
307         data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
308       aDirY->setValue(aYDir);
309       boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
310     }
311     myIsUpdated = false;
312   }
313 }