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