1 // File: Model_AttributeSelection.h
3 // Author: Mikhail PONIKAROV
5 #include "Model_AttributeSelection.h"
6 #include "Model_Application.h"
7 #include "Model_Events.h"
8 #include "Model_Data.h"
9 #include <ModelAPI_Feature.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultConstruction.h>
12 #include <ModelAPI_CompositeFeature.h>
13 #include <GeomAPI_Shape.h>
14 #include <GeomAPI_PlanarEdges.h>
15 #include <GeomAlgoAPI_SketchBuilder.h>
16 #include <Events_Error.h>
18 #include <TNaming_Selector.hxx>
19 #include <TNaming_NamedShape.hxx>
20 #include <TNaming_Tool.hxx>
21 #include <TNaming_Builder.hxx>
22 #include <TopoDS_Shape.hxx>
23 #include <TDataStd_IntPackedMap.hxx>
24 #include <TDataStd_Integer.hxx>
25 #include <TopTools_MapOfShape.hxx>
26 #include <TopExp_Explorer.hxx>
27 #include <TDF_LabelMap.hxx>
28 #include <BRep_Tool.hxx>
29 #include <TopoDS_Edge.hxx>
31 #include <TColStd_MapOfTransient.hxx>
33 #include <Precision.hxx>
36 /// adeed to the index in the packed map to signalize that the vertex of edge is seleted
37 /// (multiplied by the index of the edge)
38 static const int kSTART_VERTEX_DELTA = 1000000;
40 // on this label is stored:
41 // TNaming_NamedShape - selected shape
42 // TNaming_Naming - topological selection information (for the body)
43 // TDataStd_IntPackedMap - indexes of edges in composite element (for construction)
44 // TDataStd_Integer - type of the selected shape (for construction)
45 // TDF_Reference - from ReferenceAttribute, the context
47 void Model_AttributeSelection::setValue(const ResultPtr& theContext,
48 const boost::shared_ptr<GeomAPI_Shape>& theSubShape)
50 const boost::shared_ptr<GeomAPI_Shape>& anOldShape = value();
52 (theSubShape == anOldShape || (theSubShape && anOldShape && theSubShape->isEqual(anOldShape)));
53 if (isOldShape) return; // shape is the same, so context is also unchanged
54 // update the referenced object if needed
55 bool isOldContext = theContext == myRef.value();
57 myRef.setValue(theContext);
59 if (theContext->groupName() == ModelAPI_ResultBody::group())
60 selectBody(theContext, theSubShape);
61 else if (theContext->groupName() == ModelAPI_ResultConstruction::group())
62 selectConstruction(theContext, theSubShape);
64 myIsInitialized = true;
65 owner()->data()->sendAttributeUpdated(this);
68 boost::shared_ptr<GeomAPI_Shape> Model_AttributeSelection::value()
70 boost::shared_ptr<GeomAPI_Shape> aResult;
71 if (myIsInitialized) {
72 Handle(TNaming_NamedShape) aSelection;
73 if (selectionLabel().FindAttribute(TNaming_NamedShape::GetID(), aSelection)) {
74 TopoDS_Shape aSelShape = aSelection->Get();
75 aResult = boost::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
76 aResult->setImpl(new TopoDS_Shape(aSelShape));
82 Model_AttributeSelection::Model_AttributeSelection(TDF_Label& theLabel)
85 myIsInitialized = myRef.isInitialized();
88 ResultPtr Model_AttributeSelection::context() {
89 return boost::dynamic_pointer_cast<ModelAPI_Result>(myRef.value());
93 void Model_AttributeSelection::setObject(const boost::shared_ptr<ModelAPI_Object>& theObject)
95 ModelAPI_AttributeSelection::setObject(theObject);
96 myRef.setObject(theObject);
99 bool Model_AttributeSelection::update()
101 ResultPtr aContext = context();
102 if (!aContext) return false;
103 if (aContext->groupName() == ModelAPI_ResultBody::group()) {
104 // body: just a named shape, use selection mechanism from OCCT
105 TNaming_Selector aSelector(selectionLabel());
106 TDF_LabelMap aScope; // empty means the whole document
107 owner()->data()->sendAttributeUpdated(this);
108 bool aResult = aSelector.Solve(aScope) == Standard_True;
109 owner()->data()->sendAttributeUpdated(this);
111 } else if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
112 // construction: identification by the results indexes, recompute faces and
113 // take the face that more close by the indexes
114 boost::shared_ptr<GeomAPI_PlanarEdges> aWirePtr =
115 boost::dynamic_pointer_cast<GeomAPI_PlanarEdges>(
116 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext)->shape());
117 if (aWirePtr && aWirePtr->hasPlane()) {
118 boost::shared_ptr<Model_Data> aData =
119 boost::dynamic_pointer_cast<Model_Data>(owner()->data());
120 TDF_Label aLab = aData->label();
121 // getting a type of selected shape
122 Handle(TDataStd_Integer) aTypeAttr;
123 if (!aLab.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr)) {
126 TopAbs_ShapeEnum aShapeType = (TopAbs_ShapeEnum)(aTypeAttr->Get());
127 // selected indexes will be needed in each "if"
128 Handle(TDataStd_IntPackedMap) aSubIds;
129 boost::shared_ptr<GeomAPI_Shape> aNewSelected;
131 !aLab.FindAttribute(TDataStd_IntPackedMap::GetID(), aSubIds) || aSubIds->Extent() == 0;
132 // for now working only with composite features
133 FeaturePtr aContextFeature = owner()->document()->feature(aContext);
134 CompositeFeaturePtr aComposite =
135 boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
136 if (!aComposite || aComposite->numberOfSubs() == 0) {
140 if (aShapeType == TopAbs_FACE) {
141 // If this is a wire with plane defined thin it is a sketch-like object
142 std::list<boost::shared_ptr<GeomAPI_Shape> > aFaces;
143 GeomAlgoAPI_SketchBuilder::createFaces(aWirePtr->origin(), aWirePtr->dirX(),
144 aWirePtr->dirY(), aWirePtr->norm(), aWirePtr, aFaces);
145 if (aFaces.empty()) // no faces, update can not work correctly
147 // if there is no edges indexes, any face can be used: take the first
148 boost::shared_ptr<GeomAPI_Shape> aNewSelected;
150 aNewSelected = *(aFaces.begin());
151 } else { // searching for most looks-like initial face by the indexes
152 // prepare edges of the current resut for the fast searching
153 TColStd_MapOfTransient allCurves;
154 const int aSubNum = aComposite->numberOfSubs();
155 for(int a = 0; a < aSubNum; a++) {
156 if (aSubIds->Contains(aComposite->subFeatureId(a))) {
157 FeaturePtr aSub = aComposite->subFeature(a);
158 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
159 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
160 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
161 ResultConstructionPtr aConstr =
162 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
163 if (aConstr->shape() && aConstr->shape()->isEdge()) {
164 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
165 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
166 if (!anEdge.IsNull()) {
167 Standard_Real aFirst, aLast;
168 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
169 allCurves.Add(aCurve);
175 // iterate new result faces and searching for these edges
176 std::list<boost::shared_ptr<GeomAPI_Shape> >::iterator aFacesIter = aFaces.begin();
177 double aBestFound = 0; // best percentage of found edges
178 for(; aFacesIter != aFaces.end(); aFacesIter++) {
179 int aFound = 0, aNotFound = 0;
180 TopExp_Explorer anEdgesExp((*aFacesIter)->impl<TopoDS_Shape>(), TopAbs_EDGE);
181 for(; anEdgesExp.More(); anEdgesExp.Next()) {
182 TopoDS_Edge anEdge = TopoDS::Edge(anEdgesExp.Current());
183 if (!anEdge.IsNull()) {
184 Standard_Real aFirst, aLast;
185 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
186 if (allCurves.Contains(aCurve)) {
193 if (aFound + aNotFound != 0) {
194 double aPercentage = double(aFound) / double(aFound + aNotFound);
195 if (aPercentage > aBestFound) {
196 aBestFound = aPercentage;
197 aNewSelected = *aFacesIter;
202 if (aNewSelected) { // store this new selection
203 selectConstruction(aContext, aNewSelected);
204 owner()->data()->sendAttributeUpdated(this);
207 } else if (aShapeType == TopAbs_EDGE) {
208 // just reselect the edge by the id
209 const int aSubNum = aComposite->numberOfSubs();
210 for(int a = 0; a < aSubNum; a++) {
211 // if aSubIds take any, the first appropriate
212 if (aSubIds->IsEmpty() || aSubIds->Contains(aComposite->subFeatureId(a))) {
213 // found the appropriate feature
214 FeaturePtr aFeature = aComposite->subFeature(a);
215 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
216 aFeature->results().cbegin();
217 for(;aResIter != aFeature->results().cend(); aResIter++) {
218 ResultConstructionPtr aRes =
219 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
220 if (aRes && aRes->shape() && aRes->shape()->isEdge()) { // found!
221 selectConstruction(aContext, aRes->shape());
222 owner()->data()->sendAttributeUpdated(this);
228 } else if (aShapeType == TopAbs_VERTEX) {
229 // just reselect the vertex by the id of edge
230 const int aSubNum = aComposite->numberOfSubs();
231 for(int a = 0; a < aSubNum; a++) {
232 // if aSubIds take any, the first appropriate
233 int aFeatureID = aComposite->subFeatureId(a);
234 if (aSubIds->IsEmpty() || aSubIds->Contains(aFeatureID)) {
235 // searching for deltas
237 if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 1;
238 else if (aSubIds->Contains(aFeatureID + kSTART_VERTEX_DELTA)) aVertexNum = 2;
239 // found the feature with appropriate edge
240 FeaturePtr aFeature = aComposite->subFeature(a);
241 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
242 aFeature->results().cbegin();
243 for(;aResIter != aFeature->results().cend(); aResIter++) {
244 ResultConstructionPtr aRes =
245 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aResIter);
246 if (aRes && aRes->shape()) {
247 if (aRes->shape()->isVertex() && aVertexNum == 0) { // found!
248 selectConstruction(aContext, aRes->shape());
249 owner()->data()->sendAttributeUpdated(this);
251 } else if (aRes->shape()->isEdge() && aVertexNum > 0) {
252 const TopoDS_Shape& anEdge = aRes->shape()->impl<TopoDS_Shape>();
254 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
255 if (aVIndex == aVertexNum) { // found!
256 boost::shared_ptr<GeomAPI_Shape> aVertex(new GeomAPI_Shape);
257 aVertex->setImpl(new TopoDS_Shape(aVExp.Current()));
258 selectConstruction(aContext, aVertex);
259 owner()->data()->sendAttributeUpdated(this);
272 return false; // unknown case
276 void Model_AttributeSelection::selectBody(
277 const ResultPtr& theContext, const boost::shared_ptr<GeomAPI_Shape>& theSubShape)
279 // perform the selection
280 TNaming_Selector aSel(selectionLabel());
281 TopoDS_Shape aNewShape = theSubShape ? theSubShape->impl<TopoDS_Shape>() : TopoDS_Shape();
282 TopoDS_Shape aContext;
284 ResultBodyPtr aBody = boost::dynamic_pointer_cast<ModelAPI_ResultBody>(myRef.value());
286 aContext = aBody->shape()->impl<TopoDS_Shape>();
288 ResultConstructionPtr aConstr = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(myRef.value());
290 aContext = aConstr->shape()->impl<TopoDS_Shape>();
292 Events_Error::send("A result with shape is expected");
296 aSel.Select(aNewShape, aContext);
299 void Model_AttributeSelection::selectConstruction(
300 const ResultPtr& theContext, const boost::shared_ptr<GeomAPI_Shape>& theSubShape)
302 FeaturePtr aContextFeature = owner()->document()->feature(theContext);
303 CompositeFeaturePtr aComposite =
304 boost::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aContextFeature);
305 if (!aComposite || aComposite->numberOfSubs() == 0) {
306 return; // saving of context is enough: result construction contains exactly the needed shape
308 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(owner()->data());
309 TDF_Label aLab = aData->label();
310 // identify the reuslts of sub-object of the composite by edges
311 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
312 // save type of the selected shape in integer attribute
313 TopAbs_ShapeEnum aShapeType = aSubShape.ShapeType();
314 TDataStd_Integer::Set(aLab, (int)aShapeType);
316 TColStd_MapOfTransient allCurves;
317 if (aShapeType == TopAbs_VERTEX) { // compare positions
318 aVertexPos = BRep_Tool::Pnt(TopoDS::Vertex(aSubShape));
320 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
321 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
322 Standard_Real aFirst, aLast;
323 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
324 allCurves.Add(aCurve);
327 // iterate and store the result ids of sub-elements
328 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<boost::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
333 std::list<boost::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 boost::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);