Salome HOME
850058e8c1d438da7edb4a59c41fcac9d05ded49
[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_XYZ.h>
9 #include <GeomDataAPI_Dir.h>
10 #include <GeomDataAPI_Point.h>
11 #include <GeomAlgoAPI_FaceBuilder.h>
12 #include <GeomAlgoAPI_CompoundBuilder.h>
13 #include <GeomAlgoAPI_SketchBuilder.h>
14
15 using namespace std;
16
17 // face of the square-face displayed for selection of general plane
18 const double PLANE_SIZE = 200;
19
20 SketchPlugin_Sketch::SketchPlugin_Sketch()
21 {
22 }
23
24 void SketchPlugin_Sketch::initAttributes()
25 {
26   data()->addAttribute(SKETCH_ATTR_ORIGIN, GeomDataAPI_Point::type());
27   data()->addAttribute(SKETCH_ATTR_DIRX, GeomDataAPI_Dir::type());
28   data()->addAttribute(SKETCH_ATTR_DIRY, GeomDataAPI_Dir::type());
29   data()->addAttribute(SKETCH_ATTR_NORM, GeomDataAPI_Dir::type());
30   data()->addAttribute(SKETCH_ATTR_FEATURES, ModelAPI_AttributeRefList::type());
31 }
32
33 void SketchPlugin_Sketch::execute() 
34 {
35   if (!data()->isValid())
36     return ;
37   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
38     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(SKETCH_ATTR_FEATURES));
39
40   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
41     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
42   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
43     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
44   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
45     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
46
47   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
48   if (aFeatures.empty())
49     return ;
50
51   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
52                                                                   aLast = aFeatures.end();
53
54   boost::shared_ptr<SketchPlugin_Feature> aFeature;
55   std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
56   for (; anIt != aLast; anIt++) {
57     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
58     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
59     if (aPreview)
60       aFeaturesPreview.push_back(aPreview);
61   }
62
63   std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
64   std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
65   GeomAlgoAPI_SketchBuilder::createFaces(aDirX->dir(), aDirY->dir(), aNorm->dir(),
66                                          aFeaturesPreview, aLoops, aWires);
67
68   aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
69   boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
70   data()->store(aCompound);
71 }
72
73 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Sketch::preview()
74 {
75   if (isPlaneSet()) {
76     setPreview(boost::shared_ptr<GeomAPI_Shape>());
77   }
78   else {
79     std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
80
81     addPlane(1, 0, 0, aFaces); // YZ plane
82     addPlane(0, 1, 0, aFaces); // XZ plane
83     addPlane(0, 0, 1, aFaces); // XY plane
84     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
85     setPreview(aCompound);
86   }
87   return getPreview();
88 }
89
90 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
91 {
92   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
93   data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
94 }
95
96 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
97                                    std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
98 {
99   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
100   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
101   boost::shared_ptr<GeomAPI_Shape> aFace = 
102     GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
103   theShapes.push_back(aFace);
104 }
105
106 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
107 {
108   boost::shared_ptr<GeomDataAPI_Point> aC = 
109     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
110   boost::shared_ptr<GeomDataAPI_Dir> aX = 
111     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
112   boost::shared_ptr<GeomDataAPI_Dir> aY = 
113     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
114
115   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
116     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
117
118   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
119 }
120
121 bool SketchPlugin_Sketch::isPlaneSet()
122 {
123   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
124     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
125
126   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
127 }