diff --git a/src/XCAFDoc/XCAFDoc_ShapeTool.cxx b/src/XCAFDoc/XCAFDoc_ShapeTool.cxx index 85e3ecc0e3..67b7691d57 100644 --- a/src/XCAFDoc/XCAFDoc_ShapeTool.cxx +++ b/src/XCAFDoc/XCAFDoc_ShapeTool.cxx @@ -353,6 +353,47 @@ TopoDS_Shape XCAFDoc_ShapeTool::GetShape(const TDF_Label& L) return aShape; } +//======================================================================= +//function : GetShapes +//purpose : +//======================================================================= +TopoDS_Shape XCAFDoc_ShapeTool::GetOneShape(const TDF_LabelSequence& theLabels) +{ + TopoDS_Shape aShape; + if (theLabels.Length() == 1) + { + return GetShape(theLabels.Value(1)); + } + TopoDS_Compound aCompound; + BRep_Builder aBuilder; + aBuilder.MakeCompound(aCompound); + for (TDF_LabelSequence::Iterator anIt(theLabels); anIt.More(); anIt.Next()) + { + TopoDS_Shape aFreeShape; + if (!GetShape(anIt.Value(), aFreeShape)) + { + continue; + } + aBuilder.Add(aCompound, aFreeShape); + } + if (aCompound.NbChildren() > 0) + { + aShape = aCompound; + } + return aShape; +} + +//======================================================================= +//function : GetOneShape +//purpose : +//======================================================================= +TopoDS_Shape XCAFDoc_ShapeTool::GetOneShape() const +{ + TDF_LabelSequence aLabels; + GetFreeShapes(aLabels); + return GetOneShape(aLabels); +} + //======================================================================= //function : NewShape //purpose : diff --git a/src/XCAFDoc/XCAFDoc_ShapeTool.hxx b/src/XCAFDoc/XCAFDoc_ShapeTool.hxx index 100b0f6b10..752688afa6 100644 --- a/src/XCAFDoc/XCAFDoc_ShapeTool.hxx +++ b/src/XCAFDoc/XCAFDoc_ShapeTool.hxx @@ -196,6 +196,15 @@ public: //! For component, returns new shape with correct location //! Returns Null shape if label does not contain shape Standard_EXPORT static TopoDS_Shape GetShape (const TDF_Label& L); + + //! Gets shape from a sequence of shape's labels + //! @param[in] theLabels a sequence of labels to get shapes from + //! @return original shape in case of one label and a compound of shapes in case of more + Standard_EXPORT static TopoDS_Shape GetOneShape(const TDF_LabelSequence& theLabels); + + //! Gets shape from a sequence of all top-level shapes which are free + //! @return original shape in case of one label and a compound of shapes in case of more + Standard_EXPORT TopoDS_Shape GetOneShape() const; //! Creates new (empty) top-level shape. //! Initially it holds empty TopoDS_Compound diff --git a/src/XDEDRAW/XDEDRAW_Shapes.cxx b/src/XDEDRAW/XDEDRAW_Shapes.cxx index dc0edf0b5e..eeb7a8bb3e 100644 --- a/src/XDEDRAW/XDEDRAW_Shapes.cxx +++ b/src/XDEDRAW/XDEDRAW_Shapes.cxx @@ -509,40 +509,37 @@ static Standard_Integer getFreeShapes (Draw_Interpretor& di, Standard_Integer ar return 0; } -static Standard_Integer getOneShape (Draw_Interpretor& di, Standard_Integer argc, const char** argv) +//======================================================================= +//function : getOneShape +//purpose : +//======================================================================= +static Standard_Integer getOneShape (Draw_Interpretor& theDI, + Standard_Integer theNbArgs, + const char** theArgVec) { - if (argc!=3) { - di<<"Use: "<Main()); - STool->GetFreeShapes(Labels); - if ( Labels.Length() <=0 ) { - di << "Document " << argv[2] << " contain no shapes\n"; - return 0; + Handle(XCAFDoc_ShapeTool) aSTool = XCAFDoc_DocumentTool::ShapeTool(aDoc->Main()); + TopoDS_Shape aShape = aSTool->GetOneShape(); + if (aShape.IsNull()) + { + theDI << "Error: Document " << theArgVec[2] << " contain no shapes\n"; + return 1; } - - if ( Labels.Length() ==1 ) { - TopoDS_Shape S = STool->GetShape ( Labels.Value(1) ); - DBRep::Set ( argv[1], S ); - } - else { - TopoDS_Compound C; - BRep_Builder B; - B.MakeCompound ( C ); - for ( Standard_Integer i = 1; i<= Labels.Length(); i++) { - TopoDS_Shape S = STool->GetShape ( Labels.Value(i) ); - B.Add ( C, S ); - } - DBRep::Set ( argv[1], C ); - } - di << argv[1]; + DBRep::Set (theArgVec[1], aShape); + theDI << theArgVec[1]; return 0; } diff --git a/tests/bugs/xde/bug30828 b/tests/bugs/xde/bug30828 new file mode 100644 index 0000000000..ea34f27561 --- /dev/null +++ b/tests/bugs/xde/bug30828 @@ -0,0 +1,16 @@ +puts "=======" +puts "0030828: Data Exchange - The commands getting shapes from XCAF document should be available in C++" +puts "=======" + +pload OCAF +XNewDoc D +box b1 10 10 10 +XAddShape D b1 1 +XGetOneShape b D +checknbshapes b -shape 34 +box b2 10 10 10 +ttranslate b2 20 0 0 +XAddShape D b2 1 +XGetOneShape c D +checknbshapes c -shape 69 -compound 1 +Close D -silent