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 "SketchPlugin_Sketch.h"
6 #include <ModelAPI_Data.h>
7 #include <ModelAPI_AttributeRefList.h>
8 #include <GeomAPI_AISObject.h>
9 #include <GeomAPI_XYZ.h>
10 #include <GeomDataAPI_Dir.h>
11 #include <GeomDataAPI_Point.h>
12 #include <GeomAlgoAPI_FaceBuilder.h>
13 #include <GeomAlgoAPI_CompoundBuilder.h>
14 #include <GeomAlgoAPI_SketchBuilder.h>
15 #include <ModelAPI_ResultConstruction.h>
16
17 #include <Config_PropManager.h>
18
19 using namespace std;
20
21 SketchPlugin_Sketch::SketchPlugin_Sketch()
22 {
23 }
24
25 void SketchPlugin_Sketch::initAttributes()
26 {
27   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
28   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
29   data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
30   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
31   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
32 }
33
34 void SketchPlugin_Sketch::execute()
35 {
36   if (!data()->isValid())
37     return;
38   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
39       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
40
41   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
42       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
43   boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
44       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
45   boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
46       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
47   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
48       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
49
50   std::list<ObjectPtr> aFeatures = aRefList->list();
51   if (aFeatures.empty())
52     return;
53
54   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
55   boost::shared_ptr<SketchPlugin_Feature> aFeature;
56   std::list<boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
57   for (; anIt != aLast; anIt++) {
58     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
59     if (aFeature) {
60
61       const std::list<boost::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
62       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
63       for (; aResIter != aRes.cend(); aResIter++) {
64         boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = boost::dynamic_pointer_cast<
65             ModelAPI_ResultConstruction>(*aResIter);
66         if (aConstr) {
67           boost::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
68           if (aShape)
69             aFeaturesPreview.push_back(aShape);
70         }
71       }
72     }
73   }
74
75   if (aFeaturesPreview.empty())
76     return;
77   std::list<boost::shared_ptr<GeomAPI_Shape> > aLoops;
78   std::list<boost::shared_ptr<GeomAPI_Shape> > aWires;
79   GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
80                                          aFeaturesPreview, aLoops, aWires);
81
82   aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
83   boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
84   boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
85   aConstr->setShape(aCompound);
86   setResult(aConstr);
87 }
88
89 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
90 {
91   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
92   data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(theFeature);
93 }
94
95 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
96 {
97   boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
98       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
99   boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
100       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
101   boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
102       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
103
104   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
105       ->added(aY->dir()->xyz()->multiplied(theY));
106
107   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
108 }
109
110 bool SketchPlugin_Sketch::isPlaneSet()
111 {
112   boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
113       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
114
115   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
116 }
117
118 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
119 {
120   boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
121       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
122   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
123       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
124
125   if (!anOrigin || !aNorm)
126     return boost::shared_ptr<GeomAPI_Pln>();
127
128   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
129 }
130
131 void addPlane(double theX, double theY, double theZ,
132               std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
133 {
134   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
135   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
136   double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
137   boost::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
138                                                                            aSize);
139   theShapes.push_back(aFace);
140 }
141
142 AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
143 {
144   boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
145       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
146
147   if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
148     AISObjectPtr aAIS = thePrevious;
149     if (!aAIS) {
150       std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
151
152       addPlane(1, 0, 0, aFaces);  // YZ plane
153       addPlane(0, 1, 0, aFaces);  // XZ plane
154       addPlane(0, 0, 1, aFaces);  // XY plane
155       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
156       aAIS = AISObjectPtr(new GeomAPI_AISObject());
157       aAIS->createShape(aCompound);
158
159       std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
160       SKETCH_PLANE_COLOR);
161       aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
162
163       aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thikness",
164       SKETCH_WIDTH));
165     }
166     return aAIS;
167   }
168   return AISObjectPtr();
169 }
170
171 void SketchPlugin_Sketch::erase()
172 {
173   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
174       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
175   std::list<ObjectPtr> aFeatures = aRefList->list();
176   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
177   for (; anIt != aFeatures.end(); anIt++) {
178     FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
179     if (aFeature) {
180        // subs are referenced from sketch, but must be removed for sure, so not checkings
181       document()->removeFeature(aFeature, false);
182     }
183   }
184   SketchPlugin_Feature::erase();
185 }