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>
35 #include <Events_Loop.h>
44 SketchPlugin_Sketch::SketchPlugin_Sketch()
48 void SketchPlugin_Sketch::initAttributes()
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());
60 void SketchPlugin_Sketch::execute()
62 if (!data()->isValid())
64 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
65 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
67 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
68 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
69 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
70 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
71 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
72 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
74 std::list<ObjectPtr> aFeatures = aRefList->list();
75 if (aFeatures.empty())
78 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
79 std::shared_ptr<SketchPlugin_Feature> aFeature;
80 std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
81 for (; anIt != aLast; anIt++) {
82 aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
84 if (!aFeature->sketch()) // on load document the back references are missed
85 aFeature->setSketch(this);
86 // do not include the external edges into the result
87 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
88 if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value())
91 // do not include the construction entities in the result
92 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
93 if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
97 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
98 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
99 for (; aResIter != aRes.cend(); aResIter++) {
100 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
101 ModelAPI_ResultConstruction>(*aResIter);
103 std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
105 aFeaturesPreview.push_back(aShape);
111 if (aFeaturesPreview.empty())
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);
120 aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
121 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
122 aConstr->setShape(aBigWire);
126 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
128 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
130 std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
131 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
136 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
138 if (!data().get()) // sketch is already removed (case on undo of sketch), sync is not needed
140 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
141 list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
142 bool isRemoved = false;
143 bool aHasEmtpyFeature = false;
144 for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
145 std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
146 if (aFeature.get() != NULL && aFeature == theFeature) {
147 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
150 else if (aFeature.get() == NULL)
151 aHasEmtpyFeature = true;
153 // if the object is not found in the sketch sub-elements, that means that the object is removed already.
154 // Find the first empty element and remove it
155 if (!isRemoved && aHasEmtpyFeature)
156 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
159 int SketchPlugin_Sketch::numberOfSubs() const
161 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
164 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
166 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
167 return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
170 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
172 return subFeature(theIndex)->data()->featureId();
175 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
177 // check is this feature of result
178 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
180 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
182 aFeature = document()->feature(aRes);
185 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
186 for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
187 if (*aSubIt == aFeature)
195 void SketchPlugin_Sketch::erase()
197 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
198 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
199 std::list<ObjectPtr> aFeatures = aRefList->list();
200 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
201 for (; anIt != aFeatures.end(); anIt++) {
202 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
204 // subs are referenced from sketch, but must be removed for sure, so not checkings
205 document()->removeFeature(aFeature);
208 ModelAPI_CompositeFeature::erase();
211 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
212 if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
213 std::shared_ptr<GeomAPI_Shape> aSelection =
214 data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
215 if (aSelection) { // update arguments due to the selection value
216 // update the sketch plane
217 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
219 double anA, aB, aC, aD;
220 aPlane->coefficients(anA, aB, aC, aD);
222 // calculate attributes of the sketch
223 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
224 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
225 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
226 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
227 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
228 // X axis is preferable to be dirX on the sketch
229 static const double tol = 1.e-7;
230 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
231 std::shared_ptr<GeomAPI_Dir> aTempDir(
232 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
233 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
234 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
236 // update position of the sketch
237 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
238 <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
239 anOrigin->setValue(anOrigPnt);
240 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
241 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
242 aNormal->setValue(aNormDir);
243 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
244 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
245 aDirX->setValue(aXDir);
246 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
249 } else if (theID == SketchPlugin_Sketch::NORM_ID() || theID == SketchPlugin_Sketch::DIRX_ID()) {
250 // send all sub-elements are also updated: all entities become created on different plane
251 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
252 std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
253 std::list<ObjectPtr>::iterator aSub = aSubs.begin();
254 for(; aSub != aSubs.end(); aSub++) {
255 ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
260 void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
261 SketchPlugin_Sketch* theSketch,
262 const std::string& theAttributeID, const int theIndex)
264 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
265 theFeature->attribute(theAttributeID));
267 if (!aPoint || !aPoint->isInitialized())
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);
279 theFeature->setResult(aResult, theIndex);