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

Integration of OCCT 6.5.0 from SVN

This commit is contained in:
bugmaster
2011-03-16 07:30:28 +00:00
committed by bugmaster
parent 4903637061
commit 7fd59977df
16375 changed files with 3882564 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
-- File: IGESCAFControl.cdl
-- Created: Wed Aug 16 13:39:19 2000
-- Author: Andrey BETENEV
-- <abv@hotdox.nnov.matra-dtv.fr>
---Copyright: Matra Datavision 2000
package IGESCAFControl
---Purpose: Provides high-level API to translate IGES file
-- to and from DECAF document
uses
Quantity,
TCollection,
TopoDS,
TopTools,
TDocStd,
XCAFDoc,
XCAFPrs,
XSControl,
IGESControl
is
class Reader;
---Purpose: Provides a tool for reading IGES file
class Writer;
---Purpose: Provides a tool for writing IGES file
DecodeColor (col: Integer) returns Color from Quantity;
---Purpose: Converts IGES color index to CASCADE color
EncodeColor (col: Color from Quantity) returns Integer;
---Purpose: Tries to Convert CASCADE color to IGES color index
-- If no corresponding color defined in IGES, returns 0
end IGESCAFControl;

View File

@@ -0,0 +1,54 @@
// File: IGESCAFControl.cxx
// Created: Wed Aug 16 18:46:01 2000
// Author: Andrey BETENEV
// <abv@doomox.nnov.matra-dtv.fr>
#include <IGESCAFControl.ixx>
//=======================================================================
//function : DecodeColor
//purpose :
//=======================================================================
Quantity_Color IGESCAFControl::DecodeColor (const Standard_Integer color)
{
switch ( color ) {
case 1: return Quantity_Color ( Quantity_NOC_BLACK );
case 2: return Quantity_Color ( Quantity_NOC_RED );
case 3: return Quantity_Color ( Quantity_NOC_GREEN );
case 4: return Quantity_Color ( Quantity_NOC_BLUE1 );
case 5: return Quantity_Color ( Quantity_NOC_YELLOW );
case 6: return Quantity_Color ( Quantity_NOC_MAGENTA1 );
case 7: return Quantity_Color ( Quantity_NOC_CYAN1 );
case 8:
default:return Quantity_Color ( Quantity_NOC_WHITE );
}
}
//=======================================================================
//function : DecodeColor
//purpose :
//=======================================================================
Standard_Integer IGESCAFControl::EncodeColor (const Quantity_Color &col)
{
Standard_Integer code = 0;
if ( Abs ( col.Red() - 1. ) <= col.Epsilon() ) code |= 0x001;
else if ( Abs ( col.Red() ) > col.Epsilon() ) return 0;
if ( Abs ( col.Green() - 1. ) <= col.Epsilon() ) code |= 0x010;
else if ( Abs ( col.Green() ) > col.Epsilon() ) return 0;
if ( Abs ( col.Blue() - 1. ) <= col.Epsilon() ) code |= 0x100;
else if ( Abs ( col.Blue() ) > col.Epsilon() ) return 0;
switch ( code ) {
case 0x000: return 1;
case 0x001: return 2;
case 0x010: return 3;
case 0x100: return 4;
case 0x011: return 5;
case 0x101: return 6;
case 0x110: return 7;
case 0x111:
default : return 8;
}
}

View File

@@ -0,0 +1,99 @@
-- File: IGESCAFControl_Reader.cdl
-- Created: Tue Aug 15 12:01:29 2000
-- Author: Andrey BETENEV
-- <abv@doomox.nnov.matra-dtv.fr>
---Copyright: Matra Datavision 2000
class Reader from IGESCAFControl inherits Reader from IGESControl
---Purpose: Provides a tool to read IGES file and put it into
-- DECAF document. Besides transfer of shapes (including
-- assemblies) provided by IGESControl, supports also
-- colors and part names
-- IGESCAFControl_Reader reader; Methods for translation of an IGES file:
-- reader.ReadFile("filename");
-- reader.Transfer(Document); or
-- reader.Perform("filename",doc);
-- Methods for managing reading attributes.
-- Colors
-- reader.SetColorMode(colormode);
-- Standard_Boolean colormode = reader.GetColorMode();
-- Layers
-- reader.SetLayerMode(layermode);
-- Standard_Boolean layermode = reader.GetLayerMode();
-- Names
-- reader.SetNameMode(namemode);
-- Standard_Boolean namemode = reader.GetNameMode();
uses
AsciiString from TCollection,
WorkSession from XSControl,
Document from TDocStd
is
Create returns Reader;
---Purpose: Creates a reader with an empty
-- IGES model and sets ColorMode, LayerMode and NameMode to Standard_True.
Create (WS : mutable WorkSession from XSControl;
scratch : Boolean = Standard_True) returns Reader;
---Purpose: Creates a reader tool and attaches it to an already existing Session
-- Clears the session if it was not yet set for IGES
Transfer (me: in out; doc: in out Document from TDocStd)
returns Boolean;
---Purpose: Translates currently loaded IGES file into the document
-- Returns True if succeeded, and False in case of fail
Perform (me: in out; filename: AsciiString from TCollection;
doc: in out Document from TDocStd) returns Boolean;
Perform (me: in out; filename: CString; doc: in out Document from TDocStd)
returns Boolean;
---Purpose: Translate IGES file given by filename into the document
-- Return True if succeeded, and False in case of fail
---Scope: protected methods
ReadColors (me; doc: in out Document from TDocStd)
returns Boolean is protected;
---Purpose: Reads colors of IGES entities and sets
-- corresponding color assignments in the DECAF document
ReadNames (me; doc: in out Document from TDocStd)
returns Boolean is protected;
---Purpose: Reads Names of IGES entities and sets
-- corresponding name to label with shape in the DECAF document
ReadLayers (me; doc: in out Document from TDocStd)
returns Boolean is protected;
---Purpose: Reads layers of parts defined in the IGES model and
-- set reference between shape and layers in the DECAF document
--- Work with fileds for different mode of reading IGES file.
SetColorMode(me: in out; colormode: Boolean from Standard);
---Purpose: Set ColorMode for indicate read Colors or not.
GetColorMode(me) returns Boolean;
SetNameMode(me: in out; namemode: Boolean from Standard);
---Purpose: Set NameMode for indicate read Name or not.
GetNameMode(me) returns Boolean;
SetLayerMode(me: in out; layermode: Boolean from Standard);
---Purpose: Set LayerMode for indicate read Layers or not.
GetLayerMode(me) returns Boolean;
fields
myColorMode: Boolean;
myNameMode: Boolean;
myLayerMode: Boolean;
end Reader;

