1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
3 // File: SketchPlugin_Sketch.cxx
4 // Created: 27 Mar 2014
5 // Author: Mikhail PONIKAROV
7 #include <Config_PropManager.h>
9 #include <GeomAlgoAPI_CompoundBuilder.h>
10 #include <GeomAlgoAPI_FaceBuilder.h>
12 #include <GeomAPI_AISObject.h>
13 #include <GeomAPI_Dir.h>
14 #include <GeomAPI_PlanarEdges.h>
16 #include <GeomAlgoAPI_FaceBuilder.h>
18 #include <ModelAPI_AttributeRefList.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_ResultConstruction.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Session.h>
27 #include <SketchPlugin_Sketch.h>
28 #include <SketchPlugin_Feature.h>
29 #include <SketchPlugin_SketchEntity.h>
38 SketchPlugin_Sketch::SketchPlugin_Sketch()
42 void SketchPlugin_Sketch::initAttributes()
44 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
45 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
46 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
47 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
48 // the selected face, base for the sketcher plane, not obligatory
49 data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
50 ModelAPI_Session::get()->validators()->registerNotObligatory(
51 getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
54 void SketchPlugin_Sketch::execute()
56 if (!data()->isValid())
58 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
59 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
61 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
62 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
63 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
64 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
65 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
66 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
68 std::list<ObjectPtr> aFeatures = aRefList->list();
69 if (aFeatures.empty())
72 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
73 std::shared_ptr<SketchPlugin_Feature> aFeature;
74 std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
75 for (; anIt != aLast; anIt++) {
76 aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
78 if (!aFeature->sketch()) // on load document the back references are missed
79 aFeature->setSketch(this);
80 // do not include the external edges into the result
81 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
82 if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value())
85 // do not include the construction entities in the result
86 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
87 if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
91 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
92 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
93 for (; aResIter != aRes.cend(); aResIter++) {
94 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
95 ModelAPI_ResultConstruction>(*aResIter);
97 std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
99 aFeaturesPreview.push_back(aShape);
105 if (aFeaturesPreview.empty())
108 // Collect all edges as one big wire
109 std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
110 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
111 for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
112 aBigWire->addEdge(*aShapeIt);
114 aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
115 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
116 aConstr->setShape(aBigWire);
120 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
122 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID);
124 std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew)->setSketch(this);
125 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
130 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
132 if (!data().get()) // sketch is already removed (case on undo of sketch), sync is not needed
134 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
135 list<ObjectPtr>::iterator aSubIt = aSubs.begin(), aLastIt = aSubs.end();
136 bool isRemoved = false;
137 bool aHasEmtpyFeature = false;
138 for(; aSubIt != aLastIt && !isRemoved; aSubIt++) {
139 std::shared_ptr<ModelAPI_Feature> aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*aSubIt);
140 if (aFeature.get() != NULL && aFeature == theFeature) {
141 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(aFeature);
144 else if (aFeature.get() == NULL)
145 aHasEmtpyFeature = true;
147 // if the object is not found in the sketch sub-elements, that means that the object is removed already.
148 // Find the first empty element and remove it
149 if (!isRemoved && aHasEmtpyFeature)
150 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->remove(ObjectPtr());
153 int SketchPlugin_Sketch::numberOfSubs() const
155 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->size();
158 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(const int theIndex) const
160 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex);
161 return std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
164 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
166 return subFeature(theIndex)->data()->featureId();
169 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
171 // check is this feature of result
172 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
174 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
176 aFeature = document()->feature(aRes);
179 list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
180 for(list<ObjectPtr>::iterator aSubIt = aSubs.begin(); aSubIt != aSubs.end(); aSubIt++) {
181 if (*aSubIt == aFeature)
189 void SketchPlugin_Sketch::erase()
191 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
192 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
193 std::list<ObjectPtr> aFeatures = aRefList->list();
194 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
195 for (; anIt != aFeatures.end(); anIt++) {
196 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(*anIt);
198 // subs are referenced from sketch, but must be removed for sure, so not checkings
199 document()->removeFeature(aFeature);
202 ModelAPI_CompositeFeature::erase();
205 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
206 if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
207 std::shared_ptr<GeomAPI_Shape> aSelection =
208 data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
209 if (aSelection) { // update arguments due to the selection value
210 // update the sketch plane
211 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
213 double anA, aB, aC, aD;
214 aPlane->coefficients(anA, aB, aC, aD);
216 // calculate attributes of the sketch
217 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
218 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
219 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
220 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
221 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
222 // X axis is preferable to be dirX on the sketch
223 static const double tol = 1.e-7;
224 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
225 std::shared_ptr<GeomAPI_Dir> aTempDir(
226 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
227 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
228 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
230 // update position of the sketch
231 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
232 <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
233 anOrigin->setValue(anOrigPnt);
234 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
235 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
236 aNormal->setValue(aNormDir);
237 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
238 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
239 aDirX->setValue(aXDir);
240 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();