Salome HOME
External edges for sketch: lines and circles
[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
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_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
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   aBigWire->setOrigin(anOrigin->pnt());
97   aBigWire->setDirX(aDirX->dir());
98   aBigWire->setDirY(aDirY->dir());
99   aBigWire->setNorm(aNorm->dir());
100
101 //  GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
102 //                                         aFeaturesPreview, aLoops, aWires);
103   boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
104   aConstr->setShape(aBigWire);
105   setResult(aConstr);
106 }
107
108 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
109 {
110   boost::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
111   if (aNew) {
112     boost::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
113     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
114   }
115   return aNew;
116 }
117
118 int SketchPlugin_Sketch::numberOfSubs() const
119 {
120   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
121 }
122
123 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
124 {
125   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
126   return boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
127 }
128
129 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
130 {
131   return subFeature(theIndex)->data()->featureId();
132 }
133
134 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
135 {
136   // check is this feature of result
137   FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
138   if (!aFeature) {
139     ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
140     if (aRes)
141       aFeature = document()->feature(aRes);
142   }
143   if (aFeature) {
144     list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
145     for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
146       if (*aSubIt == aFeature)
147         return true;
148     }
149   }
150   return false;
151 }
152
153 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
154 {
155   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
156       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
157   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
158       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
159   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
160       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
161
162   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
163       ->added(aY->dir()->xyz()->multiplied(theY));
164
165   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
166 }
167
168 boost::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
169   const boost::shared_ptr<GeomAPI_Pnt>& thePnt)
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   return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
178 }
179
180
181 bool SketchPlugin_Sketch::isPlaneSet()
182 {
183   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
184       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
185
186   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
187 }
188
189 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
190 {
191   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
192       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
193   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
194       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
195
196   if (!anOrigin || !aNorm)
197     return boost::shared_ptr<GeomAPI_Pln>();
198
199   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
200 }
201
202 void addPlane(double theX, double theY, double theZ,
203               std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
204 {
205   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
206   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
207   double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
208   boost::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
209                                                                            aSize);
210   theShapes.push_back(aFace);
211 }
212
213 AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
214 {
215   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
216       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
217
218   if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
219     AISObjectPtr aAIS = thePrevious;
220     if (!aAIS) {
221       std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
222
223       addPlane(1, 0, 0, aFaces);  // YZ plane
224       addPlane(0, 1, 0, aFaces);  // XZ plane
225       addPlane(0, 0, 1, aFaces);  // XY plane
226       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
227       aAIS = AISObjectPtr(new GeomAPI_AISObject());
228       aAIS->createShape(aCompound);
229
230       std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
231       SKETCH_PLANE_COLOR);
232       aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
233
234       aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
235       SKETCH_WIDTH));
236     }
237     return aAIS;
238   }
239   return AISObjectPtr();
240 }
241
242 void SketchPlugin_Sketch::erase()
243 {
244   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
245       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
246   std::list<ObjectPtr> aFeatures = aRefList->list();
247   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
248   for (; anIt != aFeatures.end(); anIt++) {
249     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
250     if (aFeature) {
251        // subs are referenced from sketch, but must be removed for sure, so not checkings
252       document()->removeFeature(aFeature, false);
253     }
254   }
255   ModelAPI_CompositeFeature::erase();
256 }