View File

@@ -0,0 +1,533 @@
// File: IGESCAFControl_Reader.cxx
// Created: Wed Aug 16 18:01:56 2000
// Author: Andrey BETENEV
// <abv@doomox.nnov.matra-dtv.fr>
#include <TDF_Label.hxx>
#include <IGESCAFControl_Reader.ixx>
#include <TopoDS_Shape.hxx>
#include <XCAFDoc_ShapeTool.hxx>
#include <XSControl_TransferReader.hxx>
#include <XCAFDoc_ColorTool.hxx>
#include <IGESData_IGESEntity.hxx>
#include <Transfer_Binder.hxx>
#include <TransferBRep.hxx>
#include <Quantity_Color.hxx>
#include <IGESCAFControl.hxx>
#include <IGESGraph_Color.hxx>
#include <Interface_InterfaceModel.hxx>
#include <Transfer_TransientProcess.hxx>
#include <XCAFDoc_DocumentTool.hxx>
#include <TopoDS_Iterator.hxx>
#include <TCollection_ExtendedString.hxx>
#include <TDataStd_Name.hxx>
#include <XCAFDoc_LayerTool.hxx>
#include <IGESData_LevelListEntity.hxx>
#include <TCollection_HAsciiString.hxx>
#include <XCAFDoc_ShapeMapTool.hxx>
//=======================================================================
//function : IGESCAFControl_Reader
//purpose :
//=======================================================================
IGESCAFControl_Reader::IGESCAFControl_Reader () :
myColorMode( Standard_True ),
myNameMode ( Standard_True ),
myLayerMode( Standard_True )
{
}
//=======================================================================
//function : IGESCAFControl_Reader
//purpose :
//=======================================================================
IGESCAFControl_Reader::IGESCAFControl_Reader (const Handle(XSControl_WorkSession)& WS,
const Standard_Boolean scratch)
{
SetWS (WS,scratch);
myColorMode = Standard_True;
myNameMode = Standard_True;
myLayerMode = Standard_True;
}
//=======================================================================
//function : Transfer
//purpose : basic working method
//=======================================================================
Standard_Boolean IGESCAFControl_Reader::Transfer (Handle(TDocStd_Document) &doc)
{
// read all shapes
Standard_Integer num;// = NbRootsForTransfer();
//if ( num <=0 ) return Standard_False;
//for ( Standard_Integer i=1; i <= num; i++ ) {
// TransferOneRoot ( i );
//}
TransferRoots(); // replaces the above
num = NbShapes();
if ( num <=0 ) return Standard_False;
// and insert them to the document
Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
if(STool.IsNull()) return Standard_False;
Standard_Integer i;
for(i=1; i<=num; i++) {
TopoDS_Shape sh = Shape ( i );
// ---- HERE -- to add check [ assembly / hybrid model ]
STool->AddShape ( sh, Standard_False );
}
// added by skl 13.10.2003
Handle(Interface_InterfaceModel) Model = WS()->Model();
//WS()->TransferReader()->SetTransientProcess(TransientProcess());
Handle(XSControl_TransferReader) TR = WS()->TransferReader();
Handle(Transfer_TransientProcess) TP = TR->TransientProcess();
Standard_Boolean IsCTool = Standard_True;
Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool(doc->Main());
if(CTool.IsNull()) IsCTool = Standard_False;
Standard_Boolean IsLTool = Standard_True;
Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool(doc->Main());
if(LTool.IsNull()) IsLTool = Standard_False;
Standard_Integer nb = Model->NbEntities();
for(i=1; i<=nb; i++) {
Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
if ( ent.IsNull() ) continue;
Handle(Transfer_Binder) binder = TP->Find ( ent );
if ( binder.IsNull() ) continue;
TopoDS_Shape S = TransferBRep::ShapeResult (binder);
if ( S.IsNull() ) continue;
Standard_Boolean IsColor = Standard_False;
Quantity_Color col;
if( GetColorMode() && IsCTool ) {
// read colors
if(ent->DefColor()==IGESData_DefValue ||
ent->DefColor()==IGESData_DefReference) {
// color is assigned
// decode color and set to document
IsColor = Standard_True;
if ( ent->DefColor() == IGESData_DefValue ) {
col = IGESCAFControl::DecodeColor ( ent->RankColor() );
}
else {
Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
if ( color.IsNull() ) {
#ifdef DEB
cout << "Error: Unrecognized type of color definition" << endl;
#endif
IsColor = Standard_False;
}
else {
Standard_Real r, g, b;
color->RGBIntensity ( r, g, b );
col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
}
}
}
}
TDF_Label L;
Standard_Boolean IsFound;
if(IsColor) {
CTool->AddColor(col);
IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_True);
}
else {
IsFound = STool->SearchUsingMap(S,L,Standard_False,Standard_False);
}
if(!IsFound) {
if(IsColor) {
for (TopoDS_Iterator it(S); it.More(); it.Next()) {
if(STool->SearchUsingMap(it.Value(),L,Standard_False,Standard_True)) {
CTool->SetColor(L,col,XCAFDoc_ColorGen);
if( GetLayerMode() && IsLTool ) {
// read layers
// set a layers to the document
IGESData_DefList aDeflist = ent->DefLevel();
switch (aDeflist) {
case IGESData_DefOne : {
TCollection_ExtendedString aLayerName ( ent->Level() );
LTool->SetLayer( L, aLayerName );
break;
}
case IGESData_DefSeveral : {
Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
Standard_Integer layerNb = aLevelList->NbLevelNumbers();
for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
LTool->SetLayer( L, aLayerName );
}
break;
}
default : break;
}
}
}
}
}
}
else {
if(IsColor) {
CTool->SetColor(L,col,XCAFDoc_ColorGen);
}
if(GetNameMode()) {
// read names
if(ent->HasName()) {
TCollection_AsciiString string = ent->NameValue()->String();
string.LeftAdjust();
string.RightAdjust();
TCollection_ExtendedString str(string);
TDataStd_Name::Set(L,str);
}
}
if( GetLayerMode() && IsLTool ) {
// read layers
// set a layers to the document
IGESData_DefList aDeflist = ent->DefLevel();
switch (aDeflist) {
case IGESData_DefOne : {
TCollection_ExtendedString aLayerName ( ent->Level() );
LTool->SetLayer( L, aLayerName );
break;
}
case IGESData_DefSeveral : {
Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
Standard_Integer layerNb = aLevelList->NbLevelNumbers();
for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
LTool->SetLayer( L, aLayerName );
}
break;
}
default : break;
}
}
}
}
CTool->ReverseChainsOfTreeNodes();
// end added by skl 13.10.2003
// read colors
// if ( GetColorMode() )
// ReadColors ( doc );
// read names
// if ( GetNameMode() )
// ReadNames ( doc );
// read layers
// if ( GetLayerMode() )
// ReadLayers ( doc );
return Standard_True;
}
//=======================================================================
//function : Perform
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Reader::Perform (const Standard_CString filename,
Handle(TDocStd_Document) &doc)
{
if ( ReadFile ( filename ) != IFSelect_RetDone ) return Standard_False;
return Transfer ( doc );
}
//=======================================================================
//function : Perform
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Reader::Perform (const TCollection_AsciiString &filename,
Handle(TDocStd_Document) &doc)
{
if ( ReadFile ( filename.ToCString() ) != IFSelect_RetDone ) return Standard_False;
return Transfer ( doc );
}
//=======================================================================
//function : ReadColors
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Reader::ReadColors (Handle(TDocStd_Document)& Doc) const
{
Handle(Interface_InterfaceModel) Model = WS()->Model();
//WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
Handle(XSControl_TransferReader) TR = WS()->TransferReader();
Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
Handle(XCAFDoc_ColorTool) CTool = XCAFDoc_DocumentTool::ColorTool( Doc->Main() );
/*
Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
if ( STool.IsNull() ) return Standard_False;
Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
SMTool->ComputeMaps();
Standard_Integer nb = Model->NbEntities();
for (Standard_Integer i = 1; i <= nb; i ++) {
Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
if ( ent.IsNull() ) continue;
if ( ent->DefColor() != IGESData_DefValue &&
ent->DefColor() != IGESData_DefReference ) continue; // no color assigned
// find tartet shape
Handle(Transfer_Binder) binder = TP->Find ( ent );
if ( binder.IsNull() ) continue;
TopoDS_Shape S = TransferBRep::ShapeResult (binder);
if ( S.IsNull() ) continue;
// decode color and set to document
Quantity_Color col;
if ( ent->DefColor() == IGESData_DefValue ) {
col = IGESCAFControl::DecodeColor ( ent->RankColor() );
}
else {
Handle(IGESGraph_Color) color = Handle(IGESGraph_Color)::DownCast ( ent->Color() );
if ( color.IsNull() ) {
cout << "Error: Unrecognized type of color definition" << endl;
continue;
}
Standard_Real r, g, b;
color->RGBIntensity ( r, g, b );
col.SetValues ( 0.01*r, 0.01*g, 0.01*b, Quantity_TOC_RGB );
}
TDF_Label L;
cout<<"i="<<i<<endl;
if(SMTool->Search(S,L)) {
cout<<" find Instance"<<endl;
CTool->SetColor(L, col, XCAFDoc_ColorGen);
}
if(L.IsNull()) {
cout<<"L1 is Null"<<endl;
if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
cout<<" add new label1 :"<<L<<endl;
CTool->SetColor(L, col, XCAFDoc_ColorGen);
}
}
if(L.IsNull()) {
//else {
cout<<" try to find splitting"<<endl;
// may be S is compound of shapes resulting from splitting
for (TopoDS_Iterator it(S); it.More(); it.Next()) {
//TDF_Label L1;
if(!SMTool->Search(it.Value(),L)) continue; //break-?
cout<<" find splitting"<<endl;
CTool->SetColor(L, col, XCAFDoc_ColorGen);
}
if(L.IsNull()) {
for (TopoDS_Iterator it(S); it.More(); it.Next()) {
if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
cout<<" add new label2 :"<<L<<endl;
CTool->SetColor(L, col, XCAFDoc_ColorGen);
}
}
}
}
// cout<<"L.Dump():"<<L<<endl;
// if(L.IsNull()) {
// cout<<"L2 is Null"<<endl;
// if(STool->Search(S,L,Standard_False,Standard_False,Standard_True)) {
// cout<<" add new label2 :"<<L<<endl;
// CTool->SetColor(L, col, XCAFDoc_ColorGen);
// }
// }
}
*/
return Standard_True;
}
//=======================================================================
//function : ReadNames
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Reader::ReadNames (Handle(TDocStd_Document)& Doc) const
{
Handle(Interface_InterfaceModel) Model = WS()->Model();
//WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
Handle(XSControl_TransferReader) TR = WS()->TransferReader();
Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
/*
Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
if ( STool.IsNull() ) return Standard_False;
Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
SMTool->ComputeMaps();
Standard_Integer nb = Model->NbEntities();
for (Standard_Integer i = 1; i <= nb; i ++) {
Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
if ( ent.IsNull() || ! ent->HasName() ) continue; //not only Entity Label (f.18) but Name Property also
// find target shape
Handle(Transfer_Binder) binder = TP->Find ( ent );
if ( binder.IsNull() ) continue;
TopoDS_Shape S = TransferBRep::ShapeResult (binder);
if ( S.IsNull() ) continue;
TDF_Label L;
//if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
if ( ! SMTool->Search ( S, L, Standard_True, Standard_True) ) continue;
// set a name to the document
TCollection_AsciiString string = ent->NameValue()->String();
string.LeftAdjust();
string.RightAdjust();
TCollection_ExtendedString str ( string );
TDataStd_Name::Set ( L, str );
}
*/
return Standard_True;
}
//=======================================================================
//function : ReadLayers
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Reader::ReadLayers (Handle(TDocStd_Document)& Doc) const
{
Handle(Interface_InterfaceModel) Model = WS()->Model();
//WS()->TransferReader()->SetTransientProcess(TransientProcess()); // !!!!!!!!!
Handle(XSControl_TransferReader) TR = WS()->TransferReader();
Handle(Transfer_TransientProcess) TP = /*TransientProcess();*/TR->TransientProcess();
/*
Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
if ( STool.IsNull() ) return Standard_False;
Handle(XCAFDoc_ShapeMapTool) SMTool = XCAFDoc_ShapeMapTool::Set(STool->Label());
SMTool->ComputeMaps();
Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
if ( LTool.IsNull() ) return Standard_False;
Standard_Integer nb = Model->NbEntities();
for (Standard_Integer i = 1; i <= nb; i ++) {
Handle(IGESData_IGESEntity) ent = Handle(IGESData_IGESEntity)::DownCast ( Model->Value(i) );
if ( ent.IsNull() ) continue;
// find target shape
Handle(Transfer_Binder) binder = TP->Find ( ent );
if ( binder.IsNull() ) continue;
TopoDS_Shape S = TransferBRep::ShapeResult (binder);
if ( S.IsNull() ) continue;
TDF_Label L;
//if ( ! STool->Search ( S, L, Standard_True, Standard_True, Standard_False ) ) continue;
if( !SMTool->Search(S, L, Standard_True, Standard_True) ) continue;
// set a layers to the document
IGESData_DefList aDeflist = ent->DefLevel();
switch (aDeflist) {
case IGESData_DefOne : {
TCollection_ExtendedString aLayerName ( ent->Level() );
LTool->SetLayer( L, aLayerName );
#ifdef DEB
// cout << "Added layer " << aLayerName << endl;
#endif
break;
}
case IGESData_DefSeveral : {
Handle(IGESData_LevelListEntity) aLevelList = ent->LevelList();
Standard_Integer layerNb = aLevelList->NbLevelNumbers();
for ( Standard_Integer ilev = 1; ilev <= layerNb; ilev++ ) {
TCollection_ExtendedString aLayerName ( aLevelList->LevelNumber(ilev) );
LTool->SetLayer( L, aLayerName );
#ifdef DEB
// cout << "Added layer " << aLayerName << endl;
#endif
}
break;
}
default : break;
}
}
*/
return Standard_True;
}
//=======================================================================
//function : SetColorMode
//purpose :
//=======================================================================
void IGESCAFControl_Reader::SetColorMode (const Standard_Boolean colormode)
{
myColorMode = colormode;
}
//=======================================================================
//function : GetColorMode
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Reader::GetColorMode () const
{
return myColorMode;
}
//=======================================================================
//function : SetNameMode
//purpose :
//=======================================================================
void IGESCAFControl_Reader::SetNameMode (const Standard_Boolean namemode)
{
myNameMode = namemode;
}
//=======================================================================
//function : GetNameMode
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Reader::GetNameMode () const
{
return myNameMode;
}
//=======================================================================
//function : SetLayerMode
//purpose :
//=======================================================================
void IGESCAFControl_Reader::SetLayerMode (const Standard_Boolean layermode)
{
myLayerMode = layermode;
}
//=======================================================================
//function : GetLayerMode
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Reader::GetLayerMode () const
{
return myLayerMode;
}

