1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Sketch.cxx
4 // Created: 27 Mar 2014
5 // Author: Mikhail PONIKAROV
7 #include <Config_PropManager.h>
9 #include <GeomAlgoAPI_CompoundBuilder.h>
10 #include <GeomAlgoAPI_FaceBuilder.h>
12 #include <GeomAPI_Dir.h>
13 #include <GeomAPI_PlanarEdges.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAlgoAPI_PointBuilder.h>
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>
29 #include <SketchPlugin_Sketch.h>
30 #include <SketchPlugin_Feature.h>
31 #include <SketchPlugin_SketchEntity.h>
32 #include <SketchPlugin_Tools.h>
34 #include <Events_Loop.h>
43 SketchPlugin_Sketch::SketchPlugin_Sketch()
47 void SketchPlugin_Sketch::initAttributes()
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());
62 void SketchPlugin_Sketch::execute()
64 if (!data()->isValid())
66 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
67 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
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()));
76 std::list<ObjectPtr> aFeatures = aRefList->list();
77 if (aFeatures.empty()) // actually, this must be avoided by the validators
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);
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())
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())
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);
105 std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
107 aFeaturesPreview.push_back(aShape);
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);
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);
126 aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
127 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
128 aConstr->setShape(aBigWire);
132 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
134 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
136 std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
137 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
139 // set as current also after it becomes sub to set correctly enabled for other sketch subs
140 document()->setCurrentFeature(aNew, false);
144 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
146 if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
148 // to keep the persistent sub-elements indexing, do not remove elements from list,
149 // but substitute by nulls
150 reflist(SketchPlugin_Sketch::FEATURES_ID())->substitute(theFeature, ObjectPtr());
153 int SketchPlugin_Sketch::numberOfSubs(bool forTree) const
157 return data()->reflist(FEATURES_ID())->size(false);
160 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(
161 const int theIndex, bool forTree)
166 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
167 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
171 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
173 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
174 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
175 std::list<ObjectPtr> aFeatures = aRefList->list();
176 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
177 int aResultIndex = 1; // number of the counted (created) features, started from 1
178 int aFeatureIndex = -1; // number of the not-empty features in the list
179 for (; anIt != aFeatures.end(); anIt++) {
182 if (aFeatureIndex == theIndex)
189 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
191 // check is this feature of result
192 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
194 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
196 aFeature = document()->feature(aRes);
199 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
205 void SketchPlugin_Sketch::erase()
207 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
208 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
209 std::list<ObjectPtr> aFeatures = aRefList->list();
210 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
211 for (; anIt != aFeatures.end(); anIt++) {
212 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
214 // subs are referenced from sketch, but must be removed for sure, so not checkings
215 document()->removeFeature(aFeature);
218 ModelAPI_CompositeFeature::erase();
221 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
222 if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
223 std::shared_ptr<GeomAPI_Shape> aSelection =
224 data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
225 if (aSelection) { // update arguments due to the selection value
226 // update the sketch plane
227 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
229 double anA, aB, aC, aD;
230 aPlane->coefficients(anA, aB, aC, aD);
232 // calculate attributes of the sketch
233 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
234 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
235 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
236 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
237 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
238 // X axis is preferable to be dirX on the sketch
239 static const double tol = 0.1; // here can not be very small value to avoid very close to X normal axis (issue 595)
240 bool isX = fabs(anA) - 1.0 < tol && fabs(aB) < tol && fabs(aC) < tol;
241 std::shared_ptr<GeomAPI_Dir> aTempDir(
242 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
243 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
244 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
246 // update position of the sketch
247 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
248 <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
249 anOrigin->setValue(anOrigPnt);
250 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
251 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
252 aNormal->setValue(aNormDir);
253 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
254 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
255 aDirX->setValue(aXDir);
256 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
259 } else if (theID == SketchPlugin_Sketch::NORM_ID() || theID == SketchPlugin_Sketch::DIRX_ID()) {
260 // send all sub-elements are also updated: all entities become created on different plane
261 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
262 std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
263 std::list<ObjectPtr>::iterator aSub = aSubs.begin();
264 for(; aSub != aSubs.end(); aSub++) {
266 ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
271 void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
272 SketchPlugin_Sketch* theSketch,
273 const std::string& theAttributeID, const int theIndex)
275 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
276 theFeature->attribute(theAttributeID));
278 if (!aPoint || !aPoint->isInitialized())
281 std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
282 //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
283 // make a visible point
284 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
285 std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
286 theFeature->data(), theIndex);
287 aResult->setShape(aCenterPointShape);
288 aResult->setIsInHistory(false);
290 theFeature->setResult(aResult, theIndex);
293 FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
294 SketchPlugin_Sketch* theSketch)
296 FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
297 // addFeature generates a unique name for the feature, it caches the name
298 std::string aUniqueFeatureName = aNewFeature->data()->name();
299 // all attribute values are copied\pasted to the new feature, name is not an exception
300 theFeature->data()->copyTo(aNewFeature->data());
301 // as a name for the feature, the generated unique name is set
302 aNewFeature->data()->setName(aUniqueFeatureName);
303 // text expressions could block setValue of some attributes
304 clearExpressions(aNewFeature);
309 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
311 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
313 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
314 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
315 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
316 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
317 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
318 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
320 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
323 void SketchPlugin_Sketch::exchangeIDs(
324 std::shared_ptr<ModelAPI_Feature> theFeature1, std::shared_ptr<ModelAPI_Feature> theFeature2)
326 reflist(SketchPlugin_Sketch::FEATURES_ID())->exchange(theFeature1, theFeature2);