]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Sketch.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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_AISObject.h>
9 #include <GeomAPI_XYZ.h>
10 #include <GeomDataAPI_Dir.h>
11 #include <GeomDataAPI_Point.h>
12 #include <GeomAlgoAPI_FaceBuilder.h>
13 #include <GeomAlgoAPI_CompoundBuilder.h>
14 #include <GeomAlgoAPI_SketchBuilder.h>
15 #include <ModelAPI_ResultConstruction.h>
16
17
18 const int SKETCH_PLANE_COLOR = Colors::COLOR_BROWN; /// the plane edge color
19 const double SKETCH_WIDTH = 4.0; /// the plane edge width
20
21 using namespace std;
22
23 // face of the square-face displayed for selection of general plane
24 const double PLANE_SIZE = 200;
25
26 SketchPlugin_Sketch::SketchPlugin_Sketch()
27 {
28 }
29
30 void SketchPlugin_Sketch::initAttributes()
31 {
32   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
33   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
34   data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
35   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
36   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
37 }
38
39 void SketchPlugin_Sketch::execute() 
40 {
41   if (!isPlaneSet()) {
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     boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = 
49       document()->createConstruction(data());
50     aConstr->setShape(aCompound);
51     setResult(aConstr);
52     return;
53   }
54   if (!data()->isValid())
55     return ;
56   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
57     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
58
59   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
60     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
61   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
62     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
63   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
64     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
65   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
66     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
67
68   std::list<ObjectPtr> aFeatures = aRefList->list();
69   if (aFeatures.empty())
70     return ;
71
72   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
73   boost::shared_ptr<SketchPlugin_Feature> aFeature;
74   std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
75   for (; anIt != aLast; anIt++) {
76     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
77     boost::shared_ptr<ModelAPI_ResultConstruction> aRes = 
78       boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFeature->firstResult());
79     if (aRes) {
80       boost::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
81       if (aShape)
82         aFeaturesPreview.push_back(aShape);
83     }
84   }
85
86   if (aFeaturesPreview.empty())
87     return ;
88   std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
89   std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
90   GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
91                                          aFeaturesPreview, aLoops, aWires);
92
93   aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
94   boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
95   boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
96   aConstr->setShape(aCompound);
97   setResult(aConstr);
98 }
99
100 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_Sketch::getAISObject(
101                                 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
102 {
103   boost::shared_ptr<GeomAPI_AISObject> aResult = simpleAISObject(firstResult(), thePrevious);
104   aResult->setColor(SKETCH_PLANE_COLOR);
105   aResult->setWidth(SKETCH_WIDTH);
106   //anAIS->Redisplay();
107   return aResult;
108 }
109
110 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
111 {
112   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
113   data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(theFeature);
114 }
115
116 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
117                                    std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
118 {
119   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
120   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
121   boost::shared_ptr<GeomAPI_Shape> aFace = 
122     GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
123   theShapes.push_back(aFace);
124 }
125
126 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
127 {
128   boost::shared_ptr<GeomDataAPI_Point> aC = 
129     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
130   boost::shared_ptr<GeomDataAPI_Dir> aX = 
131     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
132   boost::shared_ptr<GeomDataAPI_Dir> aY = 
133     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
134
135   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
136     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
137
138   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
139 }
140
141 bool SketchPlugin_Sketch::isPlaneSet()
142 {
143   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
144     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
145
146   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
147 }
148
149 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
150 {
151   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
152     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
153   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
154     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
155
156   if (!anOrigin || !aNorm)
157     return boost::shared_ptr<GeomAPI_Pln>();
158
159   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
160 }