1
0
mirror of https://git.dev.opencascade.org/repos/occt.git synced 2025-08-09 13:22:24 +03:00

0026216: New functionality. Converting the compound to assembly.

Added new functionality for converting the compound to assembly.
Added new drw command XExpand.
Changed DumpShape and DumpAssembly
Added test.

test changed
This commit is contained in:
ink
2015-08-19 12:37:28 +03:00
committed by bugmaster
parent d33a8cde96
commit 5f07d05d0e
10 changed files with 599 additions and 18 deletions

View File

@@ -25,6 +25,8 @@ XCAFDoc_DimTolTool.cxx
XCAFDoc_DimTolTool.hxx
XCAFDoc_DocumentTool.cxx
XCAFDoc_DocumentTool.hxx
XCAFDoc_Editor.cxx
XCAFDoc_Editor.hxx
XCAFDoc_GraphNode.cxx
XCAFDoc_GraphNode.hxx
XCAFDoc_GraphNodeSequence.hxx

View File

@@ -40,6 +40,7 @@ class XCAFDoc_DimTolTool;
class XCAFDoc_LayerTool;
class XCAFDoc_MaterialTool;
class XCAFDoc_GraphNode;
class XCAFDoc_Editor;
//! Definition of general structure of DECAF document
@@ -130,6 +131,7 @@ friend class XCAFDoc_DimTolTool;
friend class XCAFDoc_LayerTool;
friend class XCAFDoc_MaterialTool;
friend class XCAFDoc_GraphNode;
friend class XCAFDoc_Editor;
};

View File

