1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-19 13:40:49 +03:00

0026302: New functionality. Converting the assembly to compound.

Added new functionality for converting the assembly to compaund.
Added new drw command XCompact.
Changed DumpShape and DumpAssembly
Added test.
This commit is contained in:
ink
2015-08-19 12:13:17 +03:00
parent 8265e5deb8
commit 632cbd6169
6 changed files with 276 additions and 5 deletions

View File

@@ -31,6 +31,7 @@
#include <XCAFDoc_ShapeMapTool.hxx>
#include <XCAFDoc_Location.hxx>
#include <TNaming_NamedShape.hxx>
#include <BRep_Builder.hxx>
//=======================================================================
//function : Expand
@@ -213,4 +214,137 @@ Standard_Boolean XCAFDoc_Editor::setParams (const TDF_Label& Doc, const TDF_Labe
TDataStd_Name::Set(Label, TCollection_ExtendedString(aName));
}
return Standard_True;
}
}
//=======================================================================
//function : Compress
//purpose : Convert assembly to compound
//=======================================================================
Standard_Boolean XCAFDoc_Editor::Compact (const TDF_Label& Doc, const TDF_Label& Shape)
{
if(Doc.IsNull() || Shape.IsNull())
return Standard_False;
Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc);
TopoDS_Shape aS = aShapeTool->GetShape(Shape);
if (!aS.IsNull() && aS.ShapeType() == TopAbs_COMPOUND && aShapeTool->IsAssembly(Shape))
{
Shape.ForgetAttribute(XCAFDoc::AssemblyGUID());
TopoDS_Compound Comp;//new compound for Shape label
BRep_Builder B;
B.MakeCompound(Comp);
//convert assembly to compound
TDF_ChildIterator anIter(Shape);
for(; anIter.More(); anIter.Next())
{
TDF_Label aChild = anIter.Value();
TopoDS_Shape aChildShape = aShapeTool->GetShape(aChild);
TDF_Label aPart;
if(aShapeTool->GetReferredShape(aChild, aPart))
{
if (aChildShape.ShapeType() == TopAbs_COMPOUND && aShapeTool->IsAssembly(aPart))
{
//iterate next level if it needed
Compact(Doc, aPart);
}
//get location
Handle(XCAFDoc_Location) LocationAttribute;
aChild.FindAttribute(XCAFDoc_Location::GetID(), LocationAttribute);
TopLoc_Location aLoc;
if(!LocationAttribute.IsNull())
{
aLoc = LocationAttribute->Get();
}
if(aChildShape.ShapeType() != TopAbs_COMPOUND)
{
TDF_LabelSequence aLayers;
TDF_LabelSequence aColors;
Handle(TDataStd_Name) aName;
getParams(Doc, aPart, aColors, aLayers, aName);
aChild.ForgetAllAttributes(Standard_False);
//move shape
aShapeTool->SetShape(aChild, aChildShape.Located(aLoc), Standard_False);
aChildShape.Free(Standard_True);
B.Add(Comp, aChildShape.Located(aLoc));
setParams(Doc, aChild, aColors, aLayers, aName);
}
else
{
aChild.ForgetAllAttributes(Standard_False);
}
//move subshapes
TDF_LabelSequence aSub;
aShapeTool->GetSubShapes(aPart,aSub);
for(Standard_Integer i = 1; i <= aSub.Length(); i++)
{
TopoDS_Shape aShapeSub = aShapeTool->GetShape(aSub(i));
TDF_LabelSequence aLayers;
TDF_LabelSequence aColors;
Handle(TDataStd_Name) aName;
getParams(Doc, aSub(i), aColors, aLayers, aName);
aSub(i).ForgetAllAttributes(Standard_False);
TDF_TagSource aTag;
TDF_Label aSubC = aTag.NewChild(Shape);
TopLoc_Location loc = aLoc;
if(aChildShape.ShapeType() == TopAbs_COMPOUND)
{
loc = aShapeSub.Location().Multiplied(aLoc);
}
//set shape
aShapeTool->SetShape(aSubC, aShapeSub.Located(loc), Standard_False);
aSubC.ForgetAttribute(XCAFDoc_ShapeMapTool::GetID() );
if(aChildShape.ShapeType() == TopAbs_COMPOUND)
{
aShapeSub.Free(Standard_True);
B.Add(Comp, aShapeSub.Located(loc));
}
setParams(Doc, aSubC, aColors, aLayers, aName);
}
//remove part
aPart.ForgetAllAttributes();
}
}
aShapeTool->SetShape(Shape, Comp);
return Standard_True;
}
return Standard_False;
}
//=======================================================================
//function : Compress
//purpose : Convert all assambly in Doc to compound
//=======================================================================
Standard_Boolean XCAFDoc_Editor::Compact (const TDF_Label& Doc)
{
Standard_Boolean result = Standard_False;
TDF_LabelSequence aLabels;
Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc);
aShapeTool->GetFreeShapes(aLabels);
for(Standard_Integer i = 1; i <= aLabels.Length(); i++)
{
TopoDS_Shape aS = aShapeTool->GetShape(aLabels(i));
if (!aS.IsNull() && aS.ShapeType() == TopAbs_COMPOUND && aShapeTool->IsAssembly(aLabels(i)))
if (Compact(Doc, aLabels(i)))
{
result = Standard_True;
}
}
return result;
}

