Salome HOME
Merge branch 'master' of newgeom:newgeom
[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
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
25 #include <SketchPlugin_Sketch.h>
26
27 #include <boost/smart_ptr/shared_ptr.hpp>
28
29 #include <vector>
30
31 using namespace std;
32
33 SketchPlugin_Sketch::SketchPlugin_Sketch()
34 {
35 }
36
37 void SketchPlugin_Sketch::initAttributes()
38 {
39   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
40   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
41   data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
42   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
43   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
44 }
45
46 void SketchPlugin_Sketch::execute()
47 {
48   if (!data()->isValid())
49     return;
50   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
51       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
52
53   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
54       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
55   boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
56       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
57   boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
58       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
59   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
60       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
61
62   std::list<ObjectPtr> aFeatures = aRefList->list();
63   if (aFeatures.empty())
64     return;
65
66   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
67   boost::shared_ptr<SketchPlugin_Feature> aFeature;
68   std::list<boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
69   for (; anIt != aLast; anIt++) {
70     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
71     if (aFeature) {
72       // do not include the external edges into the result
73       if (aFeature->data()->attribute(SketchPlugin_Feature::EXTERNAL_ID())) {
74         if (aFeature->data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value())
75           continue;
76       }
77
78       const std::list<boost::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
79       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
80       for (; aResIter != aRes.cend(); aResIter++) {
81         boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = boost::dynamic_pointer_cast<
82             ModelAPI_ResultConstruction>(*aResIter);
83         if (aConstr) {
84           boost::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
85           if (aShape)
86             aFeaturesPreview.push_back(aShape);
87         }
88       }
89     }
90   }
91
92   if (aFeaturesPreview.empty())
93     return;
94
95   // Collect all edges as one big wire
96   boost::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
97   std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
98   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
99     aBigWire->addEdge(*aShapeIt);
100   }
101   aBigWire->setOrigin(anOrigin->pnt());
102   aBigWire->setDirX(aDirX->dir());
103   aBigWire->setDirY(aDirY->dir());
104   aBigWire->setNorm(aNorm->dir());
105
106 //  GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
107 //                                         aFeaturesPreview, aLoops, aWires);
108   boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
109   aConstr->setShape(aBigWire);
110   setResult(aConstr);
111 }
112
113 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
114 {
115   boost::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
116   if (aNew) {
117     boost::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
118     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
119   }
120   return aNew;
121 }
122
123 int SketchPlugin_Sketch::numberOfSubs() const
124 {
125   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
126 }
127
128 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
129 {
130   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
131   return boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
132 }
133
134 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
135 {
136   return subFeature(theIndex)->data()->featureId();
137 }
138
139 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
140 {
141   // check is this feature of result
142   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
143   if (!aFeature) {
144     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
145     if (aRes)
146       aFeature = document()->feature(aRes);
147   }
148   if (aFeature) {
149     list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
150     for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
151       if (*aSubIt == aFeature)
152         return true;
153     }
154   }
155   return false;
156 }
157
158 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
159 {
160   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
161       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
162   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
163       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
164   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
165       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
166
167   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
168       ->added(aY->dir()->xyz()->multiplied(theY));
169
170   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
171 }
172
173 boost::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
174   const boost::shared_ptr<GeomAPI_Pnt>& thePnt)
175 {
176   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
177       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
178   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
179       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
180   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
181       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
182   return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
183 }
184
185
186 bool SketchPlugin_Sketch::isPlaneSet()
187 {
188   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
189       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
190
191   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
192 }
193
194 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
195 {
196   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
197       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
198   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
199       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
200
201   if (!anOrigin || !aNorm)
202     return boost::shared_ptr<GeomAPI_Pln>();
203
204   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
205 }
206
207 void addPlane(double theX, double theY, double theZ,
208               std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
209 {
210   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
211   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
212   double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
213   boost::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
214                                                                            aSize);
215   theShapes.push_back(aFace);
216 }
217
218 AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
219 {
220   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
221       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
222
223   if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
224     AISObjectPtr aAIS = thePrevious;
225     if (!aAIS) {
226       std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
227
228       addPlane(1, 0, 0, aFaces);  // YZ plane
229       addPlane(0, 1, 0, aFaces);  // XZ plane
230       addPlane(0, 0, 1, aFaces);  // XY plane
231       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
232       aAIS = AISObjectPtr(new GeomAPI_AISObject());
233       aAIS->createShape(aCompound);
234
235       std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
236       SKETCH_PLANE_COLOR);
237       aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
238
239       aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
240       SKETCH_WIDTH));
241     }
242     return aAIS;
243   }
244   return AISObjectPtr();
245 }
246
247 void SketchPlugin_Sketch::erase()
248 {
249   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
250       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
251   std::list<ObjectPtr> aFeatures = aRefList->list();
252   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
253   for (; anIt != aFeatures.end(); anIt++) {
254     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
255     if (aFeature) {
256        // subs are referenced from sketch, but must be removed for sure, so not checkings
257       document()->removeFeature(aFeature, false);
258     }
259   }
260   ModelAPI_CompositeFeature::erase();
261 }