1 // File: SketchPlugin_Sketch.cxx
2 // Created: 27 Mar 2014
3 // Author: Mikhail PONIKAROV
5 #include <Config_PropManager.h>
7 #include <GeomAlgoAPI_CompoundBuilder.h>
8 #include <GeomAlgoAPI_FaceBuilder.h>
10 #include <GeomAPI_AISObject.h>
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_XYZ.h>
15 #include <GeomDataAPI_Dir.h>
16 #include <GeomDataAPI_Point.h>
17 #include <GeomAlgoAPI_FaceBuilder.h>
19 #include <ModelAPI_AttributeRefList.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Object.h>
24 #include <ModelAPI_ResultConstruction.h>
25 #include <ModelAPI_Validator.h>
26 #include <ModelAPI_Session.h>
28 #include <SketchPlugin_Sketch.h>
29 #include <SketchPlugin_Feature.h>
31 #include <boost/smart_ptr/shared_ptr.hpp>
37 SketchPlugin_Sketch::SketchPlugin_Sketch()
41 void SketchPlugin_Sketch::initAttributes()
43 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::type());
44 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::type());
45 data()->addAttribute(SketchPlugin_Sketch::DIRY_ID(), GeomDataAPI_Dir::type());
46 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::type());
47 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::type());
48 // the selected face, base for the sketcher plane, not obligatory
49 data()->addAttribute(SketchPlugin_Feature::EXTERNAL_ID(), ModelAPI_AttributeSelection::type());
50 ModelAPI_Session::get()->validators()->registerNotObligatory(
51 getKind(), SketchPlugin_Feature::EXTERNAL_ID());
54 void SketchPlugin_Sketch::execute()
56 if (!data()->isValid())
58 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
59 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
61 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
62 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
63 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
64 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
65 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
66 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
67 boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
68 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
70 std::list<ObjectPtr> aFeatures = aRefList->list();
71 if (aFeatures.empty())
74 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
75 boost::shared_ptr<SketchPlugin_Feature> aFeature;
76 std::list<boost::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
77 for (; anIt != aLast; anIt++) {
78 aFeature = boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
80 // do not include the external edges into the result
81 if (aFeature->data()->attribute(SketchPlugin_Feature::EXTERNAL_ID())) {
82 if (aFeature->data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value())
86 const std::list<boost::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
87 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
88 for (; aResIter != aRes.cend(); aResIter++) {
89 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = boost::dynamic_pointer_cast<
90 ModelAPI_ResultConstruction>(*aResIter);
92 boost::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
94 aFeaturesPreview.push_back(aShape);
100 if (aFeaturesPreview.empty())
103 // Collect all edges as one big wire
104 boost::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
105 std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
106 for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
107 aBigWire->addEdge(*aShapeIt);
109 aBigWire->setOrigin(anOrigin->pnt());
110 aBigWire->setDirX(aDirX->dir());
111 aBigWire->setDirY(aDirY->dir());
112 aBigWire->setNorm(aNorm->dir());
114 // GeomAlgoAPI_SketchBuilder::createFaces(anOrigin->pnt(), aDirX->dir(), aDirY->dir(), aNorm->dir(),
115 // aFeaturesPreview, aLoops, aWires);
116 boost::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
117 aConstr->setShape(aBigWire);
121 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
123 boost::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
125 boost::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
126 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
131 int SketchPlugin_Sketch::numberOfSubs() const
133 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
136 boost::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
138 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
139 return boost::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
142 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
144 return subFeature(theIndex)->data()->featureId();
147 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
149 // check is this feature of result
150 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
152 ResultPtr aRes = boost::dynamic_pointer_cast<ModelAPI_Result>(theObject);
154 aFeature = document()->feature(aRes);
157 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
158 for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
159 if (*aSubIt == aFeature)
166 boost::shared_ptr<GeomAPI_Pnt> SketchPlugin_Sketch::to3D(const double theX, const double theY)
168 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
169 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
170 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
171 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
172 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
173 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
175 boost::shared_ptr<GeomAPI_XYZ> aSum = aC->pnt()->xyz()->added(aX->dir()->xyz()->multiplied(theX))
176 ->added(aY->dir()->xyz()->multiplied(theY));
178 return boost::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aSum));
181 boost::shared_ptr<GeomAPI_Pnt2d> SketchPlugin_Sketch::to2D(
182 const boost::shared_ptr<GeomAPI_Pnt>& thePnt)
184 boost::shared_ptr<GeomDataAPI_Point> aC = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
185 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
186 boost::shared_ptr<GeomDataAPI_Dir> aX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
187 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
188 boost::shared_ptr<GeomDataAPI_Dir> aY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
189 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
190 return thePnt->to2D(aC->pnt(), aX->dir(), aY->dir());
194 bool SketchPlugin_Sketch::isPlaneSet()
196 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
197 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
199 return aNormal && !(aNormal->x() == 0 && aNormal->y() == 0 && aNormal->z() == 0);
202 boost::shared_ptr<GeomAPI_Pln> SketchPlugin_Sketch::plane()
204 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
205 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
206 boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
207 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
209 if (!anOrigin || !aNorm)
210 return boost::shared_ptr<GeomAPI_Pln>();
212 return boost::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(anOrigin->pnt(), aNorm->dir()));
215 void addPlane(double theX, double theY, double theZ,
216 std::list<boost::shared_ptr<GeomAPI_Shape> >& theShapes)
218 boost::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
219 boost::shared_ptr<GeomAPI_Dir> aNormal(new GeomAPI_Dir(theX, theY, theZ));
220 double aSize = Config_PropManager::integer("Sketch planes", "Size of planes", PLANE_SIZE);
221 boost::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(anOrigin, aNormal,
223 theShapes.push_back(aFace);
226 AISObjectPtr SketchPlugin_Sketch::getAISObject(AISObjectPtr thePrevious)
228 boost::shared_ptr<GeomDataAPI_Dir> aNorm = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
229 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
231 if (!aNorm || (aNorm->x() == 0 && aNorm->y() == 0 && aNorm->z() == 0)) {
232 AISObjectPtr aAIS = thePrevious;
234 std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
236 addPlane(1, 0, 0, aFaces); // YZ plane
237 addPlane(0, 1, 0, aFaces); // XZ plane
238 addPlane(0, 0, 1, aFaces); // XY plane
239 boost::shared_ptr<GeomAPI_Shape> aCompound = GeomAlgoAPI_CompoundBuilder::compound(aFaces);
240 aAIS = AISObjectPtr(new GeomAPI_AISObject());
241 aAIS->createShape(aCompound);
243 std::vector<int> aRGB = Config_PropManager::color("Sketch planes", "planes_color",
245 aAIS->setColor(aRGB[0], aRGB[1], aRGB[2]);
247 aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness",
252 return AISObjectPtr();
255 void SketchPlugin_Sketch::erase()
257 boost::shared_ptr<ModelAPI_AttributeRefList> aRefList = boost::dynamic_pointer_cast<
258 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
259 std::list<ObjectPtr> aFeatures = aRefList->list();
260 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
261 for (; anIt != aFeatures.end(); anIt++) {
262 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
264 // subs are referenced from sketch, but must be removed for sure, so not checkings
265 document()->removeFeature(aFeature, false);
268 ModelAPI_CompositeFeature::erase();
271 void SketchPlugin_Sketch::attributeChanged() {
272 static bool myIsUpdated = false; // to avoid infinitive cycle on attrubtes change
273 boost::shared_ptr<GeomAPI_Shape> aSelection =
274 data()->selection(SketchPlugin_Feature::EXTERNAL_ID())->value();
275 if (aSelection && !myIsUpdated) { // update arguments due to the selection value
277 // update the sketch plane
278 boost::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
280 double anA, aB, aC, aD;
281 aPlane->coefficients(anA, aB, aC, aD);
283 // calculate attributes of the sketch
284 boost::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
285 boost::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
286 boost::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
287 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
288 boost::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
289 // X axis is preferable to be dirX on the sketch
290 static const double tol = 1.e-7;
291 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
292 boost::shared_ptr<GeomAPI_Dir> aTempDir(
293 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
294 boost::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
295 boost::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
297 boost::shared_ptr<GeomDataAPI_Point> anOrigin = boost::dynamic_pointer_cast<GeomDataAPI_Point>(
298 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
299 anOrigin->setValue(anOrigPnt);
300 boost::shared_ptr<GeomDataAPI_Dir> aNormal = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
301 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
302 aNormal->setValue(aNormDir);
303 boost::shared_ptr<GeomDataAPI_Dir> aDirX = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
304 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
305 aDirX->setValue(aXDir);
306 boost::shared_ptr<GeomDataAPI_Dir> aDirY = boost::dynamic_pointer_cast<GeomDataAPI_Dir>(
307 data()->attribute(SketchPlugin_Sketch::DIRY_ID()));
308 aDirY->setValue(aYDir);
309 boost::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();