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 <OSD_File.hxx>
24 #include <TCollection_AsciiString.hxx>
25 #include <TDF_Label.hxx>
26 #include <TDF_LabelSequence.hxx>
27 #include <TDF_Tool.hxx>
28 #include <TDataStd_Name.hxx>
29 #include <TDocStd_Document.hxx>
30 #include <TopoDS_Shape.hxx>
31 #include <ViewerTest.hxx>
32 #include <XCAFDoc_ColorTool.hxx>
33 #include <XCAFDoc_DocumentTool.hxx>
34 #include <XCAFDoc_ShapeTool.hxx>
35 #include <XCAFDoc_VisMaterial.hxx>
36 #include <XCAFDoc_VisMaterialTool.hxx>
37 #include <XDEDRAW_Colors.hxx>
39 //! Parse XCAFDoc_ColorType enumeration argument.
40 static bool parseXDocColorType (const TCollection_AsciiString& theArg,
41 XCAFDoc_ColorType& theType)
43 TCollection_AsciiString anArgCase (theArg);
44 anArgCase.LowerCase();
45 if (anArgCase == "surf"
46 || anArgCase == "surface"
49 theType = XCAFDoc_ColorSurf;
52 else if (anArgCase == "curve"
55 theType = XCAFDoc_ColorCurv;
58 else if (anArgCase == "gen"
59 || anArgCase == "generic")
61 theType = XCAFDoc_ColorGen;
67 //! Print triplet of values.
68 template<class S, class T> static S& operator<< (S& theStream, const NCollection_Vec3<T>& theVec)
70 theStream << theVec[0] << " " << theVec[1] << " " << theVec[2];
75 template<class S, class T> static S& operator<< (S& theStream, const NCollection_Vec4<T>& theVec)
77 theStream << theVec[0] << " " << theVec[1] << " " << theVec[2] << " " << theVec[3];
81 //! Convert alpha mode into string.
82 static const char* alphaModeToString (Graphic3d_AlphaMode theMode)
86 case Graphic3d_AlphaMode_Opaque: return "Opaque";
87 case Graphic3d_AlphaMode_Mask: return "Mask";
88 case Graphic3d_AlphaMode_Blend: return "Blend";
89 case Graphic3d_AlphaMode_BlendAuto: return "BlendAuto";
94 //! Find existing visualization material in the document.
95 static TDF_Label findVisMaterial (const Handle(TDocStd_Document)& theDoc,
96 const TCollection_AsciiString& theKey)
98 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (theDoc->Main());
100 TDF_Tool::Label (theDoc->GetData(), theKey, aMatLab);
101 if (!aMatLab.IsNull())
103 return aMatTool->IsMaterial (aMatLab) ? aMatLab : TDF_Label();
106 TDF_LabelSequence aLabels;
107 aMatTool->GetMaterials (aLabels);
108 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next())
110 Handle(TDataStd_Name) aNodeName;
111 if (aLabIter.Value().FindAttribute (TDataStd_Name::GetID(), aNodeName)
112 && aNodeName->Get().IsEqual (theKey))
114 return aLabIter.Value();
120 //! Check if image file exists.
121 static bool isImageFileExist (const TCollection_AsciiString& thePath)
123 const OSD_Path aPath (thePath);
124 if (!OSD_File (aPath).Exists())
126 std::cout << "Error: file '" << thePath << " not found\n";
132 //! Parse RGB values coming after specified argument.
133 static bool parseRgbColor (Standard_Integer& theArgIter,
134 Quantity_Color& theColor,
135 Standard_Integer theNbArgs,
136 const char** theArgVec)
138 Standard_Integer aNbParsed = ViewerTest::ParseColor (theNbArgs - theArgIter - 1,
139 theArgVec + theArgIter + 1,
143 std::cout << "Syntax error at '" << theArgVec[theArgIter] << "'\n";
146 theArgIter += aNbParsed;
150 //! Parse normalized real value within 0..1 range.
151 static bool parseNormalizedReal (const char* theString,
152 Standard_ShortReal& theValue)
154 theValue = (Standard_ShortReal )Draw::Atof (theString);
155 if (theValue < 0.0f || theValue > 1.0f)
157 std::cerr << "Syntax error at '" << theString << "'\n";
163 //=======================================================================
164 // Section: Work with colors
165 //=======================================================================
166 static Standard_Integer setColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
170 std::cout << "Syntax error: wrong number of arguments\n";
174 Handle(TDocStd_Document) aDoc;
175 DDocStd::GetDocument (argv[1], aDoc);
178 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
184 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
187 aShape = DBRep::Get (argv[2]);
190 std::cout << "Syntax error: " << argv[2] << " is not a label nor shape\n";
195 Quantity_ColorRGBA aColor;
196 bool isColorDefined = false;
197 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
198 for (Standard_Integer anArgIter = 3; anArgIter < argc; ++anArgIter)
200 if (parseXDocColorType (argv[anArgIter], aColType))
204 else if (!isColorDefined)
206 isColorDefined = true;
207 Standard_Integer aNbParsed = ViewerTest::ParseColor (argc - anArgIter,
212 std::cout << "Syntax error at '" << argv[anArgIter] << "'\n";
215 anArgIter += aNbParsed - 1;
219 std::cout << "Syntax error at '" << argv[anArgIter] << "'\n";
225 std::cout << "Syntax error: wrong number of arguments\n";
229 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
230 if (!aLabel.IsNull())
232 aColorTool->SetColor (aLabel, aColor, aColType);
234 else if (!aColorTool->SetColor (aShape, aColor, aColType))
236 std::cout << "Syntax error: " << argv[2] << " is not a label nor shape\n";
242 static Standard_Integer getColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
246 std::cout << "Syntax error: wrong number of arguments\n";
250 Handle(TDocStd_Document) aDoc;
251 DDocStd::GetDocument (argv[1], aDoc);
254 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
259 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
260 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
261 Quantity_ColorRGBA aColor;
262 if (!myColors->GetColor (aLabel, aColor))
267 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
269 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
273 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
278 static Standard_Integer getShapeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
280 if (argc != 3 && argc != 4)
282 std::cout << "Syntax error: wrong number of arguments\n";
286 Handle(TDocStd_Document) aDoc;
287 DDocStd::GetDocument (argv[1], aDoc);
290 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
295 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
298 std::cout << "Syntax error: '" << argv[2] << "' label is not found in the document\n";
302 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
303 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
304 if (argc > 3 && !parseXDocColorType (argv[3], aColType))
306 std::cout << "Syntax error: unknown color type '" << argv[3] << "'\n";
310 Quantity_ColorRGBA aColor;
311 if (!myColors->GetColor (aLabel, aColType, aColor))
316 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
318 di << aColor.GetRGB().StringName(aColor.GetRGB().Name());
322 di << aColor.GetRGB().StringName(aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
328 static Standard_Integer getAllColors (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
332 std::cout << "Syntax error: wrong number of arguments\n";
336 Handle(TDocStd_Document) aDoc;
337 DDocStd::GetDocument (argv[1], aDoc);
340 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
344 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
345 TDF_LabelSequence aLabels;
346 aColorTool->GetColors (aLabels);
347 if (aLabels.Length() >= 1)
349 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next())
351 Quantity_ColorRGBA aColor;
352 if (!aColorTool->GetColor (aLabIter.Value(), aColor))
356 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
358 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
362 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
370 static Standard_Integer addColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
374 std::cout << "Syntax error: wrong number of arguments\n";
378 Handle(TDocStd_Document) aDoc;
379 DDocStd::GetDocument (argv[1], aDoc);
382 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
386 Quantity_ColorRGBA aColRGBA;
387 Standard_Integer aNbParsed = ViewerTest::ParseColor (argc - 2, argv + 2, aColRGBA);
388 if (aNbParsed != argc - 2)
390 std::cout << "Syntax error at '" << argv[2] << "'\n";
394 TCollection_AsciiString anEntry;
395 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
396 TDF_Label aLabel = aColorTool->AddColor (aColRGBA);
397 TDF_Tool::Entry (aLabel, anEntry);
402 static Standard_Integer removeColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
406 std::cout << "Syntax error: wrong number of arguments\n";
410 Handle(TDocStd_Document) aDoc;
412 DDocStd::GetDocument (argv[1], aDoc);
415 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
418 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
421 std::cout << "Syntax error: " << argv[2] << " label is not found in the document\n";
425 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
426 aColorTool->RemoveColor (aLabel);
430 static Standard_Integer findColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
434 std::cout << "Syntax error: wrong number of arguments\n";
438 Handle(TDocStd_Document) aDoc;
439 DDocStd::GetDocument (argv[1], aDoc);
442 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
446 Quantity_ColorRGBA aColRGBA;
447 Standard_Integer aNbParsed = ViewerTest::ParseColor (argc - 2, argv + 2, aColRGBA);
448 if (aNbParsed != argc - 2)
450 std::cout << "Syntax error at '" << argv[2] << "'\n";
454 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
455 TCollection_AsciiString anEntry;
456 TDF_Tool::Entry (aColorTool->FindColor (aColRGBA), anEntry);
461 static Standard_Integer unsetColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
465 std::cout << "Syntax error: wrong number of arguments\n";
469 Handle(TDocStd_Document) aDoc;
470 DDocStd::GetDocument (argv[1], aDoc);
473 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
477 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
478 if (!parseXDocColorType (argv[3], aColType))
480 std::cout << "Syntax error: unknown color type '" << argv[3] << "'\n";
485 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
486 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
487 if (!aLabel.IsNull())
489 myColors->UnSetColor (aLabel, aColType);
493 TopoDS_Shape aShape = DBRep::Get (argv[2]);
496 std::cout << "Syntax error: " << argv[2] << " is not a label nor shape\n";
499 myColors->UnSetColor (aShape, aColType);
503 static Standard_Integer setVisibility (Draw_Interpretor& , Standard_Integer argc, const char** argv)
505 if (argc != 3 && argc != 4)
507 std::cout << "Syntax error: wrong number of arguments\n";
511 Handle(TDocStd_Document) aDoc;
513 DDocStd::GetDocument (argv[1], aDoc);
516 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
520 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
521 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
524 // get label by shape
525 TopoDS_Shape aShape = DBRep::Get (argv[2]);
526 if (!aShape.IsNull())
528 aLabel = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
533 std::cout << "Syntax error: " << argv[2] << " is not a label not shape\n";
537 Standard_Boolean isVisible = Standard_False;
540 TCollection_AsciiString aVisArg (argv[3]);
543 isVisible = Standard_True;
545 else if (aVisArg == "0")
547 isVisible = Standard_False;
551 std::cout << "Syntax error: unknown argument '" << argv[3] << "'\n";
555 aColorTool->SetVisibility (aLabel, isVisible);
559 static Standard_Integer getVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
563 std::cout << "Syntax error: wrong number of arguments\n";
567 Handle(TDocStd_Document) aDoc;
568 DDocStd::GetDocument (argv[1], aDoc);
571 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
575 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
577 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
580 // get label by shape
581 TopoDS_Shape aShape = DBRep::Get (argv[2]);
582 if (!aShape.IsNull())
584 aLabel = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
589 std::cout << "Syntax error: " << argv[2] << " is not a label not shape\n";
593 di << (aColorTool->IsVisible (aLabel) ? 1 : 0);
597 static Standard_Integer getStyledVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
601 std::cout << "Syntax error: wrong number of arguments\n";
605 Handle(TDocStd_Document) aDoc;
606 DDocStd::GetDocument (argv[1], aDoc);
607 TopoDS_Shape aShape = DBRep::Get(argv[2]);
610 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
615 std::cout << "Syntax error: " << argv[2] << " is not a shape\n";
619 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
620 di << (aColorTool->IsInstanceVisible (aShape) ? 1 : 0);
624 static Standard_Integer getStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
626 if (argc != 3 && argc != 4)
628 std::cout << "Syntax error: wrong number of arguments\n";
632 Handle(TDocStd_Document) aDoc;
633 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
634 DDocStd::GetDocument (argv[1], aDoc);
635 TopoDS_Shape aShape = DBRep::Get (argv[2]);
638 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
643 std::cout << "Syntax error: " << argv[2] << " is not a shape\n";
646 if (argc > 3 && !parseXDocColorType (argv[3], aColType))
648 std::cout << "Syntax error: unknown color type '" << argv[3] << "'\n";
652 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
653 Quantity_ColorRGBA aColor;
654 if (aColorTool->GetInstanceColor (aShape, aColType, aColor))
656 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
658 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
662 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
668 static Standard_Integer setStyledcolor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
672 std::cout << "Syntax error: wrong number of arguments\n";
676 Handle(TDocStd_Document) aDoc;
677 DDocStd::GetDocument (argv[1], aDoc);
680 std::cout << "Syntax error: " << argv[1] << " is not a document\n";
684 TopoDS_Shape aShape = DBRep::Get (argv[2]);
687 std::cout << "Syntax error: " << argv[2] << " is not a shape\n";
691 XCAFDoc_ColorType aColorType = XCAFDoc_ColorGen;
692 Quantity_ColorRGBA aColRGBA;
693 for (Standard_Integer anArgIter = 3; anArgIter < argc; ++anArgIter)
695 if (parseXDocColorType (argv[anArgIter], aColorType))
701 Standard_Integer aNbParsed = ViewerTest::ParseColor (argc - anArgIter,
706 std::cout << "Syntax error at '" << argv[anArgIter] << "'\n";
709 anArgIter += aNbParsed - 1;
713 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
714 if (!aColorTool->SetInstanceColor (aShape, aColorType, aColRGBA))
716 std::cout << "Error: cannot set color for the indicated component\n";
722 // ================================================================
723 // Function : XGetAllVisMaterials
725 // ================================================================
726 static Standard_Integer XGetAllVisMaterials (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgVec)
728 if (theNbArgs != 2 && theNbArgs != 3)
730 std::cout << "Syntax error: wrong number of arguments\n";
734 Handle(TDocStd_Document) aDoc;
735 DDocStd::GetDocument (theArgVec[1], aDoc);
738 std::cout << "Syntax error: " << theArgVec[1] << " is not a document\n";
742 bool toPrintNames = true;
745 TCollection_AsciiString anArgCase (theArgVec[2]);
746 anArgCase.LowerCase();
747 if (anArgCase == "-names")
751 else if (anArgCase == "-labels")
753 toPrintNames = false;
757 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
758 TDF_LabelSequence aLabels;
759 aMatTool->GetMaterials (aLabels);
760 Standard_Integer aMatIndex = 1;
761 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next(), ++aMatIndex)
763 const TDF_Label& aMatLab = aLabIter.Value();
766 TCollection_AsciiString anEntryId;
767 TDF_Tool::Entry (aMatLab, anEntryId);
768 theDI << anEntryId << " ";
772 Handle(TDataStd_Name) aNodeName;
773 if (aMatLab.FindAttribute (TDataStd_Name::GetID(), aNodeName))
775 theDI << aNodeName->Get() << " ";
779 TCollection_AsciiString aName = TCollection_AsciiString("<UNNAMED") + aMatIndex + ">";
780 theDI << aName << " ";
786 // ================================================================
787 // Function : XGetVisMaterial
789 // ================================================================
790 static Standard_Integer XGetVisMaterial (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgVec)
794 std::cout << "Syntax error: wrong number of arguments\n";
798 Handle(TDocStd_Document) aDoc;
799 DDocStd::GetDocument (theArgVec[1], aDoc);
802 std::cout << "Syntax error: " << theArgVec[1] << " is not a document\n";
806 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
807 Handle(XCAFDoc_VisMaterial) aMat;
808 TDF_Label aMatLab = findVisMaterial (aDoc, theArgVec[2]);
809 if (!aMatLab.IsNull())
811 aMat = aMatTool->GetMaterial (aMatLab);
816 TDF_Tool::Label (aDoc->GetData(), theArgVec[2], aShapeLab);
817 if (aShapeLab.IsNull())
819 TopoDS_Shape aShape = DBRep::Get (theArgVec[2]);
820 if (!aShape.IsNull())
822 aShapeLab = aMatTool->ShapeTool()->FindShape (aShape);
825 if (!aShapeLab.IsNull()
826 && !aMatTool->ShapeTool()->IsShape (aShapeLab))
830 if (aShapeLab.IsNull())
832 std::cout << "Syntax error: " << theArgVec[2] << " is not material nor shape\n";
836 aMat = aMatTool->GetShapeMaterial (aShapeLab);
845 TCollection_AsciiString anEntryId;
846 TDF_Tool::Entry (aMat->Label(), anEntryId);
847 theDI << "Label: " << anEntryId << "\n";
849 Handle(TDataStd_Name) aNodeName;
850 if (aMat->Label().FindAttribute (TDataStd_Name::GetID(), aNodeName))
852 theDI << "Name: " << aNodeName->Get() << "\n";
859 theDI << "AlphaMode: " << alphaModeToString (aMat->AlphaMode()) << "\n";
860 theDI << "AlphaCutOff: " << aMat->AlphaCutOff() << "\n";
861 theDI << "IsDoubleSided: " << aMat->IsDoubleSided() << "\n";
862 if (aMat->HasCommonMaterial())
864 const XCAFDoc_VisMaterialCommon& aMatCom = aMat->CommonMaterial();
865 theDI << "Common.Ambient: " << (Graphic3d_Vec3 )aMatCom.AmbientColor << "\n";
866 theDI << "Common.Diffuse: " << (Graphic3d_Vec3 )aMatCom.DiffuseColor << "\n";
867 if (!aMatCom.DiffuseTexture.IsNull())
869 theDI << "Common.DiffuseTexture: " << aMatCom.DiffuseTexture->TextureId() << "\n";
871 theDI << "Common.Specular: " << (Graphic3d_Vec3 )aMatCom.SpecularColor << "\n";
872 theDI << "Common.Emissive: " << (Graphic3d_Vec3 )aMatCom.EmissiveColor << "\n";
873 theDI << "Common.Shininess: " << aMatCom.Shininess << "\n";
874 theDI << "Common.Transparency: " << aMatCom.Transparency << "\n";
876 if (aMat->HasPbrMaterial())
878 const XCAFDoc_VisMaterialPBR& aMatPbr = aMat->PbrMaterial();
879 theDI << "PBR.BaseColor: " << (Graphic3d_Vec3 )aMatPbr.BaseColor.GetRGB() << "\n";
880 theDI << "PBR.Transparency: " << (1.0 - aMatPbr.BaseColor.Alpha()) << "\n";
881 theDI << "PBR.RefractionIndex: " << aMatPbr.RefractionIndex << "\n";
882 if (!aMatPbr.BaseColorTexture.IsNull())
884 theDI << "PBR.BaseColorTexture: " << aMatPbr.BaseColorTexture->TextureId() << "\n";
886 theDI << "PBR.EmissiveFactor: " << aMatPbr.EmissiveFactor << "\n";
887 if (!aMatPbr.EmissiveTexture.IsNull())
889 theDI << "PBR.EmissiveTexture: " << aMatPbr.EmissiveTexture->TextureId() << "\n";
891 theDI << "PBR.Metallic: " << aMatPbr.Metallic << "\n";
892 theDI << "PBR.Roughness: " << aMatPbr.Roughness << "\n";
893 if (!aMatPbr.MetallicRoughnessTexture.IsNull())
895 theDI << "PBR.MetallicRoughnessTexture: " << aMatPbr.MetallicRoughnessTexture->TextureId() << "\n";
897 if (!aMatPbr.OcclusionTexture.IsNull())
899 theDI << "PBR.OcclusionTexture: " << aMatPbr.OcclusionTexture->TextureId() << "\n";
901 if (!aMatPbr.NormalTexture.IsNull())
903 theDI << "PBR.NormalTexture: " << aMatPbr.NormalTexture->TextureId() << "\n";
909 // ================================================================
910 // Function : XAddVisMaterial
912 // ================================================================
913 static Standard_Integer XAddVisMaterial (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
917 std::cout << "Syntax error: wrong number of arguments\n";
921 Handle(TDocStd_Document) aDoc;
922 DDocStd::GetDocument (theArgVec[1], aDoc);
925 std::cout << "Syntax error: " << theArgVec[1] << " is not a document\n";
929 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
930 TDF_Label aMatLab = findVisMaterial (aDoc, theArgVec[2]);
931 if (aMatLab.IsNull())
933 aMatLab = aMatTool->AddMaterial (theArgVec[2]);
936 Handle(XCAFDoc_VisMaterial) aMat = aMatTool->GetMaterial (aMatLab);
937 XCAFDoc_VisMaterialCommon aMatCom = aMat->CommonMaterial();
938 XCAFDoc_VisMaterialPBR aMatPbr = aMat->PbrMaterial();
939 Standard_ShortReal aRealValue = 0.0f;
940 for (Standard_Integer anArgIter = 3; anArgIter < theNbArgs; ++anArgIter)
942 TCollection_AsciiString anArg (theArgVec[anArgIter]);
944 if ((anArg == "-transparency"
945 || anArg == "-alpha")
946 && anArgIter + 1 < theNbArgs
947 && parseNormalizedReal (theArgVec[anArgIter + 1], aMatCom.Transparency))
950 if (anArg == "-alpha")
952 aMatCom.Transparency = 1.0f - aMatCom.Transparency;
954 aMatPbr.BaseColor.SetAlpha (1.0f - aMatCom.Transparency);
956 else if (anArgIter + 1 < theNbArgs
957 && (anArg == "-refractionindex" || anArg == "-ior"))
959 aMatPbr.RefractionIndex = (Standard_ShortReal )Draw::Atof (theArgVec[anArgIter + 1]);
960 if (aMatPbr.RefractionIndex < 1.0f || aMatPbr.RefractionIndex > 3.0f)
962 std::cout << "Syntax error at '" << anArg << "'\n";
967 aMatPbr.IsDefined = true;
969 else if (anArg == "-alphaMode"
970 && anArgIter + 2 < theNbArgs
971 && parseNormalizedReal (theArgVec[anArgIter + 2], aRealValue))
973 TCollection_AsciiString aModeStr (theArgVec[anArgIter + 1]);
974 aModeStr.LowerCase();
975 Graphic3d_AlphaMode anAlphaMode = Graphic3d_AlphaMode_Opaque;
976 if (aModeStr == "opaque")
978 anAlphaMode = Graphic3d_AlphaMode_Opaque;
980 else if (aModeStr == "mask")
982 anAlphaMode = Graphic3d_AlphaMode_Mask;
984 else if (aModeStr == "blend")
986 anAlphaMode = Graphic3d_AlphaMode_Blend;
988 else if (aModeStr == "blendauto")
990 anAlphaMode = Graphic3d_AlphaMode_BlendAuto;
994 std::cerr << "Syntax error at '" << anArg << "'\n";
997 aMat->SetAlphaMode (anAlphaMode, aRealValue);
1000 else if (anArg == "-diffuse"
1001 || anArg == "-basecolor"
1002 || anArg == "-albedo")
1004 Quantity_ColorRGBA aColorRGBA;
1005 Standard_Integer aNbParsed = ViewerTest::ParseColor (theNbArgs - anArgIter - 1,
1006 theArgVec + anArgIter + 1,
1010 std::cout << "Syntax error at '" << theArgVec[anArgIter] << "'\n";
1013 anArgIter += aNbParsed;
1015 if (anArg == "-diffuse")
1017 aMatCom.IsDefined = true;
1018 aMatCom.DiffuseColor = aColorRGBA.GetRGB();
1019 if (aNbParsed == 2 || aNbParsed == 4)
1021 aMatCom.Transparency = 1.0f - aColorRGBA.Alpha();
1026 aMatPbr.IsDefined = true;
1027 if (aNbParsed == 2 || aNbParsed == 4)
1029 aMatPbr.BaseColor = aColorRGBA;
1033 aMatPbr.BaseColor.SetRGB (aColorRGBA.GetRGB());
1037 else if (anArg == "-specular"
1038 && parseRgbColor (anArgIter, aMatCom.SpecularColor,
1039 theNbArgs, theArgVec))
1041 aMatCom.IsDefined = true;
1043 else if (anArg == "-ambient"
1044 && parseRgbColor (anArgIter, aMatCom.AmbientColor,
1045 theNbArgs, theArgVec))
1047 aMatCom.IsDefined = true;
1049 else if (anArg == "-emissive"
1050 && parseRgbColor (anArgIter, aMatCom.EmissiveColor,
1051 theNbArgs, theArgVec))
1053 aMatCom.IsDefined = true;
1055 else if (anArg == "-shininess"
1056 && anArgIter + 1 < theNbArgs)
1058 aMatCom.IsDefined = true;
1059 aMatCom.Shininess = (float )Draw::Atof (theArgVec[++anArgIter]);
1060 if (aMatCom.Shininess < 0.0f || aMatCom.Shininess > 1.0f)
1062 std::cout << "Syntax error at '" << anArg << "'\n";
1066 else if (anArgIter + 1 < theNbArgs
1067 && anArg == "-diffusetexture"
1068 && isImageFileExist (theArgVec[anArgIter + 1]))
1070 aMatCom.IsDefined = true;
1071 aMatCom.DiffuseTexture = new Image_Texture (theArgVec[++anArgIter]);
1073 else if (anArgIter + 1 < theNbArgs
1074 && anArg == "-basecolortexture"
1075 && isImageFileExist (theArgVec[anArgIter + 1]))
1077 aMatPbr.IsDefined = true;
1078 aMatPbr.BaseColorTexture = new Image_Texture (theArgVec[++anArgIter]);
1080 else if (anArgIter + 1 < theNbArgs
1081 && anArg == "-emissivetexture"
1082 && isImageFileExist (theArgVec[anArgIter + 1]))
1084 aMatPbr.IsDefined = true;
1085 aMatPbr.EmissiveTexture = new Image_Texture (theArgVec[++anArgIter]);
1087 else if (anArgIter + 1 < theNbArgs
1088 && anArg == "-metallicroughnesstexture"
1089 && isImageFileExist (theArgVec[anArgIter + 1]))
1091 aMatPbr.IsDefined = true;
1092 aMatPbr.MetallicRoughnessTexture = new Image_Texture (theArgVec[++anArgIter]);
1094 else if (anArgIter + 1 < theNbArgs
1095 && anArg == "-normaltexture"
1096 && isImageFileExist (theArgVec[anArgIter + 1]))
1098 aMatPbr.IsDefined = true;
1099 aMatPbr.NormalTexture = new Image_Texture (theArgVec[++anArgIter]);
1101 else if (anArgIter + 1 < theNbArgs
1102 && anArg == "-occlusiontexture"
1103 && isImageFileExist (theArgVec[anArgIter + 1]))
1105 aMatPbr.IsDefined = true;
1106 aMatPbr.OcclusionTexture = new Image_Texture (theArgVec[++anArgIter]);
1108 else if (anArg == "-emissivefactor"
1109 && anArgIter + 4 < theNbArgs)
1111 aMatPbr.IsDefined = true;
1112 aMatPbr.EmissiveFactor.SetValues ((float )Draw::Atof (theArgVec[anArgIter + 1]),
1113 (float )Draw::Atof (theArgVec[anArgIter + 2]),
1114 (float )Draw::Atof (theArgVec[anArgIter + 3]));
1117 else if (anArg == "-doublesided")
1119 aMatPbr.IsDefined = true;
1120 bool isDoubleSided = true;
1121 if (anArgIter + 1 < theNbArgs
1122 && ViewerTest::ParseOnOff (theArgVec[anArgIter + 1], isDoubleSided))
1126 aMat->SetDoubleSided (isDoubleSided);
1128 else if (anArgIter + 1 < theNbArgs
1129 && anArg == "-metallic"
1130 && parseNormalizedReal (theArgVec[anArgIter + 1], aMatPbr.Metallic))
1133 aMatPbr.IsDefined = true;
1135 else if (anArgIter + 1 < theNbArgs
1136 && anArg == "-roughness"
1137 && parseNormalizedReal (theArgVec[anArgIter + 1], aMatPbr.Roughness))
1140 aMatPbr.IsDefined = true;
1144 std::cout << "Syntax error at '" << theArgVec[anArgIter] << "'\n";
1149 aMat->SetCommonMaterial (aMatCom);
1150 aMat->SetPbrMaterial (aMatPbr);
1154 // ================================================================
1155 // Function : XRemoveVisMaterial
1157 // ================================================================
1158 static Standard_Integer XRemoveVisMaterial (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
1162 std::cout << "Syntax error: wrong number of arguments\n";
1166 Handle(TDocStd_Document) aDoc;
1167 DDocStd::GetDocument (theArgVec[1], aDoc);
1170 std::cout << "Syntax error: " << theArgVec[1] << " is not a document\n";
1174 TDF_Label aMatLab = findVisMaterial (aDoc, theArgVec[2]);
1175 if (aMatLab.IsNull())
1177 std::cout << "Syntax error: " << theArgVec[2] << " is not a material\n";
1181 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
1182 aMatTool->RemoveMaterial (aMatLab);
1186 // ================================================================
1187 // Function : XSetVisMaterial
1189 // ================================================================
1190 static Standard_Integer XSetVisMaterial (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
1192 if (theNbArgs != 3 && theNbArgs != 4)
1194 std::cout << "Syntax error: wrong number of arguments\n";
1198 Handle(TDocStd_Document) aDoc;
1199 TDF_Label aShapeLab;
1200 DDocStd::GetDocument (theArgVec[1], aDoc);
1203 std::cout << "Syntax error: " << theArgVec[1] << " is not a document\n";
1207 TDF_Tool::Label (aDoc->GetData(), theArgVec[2], aShapeLab);
1208 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
1209 if (aShapeLab.IsNull())
1211 // get label by shape
1212 TopoDS_Shape aShape = DBRep::Get (theArgVec[2]);
1213 if (!aShape.IsNull())
1215 aShapeLab = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
1218 if (aShapeLab.IsNull())
1220 std::cout << "Syntax error: " << theArgVec[2] << " is not a label not shape\n";
1227 aMatLab = findVisMaterial (aDoc, theArgVec[3]);
1228 if (aMatLab.IsNull())
1230 std::cout << "Syntax error: " << theArgVec[3] << " is not a material\n";
1235 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
1236 aMatTool->SetShapeMaterial (aShapeLab, aMatLab);
1240 //=======================================================================
1241 //function : InitCommands
1243 //=======================================================================
1245 void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di)
1247 static Standard_Boolean initactor = Standard_False;
1252 initactor = Standard_True;
1254 //=====================================
1256 //=====================================
1258 Standard_CString g = "XDE color's commands";
1260 di.Add ("XSetColor","Doc {Label|Shape} R G B [alpha] [{generic|surface|curve}=gen]"
1261 "\t: Set color [R G B] to shape given by Label, "
1262 "type of color 's' - for surface, 'c' - for curve (default generic)",
1263 __FILE__, setColor, g);
1265 di.Add ("XGetColor","Doc label"
1266 "\t: Return color defined on label in colortable",
1267 __FILE__, getColor, g);
1269 di.Add ("XGetShapeColor","Doc Label {generic|surface|curve}"
1270 "\t: Returns color defined by label",
1271 __FILE__, getShapeColor, g);
1273 di.Add ("XGetAllColors","Doc"
1274 "\t: Print all colors that defined in document",
1275 __FILE__, getAllColors, g);
1277 di.Add ("XAddColor","Doc R G B [alpha]"
1278 "\t: Add color in document to color table",
1279 __FILE__, addColor, g);
1281 di.Add ("XRemoveColor","Doc Label"
1282 "\t: Remove color in document from color table",
1283 __FILE__, removeColor, g);
1285 di.Add ("XFindColor","Doc R G B [alpha]"
1286 "\t: Find label where indicated color is situated",
1287 __FILE__, findColor, g);
1289 di.Add ("XUnsetColor","Doc {Label|Shape} {generic|surface|curve}"
1291 __FILE__, unsetColor, g);
1293 di.Add ("XSetObjVisibility","Doc {Label|Shape} (0\1) \t: Set the visibility of shape ",
1294 __FILE__, setVisibility, g);
1296 di.Add ("XGetObjVisibility","Doc {Label|Shape} \t: Return the visibility of shape ",
1297 __FILE__, getVisibility, g);
1299 di.Add ("XGetInstanceVisible","Doc Shape \t: Return the visibility of shape ",
1300 __FILE__, getStyledVisibility, g);
1302 di.Add ("XGetInstanceColor","Doc Shape [{generic|surface|curve}=gen]"
1303 "\t: Return the color of component shape",
1304 __FILE__, getStyledcolor, g);
1306 di.Add ("XSetInstanceColor","Doc Shape R G B [alpha] [{generic|surface|curve}=gen]"
1307 "\t: sets color for component of shape if SHUO structure exists already",
1308 __FILE__, setStyledcolor, g);
1310 di.Add ("XGetAllVisMaterials","Doc [{-names|-labels}=-names]"
1311 "\t: Print all visualization materials defined in document",
1312 __FILE__, XGetAllVisMaterials, g);
1313 di.Add ("XGetVisMaterial","Doc {Material|Shape}"
1314 "\t: Print visualization material properties",
1315 __FILE__, XGetVisMaterial, g);
1316 di.Add ("XAddVisMaterial",
1318 "\n\t\t: [-transparency 0..1] [-alphaMode {Opaque|Mask|Blend|BlendAuto} CutOffValue] [-refractionIndex 1..3]"
1319 "\n\t\t: [-diffuse RGB] [-diffuseTexture ImagePath]"
1320 "\n\t\t: [-specular RGB] [-ambient RGB] [-emissive RGB] [-shininess 0..1]"
1321 "\n\t\t: [-baseColor RGB] [-baseColorTexture ImagePath]"
1322 "\n\t\t: [-emissiveFactor RGB] [-emissiveTexture ImagePath]"
1323 "\n\t\t: [-metallic 0..1] [-roughness 0..1] [-metallicRoughnessTexture ImagePath]"
1324 "\n\t\t: [-occlusionTexture ImagePath] [-normalTexture ImagePath]"
1325 "\n\t\t: [-doubleSided {0|1}]"
1326 "\n\t\t: Add material into Document's material table.",
1327 __FILE__, XAddVisMaterial, g);
1328 di.Add ("XRemoveVisMaterial","Doc Material"
1329 "\t: Remove material in document from material table",
1330 __FILE__, XRemoveVisMaterial, g);
1331 di.Add ("XSetVisMaterial", "Doc Shape Material"
1332 "\t: Set material to shape",
1333 __FILE__, XSetVisMaterial, g);
1334 di.Add ("XUnsetVisMaterial", "Doc Shape"
1335 "\t: Unset material from shape",
1336 __FILE__, XSetVisMaterial, g);