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>
18 const int SKETCH_PLANE_COLOR = Colors::COLOR_BROWN; /// the plane edge color
19 const double SKETCH_WIDTH = 4.0; /// the plane edge width
23 // face of the square-face displayed for selection of general plane
24 const double PLANE_SIZE = 200;
26 SketchPlugin_Sketch::SketchPlugin_Sketch()
30 void SketchPlugin_Sketch::initAttributes()
32 data()->addAttribute(SKETCH_ATTR_ORIGIN, GeomDataAPI_Point::type());
33 data()->addAttribute(SKETCH_ATTR_DIRX, GeomDataAPI_Dir::type());
34 data()->addAttribute(SKETCH_ATTR_DIRY, GeomDataAPI_Dir::type());
35 data()->addAttribute(SKETCH_ATTR_NORM, GeomDataAPI_Dir::type());
36 data()->addAttribute(SKETCH_ATTR_FEATURES, ModelAPI_AttributeRefList::type());
39 void SketchPlugin_Sketch::execute()
42 std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
44 addPlane(1, 0, 0, aFaces); // YZ plane
45 addPlane(0, 1, 0, aFaces); // XZ plane
46 addPlane(0, 0, 1, aFaces); // XY plane
47 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
48 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction();
49 aConstr->setShape(aCompound);
50 results().push_back(aConstr);
53 if (!data()->isValid())
55 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList =
56 boost::dynamic_pointer_cast<ModelAPI_AttributeRefList>(data()->attribute(SKETCH_ATTR_FEATURES));
58 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
59 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
60 boost::shared_ptr<GeomDataAPI_Dir> aDirX =
61 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
62 boost::shared_ptr<GeomDataAPI_Dir> aDirY =
63 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
64 boost::shared_ptr<GeomDataAPI_Dir> aNorm =
65 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
67 std::list<boost::shared_ptr<ModelAPI_Feature> > aFeatures = aRefList->list();
68 if (aFeatures.empty())
71 std::list<boost::shared_ptr<ModelAPI_Feature> >::const_iterator anIt = aFeatures.begin(),
72 aLast = aFeatures.end();
74 boost::shared_ptr<SketchPlugin_Feature> aFeature;
75 std::list< boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
76 for (; anIt != aLast; anIt++) {
77 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
78 boost::shared_ptr<ModelAPI_ResultConstruction> aRes =
79 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aFeature->firstResult());
81 boost::shared_ptr<GeomAPI_Shape> aShape = aRes->shape();
83 aFeaturesPreview.push_back(aShape);
87 if (aFeaturesPreview.empty())
89 std::list< boost::shared_ptr<GeomAPI_Shape> > aLoops;
90 std::list< boost::shared_ptr<GeomAPI_Shape> > aWires;
91 GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
92 aFeaturesPreview, aLoops, aWires);
94 aLoops.insert(aLoops.end(), aWires.begin(), aWires.end());
95 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aLoops);
96 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction();
97 aConstr->setShape(aCompound);
98 results().push_back(aConstr);
101 boost::shared_ptr<GeomAPI_AISObject> SketchPlugin_Sketch::getAISObject(
102 boost::shared_ptr<GeomAPI_AISObject> thePrevious)
104 boost::shared_ptr<GeomAPI_AISObject> aResult = simpleAISObject(firstResult(), thePrevious);
105 aResult->setColor(SKETCH_PLANE_COLOR);
106 aResult->setWidth(SKETCH_WIDTH);
107 //anAIS->Redisplay();
111 const void SketchPlugin_Sketch::addSub(const FeaturePtr& theFeature)
113 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature)->setSketch(this);
114 data()->reflist(SKETCH_ATTR_FEATURES)->append(theFeature);
117 void SketchPlugin_Sketch::addPlane(double theX, double theY, double theZ,
118 std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes) const
120 boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
121 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
122 boost::shared_ptr<GeomAPI_Shape> aFace =
123 GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal, PLANE_SIZE);
124 theShapes.push_back(aFace);
127 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
129 boost::shared_ptr<GeomDataAPI_Point> aC =
130 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
131 boost::shared_ptr<GeomDataAPI_Dir> aX =
132 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRX));
133 boost::shared_ptr<GeomDataAPI_Dir> aY =
134 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_DIRY));
136 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(
137 aX->dir()->xyz()->multiplied(theX))->added(aY->dir()->xyz()->multiplied(theY));
139 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
142 bool SketchPlugin_Sketch::isPlaneSet()
144 boost::shared_ptr<GeomDataAPI_Dir> aNormal =
145 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
147 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
150 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
152 boost::shared_ptr<GeomDataAPI_Point> anOrigin =
153 boost::dynamic_pointer_cast<GeomDataAPI_Point>(data()->attribute(SKETCH_ATTR_ORIGIN));
154 boost::shared_ptr<GeomDataAPI_Dir> aNorm =
155 boost::dynamic_pointer_cast<GeomDataAPI_Dir>(data()->attribute(SKETCH_ATTR_NORM));
157 if (!anOrigin || !aNorm)
158 return boost::shared_ptr<GeomAPI_Pln>();
160 return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));