0026922: Huge performance issue writing data to the output stream
[occt.git] / src / XDEDRAW / XDEDRAW_Colors.cxx
1 // Created on: 2000-08-04
2 // Created by: Pavel TELKOV
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 <DBRep.hxx>
18 #include <DDocStd.hxx>
19 #include <Draw.hxx>
20 #include <Quantity_Color.hxx>
21 #include <TCollection_AsciiString.hxx>
22 #include <TDF_Label.hxx>
23 #include <TDF_LabelSequence.hxx>
24 #include <TDF_Tool.hxx>
25 #include <TDocStd_Document.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <XCAFDoc_ColorTool.hxx>
28 #include <XCAFDoc_DocumentTool.hxx>
29 #include <XCAFDoc_ShapeTool.hxx>
30 #include <XDEDRAW_Colors.hxx>
31
32 //=======================================================================
33 // Section: Work with colors
34 //=======================================================================
35 static Standard_Integer setColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
36 {
37   if (argc < 6) {
38     di<<"Use: "<<argv[0]<<" Doc {Label|Shape} R G B [curve|surf]\n";
39     return 1;
40   }
41   Handle(TDocStd_Document) Doc;   
42   DDocStd::GetDocument(argv[1], Doc);
43   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
44   
45   TDF_Label aLabel;
46   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
47   Quantity_Color Col ( Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atof(argv[5]), Quantity_TOC_RGB );
48   
49   Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
50   const XCAFDoc_ColorType ctype = ( argc <= 6 ? XCAFDoc_ColorGen : ( argv[6][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv ) );
51   if ( !aLabel.IsNull() ) {
52     myColors->SetColor ( aLabel, Col, ctype );
53   }
54   else {
55     TopoDS_Shape aShape= DBRep::Get(argv[2]);
56     if ( !aShape.IsNull() ) {
57       myColors->SetColor ( aShape, Col, ctype );
58     }
59   }
60   return 0;
61 }
62
63 static Standard_Integer getColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
64 {
65   if (argc!=3) {
66     di<<"Use: "<<argv[0]<<" Doc Label\n";
67     return 1;
68   }
69   Handle(TDocStd_Document) Doc;   
70   DDocStd::GetDocument(argv[1], Doc);
71   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
72
73   TDF_Label aLabel;
74   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
75   Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
76   Quantity_Color col;
77   if ( !myColors->GetColor(aLabel, col) ) return 0;
78   
79   di << col.StringName ( col.Name() );
80    
81   return 0;
82 }
83
84 static Standard_Integer getShapeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
85 {
86   if (argc < 3) {
87     di<<"Use: "<<argv[0]<<" Doc Label [curve|surf]\n";
88     return 1;
89   }
90   Handle(TDocStd_Document) Doc;   
91   DDocStd::GetDocument(argv[1], Doc);
92   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
93
94   TDF_Label aLabel;
95   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
96   if ( aLabel.IsNull() ) {
97     di << " no such label in document\n";
98     return 1;
99   }
100
101   Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
102   const XCAFDoc_ColorType ctype = ( argc <= 3 ? XCAFDoc_ColorGen : ( argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv ) );
103
104   Quantity_Color col;
105   if ( !myColors->GetColor(aLabel, ctype, col) ) return 0;
106
107   TCollection_AsciiString Entry;
108   Entry = col.StringName ( col.Name() );
109   di << Entry.ToCString();
110
111   return 0;
112 }
113
114 static Standard_Integer getAllColors (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
115 {
116   if (argc!=2) {
117     di<<"Use: "<<argv[0]<<" Doc \n";
118     return 1;
119   }
120   Handle(TDocStd_Document) Doc;
121   DDocStd::GetDocument(argv[1], Doc);
122   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
123
124   TDF_Label aLabel;
125   Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
126   Quantity_Color col;
127   TDF_LabelSequence Labels;
128   myColors->GetColors(Labels);
129   if (Labels.Length() >= 1) {
130     for ( Standard_Integer i = 1; i<= Labels.Length(); i++) {
131       aLabel = Labels.Value(i);
132       if ( !myColors->GetColor(aLabel, col) ) continue;
133       di << col.StringName ( col.Name() );
134       di << " ";
135     }
136   }
137   return 0;
138 }
139
140
141 static Standard_Integer addColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
142 {
143   if (argc!=5) {
144     di<<"Use: "<<argv[0]<<" DocName R G B\n";
145     return 1;
146   }
147   Handle(TDocStd_Document) Doc;   
148   DDocStd::GetDocument(argv[1], Doc);
149   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
150
151   TDF_Label aLabel;
152   Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
153
154   Quantity_Color Col ( Draw::Atof(argv[2]), Draw::Atof(argv[3]), Draw::Atof(argv[4]), Quantity_TOC_RGB );
155   aLabel = myColors->AddColor(Col);
156   
157   TCollection_AsciiString Entry;
158   TDF_Tool::Entry(aLabel, Entry);
159   di << Entry.ToCString();
160   return 0;
161 }
162
163 static Standard_Integer removeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
164 {
165   if (argc!=3) {
166     di<<"Use: "<<argv[0]<<" DocName Label\n";
167     return 1;
168   }
169   Handle(TDocStd_Document) Doc;   
170   DDocStd::GetDocument(argv[1], Doc);
171   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
172
173   TDF_Label aLabel;
174   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
175   Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
176   myColors->RemoveColor(aLabel);
177   
178   return 0;
179 }
180
181 static Standard_Integer findColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
182 {
183   if (argc!=5) {
184     di<<"Use: "<<argv[0]<<" DocName R G B\n";
185     return 1;
186   }
187   Handle(TDocStd_Document) Doc;   
188   DDocStd::GetDocument(argv[1], Doc);
189   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
190
191   Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
192   
193   Quantity_Color Col ( Draw::Atof(argv[2]), Draw::Atof(argv[3]), Draw::Atof(argv[4]), Quantity_TOC_RGB );
194   
195   TCollection_AsciiString Entry;
196   TDF_Tool::Entry(myColors->FindColor(Col), Entry);
197   di << Entry.ToCString();
198   return 0;
199 }
200
201 static Standard_Integer unsetColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
202 {
203   if (argc!=4) {
204     di<<"Use: "<<argv[0]<<" DocName {Label|Shape} ColorType\n";
205     return 1;
206   }
207   Handle(TDocStd_Document) Doc;   
208   DDocStd::GetDocument(argv[1], Doc);
209   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
210
211   TDF_Label aLabel;
212   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
213   Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
214   if ( !aLabel.IsNull() ) {
215     myColors->UnSetColor(aLabel, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv);
216   }
217   TopoDS_Shape aShape= DBRep::Get(argv[2]);
218   if ( !aShape.IsNull() ) {
219     myColors->UnSetColor(aShape, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv);
220   }
221   return 0;
222 }
223
224 static Standard_Integer setVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
225 {
226   if (argc<3) {
227     di<<"Use: "<<argv[0]<<"DocName {Lable|Shape} [isvisible(1/0)]\n";
228     return 1;
229   }
230   Handle(TDocStd_Document) Doc;
231   DDocStd::GetDocument(argv[1], Doc);
232   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
233   Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
234   Standard_Boolean isvisible = Standard_False;
235   if ( (argc==4) && (Draw::Atoi(argv[3])==1) ) isvisible = Standard_True;
236   
237   TDF_Label aLabel;
238   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
239   if ( aLabel.IsNull() ) {
240     // get label by shape
241     TopoDS_Shape aShape= DBRep::Get(argv[2]);
242     if ( !aShape.IsNull() ) {
243       aLabel = localTool->ShapeTool()->FindShape( aShape, Standard_True );
244     }
245   }
246   if ( aLabel.IsNull() ) {
247     di << " cannot find indicated label in document\n";
248     return 1;
249   }
250   localTool->SetVisibility( aLabel, isvisible );
251   return 0;
252 }
253
254 static Standard_Integer getVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
255 {
256   if (argc<3) {
257     di<<"Use: "<<argv[0]<<"DocName {Lable|Shape}\n";
258     return 1;
259   }
260   Handle(TDocStd_Document) Doc;
261   DDocStd::GetDocument(argv[1], Doc);
262   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
263   Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
264   TDF_Label aLabel;
265   TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
266   if ( aLabel.IsNull() ) {
267     // get label by shape
268     TopoDS_Shape aShape= DBRep::Get(argv[2]);
269     if ( !aShape.IsNull() ) {
270       aLabel = localTool->ShapeTool()->FindShape( aShape, Standard_True );
271     }
272   }
273   if ( aLabel.IsNull() ) {
274     di << " cannot find indicated label in document\n";
275     return 1;
276   }
277   if (localTool->IsVisible( aLabel) ) di << 1;
278   else di << 0;
279   return 0;
280 }
281
282 static Standard_Integer getStyledVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
283 {
284   if (argc<3) {
285     di<<"Use: "<<argv[0]<<"DocName Shape\n";
286     return 1;
287   }
288   Handle(TDocStd_Document) Doc;
289   DDocStd::GetDocument(argv[1], Doc);
290   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
291   Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
292   TopoDS_Shape aShape;
293   aShape = DBRep::Get(argv[2]);
294   if (localTool->IsInstanceVisible( aShape) ) di << 1;
295   else di << 0;
296   return 0;
297 }
298
299 static Standard_Integer getStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
300 {
301   if (argc<3) {
302     di<<"Use: "<<argv[0]<<" Doc shape colortype(s/c)\n";
303     return 1;
304   }
305   Handle(TDocStd_Document) Doc;   
306   DDocStd::GetDocument(argv[1], Doc);
307   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
308   TopoDS_Shape aShape;
309   aShape = DBRep::Get(argv[2]);
310
311   Quantity_Color col;
312   XCAFDoc_ColorType type;
313   if ( argv[3] && argv[3][0] == 's' )
314     type = XCAFDoc_ColorSurf;
315   else if ( argv[3] && argv[3][0] == 'c' )
316     type = XCAFDoc_ColorCurv;
317   else
318     type = XCAFDoc_ColorGen;
319   Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
320   if (localTool->GetInstanceColor( aShape, type, col) ) 
321   {
322     TCollection_AsciiString Entry;
323     Entry = col.StringName ( col.Name() );
324     di << Entry.ToCString();
325   }
326   return 0;
327 }
328
329 static Standard_Integer setStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
330 {
331   if (argc<6) {
332     di<<"Use: "<<argv[0]<<" Doc shape R G B type(s/c)\n";
333     return 1;
334   }
335   Handle(TDocStd_Document) Doc;   
336   DDocStd::GetDocument(argv[1], Doc);
337   if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
338   TopoDS_Shape aShape;
339   aShape = DBRep::Get(argv[2]);
340
341   Quantity_Color col ( Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atof(argv[5]), Quantity_TOC_RGB );
342   XCAFDoc_ColorType type;
343   if ( argv[6] && argv[6][0] == 's' )
344     type = XCAFDoc_ColorSurf;
345   else if ( argv[6] && argv[6][0] == 'c' )
346     type = XCAFDoc_ColorCurv;
347   else
348     type = XCAFDoc_ColorGen;
349   Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
350   if (!localTool->SetInstanceColor( aShape, type, col) ) 
351   {
352     di << "cannot set color for the indicated component\n";
353     return 1;
354   }
355   return 0;
356 }
357
358 //=======================================================================
359 //function : InitCommands
360 //purpose  : 
361 //=======================================================================
362
363 void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di) 
364 {
365
366   static Standard_Boolean initactor = Standard_False;
367   if (initactor) return;  initactor = Standard_True;
368
369   //=====================================
370   // Work with colors
371   //=====================================  
372   
373   Standard_CString g = "XDE color's commands";
374   
375   di.Add ("XSetColor","Doc {Label|Shape} R G B [c|s]\t: Set color [R G B] to shape given by Label, "
376                       "type of color 's' - for surface, 'c' - for curve (default generic)",
377                    __FILE__, setColor, g);
378
379   di.Add ("XGetColor","Doc label \t: Return color defined on label in colortable",
380                    __FILE__, getColor, g);
381
382   di.Add ("XGetShapeColor","Doc Label ColorType \t: Returns color defined by label",
383                    __FILE__, getShapeColor, g);
384   
385   di.Add ("XGetAllColors","Doc \t: Print all colors that defined in document",
386                    __FILE__, getAllColors, g);
387   
388   di.Add ("XAddColor","Doc R G B \t: Add color in document to color table",
389                    __FILE__, addColor, g);
390   
391   di.Add ("XRemoveColor","Doc Label \t: Remove color in document from color table",
392                    __FILE__, removeColor, g);
393
394   di.Add ("XFindColor","Doc R G B \t: Find label where indicated color is situated",
395                    __FILE__, findColor, g);
396
397   di.Add ("XUnsetColor","Doc {Label|Shape} ColorType \t: Unset color ",
398                    __FILE__, unsetColor, g);
399   
400   di.Add ("XSetObjVisibility","Doc {Label|Shape} (0\1) \t: Set the visibility of shape  ",
401                    __FILE__, setVisibility, g);
402   
403   di.Add ("XGetObjVisibility","Doc {Label|Shape} \t: Return the visibility of shape ",
404                    __FILE__, getVisibility, g);
405
406   di.Add ("XGetInstanceVisible","Doc Shape \t: Return the visibility of shape ",
407                    __FILE__, getStyledVisibility, g);
408
409   di.Add ("XGetInstanceColor","Doc Shape \t: Return the color of component shape ",
410                    __FILE__, getStyledcolor, g);
411
412   di.Add ("XSetInstanceColor","Doc Shape color type \t: sets color for component of shape if SHUO structure exists already ",
413                    __FILE__, setStyledcolor, g);
414
415 }