Salome HOME
Create sketch with new model architecture
[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 using namespace std;
19
20 SketchPlugin_Sketch::SketchPlugin_Sketch()
21 {
22 }
23
24 void SketchPlugin_Sketch::initAttributes()
25 {
26   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
27   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
28   data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
29   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
30   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), 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(SketchPlugin_Sketch::FEATURES_ID()));
39
40   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
41     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
42   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
43     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
44   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
45     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
46   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
47     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
48
49   std::list<ObjectPtr> aFeatures = aRefList->list();
50   if (aFeatures.empty())
51     return ;
52
53   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
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<ModelAPI_ResultConstruction> aRes = 
59       boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFeature->firstResult());
60     if (aRes) {
61       boost::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
62       if (aShape)
63         aFeaturesPreview.push_back(aShape);
64     }
65   }
66
67   if (aFeaturesPreview.empty())
68     return ;
69   std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
70   std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
71   GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
72                                          aFeaturesPreview, aLoops, aWires);
73
74   aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
75   boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
76   boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
77   aConstr->setShape(aCompound);
78   setResult(aConstr);
79 }
80
81 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
82 {
83   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
84   data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(theFeature);
85 }
86
87 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
88 {
89   boost::shared_ptr<GeomDataAPI_Point> aC = 
90     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
91   boost::shared_ptr<GeomDataAPI_Dir> aX = 
92     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
93   boost::shared_ptr<GeomDataAPI_Dir> aY = 
94     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
95
96   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
97     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
98
99   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
100 }
101
102 bool SketchPlugin_Sketch::isPlaneSet()
103 {
104   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
105     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
106
107   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
108 }
109
110 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
111 {
112   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
113     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
114   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
115     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
116
117   if (!anOrigin || !aNorm)
118     return boost::shared_ptr<GeomAPI_Pln>();
119
120   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
121 }