Salome HOME
35d90ab5d8682df9c526f89fc87cffe71e35ad20
[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 <GeomAPI_XYZ.h>
8 #include <GeomDataAPI_Dir.h>
9 #include <GeomDataAPI_Point.h>
10 #include <GeomAlgoAPI_FaceBuilder.h>
11 #include <GeomAlgoAPI_CompoundBuilder.h>
12
13 using namespace std;
14
15 /// the active sketch
16 boost::shared_ptr<SketchPlugin_Sketch> MY_ACITVE_SKETCH;
17
18 // face of the square-face displayed for selection of general plane
19 const double PLANE_SIZE = 200;
20
21 SketchPlugin_Sketch::SketchPlugin_Sketch()
22 {
23 }
24
25 void SketchPlugin_Sketch::initAttributes()
26 {
27   data()->addAttribute(SKETCH_ATTR_ORIGIN, GeomDataAPI_Point::type());
28   data()->addAttribute(SKETCH_ATTR_DIRX, GeomDataAPI_Dir::type());
29   data()->addAttribute(SKETCH_ATTR_DIRY, GeomDataAPI_Dir::type());
30   data()->addAttribute(SKETCH_ATTR_NORM, GeomDataAPI_Dir::type());
31 }
32
33 void SketchPlugin_Sketch::execute() 
34 {
35 }
36
37 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Sketch::preview()
38 {
39   std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
40
41   addPlane(1, 0, 0, aFaces); // YZ plane
42   addPlane(0, 1, 0, aFaces); // XZ plane
43   addPlane(0, 0, 1, aFaces); // XY plane
44   boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
45   setPreview(aCompound);
46
47   return getPreview();
48 }
49
50 const void SketchPlugin_Sketch::addSub(const boost::shared_ptr<ModelAPI_Feature>& theFeature)
51 {
52   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
53 }
54
55 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
56                                    std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
57 {
58   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
59   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
60   boost::shared_ptr<GeomAPI_Shape> aFace = 
61     GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
62   theShapes.push_back(aFace);
63 }
64
65 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
66 {
67   boost::shared_ptr<GeomDataAPI_Point> aC = 
68     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
69   boost::shared_ptr<GeomDataAPI_Dir> aX = 
70     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
71   boost::shared_ptr<GeomDataAPI_Dir> aY = 
72     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
73
74   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
75     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
76
77   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
78 }