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>
40 SketchPlugin_Sketch::SketchPlugin_Sketch()
44 void SketchPlugin_Sketch::initAttributes()
46 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
47 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
48 data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
49 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
50 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
51 // the selected face, base for the sketcher plane, not obligatory
52 data()->addAttribute(SketchPlugin_Feature::EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
53 ModelAPI_Session::get()->validators()->registerNotObligatory(
54 getKind(), SketchPlugin_Feature::EXTERNAL_ID());
57 void SketchPlugin_Sketch::execute()
59 if (!data()->isValid())
61 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
62 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
64 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
65 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
66 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
67 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
68 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
69 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
70 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
71 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
73 std::list<ObjectPtr> aFeatures = aRefList->list();
74 if (aFeatures.empty())
77 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
78 std::shared_ptr<SketchPlugin_Feature> aFeature;
79 std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
80 for (; anIt != aLast; anIt++) {
81 aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
83 if (!aFeature->sketch()) // on load document the back references are missed
84 aFeature->setSketch(this);
85 // do not include the external edges into the result
86 if (aFeature->data()->attribute(SketchPlugin_Feature::EXTERNAL_ID())) {
87 if (aFeature->data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value())
91 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
92 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
93 for (; aResIter != aRes.cend(); aResIter++) {
94 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
95 ModelAPI_ResultConstruction>(*aResIter);
97 std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
99 aFeaturesPreview.push_back(aShape);
105 if (aFeaturesPreview.empty())
108 // Collect all edges as one big wire
109 std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
110 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
111 for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
112 aBigWire->addEdge(*aShapeIt);
114 aBigWire->setOrigin(anOrigin->pnt());
115 aBigWire->setDirX(aDirX->dir());
116 aBigWire->setDirY(aDirY->dir());
117 aBigWire->setNorm(aNorm->dir());
119 // GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
120 // aFeaturesPreview, aLoops, aWires);
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(ModelAPI_Feature* theFeature)
138 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
139 list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
140 bool isRemoved = false;
141 bool aHasEmtpyFeature = false;
142 for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
143 std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
144 if (aFeature.get() != NULL && aFeature.get() == theFeature) {
145 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
148 else if (aFeature.get() == NULL)
149 aHasEmtpyFeature = true;
151 // if the object is not found in the sketch sub-elements, that means that the object is removed already.
152 // Find the first empty element and remove it
153 if (!isRemoved && aHasEmtpyFeature)
154 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(NULL);
157 int SketchPlugin_Sketch::numberOfSubs() const
159 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
162 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
164 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
165 return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
168 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
170 return subFeature(theIndex)->data()->featureId();
173 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
175 // check is this feature of result
176 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
178 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
180 aFeature = document()->feature(aRes);
183 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
184 for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
185 if (*aSubIt == aFeature)
192 std::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
194 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
195 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
196 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
197 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
198 std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
199 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
201 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
202 ->added(aY->dir()->xyz()->multiplied(theY));
204 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
207 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
208 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
210 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
211 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
212 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
213 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
214 std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
215 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
216 return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
220 bool SketchPlugin_Sketch::isPlaneSet()
222 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
223 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
225 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
228 std::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
230 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
231 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
232 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
233 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
235 if (!anOrigin || !aNorm)
236 return std::shared_ptr<GeomAPI_Pln>();
238 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
241 //void addPlane(double theX, double theY, double theZ,
242 // std::list<std::shared_ptr<GeomAPI_Shape> >& theShapes)
244 // std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
245 // std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
246 // double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
247 // std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
249 // theShapes.push_back(aFace);
252 //AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
254 // std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
255 // data()->attribute(SketchPlugin_Sketch::NORM_ID()));
257 // if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
258 // AISObjectPtr aAIS = thePrevious;
260 // std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
262 // addPlane(1, 0, 0, aFaces); // YZ plane
263 // addPlane(0, 1, 0, aFaces); // XZ plane
264 // addPlane(0, 0, 1, aFaces); // XY plane
265 // std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
266 // aAIS = AISObjectPtr(new GeomAPI_AISObject());
267 // aAIS->createShape(aCompound);
269 // std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
270 // SKETCH_PLANE_COLOR);
271 // aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
273 // aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
278 // return AISObjectPtr();
281 void SketchPlugin_Sketch::erase()
283 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
284 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
285 std::list<ObjectPtr> aFeatures = aRefList->list();
286 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
287 for (; anIt != aFeatures.end(); anIt++) {
288 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
290 // subs are referenced from sketch, but must be removed for sure, so not checkings
291 document()->removeFeature(aFeature);
294 ModelAPI_CompositeFeature::erase();
297 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
298 if (theID == SketchPlugin_Feature::EXTERNAL_ID()) {
299 std::shared_ptr<GeomAPI_Shape> aSelection =
300 data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value();
301 if (aSelection) { // update arguments due to the selection value
302 // update the sketch plane
303 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
305 double anA, aB, aC, aD;
306 aPlane->coefficients(anA, aB, aC, aD);
308 // calculate attributes of the sketch
309 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
310 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
311 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
312 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
313 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
314 // X axis is preferable to be dirX on the sketch
315 static const double tol = 1.e-7;
316 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
317 std::shared_ptr<GeomAPI_Dir> aTempDir(
318 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
319 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
320 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
322 // update position of the sketch
323 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
324 <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
325 anOrigin->setValue(anOrigPnt);
326 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
327 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
328 aNormal->setValue(aNormDir);
329 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
330 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
331 aDirX->setValue(aXDir);
332 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
333 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
334 aDirY->setValue(aYDir);
335 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();