1 // Created on: 2000-08-04
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
18 #include <DDocStd.hxx>
20 #include <Precision.hxx>
21 #include <Quantity_Color.hxx>
22 #include <Quantity_ColorRGBA.hxx>
23 #include <TCollection_AsciiString.hxx>
24 #include <TDF_Label.hxx>
25 #include <TDF_LabelSequence.hxx>
26 #include <TDF_Tool.hxx>
27 #include <TDocStd_Document.hxx>
28 #include <TopoDS_Shape.hxx>
29 #include <XCAFDoc_ColorTool.hxx>
30 #include <XCAFDoc_DocumentTool.hxx>
31 #include <XCAFDoc_ShapeTool.hxx>
32 #include <XDEDRAW_Colors.hxx>
34 //=======================================================================
35 // Section: Work with colors
36 //=======================================================================
37 static Standard_Integer setColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
40 di<<"Use: "<<argv[0]<<" Doc {Label|Shape} R G B [alpha] [curve|surf]\n";
43 Handle(TDocStd_Document) Doc;
44 DDocStd::GetDocument(argv[1], Doc);
45 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
48 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
49 Quantity_Color Col ( Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atof(argv[5]), Quantity_TOC_RGB );
51 Quantity_ColorRGBA aColRGBA;
53 if (argc > 6 && (argv[6][0] != 's' && argv[6][0] != 'c')) {
54 aColRGBA.SetAlpha((Standard_ShortReal)(Draw::Atof(argv[6])));
57 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
58 XCAFDoc_ColorType ctype = XCAFDoc_ColorGen;
60 if (argv[argc - 1][0] == 's')
61 ctype = XCAFDoc_ColorSurf;
62 else if (argv[argc - 1][0] == 'c')
63 ctype = XCAFDoc_ColorCurv;
66 if ( !aLabel.IsNull() ) {
67 myColors->SetColor(aLabel, aColRGBA, ctype);
70 TopoDS_Shape aShape= DBRep::Get(argv[2]);
71 if ( !aShape.IsNull() ) {
72 myColors->SetColor(aShape, aColRGBA, ctype);
78 static Standard_Integer getColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
81 di<<"Use: "<<argv[0]<<" Doc Label\n";
84 Handle(TDocStd_Document) Doc;
85 DDocStd::GetDocument(argv[1], Doc);
86 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
89 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
90 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
91 Quantity_ColorRGBA col;
92 if ( !myColors->GetColor(aLabel, col) ) return 0;
94 if ((1 - col.Alpha()) < Precision::Confusion())
95 di << col.GetRGB().StringName(col.GetRGB().Name());
97 di << col.GetRGB().StringName ( col.GetRGB().Name() ) << " (" << col.Alpha() << ")";
102 static Standard_Integer getShapeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
105 di<<"Use: "<<argv[0]<<" Doc Label [curve|surf]\n";
108 Handle(TDocStd_Document) Doc;
109 DDocStd::GetDocument(argv[1], Doc);
110 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
113 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
114 if ( aLabel.IsNull() ) {
115 di << " no such label in document\n";
119 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
120 const XCAFDoc_ColorType ctype = ( argc <= 3 ? XCAFDoc_ColorGen : ( argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv ) );
122 Quantity_ColorRGBA col;
123 if ( !myColors->GetColor(aLabel, ctype, col) ) return 0;
125 if ((1 - col.Alpha()) < Precision::Confusion())
126 di << col.GetRGB().StringName(col.GetRGB().Name());
128 di << col.GetRGB().StringName(col.GetRGB().Name()) << " (" << col.Alpha() << ")";
133 static Standard_Integer getAllColors (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
136 di<<"Use: "<<argv[0]<<" Doc \n";
139 Handle(TDocStd_Document) Doc;
140 DDocStd::GetDocument(argv[1], Doc);
141 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
144 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
145 Quantity_ColorRGBA col;
146 TDF_LabelSequence Labels;
147 myColors->GetColors(Labels);
148 if (Labels.Length() >= 1) {
149 for ( Standard_Integer i = 1; i<= Labels.Length(); i++) {
150 aLabel = Labels.Value(i);
151 if ( !myColors->GetColor(aLabel, col) ) continue;
152 if ((1 - col.Alpha()) < Precision::Confusion())
153 di << col.GetRGB().StringName(col.GetRGB().Name());
155 di << col.GetRGB().StringName(col.GetRGB().Name()) << " (" << col.Alpha() << ")";
163 static Standard_Integer addColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
166 di<<"Use: "<<argv[0]<<" DocName R G B [alpha]\n";
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; }
174 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
176 Quantity_Color Col ( Draw::Atof(argv[2]), Draw::Atof(argv[3]), Draw::Atof(argv[4]), Quantity_TOC_RGB );
178 Quantity_ColorRGBA aColRGBA(Col);
179 aColRGBA.SetAlpha((Standard_ShortReal)(Draw::Atof(argv[5])));
180 aLabel = myColors->AddColor(aColRGBA);
183 aLabel = myColors->AddColor(Col);
185 TCollection_AsciiString Entry;
186 TDF_Tool::Entry(aLabel, Entry);
187 di << Entry.ToCString();
191 static Standard_Integer removeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
194 di<<"Use: "<<argv[0]<<" DocName Label\n";
197 Handle(TDocStd_Document) Doc;
198 DDocStd::GetDocument(argv[1], Doc);
199 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
202 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
203 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
204 myColors->RemoveColor(aLabel);
209 static Standard_Integer findColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
212 di<<"Use: "<<argv[0]<<" DocName R G B [alpha]\n";
215 Handle(TDocStd_Document) Doc;
216 DDocStd::GetDocument(argv[1], Doc);
217 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
219 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
220 TCollection_AsciiString Entry;
221 Quantity_Color Col(Draw::Atof(argv[2]), Draw::Atof(argv[3]), Draw::Atof(argv[4]), Quantity_TOC_RGB);
223 TDF_Tool::Entry(myColors->FindColor(Col), Entry);
226 Quantity_ColorRGBA aColRGBA(Col);
227 aColRGBA.SetAlpha((Standard_ShortReal)Draw::Atof(argv[5]));
228 TDF_Tool::Entry(myColors->FindColor(aColRGBA), Entry);
230 di << Entry.ToCString();
234 static Standard_Integer unsetColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
237 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} ColorType\n";
240 Handle(TDocStd_Document) Doc;
241 DDocStd::GetDocument(argv[1], Doc);
242 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
245 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
246 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
247 if ( !aLabel.IsNull() ) {
248 myColors->UnSetColor(aLabel, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv);
250 TopoDS_Shape aShape= DBRep::Get(argv[2]);
251 if ( !aShape.IsNull() ) {
252 myColors->UnSetColor(aShape, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv);
257 static Standard_Integer setVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
260 di<<"Use: "<<argv[0]<<"DocName {Lable|Shape} [isvisible(1/0)]\n";
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 Standard_Boolean isvisible = Standard_False;
268 if ( (argc==4) && (Draw::Atoi(argv[3])==1) ) isvisible = Standard_True;
271 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
272 if ( aLabel.IsNull() ) {
273 // get label by shape
274 TopoDS_Shape aShape= DBRep::Get(argv[2]);
275 if ( !aShape.IsNull() ) {
276 aLabel = localTool->ShapeTool()->FindShape( aShape, Standard_True );
279 if ( aLabel.IsNull() ) {
280 di << " cannot find indicated label in document\n";
283 localTool->SetVisibility( aLabel, isvisible );
287 static Standard_Integer getVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
290 di<<"Use: "<<argv[0]<<"DocName {Lable|Shape}\n";
293 Handle(TDocStd_Document) Doc;
294 DDocStd::GetDocument(argv[1], Doc);
295 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
296 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
298 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
299 if ( aLabel.IsNull() ) {
300 // get label by shape
301 TopoDS_Shape aShape= DBRep::Get(argv[2]);
302 if ( !aShape.IsNull() ) {
303 aLabel = localTool->ShapeTool()->FindShape( aShape, Standard_True );
306 if ( aLabel.IsNull() ) {
307 di << " cannot find indicated label in document\n";
310 if (localTool->IsVisible( aLabel) ) di << 1;
315 static Standard_Integer getStyledVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
318 di<<"Use: "<<argv[0]<<"DocName Shape\n";
321 Handle(TDocStd_Document) Doc;
322 DDocStd::GetDocument(argv[1], Doc);
323 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
324 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
326 aShape = DBRep::Get(argv[2]);
327 if (localTool->IsInstanceVisible( aShape) ) di << 1;
332 static Standard_Integer getStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
335 di<<"Use: "<<argv[0]<<" Doc shape colortype(s/c)\n";
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; }
342 aShape = DBRep::Get(argv[2]);
344 Quantity_ColorRGBA col;
345 XCAFDoc_ColorType type;
346 if ( argv[3] && argv[3][0] == 's' )
347 type = XCAFDoc_ColorSurf;
348 else if ( argv[3] && argv[3][0] == 'c' )
349 type = XCAFDoc_ColorCurv;
351 type = XCAFDoc_ColorGen;
352 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
353 if (localTool->GetInstanceColor( aShape, type, col) )
355 if ((1 - col.Alpha()) < Precision::Confusion())
356 di << col.GetRGB().StringName(col.GetRGB().Name());
358 di << col.GetRGB().StringName(col.GetRGB().Name()) << " (" << col.Alpha() << ")";
363 static Standard_Integer setStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
366 di<<"Use: "<<argv[0]<<" Doc shape R G B [alpha] type(s/c)\n";
369 Handle(TDocStd_Document) Doc;
370 DDocStd::GetDocument(argv[1], Doc);
371 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
373 aShape = DBRep::Get(argv[2]);
375 Quantity_Color col ( Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atof(argv[5]), Quantity_TOC_RGB );
376 Quantity_ColorRGBA aColRGBA;
377 aColRGBA.SetRGB(col);
378 if (argc > 6 && (argv[6][0] != 's' && argv[6][0] != 'c')) {
379 aColRGBA.SetAlpha((Standard_ShortReal)(Draw::Atof(argv[6])));
382 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
383 XCAFDoc_ColorType ctype = XCAFDoc_ColorGen;
385 if (argv[argc - 1][0] == 's')
386 ctype = XCAFDoc_ColorSurf;
387 else if (argv[argc - 1][0] == 'c')
388 ctype = XCAFDoc_ColorCurv;
390 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
391 if (!localTool->SetInstanceColor(aShape, ctype, aColRGBA))
393 di << "cannot set color for the indicated component\n";
399 //=======================================================================
400 //function : InitCommands
402 //=======================================================================
404 void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di)
406 static Standard_Boolean initactor = Standard_False;
411 initactor = Standard_True;
413 //=====================================
415 //=====================================
417 Standard_CString g = "XDE color's commands";
419 di.Add ("XSetColor","Doc {Label|Shape} R G B [alpha] [c|s]\t: Set color [R G B] to shape given by Label, "
420 "type of color 's' - for surface, 'c' - for curve (default generic)",
421 __FILE__, setColor, g);
423 di.Add ("XGetColor","Doc label \t: Return color defined on label in colortable",
424 __FILE__, getColor, g);
426 di.Add ("XGetShapeColor","Doc Label ColorType \t: Returns color defined by label",
427 __FILE__, getShapeColor, g);
429 di.Add ("XGetAllColors","Doc \t: Print all colors that defined in document",
430 __FILE__, getAllColors, g);
432 di.Add ("XAddColor","Doc R G B [alpha]\t: Add color in document to color table",
433 __FILE__, addColor, g);
435 di.Add ("XRemoveColor","Doc Label \t: Remove color in document from color table",
436 __FILE__, removeColor, g);
438 di.Add ("XFindColor","Doc R G B [alpha]\t: Find label where indicated color is situated",
439 __FILE__, findColor, g);
441 di.Add ("XUnsetColor","Doc {Label|Shape} ColorType \t: Unset color ",
442 __FILE__, unsetColor, g);
444 di.Add ("XSetObjVisibility","Doc {Label|Shape} (0\1) \t: Set the visibility of shape ",
445 __FILE__, setVisibility, g);
447 di.Add ("XGetObjVisibility","Doc {Label|Shape} \t: Return the visibility of shape ",
448 __FILE__, getVisibility, g);
450 di.Add ("XGetInstanceVisible","Doc Shape \t: Return the visibility of shape ",
451 __FILE__, getStyledVisibility, g);
453 di.Add ("XGetInstanceColor","Doc Shape \t: Return the color of component shape ",
454 __FILE__, getStyledcolor, g);
456 di.Add ("XSetInstanceColor","Doc Shape R G B [alpha] type \t: sets color for component of shape if SHUO structure exists already ",
457 __FILE__, setStyledcolor, g);