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>
22 SketchPlugin_Sketch::SketchPlugin_Sketch()
26 void SketchPlugin_Sketch::initAttributes()
28 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
29 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
30 data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
31 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
32 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
35 void SketchPlugin_Sketch::execute()
37 if (!data()->isValid())
39 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
40 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(
41 data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
43 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
44 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
45 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
46 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
47 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
48 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
49 boost::shared_ptr<GeomDataAPI_Dir> aNorm =
50 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
52 std::list<ObjectPtr> aFeatures = aRefList->list();
53 if (aFeatures.empty())
56 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
57 boost::shared_ptr<SketchPlugin_Feature> aFeature;
58 std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
59 for (; anIt != aLast; anIt++) {
60 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
63 const std::list<boost::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
64 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
65 for(; aResIter != aRes.cend(); aResIter++) {
66 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr =
67 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
69 boost::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
71 aFeaturesPreview.push_back(aShape);
77 if (aFeaturesPreview.empty())
79 std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
80 std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
81 GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
82 aFeaturesPreview, aLoops, aWires);
84 aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
85 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
86 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
87 aConstr->setShape(aCompound);
91 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
93 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
94 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(theFeature);
97 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
99 boost::shared_ptr<GeomDataAPI_Point> aC =
100 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
101 boost::shared_ptr<GeomDataAPI_Dir> aX =
102 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
103 boost::shared_ptr<GeomDataAPI_Dir> aY =
104 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
106 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
107 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
109 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
112 bool SketchPlugin_Sketch::isPlaneSet()
114 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
115 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
117 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
120 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
122 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
123 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
124 boost::shared_ptr<GeomDataAPI_Dir> aNorm =
125 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
127 if (!anOrigin || !aNorm)
128 return boost::shared_ptr<GeomAPI_Pln>();
130 return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
133 void addPlane(double theX, double theY, double theZ, std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
135 boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
136 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
137 double aSize = Config_PropManager::integer("Sketch definition", "Size of planes", PLANE_SIZE);
138 boost::shared_ptr<GeomAPI_Shape> aFace =
139 GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, aSize);
140 theShapes.push_back(aFace);
143 AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
145 boost::shared_ptr<GeomDataAPI_Dir> aNorm =
146 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SketchPlugin_Sketch::NORM_ID()));
148 if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
149 AISObjectPtr aAIS = thePrevious;
151 std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
153 addPlane(1, 0, 0, aFaces); // YZ plane
154 addPlane(0, 1, 0, aFaces); // XZ plane
155 addPlane(0, 0, 1, aFaces); // XY plane
156 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
157 aAIS = AISObjectPtr(new GeomAPI_AISObject());
158 aAIS->createShape(aCompound);
160 std::vector<int> aRGB = Config_PropManager::color("Sketch definition",
163 aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
165 aAIS->setWidth(Config_PropManager::integer("Sketch definition",
171 return AISObjectPtr();