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