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