1 // File: SketchPlugin_Sketch.cxx
2 // Created: 27 Mar 2014
3 // Author: Mikhail PONIKAROV
5 #include <Config_PropManager.h>
7 #include <GeomAlgoAPI_CompoundBuilder.h>
8 #include <GeomAlgoAPI_FaceBuilder.h>
10 #include <GeomAPI_AISObject.h>
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_XYZ.h>
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomDataAPI_Point.h>
17 #include <GeomAlgoAPI_FaceBuilder.h>
19 #include <ModelAPI_AttributeRefList.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Object.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_Validator.h>
26 #include <ModelAPI_Session.h>
28 #include <SketchPlugin_Sketch.h>
29 #include <SketchPlugin_Feature.h>
38 SketchPlugin_Sketch::SketchPlugin_Sketch()
42 void SketchPlugin_Sketch::initAttributes()
44 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
45 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
46 data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
47 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
48 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
49 // the selected face, base for the sketcher plane, not obligatory
50 data()->addAttribute(SketchPlugin_Feature::EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
51 ModelAPI_Session::get()->validators()->registerNotObligatory(
52 getKind(), SketchPlugin_Feature::EXTERNAL_ID());
55 void SketchPlugin_Sketch::execute()
57 if (!data()->isValid())
59 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
60 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
62 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
63 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
64 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
65 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
66 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
67 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
68 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
69 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
71 std::list<ObjectPtr> aFeatures = aRefList->list();
72 if (aFeatures.empty())
75 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
76 std::shared_ptr<SketchPlugin_Feature> aFeature;
77 std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
78 for (; anIt != aLast; anIt++) {
79 aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
81 if (!aFeature->sketch()) // on load document the back references are missed
82 aFeature->setSketch(this);
83 // do not include the external edges into the result
84 if (aFeature->data()->attribute(SketchPlugin_Feature::EXTERNAL_ID())) {
85 if (aFeature->data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value())
89 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
90 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
91 for (; aResIter != aRes.cend(); aResIter++) {
92 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
93 ModelAPI_ResultConstruction>(*aResIter);
95 std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
97 aFeaturesPreview.push_back(aShape);
103 if (aFeaturesPreview.empty())
106 // Collect all edges as one big wire
107 std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
108 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
109 for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
110 aBigWire->addEdge(*aShapeIt);
112 aBigWire->setOrigin(anOrigin->pnt());
113 aBigWire->setDirX(aDirX->dir());
114 aBigWire->setDirY(aDirY->dir());
115 aBigWire->setNorm(aNorm->dir());
117 // GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
118 // aFeaturesPreview, aLoops, aWires);
119 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
120 aConstr->setShape(aBigWire);
124 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
126 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
128 std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
129 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
134 int SketchPlugin_Sketch::numberOfSubs() const
136 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
139 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
141 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
142 return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
145 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
147 return subFeature(theIndex)->data()->featureId();
150 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
152 // check is this feature of result
153 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
155 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
157 aFeature = document()->feature(aRes);
160 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
161 for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
162 if (*aSubIt == aFeature)
169 std::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
171 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
172 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
173 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
174 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
175 std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
176 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
178 std::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
179 ->added(aY->dir()->xyz()->multiplied(theY));
181 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
184 std::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
185 const std::shared_ptr<GeomAPI_Pnt>& thePnt)
187 std::shared_ptr<GeomDataAPI_Point> aC = std::dynamic_pointer_cast<GeomDataAPI_Point>(
188 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
189 std::shared_ptr<GeomDataAPI_Dir> aX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
190 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
191 std::shared_ptr<GeomDataAPI_Dir> aY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
192 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
193 return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
197 bool SketchPlugin_Sketch::isPlaneSet()
199 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
200 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
202 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
205 std::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
207 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
208 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
209 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
210 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
212 if (!anOrigin || !aNorm)
213 return std::shared_ptr<GeomAPI_Pln>();
215 return std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
218 //void addPlane(double theX, double theY, double theZ,
219 // std::list<std::shared_ptr<GeomAPI_Shape> >& theShapes)
221 // std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
222 // std::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
223 // double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
224 // std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
226 // theShapes.push_back(aFace);
229 //AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
231 // std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
232 // data()->attribute(SketchPlugin_Sketch::NORM_ID()));
234 // if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
235 // AISObjectPtr aAIS = thePrevious;
237 // std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
239 // addPlane(1, 0, 0, aFaces); // YZ plane
240 // addPlane(0, 1, 0, aFaces); // XZ plane
241 // addPlane(0, 0, 1, aFaces); // XY plane
242 // std::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
243 // aAIS = AISObjectPtr(new GeomAPI_AISObject());
244 // aAIS->createShape(aCompound);
246 // std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
247 // SKETCH_PLANE_COLOR);
248 // aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
250 // aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
255 // return AISObjectPtr();
258 void SketchPlugin_Sketch::erase()
260 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
261 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
262 std::list<ObjectPtr> aFeatures = aRefList->list();
263 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
264 for (; anIt != aFeatures.end(); anIt++) {
265 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
267 // subs are referenced from sketch, but must be removed for sure, so not checkings
268 document()->removeFeature(aFeature, false);
271 ModelAPI_CompositeFeature::erase();
274 void SketchPlugin_Sketch::attributeChanged() {
275 static bool kIsUpdated = false; // to avoid infinitive cycle on attrubtes change
276 static bool kIsAttrChanged = false;
277 std::shared_ptr<GeomAPI_Shape> aSelection =
278 data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value();
279 if (aSelection && !kIsUpdated) { // update arguments due to the selection value
281 // update the sketch plane
282 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
284 double anA, aB, aC, aD;
285 aPlane->coefficients(anA, aB, aC, aD);
287 // calculate attributes of the sketch
288 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
289 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
290 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
291 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
292 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
293 // X axis is preferable to be dirX on the sketch
294 static const double tol = 1.e-7;
295 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
296 std::shared_ptr<GeomAPI_Dir> aTempDir(
297 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
298 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
299 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
301 kIsAttrChanged = false; // track the attributes were really changed during the plane update
302 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
303 <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
304 anOrigin->setValue(anOrigPnt);
305 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
306 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
307 aNormal->setValue(aNormDir);
308 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
309 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
310 aDirX->setValue(aXDir);
311 std::shared_ptr<GeomDataAPI_Dir> aDirY = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
312 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
313 aDirY->setValue(aYDir);
314 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
316 if (kIsAttrChanged) {
317 /* now it is in updater
318 // the plane was changed, so reexecute sub-elements to update shapes (located in new plane)
319 ModelAPI_ValidatorsFactory* aFactory = ModelAPI_Session::get()->validators();
320 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
321 for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
322 std::shared_ptr<SketchPlugin_Feature> aFeature =
323 std::dynamic_pointer_cast<SketchPlugin_Feature>(*aSubIt);
324 if (aFeature && aFactory->validate(aFeature)) {
329 kIsAttrChanged = false;
333 } else if (kIsUpdated) { // other attributes are updated during the selection comupation
334 kIsAttrChanged = true;