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