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_XYZ.h>
9 #include <GeomDataAPI_Dir.h>
10 #include <GeomDataAPI_Point.h>
11 #include <GeomAlgoAPI_FaceBuilder.h>
12 #include <GeomAlgoAPI_CompoundBuilder.h>
13 #include <GeomAlgoAPI_SketchBuilder.h>
17 // face of the square-face displayed for selection of general plane
18 const double PLANE_SIZE = 200;
20 SketchPlugin_Sketch::SketchPlugin_Sketch()
24 void SketchPlugin_Sketch::initAttributes()
26 data()->addAttribute(SKETCH_ATTR_ORIGIN, GeomDataAPI_Point::type());
27 data()->addAttribute(SKETCH_ATTR_DIRX, GeomDataAPI_Dir::type());
28 data()->addAttribute(SKETCH_ATTR_DIRY, GeomDataAPI_Dir::type());
29 data()->addAttribute(SKETCH_ATTR_NORM, GeomDataAPI_Dir::type());
30 data()->addAttribute(SKETCH_ATTR_FEATURES, 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(SKETCH_ATTR_FEATURES));
40 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
41 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
42 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
43 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
44 boost::shared_ptr<GeomDataAPI_Dir> aNorm =
45 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
47 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
48 if (aFeatures.empty())
51 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
52 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<GeomAPI_Shape> aPreview = aFeature->preview();
60 aFeaturesPreview.push_back(aPreview);
63 std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
64 std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
65 GeomAlgoAPI_SketchBuilder::createFaces(aDirX->dir(), aDirY->dir(), aNorm->dir(),
66 aFeaturesPreview, aLoops, aWires);
69 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Sketch::preview()
72 setPreview(boost::shared_ptr<GeomAPI_Shape>());
75 std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
77 addPlane(1, 0, 0, aFaces); // YZ plane
78 addPlane(0, 1, 0, aFaces); // XZ plane
79 addPlane(0, 0, 1, aFaces); // XY plane
80 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
81 setPreview(aCompound);
86 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
88 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
89 data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
92 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
93 std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
95 boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
96 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
97 boost::shared_ptr<GeomAPI_Shape> aFace =
98 GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
99 theShapes.push_back(aFace);
102 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
104 boost::shared_ptr<GeomDataAPI_Point> aC =
105 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
106 boost::shared_ptr<GeomDataAPI_Dir> aX =
107 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
108 boost::shared_ptr<GeomDataAPI_Dir> aY =
109 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
111 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
112 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
114 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
117 bool SketchPlugin_Sketch::isPlaneSet()
119 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
120 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
122 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);