Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[modules/shaper.git] / src / SketchPlugin / SketchPlugin_Sketch.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <Config_PropManager.h>
22
23 #include <GeomAlgoAPI_CompoundBuilder.h>
24 #include <GeomAlgoAPI_FaceBuilder.h>
25
26 #include <GeomAPI_Dir.h>
27 #include <GeomAPI_PlanarEdges.h>
28 #include <GeomAPI_Vertex.h>
29
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomAlgoAPI_PointBuilder.h>
32
33 #include <ModelAPI_AttributeRefList.h>
34 #include <ModelAPI_AttributeString.h>
35 #include <ModelAPI_Data.h>
36 #include <ModelAPI_Document.h>
37 #include <ModelAPI_Feature.h>
38 #include <ModelAPI_Object.h>
39 #include <ModelAPI_ResultConstruction.h>
40 #include <ModelAPI_Validator.h>
41 #include <ModelAPI_Session.h>
42 #include <ModelAPI_Events.h>
43
44 #include <SketchPlugin_Sketch.h>
45 #include <SketchPlugin_Feature.h>
46 #include <SketchPlugin_SketchEntity.h>
47 #include <SketchPlugin_Tools.h>
48
49 #include <Events_Loop.h>
50
51 #include <memory>
52
53 #include <math.h>
54 #include <vector>
55
56 SketchPlugin_Sketch::SketchPlugin_Sketch()
57 {
58 }
59
60 void SketchPlugin_Sketch::initAttributes()
61 {
62   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
63   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
64   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
65   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
66   // the selected face, base for the sketcher plane, not obligatory
67   data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(),
68     ModelAPI_AttributeSelection::typeId());
69   ModelAPI_Session::get()->validators()->registerNotObligatory(
70     getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
71   data()->addAttribute(SketchPlugin_Sketch::SOLVER_ERROR(), ModelAPI_AttributeString::typeId());
72   ModelAPI_Session::get()->validators()->registerNotObligatory(
73     getKind(), SketchPlugin_Sketch::SOLVER_ERROR());
74   data()->addAttribute(SketchPlugin_Sketch::SOLVER_DOF(), ModelAPI_AttributeString::typeId());
75   ModelAPI_Session::get()->validators()->registerNotObligatory(
76     getKind(), SketchPlugin_Sketch::SOLVER_DOF());
77 }
78
79 void SketchPlugin_Sketch::execute()
80 {
81   if (!data()->isValid())
82     return;
83   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
84       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
85
86   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
87       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
88   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
89       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
90   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
91       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
92
93   std::list<ObjectPtr> aFeatures = aRefList->list();
94   if (aFeatures.empty()) // actually, this must be avoided by the validators
95     return;
96
97   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
98   std::shared_ptr<SketchPlugin_Feature> aFeature;
99   std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
100   for (; anIt != aLast; anIt++) {
101     aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
102     if (aFeature) {
103       if (!aFeature->sketch()) // on load document the back references are missed
104         aFeature->setSketch(this);
105       // do not include the external edges into the result
106       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
107         if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->context())
108           continue;
109       }
110       // do not include the construction entities in the result
111       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
112         if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
113           continue;
114       }
115
116       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
117       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
118       for (; aResIter != aRes.cend(); aResIter++) {
119         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
120             ModelAPI_ResultConstruction>(*aResIter);
121         if (aConstr) {
122           std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
123           if (aShape)
124             aFeaturesPreview.push_back(aShape);
125         }
126       }
127     }
128   }
129
130   // Collect all edges as one big wire
131   std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
132   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
133   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
134     aBigWire->addEdge(*aShapeIt);
135   }
136   aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
137   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
138   aConstr->setShape(aBigWire);
139   setResult(aConstr);
140 }
141
142 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
143 {
144   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
145   if (aNew) {
146     // the sketch cannot be specified for the macro-features defined in python
147     // like SketchRectangle, so we need to check the type of new feature
148     std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
149         std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew);
150     if (aSketchFeature)
151       aSketchFeature->setSketch(this);
152     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
153   }
154    // set as current also after it becomes sub to set correctly enabled for other sketch subs
155   document()->setCurrentFeature(aNew, false);
156
157   return aNew;
158 }
159
160 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
161 {
162   if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
163     return;
164   AttributeRefListPtr aList = reflist(SketchPlugin_Sketch::FEATURES_ID());
165   // if the object is last, remove it from the list
166   // (needed to skip empty transaction on edit of sketch feature)
167   if (aList->object(aList->size(true) - 1, true) == theFeature) {
168     aList->remove(theFeature);
169   } else {
170     // to keep the persistent sub-elements indexing, do not remove elements from list,
171     // but substitute by nulls
172     aList->substitute(theFeature, ObjectPtr());
173   }
174 }
175
176 int SketchPlugin_Sketch::numberOfSubs(bool forTree) const
177 {
178   if (forTree)
179     return 0;
180   return data()->reflist(FEATURES_ID())->size(false);
181 }
182
183 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(
184   const int theIndex, bool forTree)
185 {
186   if (forTree)
187     return FeaturePtr();
188
189   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
190   FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
191   return aRes;
192 }
193
194 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
195 {
196   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
197       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
198   std::list<ObjectPtr> aFeatures = aRefList->list();
199   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
200   int aResultIndex = 1; // number of the counted (created) features, started from 1
201   int aFeatureIndex = -1; // number of the not-empty features in the list
202   for (; anIt != aFeatures.end(); anIt++) {
203     if (anIt->get())
204       aFeatureIndex++;
205     if (aFeatureIndex == theIndex)
206       break;
207     aResultIndex++;
208   }
209   return aResultIndex;
210 }
211
212 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
213 {
214   // check is this feature of result
215   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
216   if (!aFeature) {
217     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
218     if (aRes)
219       aFeature = document()->feature(aRes);
220   }
221   if (aFeature) {
222     return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
223   }
224   return false;
225 }
226
227
228 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
229   if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
230     AttributeSelectionPtr aSelAttr = selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
231     if (aSelAttr->context().get()) { // update arguments due to the selection value
232       std::shared_ptr<GeomAPI_Shape> aSelection = aSelAttr->value();
233       if (!aSelection.get()) aSelection = aSelAttr->context()->shape();
234       // update the sketch plane
235       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aSelection));
236       std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
237       if (aPlane) {
238         double anA, aB, aC, aD;
239         aPlane->coefficients(anA, aB, aC, aD);
240
241         // calculate attributes of the sketch
242         std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
243         std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
244         std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
245         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
246         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
247         // X axis is preferable to be dirX on the sketch
248         // here can not be very small value to avoid very close to X normal axis (issue 595)
249         static const double tol = 0.1;
250         bool isX = fabs(anA) - 1.0 < tol && fabs(aB) < tol && fabs(aC) < tol;
251         std::shared_ptr<GeomAPI_Dir> aTempDir(
252           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
253         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
254         std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
255
256         // update position of the sketch
257         std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
258           <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
259         anOrigin->setValue(anOrigPnt);
260         std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
261           data()->attribute(SketchPlugin_Sketch::NORM_ID()));
262         aNormal->setValue(aNormDir);
263         std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
264           data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
265         aDirX->setValue(aXDir);
266         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
267       }
268     }
269   } else if (theID == NORM_ID() || theID == DIRX_ID() || theID == ORIGIN_ID()) {
270     // send all sub-elements are also updated: all entities become created on different plane
271     static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
272     std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
273     std::list<ObjectPtr>::iterator aSub = aSubs.begin();
274     for(; aSub != aSubs.end(); aSub++) {
275       if (aSub->get())
276         ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
277     }
278   }
279 }
280
281 void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
282                                               SketchPlugin_Sketch* theSketch,
283                                               const std::string& theAttributeID, const int theIndex)
284 {
285   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
286     theFeature->attribute(theAttributeID));
287
288   if (!aPoint || !aPoint->isInitialized())
289     return;
290
291   std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
292   //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
293   // make a visible point
294   std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
295   std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
296                      theFeature->data(), theIndex);
297   aResult->setShape(aCenterPointShape);
298   aResult->setIsInHistory(false);
299
300   theFeature->setResult(aResult, theIndex);
301 }
302
303 FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
304                                                             SketchPlugin_Sketch* theSketch,
305                                                             const bool theIsCopy)
306 {
307   FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
308   // addFeature generates a unique name for the feature, it caches the name
309   std::string aUniqueFeatureName = aNewFeature->data()->name();
310   // all attribute values are copied\pasted to the new feature, name is not an exception
311   theFeature->data()->copyTo(aNewFeature->data());
312   // external state should not be copied as a new object is an object of the current sketch
313   if (theFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()).get())
314     aNewFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->setValue(ResultPtr(),
315                                                                                GeomShapePtr());
316   aNewFeature->data()->setName(aUniqueFeatureName);
317   // text expressions could block setValue of some attributes
318   SketchPlugin_Tools::clearExpressions(aNewFeature);
319   // Set copy attribute
320   AttributeBooleanPtr anAttr = aNewFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
321   if(anAttr.get()) {
322     anAttr->setValue(theIsCopy);
323   }
324
325   return aNewFeature;
326 }
327
328 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
329 {
330   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
331
332   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
333       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
334   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
335       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
336   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
337       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
338
339   return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
340 }
341
342 void SketchPlugin_Sketch::exchangeIDs(
343   std::shared_ptr<ModelAPI_Feature> theFeature1, std::shared_ptr<ModelAPI_Feature> theFeature2)
344 {
345   reflist(SketchPlugin_Sketch::FEATURES_ID())->exchange(theFeature1, theFeature2);
346 }