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 bool aResult = aSelector.Solve(aScope) == Standard_True;
108 owner()->data()->sendAttributeUpdated(this);
110 } else if (aContext->groupName() == ModelAPI_ResultConstruction::group()) {
111 // construction: identification by the results indexes, recompute faces and
112 // take the face that more close by the indexes
113 boost::shared_ptr<GeomAPI_PlanarEdges> aWirePtr =
114 boost::dynamic_pointer_cast<GeomAPI_PlanarEdges>(
115 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext)->shape());
116 if (aWirePtr && aWirePtr->hasPlane()) {
117 boost::shared_ptr<Model_Data> aData =
118 boost::dynamic_pointer_cast<Model_Data>(owner()->data());
119 TDF_Label aLab = aData->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 boost::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 = owner()->document()->feature(aContext);
133 CompositeFeaturePtr aComposite =
134 boost::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<boost::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 boost::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<boost::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
158 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRes;
159 for(aRes = aResults.cbegin(); aRes != aResults.cend(); aRes++) {
160 ResultConstructionPtr aConstr =
161 boost::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<boost::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<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
215 aFeature->results().cbegin();
216 for(;aResIter != aFeature->results().cend(); aResIter++) {
217 ResultConstructionPtr aRes =
218 boost::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<boost::shared_ptr<ModelAPI_Result> >::const_iterator aResIter =
241 aFeature->results().cbegin();
242 for(;aResIter != aFeature->results().cend(); aResIter++) {
243 ResultConstructionPtr aRes =
244 boost::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 boost::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 boost::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 = boost::dynamic_pointer_cast<ModelAPI_ResultBody>(myRef.value());
285 aContext = aBody->shape()->impl<TopoDS_Shape>();
287 ResultConstructionPtr aConstr = boost::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 boost::shared_ptr<GeomAPI_Shape>& theSubShape)
301 FeaturePtr aContextFeature = owner()->document()->feature(theContext);
302 CompositeFeaturePtr aComposite =
303 boost::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 boost::shared_ptr<Model_Data> aData = boost::dynamic_pointer_cast<Model_Data>(owner()->data());
308 TDF_Label aLab = aData->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);
328 const int aSubNum = aComposite->numberOfSubs();
329 for(int a = 0; a < aSubNum; a++) {
330 FeaturePtr aSub = aComposite->subFeature(a);
331 const std::list<boost::shared_ptr<ModelAPI_Result> >& aResults = aSub->results();
332 std::list<boost::shared_ptr<ModelAPI_Result> >::const_iterator aRes = aResults.cbegin();
333 // there may be many shapes (circle and center): register if at least one is in selection
334 for(; aRes != aResults.cend(); aRes++) {
335 ResultConstructionPtr aConstr =
336 boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(*aRes);
337 if (!aConstr->shape()) {
340 if (aShapeType == TopAbs_VERTEX) {
341 if (aConstr->shape()->isVertex()) { // compare vertices positions
342 const TopoDS_Shape& aVertex = aConstr->shape()->impl<TopoDS_Shape>();
343 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVertex));
344 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
345 aRefs->Add(aComposite->subFeatureId(a));
347 } else { // get first or last vertex of the edge: last is stored with negative sign
348 const TopoDS_Shape& anEdge = aConstr->shape()->impl<TopoDS_Shape>();
349 int aDelta = kSTART_VERTEX_DELTA;
350 for(TopExp_Explorer aVExp(anEdge, TopAbs_VERTEX); aVExp.More(); aVExp.Next()) {
351 gp_Pnt aPnt = BRep_Tool::Pnt(TopoDS::Vertex(aVExp.Current()));
352 if (aPnt.IsEqual(aVertexPos, Precision::Confusion())) {
353 aRefs->Add(aComposite->subFeatureId(a));
354 aRefs->Add(aDelta + aComposite->subFeatureId(a));
357 aDelta += kSTART_VERTEX_DELTA;
361 if (aConstr->shape()->isEdge()) {
362 const TopoDS_Shape& aResShape = aConstr->shape()->impl<TopoDS_Shape>();
363 TopoDS_Edge anEdge = TopoDS::Edge(aResShape);
364 if (!anEdge.IsNull()) {
365 Standard_Real aFirst, aLast;
366 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
367 if (allCurves.Contains(aCurve)) {
368 int anID = aComposite->subFeatureId(a);
370 // add edges to sub-label to support naming for edges selection
371 for(TopExp_Explorer anEdgeExp(aSubShape, TopAbs_EDGE); anEdgeExp.More(); anEdgeExp.Next()) {
372 TopoDS_Edge anEdge = TopoDS::Edge(anEdgeExp.Current());
373 Standard_Real aFirst, aLast;
374 Handle(Geom_Curve) aFaceCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
375 if (aFaceCurve == aCurve) {
376 TNaming_Builder anEdgeBuilder(selectionLabel().FindChild(anID));
377 anEdgeBuilder.Generated(anEdge);
386 // store the selected as primitive
387 TNaming_Builder aBuilder(selectionLabel());
388 aBuilder.Generated(aSubShape);
391 TDF_Label Model_AttributeSelection::selectionLabel()
393 return myRef.myRef->Label().FindChild(1);