Salome HOME
Values of constraint (length, distance, radius) now initialized on reference definition
[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 <Config_PropManager.h>
6
7 #include <GeomAlgoAPI_CompoundBuilder.h>
8 #include <GeomAlgoAPI_FaceBuilder.h>
9
10 #include <GeomAPI_AISObject.h>
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_XYZ.h>
14
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomDataAPI_Point.h>
17 #include <GeomAlgoAPI_FaceBuilder.h>
18
19 #include <ModelAPI_AttributeRefList.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Object.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_Validator.h>
26 #include <ModelAPI_Session.h>
27
28 #include <SketchPlugin_Sketch.h>
29 #include <SketchPlugin_Feature.h>
30
31 #include <memory>
32
33 #include <math.h>
34 #include <vector>
35
36 using namespace std;
37
38 SketchPlugin_Sketch::SketchPlugin_Sketch()
39 {
40 }
41
42 void SketchPlugin_Sketch::initAttributes()
43 {
44   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
45   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
46   data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
47   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
48   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
49   // the selected face, base for the sketcher plane, not obligatory
50   data()->addAttribute(SketchPlugin_Feature::EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
51   ModelAPI_Session::get()->validators()->registerNotObligatory(
52     getKind(), SketchPlugin_Feature::EXTERNAL_ID());
53 }
54
55 void SketchPlugin_Sketch::execute()
56 {
57   if (!data()->isValid())
58     return;
59   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
60       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
61
62   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
63       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
64   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
65       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
66   std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
67       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
68   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
69       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
70
71   std::list<ObjectPtr> aFeatures = aRefList->list();
72   if (aFeatures.empty())
73     return;
74
75   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
76   std::shared_ptr<SketchPlugin_Feature> aFeature;
77   std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
78   for (; anIt != aLast; anIt++) {
79     aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
80     if (aFeature) {
81       if (!aFeature->sketch()) // on load document the back references are missed
82         aFeature->setSketch(this);
83       // do not include the external edges into the result
84       if (aFeature->data()->attribute(SketchPlugin_Feature::EXTERNAL_ID())) {
85         if (aFeature->data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value())
86           continue;
87       }
88
89       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
90       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
91       for (; aResIter != aRes.cend(); aResIter++) {
92         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
93             ModelAPI_ResultConstruction>(*aResIter);
94         if (aConstr) {
95           std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
96           if (aShape)
97             aFeaturesPreview.push_back(aShape);
98         }
99       }
100     }
101   }
102
103   if (aFeaturesPreview.empty())
104     return;
105
106   // Collect all edges as one big wire
107   std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
108   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
109   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
110     aBigWire->addEdge(*aShapeIt);
111   }
112   aBigWire->setOrigin(anOrigin->pnt());
113   aBigWire->setDirX(aDirX->dir());
114   aBigWire->setDirY(aDirY->dir());
115   aBigWire->setNorm(aNorm->dir());
116
117 //  GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
118 //                                         aFeaturesPreview, aLoops, aWires);
119   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
120   aConstr->setShape(aBigWire);
121   setResult(aConstr);
122 }
123
124 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
125 {
126   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
127   if (aNew) {
128     std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
129     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
130   }
131   return aNew;
132 }
133
134 int SketchPlugin_Sketch::numberOfSubs() const
135 {
136   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
137 }
138
139 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
140 {
141   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
142   return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
143 }
144
145 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
146 {
147   return subFeature(theIndex)->data()->featureId();
148 }
149
150 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
151 {
152   // check is this feature of result
153   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
154   if (!aFeature) {
155     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
156     if (aRes)
157       aFeature = document()->feature(aRes);
158   }
159   if (aFeature) {
160     list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
161     for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
162       if (*aSubIt == aFeature)
163         return true;
164     }
165   }
166   return false;
167 }
168
169 std::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
170 {
171   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
172       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
173   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
174       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
175   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
176       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
177
178   std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
179       ->added(aY->dir()->xyz()->multiplied(theY));
180
181   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
182 }
183
184 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
185   const std::shared_ptr<GeomAPI_Pnt>& thePnt)
186 {
187   std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
188       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
189   std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
190       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
191   std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
192       data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
193   return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
194 }
195
196
197 bool SketchPlugin_Sketch::isPlaneSet()
198 {
199   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
200       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
201
202   return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
203 }
204
205 std::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
206 {
207   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
208       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
209   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
210       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
211
212   if (!anOrigin || !aNorm)
213     return std::shared_ptr<GeomAPI_Pln>();
214
215   return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
216 }
217
218 //void addPlane(double theX, double theY, double theZ,
219 //              std::list<std::shared_ptr<GeomAPI_Shape> >& theShapes)
220 //{
221 //  std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
222 //  std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
223 //  double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
224 //  std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
225 //                                                                           aSize);
226 //  theShapes.push_back(aFace);
227 //}
228
229 //AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
230 //{
231 //  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
232 //      data()->attribute(SketchPlugin_Sketch::NORM_ID()));
233 //
234 //  if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
235 //    AISObjectPtr aAIS = thePrevious;
236 //    if (!aAIS) {
237 //      std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
238 //
239 //      addPlane(1, 0, 0, aFaces);  // YZ plane
240 //      addPlane(0, 1, 0, aFaces);  // XZ plane
241 //      addPlane(0, 0, 1, aFaces);  // XY plane
242 //      std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
243 //      aAIS = AISObjectPtr(new GeomAPI_AISObject());
244 //      aAIS->createShape(aCompound);
245 //
246 //      std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
247 //      SKETCH_PLANE_COLOR);
248 //      aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
249 //
250 //      aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
251 //      SKETCH_WIDTH));
252 //    }
253 //    return aAIS;
254 //  }
255 //  return AISObjectPtr();
256 //}
257
258 void SketchPlugin_Sketch::erase()
259 {
260   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
261       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
262   std::list<ObjectPtr> aFeatures = aRefList->list();
263   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
264   for (; anIt != aFeatures.end(); anIt++) {
265     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
266     if (aFeature) {
267        // subs are referenced from sketch, but must be removed for sure, so not checkings
268       document()->removeFeature(aFeature, false);
269     }
270   }
271   ModelAPI_CompositeFeature::erase();
272 }
273
274 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
275   if (theID == SketchPlugin_Feature::EXTERNAL_ID()) {
276     std::shared_ptr<GeomAPI_Shape> aSelection = 
277       data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value();
278     if (aSelection) { // update arguments due to the selection value
279       // update the sketch plane
280       std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
281       if (aPlane) {
282         double anA, aB, aC, aD;
283         aPlane->coefficients(anA, aB, aC, aD);
284
285         // calculate attributes of the sketch
286         std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
287         std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
288         std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
289         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
290         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
291         // X axis is preferable to be dirX on the sketch
292         static const double tol = 1.e-7;
293         bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
294         std::shared_ptr<GeomAPI_Dir> aTempDir(
295           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
296         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
297         std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
298
299         // update position of the sketch
300         std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
301           <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
302         anOrigin->setValue(anOrigPnt);
303         std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
304           data()->attribute(SketchPlugin_Sketch::NORM_ID()));
305         aNormal->setValue(aNormDir);
306         std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
307           data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
308         aDirX->setValue(aXDir);
309         std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
310           data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
311         aDirY->setValue(aYDir);
312         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
313       }
314     }
315   }
316 }