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