]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Sketch.cpp
Salome HOME
Initial version of redesign of working with results
[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(SKETCH_ATTR_ORIGIN, GeomDataAPI_Point::type());
33   data()->addAttribute(SKETCH_ATTR_DIRX, GeomDataAPI_Dir::type());
34   data()->addAttribute(SKETCH_ATTR_DIRY, GeomDataAPI_Dir::type());
35   data()->addAttribute(SKETCH_ATTR_NORM, GeomDataAPI_Dir::type());
36   data()->addAttribute(SKETCH_ATTR_FEATURES, ModelAPI_AttributeRefList::type());
37 }
38
39 void SketchPlugin_Sketch::execute(boost::shared_ptr<ModelAPI_Result>& theResult) 
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::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult)->setShape(aCompound);
49     return;
50   }
51   if (!data()->isValid())
52     return ;
53   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
54     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(SKETCH_ATTR_FEATURES));
55
56   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
57     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
58   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
59     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
60   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
61     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
62   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
63     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
64
65   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
66   if (aFeatures.empty())
67     return ;
68
69   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
70                                                                   aLast = aFeatures.end();
71
72   boost::shared_ptr<SketchPlugin_Feature> aFeature;
73   std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
74   for (; anIt != aLast; anIt++) {
75     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
76     boost::shared_ptr<ModelAPI_ResultConstruction> aRes = 
77       boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(document()->result(aFeature));
78     if (aRes) {
79       boost::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
80       if (aShape)
81         aFeaturesPreview.push_back(aShape);
82     }
83   }
84
85   if (aFeaturesPreview.empty())
86     return ;
87   std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
88   std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
89   GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
90                                          aFeaturesPreview, aLoops, aWires);
91
92   aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
93   boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
94   boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(theResult)->setShape(aCompound);
95 }
96
97 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_Sketch::getAISObject(
98                                 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
99 {
100   boost::shared_ptr<GeomAPI_AISObject> anAIS = prepareAISShape(thePrevious);
101   anAIS->setColor(SKETCH_PLANE_COLOR);
102   anAIS->setWidth(SKETCH_WIDTH);
103   //anAIS->Redisplay();
104   return anAIS;
105 }
106
107 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
108 {
109   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
110   data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
111 }
112
113 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
114                                    std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
115 {
116   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
117   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
118   boost::shared_ptr<GeomAPI_Shape> aFace = 
119     GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
120   theShapes.push_back(aFace);
121 }
122
123 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
124 {
125   boost::shared_ptr<GeomDataAPI_Point> aC = 
126     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
127   boost::shared_ptr<GeomDataAPI_Dir> aX = 
128     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
129   boost::shared_ptr<GeomDataAPI_Dir> aY = 
130     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
131
132   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
133     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
134
135   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
136 }
137
138 bool SketchPlugin_Sketch::isPlaneSet()
139 {
140   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
141     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
142
143   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
144 }
145
146 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
147 {
148   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
149     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
150   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
151     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
152
153   if (!anOrigin || !aNorm)
154     return boost::shared_ptr<GeomAPI_Pln>();
155
156   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
157 }