0032743: Coding Rules - remove unused class friends of Package classes
[occt.git] / src / ShapeProcess / ShapeProcess.cxx
1 // Created on: 2000-08-21
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <ShapeProcess.hxx>
17
18 #include <NCollection_DataMap.hxx>
19 #include <Message_Messenger.hxx>
20 #include <Message_Msg.hxx>
21 #include <Message_ProgressScope.hxx>
22 #include <ShapeProcess_Context.hxx>
23 #include <ShapeProcess_Operator.hxx>
24 #include <Standard_ErrorHandler.hxx>
25 #include <Standard_Failure.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TColStd_SequenceOfAsciiString.hxx>
28
29 static NCollection_DataMap<TCollection_AsciiString, Handle(ShapeProcess_Operator)> aMapOfOperators;
30 //=======================================================================
31 //function : RegisterOperator
32 //purpose  : 
33 //=======================================================================
34
35 Standard_Boolean ShapeProcess::RegisterOperator (const Standard_CString name,
36                                                  const Handle(ShapeProcess_Operator)& op)
37 {
38   if (aMapOfOperators.IsBound(name)) {
39 #ifdef OCCT_DEBUG
40     std::cout << "Warning: operator with name " << name << " is already registered!" << std::endl;
41 #endif
42     return Standard_False;
43   }
44   aMapOfOperators.Bind( name, op );
45   return Standard_True;
46 }
47
48 //=======================================================================
49 //function : FindOperator
50 //purpose  : 
51 //=======================================================================
52
53 Standard_Boolean ShapeProcess::FindOperator (const Standard_CString name,
54                                              Handle(ShapeProcess_Operator)& op)
55 {
56   if (!aMapOfOperators.IsBound(name)) {
57 #ifdef OCCT_DEBUG
58     std::cout << "Error: no operator with name " << name << " registered!" << std::endl;
59 #endif
60     return Standard_False;
61   }
62   op = aMapOfOperators.ChangeFind(name);
63   return !op.IsNull();
64 }
65
66 //=======================================================================
67 //function : Perform
68 //purpose  : 
69 //=======================================================================
70
71 Standard_Boolean ShapeProcess::Perform (const Handle(ShapeProcess_Context)& context,
72                                         const Standard_CString seq,
73                                         const Message_ProgressRange& theProgress)
74 {
75   context->SetScope ( seq );
76   
77   // get description of the sequence
78   TCollection_AsciiString sequence;
79   if ( ! context->GetString ( "exec.op", sequence ) ) {
80 #ifdef OCCT_DEBUG
81     std::cout << "Error: ShapeProcess_Performer::Perform: sequence not defined for " << seq << std::endl;
82 #endif
83     if ( context->TraceLevel() >0 ) {
84       Message_Msg SMSG3 ("SP.Sequence.Warn.NoSeq"); // Sequence %s not found
85       context->Messenger()->Send (SMSG3 << seq, Message_Warning);
86     }
87     context->UnSetScope();
88     return Standard_False;
89   }
90   TColStd_SequenceOfAsciiString sequenceOfOperators;
91   TCollection_AsciiString oper;
92   Standard_Integer i;
93   for ( i=1; ; i++ ) {
94     oper = sequence.Token ( " \t,;", i );
95     if ( oper.Length() <=0 ) break;
96     sequenceOfOperators.Append(oper);
97   }
98   
99   // put a message
100   if ( context->TraceLevel() >=2 ) {
101     Message_Msg SMSG0 ("SP.Sequence.Info.Seq"); //Sequence of operators: %s
102     TCollection_AsciiString Seq;
103     for ( Standard_Integer i1=1; i1 <= sequenceOfOperators.Length(); i1++ ) {
104       if (i1 > 1) Seq += ",";
105       Seq += sequenceOfOperators.Value(i1);
106     }
107     SMSG0.Arg (Seq.ToCString());
108     context->Messenger()->Send (SMSG0, Message_Info);
109   }
110
111   // iterate on operators in the sequence
112   Standard_Boolean isDone = Standard_False;
113   Message_ProgressScope aPS(theProgress, NULL, sequenceOfOperators.Length());
114   for (i = 1; i<=sequenceOfOperators.Length() && aPS.More(); i++)
115   {
116     oper = sequenceOfOperators.Value(i);
117     Message_ProgressRange aRange = aPS.Next();
118     
119     if ( context->TraceLevel() >=2 ) {
120       Message_Msg SMSG5 ("SP.Sequence.Info.Operator"); //Operator %d/%d: %s
121       SMSG5 << i << sequenceOfOperators.Length() << oper.ToCString();
122       context->Messenger()->Send (SMSG5, Message_Alarm);
123     }
124     
125     Handle(ShapeProcess_Operator) op;
126     if ( ! ShapeProcess::FindOperator ( oper.ToCString(), op ) ) {
127       if ( context->TraceLevel() >0 ) {
128         Message_Msg SMSG1 ("SP.Sequence.Error.NoOp"); //Operator %s not found
129         context->Messenger()->Send (SMSG1 << oper, Message_Alarm);
130       }
131       continue;
132     }
133     
134     context->SetScope ( oper.ToCString() );
135     try {
136       OCC_CATCH_SIGNALS
137       if (op->Perform(context, aRange))
138         isDone = Standard_True;
139     }
140     catch (Standard_Failure const& anException) {
141       Message_Msg SMSG2 ("SP.Sequence.Error.Except"); //Operator %s failed with exception %s
142       SMSG2 << oper << anException.GetMessageString();
143       context->Messenger()->Send (SMSG2, Message_Alarm);
144     }
145     context->UnSetScope();
146   }
147   
148   context->UnSetScope();
149   return isDone;
150 }