0030493: Draw, ViewerTest - minor improvement of vdisplay command
[occt.git] / src / DDocStd / DDocStd.cxx
1 // Created on: 2000-03-01
2 // Created by: Denis PASCAL
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
17 #include <CDF_Session.hxx>
18 #include <DBRep.hxx>
19 #include <DDocStd.hxx>
20 #include <DDocStd_DrawDocument.hxx>
21 #include <Draw.hxx>
22 #include <Draw_Interpretor.hxx>
23 #include <Standard_DomainError.hxx>
24 #include <Standard_GUID.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <TDF_Attribute.hxx>
27 #include <TDF_Label.hxx>
28 #include <TDF_MapIteratorOfLabelMap.hxx>
29 #include <TDF_Tool.hxx>
30 #include <TDocStd_Application.hxx>
31 #include <TDocStd_Document.hxx>
32
33 #include <StdLDrivers.hxx>
34 #include <BinLDrivers.hxx>
35 #include <XmlLDrivers.hxx>
36 #include <StdDrivers.hxx>
37 #include <BinDrivers.hxx>
38 #include <XmlDrivers.hxx>
39
40 //=======================================================================
41 //function : Find
42 //purpose  : 
43 //=======================================================================
44
45 const Handle(TDocStd_Application)& DDocStd::GetApplication()
46 {
47   static Handle(TDocStd_Application) anApp;
48   if (anApp.IsNull())
49   {
50     anApp = new TDocStd_Application;
51
52     // Initialize standard document formats at creation - they should
53     // be available even if this DRAW plugin is not loaded by pload command
54     StdLDrivers::DefineFormat(anApp);
55     BinLDrivers::DefineFormat(anApp);
56     XmlLDrivers::DefineFormat(anApp);
57     StdDrivers::DefineFormat(anApp);
58     BinDrivers::DefineFormat(anApp);
59     XmlDrivers::DefineFormat(anApp);
60   }
61   return anApp;
62 }
63
64
65 //=======================================================================
66 //function : GetDocument
67 //purpose  : 
68 //=======================================================================
69
70 Standard_Boolean DDocStd::GetDocument (Standard_CString&         Name,
71                                        Handle(TDocStd_Document)& DOC,
72                                        const Standard_Boolean    Complain)
73 {
74   Handle(DDocStd_DrawDocument) DD = Handle(DDocStd_DrawDocument)::DownCast (Draw::GetExisting (Name));
75   if (DD.IsNull()) {
76     if (Complain) cout << Name << " is not a Document" << endl; 
77     return Standard_False;
78   }
79   Handle(TDocStd_Document) STDDOC = DD->GetDocument();
80   if (!STDDOC.IsNull()) {
81     DOC = STDDOC;
82     return Standard_True;
83   }
84   if (Complain) cout << Name << " is not a CAF Document" << endl; 
85   return Standard_False;
86 }
87
88
89 //=======================================================================
90 //function : Label
91 //purpose  : try to retrieve a label 
92 //=======================================================================
93
94 Standard_Boolean DDocStd::Find (const Handle(TDocStd_Document)& D,
95                                 const Standard_CString  Entry,
96                                 TDF_Label& Label,   
97                                 const Standard_Boolean  Complain)
98 {
99   Label.Nullify();
100   TDF_Tool::Label(D->GetData(),Entry,Label,Standard_False);
101   if (Label.IsNull() && Complain) cout<<"No label for entry "<<Entry<<endl;
102   return !Label.IsNull();
103 }
104
105 //=======================================================================
106 //function : Find
107 //purpose  : Try to retrieve an attribute.
108 //=======================================================================
109
110 Standard_Boolean DDocStd::Find (const Handle(TDocStd_Document)& D,
111                                 const Standard_CString  Entry,
112                                 const Standard_GUID&    ID,
113                                 Handle(TDF_Attribute)&  A,
114                                 const Standard_Boolean  Complain) 
115 {
116   TDF_Label L;
117   if (Find(D,Entry,L,Complain)) {
118     if (L.FindAttribute(ID,A)) return Standard_True;
119     if (Complain) cout <<"attribute not found for entry : "<< Entry <<endl; 
120   }
121   return Standard_False;   
122 }
123
124
125 //=======================================================================
126 //function : ReturnLabel
127 //purpose  : 
128 //=======================================================================
129  
130 Draw_Interpretor& DDocStd::ReturnLabel(Draw_Interpretor& di, const TDF_Label& L)
131 {
132   TCollection_AsciiString S;
133   TDF_Tool::Entry(L,S);
134   di << S.ToCString();
135   return di;
136 }
137
138 //=======================================================================
139 //function : AllCommands
140 //purpose  : 
141 //=======================================================================
142
143 void DDocStd::AllCommands(Draw_Interpretor& theCommands) 
144 {
145   static Standard_Boolean done = Standard_False;
146   if (done) return;
147   done = Standard_True;
148
149   // define commands
150   DDocStd::ApplicationCommands(theCommands);
151   DDocStd::DocumentCommands(theCommands);
152   DDocStd::ToolsCommands(theCommands);
153   DDocStd::MTMCommands(theCommands);
154   DDocStd::ShapeSchemaCommands(theCommands);
155 }