1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 // dce 21/01/99 S3767 : Suppression of general messages
16 #include <Interface_Check.hxx>
17 #include <Interface_CheckFailure.hxx>
18 #include <Interface_FileReaderData.hxx>
19 #include <Interface_FileReaderTool.hxx>
20 #include <Interface_GeneralLib.hxx>
21 #include <Interface_GeneralModule.hxx>
22 #include <Interface_InterfaceMismatch.hxx>
23 #include <Interface_InterfaceModel.hxx>
24 #include <Interface_Protocol.hxx>
25 #include <Interface_ReaderLib.hxx>
26 #include <Interface_ReaderModule.hxx>
27 #include <Interface_ReportEntity.hxx>
28 #include <Message.hxx>
29 #include <Message_Messenger.hxx>
30 #include <Message_Msg.hxx>
31 #include <Standard_DomainError.hxx>
32 #include <Standard_ErrorHandler.hxx>
33 #include <Standard_Failure.hxx>
34 #include <Standard_NoSuchObject.hxx>
35 #include <Standard_OutOfRange.hxx>
36 #include <Standard_Transient.hxx>
37 #include <Standard_TypeMismatch.hxx>
40 #include <OSD_Exception.hxx>
42 #include <OSD_Signal.hxx>
48 #include <Message_Msg.hxx>
49 // To use TCollectionHAsciiString
50 #include <TCollection_HAsciiString.hxx>
52 // Failure pour recuperer erreur en lecture fichier,
53 // TypeMismatch pour message d erreur circonstancie (cas particulier important)
56 // Gere le chargement d un Fichier, prealablement transforme en FileReaderData
57 // (de la bonne norme), dans un Modele
60 //=======================================================================
61 //function : Interface_FileReaderTool
63 //=======================================================================
65 Interface_FileReaderTool::Interface_FileReaderTool ()
67 themessenger = Message::DefaultMessenger();
68 theerrhand = Standard_True;
70 thenbrep0 = thenbreps = 0;
73 //=======================================================================
76 //=======================================================================
78 void Interface_FileReaderTool::SetData(const Handle(Interface_FileReaderData)& reader,
79 const Handle(Interface_Protocol)& protocol)
86 //=======================================================================
89 //=======================================================================
91 Handle(Interface_Protocol) Interface_FileReaderTool::Protocol () const
97 //=======================================================================
100 //=======================================================================
102 Handle(Interface_FileReaderData) Interface_FileReaderTool::Data () const
108 //=======================================================================
109 //function : SetModel
111 //=======================================================================
113 void Interface_FileReaderTool::SetModel
114 (const Handle(Interface_InterfaceModel)& amodel)
120 //=======================================================================
123 //=======================================================================
125 Handle(Interface_InterfaceModel) Interface_FileReaderTool::Model () const
130 //=======================================================================
131 //function : SetMessenger
133 //=======================================================================
135 void Interface_FileReaderTool::SetMessenger (const Handle(Message_Messenger)& messenger)
137 if ( messenger.IsNull() )
138 themessenger = Message::DefaultMessenger();
140 themessenger = messenger;
143 //=======================================================================
144 //function : Messenger
146 //=======================================================================
148 Handle(Message_Messenger) Interface_FileReaderTool::Messenger () const
153 //=======================================================================
154 //function : SetTraceLevel
156 //=======================================================================
158 void Interface_FileReaderTool::SetTraceLevel (const Standard_Integer tracelev)
163 //=======================================================================
164 //function : TraceLevel
166 //=======================================================================
168 Standard_Integer Interface_FileReaderTool::TraceLevel () const
173 //=======================================================================
174 //function : SetErrorHandle
176 //=======================================================================
178 void Interface_FileReaderTool::SetErrorHandle(const Standard_Boolean err)
184 //=======================================================================
185 //function : ErrorHandle
187 //=======================================================================
189 Standard_Boolean Interface_FileReaderTool::ErrorHandle() const
194 // .... Actions Connexes au CHARGEMENT DU MODELE ....
196 // SetEntities fait appel a des methodes a fournir :
197 // s appuyant sur un Recognizer adapte a l interface :
198 // - Recognize fait reco->Evaluate(... : selon record no num)
199 // et recupere le resultat
200 // ainsi que la definition de l entite inconnue de l interface
203 //=======================================================================
204 //function : SetEntities
206 //=======================================================================
208 void Interface_FileReaderTool::SetEntities ()
210 Standard_Integer num;
211 thenbreps = 0; thenbrep0 = 0;
213 for (num = thereader->FindNextRecord(0); num > 0;
214 num = thereader->FindNextRecord(num)) {
215 Handle(Standard_Transient) newent;
216 Handle(Interface_Check) ach = new Interface_Check;
217 if (!Recognize (num,ach,newent)) {
218 newent = UnknownEntity();
219 if (thereports.IsNull()) thereports =
220 new TColStd_HArray1OfTransient (1,thereader->NbRecords());
221 thenbreps ++; thenbrep0 ++;
222 thereports->SetValue (num,new Interface_ReportEntity(ach,newent));
224 else if ((ach->NbFails() + ach->NbWarnings() > 0) && !newent.IsNull()) {
225 if (thereports.IsNull()) thereports =
226 new TColStd_HArray1OfTransient (1,thereader->NbRecords());
227 thenbreps ++; thenbrep0 ++;
228 thereports->SetValue (num,new Interface_ReportEntity(ach,newent));
230 thereader->BindEntity (num,newent);
235 //=======================================================================
236 //function : RecognizeByLib
238 //=======================================================================
240 Standard_Boolean Interface_FileReaderTool::RecognizeByLib(const Standard_Integer num,
241 Interface_GeneralLib& glib,
242 Interface_ReaderLib& rlib,
243 Handle(Interface_Check)& ach,
244 Handle(Standard_Transient)& ent) const
246 Handle(Interface_GeneralModule) gmod;
247 Handle(Interface_ReaderModule) rmod;
248 Handle(Interface_Protocol) proto;
249 Standard_Integer CN = 0;
250 // Chercher dans ReaderLib : Reconnaissance de cas -> CN , proto
251 for (rlib.Start(); rlib.More(); rlib.Next()) {
252 rmod = rlib.Module();
253 if (rmod.IsNull()) continue;
254 CN = rmod->CaseNum(thereader,num);
255 if (CN > 0) { proto = rlib.Protocol(); break; }
257 if (CN <= 0 || proto.IsNull()) return Standard_False;
258 // Se recaler dans GeneralLib : Creation de l entite vide
259 Handle(Standard_Type) typrot = proto->DynamicType();
260 for (glib.Start(); glib.More(); glib.Next()) {
261 proto = glib.Protocol();
262 if (proto.IsNull()) continue;
263 if (proto->DynamicType() != typrot) continue;
264 Standard_Boolean res = glib.Module()->NewVoid(CN,ent);
266 if (!rmod.IsNull()) return rmod->NewRead (CN,thereader,num,ach,ent);
269 return Standard_False;
273 //=======================================================================
274 //function : UnknownEntity
276 //=======================================================================
278 Handle(Standard_Transient) Interface_FileReaderTool::UnknownEntity() const
280 return theproto->UnknownEntity();
284 //=======================================================================
285 //function : NewModel
287 //=======================================================================
289 Handle(Interface_InterfaceModel) Interface_FileReaderTool::NewModel() const
291 return theproto->NewModel();
295 //=======================================================================
298 //=======================================================================
300 void Interface_FileReaderTool::EndRead(const Handle(Interface_InterfaceModel)& )
302 } // par defaut, ne fait rien; redefinissable selon besoin
305 // .... (Sa Majeste le) CHARGEMENT DU MODELE ....
308 //=======================================================================
309 //function : LoadModel
311 //=======================================================================
313 void Interface_FileReaderTool::LoadModel
314 (const Handle(Interface_InterfaceModel)& amodel)
316 // Methode generale de lecture d un fichier : il est lu via un FileReaderData
317 // qui doit y donner acces de la facon la plus performante possible
318 // chaque interface definit son FileHeader avec ses methodes, appelees ici
321 // Building of Messages
322 //====================================
323 Handle(Message_Messenger) TF = Messenger();
324 //====================================
325 Handle(Interface_Check) ach = new Interface_Check;
329 // .. Demarrage : Lecture du Header ..
333 BeginRead(amodel); // selon la norme
335 catch (Standard_Failure const&) {
336 // Sendinf of message : Internal error during the header reading
339 Message_Msg Msg11("XSTEP_11");
340 TF->Send(Msg11, Message_Info);
345 BeginRead(amodel); // selon la norme
347 // .. Lecture des Entites ..
349 amodel->Reservate (thereader->NbEntities());
351 Standard_Integer num, num0 = thereader->FindNextRecord(0);
355 Standard_Integer ierr = 0; // erreur sur analyse d une entite
356 Handle(Standard_Transient) anent;
359 for (num = num0; num > 0; num = thereader->FindNextRecord(num)) {
362 // Lecture sous protection contre crash
363 // (fait aussi AddEntity mais pas SetReportEntity)
364 anent = LoadedEntity(num);
366 // Lecture non protegee : utile pour travailler avec dbx
368 //// anent = LoadedEntity(num);
371 if (anent.IsNull()) {
372 // Sending of message : Number of ignored Null Entities
375 Message_Msg Msg21("XSTEP_21");
376 Msg21.Arg(amodel->NbEntities());
377 TF->Send(Msg21, Message_Info);
381 // LoadedEntity fait AddEntity MAIS PAS SetReport (en bloc a la fin)
383 } // ---- fin boucle sur entites
384 num0 = 0; // plus rien
385 } // ---- fin du try, le catch suit
387 // En cas d erreur NON PREVUE par l analyse, recuperation par defaut
388 // Attention : la recuperation peut elle-meme planter ... (cf ierr)
389 catch (Standard_Failure const& anException) {
390 // Au passage suivant, on attaquera le record suivant
391 num0 = thereader->FindNextRecord(num); //:g9 abv 28 May 98: tr8_as2_ug.stp - infinite cycle: (0);
394 if (anException.IsKind(STANDARD_TYPE(OSD_Exception))) ierr = 2;
396 if (anException.IsKind(STANDARD_TYPE(OSD_Signal))) ierr = 2;
398 //:abv 03Apr00: anent is actually a previous one: if (anent.IsNull())
399 anent = thereader->BoundEntity(num);
400 if (anent.IsNull()) {
403 // Sending of message : Number of ignored Null Entities
407 Message_Msg Msg21("XSTEP_21");
408 Msg21.Arg(amodel->NbEntities() + 1);
410 TF->Send(Msg21, Message_Info);
415 /*Handle(Interface_Check)*/ ach = new Interface_Check(anent);
416 //: abv 03 Apr 00: trj3_s1-tc-214.stp: generate a message on exception
417 Message_Msg Msg278("XSTEP_278");
418 Msg278.Arg(amodel->StringLabel(anent));
419 ach->SendFail (Msg278);
422 // Sending of message : reading of entity failed
425 Message_Msg Msg22("XSTEP_22");
426 Msg22.Arg(amodel->StringLabel(anent));
427 TF->Send(Msg22, Message_Info);
433 //char mess[100]; svv #2
435 // ce qui serait bien ici serait de recuperer le texte de l erreur pour ach ...
437 // Sending of message : recovered entity
440 Message_Msg Msg23("XSTEP_23");
442 TF->Send(Msg23, Message_Info);
446 // Finalement, on charge une Entite Inconnue
448 Handle(Interface_ReportEntity) rep =
449 new Interface_ReportEntity(ach,anent);
450 Handle(Standard_Transient) undef = UnknownEntity();
451 AnalyseRecord(num,undef,ach);
452 rep->SetContent(undef);
454 if (thereports.IsNull()) thereports =
455 new TColStd_HArray1OfTransient (1,thereader->NbRecords());
457 thereports->SetValue (num,rep);
459 amodel->AddEntity (anent); // pas fait par LoadedEntity ...
463 // Sending of message : reading of entity failed
466 Message_Msg Msg22("XSTEP_22");
467 Msg22.Arg(amodel->StringLabel(anent));
468 TF->Send(Msg22, Message_Info);
471 // On garde <rep> telle quelle : pas d analyse fichier supplementaire,
472 // Mais la phase preliminaire eventuelle est conservee
473 // (en particulier, on garde trace du Type lu du fichier, etc...)
475 } // ----- fin complete du try/catch
476 } // ----- fin du while
478 // .. Ajout des Reports, silya
479 if (!thereports.IsNull()) {
482 // Sending of message : report
485 Message_Msg Msg24("XSTEP_24");
486 Msg24.Arg(thenbreps);
487 TF->Send(Msg24, Message_Info);
490 amodel->Reservate (-thenbreps-10);
491 thenbreps = thereports->Upper();
492 for (Standard_Integer nr = 1; nr <= thenbreps; nr ++) {
493 if (thereports->Value(nr).IsNull()) continue;
494 Handle(Standard_Transient) anent = thereader->BoundEntity (nr);
495 Handle(Interface_ReportEntity) rep =
496 Handle(Interface_ReportEntity)::DownCast(thereports->Value(nr));
497 amodel->SetReportEntity (-amodel->Number(anent),rep);
501 // Conclusion : peut ne rien faire : selon necessite
505 EndRead(amodel); // selon la norme
507 catch (Standard_Failure const&) {
508 // Sendinf of message : Internal error during the header reading
511 Message_Msg Msg11("XSTEP_11");
512 TF->Send(Msg11, Message_Info);
517 EndRead(amodel); // selon la norme
521 //=======================================================================
522 //function : LoadedEntity
524 //=======================================================================
526 Handle(Standard_Transient) Interface_FileReaderTool::LoadedEntity
527 (const Standard_Integer num)
529 Handle(Standard_Transient) anent = thereader->BoundEntity(num);
530 Handle(Interface_Check) ach = new Interface_Check(anent);
531 Handle(Interface_ReportEntity) rep; // entite Report, s il y a lieu
532 Standard_Integer irep = 0;
533 //Standard_Integer nbe = 0; svv #2
535 rep = Handle(Interface_ReportEntity)::DownCast(thereports->Value(num));
536 if (!rep.IsNull()) { irep = num; ach = rep->Check(); }
539 // Trace Entite Inconnue
540 if (thetrace >= 2 && theproto->IsUnknownEntity(anent)) {
541 Handle(Message_Messenger) TF = Messenger();
544 Message_Msg Msg22("XSTEP_22");
545 // Sending of message : reading of entity failed
546 Msg22.Arg(themodel->StringLabel(anent)->String());
547 TF->Send(Msg22, Message_Info);
550 // .. Chargement proprement dit : Specifique de la Norme ..
551 AnalyseRecord(num,anent,ach);
553 // .. Ajout dans le modele de l entite telle quelle ..
554 // ATTENTION, ReportEntity traitee en bloc apres les Load
555 themodel->AddEntity(anent);
557 // Erreur ou Correction : On cree une ReportEntity qui memorise le Check,
558 // l Entite, et en cas d Erreur une UndefinedEntity pour les Parametres
560 // On exploite ici le flag IsLoadError : s il a ete defini (a vrai ou faux)
561 // il a priorite sur les fails du check. Sinon, ce sont les fails qui parlent
563 Standard_Integer nbf = ach->NbFails();
564 Standard_Integer nbw = ach->NbWarnings();
566 //Standard_Integer n0; svv #2
567 themodel->NbEntities();
568 rep = new Interface_ReportEntity(ach,anent);
570 if (thereports.IsNull()) thereports =
571 new TColStd_HArray1OfTransient (1,thereader->NbRecords());
575 thereports->SetValue(irep,rep);
577 if ( thetrace >= 2 && !Messenger().IsNull())
578 ach->Print (Messenger(),2);
581 // Rechargement ? si oui, dans une UnknownEntity fournie par le protocole
582 if (thereader->IsErrorLoad()) nbf = (thereader->ResetErrorLoad() ? 1 : 0);
584 Handle(Standard_Transient) undef = UnknownEntity();
585 AnalyseRecord(num,undef,ach);
586 rep->SetContent(undef);
589 // Conclusion (Unknown : traite en externe because traitement Raise)
590 //// if (irep > 0) themodel->SetReportEntity (nbe,rep); en bloc a la fin
596 //=======================================================================
597 //function : ~Interface_FileReaderTool
599 //=======================================================================
601 Interface_FileReaderTool::~Interface_FileReaderTool()
604 void Interface_FileReaderTool::Clear()
609 thereports.Nullify();