]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Sketch.cpp
Salome HOME
a8fef3263d7e4a5b3c3922a851e92dff80db7c0b
[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_Wire.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
73       const std::list<boost::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
74       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
75       for (; aResIter != aRes.cend(); aResIter++) {
76         boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = boost::dynamic_pointer_cast<
77             ModelAPI_ResultConstruction>(*aResIter);
78         if (aConstr) {
79           boost::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
80           if (aShape)
81             aFeaturesPreview.push_back(aShape);
82         }
83       }
84     }
85   }
86
87   if (aFeaturesPreview.empty())
88     return;
89
90   // Collect all edges as one big wire
91   boost::shared_ptr<GeomAPI_Wire> aBigWire(new GeomAPI_Wire);
92   std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
93   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
94     aBigWire->addEdge(*aShapeIt);
95   }
96 //  GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
97 //                                         aFeaturesPreview, aLoops, aWires);
98   boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
99   aConstr->setShape(aBigWire);
100   setResult(aConstr);
101 }
102
103 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
104 {
105   boost::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
106   if (aNew) {
107     boost::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
108     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
109   }
110   return aNew;
111 }
112
113 int SketchPlugin_Sketch::numberOfSubs() const
114 {
115   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
116 }
117
118 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
119 {
120   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
121   return boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
122 }
123
124 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
125 {
126   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
127       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
128   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
129       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
130   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
131       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
132
133   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
134       ->added(aY->dir()->xyz()->multiplied(theY));
135
136   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
137 }
138
139 bool SketchPlugin_Sketch::isPlaneSet()
140 {
141   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
142       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
143
144   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
145 }
146
147 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
148 {
149   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
150       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
151   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
152       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
153
154   if (!anOrigin || !aNorm)
155     return boost::shared_ptr<GeomAPI_Pln>();
156
157   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
158 }
159
160 void addPlane(double theX, double theY, double theZ,
161               std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
162 {
163   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
164   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
165   double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
166   boost::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
167                                                                            aSize);
168   theShapes.push_back(aFace);
169 }
170
171 AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
172 {
173   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
174       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
175
176   if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
177     AISObjectPtr aAIS = thePrevious;
178     if (!aAIS) {
179       std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
180
181       addPlane(1, 0, 0, aFaces);  // YZ plane
182       addPlane(0, 1, 0, aFaces);  // XZ plane
183       addPlane(0, 0, 1, aFaces);  // XY plane
184       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
185       aAIS = AISObjectPtr(new GeomAPI_AISObject());
186       aAIS->createShape(aCompound);
187
188       std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
189       SKETCH_PLANE_COLOR);
190       aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
191
192       aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thikness",
193       SKETCH_WIDTH));
194     }
195     return aAIS;
196   }
197   return AISObjectPtr();
198 }
199
200 void SketchPlugin_Sketch::erase()
201 {
202   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
203       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
204   std::list<ObjectPtr> aFeatures = aRefList->list();
205   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
206   for (; anIt != aFeatures.end(); anIt++) {
207     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
208     if (aFeature) {
209        // subs are referenced from sketch, but must be removed for sure, so not checkings
210       document()->removeFeature(aFeature, false);
211     }
212   }
213   ModelAPI_CompositeFeature::erase();
214 }