Salome HOME
aabee0fa3fc9b6ee43fee64d1ce46c7cf2849765
[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_Projection.h>
47 #include <SketchPlugin_SketchEntity.h>
48 #include <SketchPlugin_Tools.h>
49
50 #include <Events_Loop.h>
51
52 #include <memory>
53
54 #include <math.h>
55 #include <vector>
56
57 SketchPlugin_Sketch::SketchPlugin_Sketch()
58 {
59 }
60
61 void SketchPlugin_Sketch::initAttributes()
62 {
63   data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
64   data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
65   data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
66   data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
67   // the selected face, base for the sketcher plane, not obligatory
68   data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(),
69     ModelAPI_AttributeSelection::typeId());
70   ModelAPI_Session::get()->validators()->registerNotObligatory(
71     getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
72   data()->addAttribute(SketchPlugin_Sketch::SOLVER_ERROR(), ModelAPI_AttributeString::typeId());
73   ModelAPI_Session::get()->validators()->registerNotObligatory(
74     getKind(), SketchPlugin_Sketch::SOLVER_ERROR());
75   data()->addAttribute(SketchPlugin_Sketch::SOLVER_DOF(), ModelAPI_AttributeString::typeId());
76   ModelAPI_Session::get()->validators()->registerNotObligatory(
77     getKind(), SketchPlugin_Sketch::SOLVER_DOF());
78 }
79
80 void SketchPlugin_Sketch::execute()
81 {
82   if (!data()->isValid())
83     return;
84   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
85       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
86
87   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
88       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
89   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
90       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
91   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
92       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
93
94   std::list<ObjectPtr> aFeatures = aRefList->list();
95   if (aFeatures.empty()) // actually, this must be avoided by the validators
96     return;
97
98   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
99   std::shared_ptr<SketchPlugin_Feature> aFeature;
100   std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
101   for (; anIt != aLast; anIt++) {
102     aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
103     if (aFeature) {
104       if (!aFeature->sketch()) // on load document the back references are missed
105         aFeature->setSketch(this);
106       // do not include into the result the external edges with disabled flag "Include into result"
107       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
108         if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->context()) {
109           AttributeBooleanPtr aKeepResult =
110               aFeature->boolean(SketchPlugin_Projection::INCLUDE_INTO_RESULT());
111           if (!aKeepResult || !aKeepResult->value())
112             continue;
113         }
114       }
115       // do not include the construction entities in the result
116       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
117         if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
118           continue;
119       }
120
121       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
122       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
123       for (; aResIter != aRes.cend(); aResIter++) {
124         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
125             ModelAPI_ResultConstruction>(*aResIter);
126         if (aConstr) {
127           std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
128           if (aShape)
129             aFeaturesPreview.push_back(aShape);
130         }
131       }
132     }
133   }
134
135   // Collect all edges as one big wire
136   std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
137   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
138   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
139     aBigWire->addEdge(*aShapeIt);
140   }
141   aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
142   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
143   aConstr->setShape(aBigWire);
144   setResult(aConstr);
145 }
146
147 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
148 {
149   // Set last feature of the sketch as current feature.
150   // Reason: Changing of parameter through Python API may lead to creation of new features
151   //         (e.g. changing number of copies in MultiRotation). If the sketch is not the last
152   //         feature in the Object Browser, then new features will be added to the end feature.
153   //         Therefore, setting any feature below the sketch as a current feature will disable
154   //         these newly created features.
155   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
156       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
157   int aSize = aRefList->size(false);
158   ObjectPtr aLastObject = aSize == 0 ? data()->owner() : aRefList->object(aSize - 1, false);
159   FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastObject);
160   document()->setCurrentFeature(aLastFeature, false);
161
162   // add new feature
163   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
164   if (aNew) {
165     // the sketch cannot be specified for the macro-features defined in python
166     // like SketchRectangle, so we need to check the type of new feature
167     std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
168         std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew);
169     if (aSketchFeature)
170       aSketchFeature->setSketch(this);
171     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
172   }
173    // set as current also after it becomes sub to set correctly enabled for other sketch subs
174   document()->setCurrentFeature(aNew, false);
175
176   return aNew;
177 }
178
179 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
180 {
181   if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
182     return;
183   AttributeRefListPtr aList = reflist(SketchPlugin_Sketch::FEATURES_ID());
184   // if the object is last, remove it from the list
185   // (needed to skip empty transaction on edit of sketch feature)
186   if (aList->object(aList->size(true) - 1, true) == theFeature) {
187     aList->remove(theFeature);
188   } else {
189     // to keep the persistent sub-elements indexing, do not remove elements from list,
190     // but substitute by nulls
191     aList->substitute(theFeature, ObjectPtr());
192   }
193 }
194
195 int SketchPlugin_Sketch::numberOfSubs(bool forTree) const
196 {
197   if (forTree)
198     return 0;
199   return data()->reflist(FEATURES_ID())->size(false);
200 }
201
202 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(
203   const int theIndex, bool forTree)
204 {
205   if (forTree)
206     return FeaturePtr();
207
208   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
209   FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
210   return aRes;
211 }
212
213 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
214 {
215   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
216       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
217   std::list<ObjectPtr> aFeatures = aRefList->list();
218   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
219   int aResultIndex = 1; // number of the counted (created) features, started from 1
220   int aFeatureIndex = -1; // number of the not-empty features in the list
221   for (; anIt != aFeatures.end(); anIt++) {
222     if (anIt->get())
223       aFeatureIndex++;
224     if (aFeatureIndex == theIndex)
225       break;
226     aResultIndex++;
227   }
228   return aResultIndex;
229 }
230
231 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
232 {
233   // check is this feature of result
234   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
235   if (!aFeature) {
236     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
237     if (aRes)
238       aFeature = document()->feature(aRes);
239   }
240   if (aFeature) {
241     return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
242   }
243   return false;
244 }
245
246
247 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
248   if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
249     AttributeSelectionPtr aSelAttr = selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
250     if (aSelAttr->context().get()) { // update arguments due to the selection value
251       std::shared_ptr<GeomAPI_Shape> aSelection = aSelAttr->value();
252       if (!aSelection.get()) aSelection = aSelAttr->context()->shape();
253       // update the sketch plane
254       std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aSelection));
255       std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
256       if (aPlane) {
257         double anA, aB, aC, aD;
258         aPlane->coefficients(anA, aB, aC, aD);
259
260         // calculate attributes of the sketch
261         std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
262         std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
263         std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
264         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
265         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
266         // X axis is preferable to be dirX on the sketch
267         // here can not be very small value to avoid very close to X normal axis (issue 595)
268         static const double tol = 0.1;
269         bool isX = fabs(anA) - 1.0 < tol && fabs(aB) < tol && fabs(aC) < tol;
270         std::shared_ptr<GeomAPI_Dir> aTempDir(
271           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
272         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
273         std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
274
275         // update position of the sketch
276         std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
277           <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
278         anOrigin->setValue(anOrigPnt);
279         std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
280           data()->attribute(SketchPlugin_Sketch::NORM_ID()));
281         aNormal->setValue(aNormDir);
282         std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
283           data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
284         aDirX->setValue(aXDir);
285         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
286       }
287     }
288   } else if (theID == NORM_ID() || theID == DIRX_ID() || theID == ORIGIN_ID()) {
289     // send all sub-elements are also updated: all entities become created on different plane
290     static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
291     std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
292     std::list<ObjectPtr>::iterator aSub = aSubs.begin();
293     for(; aSub != aSubs.end(); aSub++) {
294       if (aSub->get())
295         ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
296     }
297   }
298 }
299
300 void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
301                                               SketchPlugin_Sketch* theSketch,
302                                               const std::string& theAttributeID, const int theIndex)
303 {
304   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
305     theFeature->attribute(theAttributeID));
306
307   if (!aPoint || !aPoint->isInitialized())
308     return;
309
310   std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
311   //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
312   // make a visible point
313   std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
314   std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
315                      theFeature->data(), theIndex);
316   aResult->setShape(aCenterPointShape);
317   aResult->setIsInHistory(false);
318
319   theFeature->setResult(aResult, theIndex);
320 }
321
322 FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
323                                                             SketchPlugin_Sketch* theSketch,
324                                                             const bool theIsCopy)
325 {
326   FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
327   // addFeature generates a unique name for the feature, it caches the name
328   std::string aUniqueFeatureName = aNewFeature->data()->name();
329   // all attribute values are copied\pasted to the new feature, name is not an exception
330   theFeature->data()->copyTo(aNewFeature->data());
331   // external state should not be copied as a new object is an object of the current sketch
332   if (theFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()).get())
333     aNewFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->setValue(ResultPtr(),
334                                                                                GeomShapePtr());
335   aNewFeature->data()->setName(aUniqueFeatureName);
336   // text expressions could block setValue of some attributes
337   SketchPlugin_Tools::clearExpressions(aNewFeature);
338   // Set copy attribute
339   AttributeBooleanPtr anAttr = aNewFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
340   if(anAttr.get()) {
341     anAttr->setValue(theIsCopy);
342   }
343
344   return aNewFeature;
345 }
346
347 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
348 {
349   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
350
351   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
352       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
353   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
354       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
355   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
356       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
357
358   return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
359 }
360
361 void SketchPlugin_Sketch::exchangeIDs(
362   std::shared_ptr<ModelAPI_Feature> theFeature1, std::shared_ptr<ModelAPI_Feature> theFeature2)
363 {
364   reflist(SketchPlugin_Sketch::FEATURES_ID())->exchange(theFeature1, theFeature2);
365 }