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>
15 #include <GeomAPI_XYZ.h>
17 #include <GeomDataAPI_Dir.h>
18 #include <GeomDataAPI_Point.h>
19 #include <GeomAlgoAPI_FaceBuilder.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>
30 #include <SketchPlugin_Sketch.h>
31 #include <SketchPlugin_Feature.h>
32 #include <SketchPlugin_SketchEntity.h>
41 SketchPlugin_Sketch::SketchPlugin_Sketch()
45 void SketchPlugin_Sketch::initAttributes()
47 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
48 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
49 data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
50 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
51 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
52 // the selected face, base for the sketcher plane, not obligatory
53 data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
54 ModelAPI_Session::get()->validators()->registerNotObligatory(
55 getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
58 void SketchPlugin_Sketch::execute()
60 if (!data()->isValid())
62 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
63 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
65 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
66 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
67 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
68 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
69 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
70 data()->attribute(SketchPlugin_Sketch::DIRY_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(), aDirY->dir(), aNorm->dir());
122 // GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
123 // aFeaturesPreview, aLoops, aWires);
124 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
125 aConstr->setShape(aBigWire);
129 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
131 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
133 std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
134 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
139 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
141 if (!data().get()) // 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 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
189 for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
190 if (*aSubIt == aFeature)
197 std::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
199 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
200 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
201 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
202 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
203 std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
204 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
206 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
207 ->added(aY->dir()->xyz()->multiplied(theY));
209 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
212 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
213 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
215 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
216 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
217 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
218 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
219 std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
220 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
221 return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
225 bool SketchPlugin_Sketch::isPlaneSet()
227 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
228 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
230 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
233 std::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
235 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
236 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
237 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
238 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
240 if (!anOrigin || !aNorm)
241 return std::shared_ptr<GeomAPI_Pln>();
243 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
246 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::coordinatePlane() const
248 DataPtr aData = data();
249 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
250 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
251 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
252 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
253 std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
254 aData->attribute(SketchPlugin_Sketch::DIRY_ID()));
255 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
256 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
258 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(aC->pnt(), aX->dir(), aY->dir(), aNorm->dir()));
261 void SketchPlugin_Sketch::erase()
263 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
264 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
265 std::list<ObjectPtr> aFeatures = aRefList->list();
266 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
267 for (; anIt != aFeatures.end(); anIt++) {
268 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
270 // subs are referenced from sketch, but must be removed for sure, so not checkings
271 document()->removeFeature(aFeature);
274 ModelAPI_CompositeFeature::erase();
277 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
278 if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
279 std::shared_ptr<GeomAPI_Shape> aSelection =
280 data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
281 if (aSelection) { // update arguments due to the selection value
282 // update the sketch plane
283 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
285 double anA, aB, aC, aD;
286 aPlane->coefficients(anA, aB, aC, aD);
288 // calculate attributes of the sketch
289 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
290 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
291 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
292 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
293 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
294 // X axis is preferable to be dirX on the sketch
295 static const double tol = 1.e-7;
296 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
297 std::shared_ptr<GeomAPI_Dir> aTempDir(
298 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
299 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
300 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
302 // update position of the sketch
303 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
304 <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
305 anOrigin->setValue(anOrigPnt);
306 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
307 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
308 aNormal->setValue(aNormDir);
309 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
310 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
311 aDirX->setValue(aXDir);
312 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
313 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
314 aDirY->setValue(aYDir);
315 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();