f7a04e6d8c795e9dd1a7649caa123239a0c96c1d
[occt.git] / src / XSDRAW / XSDRAW.cxx
1 // Copyright (c) 1999-2012 OPEN CASCADE SAS
2 //
3 // The content of this file is subject to the Open CASCADE Technology Public
4 // License Version 6.5 (the "License"). You may not use the content of this file
5 // except in compliance with the License. Please obtain a copy of the License
6 // at http://www.opencascade.org and read it completely before using this file.
7 //
8 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
9 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
10 //
11 // The Original Code and all software distributed under the License is
12 // distributed on an "AS IS" basis, without warranty of any kind, and the
13 // Initial Developer hereby disclaims all such warranties, including without
14 // limitation, any warranties of merchantability, fitness for a particular
15 // purpose or non-infringement. Please see the License for the specific terms
16 // and conditions governing the rights and limitations under the License.
17
18 #include <XSDRAW.ixx>
19 #include <Dico_DictionaryOfInteger.hxx>
20 #include <TCollection_AsciiString.hxx>
21 #include <TColStd_HSequenceOfAsciiString.hxx>
22 #include <IFSelect_Functions.hxx>
23 #include <IFSelect_SessionPilot.hxx>
24 #include <SWDRAW.hxx>
25 //#include <XSDRAW_Shape.hxx>
26 #include <XSDRAW_Vars.hxx>
27 #include <XSControl_Functions.hxx>
28 #include <XSControl_FuncShape.hxx>
29 #include <Draw_Appli.hxx>
30 #include <XSControl.hxx>
31 #include <XSControl_TransferWriter.hxx>
32
33 #include <TopoDS_Shape.hxx>
34 #include <TransferBRep.hxx>
35 #include <DBRep.hxx>
36
37 #include <Interface_Macros.hxx>
38 #include <stdio.h>
39
40 #include <Message.hxx>
41 #include <Message_Messenger.hxx>
42
43 static int deja = 0, dejald = 0;
44 //unused variable 
45 //static int okxset = 0;
46
47 static Handle(Dico_DictionaryOfInteger)       theolds;
48 static Handle(TColStd_HSequenceOfAsciiString) thenews;
49
50 static Handle(IFSelect_SessionPilot)    thepilot;  // detient Session, Model
51
52 static Standard_Integer XSTEPDRAWRUN (Draw_Interpretor& , Standard_Integer argc, const char** argv)
53 {
54   TCollection_AsciiString mess;
55   for (Standard_Integer i = 0; i < argc; i ++) {
56     mess.AssignCat(argv[i]);  mess.AssignCat(" ");
57   }
58
59   IFSelect_ReturnStatus stat = thepilot->Execute (mess.ToCString());
60   if (stat == IFSelect_RetError || stat == IFSelect_RetFail) return 1;
61   else return 0;
62 }
63
64     void  XSDRAW::ChangeCommand
65   (const Standard_CString oldname, const Standard_CString newname)
66 {
67   if (theolds.IsNull()) theolds = new Dico_DictionaryOfInteger;
68   Standard_Integer num = 0;
69   if (newname[0] != '\0') {
70     if (thenews.IsNull()) thenews = new TColStd_HSequenceOfAsciiString();
71     TCollection_AsciiString newstr(newname);
72     thenews->Append(newstr);
73     num = thenews->Length();
74   }
75   theolds->SetItem (oldname,num);
76 }
77
78     void  XSDRAW::RemoveCommand
79   (const Standard_CString oldname)
80 {
81   ChangeCommand (oldname,"");
82 }
83
84     Standard_Boolean  XSDRAW::LoadSession ()
85 {
86   if (deja) return Standard_False;
87   deja = 1;
88   thepilot   = new IFSelect_SessionPilot("XSTEP-DRAW>");
89   Handle(XSControl_WorkSession) WS = new XSControl_WorkSession;
90   WS->SetVars (new XSDRAW_Vars);
91   thepilot->SetSession (WS);
92
93   IFSelect_Functions::Init();
94   XSControl_Functions::Init();
95   XSControl_FuncShape::Init();
96 //  XSDRAW_Shape::Init();   passe a present par theCommands
97   return Standard_True;
98 }
99
100 void XSDRAW::LoadDraw (Draw_Interpretor& theCommands)
101 {
102   if (dejald) return;  dejald = 1;
103 //  XSDRAW_Shape::InitCommands (theCommands);
104 //  Pour tout faire d un coup : BRepTest & cie, SWDRAW & cie :
105   SWDRAW::Init (theCommands);
106   LoadSession();
107
108   //skl: we make remove commands "x" and "exit" in order to this commands are
109   //     performed not in IFSelect_SessionPilot but in standard Tcl interpretor
110   XSDRAW::RemoveCommand("x");
111   XSDRAW::RemoveCommand("exit");
112 //  if (!getenv("WBHOSTTOP")) XSDRAW::RemoveCommand("xsnew");
113   Handle(TColStd_HSequenceOfAsciiString) list =
114     IFSelect_Activator::Commands(0);
115   TCollection_AsciiString com;
116   Standard_Integer i, nb = list->Length();
117   for (i = 1; i <= nb; i ++) {
118     Handle(IFSelect_Activator) act;
119     Standard_Integer nact, num = -1;
120     char help[200];
121     com = list->Value(i);
122     if (!theolds.IsNull())
123       if (theolds->HasItem(com.ToCString())) num = theolds->Item(com.ToCString());
124     if (num == 0) continue;
125     if (!IFSelect_Activator::Select(com.ToCString(),nact,act))
126       sprintf (help,"type :  xhelp %s for help",com.ToCString());
127     else if (!act.IsNull()) strcpy(help,act->Help(nact));
128     if (num < 0) theCommands.Add (com.ToCString(),help,XSTEPDRAWRUN,act->Group());
129     else theCommands.Add (thenews->Value(num).ToCString(),help,XSTEPDRAWRUN,act->Group());
130   }
131 }
132
133     Standard_Integer  XSDRAW::Execute
134     (const Standard_CString command, const Standard_CString varname)
135 {
136   char mess[100];
137   sprintf (mess,command,varname);
138 #ifdef DEB
139   IFSelect_ReturnStatus stat = 
140 #endif
141     thepilot->Execute (mess);
142   return 1;  // stat ?
143 }
144
145     Handle(IFSelect_SessionPilot)  XSDRAW::Pilot ()
146       {  return thepilot;  }
147
148     Handle(XSControl_WorkSession)  XSDRAW::Session ()
149       {  return XSControl::Session(thepilot);  }
150
151     void  XSDRAW::SetController (const Handle(XSControl_Controller)& control)
152 {
153   if (thepilot.IsNull()) XSDRAW::LoadSession();
154   if (control.IsNull()) cout<<"XSTEP Controller not defined"<<endl;
155   else if (!Session().IsNull()) Session()->SetController (control);
156   else cout<<"XSTEP Session badly or not defined"<<endl;
157 }
158
159
160     Handle(XSControl_Controller) XSDRAW::Controller ()
161       {  return Session()->NormAdaptor();  }
162
163     Standard_Boolean  XSDRAW::SetNorm
164   (const Standard_CString norm, const Standard_CString profile)
165 {
166   Standard_Boolean stat = Session()->SelectNorm (norm,profile);
167   if (stat) IFSelect_Activator::SetCurrentAlias (Session()->SelectedNorm(Standard_True));
168   return stat;
169 }
170
171     Handle(Interface_Protocol)   XSDRAW::Protocol ()
172       {  return thepilot->Session()->Protocol();  }
173
174     Handle(Interface_InterfaceModel)  XSDRAW::Model ()
175       {  return thepilot->Session()->Model();  }
176
177     void  XSDRAW::SetModel
178   (const Handle(Interface_InterfaceModel)& model, const Standard_CString file)
179 {
180   thepilot->Session()->SetModel(model);
181   if (file && file[0] != '\0') thepilot->Session()->SetLoadedFile(file);
182 }
183
184     Handle(Interface_InterfaceModel)  XSDRAW::NewModel ()
185       {  return Session()->NewModel();  }
186
187     Handle(Standard_Transient)  XSDRAW::Entity (const Standard_Integer num)
188       {  return thepilot->Session()->StartingEntity(num);  }
189
190     Standard_Integer  XSDRAW::Number (const Handle(Standard_Transient)& ent)
191       {  return thepilot->Session()->StartingNumber(ent);  }
192
193     void  XSDRAW::SetTransferProcess (const Handle(Standard_Transient)& ATP)
194 {
195   DeclareAndCast(Transfer_FinderProcess,FP,ATP);
196   DeclareAndCast(Transfer_TransientProcess,TP,ATP);
197
198 //   Cas FinderProcess    ==> TransferWriter
199   if (!FP.IsNull())  Session()->SetMapWriter(FP);
200
201 //   Cas TransientProcess ==> TransferReader
202   if (!TP.IsNull()) {
203     if (!TP->Model().IsNull() && TP->Model() != Session()->Model())
204       Session()->SetModel (TP->Model());
205     Session()->SetMapReader(TP);
206   }
207 }
208
209     Handle(Transfer_TransientProcess)  XSDRAW::TransientProcess  ()
210       {  return Session()->TransferReader()->TransientProcess();  }
211
212     Handle(Transfer_FinderProcess)  XSDRAW::FinderProcess  ()
213       {  return Session()->TransferWriter()->FinderProcess();  }
214
215
216     void XSDRAW::InitTransferReader (const Standard_Integer mode)
217 {
218 //   0 nullify  1 clear
219 //   2 init TR avec contenu TP (roots)  3 init TP avec contenu TR
220 //   4 init avec model (debut scratch)
221   Session()->InitTransferReader(mode);
222 }
223
224     Handle(XSControl_TransferReader)  XSDRAW::TransferReader ()
225       {  return Session()->TransferReader();  }
226
227
228 //  ############  AUXILIAIRES  #############
229
230     Handle(Standard_Transient)  XSDRAW::GetEntity (const Standard_CString name)
231       {  return  IFSelect_Functions::GiveEntity (Session(),name);  }
232
233     Standard_Integer  XSDRAW::GetEntityNumber  (const Standard_CString name)
234       {  return  IFSelect_Functions::GiveEntityNumber (Session(),name);  }
235
236
237     Handle(TColStd_HSequenceOfTransient)  XSDRAW::GetList
238   (const Standard_CString first, const Standard_CString second)
239 {
240   Handle(TColStd_HSequenceOfTransient) list;
241   if (!first || first[0] == '\0') {
242     char ligne[80];  ligne[0] = '\0'; char truc;
243 //    cin.clear();  cin.get (ligne,79,'\n');
244     cin >> ligne;  Standard_Size ln = strlen(ligne);
245     char *ff = &ligne[0], *ss = NULL;
246     cin.get(truc);  if (truc != '\n') { cin>>&ligne[ln+1]; ss = &ligne[ln+1]; }
247     return  XSDRAW::GetList (ff,ss);
248   }
249 //  return IFSelect_Functions::GiveList (Session(),first,second);
250   return IFSelect_Functions::GiveList (Session(),first,second);
251 }
252
253
254     Standard_Boolean  XSDRAW::FileAndVar
255   (const Standard_CString file, const Standard_CString var,
256    const Standard_CString def,
257    TCollection_AsciiString& resfile,   TCollection_AsciiString& resvar)
258 {  return XSControl_FuncShape::FileAndVar
259      (XSDRAW::Session(),file,var,def,resfile,resvar); }
260
261     Standard_Integer  XSDRAW::MoreShapes
262   (Handle(TopTools_HSequenceOfShape)& list, const Standard_CString name)
263 {  return XSControl_FuncShape::MoreShapes  (XSDRAW::Session(),list,name);  }
264
265
266 //  FONCTION POUR LE DEBUG
267
268 Standard_Integer XSDRAW_WHAT (const Handle_Standard_Transient& ent)
269 {
270   if (ent.IsNull()) { cout<<"(Null Handle)"<<endl; return 0; }
271   Handle(Interface_InterfaceModel) model = XSDRAW::Model();
272   if (model.IsNull()) { cout<<"(No model)  Type:"<<ent->DynamicType()->Name()<<endl; return 0; }
273   cout<<" Num/Id :";  
274   model->Print (ent, Message::DefaultMessenger(), 0);
275   cout<<"  --  Recorded Type:"<<model->TypeName (ent)<<endl;
276   return model->Number(ent);
277 }