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