View File

@@ -41,6 +41,11 @@ public:
//! Convert all compounds in Doc to assembly
Standard_EXPORT static Standard_Boolean Expand (const TDF_Label& Doc, const Standard_Boolean recursively = Standard_True) ;
//! Convert Shape(assembly) to compound
Standard_EXPORT static Standard_Boolean Compact (const TDF_Label& Doc, const TDF_Label& Shape) ;
//! Convert all assembly in Doc to compounds
Standard_EXPORT static Standard_Boolean Compact (const TDF_Label& Doc) ;

View File

@@ -399,9 +399,10 @@ TDF_Label XCAFDoc_ShapeTool::NewShape() const
//purpose :
//=======================================================================
void XCAFDoc_ShapeTool::SetShape (const TDF_Label& L, const TopoDS_Shape& S)
void XCAFDoc_ShapeTool::SetShape (const TDF_Label& L, const TopoDS_Shape& S,
const Standard_Boolean theProtection)
{
if(IsReference(L) || !IsTopLevel(L) || /*IsAssembly(L) ||*/ !S.Location().IsIdentity())
if(IsReference(L) || ((!IsTopLevel(L) ||!S.Location().IsIdentity()) && theProtection))
return;
TDF_LabelSequence aSubShapes;
@@ -1936,4 +1937,4 @@ void XCAFDoc_ShapeTool::makeSubShape (const TDF_Label& Part, const TopoDS_Shape&
}
makeSubShape(Part, aChildShape);
}
}
}

View File

@@ -203,12 +203,13 @@ public:
Standard_EXPORT TDF_Label NewShape() const;
//! Sets representation (TopoDS_Shape) for top-level shape.
//! If you set theProtection = Standard_False you can set shape for subshapes(not top-level)
//! If S has location(location.IsIdentity() is false),
//! command will be skipped. Sub-shapes of S which is
//! subshape of old shape, will be stored ( all atributes will be stored).
//! If a sub-label of L is not a sub-shape of the new shape,
//! it will be removed.
Standard_EXPORT void SetShape (const TDF_Label& L, const TopoDS_Shape& S);
Standard_EXPORT void SetShape (const TDF_Label& L, const TopoDS_Shape& S, const Standard_Boolean theProtection = Standard_True);
//! Adds a new top-level (creates and returns a new label)
//! If makeAssembly is True, treats TopAbs_COMPOUND shapes

View File

