Salome HOME
Merge branch 'master' of newgeom:newgeom
[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
14 using namespace std;
15
16 // face of the square-face displayed for selection of general plane
17 const double PLANE_SIZE = 200;
18
19 SketchPlugin_Sketch::SketchPlugin_Sketch()
20 {
21 }
22
23 void SketchPlugin_Sketch::initAttributes()
24 {
25   data()->addAttribute(SKETCH_ATTR_ORIGIN, GeomDataAPI_Point::type());
26   data()->addAttribute(SKETCH_ATTR_DIRX, GeomDataAPI_Dir::type());
27   data()->addAttribute(SKETCH_ATTR_DIRY, GeomDataAPI_Dir::type());
28   data()->addAttribute(SKETCH_ATTR_NORM, GeomDataAPI_Dir::type());
29   data()->addAttribute(SKETCH_ATTR_FEATURES, ModelAPI_AttributeRefList::type());
30 }
31
32 void SketchPlugin_Sketch::execute() 
33 {
34 }
35
36 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Sketch::preview()
37 {
38   if (isPlaneSet()) {
39     setPreview(boost::shared_ptr<GeomAPI_Shape>());
40   }
41   else {
42     std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
43
44     addPlane(1, 0, 0, aFaces); // YZ plane
45     addPlane(0, 1, 0, aFaces); // XZ plane
46     addPlane(0, 0, 1, aFaces); // XY plane
47     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
48     setPreview(aCompound);
49   }
50   return getPreview();
51 }
52
53 const void SketchPlugin_Sketch::addSub(const boost::shared_ptr<ModelAPI_Feature>& theFeature)
54 {
55   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
56   data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
57 }
58
59 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
60                                    std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
61 {
62   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
63   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
64   boost::shared_ptr<GeomAPI_Shape> aFace = 
65     GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
66   theShapes.push_back(aFace);
67 }
68
69 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
70 {
71   boost::shared_ptr<GeomDataAPI_Point> aC = 
72     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
73   boost::shared_ptr<GeomDataAPI_Dir> aX = 
74     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
75   boost::shared_ptr<GeomDataAPI_Dir> aY = 
76     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
77
78   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
79     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
80
81   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
82 }
83
84 bool SketchPlugin_Sketch::isPlaneSet()
85 {
86   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
87     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
88
89   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
90 }