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_AISObject.h>
13 #include <GeomAPI_Dir.h>
14 #include <GeomAPI_PlanarEdges.h>
16 #include <GeomAlgoAPI_FaceBuilder.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <GeomAlgoAPI_PointBuilder.h>
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>
31 #include <SketchPlugin_Sketch.h>
32 #include <SketchPlugin_Feature.h>
33 #include <SketchPlugin_SketchEntity.h>
34 #include <SketchPlugin_Tools.h>
36 #include <Events_Loop.h>
45 SketchPlugin_Sketch::SketchPlugin_Sketch()
49 void SketchPlugin_Sketch::initAttributes()
51 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
52 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
53 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
54 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
55 // the selected face, base for the sketcher plane, not obligatory
56 data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
57 ModelAPI_Session::get()->validators()->registerNotObligatory(
58 getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
61 void SketchPlugin_Sketch::execute()
63 if (!data()->isValid())
65 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
66 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
68 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
69 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
70 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
71 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
72 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
73 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
75 std::list<ObjectPtr> aFeatures = aRefList->list();
76 if (aFeatures.empty())
79 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
80 std::shared_ptr<SketchPlugin_Feature> aFeature;
81 std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
82 for (; anIt != aLast; anIt++) {
83 aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
85 if (!aFeature->sketch()) // on load document the back references are missed
86 aFeature->setSketch(this);
87 // do not include the external edges into the result
88 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
89 if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value())
92 // do not include the construction entities in the result
93 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
94 if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
98 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
99 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
100 for (; aResIter != aRes.cend(); aResIter++) {
101 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
102 ModelAPI_ResultConstruction>(*aResIter);
104 std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
106 aFeaturesPreview.push_back(aShape);
112 if (aFeaturesPreview.empty())
115 // Collect all edges as one big wire
116 std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
117 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
118 for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
119 aBigWire->addEdge(*aShapeIt);
121 aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
122 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
123 aConstr->setShape(aBigWire);
127 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
129 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
131 std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
132 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
134 // set as current also after it becomes sub to set correctly enabled for other sketch subs
135 document()->setCurrentFeature(aNew, false);
139 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
141 if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
143 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
144 list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
145 bool isRemoved = false;
146 bool aHasEmtpyFeature = false;
147 for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
148 std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
149 if (aFeature.get() != NULL && aFeature == theFeature) {
150 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
153 else if (aFeature.get() == NULL)
154 aHasEmtpyFeature = true;
156 // if the object is not found in the sketch sub-elements, that means that the object is removed already.
157 // Find the first empty element and remove it
158 if (!isRemoved && aHasEmtpyFeature)
159 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
162 int SketchPlugin_Sketch::numberOfSubs() const
164 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
167 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
169 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
170 return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
173 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
175 return subFeature(theIndex)->data()->featureId();
178 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
180 // check is this feature of result
181 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
183 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
185 aFeature = document()->feature(aRes);
188 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
194 void SketchPlugin_Sketch::erase()
196 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
197 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
198 std::list<ObjectPtr> aFeatures = aRefList->list();
199 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
200 for (; anIt != aFeatures.end(); anIt++) {
201 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
203 // subs are referenced from sketch, but must be removed for sure, so not checkings
204 document()->removeFeature(aFeature);
207 ModelAPI_CompositeFeature::erase();
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);
218 double anA, aB, aC, aD;
219 aPlane->coefficients(anA, aB, aC, aD);
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)));
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();
248 } else if (theID == SketchPlugin_Sketch::NORM_ID() || theID == SketchPlugin_Sketch::DIRX_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 ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
259 void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
260 SketchPlugin_Sketch* theSketch,
261 const std::string& theAttributeID, const int theIndex)
263 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
264 theFeature->attribute(theAttributeID));
266 if (!aPoint || !aPoint->isInitialized())
269 std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
270 //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
271 // make a visible point
272 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
273 std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
274 theFeature->data(), theIndex);
275 aResult->setShape(aCenterPointShape);
276 aResult->setIsInHistory(false);
278 theFeature->setResult(aResult, theIndex);
281 FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
282 SketchPlugin_Sketch* theSketch)
284 FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
285 // addFeature generates a unique name for the feature, it caches the name
286 std::string aUniqueFeatureName = aNewFeature->data()->name();
287 // all attribute values are copied\pasted to the new feature, name is not an exception
288 theFeature->data()->copyTo(aNewFeature->data());
289 // as a name for the feature, the generated unique name is set
290 aNewFeature->data()->setName(aUniqueFeatureName);
291 // text expressions could block setValue of some attributes
292 clearExpressions(aNewFeature);
297 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
299 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
301 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
302 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
303 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
304 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
305 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
306 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
308 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));