1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include <Config_PropManager.h>
22 #include <GeomAlgoAPI_CompoundBuilder.h>
23 #include <GeomAlgoAPI_FaceBuilder.h>
25 #include <GeomAPI_Dir.h>
26 #include <GeomAPI_PlanarEdges.h>
27 #include <GeomAPI_ShapeIterator.h>
28 #include <GeomAPI_Vertex.h>
30 #include <GeomDataAPI_Point2D.h>
31 #include <GeomAlgoAPI_PointBuilder.h>
33 #include <ModelAPI_AttributeRefList.h>
34 #include <ModelAPI_AttributeString.h>
35 #include <ModelAPI_Data.h>
36 #include <ModelAPI_Document.h>
37 #include <ModelAPI_Feature.h>
38 #include <ModelAPI_Object.h>
39 #include <ModelAPI_ResultConstruction.h>
40 #include <ModelAPI_Validator.h>
41 #include <ModelAPI_Session.h>
42 #include <ModelAPI_Events.h>
44 #include <SketchPlugin_Sketch.h>
45 #include <SketchPlugin_Feature.h>
46 #include <SketchPlugin_IntersectionPoint.h>
47 #include <SketchPlugin_Projection.h>
48 #include <SketchPlugin_SketchEntity.h>
49 #include <SketchPlugin_Tools.h>
51 #include <Events_Loop.h>
58 SketchPlugin_Sketch::SketchPlugin_Sketch()
62 void SketchPlugin_Sketch::initAttributes()
64 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
65 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
66 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
67 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
68 // the selected face, base for the sketcher plane, not obligatory
69 data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(),
70 ModelAPI_AttributeSelection::typeId());
71 ModelAPI_Session::get()->validators()->registerNotObligatory(
72 getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
73 data()->addAttribute(SketchPlugin_Sketch::SOLVER_ERROR(), ModelAPI_AttributeString::typeId());
74 ModelAPI_Session::get()->validators()->registerNotObligatory(
75 getKind(), SketchPlugin_Sketch::SOLVER_ERROR());
76 data()->addAttribute(SketchPlugin_Sketch::SOLVER_DOF(), ModelAPI_AttributeString::typeId());
77 ModelAPI_Session::get()->validators()->registerNotObligatory(
78 getKind(), SketchPlugin_Sketch::SOLVER_DOF());
81 void SketchPlugin_Sketch::execute()
83 if (!data()->isValid())
85 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
86 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
88 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
89 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
90 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
91 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
92 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
93 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
95 std::list<ObjectPtr> aFeatures = aRefList->list();
96 if (aFeatures.empty()) // actually, this must be avoided by the validators
99 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
100 std::shared_ptr<SketchPlugin_Feature> aFeature;
101 std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
102 for (; anIt != aLast; anIt++) {
103 aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
105 if (!aFeature->sketch()) // on load document the back references are missed
106 aFeature->setSketch(this);
107 // do not include into the result the external edges with disabled flag "Include into result"
108 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
109 if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->context()) {
110 const std::string& anAttrName =
111 aFeature->getKind() == SketchPlugin_Projection::ID() ?
112 SketchPlugin_Projection::INCLUDE_INTO_RESULT() :
113 SketchPlugin_IntersectionPoint::INCLUDE_INTO_RESULT();
114 AttributeBooleanPtr aKeepResult = aFeature->boolean(anAttrName);
115 if (!aKeepResult || !aKeepResult->value())
119 // do not include the construction entities in the result
120 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
121 if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
125 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
126 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
127 for (; aResIter != aRes.cend(); aResIter++) {
128 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
129 ModelAPI_ResultConstruction>(*aResIter);
131 std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
133 aFeaturesPreview.push_back(aShape);
139 // Collect all edges as one big wire
140 std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
141 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
142 for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
143 aBigWire->addEdge(*aShapeIt);
145 aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
146 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
147 aConstr->setShape(aBigWire);
151 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
153 // Set last feature of the sketch as current feature.
154 // Reason: Changing of parameter through Python API may lead to creation of new features
155 // (e.g. changing number of copies in MultiRotation). If the sketch is not the last
156 // feature in the Object Browser, then new features will be added to the end feature.
157 // Therefore, setting any feature below the sketch as a current feature will disable
158 // these newly created features.
159 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
160 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
161 int aSize = aRefList->size(false);
162 ObjectPtr aLastObject = aSize == 0 ? data()->owner() : aRefList->object(aSize - 1, false);
163 FeaturePtr aLastFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aLastObject);
164 document()->setCurrentFeature(aLastFeature, false);
167 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
169 // the sketch cannot be specified for the macro-features defined in python
170 // like SketchRectangle, so we need to check the type of new feature
171 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
172 std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew);
174 aSketchFeature->setSketch(this);
175 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
177 // set as current also after it becomes sub to set correctly enabled for other sketch subs
178 document()->setCurrentFeature(aNew, false);
183 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
185 if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
187 AttributeRefListPtr aList = reflist(SketchPlugin_Sketch::FEATURES_ID());
188 // if the object is last, remove it from the list
189 // (needed to skip empty transaction on edit of sketch feature)
190 if (aList->object(aList->size(true) - 1, true) == theFeature) {
191 aList->remove(theFeature);
193 // to keep the persistent sub-elements indexing, do not remove elements from list,
194 // but substitute by nulls
195 aList->substitute(theFeature, ObjectPtr());
199 int SketchPlugin_Sketch::numberOfSubs(bool forTree) const
203 return data()->reflist(FEATURES_ID())->size(false);
206 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(
207 const int theIndex, bool forTree)
212 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
213 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
217 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
219 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
220 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
221 std::list<ObjectPtr> aFeatures = aRefList->list();
222 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
223 int aResultIndex = 1; // number of the counted (created) features, started from 1
224 int aFeatureIndex = -1; // number of the not-empty features in the list
225 for (; anIt != aFeatures.end(); anIt++) {
228 if (aFeatureIndex == theIndex)
235 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
237 // check is this feature of result
238 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
240 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
242 aFeature = document()->feature(aRes);
245 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
251 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
252 if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
253 AttributeSelectionPtr aSelAttr = selection(SketchPlugin_SketchEntity::EXTERNAL_ID());
254 if (aSelAttr->context().get()) { // update arguments due to the selection value
255 std::shared_ptr<GeomAPI_Shape> aSelection = aSelAttr->value();
256 if (!aSelection.get()) aSelection = aSelAttr->context()->shape();
257 // update the sketch plane
258 std::shared_ptr<GeomAPI_Face> aFace;
259 if (aSelection->isFace()) {
260 aFace = aSelection->face();
261 } else if (aSelection->isCompound()) {
262 GeomAPI_ShapeIterator anIt(aSelection);
263 aFace = anIt.current()->face();
266 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
268 double anA, aB, aC, aD;
269 aPlane->coefficients(anA, aB, aC, aD);
271 // calculate attributes of the sketch
272 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
273 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
274 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
275 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
276 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
277 // X axis is preferable to be dirX on the sketch
278 // here can not be very small value to avoid very close to X normal axis (issue 595)
279 static const double tol = 0.1;
280 bool isX = fabs(anA) - 1.0 < tol && fabs(aB) < tol && fabs(aC) < tol;
281 std::shared_ptr<GeomAPI_Dir> aTempDir(
282 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
283 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
284 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
286 // update position of the sketch
287 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
288 <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
289 anOrigin->setValue(anOrigPnt);
290 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
291 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
292 aNormal->setValue(aNormDir);
293 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
294 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
295 aDirX->setValue(aXDir);
296 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
300 } else if (theID == NORM_ID() || theID == DIRX_ID() || theID == ORIGIN_ID()) {
301 // send all sub-elements are also updated: all entities become created on different plane
302 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
303 std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
304 std::list<ObjectPtr>::iterator aSub = aSubs.begin();
305 for(; aSub != aSubs.end(); aSub++) {
307 ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
312 void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
313 SketchPlugin_Sketch* theSketch,
314 const std::string& theAttributeID, const int theIndex)
316 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
317 theFeature->attribute(theAttributeID));
319 if (!aPoint || !aPoint->isInitialized())
322 std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
323 //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
324 // make a visible point
325 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::vertex(aCenter);
326 std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
327 theFeature->data(), theIndex);
328 aResult->setShape(aCenterPointShape);
329 aResult->setIsInHistory(false);
331 theFeature->setResult(aResult, theIndex);
334 FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
335 SketchPlugin_Sketch* theSketch,
336 const bool theIsCopy)
338 FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
339 // addFeature generates a unique name for the feature, it caches the name
340 std::string aUniqueFeatureName = aNewFeature->data()->name();
341 // all attribute values are copied\pasted to the new feature, name is not an exception
342 theFeature->data()->copyTo(aNewFeature->data());
343 // external state should not be copied as a new object is an object of the current sketch
344 if (theFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID()).get())
345 aNewFeature->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->setValue(ResultPtr(),
347 aNewFeature->data()->setName(aUniqueFeatureName);
348 // text expressions could block setValue of some attributes
349 SketchPlugin_Tools::clearExpressions(aNewFeature);
350 // Set copy attribute
351 AttributeBooleanPtr anAttr = aNewFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
353 anAttr->setValue(theIsCopy);
359 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
361 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
363 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
364 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
365 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
366 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
367 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
368 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
370 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));