View File

@@ -0,0 +1,110 @@
-- File: IGESCAFControl_Writer.cdl
-- Created: Thu Aug 17 09:52:56 2000
-- Author: Andrey BETENEV
-- <abv@doomox.nnov.matra-dtv.fr>
---Copyright: Matra Datavision 2000
class Writer from IGESCAFControl inherits Writer from IGESControl
---Purpose: Provides a tool to write DECAF document to the
-- IGES file. Besides transfer of shapes (including
-- assemblies) provided by IGESControl, supports also
-- colors and part names
-- IGESCAFControl_Writer writer();
-- Methods for writing IGES file:
-- writer.Transfer (Document);
-- writer.Write("filename") or writer.Write(OStream) or
-- writer.Perform(Document,"filename");
-- Methods for managing the writing of attributes.
-- Colors
-- writer.SetColorMode(colormode);
-- Standard_Boolean colormode = writer.GetColorMode();
-- Layers
-- writer.SetLayerMode(layermode);
-- Standard_Boolean layermode = writer.GetLayerMode();
-- Names
-- writer.SetNameMode(namemode);
-- Standard_Boolean namemode = writer.GetNameMode();
uses
AsciiString from TCollection,
Shape from TopoDS,
MapOfShape from TopTools,
WorkSession from XSControl,
Document from TDocStd,
Style from XCAFPrs,
DataMapOfShapeStyle from XCAFPrs,
DataMapOfStyleTransient from XCAFPrs
is
Create returns Writer;
---Purpose: Creates a writer with an empty
-- IGES model and sets ColorMode, LayerMode and NameMode to Standard_True.
Create (WS : mutable WorkSession from XSControl;
scratch : Boolean = Standard_True) returns Writer;
---Purpose: Creates a reader tool and attaches it to an already existing Session
-- Clears the session if it was not yet set for IGES
Transfer (me : in out; doc : Document from TDocStd)
returns Boolean;
---Purpose: Transfers a document to a IGES model
-- Returns True if translation is OK
Perform (me : in out; doc : Document from TDocStd;
filename: AsciiString from TCollection)
returns Boolean;
Perform (me : in out; doc : Document from TDocStd; filename: CString)
returns Boolean;
---Purpose : Transfers a document and writes it to a IGES file
-- Returns True if translation is OK
---Scope: Internal methods
WriteAttributes (me: in out; doc: Document from TDocStd)
returns Boolean is protected;
---Purpose: Reads colors from DECAF document and assigns them
-- to corresponding IGES entities
MakeColors (me: in out; S: Shape from TopoDS; settings: DataMapOfShapeStyle from XCAFPrs;
colors: in out DataMapOfStyleTransient from XCAFPrs;
Map: in out MapOfShape from TopTools;
inherit: Style from XCAFPrs) is private;
---Purpose: Recursively iterates on subshapes and assigns colors
-- to faces and edges (if set)
WriteLayers (me: in out; doc: Document from TDocStd)
returns Boolean is protected;
---Purpose: Reads layers from DECAF document and assigns them
-- to corresponding IGES entities
WriteNames (me: in out; doc: Document from TDocStd)
returns Boolean is protected;
---Purpose: Recursivile iterates on subshapes and assign names
-- to IGES entity
--- Work with fileds for different mode of writing IGES file.
SetColorMode(me: in out; colormode: Boolean from Standard);
---Purpose: Set ColorMode for indicate write Colors or not.
GetColorMode(me) returns Boolean;
SetNameMode(me: in out; namemode: Boolean from Standard);
---Purpose: Set NameMode for indicate write Name or not.
GetNameMode(me) returns Boolean;
SetLayerMode(me: in out; layermode: Boolean from Standard);
---Purpose: Set LayerMode for indicate write Layers or not.
GetLayerMode(me) returns Boolean;
fields
myColorMode: Boolean;
myNameMode: Boolean;
myLayerMode: Boolean;
end Writer;

