1 // File: SketchPlugin_Sketch.cxx
2 // Created: 27 Mar 2014
3 // Author: Mikhail PONIKAROV
5 #include "SketchPlugin_Sketch.h"
6 #include <ModelAPI_Data.h>
7 #include <ModelAPI_AttributeRefList.h>
8 #include <GeomAPI_AISObject.h>
9 #include <GeomAPI_XYZ.h>
10 #include <GeomDataAPI_Dir.h>
11 #include <GeomDataAPI_Point.h>
12 #include <GeomAlgoAPI_FaceBuilder.h>
13 #include <GeomAlgoAPI_CompoundBuilder.h>
14 #include <GeomAlgoAPI_SketchBuilder.h>
15 #include <ModelAPI_ResultConstruction.h>
20 SketchPlugin_Sketch::SketchPlugin_Sketch()
24 void SketchPlugin_Sketch::initAttributes()
26 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
27 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
28 data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
29 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
30 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
33 void SketchPlugin_Sketch::execute()
35 if (!data()->isValid())
37 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
38 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
40 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
41 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
42 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
43 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
44 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
45 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
46 boost::shared_ptr<GeomDataAPI_Dir> aNorm =
47 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
49 std::list<ObjectPtr> aFeatures = aRefList->list();
50 if (aFeatures.empty())
53 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
54 boost::shared_ptr<SketchPlugin_Feature> aFeature;
55 std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
56 for (; anIt != aLast; anIt++) {
57 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
58 boost::shared_ptr<ModelAPI_ResultConstruction> aRes =
59 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFeature->firstResult());
61 boost::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
63 aFeaturesPreview.push_back(aShape);
67 if (aFeaturesPreview.empty())
69 std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
70 std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
71 GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
72 aFeaturesPreview, aLoops, aWires);
74 aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
75 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
76 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
77 aConstr->setShape(aCompound);
81 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
83 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
84 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(theFeature);
87 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
89 boost::shared_ptr<GeomDataAPI_Point> aC =
90 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
91 boost::shared_ptr<GeomDataAPI_Dir> aX =
92 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
93 boost::shared_ptr<GeomDataAPI_Dir> aY =
94 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
96 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
97 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
99 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
102 bool SketchPlugin_Sketch::isPlaneSet()
104 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
105 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
107 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
110 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
112 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
113 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
114 boost::shared_ptr<GeomDataAPI_Dir> aNorm =
115 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
117 if (!anOrigin || !aNorm)
118 return boost::shared_ptr<GeomAPI_Pln>();
120 return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));