]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Sketch.cpp
Salome HOME
bc9d72735210f7e0e9647aa93afa68a1cb16f5a7
[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
69 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Sketch::preview()
70 {
71   if (isPlaneSet()) {
72     setPreview(boost::shared_ptr<GeomAPI_Shape>());
73   }
74   else {
75     std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
76
77     addPlane(1, 0, 0, aFaces); // YZ plane
78     addPlane(0, 1, 0, aFaces); // XZ plane
79     addPlane(0, 0, 1, aFaces); // XY plane
80     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
81     setPreview(aCompound);
82   }
83   return getPreview();
84 }
85
86 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
87 {
88   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
89   data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
90 }
91
92 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
93                                    std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
94 {
95   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
96   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
97   boost::shared_ptr<GeomAPI_Shape> aFace = 
98     GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
99   theShapes.push_back(aFace);
100 }
101
102 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
103 {
104   boost::shared_ptr<GeomDataAPI_Point> aC = 
105     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
106   boost::shared_ptr<GeomDataAPI_Dir> aX = 
107     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
108   boost::shared_ptr<GeomDataAPI_Dir> aY = 
109     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
110
111   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
112     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
113
114   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
115 }
116
117 bool SketchPlugin_Sketch::isPlaneSet()
118 {
119   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
120     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
121
122   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
123 }