1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_AttributeSelection.h
5 // Author: Mikhail PONIKAROV
7 #include "Model_AttributeSelection.h"
8 #include "Model_Application.h"
9 #include "Model_Events.h"
10 #include "Model_Data.h"
11 #include <ModelAPI_Feature.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_ResultConstruction.h>
14 #include <ModelAPI_CompositeFeature.h>
15 #include <GeomAPI_Shape.h>
16 #include <GeomAPI_PlanarEdges.h>
17 #include <GeomAlgoAPI_SketchBuilder.h>
18 #include <Events_Error.h>
20 #include <TNaming_Selector.hxx>
21 #include <TNaming_NamedShape.hxx>
22 #include <TNaming_Tool.hxx>
23 #include <TNaming_Builder.hxx>
24 #include <TopoDS_Shape.hxx>
25 #include <TDataStd_IntPackedMap.hxx>
26 #include <TDataStd_Integer.hxx>
27 #include <TopTools_MapOfShape.hxx>
28 #include <TopExp_Explorer.hxx>
29 #include <TDF_LabelMap.hxx>
30 #include <BRep_Tool.hxx>
31 #include <TopoDS_Edge.hxx>
33 #include <TColStd_MapOfTransient.hxx>
35 #include <Precision.hxx>
38 /// adeed to the index in the packed map to signalize that the vertex of edge is seleted
39 /// (multiplied by the index of the edge)
40 static const int kSTART_VERTEX_DELTA = 1000000;
42 // on this label is stored:
43 // TNaming_NamedShape - selected shape
44 // TNaming_Naming - topological selection information (for the body)
45 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
46 // TDataStd_Integer - type of the selected shape (for construction)
47 // TDF_Reference - from ReferenceAttribute, the context
49 void Model_AttributeSelection::setValue(const ResultPtr& theContext,
50 const std::shared_ptr<GeomAPI_Shape>& theSubShape)
52 const std::shared_ptr<GeomAPI_Shape>& anOldShape = value();
54 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
55 if (isOldShape) return; // shape is the same, so context is also unchanged
56 // update the referenced object if needed
57 bool isOldContext = theContext == myRef.value();
59 myRef.setValue(theContext);
61 if (theContext->groupName() == ModelAPI_ResultBody::group())
62 selectBody(theContext, theSubShape);
63 else if (theContext->groupName() == ModelAPI_ResultConstruction::group())
64 selectConstruction(theContext, theSubShape);
66 myIsInitialized = true;
67 owner()->data()->sendAttributeUpdated(this);
70 std::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
72 std::shared_ptr<GeomAPI_Shape> aResult;
73 if (myIsInitialized) {
74 Handle(TNaming_NamedShape) aSelection;
75 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
76 TopoDS_Shape aSelShape = aSelection->Get();
77 aResult = std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
78 aResult->setImpl(new TopoDS_Shape(aSelShape));
84 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
87 myIsInitialized = myRef.isInitialized();
90 ResultPtr Model_AttributeSelection::context() {
91 return std::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
95 void Model_AttributeSelection::setObject(const std::shared_ptr<ModelAPI_Object>& theObject)
97 ModelAPI_AttributeSelection::setObject(theObject);
98 myRef.setObject(theObject);
101 bool Model_AttributeSelection::update()
103 ResultPtr aContext = context();
104 if (!aContext) return false;
105 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
106 // body: just a named shape, use selection mechanism from OCCT
107 TNaming_Selector aSelector(selectionLabel());
108 TDF_LabelMap aScope; // empty means the whole document
109 bool aResult = aSelector.Solve(aScope) == Standard_True;
110 owner()->data()->sendAttributeUpdated(this);
112 } else if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
113 // construction: identification by the results indexes, recompute faces and
114 // take the face that more close by the indexes
115 std::shared_ptr<GeomAPI_PlanarEdges> aWirePtr =
116 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(
117 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext)->shape());
118 if (aWirePtr && aWirePtr->hasPlane()) {
119 TDF_Label aLab = myRef.myRef->Label();
120 // getting a type of selected shape
121 Handle(TDataStd_Integer) aTypeAttr;
122 if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
125 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
126 // selected indexes will be needed in each "if"
127 Handle(TDataStd_IntPackedMap) aSubIds;
128 std::shared_ptr<GeomAPI_Shape> aNewSelected;
130 !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
131 // for now working only with composite features
132 FeaturePtr aContextFeature = aContext->document()->feature(aContext);
133 CompositeFeaturePtr aComposite =
134 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
135 if (!aComposite || aComposite->numberOfSubs() == 0) {
139 if (aShapeType == TopAbs_FACE) {
140 // If this is a wire with plane defined thin it is a sketch-like object
141 std::list<std::shared_ptr<GeomAPI_Shape> > aFaces;
142 GeomAlgoAPI_SketchBuilder::createFaces(aWirePtr->origin(), aWirePtr->dirX(),
143 aWirePtr->dirY(), aWirePtr->norm(), aWirePtr, aFaces);
144 if (aFaces.empty()) // no faces, update can not work correctly
146 // if there is no edges indexes, any face can be used: take the first
147 std::shared_ptr<GeomAPI_Shape> aNewSelected;
149 aNewSelected = *(aFaces.begin());
150 } else { // searching for most looks-like initial face by the indexes
151 // prepare edges of the current resut for the fast searching
152 TColStd_MapOfTransient allCurves;
153 const int aSubNum = aComposite->numberOfSubs();
154 for(int a = 0; a < aSubNum; a++) {
155 if (aSubIds->Contains(aComposite->subFeatureId(a))) {
156 FeaturePtr aSub = aComposite->subFeature(a);
157 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
158 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
159 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
160 ResultConstructionPtr aConstr =
161 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
162 if (aConstr->shape() && aConstr->shape()->isEdge()) {
163 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
164 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
165 if (!anEdge.IsNull()) {
166 Standard_Real aFirst, aLast;
167 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
168 allCurves.Add(aCurve);
174 // iterate new result faces and searching for these edges
175 std::list<std::shared_ptr<GeomAPI_Shape> >::iterator aFacesIter = aFaces.begin();
176 double aBestFound = 0; // best percentage of found edges
177 for(; aFacesIter != aFaces.end(); aFacesIter++) {
178 int aFound = 0, aNotFound = 0;
179 TopExp_Explorer anEdgesExp((*aFacesIter)->impl<TopoDS_Shape>(), TopAbs_EDGE);
180 for(; anEdgesExp.More(); anEdgesExp.Next()) {
181 TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
182 if (!anEdge.IsNull()) {
183 Standard_Real aFirst, aLast;
184 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
185 if (allCurves.Contains(aCurve)) {
192 if (aFound + aNotFound != 0) {
193 double aPercentage = double(aFound) / double(aFound + aNotFound);
194 if (aPercentage > aBestFound) {
195 aBestFound = aPercentage;
196 aNewSelected = *aFacesIter;
201 if (aNewSelected) { // store this new selection
202 selectConstruction(aContext, aNewSelected);
203 owner()->data()->sendAttributeUpdated(this);
206 } else if (aShapeType == TopAbs_EDGE) {
207 // just reselect the edge by the id
208 const int aSubNum = aComposite->numberOfSubs();
209 for(int a = 0; a < aSubNum; a++) {
210 // if aSubIds take any, the first appropriate
211 if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
212 // found the appropriate feature
213 FeaturePtr aFeature = aComposite->subFeature(a);
214 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
215 aFeature->results().cbegin();
216 for(;aResIter != aFeature->results().cend(); aResIter++) {
217 ResultConstructionPtr aRes =
218 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
219 if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
220 selectConstruction(aContext, aRes->shape());
221 owner()->data()->sendAttributeUpdated(this);
227 } else if (aShapeType == TopAbs_VERTEX) {
228 // just reselect the vertex by the id of edge
229 const int aSubNum = aComposite->numberOfSubs();
230 for(int a = 0; a < aSubNum; a++) {
231 // if aSubIds take any, the first appropriate
232 int aFeatureID = aComposite->subFeatureId(a);
233 if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID)) {
234 // searching for deltas
236 if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
237 else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 2;
238 // found the feature with appropriate edge
239 FeaturePtr aFeature = aComposite->subFeature(a);
240 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
241 aFeature->results().cbegin();
242 for(;aResIter != aFeature->results().cend(); aResIter++) {
243 ResultConstructionPtr aRes =
244 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
245 if (aRes && aRes->shape()) {
246 if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
247 selectConstruction(aContext, aRes->shape());
248 owner()->data()->sendAttributeUpdated(this);
250 } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
251 const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
253 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
254 if (aVIndex == aVertexNum) { // found!
255 std::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
256 aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
257 selectConstruction(aContext, aVertex);
258 owner()->data()->sendAttributeUpdated(this);
271 return false; // unknown case
275 void Model_AttributeSelection::selectBody(
276 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
278 // perform the selection
279 TNaming_Selector aSel(selectionLabel());
280 TopoDS_Shape aNewShape = theSubShape ? theSubShape->impl<TopoDS_Shape>() : TopoDS_Shape();
281 TopoDS_Shape aContext;
283 ResultBodyPtr aBody = std::dynamic_pointer_cast<ModelAPI_ResultBody>(myRef.value());
285 aContext = aBody->shape()->impl<TopoDS_Shape>();
287 ResultConstructionPtr aConstr = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myRef.value());
289 aContext = aConstr->shape()->impl<TopoDS_Shape>();
291 Events_Error::send("A result with shape is expected");
295 aSel.Select(aNewShape, aContext);
298 void Model_AttributeSelection::selectConstruction(
299 const ResultPtr& theContext, const std::shared_ptr<GeomAPI_Shape>& theSubShape)
301 FeaturePtr aContextFeature = theContext->document()->feature(theContext);
302 CompositeFeaturePtr aComposite =
303 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
304 if (!aComposite || aComposite->numberOfSubs() == 0) {
305 return; // saving of context is enough: result construction contains exactly the needed shape
307 std::shared_ptr<Model_Data> aData = std::dynamic_pointer_cast<Model_Data>(owner()->data());
308 TDF_Label aLab = myRef.myRef->Label();
309 // identify the reuslts of sub-object of the composite by edges
310 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
311 // save type of the selected shape in integer attribute
312 TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
313 TDataStd_Integer::Set(aLab, (int)aShapeType);
315 TColStd_MapOfTransient allCurves;
316 if (aShapeType == TopAbs_VERTEX) { // compare positions
317 aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
319 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
320 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
321 Standard_Real aFirst, aLast;
322 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
323 allCurves.Add(aCurve);
326 // iterate and store the result ids of sub-elements
327 Handle(TDataStd_IntPackedMap) aRefs = TDataStd_IntPackedMap::Set(aLab);
329 const int aSubNum = aComposite->numberOfSubs();
330 for(int a = 0; a < aSubNum; a++) {
331 FeaturePtr aSub = aComposite->subFeature(a);
332 const std::list<std::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
333 std::list<std::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
334 // there may be many shapes (circle and center): register if at least one is in selection
335 for(; aRes != aResults.cend(); aRes++) {
336 ResultConstructionPtr aConstr =
337 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
338 if (!aConstr->shape()) {
341 if (aShapeType == TopAbs_VERTEX) {
342 if (aConstr->shape()->isVertex()) { // compare vertices positions
343 const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
344 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
345 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
346 aRefs->Add(aComposite->subFeatureId(a));
348 } else { // get first or last vertex of the edge: last is stored with negative sign
349 const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
350 int aDelta = kSTART_VERTEX_DELTA;
351 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
352 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
353 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
354 aRefs->Add(aComposite->subFeatureId(a));
355 aRefs->Add(aDelta + aComposite->subFeatureId(a));
358 aDelta += kSTART_VERTEX_DELTA;
362 if (aConstr->shape()->isEdge()) {
363 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
364 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
365 if (!anEdge.IsNull()) {
366 Standard_Real aFirst, aLast;
367 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
368 if (allCurves.Contains(aCurve)) {
369 int anID = aComposite->subFeatureId(a);
371 // add edges to sub-label to support naming for edges selection
372 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
373 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
374 Standard_Real aFirst, aLast;
375 Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
376 if (aFaceCurve == aCurve) {
377 TNaming_Builder anEdgeBuilder(selectionLabel().FindChild(anID));
378 anEdgeBuilder.Generated(anEdge);
387 // store the selected as primitive
388 TNaming_Builder aBuilder(selectionLabel());
389 aBuilder.Generated(aSubShape);
392 TDF_Label Model_AttributeSelection::selectionLabel()
394 return myRef.myRef->Label().FindChild(1);