]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Sketch.cpp
Salome HOME
Preferences improvement
[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 #include <Config_PropManager.h>
18
19 using namespace std;
20
21
22 SketchPlugin_Sketch::SketchPlugin_Sketch()
23 {
24 }
25
26 void SketchPlugin_Sketch::initAttributes()
27 {
28   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
29   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
30   data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
31   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
32   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
33 }
34
35 void SketchPlugin_Sketch::execute() 
36 {
37   if (!data()->isValid())
38     return ;
39   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
40     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
41     data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
42
43   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
44     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
45   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
46     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
47   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
48     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
49   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
50     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
51
52   std::list<ObjectPtr> aFeatures = aRefList->list();
53   if (aFeatures.empty())
54     return ;
55
56   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
57   boost::shared_ptr<SketchPlugin_Feature> aFeature;
58   std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
59   for (; anIt != aLast; anIt++) {
60     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
61     if (aFeature) {
62
63       const std::list<boost::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
64       std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
65       for(; aResIter != aRes.cend(); aResIter++) {
66         boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = 
67           boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
68         if (aConstr) {
69           boost::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
70           if (aShape)
71             aFeaturesPreview.push_back(aShape);
72         }
73       }
74     }
75   }
76
77   if (aFeaturesPreview.empty())
78     return ;
79   std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
80   std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
81   GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
82                                          aFeaturesPreview, aLoops, aWires);
83
84   aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
85   boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
86   boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
87   aConstr->setShape(aCompound);
88   setResult(aConstr);
89 }
90
91 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
92 {
93   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
94   data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(theFeature);
95 }
96
97 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
98 {
99   boost::shared_ptr<GeomDataAPI_Point> aC = 
100     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
101   boost::shared_ptr<GeomDataAPI_Dir> aX = 
102     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
103   boost::shared_ptr<GeomDataAPI_Dir> aY = 
104     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
105
106   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
107     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
108
109   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
110 }
111
112 bool SketchPlugin_Sketch::isPlaneSet()
113 {
114   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
115     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
116
117   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
118 }
119
120 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
121 {
122   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
123     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
124   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
125     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
126
127   if (!anOrigin || !aNorm)
128     return boost::shared_ptr<GeomAPI_Pln>();
129
130   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
131 }
132
133 void addPlane(double theX, double theY, double theZ, std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
134 {
135   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
136   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
137   double aSize = Config_PropManager::integer("Sketch definition", "Size of planes", PLANE_SIZE);
138   boost::shared_ptr<GeomAPI_Shape> aFace = 
139     GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, aSize);
140   theShapes.push_back(aFace);
141 }
142
143 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_Sketch::
144   getAISObject(boost::shared_ptr<GeomAPI_AISObject> thePrevious)
145 {
146   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
147     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
148
149   if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
150     boost::shared_ptr<GeomAPI_AISObject> aAIS = thePrevious;
151     if (!aAIS) {
152       std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
153
154       addPlane(1, 0, 0, aFaces); // YZ plane
155       addPlane(0, 1, 0, aFaces); // XZ plane
156       addPlane(0, 0, 1, aFaces); // XY plane
157       boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
158       aAIS = boost::shared_ptr<GeomAPI_AISObject>(new GeomAPI_AISObject());
159       aAIS->createShape(aCompound);
160
161       std::vector<int> aRGB = Config_PropManager::color("Sketch definition", 
162                                                         "planes_color", 
163                                                         SKETCH_PLANE_COLOR);
164       aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
165
166       aAIS->setWidth(Config_PropManager::integer("Sketch definition", 
167                                                  "planes_thikness",
168                                                  SKETCH_WIDTH));
169     }
170     return aAIS;
171   }
172   return boost::shared_ptr<GeomAPI_AISObject>();
173 }