View File

@@ -0,0 +1,472 @@
// File: IGESCAFControl_Writer.cxx
// Created: Thu Aug 17 10:47:37 2000
// Author: Andrey BETENEV
// <abv@doomox.nnov.matra-dtv.fr>
#include <IGESCAFControl_Writer.ixx>
#include <XCAFDoc_ShapeTool.hxx>
#include <TDF_LabelSequence.hxx>
#include <TopoDS_Shape.hxx>
#include <XCAFDoc_ColorTool.hxx>
#include <XCAFPrs.hxx>
#include <TransferBRep_ShapeMapper.hxx>
#include <TransferBRep.hxx>
#include <Transfer_FinderProcess.hxx>
#include <IGESGraph_Color.hxx>
#include <IGESCAFControl.hxx>
#include <TCollection_HAsciiString.hxx>
#include <TopoDS_Iterator.hxx>
#include <XCAFDoc_DocumentTool.hxx>
#include <TDF_Label.hxx>
#include <Transfer_TransientListBinder.hxx>
#include <Standard_Transient.hxx>
#include <XCAFDoc_LayerTool.hxx>
#include <IGESGraph_DefinitionLevel.hxx>
#include <TCollection_AsciiString.hxx>
#include <TopExp_Explorer.hxx>
#include <TopoDS.hxx>
#include <TopoDS_Face.hxx>
#include <TopAbs.hxx>
#include <TopoDS_Iterator.hxx>
#include <TDF_ChildIterator.hxx>
#include <TDataStd_Name.hxx>
#include <IGESData_NameEntity.hxx>
#include <TopTools_SequenceOfShape.hxx>
#include <TColStd_HSequenceOfExtendedString.hxx>
//=======================================================================
//function : IGESCAFControl_Writer
//purpose :
//=======================================================================
IGESCAFControl_Writer::IGESCAFControl_Writer () :
myColorMode( Standard_True ),
myNameMode ( Standard_True ),
myLayerMode( Standard_True )
{
}
//=======================================================================
//function : IGESCAFControl_Writer
//purpose :
//=======================================================================
IGESCAFControl_Writer::IGESCAFControl_Writer (const Handle(XSControl_WorkSession)& WS,
const Standard_Boolean /*scratch*/ )
{
// this code does things in a wrong way, it should be vice-versa
WS->SetModel ( Model() );
WS->SetMapWriter ( TransferProcess() );
myColorMode = Standard_True;
myNameMode = Standard_True;
myLayerMode = Standard_True;
// SetWS (WS,scratch); // this should be the only required command here
}
//=======================================================================
//function : Transfer
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Writer::Transfer (const Handle(TDocStd_Document) &doc)
{
// translate free top-level shapes of the DECAF document
Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
TDF_LabelSequence shapes;
STool->GetFreeShapes ( shapes );
if ( shapes.Length() <=0 ) return Standard_False;
for ( Standard_Integer i=1; i <= shapes.Length(); i++ ) {
TopoDS_Shape shape = STool->GetShape ( shapes.Value(i) );
if ( ! shape.IsNull() )
AddShape ( shape );
// IGESControl_Writer::Transfer ( shape );
}
// write colors
if ( GetColorMode() )
WriteAttributes ( doc );
// write layers
if ( GetLayerMode() )
WriteLayers ( doc );
// write names
if ( GetNameMode() )
WriteNames( doc );
// refresh graph
// WS()->ComputeGraph ( Standard_True );
ComputeModel();
return Standard_True;
}
//=======================================================================
//function : Perform
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
const Standard_CString filename)
{
if ( ! Transfer ( doc ) ) return Standard_False;
return Write ( filename ) == IFSelect_RetDone;
}
//=======================================================================
//function : Perform
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
const TCollection_AsciiString &filename)
{
if ( ! Transfer ( doc ) ) return Standard_False;
return Write ( filename.ToCString() ) == IFSelect_RetDone;
}
//=======================================================================
//function : WriteAttributes
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Writer::WriteAttributes (const Handle(TDocStd_Document)& Doc)
{
// Iterate on shapes in the document
Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
TDF_LabelSequence labels;
STool->GetFreeShapes ( labels );
if ( labels.Length() <=0 ) return Standard_False;
for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
TDF_Label L = labels.Value(i);
// collect color settings
XCAFPrs_DataMapOfShapeStyle settings;
TopLoc_Location loc;
XCAFPrs::CollectStyleSettings ( L, loc, settings );
if ( settings.Extent() <=0 ) continue;
// get a target shape and try to find corresponding context
// (all the colors set under that label will be put into that context)
TopoDS_Shape S;
if ( ! STool->GetShape ( L, S ) ) continue;
// iterate on subshapes and create IGES styles
XCAFPrs_DataMapOfStyleTransient colors;
TopTools_MapOfShape Map;
const XCAFPrs_Style inherit;
MakeColors ( S, settings, colors, Map, inherit );
}
return Standard_True;
}
//=======================================================================
//function : MakeColors
//purpose :
//=======================================================================
void IGESCAFControl_Writer::MakeColors (const TopoDS_Shape &S,
const XCAFPrs_DataMapOfShapeStyle &settings,
XCAFPrs_DataMapOfStyleTransient &colors,
TopTools_MapOfShape &Map,
const XCAFPrs_Style &inherit)
{
// skip already processed shapes
if ( ! Map.Add ( S ) ) return;
// check if shape has its own style (or inherits from ancestor)
XCAFPrs_Style style = inherit;
if ( settings.IsBound(S) ) {
XCAFPrs_Style own = settings.Find(S);
if ( own.IsSetColorCurv() ) style.SetColorCurv ( own.GetColorCurv() );
if ( own.IsSetColorSurf() ) style.SetColorSurf ( own.GetColorSurf() );
}
// analyze whether current entity should get a color
Standard_Boolean hasColor = Standard_False;
Quantity_Color col;
if ( S.ShapeType() == TopAbs_FACE ) {
if ( style.IsSetColorSurf() ) {
hasColor = Standard_True;
col = style.GetColorSurf();
}
}
else if ( S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE ) {
if ( style.IsSetColorCurv() ) {
hasColor = Standard_True;
col = style.GetColorCurv();
}
}
// if color has to be assigned, try to do this
if ( hasColor ) {
Handle(IGESGraph_Color) colent;
Standard_Integer rank = IGESCAFControl::EncodeColor ( col );
if ( ! rank ) {
XCAFPrs_Style c; // style used as key in the map
c.SetColorSurf ( col );
if ( colors.IsBound ( c ) ) {
colent = Handle(IGESGraph_Color)::DownCast ( colors.Find(c) );
}
else {
Handle(TCollection_HAsciiString) str =
new TCollection_HAsciiString ( col.StringName ( col.Name() ) );
colent = new IGESGraph_Color;
colent->Init ( col.Red() * 100., col.Green() * 100., col.Blue() * 100., str );
AddEntity ( colent );
colors.Bind ( c, colent );
}
}
Handle(Transfer_FinderProcess) FP = TransferProcess();
Handle(IGESData_IGESEntity) ent;
Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, S );
if ( FP->FindTypedTransient ( mapper, STANDARD_TYPE(IGESData_IGESEntity), ent ) ) {
ent->InitColor ( colent, rank );
}
else {
// may be S was splited during shape process
Handle(Transfer_Binder) bnd = FP->Find ( mapper );
if ( ! bnd.IsNull() ) {
Handle(Transfer_TransientListBinder) TransientListBinder =
//Handle(Transfer_TransientListBinder)::DownCast( bnd->Next(Standard_True) );
Handle(Transfer_TransientListBinder)::DownCast( bnd );
Standard_Integer i=0, nb=0;
if (! TransientListBinder.IsNull() ) {
nb = TransientListBinder->NbTransients();
for (i=1; i<=nb; i++) {
Handle(Standard_Transient) t = TransientListBinder->Transient(i);
ent = Handle(IGESData_IGESEntity)::DownCast(t);
if (!ent.IsNull()) ent->InitColor ( colent, rank );
}
}
/* // alternative: consider recursive mapping S -> compound -> entities
else {
TopoDS_Shape comp = TransferBRep::ShapeResult(bnd);
if ( ! comp.IsNull() && comp.ShapeType() < S.ShapeType() )
for ( TopoDS_Iterator it(comp); it.More(); it.Next() ) {
MakeColors ( it.Value(), settings, colors, Map, style );
}
}
*/
}
}
}
// iterate on subshapes (except vertices :)
if ( S.ShapeType() == TopAbs_EDGE ) return;
for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
MakeColors ( it.Value(), settings, colors, Map, style );
}
}
static void AttachLayer (const Handle(Transfer_FinderProcess) &FP,
const Handle(XCAFDoc_LayerTool)& LTool,
const TopoDS_Shape& aSh,
const Standard_Integer localIntName)
{
TopTools_SequenceOfShape shseq;
if ( aSh.ShapeType() == TopAbs_COMPOUND ) {
TopoDS_Iterator aShIt(aSh);
for ( ; aShIt.More(); aShIt.Next() ) {
TopoDS_Shape newSh = aShIt.Value();
Handle(TColStd_HSequenceOfExtendedString) shLayers = new TColStd_HSequenceOfExtendedString;
if (! LTool->GetLayers( newSh, shLayers) || newSh.ShapeType() == TopAbs_COMPOUND )
AttachLayer(FP, LTool, newSh, localIntName);
}
return;
} else if ( aSh.ShapeType() == TopAbs_SOLID || aSh.ShapeType() == TopAbs_SHELL ) {
for (TopExp_Explorer exp(aSh,TopAbs_FACE) ; exp.More(); exp.Next()) {
TopoDS_Face entSh = TopoDS::Face (exp.Current());
shseq.Append(entSh);
}
} else {
shseq.Append(aSh);
}
for (Standard_Integer i = 1; i <= shseq.Length(); i++ ) {
TopoDS_Shape localShape = shseq.Value(i);
Handle(IGESData_IGESEntity) Igesent;
Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, localShape );
if ( FP->FindTypedTransient ( mapper, STANDARD_TYPE(IGESData_IGESEntity), Igesent ) ) {
Igesent->InitLevel( 0, localIntName );
// #ifdef DEB
// cout << "Init layer " << localIntName << " for "<< localShape.TShape()->DynamicType()->Name() << endl;
// #endif
}
#ifdef DEB
else cout << "Warning: Can't find entity for shape in mapper" << endl;
#endif
}
}
static void MakeLayers (const Handle(Transfer_FinderProcess) &FP,
const Handle(XCAFDoc_ShapeTool)& STool,
const Handle(XCAFDoc_LayerTool)& LTool,
const TDF_LabelSequence& aShapeLabels,
const Standard_Integer localIntName)
{
for ( Standard_Integer j = 1; j <= aShapeLabels.Length(); j++ ) {
TDF_Label aShapeLabel = aShapeLabels.Value(j);
TopoDS_Shape aSh;
if ( ! STool->GetShape ( aShapeLabel, aSh ) ) continue;
AttachLayer (FP, LTool, aSh, localIntName);
}
}
//=======================================================================
//function : WriteLayers
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Writer::WriteLayers (const Handle(TDocStd_Document)& Doc)
{
Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
if ( STool.IsNull() ) return Standard_False;
Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( Doc->Main() );
if ( LTool.IsNull() ) return Standard_False;
Standard_Integer globalIntName = 0;
TDF_LabelSequence aLayerLabels;
LTool->GetLayerLabels( aLayerLabels );
Handle(Transfer_FinderProcess) FP = TransferProcess();
for ( Standard_Integer i = 1; i <= aLayerLabels.Length(); i++ ){
TDF_Label aOneLayerL = aLayerLabels.Value(i);
if ( aOneLayerL.IsNull() ) continue;
TCollection_ExtendedString localName;
LTool->GetLayer(aOneLayerL, localName);
Standard_Integer localIntName = 0;
TCollection_AsciiString asciiName(localName,'?');
if (asciiName.IsIntegerValue() ) {
localIntName = asciiName.IntegerValue();
if (globalIntName < localIntName) globalIntName = localIntName;
TDF_LabelSequence aShapeLabels;
LTool->GetShapesOfLayer( aOneLayerL, aShapeLabels );
if ( aShapeLabels.Length() <= 0 ) continue;
MakeLayers(FP, STool, LTool, aShapeLabels, localIntName);
}
}
for ( Standard_Integer i1 = 1; i1 <= aLayerLabels.Length(); i1++ ) {
TDF_Label aOneLayerL = aLayerLabels.Value(i1);
if ( aOneLayerL.IsNull() ) continue;
TCollection_ExtendedString localName;
LTool->GetLayer(aOneLayerL, localName);
Standard_Integer localIntName = 0;
TCollection_AsciiString asciiName(localName,'?');
if (asciiName.IsIntegerValue() ) continue;
TDF_LabelSequence aShapeLabels;
LTool->GetShapesOfLayer( aOneLayerL, aShapeLabels );
if ( aShapeLabels.Length() <= 0 ) continue;
localIntName = ++globalIntName;
MakeLayers (FP, STool, LTool, aShapeLabels, localIntName);
}
return Standard_True;
}
//=======================================================================
//function : WriteNames
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Writer::WriteNames (const Handle(TDocStd_Document)& Doc)
{
Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( Doc->Main() );
if ( STool.IsNull() ) return Standard_False;
TDF_ChildIterator labelShIt(STool->BaseLabel() , Standard_True);
for (; labelShIt.More(); labelShIt.Next() ) {
TDF_Label shLabel = labelShIt.Value();
Handle(TDataStd_Name) aName;
if ( !shLabel.FindAttribute( TDataStd_Name::GetID(), aName) ) continue;
TCollection_ExtendedString shName = aName->Get();
TopoDS_Shape Sh;
if ( !STool->GetShape(shLabel, Sh) ) continue;
Handle(Transfer_FinderProcess) FP = TransferProcess();
Handle(IGESData_IGESEntity) Igesent;
Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, Sh );
if ( FP->FindTypedTransient ( mapper, STANDARD_TYPE(IGESData_IGESEntity), Igesent ) ) {
Handle(TCollection_HAsciiString) HAname = new TCollection_HAsciiString ( " " );
Standard_Integer len = 8 - shName.Length();
if ( len <0 ) len = 0;
for ( Standard_Integer k=1; len <8; k++, len++ ) {
HAname->SetValue ( len+1, IsAnAscii(shName.Value(k)) ? (Standard_Character)shName.Value(k) : '?' );
}
Igesent->SetLabel( HAname );
// Handle(IGESData_NameEntity) aNameent = new IGESData_NameEntity;
// Igesent->AddProperty(aNameent);
}
}
return Standard_True;
}
//=======================================================================
//function : SetColorMode
//purpose :
//=======================================================================
void IGESCAFControl_Writer::SetColorMode (const Standard_Boolean colormode)
{
myColorMode = colormode;
}
//=======================================================================
//function : GetColorMode
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Writer::GetColorMode () const
{
return myColorMode;
}
//=======================================================================
//function : SetNameMode
//purpose :
//=======================================================================
void IGESCAFControl_Writer::SetNameMode (const Standard_Boolean namemode)
{
myNameMode = namemode;
}
//=======================================================================
//function : GetNameMode
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Writer::GetNameMode () const
{
return myNameMode;
}
//=======================================================================
//function : SetLayerMode
//purpose :
//=======================================================================
void IGESCAFControl_Writer::SetLayerMode (const Standard_Boolean layermode)
{
myLayerMode = layermode;
}
//=======================================================================
//function : GetLayerMode
//purpose :
//=======================================================================
Standard_Boolean IGESCAFControl_Writer::GetLayerMode () const
{
return myLayerMode;
}