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::CONSTRUCTION_ID())) {
93 if (aFeature->data()->boolean(SketchPlugin_SketchEntity::CONSTRUCTION_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->setOrigin(anOrigin->pnt());
121 aBigWire->setDirX(aDirX->dir());
122 aBigWire->setDirY(aDirY->dir());
123 aBigWire->setNorm(aNorm->dir());
125 // GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
126 // aFeaturesPreview, aLoops, aWires);
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);
136 std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
137 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
142 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
144 if (!data().get()) // sketch is already removed (case on undo of sketch), sync is not needed
146 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
147 list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
148 bool isRemoved = false;
149 bool aHasEmtpyFeature = false;
150 for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
151 std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
152 if (aFeature.get() != NULL && aFeature == theFeature) {
153 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
156 else if (aFeature.get() == NULL)
157 aHasEmtpyFeature = true;
159 // if the object is not found in the sketch sub-elements, that means that the object is removed already.
160 // Find the first empty element and remove it
161 if (!isRemoved && aHasEmtpyFeature)
162 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
165 int SketchPlugin_Sketch::numberOfSubs() const
167 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
170 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
172 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
173 return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
176 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
178 return subFeature(theIndex)->data()->featureId();
181 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
183 // check is this feature of result
184 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
186 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
188 aFeature = document()->feature(aRes);
191 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
192 for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
193 if (*aSubIt == aFeature)
200 std::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
202 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
203 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
204 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
205 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
206 std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
207 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
209 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
210 ->added(aY->dir()->xyz()->multiplied(theY));
212 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
215 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
216 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
218 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
219 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
220 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
221 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
222 std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
223 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
224 return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
228 bool SketchPlugin_Sketch::isPlaneSet()
230 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
231 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
233 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
236 std::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
238 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
239 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
240 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
241 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
243 if (!anOrigin || !aNorm)
244 return std::shared_ptr<GeomAPI_Pln>();
246 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
249 void SketchPlugin_Sketch::erase()
251 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
252 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
253 std::list<ObjectPtr> aFeatures = aRefList->list();
254 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
255 for (; anIt != aFeatures.end(); anIt++) {
256 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
258 // subs are referenced from sketch, but must be removed for sure, so not checkings
259 document()->removeFeature(aFeature);
262 ModelAPI_CompositeFeature::erase();
265 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
266 if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
267 std::shared_ptr<GeomAPI_Shape> aSelection =
268 data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
269 if (aSelection) { // update arguments due to the selection value
270 // update the sketch plane
271 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
273 double anA, aB, aC, aD;
274 aPlane->coefficients(anA, aB, aC, aD);
276 // calculate attributes of the sketch
277 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
278 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
279 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
280 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
281 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
282 // X axis is preferable to be dirX on the sketch
283 static const double tol = 1.e-7;
284 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
285 std::shared_ptr<GeomAPI_Dir> aTempDir(
286 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
287 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
288 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
290 // update position of the sketch
291 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
292 <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
293 anOrigin->setValue(anOrigPnt);
294 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
295 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
296 aNormal->setValue(aNormDir);
297 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
298 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
299 aDirX->setValue(aXDir);
300 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
301 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
302 aDirY->setValue(aYDir);
303 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();