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 <Message.hxx>
21 #include <Precision.hxx>
22 #include <Quantity_Color.hxx>
23 #include <Quantity_ColorRGBA.hxx>
24 #include <OSD_File.hxx>
25 #include <TCollection_AsciiString.hxx>
26 #include <TDF_Label.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_MaskBlend: return "MaskBlend";
90 case Graphic3d_AlphaMode_BlendAuto: return "BlendAuto";
95 //! Convert back face culling mode into string.
96 static const char* faceCullToString (Graphic3d_TypeOfBackfacingModel theMode)
100 case Graphic3d_TypeOfBackfacingModel_Auto: return "Auto";
101 case Graphic3d_TypeOfBackfacingModel_BackCulled: return "BackCulled";
102 case Graphic3d_TypeOfBackfacingModel_FrontCulled: return "FrontCulled";
103 case Graphic3d_TypeOfBackfacingModel_DoubleSided: return "DoubleSided";
108 //! Find existing visualization material in the document.
109 static TDF_Label findVisMaterial (const Handle(TDocStd_Document)& theDoc,
110 const TCollection_AsciiString& theKey)
112 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (theDoc->Main());
114 TDF_Tool::Label (theDoc->GetData(), theKey, aMatLab);
115 if (!aMatLab.IsNull())
117 return aMatTool->IsMaterial (aMatLab) ? aMatLab : TDF_Label();
120 TDF_LabelSequence aLabels;
121 aMatTool->GetMaterials (aLabels);
122 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next())
124 Handle(TDataStd_Name) aNodeName;
125 if (aLabIter.Value().FindAttribute (TDataStd_Name::GetID(), aNodeName)
126 && aNodeName->Get().IsEqual (theKey))
128 return aLabIter.Value();
134 //! Check if image file exists.
135 static bool isImageFileExist (const TCollection_AsciiString& thePath)
137 const OSD_Path aPath (thePath);
138 if (!OSD_File (aPath).Exists())
140 std::cout << "Error: file '" << thePath << " not found\n";
146 //! Parse RGB values coming after specified argument.
147 static bool parseRgbColor (Standard_Integer& theArgIter,
148 Quantity_Color& theColor,
149 Standard_Integer theNbArgs,
150 const char** theArgVec)
152 Standard_Integer aNbParsed = Draw::ParseColor (theNbArgs - theArgIter - 1,
153 theArgVec + theArgIter + 1,
157 std::cout << "Syntax error at '" << theArgVec[theArgIter] << "'\n";
160 theArgIter += aNbParsed;
164 //! Parse normalized real value within 0..1 range.
165 static bool parseNormalizedReal (const char* theString,
166 Standard_ShortReal& theValue)
168 theValue = (Standard_ShortReal )Draw::Atof (theString);
169 if (theValue < 0.0f || theValue > 1.0f)
171 std::cerr << "Syntax error at '" << theString << "'\n";
177 //=======================================================================
178 // Section: Work with colors
179 //=======================================================================
180 static Standard_Integer setColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
184 Message::SendFail() << "Syntax error: wrong number of arguments";
188 Handle(TDocStd_Document) aDoc;
189 DDocStd::GetDocument (argv[1], aDoc);
192 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
198 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
201 aShape = DBRep::Get (argv[2]);
204 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label nor shape";
209 Quantity_ColorRGBA aColor;
210 bool isColorDefined = false;
211 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
212 for (Standard_Integer anArgIter = 3; anArgIter < argc; ++anArgIter)
214 if (parseXDocColorType (argv[anArgIter], aColType))
218 else if (!isColorDefined)
220 isColorDefined = true;
221 Standard_Integer aNbParsed = Draw::ParseColor (argc - anArgIter,
226 Message::SendFail() << "Syntax error at '" << argv[anArgIter] << "'";
229 anArgIter += aNbParsed - 1;
233 Message::SendFail() << "Syntax error at '" << argv[anArgIter] << "'";
239 Message::SendFail() << "Syntax error: wrong number of arguments";
243 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
244 if (!aLabel.IsNull())
246 aColorTool->SetColor (aLabel, aColor, aColType);
248 else if (!aColorTool->SetColor (aShape, aColor, aColType))
250 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label nor shape";
256 static Standard_Integer getColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
260 Message::SendFail() << "Syntax error: wrong number of arguments";
264 Handle(TDocStd_Document) aDoc;
265 DDocStd::GetDocument (argv[1], aDoc);
268 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
273 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
274 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
275 Quantity_ColorRGBA aColor;
276 if (!myColors->GetColor (aLabel, aColor))
281 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
283 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
287 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
292 static Standard_Integer getShapeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
294 if (argc != 3 && argc != 4)
296 Message::SendFail() << "Syntax error: wrong number of arguments";
300 Handle(TDocStd_Document) aDoc;
301 DDocStd::GetDocument (argv[1], aDoc);
304 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
309 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
312 Message::SendFail() << "Syntax error: '" << argv[2] << "' label is not found in the document";
316 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
317 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
318 if (argc > 3 && !parseXDocColorType (argv[3], aColType))
320 Message::SendFail() << "Syntax error: unknown color type '" << argv[3] << "'";
324 Quantity_ColorRGBA aColor;
325 if (!myColors->GetColor (aLabel, aColType, aColor))
330 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
332 di << aColor.GetRGB().StringName(aColor.GetRGB().Name());
336 di << aColor.GetRGB().StringName(aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
342 static Standard_Integer getAllColors (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
346 Message::SendFail() << "Syntax error: wrong number of arguments";
350 Handle(TDocStd_Document) aDoc;
351 DDocStd::GetDocument (argv[1], aDoc);
354 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
358 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
359 TDF_LabelSequence aLabels;
360 aColorTool->GetColors (aLabels);
361 if (aLabels.Length() >= 1)
363 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next())
365 Quantity_ColorRGBA aColor;
366 if (!aColorTool->GetColor (aLabIter.Value(), aColor))
370 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
372 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
376 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
384 static Standard_Integer addColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
388 Message::SendFail() << "Syntax error: wrong number of arguments";
392 Handle(TDocStd_Document) aDoc;
393 DDocStd::GetDocument (argv[1], aDoc);
396 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
400 Quantity_ColorRGBA aColRGBA;
401 Standard_Integer aNbParsed = Draw::ParseColor (argc - 2, argv + 2, aColRGBA);
402 if (aNbParsed != argc - 2)
404 Message::SendFail() << "Syntax error at '" << argv[2] << "'";
408 TCollection_AsciiString anEntry;
409 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
410 TDF_Label aLabel = aColorTool->AddColor (aColRGBA);
411 TDF_Tool::Entry (aLabel, anEntry);
416 static Standard_Integer removeColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
420 Message::SendFail() << "Syntax error: wrong number of arguments";
424 Handle(TDocStd_Document) aDoc;
426 DDocStd::GetDocument (argv[1], aDoc);
429 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
432 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
435 Message::SendFail() << "Syntax error: " << argv[2] << " label is not found in the document";
439 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
440 aColorTool->RemoveColor (aLabel);
444 static Standard_Integer findColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
448 Message::SendFail() << "Syntax error: wrong number of arguments";
452 Handle(TDocStd_Document) aDoc;
453 DDocStd::GetDocument (argv[1], aDoc);
456 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
460 Quantity_ColorRGBA aColRGBA;
461 Standard_Integer aNbParsed = Draw::ParseColor (argc - 2, argv + 2, aColRGBA);
462 if (aNbParsed != argc - 2)
464 Message::SendFail() << "Syntax error at '" << argv[2] << "'";
468 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
469 TCollection_AsciiString anEntry;
470 TDF_Tool::Entry (aColorTool->FindColor (aColRGBA), anEntry);
475 static Standard_Integer unsetColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
479 Message::SendFail() << "Syntax error: wrong number of arguments";
483 Handle(TDocStd_Document) aDoc;
484 DDocStd::GetDocument (argv[1], aDoc);
487 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
491 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
492 if (!parseXDocColorType (argv[3], aColType))
494 Message::SendFail() << "Syntax error: unknown color type '" << argv[3] << "'";
499 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
500 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
501 if (!aLabel.IsNull())
503 myColors->UnSetColor (aLabel, aColType);
507 TopoDS_Shape aShape = DBRep::Get (argv[2]);
510 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label nor shape";
513 myColors->UnSetColor (aShape, aColType);
517 static Standard_Integer setVisibility (Draw_Interpretor& , Standard_Integer argc, const char** argv)
519 if (argc != 3 && argc != 4)
521 Message::SendFail() << "Syntax error: wrong number of arguments";
525 Handle(TDocStd_Document) aDoc;
527 DDocStd::GetDocument (argv[1], aDoc);
530 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
534 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
535 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
538 // get label by shape
539 TopoDS_Shape aShape = DBRep::Get (argv[2]);
540 if (!aShape.IsNull())
542 aLabel = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
547 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label not shape";
551 Standard_Boolean isVisible = Standard_False;
554 TCollection_AsciiString aVisArg (argv[3]);
557 isVisible = Standard_True;
559 else if (aVisArg == "0")
561 isVisible = Standard_False;
565 Message::SendFail() << "Syntax error: unknown argument '" << argv[3] << "'";
569 aColorTool->SetVisibility (aLabel, isVisible);
573 static Standard_Integer getVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
577 Message::SendFail() << "Syntax error: wrong number of arguments";
581 Handle(TDocStd_Document) aDoc;
582 DDocStd::GetDocument (argv[1], aDoc);
585 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
589 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
591 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
594 // get label by shape
595 TopoDS_Shape aShape = DBRep::Get (argv[2]);
596 if (!aShape.IsNull())
598 aLabel = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
603 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label not shape";
607 di << (aColorTool->IsVisible (aLabel) ? 1 : 0);
611 static Standard_Integer getStyledVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
615 Message::SendFail() << "Syntax error: wrong number of arguments";
619 Handle(TDocStd_Document) aDoc;
620 DDocStd::GetDocument (argv[1], aDoc);
621 TopoDS_Shape aShape = DBRep::Get(argv[2]);
624 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
629 Message::SendFail() << "Syntax error: " << argv[2] << " is not a shape";
633 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
634 di << (aColorTool->IsInstanceVisible (aShape) ? 1 : 0);
638 static Standard_Integer getStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
640 if (argc != 3 && argc != 4)
642 Message::SendFail() << "Syntax error: wrong number of arguments";
646 Handle(TDocStd_Document) aDoc;
647 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
648 DDocStd::GetDocument (argv[1], aDoc);
649 TopoDS_Shape aShape = DBRep::Get (argv[2]);
652 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
657 Message::SendFail() << "Syntax error: " << argv[2] << " is not a shape";
660 if (argc > 3 && !parseXDocColorType (argv[3], aColType))
662 Message::SendFail() << "Syntax error: unknown color type '" << argv[3] << "'";
666 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
667 Quantity_ColorRGBA aColor;
668 if (aColorTool->GetInstanceColor (aShape, aColType, aColor))
670 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
672 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
676 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
682 static Standard_Integer setStyledcolor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
686 Message::SendFail() << "Syntax error: wrong number of arguments";
690 Handle(TDocStd_Document) aDoc;
691 DDocStd::GetDocument (argv[1], aDoc);
694 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
698 TopoDS_Shape aShape = DBRep::Get (argv[2]);
701 Message::SendFail() << "Syntax error: " << argv[2] << " is not a shape";
705 XCAFDoc_ColorType aColorType = XCAFDoc_ColorGen;
706 Quantity_ColorRGBA aColRGBA;
707 for (Standard_Integer anArgIter = 3; anArgIter < argc; ++anArgIter)
709 if (parseXDocColorType (argv[anArgIter], aColorType))
715 Standard_Integer aNbParsed = Draw::ParseColor (argc - anArgIter,
720 Message::SendFail() << "Syntax error at '" << argv[anArgIter] << "'";
723 anArgIter += aNbParsed - 1;
727 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
728 if (!aColorTool->SetInstanceColor (aShape, aColorType, aColRGBA))
730 Message::SendFail() << "Error: cannot set color for the indicated component";
736 // ================================================================
737 // Function : XGetAllVisMaterials
739 // ================================================================
740 static Standard_Integer XGetAllVisMaterials (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgVec)
742 if (theNbArgs != 2 && theNbArgs != 3)
744 Message::SendFail() << "Syntax error: wrong number of arguments";
748 Handle(TDocStd_Document) aDoc;
749 DDocStd::GetDocument (theArgVec[1], aDoc);
752 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
756 bool toPrintNames = true;
759 TCollection_AsciiString anArgCase (theArgVec[2]);
760 anArgCase.LowerCase();
761 if (anArgCase == "-names")
765 else if (anArgCase == "-labels")
767 toPrintNames = false;
771 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
772 TDF_LabelSequence aLabels;
773 aMatTool->GetMaterials (aLabels);
774 Standard_Integer aMatIndex = 1;
775 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next(), ++aMatIndex)
777 const TDF_Label& aMatLab = aLabIter.Value();
780 TCollection_AsciiString anEntryId;
781 TDF_Tool::Entry (aMatLab, anEntryId);
782 theDI << anEntryId << " ";
786 Handle(TDataStd_Name) aNodeName;
787 if (aMatLab.FindAttribute (TDataStd_Name::GetID(), aNodeName))
789 theDI << aNodeName->Get() << " ";
793 TCollection_AsciiString aName = TCollection_AsciiString("<UNNAMED") + aMatIndex + ">";
794 theDI << aName << " ";
800 // ================================================================
801 // Function : XGetVisMaterial
803 // ================================================================
804 static Standard_Integer XGetVisMaterial (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgVec)
808 Message::SendFail() << "Syntax error: wrong number of arguments";
812 Handle(TDocStd_Document) aDoc;
813 DDocStd::GetDocument (theArgVec[1], aDoc);
816 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
820 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
821 Handle(XCAFDoc_VisMaterial) aMat;
822 TDF_Label aMatLab = findVisMaterial (aDoc, theArgVec[2]);
823 if (!aMatLab.IsNull())
825 aMat = aMatTool->GetMaterial (aMatLab);
830 TDF_Tool::Label (aDoc->GetData(), theArgVec[2], aShapeLab);
831 if (aShapeLab.IsNull())
833 TopoDS_Shape aShape = DBRep::Get (theArgVec[2]);
834 if (!aShape.IsNull())
836 aShapeLab = aMatTool->ShapeTool()->FindShape (aShape);
839 if (!aShapeLab.IsNull()
840 && !aMatTool->ShapeTool()->IsShape (aShapeLab))
844 if (aShapeLab.IsNull())
846 Message::SendFail() << "Syntax error: " << theArgVec[2] << " is not material nor shape";
850 aMat = aMatTool->GetShapeMaterial (aShapeLab);
859 TCollection_AsciiString anEntryId;
860 TDF_Tool::Entry (aMat->Label(), anEntryId);
861 theDI << "Label: " << anEntryId << "\n";
863 Handle(TDataStd_Name) aNodeName;
864 if (aMat->Label().FindAttribute (TDataStd_Name::GetID(), aNodeName))
866 theDI << "Name: " << aNodeName->Get() << "\n";
873 theDI << "AlphaMode: " << alphaModeToString (aMat->AlphaMode()) << "\n";
874 theDI << "AlphaCutOff: " << aMat->AlphaCutOff() << "\n";
875 theDI << "IsDoubleSided: " << faceCullToString (aMat->FaceCulling()) << "\n";
876 if (aMat->HasCommonMaterial())
878 const XCAFDoc_VisMaterialCommon& aMatCom = aMat->CommonMaterial();
879 theDI << "Common.Ambient: " << (Graphic3d_Vec3 )aMatCom.AmbientColor << "\n";
880 theDI << "Common.Diffuse: " << (Graphic3d_Vec3 )aMatCom.DiffuseColor << "\n";
881 if (!aMatCom.DiffuseTexture.IsNull())
883 theDI << "Common.DiffuseTexture: " << aMatCom.DiffuseTexture->TextureId() << "\n";
885 theDI << "Common.Specular: " << (Graphic3d_Vec3 )aMatCom.SpecularColor << "\n";
886 theDI << "Common.Emissive: " << (Graphic3d_Vec3 )aMatCom.EmissiveColor << "\n";
887 theDI << "Common.Shininess: " << aMatCom.Shininess << "\n";
888 theDI << "Common.Transparency: " << aMatCom.Transparency << "\n";
890 if (aMat->HasPbrMaterial())
892 const XCAFDoc_VisMaterialPBR& aMatPbr = aMat->PbrMaterial();
893 theDI << "PBR.BaseColor: " << (Graphic3d_Vec3 )aMatPbr.BaseColor.GetRGB() << "\n";
894 theDI << "PBR.Transparency: " << (1.0 - aMatPbr.BaseColor.Alpha()) << "\n";
895 theDI << "PBR.RefractionIndex: " << aMatPbr.RefractionIndex << "\n";
896 if (!aMatPbr.BaseColorTexture.IsNull())
898 theDI << "PBR.BaseColorTexture: " << aMatPbr.BaseColorTexture->TextureId() << "\n";
900 theDI << "PBR.EmissiveFactor: " << aMatPbr.EmissiveFactor << "\n";
901 if (!aMatPbr.EmissiveTexture.IsNull())
903 theDI << "PBR.EmissiveTexture: " << aMatPbr.EmissiveTexture->TextureId() << "\n";
905 theDI << "PBR.Metallic: " << aMatPbr.Metallic << "\n";
906 theDI << "PBR.Roughness: " << aMatPbr.Roughness << "\n";
907 if (!aMatPbr.MetallicRoughnessTexture.IsNull())
909 theDI << "PBR.MetallicRoughnessTexture: " << aMatPbr.MetallicRoughnessTexture->TextureId() << "\n";
911 if (!aMatPbr.OcclusionTexture.IsNull())
913 theDI << "PBR.OcclusionTexture: " << aMatPbr.OcclusionTexture->TextureId() << "\n";
915 if (!aMatPbr.NormalTexture.IsNull())
917 theDI << "PBR.NormalTexture: " << aMatPbr.NormalTexture->TextureId() << "\n";
923 // ================================================================
924 // Function : XAddVisMaterial
926 // ================================================================
927 static Standard_Integer XAddVisMaterial (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
931 Message::SendFail() << "Syntax error: wrong number of arguments";
935 Handle(TDocStd_Document) aDoc;
936 DDocStd::GetDocument (theArgVec[1], aDoc);
939 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
943 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
944 TDF_Label aMatLab = findVisMaterial (aDoc, theArgVec[2]);
945 if (aMatLab.IsNull())
947 aMatLab = aMatTool->AddMaterial (theArgVec[2]);
950 Handle(XCAFDoc_VisMaterial) aMat = aMatTool->GetMaterial (aMatLab);
951 XCAFDoc_VisMaterialCommon aMatCom = aMat->CommonMaterial();
952 XCAFDoc_VisMaterialPBR aMatPbr = aMat->PbrMaterial();
953 Standard_ShortReal aRealValue = 0.0f;
954 for (Standard_Integer anArgIter = 3; anArgIter < theNbArgs; ++anArgIter)
956 TCollection_AsciiString anArg (theArgVec[anArgIter]);
958 if ((anArg == "-transparency"
959 || anArg == "-alpha")
960 && anArgIter + 1 < theNbArgs
961 && parseNormalizedReal (theArgVec[anArgIter + 1], aMatCom.Transparency))
964 if (anArg == "-alpha")
966 aMatCom.Transparency = 1.0f - aMatCom.Transparency;
968 aMatPbr.BaseColor.SetAlpha (1.0f - aMatCom.Transparency);
970 else if (anArgIter + 1 < theNbArgs
971 && (anArg == "-refractionindex" || anArg == "-ior"))
973 aMatPbr.RefractionIndex = (Standard_ShortReal )Draw::Atof (theArgVec[anArgIter + 1]);
974 if (aMatPbr.RefractionIndex < 1.0f || aMatPbr.RefractionIndex > 3.0f)
976 Message::SendFail() << "Syntax error at '" << anArg << "'";
981 aMatPbr.IsDefined = true;
983 else if (anArg == "-alphamode"
984 && anArgIter + 2 < theNbArgs
985 && parseNormalizedReal (theArgVec[anArgIter + 2], aRealValue))
987 TCollection_AsciiString aModeStr (theArgVec[anArgIter + 1]);
988 aModeStr.LowerCase();
989 Graphic3d_AlphaMode anAlphaMode = Graphic3d_AlphaMode_Opaque;
990 if (aModeStr == "opaque")
992 anAlphaMode = Graphic3d_AlphaMode_Opaque;
994 else if (aModeStr == "mask")
996 anAlphaMode = Graphic3d_AlphaMode_Mask;
998 else if (aModeStr == "blend")
1000 anAlphaMode = Graphic3d_AlphaMode_Blend;
1002 else if (aModeStr == "maskblend"
1003 || aModeStr == "blendmask")
1005 anAlphaMode = Graphic3d_AlphaMode_MaskBlend;
1007 else if (aModeStr == "blendauto")
1009 anAlphaMode = Graphic3d_AlphaMode_BlendAuto;
1013 Message::SendFail() << "Syntax error at '" << anArg << "'";
1016 aMat->SetAlphaMode (anAlphaMode, aRealValue);
1019 else if (anArg == "-diffuse"
1020 || anArg == "-basecolor"
1021 || anArg == "-albedo")
1023 Quantity_ColorRGBA aColorRGBA;
1024 Standard_Integer aNbParsed = Draw::ParseColor (theNbArgs - anArgIter - 1,
1025 theArgVec + anArgIter + 1,
1029 Message::SendFail() << "Syntax error at '" << theArgVec[anArgIter] << "'";
1032 anArgIter += aNbParsed;
1034 if (anArg == "-diffuse")
1036 aMatCom.IsDefined = true;
1037 aMatCom.DiffuseColor = aColorRGBA.GetRGB();
1038 if (aNbParsed == 2 || aNbParsed == 4)
1040 aMatCom.Transparency = 1.0f - aColorRGBA.Alpha();
1045 aMatPbr.IsDefined = true;
1046 if (aNbParsed == 2 || aNbParsed == 4)
1048 aMatPbr.BaseColor = aColorRGBA;
1052 aMatPbr.BaseColor.SetRGB (aColorRGBA.GetRGB());
1056 else if (anArg == "-specular"
1057 && parseRgbColor (anArgIter, aMatCom.SpecularColor,
1058 theNbArgs, theArgVec))
1060 aMatCom.IsDefined = true;
1062 else if (anArg == "-ambient"
1063 && parseRgbColor (anArgIter, aMatCom.AmbientColor,
1064 theNbArgs, theArgVec))
1066 aMatCom.IsDefined = true;
1068 else if (anArg == "-emissive"
1069 && parseRgbColor (anArgIter, aMatCom.EmissiveColor,
1070 theNbArgs, theArgVec))
1072 aMatCom.IsDefined = true;
1074 else if (anArg == "-shininess"
1075 && anArgIter + 1 < theNbArgs)
1077 aMatCom.IsDefined = true;
1078 aMatCom.Shininess = (float )Draw::Atof (theArgVec[++anArgIter]);
1079 if (aMatCom.Shininess < 0.0f || aMatCom.Shininess > 1.0f)
1081 Message::SendFail() << "Syntax error at '" << anArg << "'";
1085 else if (anArgIter + 1 < theNbArgs
1086 && anArg == "-diffusetexture"
1087 && isImageFileExist (theArgVec[anArgIter + 1]))
1089 aMatCom.IsDefined = true;
1090 aMatCom.DiffuseTexture = new Image_Texture (theArgVec[++anArgIter]);
1092 else if (anArgIter + 1 < theNbArgs
1093 && anArg == "-basecolortexture"
1094 && isImageFileExist (theArgVec[anArgIter + 1]))
1096 aMatPbr.IsDefined = true;
1097 aMatPbr.BaseColorTexture = new Image_Texture (theArgVec[++anArgIter]);
1099 else if (anArgIter + 1 < theNbArgs
1100 && anArg == "-emissivetexture"
1101 && isImageFileExist (theArgVec[anArgIter + 1]))
1103 aMatPbr.IsDefined = true;
1104 aMatPbr.EmissiveTexture = new Image_Texture (theArgVec[++anArgIter]);
1106 else if (anArgIter + 1 < theNbArgs
1107 && anArg == "-metallicroughnesstexture"
1108 && isImageFileExist (theArgVec[anArgIter + 1]))
1110 aMatPbr.IsDefined = true;
1111 aMatPbr.MetallicRoughnessTexture = new Image_Texture (theArgVec[++anArgIter]);
1113 else if (anArgIter + 1 < theNbArgs
1114 && anArg == "-normaltexture"
1115 && isImageFileExist (theArgVec[anArgIter + 1]))
1117 aMatPbr.IsDefined = true;
1118 aMatPbr.NormalTexture = new Image_Texture (theArgVec[++anArgIter]);
1120 else if (anArgIter + 1 < theNbArgs
1121 && anArg == "-occlusiontexture"
1122 && isImageFileExist (theArgVec[anArgIter + 1]))
1124 aMatPbr.IsDefined = true;
1125 aMatPbr.OcclusionTexture = new Image_Texture (theArgVec[++anArgIter]);
1127 else if (anArg == "-emissivefactor"
1128 && anArgIter + 4 < theNbArgs)
1130 aMatPbr.IsDefined = true;
1131 aMatPbr.EmissiveFactor.SetValues ((float )Draw::Atof (theArgVec[anArgIter + 1]),
1132 (float )Draw::Atof (theArgVec[anArgIter + 2]),
1133 (float )Draw::Atof (theArgVec[anArgIter + 3]));
1136 else if (anArg == "-doublesided")
1138 aMatPbr.IsDefined = true;
1139 bool isDoubleSided = true;
1140 if (anArgIter + 1 < theNbArgs
1141 && Draw::ParseOnOff (theArgVec[anArgIter + 1], isDoubleSided))
1145 aMat->SetFaceCulling (isDoubleSided ? Graphic3d_TypeOfBackfacingModel_Auto : Graphic3d_TypeOfBackfacingModel_BackCulled);
1147 else if (anArgIter + 1 < theNbArgs
1148 && (anArg == "-faceculling"
1149 || anArg == "-facecull"))
1151 aMatPbr.IsDefined = true;
1152 TCollection_AsciiString aCullStr (theArgVec[++anArgIter]);
1153 Graphic3d_TypeOfBackfacingModel aMode = Graphic3d_TypeOfBackfacingModel_Auto;
1154 aCullStr.LowerCase();
1155 if (aCullStr == "auto")
1157 aMode = Graphic3d_TypeOfBackfacingModel_Auto;
1159 else if (aCullStr == "backculled"
1160 || aCullStr == "backcull"
1161 || aCullStr == "back")
1163 aMode = Graphic3d_TypeOfBackfacingModel_BackCulled;
1165 else if (aCullStr == "frontculled"
1166 || aCullStr == "frontcull"
1167 || aCullStr == "front")
1169 aMode = Graphic3d_TypeOfBackfacingModel_FrontCulled;
1171 else if (aCullStr == "doublesided")
1173 aMode = Graphic3d_TypeOfBackfacingModel_DoubleSided;
1177 Message::SendFail() << "Syntax error at '" << anArg << "'";
1180 aMat->SetFaceCulling (aMode);
1182 else if (anArgIter + 1 < theNbArgs
1183 && anArg == "-metallic"
1184 && parseNormalizedReal (theArgVec[anArgIter + 1], aMatPbr.Metallic))
1187 aMatPbr.IsDefined = true;
1189 else if (anArgIter + 1 < theNbArgs
1190 && anArg == "-roughness"
1191 && parseNormalizedReal (theArgVec[anArgIter + 1], aMatPbr.Roughness))
1194 aMatPbr.IsDefined = true;
1198 Message::SendFail() << "Syntax error at '" << theArgVec[anArgIter] << "'";
1203 aMat->SetCommonMaterial (aMatCom);
1204 aMat->SetPbrMaterial (aMatPbr);
1208 // ================================================================
1209 // Function : XRemoveVisMaterial
1211 // ================================================================
1212 static Standard_Integer XRemoveVisMaterial (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
1216 Message::SendFail() << "Syntax error: wrong number of arguments";
1220 Handle(TDocStd_Document) aDoc;
1221 DDocStd::GetDocument (theArgVec[1], aDoc);
1224 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
1228 TDF_Label aMatLab = findVisMaterial (aDoc, theArgVec[2]);
1229 if (aMatLab.IsNull())
1231 Message::SendFail() << "Syntax error: " << theArgVec[2] << " is not a material";
1235 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
1236 aMatTool->RemoveMaterial (aMatLab);
1240 // ================================================================
1241 // Function : XSetVisMaterial
1243 // ================================================================
1244 static Standard_Integer XSetVisMaterial (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
1246 if (theNbArgs != 3 && theNbArgs != 4)
1248 Message::SendFail() << "Syntax error: wrong number of arguments";
1252 Handle(TDocStd_Document) aDoc;
1253 TDF_Label aShapeLab;
1254 DDocStd::GetDocument (theArgVec[1], aDoc);
1257 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
1261 TDF_Tool::Label (aDoc->GetData(), theArgVec[2], aShapeLab);
1262 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
1263 if (aShapeLab.IsNull())
1265 // get label by shape
1266 TopoDS_Shape aShape = DBRep::Get (theArgVec[2]);
1267 if (!aShape.IsNull())
1269 aShapeLab = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
1272 if (aShapeLab.IsNull())
1274 Message::SendFail() << "Syntax error: " << theArgVec[2] << " is not a label not shape";
1281 aMatLab = findVisMaterial (aDoc, theArgVec[3]);
1282 if (aMatLab.IsNull())
1284 Message::SendFail() << "Syntax error: " << theArgVec[3] << " is not a material";
1289 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
1290 aMatTool->SetShapeMaterial (aShapeLab, aMatLab);
1294 //=======================================================================
1295 //function : InitCommands
1297 //=======================================================================
1299 void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di)
1301 static Standard_Boolean initactor = Standard_False;
1306 initactor = Standard_True;
1308 //=====================================
1310 //=====================================
1312 Standard_CString g = "XDE color's commands";
1314 di.Add ("XSetColor","Doc {Label|Shape} R G B [alpha] [{generic|surface|curve}=gen]"
1315 "\t: Set color [R G B] to shape given by Label, "
1316 "type of color 's' - for surface, 'c' - for curve (default generic)",
1317 __FILE__, setColor, g);
1319 di.Add ("XGetColor","Doc label"
1320 "\t: Return color defined on label in colortable",
1321 __FILE__, getColor, g);
1323 di.Add ("XGetShapeColor","Doc Label {generic|surface|curve}"
1324 "\t: Returns color defined by label",
1325 __FILE__, getShapeColor, g);
1327 di.Add ("XGetAllColors","Doc"
1328 "\t: Print all colors that defined in document",
1329 __FILE__, getAllColors, g);
1331 di.Add ("XAddColor","Doc R G B [alpha]"
1332 "\t: Add color in document to color table",
1333 __FILE__, addColor, g);
1335 di.Add ("XRemoveColor","Doc Label"
1336 "\t: Remove color in document from color table",
1337 __FILE__, removeColor, g);
1339 di.Add ("XFindColor","Doc R G B [alpha]"
1340 "\t: Find label where indicated color is situated",
1341 __FILE__, findColor, g);
1343 di.Add ("XUnsetColor","Doc {Label|Shape} {generic|surface|curve}"
1345 __FILE__, unsetColor, g);
1347 di.Add ("XSetObjVisibility","Doc {Label|Shape} (0\1) \t: Set the visibility of shape ",
1348 __FILE__, setVisibility, g);
1350 di.Add ("XGetObjVisibility","Doc {Label|Shape} \t: Return the visibility of shape ",
1351 __FILE__, getVisibility, g);
1353 di.Add ("XGetInstanceVisible","Doc Shape \t: Return the visibility of shape ",
1354 __FILE__, getStyledVisibility, g);
1356 di.Add ("XGetInstanceColor","Doc Shape [{generic|surface|curve}=gen]"
1357 "\t: Return the color of component shape",
1358 __FILE__, getStyledcolor, g);
1360 di.Add ("XSetInstanceColor","Doc Shape R G B [alpha] [{generic|surface|curve}=gen]"
1361 "\t: sets color for component of shape if SHUO structure exists already",
1362 __FILE__, setStyledcolor, g);
1364 di.Add ("XGetAllVisMaterials","Doc [{-names|-labels}=-names]"
1365 "\t: Print all visualization materials defined in document",
1366 __FILE__, XGetAllVisMaterials, g);
1367 di.Add ("XGetVisMaterial","Doc {Material|Shape}"
1368 "\t: Print visualization material properties",
1369 __FILE__, XGetVisMaterial, g);
1370 di.Add ("XAddVisMaterial",
1372 "\n\t\t: [-transparency 0..1] [-alphaMode {Opaque|Mask|Blend|BlendAuto} CutOffValue] [-refractionIndex 1..3]"
1373 "\n\t\t: [-diffuse RGB] [-diffuseTexture ImagePath]"
1374 "\n\t\t: [-specular RGB] [-ambient RGB] [-emissive RGB] [-shininess 0..1]"
1375 "\n\t\t: [-baseColor RGB] [-baseColorTexture ImagePath]"
1376 "\n\t\t: [-emissiveFactor RGB] [-emissiveTexture ImagePath]"
1377 "\n\t\t: [-metallic 0..1] [-roughness 0..1] [-metallicRoughnessTexture ImagePath]"
1378 "\n\t\t: [-occlusionTexture ImagePath] [-normalTexture ImagePath]"
1379 "\n\t\t: [-faceCulling {auto|backCulled|doubleSided}] [-doubleSided {0|1}]"
1380 "\n\t\t: Add material into Document's material table.",
1381 __FILE__, XAddVisMaterial, g);
1382 di.Add ("XRemoveVisMaterial","Doc Material"
1383 "\t: Remove material in document from material table",
1384 __FILE__, XRemoveVisMaterial, g);
1385 di.Add ("XSetVisMaterial", "Doc Shape Material"
1386 "\t: Set material to shape",
1387 __FILE__, XSetVisMaterial, g);
1388 di.Add ("XUnsetVisMaterial", "Doc Shape"
1389 "\t: Unset material from shape",
1390 __FILE__, XSetVisMaterial, g);