0022048: Visualization, AIS_InteractiveContext - single object selection should alway...
[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 <NCollection_DataMap.hxx>
17 #include <Message_Messenger.hxx>
18 #include <Message_Msg.hxx>
19 #include <ShapeProcess.hxx>
20 #include <ShapeProcess_Context.hxx>
21 #include <ShapeProcess_Operator.hxx>
22 #include <Standard_ErrorHandler.hxx>
23 #include <Standard_Failure.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TColStd_SequenceOfAsciiString.hxx>
26
27 static NCollection_DataMap<TCollection_AsciiString, Handle(ShapeProcess_Operator)> aMapOfOperators;
28 //=======================================================================
29 //function : RegisterOperator
30 //purpose  : 
31 //=======================================================================
32
33 Standard_Boolean ShapeProcess::RegisterOperator (const Standard_CString name,
34                                                  const Handle(ShapeProcess_Operator)& op)
35 {
36   if (aMapOfOperators.IsBound(name)) {
37 #ifdef OCCT_DEBUG
38     cout << "Warning: operator with name " << name << " is already registered!" << endl;
39 #endif
40     return Standard_False;
41   }
42   aMapOfOperators.Bind( name, op );
43   return Standard_True;
44 }
45
46 //=======================================================================
47 //function : FindOperator
48 //purpose  : 
49 //=======================================================================
50
51 Standard_Boolean ShapeProcess::FindOperator (const Standard_CString name,
52                                              Handle(ShapeProcess_Operator)& op)
53 {
54   if (!aMapOfOperators.IsBound(name)) {
55 #ifdef OCCT_DEBUG
56     cout << "Error: no operator with name " << name << " registered!" << endl;
57 #endif
58     return Standard_False;
59   }
60   op = aMapOfOperators.ChangeFind(name);
61   return !op.IsNull();
62 }
63
64 //=======================================================================
65 //function : Perform
66 //purpose  : 
67 //=======================================================================
68
69 Standard_Boolean ShapeProcess::Perform (const Handle(ShapeProcess_Context)& context,
70                                         const Standard_CString seq)
71 {
72   context->SetScope ( seq );
73   
74   // get description of the sequence
75   TCollection_AsciiString sequence;
76   if ( ! context->GetString ( "exec.op", sequence ) ) {
77 #ifdef OCCT_DEBUG
78     cout << "Error: ShapeProcess_Performer::Perform: sequence not defined for " << seq << endl;
79 #endif
80     if ( context->TraceLevel() >0 ) {
81       Message_Msg SMSG3 ("SP.Sequence.Warn.NoSeq"); // Sequence %s not found
82       context->Messenger()->Send (SMSG3 << seq, Message_Warning);
83     }
84     context->UnSetScope();
85     return Standard_False;
86   }
87   TColStd_SequenceOfAsciiString sequenceOfOperators;
88   TCollection_AsciiString oper;
89   Standard_Integer i;
90   for ( i=1; ; i++ ) {
91     oper = sequence.Token ( " \t,;", i );
92     if ( oper.Length() <=0 ) break;
93     sequenceOfOperators.Append(oper);
94   }
95   
96   // put a message
97   if ( context->TraceLevel() >=2 ) {
98     Message_Msg SMSG0 ("SP.Sequence.Info.Seq"); //Sequence of operators: %s
99     TCollection_AsciiString Seq;
100     for ( Standard_Integer i1=1; i1 <= sequenceOfOperators.Length(); i1++ ) {
101       if (i1 > 1) Seq += ",";
102       Seq += sequenceOfOperators.Value(i1);
103     }
104     SMSG0.Arg (Seq.ToCString());
105     context->Messenger()->Send (SMSG0, Message_Info);
106   }
107
108   // iterate on operators in the sequence
109   Standard_Boolean isDone = Standard_False;
110   for (i=1; i<=sequenceOfOperators.Length(); i++) {
111     oper = sequenceOfOperators.Value(i);
112     
113     if ( context->TraceLevel() >=2 ) {
114       Message_Msg SMSG5 ("SP.Sequence.Info.Operator"); //Operator %d/%d: %s
115       SMSG5 << i << sequenceOfOperators.Length() << oper.ToCString();
116       context->Messenger()->Send (SMSG5, Message_Alarm);
117     }
118     
119     Handle(ShapeProcess_Operator) op;
120     if ( ! ShapeProcess::FindOperator ( oper.ToCString(), op ) ) {
121       if ( context->TraceLevel() >0 ) {
122         Message_Msg SMSG1 ("SP.Sequence.Error.NoOp"); //Operator %s not found
123         context->Messenger()->Send (SMSG1 << oper, Message_Alarm);
124       }
125       continue;
126     }
127     
128     context->SetScope ( oper.ToCString() );
129     try {
130       OCC_CATCH_SIGNALS
131       if ( op->Perform(context) )
132         isDone = Standard_True;
133     }
134     catch (Standard_Failure const& anException) {
135       Message_Msg SMSG2 ("SP.Sequence.Error.Except"); //Operator %s failed with exception %s
136       SMSG2 << oper << anException.GetMessageString();
137       context->Messenger()->Send (SMSG2, Message_Alarm);
138     }
139     context->UnSetScope();
140   }
141   
142   context->UnSetScope();
143   return isDone;
144 }