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