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);
68 aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
69 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
70 data()->store(aCompound);
73 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Sketch::preview()
76 setPreview(boost::shared_ptr<GeomAPI_Shape>());
79 std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
81 addPlane(1, 0, 0, aFaces); // YZ plane
82 addPlane(0, 1, 0, aFaces); // XZ plane
83 addPlane(0, 0, 1, aFaces); // XY plane
84 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
85 setPreview(aCompound);
90 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
92 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
93 data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
96 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
97 std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
99 boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
100 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
101 boost::shared_ptr<GeomAPI_Shape> aFace =
102 GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
103 theShapes.push_back(aFace);
106 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
108 boost::shared_ptr<GeomDataAPI_Point> aC =
109 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
110 boost::shared_ptr<GeomDataAPI_Dir> aX =
111 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
112 boost::shared_ptr<GeomDataAPI_Dir> aY =
113 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
115 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
116 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
118 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
121 bool SketchPlugin_Sketch::isPlaneSet()
123 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
124 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
126 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);