Salome HOME
7bc0ec60084671eb3367d3baf5f3986fecbec408
[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
16
17 const int SKETCH_PLANE_COLOR = Colors::COLOR_BROWN; /// the plane edge color
18 const double SKETCH_WIDTH = 4.0; /// the plane edge width
19
20 using namespace std;
21
22 // face of the square-face displayed for selection of general plane
23 const double PLANE_SIZE = 200;
24
25 SketchPlugin_Sketch::SketchPlugin_Sketch()
26 {
27 }
28
29 void SketchPlugin_Sketch::initAttributes()
30 {
31   data()->addAttribute(SKETCH_ATTR_ORIGIN, GeomDataAPI_Point::type());
32   data()->addAttribute(SKETCH_ATTR_DIRX, GeomDataAPI_Dir::type());
33   data()->addAttribute(SKETCH_ATTR_DIRY, GeomDataAPI_Dir::type());
34   data()->addAttribute(SKETCH_ATTR_NORM, GeomDataAPI_Dir::type());
35   data()->addAttribute(SKETCH_ATTR_FEATURES, ModelAPI_AttributeRefList::type());
36 }
37
38 void SketchPlugin_Sketch::execute() 
39 {
40   if (!data()->isValid())
41     return ;
42   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
43     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(SKETCH_ATTR_FEATURES));
44
45   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
46     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
47   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
48     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
49   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
50     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
51   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
52     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
53
54   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
55   if (aFeatures.empty())
56     return ;
57
58   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
59                                                                   aLast = aFeatures.end();
60
61   boost::shared_ptr<SketchPlugin_Feature> aFeature;
62   std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
63   for (; anIt != aLast; anIt++) {
64     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
65     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
66     if (aPreview)
67       aFeaturesPreview.push_back(aPreview);
68   }
69
70   if (aFeaturesPreview.empty())
71     return ;
72   std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
73   std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
74   GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
75                                          aFeaturesPreview, aLoops, aWires);
76
77   aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
78   boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
79   data()->store(aCompound);
80 }
81
82 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_Sketch::getAISObject(
83                                 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
84 {
85   boost::shared_ptr<GeomAPI_AISObject> anAIS = prepareAISShape(thePrevious);
86   anAIS->setColor(SKETCH_PLANE_COLOR);
87   anAIS->setWidth(SKETCH_WIDTH);
88   //anAIS->Redisplay();
89   return anAIS;
90 }
91
92 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Sketch::preview()
93 {
94   if (isPlaneSet()) {
95     setPreview(boost::shared_ptr<GeomAPI_Shape>());
96   }
97   else {
98     std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
99
100     addPlane(1, 0, 0, aFaces); // YZ plane
101     addPlane(0, 1, 0, aFaces); // XZ plane
102     addPlane(0, 0, 1, aFaces); // XY plane
103     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
104     setPreview(aCompound);
105   }
106   return getPreview();
107 }
108
109 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
110 {
111   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
112   data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
113 }
114
115 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
116                                    std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
117 {
118   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
119   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
120   boost::shared_ptr<GeomAPI_Shape> aFace = 
121     GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
122   theShapes.push_back(aFace);
123 }
124
125 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
126 {
127   boost::shared_ptr<GeomDataAPI_Point> aC = 
128     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
129   boost::shared_ptr<GeomDataAPI_Dir> aX = 
130     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
131   boost::shared_ptr<GeomDataAPI_Dir> aY = 
132     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
133
134   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
135     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
136
137   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
138 }
139
140 bool SketchPlugin_Sketch::isPlaneSet()
141 {
142   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
143     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
144
145   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
146 }
147
148 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
149 {
150   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
151     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
152   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
153     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
154
155   if (!anOrigin || !aNorm)
156     return boost::shared_ptr<GeomAPI_Pln>();
157
158   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
159 }