]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchPlugin/SketchPlugin_Sketch.cpp
Salome HOME
Issue #698 - Distance constraint on circle - crash
[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_AISObject.h>
13 #include <GeomAPI_Dir.h>
14 #include <GeomAPI_PlanarEdges.h>
15
16 #include <GeomAlgoAPI_FaceBuilder.h>
17
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAlgoAPI_PointBuilder.h>
20
21 #include <ModelAPI_AttributeRefList.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Feature.h>
25 #include <ModelAPI_Object.h>
26 #include <ModelAPI_ResultConstruction.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Events.h>
30
31 #include <SketchPlugin_Sketch.h>
32 #include <SketchPlugin_Feature.h>
33 #include <SketchPlugin_SketchEntity.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(), ModelAPI_AttributeSelection::typeId());
56   ModelAPI_Session::get()->validators()->registerNotObligatory(
57     getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
58 }
59
60 void SketchPlugin_Sketch::execute()
61 {
62   if (!data()->isValid())
63     return;
64   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
65       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
66
67   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
68       data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
69   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
70       data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
71   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
72       data()->attribute(SketchPlugin_Sketch::NORM_ID()));
73
74   std::list<ObjectPtr> aFeatures = aRefList->list();
75   if (aFeatures.empty())
76     return;
77
78   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
79   std::shared_ptr<SketchPlugin_Feature> aFeature;
80   std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
81   for (; anIt != aLast; anIt++) {
82     aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
83     if (aFeature) {
84       if (!aFeature->sketch()) // on load document the back references are missed
85         aFeature->setSketch(this);
86       // do not include the external edges into the result
87       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
88         if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value())
89           continue;
90       }
91       // do not include the construction entities in the result
92       if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
93         if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
94           continue;
95       }
96
97       const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
98       std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
99       for (; aResIter != aRes.cend(); aResIter++) {
100         std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
101             ModelAPI_ResultConstruction>(*aResIter);
102         if (aConstr) {
103           std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
104           if (aShape)
105             aFeaturesPreview.push_back(aShape);
106         }
107       }
108     }
109   }
110
111   if (aFeaturesPreview.empty())
112     return;
113
114   // Collect all edges as one big wire
115   std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
116   std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
117   for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
118     aBigWire->addEdge(*aShapeIt);
119   }
120   aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
121   std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
122   aConstr->setShape(aBigWire);
123   setResult(aConstr);
124 }
125
126 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
127 {
128   std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
129   if (aNew) {
130     std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
131     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
132   }
133    // set as current also after it becomes sub to set correctly enabled for other sketch subs
134   document()->setCurrentFeature(aNew, false);
135   return aNew;
136 }
137
138 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
139 {
140   if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
141     return;
142   list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
143   list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
144   bool isRemoved = false;
145   bool aHasEmtpyFeature = false;
146   for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
147     std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
148     if (aFeature.get() != NULL && aFeature == theFeature) {
149       data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
150       isRemoved = true;
151     }
152     else if (aFeature.get() == NULL)
153       aHasEmtpyFeature = true;
154   }
155   // if the object is not found in the sketch sub-elements, that means that the object is removed already.
156   // Find the first empty element and remove it
157   if (!isRemoved && aHasEmtpyFeature)
158     data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
159 }
160
161 int SketchPlugin_Sketch::numberOfSubs() const
162 {
163   return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
164 }
165
166 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
167 {
168   ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
169   return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
170 }
171
172 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
173 {
174   return subFeature(theIndex)->data()->featureId();
175 }
176
177 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
178 {
179   // check is this feature of result
180   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
181   if (!aFeature) {
182     ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
183     if (aRes)
184       aFeature = document()->feature(aRes);
185   }
186   if (aFeature) {
187     return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
188   }
189   return false;
190 }
191
192
193 void SketchPlugin_Sketch::erase()
194 {
195   std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
196       ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
197   std::list<ObjectPtr> aFeatures = aRefList->list();
198   std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
199   for (; anIt != aFeatures.end(); anIt++) {
200     FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
201     if (aFeature) {
202       // subs are referenced from sketch, but must be removed for sure, so not checkings
203       document()->removeFeature(aFeature);
204     }
205   }
206   ModelAPI_CompositeFeature::erase();
207 }
208
209 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
210   if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
211     std::shared_ptr<GeomAPI_Shape> aSelection = 
212       data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
213     if (aSelection) { // update arguments due to the selection value
214       // update the sketch plane
215       std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
216       if (aPlane) {
217         double anA, aB, aC, aD;
218         aPlane->coefficients(anA, aB, aC, aD);
219
220         // calculate attributes of the sketch
221         std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
222         std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
223         std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
224         aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
225         std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
226         // X axis is preferable to be dirX on the sketch
227         static const double tol = 0.1; // here can not be very small value to avoid very close to X normal axis (issue 595)
228         bool isX = fabs(anA) - 1.0 < tol && fabs(aB) < tol && fabs(aC) < tol;
229         std::shared_ptr<GeomAPI_Dir> aTempDir(
230           isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
231         std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
232         std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
233
234         // update position of the sketch
235         std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
236           <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
237         anOrigin->setValue(anOrigPnt);
238         std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
239           data()->attribute(SketchPlugin_Sketch::NORM_ID()));
240         aNormal->setValue(aNormDir);
241         std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
242           data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
243         aDirX->setValue(aXDir);
244         std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
245       }
246     }
247   } else if (theID == SketchPlugin_Sketch::NORM_ID() || theID == SketchPlugin_Sketch::DIRX_ID()) {
248     // send all sub-elements are also updated: all entities become created on different plane
249     static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
250     std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
251     std::list<ObjectPtr>::iterator aSub = aSubs.begin();
252     for(; aSub != aSubs.end(); aSub++) {
253       ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
254     }
255   }
256 }
257
258 void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
259                                               SketchPlugin_Sketch* theSketch,
260                                               const std::string& theAttributeID, const int theIndex)
261 {
262   std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
263     theFeature->attribute(theAttributeID));
264
265   if (!aPoint || !aPoint->isInitialized())
266     return;
267
268   std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
269   //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
270   // make a visible point
271   std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
272   std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
273                      theFeature->data(), theIndex);
274   aResult->setShape(aCenterPointShape);
275   aResult->setIsInHistory(false);
276
277   theFeature->setResult(aResult, theIndex);
278 }
279
280 FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
281                                                             SketchPlugin_Sketch* theSketch)
282 {
283   FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
284   // addFeature generates a unique name for the feature, it caches the name
285   std::string aUniqueFeatureName = aNewFeature->data()->name();
286   // all attribute values are copied\pasted to the new feature, name is not an exception
287   theFeature->data()->copyTo(aNewFeature->data());
288   // as a name for the feature, the generated unique name is set
289   aNewFeature->data()->setName(aUniqueFeatureName);
290
291   return aNewFeature;
292 }
293
294 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
295 {
296   std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
297
298   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
299       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
300   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
301       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
302   std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
303       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
304
305   return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
306 }