]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Sketch.cpp
Salome HOME
Several improvements mostly for automatic update of result
[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 #include <AIS_InteractiveObject.hxx>
16 #include <AIS_Shape.hxx>
17
18 #include <Quantity_NameOfColor.hxx>
19
20 const Quantity_NameOfColor SKETCH_PLANE_COLOR = Quantity_NOC_CHOCOLATE; /// the plane edge color
21 const int SKETCH_WIDTH = 4; /// the plane edge width
22
23 using namespace std;
24
25 // face of the square-face displayed for selection of general plane
26 const double PLANE_SIZE = 200;
27
28 SketchPlugin_Sketch::SketchPlugin_Sketch()
29 {
30 }
31
32 void SketchPlugin_Sketch::initAttributes()
33 {
34   data()->addAttribute(SKETCH_ATTR_ORIGIN, GeomDataAPI_Point::type());
35   data()->addAttribute(SKETCH_ATTR_DIRX, GeomDataAPI_Dir::type());
36   data()->addAttribute(SKETCH_ATTR_DIRY, GeomDataAPI_Dir::type());
37   data()->addAttribute(SKETCH_ATTR_NORM, GeomDataAPI_Dir::type());
38   data()->addAttribute(SKETCH_ATTR_FEATURES, ModelAPI_AttributeRefList::type());
39 }
40
41 void SketchPlugin_Sketch::execute() 
42 {
43   if (!data()->isValid())
44     return ;
45   boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
46     boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(SKETCH_ATTR_FEATURES));
47
48   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
49     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
50   boost::shared_ptr<GeomDataAPI_Dir> aDirX = 
51     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
52   boost::shared_ptr<GeomDataAPI_Dir> aDirY = 
53     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
54   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
55     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
56
57   std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
58   if (aFeatures.empty())
59     return ;
60
61   std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
62                                                                   aLast = aFeatures.end();
63
64   boost::shared_ptr<SketchPlugin_Feature> aFeature;
65   std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
66   for (; anIt != aLast; anIt++) {
67     aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
68     boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
69     if (aPreview)
70       aFeaturesPreview.push_back(aPreview);
71   }
72
73   if (aFeaturesPreview.empty())
74     return ;
75   std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
76   std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
77   GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
78                                          aFeaturesPreview, aLoops, aWires);
79
80   aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
81   boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
82   data()->store(aCompound);
83 }
84
85 Handle(AIS_InteractiveObject) SketchPlugin_Sketch::getAISShape(Handle(AIS_InteractiveObject) thePrevious)
86 {
87   Handle(AIS_InteractiveObject) anAIS = SketchPlugin_Feature::getAISShape(thePrevious);
88   Handle(AIS_Shape) aShapeAIS = Handle(AIS_Shape)::DownCast(anAIS);
89   aShapeAIS->SetColor(Quantity_Color(SKETCH_PLANE_COLOR));
90   aShapeAIS->SetWidth(SKETCH_WIDTH);
91   aShapeAIS->Redisplay();
92   return anAIS;
93 }
94
95 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Sketch::preview()
96 {
97   if (isPlaneSet()) {
98     setPreview(boost::shared_ptr<GeomAPI_Shape>());
99   }
100   else {
101     std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
102
103     addPlane(1, 0, 0, aFaces); // YZ plane
104     addPlane(0, 1, 0, aFaces); // XZ plane
105     addPlane(0, 0, 1, aFaces); // XY plane
106     boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
107     setPreview(aCompound);
108   }
109   return getPreview();
110 }
111
112 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
113 {
114   boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
115   data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
116 }
117
118 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
119                                    std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
120 {
121   boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
122   boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
123   boost::shared_ptr<GeomAPI_Shape> aFace = 
124     GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
125   theShapes.push_back(aFace);
126 }
127
128 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
129 {
130   boost::shared_ptr<GeomDataAPI_Point> aC = 
131     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
132   boost::shared_ptr<GeomDataAPI_Dir> aX = 
133     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
134   boost::shared_ptr<GeomDataAPI_Dir> aY = 
135     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
136
137   boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
138     aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
139
140   return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
141 }
142
143 bool SketchPlugin_Sketch::isPlaneSet()
144 {
145   boost::shared_ptr<GeomDataAPI_Dir> aNormal = 
146     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
147
148   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
149 }
150
151 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
152 {
153   boost::shared_ptr<GeomDataAPI_Point> anOrigin = 
154     boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
155   boost::shared_ptr<GeomDataAPI_Dir> aNorm = 
156     boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
157
158   if (!anOrigin || !aNorm)
159     return boost::shared_ptr<GeomAPI_Pln>();
160
161   return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
162 }