@@ -519,6 +519,51 @@ static Standard_Integer Expand (Draw_Interpretor& di, Standard_Integer argc, con
return 0;
}
static Standard_Integer Compact (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
{
if (argc < 2) {
di<<"Use: "<<argv[0]<<" Doc or Doc label1 label2 ... or Doc shape1 shape2 ..."<<"\n";
return 1;
}
Handle(TDocStd_Document) Doc;
DDocStd::GetDocument(argv[1], Doc);
if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc->Main());
if (argc == 2)
{
if(!XCAFDoc_Editor::Compact(Doc->Main())){
di << "The shape is not assembly" << "\n";
return 1;
}
}
else
{
for (Standard_Integer i = 2; i < argc; i++)
{
TDF_Label aLabel;
TDF_Tool::Label(Doc->GetData(), argv[i], aLabel);
if(aLabel.IsNull()){
TopoDS_Shape aShape;
aShape = DBRep::Get(argv[i]);
aLabel = aShapeTool->FindShape(aShape);
}
if (!aLabel.IsNull()){
if(!XCAFDoc_Editor::Compact(Doc->Main(), aLabel)){
di << "The shape is not assembly" << "\n";
return 1;
}
}
else
{ di << argv[i] << " is not a shape" << "\n"; return 1; }
}
}
return 0;
}
void XDEDRAW_Common::InitCommands(Draw_Interpretor& di) {
static Standard_Boolean initactor = Standard_False;
@@ -539,4 +584,6 @@ void XDEDRAW_Common::InitCommands(Draw_Interpretor& di) {
di.Add("XExpand", "XExpand Doc recursively(0/1) or XExpand Doc recursively(0/1) label1 abel2 ..."
"or XExpand Doc recursively(0/1) shape1 shape2 ...",__FILE__, Expand, g);
di.Add("XCompact", "XCompact Doc or XCompact Doc label1 abel2 ..."
"or XCompact Doc shape1 shape2 ...",__FILE__, Compact, g);
}

83
tests/bugs/xde/bug26302 Normal file
View File

@@ -0,0 +1,83 @@
puts "========"
puts "OCC26302"
puts "========"
puts ""
#######################################################################
# Convert assembly to compound
#######################################################################
pload ALL
box b1 0 0 0 10 10 10
box b2 10 0 0 10 10 10
box b3 20 0 0 10 10 10
explode b1
explode b2
explode b3
explode b1_1
explode b2_1
explode b3_1
compound b1 b2 c1
compound c1 b3 c2
NewDocument D
XAddShape D c2 1
XSetColor D b1 1 0 0
XSetColor D b2 0 1 0
XSetColor D b3 0 0 1
XSetColor D b1_1_4 1 1 0
XSetColor D b2_1_4 0 1 1
XSetColor D b3_1_4 1 0 1
XSetColor D b1_1_3 1 0 1
XSetColor D b2_1_3 1 1 0
XSetColor D b3_1_3 0 1 1
XCompact D
if { [regexp "PART COMPOUND 0:1:1:1 \"ASSEMBLY\"" [Xdump D]] != 1 } {
puts "ERROR: Structure of document is wrong.1"
} else {
if { [regexp "BLUE" [XGetShapeColor D 0:1:1:1:2]] != 1 } {
puts "ERROR: Structure of document is wrong."
} else {
if { [regexp "RED" [XGetShapeColor D 0:1:1:1:3]] != 1 } {
puts "ERROR: Structure of document is wrong."
} else {
if { [regexp "GREEN" [XGetShapeColor D 0:1:1:1:4]] != 1 } {
puts "ERROR: Structure of document is wrong."
} else {
if { [regexp "YELLOW" [XGetShapeColor D 0:1:1:1:5]] != 1 } {
puts "ERROR: Structure of document is wrong."
} else {
if { [regexp "MAGENTA" [XGetShapeColor D 0:1:1:1:6]] != 1 } {
puts "ERROR: Structure of document is wrong."
} else {
if { [regexp "CYAN" [XGetShapeColor D 0:1:1:1:7]] != 1 } {
puts "ERROR: Structure of document is wrong."
} else {
if { [regexp "YELLOW" [XGetShapeColor D 0:1:1:1:8]] != 1 } {
puts "ERROR: Structure of document is wrong."
} else {
if { [regexp "MAGENTA" [XGetShapeColor D 0:1:1:1:9]] != 1 } {
puts "ERROR: Structure of document is wrong."
} else {
if { [regexp "CYAN" [XGetShapeColor D 0:1:1:1:10]] != 1 } {
puts "ERROR: Structure of document is wrong."
}
}
}
}
}
}
}
}
}
}