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_Point> anOrigin =
41 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
42 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
43 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
44 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
45 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
46 boost::shared_ptr<GeomDataAPI_Dir> aNorm =
47 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
49 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
50 if (aFeatures.empty())
53 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
54 aLast = aFeatures.end();
56 boost::shared_ptr<SketchPlugin_Feature> aFeature;
57 std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
58 for (; anIt != aLast; anIt++) {
59 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
60 boost::shared_ptr<GeomAPI_Shape> aPreview = aFeature->preview();
62 aFeaturesPreview.push_back(aPreview);
65 std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
66 std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
67 GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
68 aFeaturesPreview, aLoops, aWires);
70 aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
71 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
72 data()->store(aCompound);
75 const boost::shared_ptr<GeomAPI_Shape>& SketchPlugin_Sketch::preview()
78 setPreview(boost::shared_ptr<GeomAPI_Shape>());
81 std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
83 addPlane(1, 0, 0, aFaces); // YZ plane
84 addPlane(0, 1, 0, aFaces); // XZ plane
85 addPlane(0, 0, 1, aFaces); // XY plane
86 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
87 setPreview(aCompound);
92 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
94 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
95 data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
98 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
99 std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
101 boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
102 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
103 boost::shared_ptr<GeomAPI_Shape> aFace =
104 GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
105 theShapes.push_back(aFace);
108 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
110 boost::shared_ptr<GeomDataAPI_Point> aC =
111 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
112 boost::shared_ptr<GeomDataAPI_Dir> aX =
113 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
114 boost::shared_ptr<GeomDataAPI_Dir> aY =
115 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
117 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
118 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
120 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
123 bool SketchPlugin_Sketch::isPlaneSet()
125 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
126 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
128 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);