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_Dir.h>
13 #include <GeomAPI_PlanarEdges.h>
15 #include <GeomDataAPI_Point2D.h>
16 #include <GeomAlgoAPI_PointBuilder.h>
18 #include <ModelAPI_AttributeRefList.h>
19 #include <ModelAPI_AttributeString.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>
27 #include <ModelAPI_Events.h>
29 #include <SketchPlugin_Sketch.h>
30 #include <SketchPlugin_Feature.h>
31 #include <SketchPlugin_SketchEntity.h>
32 #include <SketchPlugin_Tools.h>
34 #include <Events_Loop.h>
43 SketchPlugin_Sketch::SketchPlugin_Sketch()
47 void SketchPlugin_Sketch::initAttributes()
49 data()->addAttribute(SketchPlugin_Sketch::ORIGIN_ID(), GeomDataAPI_Point::typeId());
50 data()->addAttribute(SketchPlugin_Sketch::DIRX_ID(), GeomDataAPI_Dir::typeId());
51 data()->addAttribute(SketchPlugin_Sketch::NORM_ID(), GeomDataAPI_Dir::typeId());
52 data()->addAttribute(SketchPlugin_Sketch::FEATURES_ID(), ModelAPI_AttributeRefList::typeId());
53 // the selected face, base for the sketcher plane, not obligatory
54 data()->addAttribute(SketchPlugin_SketchEntity::EXTERNAL_ID(), ModelAPI_AttributeSelection::typeId());
55 ModelAPI_Session::get()->validators()->registerNotObligatory(
56 getKind(), SketchPlugin_SketchEntity::EXTERNAL_ID());
57 data()->addAttribute(SketchPlugin_Sketch::SOLVER_ERROR(), ModelAPI_AttributeString::typeId());
58 ModelAPI_Session::get()->validators()->registerNotObligatory(
59 getKind(), SketchPlugin_Sketch::SOLVER_ERROR());
60 data()->addAttribute(SketchPlugin_Sketch::SOLVER_DOF(), ModelAPI_AttributeString::typeId());
61 ModelAPI_Session::get()->validators()->registerNotObligatory(
62 getKind(), SketchPlugin_Sketch::SOLVER_DOF());
65 void SketchPlugin_Sketch::execute()
67 if (!data()->isValid())
69 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
70 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
72 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
73 data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
74 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
75 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
76 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
77 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
79 std::list<ObjectPtr> aFeatures = aRefList->list();
80 if (aFeatures.empty()) // actually, this must be avoided by the validators
83 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin(), aLast = aFeatures.end();
84 std::shared_ptr<SketchPlugin_Feature> aFeature;
85 std::list<std::shared_ptr<GeomAPI_Shape> > aFeaturesPreview;
86 for (; anIt != aLast; anIt++) {
87 aFeature = std::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
89 if (!aFeature->sketch()) // on load document the back references are missed
90 aFeature->setSketch(this);
91 // do not include the external edges into the result
92 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID())) {
93 if (aFeature->data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value())
96 // do not include the construction entities in the result
97 if (aFeature->data()->attribute(SketchPlugin_SketchEntity::AUXILIARY_ID())) {
98 if (aFeature->data()->boolean(SketchPlugin_SketchEntity::AUXILIARY_ID())->value())
102 const std::list<std::shared_ptr<ModelAPI_Result> >& aRes = aFeature->results();
103 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter = aRes.cbegin();
104 for (; aResIter != aRes.cend(); aResIter++) {
105 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = std::dynamic_pointer_cast<
106 ModelAPI_ResultConstruction>(*aResIter);
108 std::shared_ptr<GeomAPI_Shape> aShape = aConstr->shape();
110 aFeaturesPreview.push_back(aShape);
116 // Collect all edges as one big wire
117 std::shared_ptr<GeomAPI_PlanarEdges> aBigWire(new GeomAPI_PlanarEdges);
118 std::list<std::shared_ptr<GeomAPI_Shape> >::const_iterator aShapeIt = aFeaturesPreview.begin();
119 for (; aShapeIt != aFeaturesPreview.end(); ++aShapeIt) {
120 aBigWire->addEdge(*aShapeIt);
122 aBigWire->setPlane(anOrigin->pnt(), aDirX->dir(), aNorm->dir());
123 std::shared_ptr<ModelAPI_ResultConstruction> aConstr = document()->createConstruction(data());
124 aConstr->setShape(aBigWire);
128 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::addFeature(std::string theID)
130 std::shared_ptr<ModelAPI_Feature> aNew = document()->addFeature(theID, false);
132 // the sketch cannot be specified for the macro-features defined in python
133 // like SketchRectangle, so we need to check the type of new feature
134 std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
135 std::dynamic_pointer_cast<SketchPlugin_Feature>(aNew);
137 aSketchFeature->setSketch(this);
138 data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->append(aNew);
140 // set as current also after it becomes sub to set correctly enabled for other sketch subs
141 document()->setCurrentFeature(aNew, false);
145 void SketchPlugin_Sketch::removeFeature(std::shared_ptr<ModelAPI_Feature> theFeature)
147 if (!data()->isValid()) // sketch is already removed (case on undo of sketch), sync is not needed
149 AttributeRefListPtr aList = reflist(SketchPlugin_Sketch::FEATURES_ID());
150 // if the object is last, remove it from the list (needed to skip empty transaction on edit of sketch feature)
151 if (aList->object(aList->size(true) - 1, true) == theFeature) {
152 aList->remove(theFeature);
154 // to keep the persistent sub-elements indexing, do not remove elements from list,
155 // but substitute by nulls
156 aList->substitute(theFeature, ObjectPtr());
160 int SketchPlugin_Sketch::numberOfSubs(bool forTree) const
164 return data()->reflist(FEATURES_ID())->size(false);
167 std::shared_ptr<ModelAPI_Feature> SketchPlugin_Sketch::subFeature(
168 const int theIndex, bool forTree)
173 ObjectPtr anObj = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->object(theIndex, false);
174 FeaturePtr aRes = std::dynamic_pointer_cast<ModelAPI_Feature>(anObj);
178 int SketchPlugin_Sketch::subFeatureId(const int theIndex) const
180 std::shared_ptr<ModelAPI_AttributeRefList> aRefList = std::dynamic_pointer_cast<
181 ModelAPI_AttributeRefList>(data()->attribute(SketchPlugin_Sketch::FEATURES_ID()));
182 std::list<ObjectPtr> aFeatures = aRefList->list();
183 std::list<ObjectPtr>::const_iterator anIt = aFeatures.begin();
184 int aResultIndex = 1; // number of the counted (created) features, started from 1
185 int aFeatureIndex = -1; // number of the not-empty features in the list
186 for (; anIt != aFeatures.end(); anIt++) {
189 if (aFeatureIndex == theIndex)
196 bool SketchPlugin_Sketch::isSub(ObjectPtr theObject) const
198 // check is this feature of result
199 FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theObject);
201 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
203 aFeature = document()->feature(aRes);
206 return data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->isInList(aFeature);
212 void SketchPlugin_Sketch::attributeChanged(const std::string& theID) {
213 if (theID == SketchPlugin_SketchEntity::EXTERNAL_ID()) {
214 std::shared_ptr<GeomAPI_Shape> aSelection =
215 data()->selection(SketchPlugin_SketchEntity::EXTERNAL_ID())->value();
216 if (aSelection) { // update arguments due to the selection value
217 // update the sketch plane
218 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aSelection);
220 double anA, aB, aC, aD;
221 aPlane->coefficients(anA, aB, aC, aD);
223 // calculate attributes of the sketch
224 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
225 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
226 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
227 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
228 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
229 // X axis is preferable to be dirX on the sketch
230 static const double tol = 0.1; // here can not be very small value to avoid very close to X normal axis (issue 595)
231 bool isX = fabs(anA) - 1.0 < tol && fabs(aB) < tol && fabs(aC) < tol;
232 std::shared_ptr<GeomAPI_Dir> aTempDir(
233 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
234 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
235 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
237 // update position of the sketch
238 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast
239 <GeomDataAPI_Point>(data()->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
240 anOrigin->setValue(anOrigPnt);
241 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
242 data()->attribute(SketchPlugin_Sketch::NORM_ID()));
243 aNormal->setValue(aNormDir);
244 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
245 data()->attribute(SketchPlugin_Sketch::DIRX_ID()));
246 aDirX->setValue(aXDir);
247 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
250 } else if (theID == NORM_ID() || theID == DIRX_ID() || theID == ORIGIN_ID()) {
251 // send all sub-elements are also updated: all entities become created on different plane
252 static Events_ID anUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
253 std::list<ObjectPtr> aSubs = data()->reflist(SketchPlugin_Sketch::FEATURES_ID())->list();
254 std::list<ObjectPtr>::iterator aSub = aSubs.begin();
255 for(; aSub != aSubs.end(); aSub++) {
257 ModelAPI_EventCreator::get()->sendUpdated(*aSub, anUpdateEvent);
262 void SketchPlugin_Sketch::createPoint2DResult(ModelAPI_Feature* theFeature,
263 SketchPlugin_Sketch* theSketch,
264 const std::string& theAttributeID, const int theIndex)
266 std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
267 theFeature->attribute(theAttributeID));
269 if (!aPoint || !aPoint->isInitialized())
272 std::shared_ptr<GeomAPI_Pnt> aCenter(theSketch->to3D(aPoint->x(), aPoint->y()));
273 //std::cout<<"Execute circle "<<aCenter->x()<<" "<<aCenter->y()<<" "<<aCenter->z()<<std::endl;
274 // make a visible point
275 std::shared_ptr<GeomAPI_Shape> aCenterPointShape = GeomAlgoAPI_PointBuilder::point(aCenter);
276 std::shared_ptr<ModelAPI_ResultConstruction> aResult = theFeature->document()->createConstruction(
277 theFeature->data(), theIndex);
278 aResult->setShape(aCenterPointShape);
279 aResult->setIsInHistory(false);
281 theFeature->setResult(aResult, theIndex);
284 FeaturePtr SketchPlugin_Sketch::addUniqueNamedCopiedFeature(FeaturePtr theFeature,
285 SketchPlugin_Sketch* theSketch,
286 const bool theIsCopy)
288 FeaturePtr aNewFeature = theSketch->addFeature(theFeature->getKind());
289 // addFeature generates a unique name for the feature, it caches the name
290 std::string aUniqueFeatureName = aNewFeature->data()->name();
291 // all attribute values are copied\pasted to the new feature, name is not an exception
292 theFeature->data()->copyTo(aNewFeature->data());
293 // as a name for the feature, the generated unique name is set
294 aNewFeature->data()->setName(aUniqueFeatureName);
295 // text expressions could block setValue of some attributes
296 SketchPlugin_Tools::clearExpressions(aNewFeature);
297 // Set copy attribute
298 AttributeBooleanPtr anAttr = aNewFeature->data()->boolean(SketchPlugin_SketchEntity::COPY_ID());
300 anAttr->setValue(theIsCopy);
306 std::shared_ptr<GeomAPI_Ax3> SketchPlugin_Sketch::plane(SketchPlugin_Sketch* theSketch)
308 std::shared_ptr<ModelAPI_Data> aData = theSketch->data();
310 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
311 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
312 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
313 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
314 std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
315 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
317 return std::shared_ptr<GeomAPI_Ax3>(new GeomAPI_Ax3(anOrigin->pnt(), aDirX->dir(), aNorm->dir()));
320 void SketchPlugin_Sketch::exchangeIDs(
321 std::shared_ptr<ModelAPI_Feature> theFeature1, std::shared_ptr<ModelAPI_Feature> theFeature2)
323 reflist(SketchPlugin_Sketch::FEATURES_ID())->exchange(theFeature1, theFeature2);