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>
17 #include <Config_PropManager.h>
21 SketchPlugin_Sketch::SketchPlugin_Sketch()
25 void SketchPlugin_Sketch::initAttributes()
27 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
28 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
29 data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
30 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
31 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
34 void SketchPlugin_Sketch::execute()
36 if (!data()->isValid())
38 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
39 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
41 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
42 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
43 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
44 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
45 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
46 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
47 boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
48 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
50 std::list<ObjectPtr> aFeatures = aRefList->list();
51 if (aFeatures.empty())
54 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
55 boost::shared_ptr<SketchPlugin_Feature> aFeature;
56 std::list<boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
57 for (; anIt != aLast; anIt++) {
58 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
61 const std::list<boost::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
62 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
63 for (; aResIter != aRes.cend(); aResIter++) {
64 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = boost::dynamic_pointer_cast<
65 ModelAPI_ResultConstruction>(*aResIter);
67 boost::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
69 aFeaturesPreview.push_back(aShape);
75 if (aFeaturesPreview.empty())
77 std::list<boost::shared_ptr<GeomAPI_Shape> > aLoops;
78 std::list<boost::shared_ptr<GeomAPI_Shape> > aWires;
79 GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
80 aFeaturesPreview, aLoops, aWires);
82 aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
83 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
84 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
85 aConstr->setShape(aCompound);
89 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
91 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
92 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(theFeature);
95 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
97 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
98 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
99 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
100 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
101 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
102 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
104 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
105 ->added(aY->dir()->xyz()->multiplied(theY));
107 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
110 bool SketchPlugin_Sketch::isPlaneSet()
112 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
113 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
115 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
118 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
120 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
121 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
122 boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
123 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
125 if (!anOrigin || !aNorm)
126 return boost::shared_ptr<GeomAPI_Pln>();
128 return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
131 void addPlane(double theX, double theY, double theZ,
132 std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
134 boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
135 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
136 double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
137 boost::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
139 theShapes.push_back(aFace);
142 AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
144 boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
145 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
147 if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
148 AISObjectPtr aAIS = thePrevious;
150 std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
152 addPlane(1, 0, 0, aFaces); // YZ plane
153 addPlane(0, 1, 0, aFaces); // XZ plane
154 addPlane(0, 0, 1, aFaces); // XY plane
155 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
156 aAIS = AISObjectPtr(new GeomAPI_AISObject());
157 aAIS->createShape(aCompound);
159 std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
161 aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
163 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thikness",
168 return AISObjectPtr();