@@ -0,0 +1,216 @@
// Created on: 2015-05-14
// Created by: data exchange team
// Copyright (c) 2000-2015 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#include <XCAFDoc_Editor.hxx>
#include <XCAFDoc.hxx>
#include <TDF_Label.hxx>
#include <TDF_LabelSequence.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDataStd_Name.hxx>
#include <TDataStd_UAttribute.hxx>
#include <TopLoc_Location.hxx>
#include <TopoDS_Shape.hxx>
#include <XCAFDoc_DocumentTool.hxx>
#include <XCAFDoc_ColorTool.hxx>
#include <XCAFDoc_LayerTool.hxx>
#include <XCAFDoc_ShapeTool.hxx>
#include <XCAFDoc_ShapeMapTool.hxx>
#include <XCAFDoc_Location.hxx>
#include <TNaming_NamedShape.hxx>
//=======================================================================
//function : Expand
//purpose : Convert Shape(compound) to assambly
//=======================================================================
Standard_Boolean XCAFDoc_Editor::Expand (const TDF_Label& Doc, const TDF_Label& Shape,
const Standard_Boolean recursively)
{
if(Doc.IsNull() || Shape.IsNull())
return Standard_False;
Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(Doc);
Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc);
Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc);
TopoDS_Shape aS = aShapeTool->GetShape(Shape);
if (!aS.IsNull() && aS.ShapeType() == TopAbs_COMPOUND && !aShapeTool->IsAssembly(Shape))
{
//convert compound to assembly(without attributes)
aShapeTool->Expand(Shape);
//move attrributes
TDF_ChildIterator anIter(Shape, Standard_True);
for(; anIter.More(); anIter.Next())
{
TDF_Label aChild = anIter.Value();
TDF_LabelSequence aLayers;
TDF_LabelSequence aColors;
Handle(TDataStd_Name) aName;
getParams(Doc, aChild, aColors, aLayers, aName);
//get part
TDF_Label aPart;
if(aShapeTool->GetReferredShape(aChild, aPart))
{
setParams(Doc, aPart, aColors, aLayers, aName);
//remove unnecessary links
TopoDS_Shape aShape = aShapeTool->GetShape(aChild);
if(!aShapeTool->GetShape(aPart.Father()).IsNull())
{
TopLoc_Location nulloc;
{
aPart.ForgetAttribute(XCAFDoc::ShapeRefGUID());
if(aShapeTool->GetShape(aPart.Father()).ShapeType() == TopAbs_COMPOUND)
{
aShapeTool->SetShape(aPart, aShape);
}
aPart.ForgetAttribute(XCAFDoc_ShapeMapTool::GetID());
aChild.ForgetAllAttributes(Standard_False);
}
}
aChild.ForgetAttribute(TNaming_NamedShape::GetID());
aChild.ForgetAttribute(XCAFDoc_ShapeMapTool::GetID());
}
}
//if assembly contains compound, expand it recursively(if flag recursively is true)
if(recursively)
{
anIter.Initialize(Shape);
for(; anIter.More(); anIter.Next())
{
TDF_Label aChild = anIter.Value();
TDF_Label aPart;
if(aShapeTool->GetReferredShape(aChild, aPart))
{
Expand(Doc, aPart, recursively);
}
}
}
return Standard_True;
}
return Standard_False;
}
//=======================================================================
//function : Expand
//purpose : Convert all compounds in Doc to assambly
//=======================================================================
Standard_Boolean XCAFDoc_Editor::Expand (const TDF_Label& Doc, const Standard_Boolean recursively)
{
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 (Expand(Doc, aLabels(i), recursively))
{
result = Standard_True;
}
}
return result;
}
//=======================================================================
//function : getParams
//purpose : Get colors layers and name
//=======================================================================
Standard_Boolean XCAFDoc_Editor::getParams (const TDF_Label& Doc, const TDF_Label& Label,
TDF_LabelSequence& Colors, TDF_LabelSequence& Layers,
Handle(TDataStd_Name)& Name)
{
if(Doc.IsNull() || Label.IsNull())
return Standard_False;
Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(Doc);
Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc);
//get colors
XCAFDoc_ColorType aTypeColor = XCAFDoc_ColorGen;
for(;;)
{
TDF_Label aColor;
aColorTool->GetColor(Label, aTypeColor, aColor);
Colors.Append(aColor);
if(aTypeColor == XCAFDoc_ColorCurv)
break;
aTypeColor = (XCAFDoc_ColorType)(aTypeColor + 1);
}
//get layers
aLayerTool->GetLayers(Label, Layers);
//get name
Label.FindAttribute(TDataStd_Name::GetID(), Name);
return Standard_True;
}
//=======================================================================
//function : setParams
//purpose : set colors layers and name
//=======================================================================
Standard_Boolean XCAFDoc_Editor::setParams (const TDF_Label& Doc, const TDF_Label& Label,
const TDF_LabelSequence& Colors, const TDF_LabelSequence& Layers,
const Handle(TDataStd_Name)& Name)
{
if(Doc.IsNull() || Label.IsNull())
return Standard_False;
Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(Doc);
Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc);
Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc);
//set layers
if(!Layers.IsEmpty())
{
for(Standard_Integer i = 1; i <= Layers.Length(); i++)
{
aLayerTool->SetLayer(Label, Layers.Value(i));
}
}
//set colors
if(!Colors.IsEmpty())
{
XCAFDoc_ColorType aTypeColor = XCAFDoc_ColorGen;
for(Standard_Integer i = 1; i <= Colors.Length(); i++)
{
if(!Colors.Value(i).IsNull())
aColorTool->SetColor(Label, Colors.Value(i), aTypeColor);
aTypeColor = (XCAFDoc_ColorType)(aTypeColor + 1);
}
}
//set name
if(!Name.IsNull())
{
if(Name->Get().Search("=>") < 0)
TDataStd_Name::Set(Label, Name->Get());
}
else
{
Standard_SStream Stream;
TopoDS_Shape aShape = aShapeTool->GetShape(Label);
TopAbs::Print(aShape.ShapeType(), Stream);
TCollection_AsciiString aName (Stream.str().c_str());
TDataStd_Name::Set(Label, TCollection_ExtendedString(aName));
}
return Standard_True;
}

View File

@@ -0,0 +1,73 @@
// Created on: 2015-05-14
// Created by: Ilya Novikov
// Copyright (c) 2000-2015 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#ifndef _XCAFDoc_Editor_HeaderFile
#define _XCAFDoc_Editor_HeaderFile
#include <Standard.hxx>
#include <Standard_DefineAlloc.hxx>
#include <Standard_Macro.hxx>
#include <Standard_Boolean.hxx>
#include <TDataStd_Name.hxx>
#include <TDF_Label.hxx>
#include <TDF_LabelSequence.hxx>
//! Tool for edit structure of document.
class XCAFDoc_Editor
{
public:
DEFINE_STANDARD_ALLOC
//! Convert Shape(compound) to assembly
Standard_EXPORT static Standard_Boolean Expand (const TDF_Label& Doc, const TDF_Label& Shape, const Standard_Boolean recursively = Standard_True) ;
//! Convert all compounds in Doc to assembly
Standard_EXPORT static Standard_Boolean Expand (const TDF_Label& Doc, const Standard_Boolean recursively = Standard_True) ;
protected:
private:
//! Get colors, layers and name from Label
Standard_EXPORT static Standard_Boolean getParams (const TDF_Label& Doc, const TDF_Label& Label, TDF_LabelSequence& Colors, TDF_LabelSequence& Layers, Handle(TDataStd_Name)& Name) ;
//! Set colors, layers and name from Label
Standard_EXPORT static Standard_Boolean setParams (const TDF_Label& Doc, const TDF_Label& Label, const TDF_LabelSequence& Colors, const TDF_LabelSequence& Layers, const Handle(TDataStd_Name)& Name) ;
};
#endif // _XCAFDoc_Editor_HeaderFile

