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