0029705: Foundation Classes - ShapeAnalysis_FreeBounds intention documentation is...
[occt.git] / src / XDEDRAW / XDEDRAW_Colors.cxx
CommitLineData
fcd9a94e 1// Created on: 2000-08-04
b311480e 2// Created by: Pavel TELKOV
973c2be1 3// Copyright (c) 2000-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16
17#include <DBRep.hxx>
18#include <DDocStd.hxx>
42cf5bc1 19#include <Draw.hxx>
973d410e 20#include <Precision.hxx>
7fd59977 21#include <Quantity_Color.hxx>
973d410e 22#include <Quantity_ColorRGBA.hxx>
42cf5bc1 23#include <TCollection_AsciiString.hxx>
7fd59977 24#include <TDF_Label.hxx>
25#include <TDF_LabelSequence.hxx>
42cf5bc1 26#include <TDF_Tool.hxx>
7fd59977 27#include <TDocStd_Document.hxx>
42cf5bc1 28#include <TopoDS_Shape.hxx>
29#include <XCAFDoc_ColorTool.hxx>
7fd59977 30#include <XCAFDoc_DocumentTool.hxx>
31#include <XCAFDoc_ShapeTool.hxx>
42cf5bc1 32#include <XDEDRAW_Colors.hxx>
7fd59977 33
34//=======================================================================
35// Section: Work with colors
36//=======================================================================
7fd59977 37static Standard_Integer setColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
38{
3c946c38 39 if (argc < 6) {
973d410e 40 di<<"Use: "<<argv[0]<<" Doc {Label|Shape} R G B [alpha] [curve|surf]\n";
7fd59977 41 return 1;
42 }
43 Handle(TDocStd_Document) Doc;
44 DDocStd::GetDocument(argv[1], Doc);
586db386 45 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 46
47 TDF_Label aLabel;
48 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
91322f44 49 Quantity_Color Col ( Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atof(argv[5]), Quantity_TOC_RGB );
973d410e 50
51 Quantity_ColorRGBA aColRGBA;
52 aColRGBA.SetRGB(Col);
53 if (argc > 6 && (argv[6][0] != 's' && argv[6][0] != 'c')) {
54 aColRGBA.SetAlpha((Standard_ShortReal)(Draw::Atof(argv[6])));
55 }
7fd59977 56
57 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
973d410e 58 XCAFDoc_ColorType ctype = XCAFDoc_ColorGen;
59 if (argc > 6) {
60 if (argv[argc - 1][0] == 's')
61 ctype = XCAFDoc_ColorSurf;
62 else if (argv[argc - 1][0] == 'c')
63 ctype = XCAFDoc_ColorCurv;
64 }
65
7fd59977 66 if ( !aLabel.IsNull() ) {
973d410e 67 myColors->SetColor(aLabel, aColRGBA, ctype);
7fd59977 68 }
69 else {
70 TopoDS_Shape aShape= DBRep::Get(argv[2]);
71 if ( !aShape.IsNull() ) {
973d410e 72 myColors->SetColor(aShape, aColRGBA, ctype);
7fd59977 73 }
74 }
75 return 0;
76}
77
78static Standard_Integer getColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
79{
80 if (argc!=3) {
586db386 81 di<<"Use: "<<argv[0]<<" Doc Label\n";
7fd59977 82 return 1;
83 }
84 Handle(TDocStd_Document) Doc;
85 DDocStd::GetDocument(argv[1], Doc);
586db386 86 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 87
88 TDF_Label aLabel;
89 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
90 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
973d410e 91 Quantity_ColorRGBA col;
7fd59977 92 if ( !myColors->GetColor(aLabel, col) ) return 0;
93
973d410e 94 if ((1 - col.Alpha()) < Precision::Confusion())
95 di << col.GetRGB().StringName(col.GetRGB().Name());
96 else
97 di << col.GetRGB().StringName ( col.GetRGB().Name() ) << " (" << col.Alpha() << ")";
7fd59977 98
99 return 0;
100}
101
102static Standard_Integer getShapeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
103{
3c946c38 104 if (argc < 3) {
586db386 105 di<<"Use: "<<argv[0]<<" Doc Label [curve|surf]\n";
7fd59977 106 return 1;
107 }
108 Handle(TDocStd_Document) Doc;
109 DDocStd::GetDocument(argv[1], Doc);
586db386 110 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 111
112 TDF_Label aLabel;
113 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
3c946c38 114 if ( aLabel.IsNull() ) {
115 di << " no such label in document\n";
116 return 1;
117 }
118
7fd59977 119 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
3c946c38 120 const XCAFDoc_ColorType ctype = ( argc <= 3 ? XCAFDoc_ColorGen : ( argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv ) );
121
973d410e 122 Quantity_ColorRGBA col;
3c946c38 123 if ( !myColors->GetColor(aLabel, ctype, col) ) return 0;
124
973d410e 125 if ((1 - col.Alpha()) < Precision::Confusion())
126 di << col.GetRGB().StringName(col.GetRGB().Name());
127 else
128 di << col.GetRGB().StringName(col.GetRGB().Name()) << " (" << col.Alpha() << ")";
3c946c38 129
7fd59977 130 return 0;
131}
132
133static Standard_Integer getAllColors (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
134{
135 if (argc!=2) {
586db386 136 di<<"Use: "<<argv[0]<<" Doc \n";
7fd59977 137 return 1;
138 }
139 Handle(TDocStd_Document) Doc;
140 DDocStd::GetDocument(argv[1], Doc);
586db386 141 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 142
143 TDF_Label aLabel;
144 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
973d410e 145 Quantity_ColorRGBA col;
7fd59977 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;
973d410e 152 if ((1 - col.Alpha()) < Precision::Confusion())
153 di << col.GetRGB().StringName(col.GetRGB().Name());
154 else
155 di << col.GetRGB().StringName(col.GetRGB().Name()) << " (" << col.Alpha() << ")";
7fd59977 156 di << " ";
157 }
158 }
159 return 0;
160}
161
162
163static Standard_Integer addColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
164{
973d410e 165 if (argc < 5) {
166 di<<"Use: "<<argv[0]<<" DocName R G B [alpha]\n";
7fd59977 167 return 1;
168 }
169 Handle(TDocStd_Document) Doc;
170 DDocStd::GetDocument(argv[1], Doc);
586db386 171 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 172
173 TDF_Label aLabel;
174 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
175
91322f44 176 Quantity_Color Col ( Draw::Atof(argv[2]), Draw::Atof(argv[3]), Draw::Atof(argv[4]), Quantity_TOC_RGB );
973d410e 177 if (argc == 6) {
178 Quantity_ColorRGBA aColRGBA(Col);
179 aColRGBA.SetAlpha((Standard_ShortReal)(Draw::Atof(argv[5])));
180 aLabel = myColors->AddColor(aColRGBA);
181 }
182 else
183 aLabel = myColors->AddColor(Col);
7fd59977 184
185 TCollection_AsciiString Entry;
186 TDF_Tool::Entry(aLabel, Entry);
187 di << Entry.ToCString();
188 return 0;
189}
190
191static Standard_Integer removeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
192{
193 if (argc!=3) {
586db386 194 di<<"Use: "<<argv[0]<<" DocName Label\n";
7fd59977 195 return 1;
196 }
197 Handle(TDocStd_Document) Doc;
198 DDocStd::GetDocument(argv[1], Doc);
586db386 199 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 200
201 TDF_Label aLabel;
202 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
203 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
204 myColors->RemoveColor(aLabel);
205
206 return 0;
207}
208
209static Standard_Integer findColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
210{
973d410e 211 if (argc < 5) {
212 di<<"Use: "<<argv[0]<<" DocName R G B [alpha]\n";
7fd59977 213 return 1;
214 }
215 Handle(TDocStd_Document) Doc;
216 DDocStd::GetDocument(argv[1], Doc);
586db386 217 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 218
219 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
7fd59977 220 TCollection_AsciiString Entry;
973d410e 221 Quantity_Color Col(Draw::Atof(argv[2]), Draw::Atof(argv[3]), Draw::Atof(argv[4]), Quantity_TOC_RGB);
222 if (argc == 5) {
223 TDF_Tool::Entry(myColors->FindColor(Col), Entry);
224 }
225 else {
226 Quantity_ColorRGBA aColRGBA(Col);
227 aColRGBA.SetAlpha((Standard_ShortReal)Draw::Atof(argv[5]));
228 TDF_Tool::Entry(myColors->FindColor(aColRGBA), Entry);
229 }
7fd59977 230 di << Entry.ToCString();
231 return 0;
232}
233
234static Standard_Integer unsetColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
235{
236 if (argc!=4) {
586db386 237 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} ColorType\n";
7fd59977 238 return 1;
239 }
240 Handle(TDocStd_Document) Doc;
241 DDocStd::GetDocument(argv[1], Doc);
586db386 242 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 243
244 TDF_Label aLabel;
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);
249 }
250 TopoDS_Shape aShape= DBRep::Get(argv[2]);
251 if ( !aShape.IsNull() ) {
252 myColors->UnSetColor(aShape, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv);
253 }
254 return 0;
255}
256
257static Standard_Integer setVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
258{
259 if (argc<3) {
586db386 260 di<<"Use: "<<argv[0]<<"DocName {Lable|Shape} [isvisible(1/0)]\n";
7fd59977 261 return 1;
262 }
263 Handle(TDocStd_Document) Doc;
264 DDocStd::GetDocument(argv[1], Doc);
586db386 265 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 266 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
267 Standard_Boolean isvisible = Standard_False;
91322f44 268 if ( (argc==4) && (Draw::Atoi(argv[3])==1) ) isvisible = Standard_True;
7fd59977 269
270 TDF_Label aLabel;
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 );
277 }
278 }
279 if ( aLabel.IsNull() ) {
586db386 280 di << " cannot find indicated label in document\n";
7fd59977 281 return 1;
282 }
283 localTool->SetVisibility( aLabel, isvisible );
284 return 0;
285}
286
287static Standard_Integer getVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
288{
289 if (argc<3) {
586db386 290 di<<"Use: "<<argv[0]<<"DocName {Lable|Shape}\n";
7fd59977 291 return 1;
292 }
293 Handle(TDocStd_Document) Doc;
294 DDocStd::GetDocument(argv[1], Doc);
586db386 295 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 296 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
297 TDF_Label aLabel;
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 );
304 }
305 }
306 if ( aLabel.IsNull() ) {
586db386 307 di << " cannot find indicated label in document\n";
7fd59977 308 return 1;
309 }
310 if (localTool->IsVisible( aLabel) ) di << 1;
311 else di << 0;
312 return 0;
313}
314
315static Standard_Integer getStyledVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
316{
317 if (argc<3) {
586db386 318 di<<"Use: "<<argv[0]<<"DocName Shape\n";
7fd59977 319 return 1;
320 }
321 Handle(TDocStd_Document) Doc;
322 DDocStd::GetDocument(argv[1], Doc);
586db386 323 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 324 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
325 TopoDS_Shape aShape;
326 aShape = DBRep::Get(argv[2]);
327 if (localTool->IsInstanceVisible( aShape) ) di << 1;
328 else di << 0;
329 return 0;
330}
331
332static Standard_Integer getStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
333{
334 if (argc<3) {
586db386 335 di<<"Use: "<<argv[0]<<" Doc shape colortype(s/c)\n";
7fd59977 336 return 1;
337 }
338 Handle(TDocStd_Document) Doc;
339 DDocStd::GetDocument(argv[1], Doc);
586db386 340 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 341 TopoDS_Shape aShape;
342 aShape = DBRep::Get(argv[2]);
343
973d410e 344 Quantity_ColorRGBA col;
7fd59977 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;
350 else
351 type = XCAFDoc_ColorGen;
352 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
353 if (localTool->GetInstanceColor( aShape, type, col) )
354 {
973d410e 355 if ((1 - col.Alpha()) < Precision::Confusion())
356 di << col.GetRGB().StringName(col.GetRGB().Name());
357 else
358 di << col.GetRGB().StringName(col.GetRGB().Name()) << " (" << col.Alpha() << ")";
7fd59977 359 }
360 return 0;
361}
362
363static Standard_Integer setStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
364{
365 if (argc<6) {
973d410e 366 di<<"Use: "<<argv[0]<<" Doc shape R G B [alpha] type(s/c)\n";
7fd59977 367 return 1;
368 }
369 Handle(TDocStd_Document) Doc;
370 DDocStd::GetDocument(argv[1], Doc);
586db386 371 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
7fd59977 372 TopoDS_Shape aShape;
373 aShape = DBRep::Get(argv[2]);
374
91322f44 375 Quantity_Color col ( Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atof(argv[5]), Quantity_TOC_RGB );
973d410e 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])));
380 }
381
382 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
383 XCAFDoc_ColorType ctype = XCAFDoc_ColorGen;
384 if (argc > 6) {
385 if (argv[argc - 1][0] == 's')
386 ctype = XCAFDoc_ColorSurf;
387 else if (argv[argc - 1][0] == 'c')
388 ctype = XCAFDoc_ColorCurv;
389 }
7fd59977 390 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
973d410e 391 if (!localTool->SetInstanceColor(aShape, ctype, aColRGBA))
7fd59977 392 {
586db386 393 di << "cannot set color for the indicated component\n";
7fd59977 394 return 1;
395 }
396 return 0;
397}
398
399//=======================================================================
400//function : InitCommands
401//purpose :
402//=======================================================================
403
404void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di)
405{
7fd59977 406 static Standard_Boolean initactor = Standard_False;
c48e2889 407 if (initactor)
408 {
409 return;
410 }
411 initactor = Standard_True;
7fd59977 412
413 //=====================================
414 // Work with colors
415 //=====================================
416
417 Standard_CString g = "XDE color's commands";
418
973d410e 419 di.Add ("XSetColor","Doc {Label|Shape} R G B [alpha] [c|s]\t: Set color [R G B] to shape given by Label, "
ec99ba32 420 "type of color 's' - for surface, 'c' - for curve (default generic)",
7fd59977 421 __FILE__, setColor, g);
422
423 di.Add ("XGetColor","Doc label \t: Return color defined on label in colortable",
424 __FILE__, getColor, g);
425
426 di.Add ("XGetShapeColor","Doc Label ColorType \t: Returns color defined by label",
427 __FILE__, getShapeColor, g);
428
429 di.Add ("XGetAllColors","Doc \t: Print all colors that defined in document",
430 __FILE__, getAllColors, g);
431
973d410e 432 di.Add ("XAddColor","Doc R G B [alpha]\t: Add color in document to color table",
7fd59977 433 __FILE__, addColor, g);
434
435 di.Add ("XRemoveColor","Doc Label \t: Remove color in document from color table",
436 __FILE__, removeColor, g);
437
973d410e 438 di.Add ("XFindColor","Doc R G B [alpha]\t: Find label where indicated color is situated",
7fd59977 439 __FILE__, findColor, g);
440
441 di.Add ("XUnsetColor","Doc {Label|Shape} ColorType \t: Unset color ",
442 __FILE__, unsetColor, g);
443
444 di.Add ("XSetObjVisibility","Doc {Label|Shape} (0\1) \t: Set the visibility of shape ",
445 __FILE__, setVisibility, g);
446
447 di.Add ("XGetObjVisibility","Doc {Label|Shape} \t: Return the visibility of shape ",
448 __FILE__, getVisibility, g);
449
450 di.Add ("XGetInstanceVisible","Doc Shape \t: Return the visibility of shape ",
451 __FILE__, getStyledVisibility, g);
452
453 di.Add ("XGetInstanceColor","Doc Shape \t: Return the color of component shape ",
454 __FILE__, getStyledcolor, g);
455
973d410e 456 di.Add ("XSetInstanceColor","Doc Shape R G B [alpha] type \t: sets color for component of shape if SHUO structure exists already ",
7fd59977 457 __FILE__, setStyledcolor, g);
458
459}