View File

@@ -1221,20 +1221,44 @@ static void DumpAssembly(Standard_OStream& theDumpLog,
const Standard_Integer level,
const Standard_Boolean deep)
{
TopoDS_Shape S;
XCAFDoc_ShapeTool::GetShape(L, S);
if(S.IsNull())
return;
for (Standard_Integer i=0; i<level; i++)
theDumpLog<<"\t";
TCollection_AsciiString Entry;
TDF_Tool::Entry(L, Entry);
if(XCAFDoc_ShapeTool::IsAssembly(L))
{
theDumpLog<<"ASSEMBLY ";
}
else if (XCAFDoc_ShapeTool::IsSimpleShape(L))
{
if(L.Father().Father().Father().IsRoot())
theDumpLog<<"PART ";
}
else
{
theDumpLog<<"INSTANCE ";
}
TopAbs::Print(S.ShapeType(), theDumpLog);
theDumpLog<<"ASSEMBLY "<<Entry;
theDumpLog<<" "<<Entry;
if(XCAFDoc_ShapeTool::IsReference(L))
{
Handle(TDataStd_TreeNode) aRef;
L.FindAttribute(XCAFDoc::ShapeRefGUID(), aRef);
TDF_Tool::Entry(aRef->Father()->Label(), Entry);
theDumpLog<<" (refers to "<<Entry<<")";
}
Handle(TDataStd_Name) Name;
if (L.FindAttribute(TDataStd_Name::GetID(), Name))
theDumpLog<<" "<<Name->Get();
theDumpLog<<" \""<<Name->Get()<<"\" ";
if (deep) {
TopoDS_Shape S;
XCAFDoc_ShapeTool::GetShape(L, S);
theDumpLog<<"("<<*(void**)&S.TShape();
if (! S.Location().IsIdentity())
theDumpLog<<", "<< *(void**)&S.Location();
@@ -1243,18 +1267,12 @@ static void DumpAssembly(Standard_OStream& theDumpLog,
theDumpLog<<endl;
Handle(TDataStd_TreeNode) Node;
TDF_ChildIDIterator NodeIterator(L, XCAFDoc::ShapeRefGUID());
TDF_ChildIterator NodeIterator(L);
for (; NodeIterator.More(); NodeIterator.Next()) {
Node = Handle(TDataStd_TreeNode)::DownCast(NodeIterator.Value());
if (Node->HasFather()) {
if (Node->Father()->Label().HasChild())
DumpAssembly(theDumpLog, Node->Father()->Label(), level+1, deep);
else {
XCAFDoc_ShapeTool::DumpShape(theDumpLog, Node->Father()->Label(), level+1, deep);
theDumpLog<<endl;
}
}
DumpAssembly(theDumpLog, NodeIterator.Value(), level+1, deep);
}
if(level == 0)
theDumpLog<<endl;
}
//=======================================================================
@@ -1296,16 +1314,35 @@ void XCAFDoc_ShapeTool::DumpShape(Standard_OStream& theDumpLog, const TDF_Label&
for (Standard_Integer i=0; i<level; i++)
theDumpLog<<"\t";
if (S.ShapeType() == TopAbs_COMPOUND) theDumpLog<<"ASSEMBLY";
else TopAbs::Print(S.ShapeType(), theDumpLog);
if(XCAFDoc_ShapeTool::IsAssembly(L))
{
theDumpLog<<"ASSEMBLY ";
}
else if (XCAFDoc_ShapeTool::IsSimpleShape(L))
{
if(L.Father().Father().Father().IsRoot())
theDumpLog<<"PART ";
}
else
{
theDumpLog<<"INSTANCE ";
}
TopAbs::Print(S.ShapeType(), theDumpLog);
TCollection_AsciiString Entry;
TDF_Tool::Entry(L, Entry);
theDumpLog<<" "<<Entry;
if(XCAFDoc_ShapeTool::IsReference(L))
{
Handle(TDataStd_TreeNode) aRef;
L.FindAttribute(XCAFDoc::ShapeRefGUID(), aRef);
TDF_Tool::Entry(aRef->Father()->Label(), Entry);
theDumpLog<<" (refers to "<<Entry<<")";
}
//cout<<endl;
Handle(TDataStd_Name) Name;
if (L.FindAttribute(TDataStd_Name::GetID(),Name))
theDumpLog<<" "<<Name->Get();
theDumpLog<<" \""<<Name->Get()<<"\" ";
if (deep) {
theDumpLog<<"("<<*(void**)&S.TShape();
@@ -1796,3 +1833,107 @@ Standard_Boolean XCAFDoc_ShapeTool::FindSHUO (const TDF_LabelSequence& theLabels
}
return ( !theSHUOAttr.IsNull() );
}
//=======================================================================
//function : Expand
//purpose :
//=======================================================================
Standard_Boolean XCAFDoc_ShapeTool::Expand (const TDF_Label& Shape)
{
if(Shape.IsNull())
return Standard_False;
TopoDS_Shape aShape = GetShape(Shape);
if(!aShape.IsNull() && aShape.ShapeType() == TopAbs_COMPOUND && !IsAssembly(Shape))
{
//set assembly attribute
TDataStd_UAttribute::Set ( Shape, XCAFDoc::AssemblyGUID() );
TopoDS_Iterator anIter(aShape);
for(; anIter.More(); anIter.Next())
{
TopoDS_Shape aChildShape = anIter.Value();
TDF_Label aChild = FindShape(aChildShape, Standard_True);
TDF_TagSource aTag;
Handle(TDataStd_Name) anAttr;
//make part for child
TDF_Label aPart = aTag.NewChild(Label());
//make child (if color isn't set or if it is compound)
if(aChild.IsNull())
{
TopLoc_Location nulloc;
aChild = aTag.NewChild(Shape);
SetShape(aChild, aChildShape);
SetShape(aPart, aChildShape.Located(nulloc));
}
else
{
//get name
aChild.FindAttribute(TDataStd_Name::GetID(), anAttr);
TopLoc_Location nulloc;
SetShape(aPart, aChildShape.Located(nulloc));
}
//set name to part
if(!anAttr.IsNull())
{
TDataStd_Name::Set(aPart, anAttr->Get());
}
else
{
Standard_SStream Stream;
TopAbs::Print(aChildShape.ShapeType(), Stream);
TCollection_AsciiString aName (Stream.str().c_str());
TDataStd_Name::Set(aPart, TCollection_ExtendedString(aName));
}
MakeReference(aChild, aPart, aChildShape.Location());
makeSubShape(aPart, aChildShape);
}
return Standard_True;
}
return Standard_False;
}
//=======================================================================
//function : makeSubShape
//purpose :
//=======================================================================
void XCAFDoc_ShapeTool::makeSubShape (const TDF_Label& Part, const TopoDS_Shape& Shape)
{
TDF_TagSource aTag;
TopoDS_Iterator anIter(Shape);
for(; anIter.More(); anIter.Next())
{
TopoDS_Shape aChildShape = anIter.Value();
TDF_Label aChildLabel = FindShape(aChildShape,Standard_True);
if(!aChildLabel.IsNull())
{
//get name
Handle(TDataStd_Name) anAttr;
aChildLabel.FindAttribute(TDataStd_Name::GetID(), anAttr);
TopLoc_Location nulloc;
//make subshape
TDF_Label aSubLabel = aTag.NewChild(Part);
SetShape(aSubLabel, aChildShape.Located(nulloc));
//set name to sub shape
if(!anAttr.IsNull())
{
TDataStd_Name::Set(aSubLabel, anAttr->Get());
}
else
{
Standard_SStream Stream;
TopAbs::Print(aChildShape.ShapeType(), Stream);
TCollection_AsciiString aName (Stream.str().c_str());
TDataStd_Name::Set(aSubLabel, TCollection_ExtendedString(aName));
}
MakeReference(aChildLabel, aSubLabel, aChildShape.Location());
}
makeSubShape(Part, aChildShape);
}
}

View File

@@ -409,7 +409,12 @@ public:
//! from upper_usage componet to next_usage
//! Returns null attribute if no SHUO found
Standard_EXPORT static Standard_Boolean FindSHUO (const TDF_LabelSequence& Labels, Handle(XCAFDoc_GraphNode)& theSHUOAttr);
//! Convert Shape (compound) to assembly
Standard_EXPORT Standard_Boolean Expand (const TDF_Label& Shape) ;
//! Make subshape for Part from Shape
Standard_EXPORT void makeSubShape (const TDF_Label& Part, const TopoDS_Shape& Shape) ;