summaryrefslogtreecommitdiff
path: root/src/IGESSelect/IGESSelect_WorkLibrary.cxx
blob: 1486a1e58067d2cc94ba04857d1b9e481eef82ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180

#include <Standard_Stream.hxx>

#include <IGESSelect_WorkLibrary.ixx>
#include <Standard_ErrorHandler.hxx>
#include <Message_Messenger.hxx>
#include <Message.hxx>

#include <IGESSolid.hxx>
#include <IGESAppli.hxx>
#include <IGESDefs.hxx>

#include <IGESData_IGESWriter.hxx>
#include <IGESData_IGESModel.hxx>
#include <IGESData_Protocol.hxx>
#include <IGESData_FileProtocol.hxx>
#include <IGESFile_Read.hxx>
#include <IGESSelect_Dumper.hxx>
#include <IFSelect_GeneralModifier.hxx>
#include <TColStd_HSequenceOfInteger.hxx>
#include <IGESSelect_FileModifier.hxx>

#include <Interface_ReportEntity.hxx>
#include <IGESData_IGESEntity.hxx>
#include <IGESData_IGESDumper.hxx>

#include <Interface_Check.hxx>

#include <Interface_Macros.hxx>
#include <errno.h>

static int deja = 0;
static  Handle(IGESData_FileProtocol) IGESProto;


     IGESSelect_WorkLibrary::IGESSelect_WorkLibrary
  (const Standard_Boolean modefnes)
  : themodefnes (modefnes)
{
  IGESSolid::Init();
  IGESAppli::Init();
  IGESDefs::Init();

  if (!deja) {
    Handle(IGESSelect_Dumper) sesdump = new IGESSelect_Dumper;  // ainsi,cestfait
    deja = 1;
  }
  SetDumpLevels (4,6);
  SetDumpHelp (0,"Only DNum");
  SetDumpHelp (1,"DNum, IGES Type & Form");
  SetDumpHelp (2,"Main Directory Informations");
  SetDumpHelp (3,"Complete Directory Part");
  SetDumpHelp (4,"Directory + Fields (except list contents)");
  SetDumpHelp (5,"Complete (with list contents)");
  SetDumpHelp (6,"Complete + Transformed data");
}

    Standard_Integer  IGESSelect_WorkLibrary::ReadFile
  (const Standard_CString name,
   Handle(Interface_InterfaceModel)& model,
   const Handle(Interface_Protocol)& protocol) const
{
  Handle(Message_Messenger) sout = Message::DefaultMessenger();
  Handle(IGESData_IGESModel) igesmod = new IGESData_IGESModel;
  DeclareAndCast(IGESData_Protocol,prot,protocol);

  char* pname=(char*) name;
  Standard_Integer status = IGESFile_Read (pname,igesmod,prot);

  if (status < 0) sout<<"File not found : "<<name<<endl;
  if (status > 0) sout<<"Error when reading file : "<<name<<endl;
  if (status == 0) model = igesmod;
  else             model.Nullify();
  return status;
}


    Standard_Boolean  IGESSelect_WorkLibrary::WriteFile
  (IFSelect_ContextWrite& ctx) const
{
  Handle(Message_Messenger) sout = Message::DefaultMessenger();
//  Preparation
  DeclareAndCast(IGESData_IGESModel,igesmod,ctx.Model());
  DeclareAndCast(IGESData_Protocol,prot,ctx.Protocol());

  if (igesmod.IsNull() || prot.IsNull()) return Standard_False;
  ofstream fout;
  fout.rdbuf()->open(ctx.FileName(),ios::out );
  if (!fout) {
    ctx.CCheck(0)->AddFail("IGES File could not be created");
    sout<<" - IGES File could not be created : " << ctx.FileName() << endl; return 0;
  }
  sout<<" IGES File Name : "<<ctx.FileName();
  IGESData_IGESWriter VW(igesmod);  
  sout<<"("<<igesmod->NbEntities()<<" ents) ";

//  File Modifiers
  Standard_Integer nbmod = ctx.NbModifiers();
  for (Standard_Integer numod = 1; numod <= nbmod; numod ++) {
    ctx.SetModifier (numod);
    DeclareAndCast(IGESSelect_FileModifier,filemod,ctx.FileModifier());
    if (!filemod.IsNull()) filemod->Perform(ctx,VW);
//   (impressions de mise au point)
    sout << " .. FileMod." << numod <<" "<< filemod->Label();
    if (ctx.IsForAll()) sout << " (all model)";
    else  sout << " (" << ctx.NbEntities() << " entities)";
//    sout << flush;
  }

//  Envoi
  VW.SendModel(prot);            
  sout<<" Write ";
  if (themodefnes) VW.WriteMode() = 10;
  Standard_Boolean status = VW.Print(fout);                sout<<" Done"<<endl;

  errno = 0;
  fout.close();
  status = fout.good() && status && !errno;
  if(errno)
    sout << strerror(errno) << endl;

  return status;
}

    Handle(IGESData_Protocol)  IGESSelect_WorkLibrary::DefineProtocol ()
{
  if (!IGESProto.IsNull()) return IGESProto;
  Handle(IGESData_Protocol)     IGESProto1 = IGESSolid::Protocol();
  Handle(IGESData_Protocol)     IGESProto2 = IGESAppli::Protocol();
//  Handle(IGESData_FileProtocol) IGESProto  = new IGESData_FileProtocol;
  IGESProto  = new IGESData_FileProtocol;
  IGESProto->Add(IGESProto1);
  IGESProto->Add(IGESProto2);
  return IGESProto;
}


    void  IGESSelect_WorkLibrary::DumpEntity
  (const Handle(Interface_InterfaceModel)& model, 
   const Handle(Interface_Protocol)& protocol,
   const Handle(Standard_Transient)& entity,
   const Handle(Message_Messenger)& S, const Standard_Integer level) const
{
  DeclareAndCast(IGESData_IGESModel,igesmod,model);
  DeclareAndCast(IGESData_Protocol,igespro,protocol);
  DeclareAndCast(IGESData_IGESEntity,igesent,entity);
  if (igesmod.IsNull() || igespro.IsNull() || igesent.IsNull()) return;
  Standard_Integer num = igesmod->Number(igesent);
  if (num == 0) return;

  S<<" --- Entity "<<num;
  Standard_Boolean iserr = model->IsRedefinedContent(num);
  Handle(Standard_Transient) con;
  if (iserr) con = model->ReportEntity(num)->Content();
  if (entity.IsNull()) { S<<" Null"<<endl; return ;  }

//  On attaque le dump : d abord cas de l Erreur
  if (iserr) {
    S << " ERRONEOUS, Content, Type cdl : ";
    if (!con.IsNull()) S << con->DynamicType()->Name();
    else S << "(undefined)" << endl;
    igesent = GetCasted(IGESData_IGESEntity,con);
    con.Nullify();
    Handle(Interface_Check) check = model->ReportEntity(num)->Check();
    Interface_CheckIterator chlist;
    chlist.Add (check,num);
    chlist.Print (S,igesmod,Standard_False);
    if (igesent.IsNull()) return;
  }
  else S << " Type cdl : " << igesent->DynamicType()->Name();

  IGESData_IGESDumper dump(igesmod,igespro);
  try {
    OCC_CATCH_SIGNALS
    dump.Dump(igesent,S,level,(level-1)/3);
  }
  catch (Standard_Failure) {
    S << " **  Dump Interrupt **" << endl;
  }
}