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 <ViewerTest.hxx>
30 #include <XCAFDoc_ColorTool.hxx>
31 #include <XCAFDoc_DocumentTool.hxx>
32 #include <XCAFDoc_ShapeTool.hxx>
33 #include <XDEDRAW_Colors.hxx>
35 //! Parse XCAFDoc_ColorType enumeration argument.
36 static bool parseXDocColorType (const TCollection_AsciiString& theArg,
37 XCAFDoc_ColorType& theType)
39 TCollection_AsciiString anArgCase (theArg);
40 anArgCase.LowerCase();
41 if (anArgCase == "surf"
42 || anArgCase == "surface"
45 theType = XCAFDoc_ColorSurf;
48 else if (anArgCase == "curve"
51 theType = XCAFDoc_ColorCurv;
54 else if (anArgCase == "gen"
55 || anArgCase == "generic")
57 theType = XCAFDoc_ColorGen;
63 //=======================================================================
64 // Section: Work with colors
65 //=======================================================================
66 static Standard_Integer setColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
70 std::cout << "Syntax error: wrong number of arguments\n";
74 Handle(TDocStd_Document) aDoc;
75 DDocStd::GetDocument (argv[1], aDoc);
78 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
84 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
87 aShape = DBRep::Get (argv[2]);
90 std::cout << "Syntax error: " << argv[2] << " is not a label nor shape\n";
95 Quantity_ColorRGBA aColor;
96 bool isColorDefined = false;
97 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
98 for (Standard_Integer anArgIter = 3; anArgIter < argc; ++anArgIter)
100 if (parseXDocColorType (argv[anArgIter], aColType))
104 else if (!isColorDefined)
106 isColorDefined = true;
107 Standard_Integer aNbParsed = ViewerTest::ParseColor (argc - anArgIter,
112 std::cout << "Syntax error at '" << argv[anArgIter] << "'\n";
115 anArgIter += aNbParsed - 1;
119 std::cout << "Syntax error at '" << argv[anArgIter] << "'\n";
125 std::cout << "Syntax error: wrong number of arguments\n";
129 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
130 if (!aLabel.IsNull())
132 aColorTool->SetColor (aLabel, aColor, aColType);
134 else if (!aColorTool->SetColor (aShape, aColor, aColType))
136 std::cout << "Syntax error: " << argv[2] << " is not a label nor shape\n";
142 static Standard_Integer getColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
146 std::cout << "Syntax error: wrong number of arguments\n";
150 Handle(TDocStd_Document) aDoc;
151 DDocStd::GetDocument (argv[1], aDoc);
154 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
159 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
160 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
161 Quantity_ColorRGBA aColor;
162 if (!myColors->GetColor (aLabel, aColor))
167 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
169 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
173 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
178 static Standard_Integer getShapeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
180 if (argc != 3 && argc != 4)
182 std::cout << "Syntax error: wrong number of arguments\n";
186 Handle(TDocStd_Document) aDoc;
187 DDocStd::GetDocument (argv[1], aDoc);
190 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
195 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
198 std::cout << "Syntax error: '" << argv[2] << "' label is not found in the document\n";
202 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
203 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
204 if (argc > 3 && !parseXDocColorType (argv[3], aColType))
206 std::cout << "Syntax error: unknown color type '" << argv[3] << "'\n";
210 Quantity_ColorRGBA aColor;
211 if (!myColors->GetColor (aLabel, aColType, aColor))
216 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
218 di << aColor.GetRGB().StringName(aColor.GetRGB().Name());
222 di << aColor.GetRGB().StringName(aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
228 static Standard_Integer getAllColors (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
232 std::cout << "Syntax error: wrong number of arguments\n";
236 Handle(TDocStd_Document) aDoc;
237 DDocStd::GetDocument (argv[1], aDoc);
240 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
244 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
245 TDF_LabelSequence aLabels;
246 aColorTool->GetColors (aLabels);
247 if (aLabels.Length() >= 1)
249 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next())
251 Quantity_ColorRGBA aColor;
252 if (!aColorTool->GetColor (aLabIter.Value(), aColor))
256 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
258 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
262 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
271 static Standard_Integer addColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
275 std::cout << "Syntax error: wrong number of arguments\n";
279 Handle(TDocStd_Document) aDoc;
280 DDocStd::GetDocument (argv[1], aDoc);
283 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
287 Quantity_ColorRGBA aColRGBA;
288 Standard_Integer aNbParsed = ViewerTest::ParseColor (argc - 2, argv + 2, aColRGBA);
289 if (aNbParsed != argc - 2)
291 std::cout << "Syntax error at '" << argv[2] << "'\n";
295 TCollection_AsciiString anEntry;
296 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
297 TDF_Label aLabel = aColorTool->AddColor (aColRGBA);
298 TDF_Tool::Entry (aLabel, anEntry);
303 static Standard_Integer removeColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
307 std::cout << "Syntax error: wrong number of arguments\n";
311 Handle(TDocStd_Document) aDoc;
313 DDocStd::GetDocument (argv[1], aDoc);
316 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
319 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
322 std::cout << "Syntax error: " << argv[2] << " label is not found in the document\n";
326 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
327 aColorTool->RemoveColor (aLabel);
331 static Standard_Integer findColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
335 std::cout << "Syntax error: wrong number of arguments\n";
339 Handle(TDocStd_Document) aDoc;
340 DDocStd::GetDocument (argv[1], aDoc);
343 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
347 Quantity_ColorRGBA aColRGBA;
348 Standard_Integer aNbParsed = ViewerTest::ParseColor (argc - 2, argv + 2, aColRGBA);
349 if (aNbParsed != argc - 2)
351 std::cout << "Syntax error at '" << argv[2] << "'\n";
355 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
356 TCollection_AsciiString anEntry;
357 TDF_Tool::Entry (aColorTool->FindColor (aColRGBA), anEntry);
362 static Standard_Integer unsetColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
366 std::cout << "Syntax error: wrong number of arguments\n";
370 Handle(TDocStd_Document) aDoc;
371 DDocStd::GetDocument (argv[1], aDoc);
374 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
378 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
379 if (!parseXDocColorType (argv[3], aColType))
381 std::cout << "Syntax error: unknown color type '" << argv[3] << "'\n";
386 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
387 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
388 if (!aLabel.IsNull())
390 myColors->UnSetColor (aLabel, aColType);
394 TopoDS_Shape aShape = DBRep::Get (argv[2]);
397 std::cout << "Syntax error: " << argv[2] << " is not a label nor shape\n";
400 myColors->UnSetColor (aShape, aColType);
404 static Standard_Integer setVisibility (Draw_Interpretor& , Standard_Integer argc, const char** argv)
406 if (argc != 3 && argc != 4)
408 std::cout << "Syntax error: wrong number of arguments\n";
412 Handle(TDocStd_Document) aDoc;
414 DDocStd::GetDocument (argv[1], aDoc);
417 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
421 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
422 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
425 // get label by shape
426 TopoDS_Shape aShape = DBRep::Get (argv[2]);
427 if (!aShape.IsNull())
429 aLabel = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
434 std::cout << "Syntax error: " << argv[2] << " is not a label not shape\n";
438 Standard_Boolean isVisible = Standard_False;
441 TCollection_AsciiString aVisArg (argv[3]);
444 isVisible = Standard_True;
446 else if (aVisArg == "0")
448 isVisible = Standard_False;
452 std::cout << "Syntax error: unknown argument '" << argv[3] << "'\n";
456 aColorTool->SetVisibility (aLabel, isVisible);
460 static Standard_Integer getVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
464 std::cout << "Syntax error: wrong number of arguments\n";
468 Handle(TDocStd_Document) aDoc;
469 DDocStd::GetDocument (argv[1], aDoc);
472 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
476 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
478 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
481 // get label by shape
482 TopoDS_Shape aShape = DBRep::Get (argv[2]);
483 if (!aShape.IsNull())
485 aLabel = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
490 std::cout << "Syntax error: " << argv[2] << " is not a label not shape\n";
494 di << (aColorTool->IsVisible (aLabel) ? 1 : 0);
498 static Standard_Integer getStyledVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
502 std::cout << "Syntax error: wrong number of arguments\n";
506 Handle(TDocStd_Document) aDoc;
507 DDocStd::GetDocument (argv[1], aDoc);
508 TopoDS_Shape aShape = DBRep::Get(argv[2]);
511 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
516 std::cout << "Syntax error: " << argv[2] << " is not a shape\n";
520 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
521 di << (aColorTool->IsInstanceVisible (aShape) ? 1 : 0);
525 static Standard_Integer getStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
527 if (argc != 3 && argc != 4)
529 std::cout << "Syntax error: wrong number of arguments\n";
533 Handle(TDocStd_Document) aDoc;
534 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
535 DDocStd::GetDocument (argv[1], aDoc);
536 TopoDS_Shape aShape = DBRep::Get (argv[2]);
539 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
544 std::cout << "Syntax error: " << argv[2] << " is not a shape\n";
547 if (argc > 3 && !parseXDocColorType (argv[3], aColType))
549 std::cout << "Syntax error: unknown color type '" << argv[3] << "'\n";
553 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
554 Quantity_ColorRGBA aColor;
555 if (aColorTool->GetInstanceColor (aShape, aColType, aColor))
557 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
559 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
563 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
569 static Standard_Integer setStyledcolor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
573 std::cout << "Syntax error: wrong number of arguments\n";
577 Handle(TDocStd_Document) aDoc;
578 DDocStd::GetDocument (argv[1], aDoc);
581 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
585 TopoDS_Shape aShape = DBRep::Get (argv[2]);
588 std::cout << "Syntax error: " << argv[2] << " is not a shape\n";
592 XCAFDoc_ColorType aColorType = XCAFDoc_ColorGen;
593 Quantity_ColorRGBA aColRGBA;
594 for (Standard_Integer anArgIter = 3; anArgIter < argc; ++anArgIter)
596 if (parseXDocColorType (argv[anArgIter], aColorType))
602 Standard_Integer aNbParsed = ViewerTest::ParseColor (argc - anArgIter,
607 std::cout << "Syntax error at '" << argv[anArgIter] << "'\n";
610 anArgIter += aNbParsed - 1;
614 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
615 if (!aColorTool->SetInstanceColor (aShape, aColorType, aColRGBA))
617 std::cout << "Error: cannot set color for the indicated component\n";
623 //=======================================================================
624 //function : InitCommands
626 //=======================================================================
628 void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di)
630 static Standard_Boolean initactor = Standard_False;
635 initactor = Standard_True;
637 //=====================================
639 //=====================================
641 Standard_CString g = "XDE color's commands";
643 di.Add ("XSetColor","Doc {Label|Shape} R G B [alpha] [{generic|surface|curve}=gen]"
644 "\t: Set color [R G B] to shape given by Label, "
645 "type of color 's' - for surface, 'c' - for curve (default generic)",
646 __FILE__, setColor, g);
648 di.Add ("XGetColor","Doc label"
649 "\t: Return color defined on label in colortable",
650 __FILE__, getColor, g);
652 di.Add ("XGetShapeColor","Doc Label {generic|surface|curve}"
653 "\t: Returns color defined by label",
654 __FILE__, getShapeColor, g);
656 di.Add ("XGetAllColors","Doc"
657 "\t: Print all colors that defined in document",
658 __FILE__, getAllColors, g);
660 di.Add ("XAddColor","Doc R G B [alpha]"
661 "\t: Add color in document to color table",
662 __FILE__, addColor, g);
664 di.Add ("XRemoveColor","Doc Label"
665 "\t: Remove color in document from color table",
666 __FILE__, removeColor, g);
668 di.Add ("XFindColor","Doc R G B [alpha]"
669 "\t: Find label where indicated color is situated",
670 __FILE__, findColor, g);
672 di.Add ("XUnsetColor","Doc {Label|Shape} {generic|surface|curve}"
674 __FILE__, unsetColor, g);
676 di.Add ("XSetObjVisibility","Doc {Label|Shape} (0\1) \t: Set the visibility of shape ",
677 __FILE__, setVisibility, g);
679 di.Add ("XGetObjVisibility","Doc {Label|Shape} \t: Return the visibility of shape ",
680 __FILE__, getVisibility, g);
682 di.Add ("XGetInstanceVisible","Doc Shape \t: Return the visibility of shape ",
683 __FILE__, getStyledVisibility, g);
685 di.Add ("XGetInstanceColor","Doc Shape [{generic|surface|curve}=gen]"
686 "\t: Return the color of component shape",
687 __FILE__, getStyledcolor, g);
689 di.Add ("XSetInstanceColor","Doc Shape R G B [alpha] [{generic|surface|curve}=gen]"
690 "\t: sets color for component of shape if SHUO structure exists already",
691 __FILE__, setStyledcolor, g);