0031731: Draw Harness - colorize errors and exception messages
[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>
d99f0355 20#include <Message.hxx>
973d410e 21#include <Precision.hxx>
7fd59977 22#include <Quantity_Color.hxx>
973d410e 23#include <Quantity_ColorRGBA.hxx>
a4815d55 24#include <OSD_File.hxx>
42cf5bc1 25#include <TCollection_AsciiString.hxx>
7fd59977 26#include <TDF_Label.hxx>
27#include <TDF_LabelSequence.hxx>
42cf5bc1 28#include <TDF_Tool.hxx>
a4815d55 29#include <TDataStd_Name.hxx>
7fd59977 30#include <TDocStd_Document.hxx>
42cf5bc1 31#include <TopoDS_Shape.hxx>
9196ea9d 32#include <ViewerTest.hxx>
42cf5bc1 33#include <XCAFDoc_ColorTool.hxx>
7fd59977 34#include <XCAFDoc_DocumentTool.hxx>
35#include <XCAFDoc_ShapeTool.hxx>
a4815d55 36#include <XCAFDoc_VisMaterial.hxx>
37#include <XCAFDoc_VisMaterialTool.hxx>
42cf5bc1 38#include <XDEDRAW_Colors.hxx>
7fd59977 39
9196ea9d 40//! Parse XCAFDoc_ColorType enumeration argument.
41static bool parseXDocColorType (const TCollection_AsciiString& theArg,
42 XCAFDoc_ColorType& theType)
43{
44 TCollection_AsciiString anArgCase (theArg);
45 anArgCase.LowerCase();
46 if (anArgCase == "surf"
47 || anArgCase == "surface"
48 || anArgCase == "s")
49 {
50 theType = XCAFDoc_ColorSurf;
51 return true;
52 }
53 else if (anArgCase == "curve"
54 || anArgCase == "c")
55 {
56 theType = XCAFDoc_ColorCurv;
57 return true;
58 }
59 else if (anArgCase == "gen"
60 || anArgCase == "generic")
61 {
62 theType = XCAFDoc_ColorGen;
63 return true;
64 }
65 return false;
66}
67
a4815d55 68//! Print triplet of values.
69template<class S, class T> static S& operator<< (S& theStream, const NCollection_Vec3<T>& theVec)
70{
71 theStream << theVec[0] << " " << theVec[1] << " " << theVec[2];
72 return theStream;
73}
74
75//! Print 4 values.
76template<class S, class T> static S& operator<< (S& theStream, const NCollection_Vec4<T>& theVec)
77{
78 theStream << theVec[0] << " " << theVec[1] << " " << theVec[2] << " " << theVec[3];
79 return theStream;
80}
81
82//! Convert alpha mode into string.
83static const char* alphaModeToString (Graphic3d_AlphaMode theMode)
84{
85 switch (theMode)
86 {
87 case Graphic3d_AlphaMode_Opaque: return "Opaque";
88 case Graphic3d_AlphaMode_Mask: return "Mask";
89 case Graphic3d_AlphaMode_Blend: return "Blend";
90 case Graphic3d_AlphaMode_BlendAuto: return "BlendAuto";
91 }
92 return "";
93}
94
95//! Find existing visualization material in the document.
96static TDF_Label findVisMaterial (const Handle(TDocStd_Document)& theDoc,
97 const TCollection_AsciiString& theKey)
98{
99 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (theDoc->Main());
100 TDF_Label aMatLab;
101 TDF_Tool::Label (theDoc->GetData(), theKey, aMatLab);
102 if (!aMatLab.IsNull())
103 {
104 return aMatTool->IsMaterial (aMatLab) ? aMatLab : TDF_Label();
105 }
106
107 TDF_LabelSequence aLabels;
108 aMatTool->GetMaterials (aLabels);
109 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next())
110 {
111 Handle(TDataStd_Name) aNodeName;
112 if (aLabIter.Value().FindAttribute (TDataStd_Name::GetID(), aNodeName)
113 && aNodeName->Get().IsEqual (theKey))
114 {
115 return aLabIter.Value();
116 }
117 }
118 return TDF_Label();
119}
120
121//! Check if image file exists.
122static bool isImageFileExist (const TCollection_AsciiString& thePath)
123{
124 const OSD_Path aPath (thePath);
125 if (!OSD_File (aPath).Exists())
126 {
127 std::cout << "Error: file '" << thePath << " not found\n";
128 return false;
129 }
130 return true;
131}
132
133//! Parse RGB values coming after specified argument.
134static bool parseRgbColor (Standard_Integer& theArgIter,
135 Quantity_Color& theColor,
136 Standard_Integer theNbArgs,
137 const char** theArgVec)
138{
dae2a922 139 Standard_Integer aNbParsed = Draw::ParseColor (theNbArgs - theArgIter - 1,
140 theArgVec + theArgIter + 1,
141 theColor);
a4815d55 142 if (aNbParsed == 0)
143 {
144 std::cout << "Syntax error at '" << theArgVec[theArgIter] << "'\n";
145 return false;
146 }
147 theArgIter += aNbParsed;
148 return true;
149}
150
151//! Parse normalized real value within 0..1 range.
152static bool parseNormalizedReal (const char* theString,
153 Standard_ShortReal& theValue)
154{
155 theValue = (Standard_ShortReal )Draw::Atof (theString);
156 if (theValue < 0.0f || theValue > 1.0f)
157 {
158 std::cerr << "Syntax error at '" << theString << "'\n";
159 return false;
160 }
161 return true;
162}
163
7fd59977 164//=======================================================================
165// Section: Work with colors
166//=======================================================================
9196ea9d 167static Standard_Integer setColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
7fd59977 168{
9196ea9d 169 if (argc < 4)
170 {
d99f0355 171 Message::SendFail() << "Syntax error: wrong number of arguments";
7fd59977 172 return 1;
173 }
973d410e 174
9196ea9d 175 Handle(TDocStd_Document) aDoc;
176 DDocStd::GetDocument (argv[1], aDoc);
177 if (aDoc.IsNull())
178 {
d99f0355 179 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 180 return 1;
973d410e 181 }
9196ea9d 182
183 TDF_Label aLabel;
184 TopoDS_Shape aShape;
185 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
186 if (aLabel.IsNull())
187 {
188 aShape = DBRep::Get (argv[2]);
189 if (aShape.IsNull())
190 {
d99f0355 191 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label nor shape";
9196ea9d 192 return 1;
193 }
194 }
195
196 Quantity_ColorRGBA aColor;
197 bool isColorDefined = false;
198 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
199 for (Standard_Integer anArgIter = 3; anArgIter < argc; ++anArgIter)
200 {
201 if (parseXDocColorType (argv[anArgIter], aColType))
202 {
203 //
204 }
205 else if (!isColorDefined)
206 {
207 isColorDefined = true;
dae2a922 208 Standard_Integer aNbParsed = Draw::ParseColor (argc - anArgIter,
209 argv + anArgIter,
210 aColor);
9196ea9d 211 if (aNbParsed == 0)
212 {
d99f0355 213 Message::SendFail() << "Syntax error at '" << argv[anArgIter] << "'";
9196ea9d 214 return 1;
215 }
216 anArgIter += aNbParsed - 1;
217 }
218 else
219 {
d99f0355 220 Message::SendFail() << "Syntax error at '" << argv[anArgIter] << "'";
9196ea9d 221 return 1;
7fd59977 222 }
223 }
9196ea9d 224 if (!isColorDefined)
225 {
d99f0355 226 Message::SendFail() << "Syntax error: wrong number of arguments";
9196ea9d 227 return 1;
228 }
229
230 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
231 if (!aLabel.IsNull())
232 {
233 aColorTool->SetColor (aLabel, aColor, aColType);
234 }
235 else if (!aColorTool->SetColor (aShape, aColor, aColType))
236 {
d99f0355 237 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label nor shape";
9196ea9d 238 return 1;
239 }
7fd59977 240 return 0;
241}
242
243static Standard_Integer getColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
244{
9196ea9d 245 if (argc != 3)
246 {
d99f0355 247 Message::SendFail() << "Syntax error: wrong number of arguments";
9196ea9d 248 return 1;
249 }
250
251 Handle(TDocStd_Document) aDoc;
252 DDocStd::GetDocument (argv[1], aDoc);
253 if (aDoc.IsNull())
254 {
d99f0355 255 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
7fd59977 256 return 1;
257 }
7fd59977 258
259 TDF_Label aLabel;
9196ea9d 260 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
261 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
262 Quantity_ColorRGBA aColor;
263 if (!myColors->GetColor (aLabel, aColor))
264 {
265 return 0;
266 }
267
268 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
269 {
270 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
271 }
973d410e 272 else
9196ea9d 273 {
274 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
275 }
7fd59977 276 return 0;
277}
278
279static Standard_Integer getShapeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
280{
9196ea9d 281 if (argc != 3 && argc != 4)
282 {
d99f0355 283 Message::SendFail() << "Syntax error: wrong number of arguments";
9196ea9d 284 return 1;
285 }
286
287 Handle(TDocStd_Document) aDoc;
288 DDocStd::GetDocument (argv[1], aDoc);
289 if (aDoc.IsNull())
290 {
d99f0355 291 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
7fd59977 292 return 1;
293 }
7fd59977 294
295 TDF_Label aLabel;
9196ea9d 296 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
297 if (aLabel.IsNull())
298 {
d99f0355 299 Message::SendFail() << "Syntax error: '" << argv[2] << "' label is not found in the document";
3c946c38 300 return 1;
301 }
302
9196ea9d 303 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
304 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
305 if (argc > 3 && !parseXDocColorType (argv[3], aColType))
306 {
d99f0355 307 Message::SendFail() << "Syntax error: unknown color type '" << argv[3] << "'";
9196ea9d 308 return 1;
309 }
3c946c38 310
9196ea9d 311 Quantity_ColorRGBA aColor;
312 if (!myColors->GetColor (aLabel, aColType, aColor))
313 {
314 return 0;
315 }
3c946c38 316
9196ea9d 317 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
318 {
319 di << aColor.GetRGB().StringName(aColor.GetRGB().Name());
320 }
973d410e 321 else
9196ea9d 322 {
323 di << aColor.GetRGB().StringName(aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
324 }
3c946c38 325
7fd59977 326 return 0;
327}
328
329static Standard_Integer getAllColors (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
330{
9196ea9d 331 if (argc != 2)
332 {
d99f0355 333 Message::SendFail() << "Syntax error: wrong number of arguments";
7fd59977 334 return 1;
335 }
7fd59977 336
9196ea9d 337 Handle(TDocStd_Document) aDoc;
338 DDocStd::GetDocument (argv[1], aDoc);
339 if (aDoc.IsNull())
340 {
d99f0355 341 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 342 return 1;
343 }
344
345 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
346 TDF_LabelSequence aLabels;
347 aColorTool->GetColors (aLabels);
348 if (aLabels.Length() >= 1)
349 {
350 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next())
351 {
352 Quantity_ColorRGBA aColor;
353 if (!aColorTool->GetColor (aLabIter.Value(), aColor))
354 {
355 continue;
356 }
357 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
358 {
359 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
360 }
973d410e 361 else
9196ea9d 362 {
363 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
364 }
7fd59977 365 di << " ";
366 }
367 }
368 return 0;
369}
370
7fd59977 371static Standard_Integer addColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
372{
9196ea9d 373 if (argc < 3)
374 {
d99f0355 375 Message::SendFail() << "Syntax error: wrong number of arguments";
7fd59977 376 return 1;
377 }
7fd59977 378
9196ea9d 379 Handle(TDocStd_Document) aDoc;
380 DDocStd::GetDocument (argv[1], aDoc);
381 if (aDoc.IsNull())
382 {
d99f0355 383 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 384 return 1;
385 }
7fd59977 386
9196ea9d 387 Quantity_ColorRGBA aColRGBA;
dae2a922 388 Standard_Integer aNbParsed = Draw::ParseColor (argc - 2, argv + 2, aColRGBA);
9196ea9d 389 if (aNbParsed != argc - 2)
390 {
d99f0355 391 Message::SendFail() << "Syntax error at '" << argv[2] << "'";
9196ea9d 392 return 1;
973d410e 393 }
9196ea9d 394
395 TCollection_AsciiString anEntry;
396 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
397 TDF_Label aLabel = aColorTool->AddColor (aColRGBA);
398 TDF_Tool::Entry (aLabel, anEntry);
399 di << anEntry;
7fd59977 400 return 0;
401}
402
9196ea9d 403static Standard_Integer removeColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
7fd59977 404{
9196ea9d 405 if (argc != 3)
406 {
d99f0355 407 Message::SendFail() << "Syntax error: wrong number of arguments";
7fd59977 408 return 1;
409 }
7fd59977 410
9196ea9d 411 Handle(TDocStd_Document) aDoc;
7fd59977 412 TDF_Label aLabel;
9196ea9d 413 DDocStd::GetDocument (argv[1], aDoc);
414 if (aDoc.IsNull())
415 {
d99f0355 416 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 417 return 1;
418 }
419 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
420 if (aLabel.IsNull())
421 {
d99f0355 422 Message::SendFail() << "Syntax error: " << argv[2] << " label is not found in the document";
9196ea9d 423 return 1;
424 }
425
426 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
427 aColorTool->RemoveColor (aLabel);
7fd59977 428 return 0;
429}
430
431static Standard_Integer findColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
432{
9196ea9d 433 if (argc < 3)
434 {
d99f0355 435 Message::SendFail() << "Syntax error: wrong number of arguments";
7fd59977 436 return 1;
437 }
7fd59977 438
9196ea9d 439 Handle(TDocStd_Document) aDoc;
440 DDocStd::GetDocument (argv[1], aDoc);
441 if (aDoc.IsNull())
442 {
d99f0355 443 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 444 return 1;
973d410e 445 }
9196ea9d 446
447 Quantity_ColorRGBA aColRGBA;
dae2a922 448 Standard_Integer aNbParsed = Draw::ParseColor (argc - 2, argv + 2, aColRGBA);
9196ea9d 449 if (aNbParsed != argc - 2)
450 {
d99f0355 451 Message::SendFail() << "Syntax error at '" << argv[2] << "'";
9196ea9d 452 return 1;
973d410e 453 }
9196ea9d 454
455 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
456 TCollection_AsciiString anEntry;
457 TDF_Tool::Entry (aColorTool->FindColor (aColRGBA), anEntry);
458 di << anEntry;
7fd59977 459 return 0;
460}
461
9196ea9d 462static Standard_Integer unsetColor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
7fd59977 463{
9196ea9d 464 if (argc != 4)
465 {
d99f0355 466 Message::SendFail() << "Syntax error: wrong number of arguments";
9196ea9d 467 return 1;
468 }
469
470 Handle(TDocStd_Document) aDoc;
471 DDocStd::GetDocument (argv[1], aDoc);
472 if (aDoc.IsNull())
473 {
d99f0355 474 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 475 return 1;
476 }
477
478 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
479 if (!parseXDocColorType (argv[3], aColType))
480 {
d99f0355 481 Message::SendFail() << "Syntax error: unknown color type '" << argv[3] << "'";
7fd59977 482 return 1;
483 }
7fd59977 484
485 TDF_Label aLabel;
9196ea9d 486 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
487 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
488 if (!aLabel.IsNull())
489 {
490 myColors->UnSetColor (aLabel, aColType);
491 return 0;
7fd59977 492 }
9196ea9d 493
494 TopoDS_Shape aShape = DBRep::Get (argv[2]);
495 if (aShape.IsNull())
496 {
d99f0355 497 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label nor shape";
9196ea9d 498 return 1;
7fd59977 499 }
9196ea9d 500 myColors->UnSetColor (aShape, aColType);
7fd59977 501 return 0;
502}
503
9196ea9d 504static Standard_Integer setVisibility (Draw_Interpretor& , Standard_Integer argc, const char** argv)
7fd59977 505{
9196ea9d 506 if (argc != 3 && argc != 4)
507 {
d99f0355 508 Message::SendFail() << "Syntax error: wrong number of arguments";
7fd59977 509 return 1;
510 }
9196ea9d 511
512 Handle(TDocStd_Document) aDoc;
7fd59977 513 TDF_Label aLabel;
9196ea9d 514 DDocStd::GetDocument (argv[1], aDoc);
515 if (aDoc.IsNull())
516 {
d99f0355 517 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 518 return 1;
519 }
520
521 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
522 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
523 if (aLabel.IsNull())
524 {
7fd59977 525 // get label by shape
9196ea9d 526 TopoDS_Shape aShape = DBRep::Get (argv[2]);
527 if (!aShape.IsNull())
528 {
529 aLabel = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
7fd59977 530 }
531 }
9196ea9d 532 if (aLabel.IsNull())
533 {
d99f0355 534 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label not shape";
7fd59977 535 return 1;
536 }
9196ea9d 537
538 Standard_Boolean isVisible = Standard_False;
539 if (argc == 4)
540 {
541 TCollection_AsciiString aVisArg (argv[3]);
542 if (aVisArg == "1")
543 {
544 isVisible = Standard_True;
545 }
546 else if (aVisArg == "0")
547 {
548 isVisible = Standard_False;
549 }
550 else
551 {
d99f0355 552 Message::SendFail() << "Syntax error: unknown argument '" << argv[3] << "'";
9196ea9d 553 return 1;
554 }
555 }
556 aColorTool->SetVisibility (aLabel, isVisible);
7fd59977 557 return 0;
558}
559
560static Standard_Integer getVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
561{
9196ea9d 562 if (argc != 3)
563 {
d99f0355 564 Message::SendFail() << "Syntax error: wrong number of arguments";
9196ea9d 565 return 1;
566 }
567
568 Handle(TDocStd_Document) aDoc;
569 DDocStd::GetDocument (argv[1], aDoc);
570 if (aDoc.IsNull())
571 {
d99f0355 572 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
7fd59977 573 return 1;
574 }
9196ea9d 575
576 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
7fd59977 577 TDF_Label aLabel;
9196ea9d 578 TDF_Tool::Label (aDoc->GetData(), argv[2], aLabel);
579 if (aLabel.IsNull())
580 {
7fd59977 581 // get label by shape
9196ea9d 582 TopoDS_Shape aShape = DBRep::Get (argv[2]);
583 if (!aShape.IsNull())
584 {
585 aLabel = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
7fd59977 586 }
587 }
9196ea9d 588 if (aLabel.IsNull())
589 {
d99f0355 590 Message::SendFail() << "Syntax error: " << argv[2] << " is not a label not shape";
7fd59977 591 return 1;
592 }
9196ea9d 593
594 di << (aColorTool->IsVisible (aLabel) ? 1 : 0);
7fd59977 595 return 0;
596}
597
598static Standard_Integer getStyledVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
599{
9196ea9d 600 if (argc != 3)
601 {
d99f0355 602 Message::SendFail() << "Syntax error: wrong number of arguments";
7fd59977 603 return 1;
604 }
9196ea9d 605
606 Handle(TDocStd_Document) aDoc;
607 DDocStd::GetDocument (argv[1], aDoc);
608 TopoDS_Shape aShape = DBRep::Get(argv[2]);
609 if (aDoc.IsNull())
610 {
d99f0355 611 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 612 return 1;
613 }
614 if (aShape.IsNull())
615 {
d99f0355 616 Message::SendFail() << "Syntax error: " << argv[2] << " is not a shape";
9196ea9d 617 return 1;
618 }
619
620 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
621 di << (aColorTool->IsInstanceVisible (aShape) ? 1 : 0);
7fd59977 622 return 0;
623}
624
625static Standard_Integer getStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
626{
9196ea9d 627 if (argc != 3 && argc != 4)
628 {
d99f0355 629 Message::SendFail() << "Syntax error: wrong number of arguments";
7fd59977 630 return 1;
631 }
9196ea9d 632
633 Handle(TDocStd_Document) aDoc;
634 XCAFDoc_ColorType aColType = XCAFDoc_ColorGen;
635 DDocStd::GetDocument (argv[1], aDoc);
636 TopoDS_Shape aShape = DBRep::Get (argv[2]);
637 if (aDoc.IsNull())
638 {
d99f0355 639 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 640 return 1;
641 }
642 if (aShape.IsNull())
643 {
d99f0355 644 Message::SendFail() << "Syntax error: " << argv[2] << " is not a shape";
9196ea9d 645 return 1;
646 }
647 if (argc > 3 && !parseXDocColorType (argv[3], aColType))
648 {
d99f0355 649 Message::SendFail() << "Syntax error: unknown color type '" << argv[3] << "'";
9196ea9d 650 return 1;
651 }
652
653 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
654 Quantity_ColorRGBA aColor;
655 if (aColorTool->GetInstanceColor (aShape, aColType, aColor))
7fd59977 656 {
9196ea9d 657 if ((1.0 - aColor.Alpha()) < Precision::Confusion())
658 {
659 di << aColor.GetRGB().StringName (aColor.GetRGB().Name());
660 }
973d410e 661 else
9196ea9d 662 {
663 di << aColor.GetRGB().StringName (aColor.GetRGB().Name()) << " (" << aColor.Alpha() << ")";
664 }
7fd59977 665 }
666 return 0;
667}
668
9196ea9d 669static Standard_Integer setStyledcolor (Draw_Interpretor& , Standard_Integer argc, const char** argv)
7fd59977 670{
9196ea9d 671 if (argc < 3)
672 {
d99f0355 673 Message::SendFail() << "Syntax error: wrong number of arguments";
7fd59977 674 return 1;
675 }
7fd59977 676
9196ea9d 677 Handle(TDocStd_Document) aDoc;
678 DDocStd::GetDocument (argv[1], aDoc);
679 if (aDoc.IsNull())
680 {
d99f0355 681 Message::SendFail() << "Syntax error: " << argv[1] << " is not a document";
9196ea9d 682 return 1;
683 }
684
685 TopoDS_Shape aShape = DBRep::Get (argv[2]);
686 if (aShape.IsNull())
687 {
d99f0355 688 Message::SendFail() << "Syntax error: " << argv[2] << " is not a shape";
9196ea9d 689 return 1;
973d410e 690 }
691
9196ea9d 692 XCAFDoc_ColorType aColorType = XCAFDoc_ColorGen;
693 Quantity_ColorRGBA aColRGBA;
694 for (Standard_Integer anArgIter = 3; anArgIter < argc; ++anArgIter)
695 {
696 if (parseXDocColorType (argv[anArgIter], aColorType))
697 {
698 //
699 }
700 else
701 {
dae2a922 702 Standard_Integer aNbParsed = Draw::ParseColor (argc - anArgIter,
703 argv + anArgIter,
704 aColRGBA);
9196ea9d 705 if (aNbParsed == 0)
706 {
d99f0355 707 Message::SendFail() << "Syntax error at '" << argv[anArgIter] << "'";
9196ea9d 708 return 1;
709 }
710 anArgIter += aNbParsed - 1;
711 }
973d410e 712 }
9196ea9d 713
714 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
715 if (!aColorTool->SetInstanceColor (aShape, aColorType, aColRGBA))
7fd59977 716 {
d99f0355 717 Message::SendFail() << "Error: cannot set color for the indicated component";
7fd59977 718 return 1;
719 }
720 return 0;
721}
722
a4815d55 723// ================================================================
724// Function : XGetAllVisMaterials
725// Purpose :
726// ================================================================
727static Standard_Integer XGetAllVisMaterials (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgVec)
728{
729 if (theNbArgs != 2 && theNbArgs != 3)
730 {
d99f0355 731 Message::SendFail() << "Syntax error: wrong number of arguments";
a4815d55 732 return 1;
733 }
734
735 Handle(TDocStd_Document) aDoc;
736 DDocStd::GetDocument (theArgVec[1], aDoc);
737 if (aDoc.IsNull())
738 {
d99f0355 739 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
a4815d55 740 return 1;
741 }
742
743 bool toPrintNames = true;
744 if (theNbArgs == 3)
745 {
746 TCollection_AsciiString anArgCase (theArgVec[2]);
747 anArgCase.LowerCase();
748 if (anArgCase == "-names")
749 {
750 toPrintNames = true;
751 }
752 else if (anArgCase == "-labels")
753 {
754 toPrintNames = false;
755 }
756 }
757
758 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
759 TDF_LabelSequence aLabels;
760 aMatTool->GetMaterials (aLabels);
761 Standard_Integer aMatIndex = 1;
762 for (TDF_LabelSequence::Iterator aLabIter (aLabels); aLabIter.More(); aLabIter.Next(), ++aMatIndex)
763 {
764 const TDF_Label& aMatLab = aLabIter.Value();
765 if (!toPrintNames)
766 {
767 TCollection_AsciiString anEntryId;
768 TDF_Tool::Entry (aMatLab, anEntryId);
769 theDI << anEntryId << " ";
770 continue;
771 }
772
773 Handle(TDataStd_Name) aNodeName;
774 if (aMatLab.FindAttribute (TDataStd_Name::GetID(), aNodeName))
775 {
776 theDI << aNodeName->Get() << " ";
777 }
778 else
779 {
780 TCollection_AsciiString aName = TCollection_AsciiString("<UNNAMED") + aMatIndex + ">";
781 theDI << aName << " ";
782 }
783 }
784 return 0;
785}
786
787// ================================================================
788// Function : XGetVisMaterial
789// Purpose :
790// ================================================================
791static Standard_Integer XGetVisMaterial (Draw_Interpretor& theDI, Standard_Integer theNbArgs, const char** theArgVec)
792{
793 if (theNbArgs != 3)
794 {
d99f0355 795 Message::SendFail() << "Syntax error: wrong number of arguments";
a4815d55 796 return 1;
797 }
798
799 Handle(TDocStd_Document) aDoc;
800 DDocStd::GetDocument (theArgVec[1], aDoc);
801 if (aDoc.IsNull())
802 {
d99f0355 803 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
a4815d55 804 return 1;
805 }
806
807 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
808 Handle(XCAFDoc_VisMaterial) aMat;
809 TDF_Label aMatLab = findVisMaterial (aDoc, theArgVec[2]);
810 if (!aMatLab.IsNull())
811 {
812 aMat = aMatTool->GetMaterial (aMatLab);
813 }
814 else
815 {
816 TDF_Label aShapeLab;
817 TDF_Tool::Label (aDoc->GetData(), theArgVec[2], aShapeLab);
818 if (aShapeLab.IsNull())
819 {
820 TopoDS_Shape aShape = DBRep::Get (theArgVec[2]);
821 if (!aShape.IsNull())
822 {
823 aShapeLab = aMatTool->ShapeTool()->FindShape (aShape);
824 }
825 }
826 if (!aShapeLab.IsNull()
827 && !aMatTool->ShapeTool()->IsShape (aShapeLab))
828 {
829 aShapeLab.Nullify();
830 }
831 if (aShapeLab.IsNull())
832 {
d99f0355 833 Message::SendFail() << "Syntax error: " << theArgVec[2] << " is not material nor shape";
a4815d55 834 return 1;
835 }
836
837 aMat = aMatTool->GetShapeMaterial (aShapeLab);
838 }
839
840 if (aMat.IsNull())
841 {
842 theDI << "EMPTY\n";
843 return 0;
844 }
845
846 TCollection_AsciiString anEntryId;
847 TDF_Tool::Entry (aMat->Label(), anEntryId);
848 theDI << "Label: " << anEntryId << "\n";
849
850 Handle(TDataStd_Name) aNodeName;
851 if (aMat->Label().FindAttribute (TDataStd_Name::GetID(), aNodeName))
852 {
853 theDI << "Name: " << aNodeName->Get() << "\n";
854 }
855 if (aMat->IsEmpty())
856 {
857 theDI << "EMPTY\n";
858 return 0;
859 }
860 theDI << "AlphaMode: " << alphaModeToString (aMat->AlphaMode()) << "\n";
861 theDI << "AlphaCutOff: " << aMat->AlphaCutOff() << "\n";
862 theDI << "IsDoubleSided: " << aMat->IsDoubleSided() << "\n";
863 if (aMat->HasCommonMaterial())
864 {
865 const XCAFDoc_VisMaterialCommon& aMatCom = aMat->CommonMaterial();
866 theDI << "Common.Ambient: " << (Graphic3d_Vec3 )aMatCom.AmbientColor << "\n";
867 theDI << "Common.Diffuse: " << (Graphic3d_Vec3 )aMatCom.DiffuseColor << "\n";
868 if (!aMatCom.DiffuseTexture.IsNull())
869 {
870 theDI << "Common.DiffuseTexture: " << aMatCom.DiffuseTexture->TextureId() << "\n";
871 }
872 theDI << "Common.Specular: " << (Graphic3d_Vec3 )aMatCom.SpecularColor << "\n";
873 theDI << "Common.Emissive: " << (Graphic3d_Vec3 )aMatCom.EmissiveColor << "\n";
874 theDI << "Common.Shininess: " << aMatCom.Shininess << "\n";
875 theDI << "Common.Transparency: " << aMatCom.Transparency << "\n";
876 }
877 if (aMat->HasPbrMaterial())
878 {
879 const XCAFDoc_VisMaterialPBR& aMatPbr = aMat->PbrMaterial();
880 theDI << "PBR.BaseColor: " << (Graphic3d_Vec3 )aMatPbr.BaseColor.GetRGB() << "\n";
881 theDI << "PBR.Transparency: " << (1.0 - aMatPbr.BaseColor.Alpha()) << "\n";
0858125f 882 theDI << "PBR.RefractionIndex: " << aMatPbr.RefractionIndex << "\n";
a4815d55 883 if (!aMatPbr.BaseColorTexture.IsNull())
884 {
885 theDI << "PBR.BaseColorTexture: " << aMatPbr.BaseColorTexture->TextureId() << "\n";
886 }
887 theDI << "PBR.EmissiveFactor: " << aMatPbr.EmissiveFactor << "\n";
888 if (!aMatPbr.EmissiveTexture.IsNull())
889 {
890 theDI << "PBR.EmissiveTexture: " << aMatPbr.EmissiveTexture->TextureId() << "\n";
891 }
892 theDI << "PBR.Metallic: " << aMatPbr.Metallic << "\n";
893 theDI << "PBR.Roughness: " << aMatPbr.Roughness << "\n";
894 if (!aMatPbr.MetallicRoughnessTexture.IsNull())
895 {
896 theDI << "PBR.MetallicRoughnessTexture: " << aMatPbr.MetallicRoughnessTexture->TextureId() << "\n";
897 }
898 if (!aMatPbr.OcclusionTexture.IsNull())
899 {
900 theDI << "PBR.OcclusionTexture: " << aMatPbr.OcclusionTexture->TextureId() << "\n";
901 }
902 if (!aMatPbr.NormalTexture.IsNull())
903 {
904 theDI << "PBR.NormalTexture: " << aMatPbr.NormalTexture->TextureId() << "\n";
905 }
906 }
907 return 0;
908}
909
910// ================================================================
911// Function : XAddVisMaterial
912// Purpose :
913// ================================================================
914static Standard_Integer XAddVisMaterial (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
915{
916 if (theNbArgs < 3)
917 {
d99f0355 918 Message::SendFail() << "Syntax error: wrong number of arguments";
a4815d55 919 return 1;
920 }
921
922 Handle(TDocStd_Document) aDoc;
923 DDocStd::GetDocument (theArgVec[1], aDoc);
924 if (aDoc.IsNull())
925 {
d99f0355 926 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
a4815d55 927 return 1;
928 }
929
930 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
931 TDF_Label aMatLab = findVisMaterial (aDoc, theArgVec[2]);
932 if (aMatLab.IsNull())
933 {
934 aMatLab = aMatTool->AddMaterial (theArgVec[2]);
935 }
936
937 Handle(XCAFDoc_VisMaterial) aMat = aMatTool->GetMaterial (aMatLab);
938 XCAFDoc_VisMaterialCommon aMatCom = aMat->CommonMaterial();
939 XCAFDoc_VisMaterialPBR aMatPbr = aMat->PbrMaterial();
940 Standard_ShortReal aRealValue = 0.0f;
941 for (Standard_Integer anArgIter = 3; anArgIter < theNbArgs; ++anArgIter)
942 {
943 TCollection_AsciiString anArg (theArgVec[anArgIter]);
944 anArg.LowerCase();
945 if ((anArg == "-transparency"
946 || anArg == "-alpha")
947 && anArgIter + 1 < theNbArgs
948 && parseNormalizedReal (theArgVec[anArgIter + 1], aMatCom.Transparency))
949 {
950 ++anArgIter;
951 if (anArg == "-alpha")
952 {
953 aMatCom.Transparency = 1.0f - aMatCom.Transparency;
954 }
955 aMatPbr.BaseColor.SetAlpha (1.0f - aMatCom.Transparency);
956 }
0858125f 957 else if (anArgIter + 1 < theNbArgs
958 && (anArg == "-refractionindex" || anArg == "-ior"))
959 {
960 aMatPbr.RefractionIndex = (Standard_ShortReal )Draw::Atof (theArgVec[anArgIter + 1]);
961 if (aMatPbr.RefractionIndex < 1.0f || aMatPbr.RefractionIndex > 3.0f)
962 {
d99f0355 963 Message::SendFail() << "Syntax error at '" << anArg << "'";
0858125f 964 return 1;
965 }
966
967 ++anArgIter;
968 aMatPbr.IsDefined = true;
969 }
a4815d55 970 else if (anArg == "-alphaMode"
971 && anArgIter + 2 < theNbArgs
972 && parseNormalizedReal (theArgVec[anArgIter + 2], aRealValue))
973 {
974 TCollection_AsciiString aModeStr (theArgVec[anArgIter + 1]);
975 aModeStr.LowerCase();
976 Graphic3d_AlphaMode anAlphaMode = Graphic3d_AlphaMode_Opaque;
977 if (aModeStr == "opaque")
978 {
979 anAlphaMode = Graphic3d_AlphaMode_Opaque;
980 }
981 else if (aModeStr == "mask")
982 {
983 anAlphaMode = Graphic3d_AlphaMode_Mask;
984 }
985 else if (aModeStr == "blend")
986 {
987 anAlphaMode = Graphic3d_AlphaMode_Blend;
988 }
989 else if (aModeStr == "blendauto")
990 {
991 anAlphaMode = Graphic3d_AlphaMode_BlendAuto;
992 }
993 else
994 {
d99f0355 995 Message::SendFail() << "Syntax error at '" << anArg << "'";
a4815d55 996 return 1;
997 }
998 aMat->SetAlphaMode (anAlphaMode, aRealValue);
999 anArgIter += 2;
1000 }
1001 else if (anArg == "-diffuse"
1002 || anArg == "-basecolor"
1003 || anArg == "-albedo")
1004 {
1005 Quantity_ColorRGBA aColorRGBA;
dae2a922 1006 Standard_Integer aNbParsed = Draw::ParseColor (theNbArgs - anArgIter - 1,
1007 theArgVec + anArgIter + 1,
1008 aColorRGBA);
a4815d55 1009 if (aNbParsed == 0)
1010 {
d99f0355 1011 Message::SendFail() << "Syntax error at '" << theArgVec[anArgIter] << "'";
a4815d55 1012 return 1;
1013 }
1014 anArgIter += aNbParsed;
1015
1016 if (anArg == "-diffuse")
1017 {
1018 aMatCom.IsDefined = true;
1019 aMatCom.DiffuseColor = aColorRGBA.GetRGB();
1020 if (aNbParsed == 2 || aNbParsed == 4)
1021 {
1022 aMatCom.Transparency = 1.0f - aColorRGBA.Alpha();
1023 }
1024 }
1025 else
1026 {
1027 aMatPbr.IsDefined = true;
1028 if (aNbParsed == 2 || aNbParsed == 4)
1029 {
1030 aMatPbr.BaseColor = aColorRGBA;
1031 }
1032 else
1033 {
1034 aMatPbr.BaseColor.SetRGB (aColorRGBA.GetRGB());
1035 }
1036 }
1037 }
1038 else if (anArg == "-specular"
1039 && parseRgbColor (anArgIter, aMatCom.SpecularColor,
1040 theNbArgs, theArgVec))
1041 {
1042 aMatCom.IsDefined = true;
1043 }
1044 else if (anArg == "-ambient"
1045 && parseRgbColor (anArgIter, aMatCom.AmbientColor,
1046 theNbArgs, theArgVec))
1047 {
1048 aMatCom.IsDefined = true;
1049 }
1050 else if (anArg == "-emissive"
1051 && parseRgbColor (anArgIter, aMatCom.EmissiveColor,
1052 theNbArgs, theArgVec))
1053 {
1054 aMatCom.IsDefined = true;
1055 }
1056 else if (anArg == "-shininess"
1057 && anArgIter + 1 < theNbArgs)
1058 {
1059 aMatCom.IsDefined = true;
1060 aMatCom.Shininess = (float )Draw::Atof (theArgVec[++anArgIter]);
1061 if (aMatCom.Shininess < 0.0f || aMatCom.Shininess > 1.0f)
1062 {
d99f0355 1063 Message::SendFail() << "Syntax error at '" << anArg << "'";
a4815d55 1064 return 1;
1065 }
1066 }
1067 else if (anArgIter + 1 < theNbArgs
1068 && anArg == "-diffusetexture"
1069 && isImageFileExist (theArgVec[anArgIter + 1]))
1070 {
1071 aMatCom.IsDefined = true;
1072 aMatCom.DiffuseTexture = new Image_Texture (theArgVec[++anArgIter]);
1073 }
1074 else if (anArgIter + 1 < theNbArgs
1075 && anArg == "-basecolortexture"
1076 && isImageFileExist (theArgVec[anArgIter + 1]))
1077 {
1078 aMatPbr.IsDefined = true;
1079 aMatPbr.BaseColorTexture = new Image_Texture (theArgVec[++anArgIter]);
1080 }
1081 else if (anArgIter + 1 < theNbArgs
1082 && anArg == "-emissivetexture"
1083 && isImageFileExist (theArgVec[anArgIter + 1]))
1084 {
1085 aMatPbr.IsDefined = true;
1086 aMatPbr.EmissiveTexture = new Image_Texture (theArgVec[++anArgIter]);
1087 }
1088 else if (anArgIter + 1 < theNbArgs
1089 && anArg == "-metallicroughnesstexture"
1090 && isImageFileExist (theArgVec[anArgIter + 1]))
1091 {
1092 aMatPbr.IsDefined = true;
1093 aMatPbr.MetallicRoughnessTexture = new Image_Texture (theArgVec[++anArgIter]);
1094 }
1095 else if (anArgIter + 1 < theNbArgs
1096 && anArg == "-normaltexture"
1097 && isImageFileExist (theArgVec[anArgIter + 1]))
1098 {
1099 aMatPbr.IsDefined = true;
1100 aMatPbr.NormalTexture = new Image_Texture (theArgVec[++anArgIter]);
1101 }
1102 else if (anArgIter + 1 < theNbArgs
1103 && anArg == "-occlusiontexture"
1104 && isImageFileExist (theArgVec[anArgIter + 1]))
1105 {
1106 aMatPbr.IsDefined = true;
1107 aMatPbr.OcclusionTexture = new Image_Texture (theArgVec[++anArgIter]);
1108 }
1109 else if (anArg == "-emissivefactor"
1110 && anArgIter + 4 < theNbArgs)
1111 {
1112 aMatPbr.IsDefined = true;
1113 aMatPbr.EmissiveFactor.SetValues ((float )Draw::Atof (theArgVec[anArgIter + 1]),
1114 (float )Draw::Atof (theArgVec[anArgIter + 2]),
1115 (float )Draw::Atof (theArgVec[anArgIter + 3]));
1116 anArgIter += 3;
1117 }
1118 else if (anArg == "-doublesided")
1119 {
1120 aMatPbr.IsDefined = true;
1121 bool isDoubleSided = true;
1122 if (anArgIter + 1 < theNbArgs
dae2a922 1123 && Draw::ParseOnOff (theArgVec[anArgIter + 1], isDoubleSided))
a4815d55 1124 {
1125 ++anArgIter;
1126 }
1127 aMat->SetDoubleSided (isDoubleSided);
1128 }
1129 else if (anArgIter + 1 < theNbArgs
1130 && anArg == "-metallic"
1131 && parseNormalizedReal (theArgVec[anArgIter + 1], aMatPbr.Metallic))
1132 {
1133 ++anArgIter;
1134 aMatPbr.IsDefined = true;
1135 }
1136 else if (anArgIter + 1 < theNbArgs
1137 && anArg == "-roughness"
1138 && parseNormalizedReal (theArgVec[anArgIter + 1], aMatPbr.Roughness))
1139 {
1140 ++anArgIter;
1141 aMatPbr.IsDefined = true;
1142 }
1143 else
1144 {
d99f0355 1145 Message::SendFail() << "Syntax error at '" << theArgVec[anArgIter] << "'";
a4815d55 1146 return 1;
1147 }
1148 }
1149
1150 aMat->SetCommonMaterial (aMatCom);
1151 aMat->SetPbrMaterial (aMatPbr);
1152 return 0;
1153}
1154
1155// ================================================================
1156// Function : XRemoveVisMaterial
1157// Purpose :
1158// ================================================================
1159static Standard_Integer XRemoveVisMaterial (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
1160{
1161 if (theNbArgs != 3)
1162 {
d99f0355 1163 Message::SendFail() << "Syntax error: wrong number of arguments";
a4815d55 1164 return 1;
1165 }
1166
1167 Handle(TDocStd_Document) aDoc;
1168 DDocStd::GetDocument (theArgVec[1], aDoc);
1169 if (aDoc.IsNull())
1170 {
d99f0355 1171 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
a4815d55 1172 return 1;
1173 }
1174
1175 TDF_Label aMatLab = findVisMaterial (aDoc, theArgVec[2]);
1176 if (aMatLab.IsNull())
1177 {
d99f0355 1178 Message::SendFail() << "Syntax error: " << theArgVec[2] << " is not a material";
a4815d55 1179 return 1;
1180 }
1181
1182 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
1183 aMatTool->RemoveMaterial (aMatLab);
1184 return 0;
1185}
1186
1187// ================================================================
1188// Function : XSetVisMaterial
1189// Purpose :
1190// ================================================================
1191static Standard_Integer XSetVisMaterial (Draw_Interpretor& , Standard_Integer theNbArgs, const char** theArgVec)
1192{
1193 if (theNbArgs != 3 && theNbArgs != 4)
1194 {
d99f0355 1195 Message::SendFail() << "Syntax error: wrong number of arguments";
a4815d55 1196 return 1;
1197 }
1198
1199 Handle(TDocStd_Document) aDoc;
1200 TDF_Label aShapeLab;
1201 DDocStd::GetDocument (theArgVec[1], aDoc);
1202 if (aDoc.IsNull())
1203 {
d99f0355 1204 Message::SendFail() << "Syntax error: " << theArgVec[1] << " is not a document";
a4815d55 1205 return 1;
1206 }
1207
1208 TDF_Tool::Label (aDoc->GetData(), theArgVec[2], aShapeLab);
1209 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool (aDoc->Main());
1210 if (aShapeLab.IsNull())
1211 {
1212 // get label by shape
1213 TopoDS_Shape aShape = DBRep::Get (theArgVec[2]);
1214 if (!aShape.IsNull())
1215 {
1216 aShapeLab = aColorTool->ShapeTool()->FindShape (aShape, Standard_True);
1217 }
1218 }
1219 if (aShapeLab.IsNull())
1220 {
d99f0355 1221 Message::SendFail() << "Syntax error: " << theArgVec[2] << " is not a label not shape";
a4815d55 1222 return 1;
1223 }
1224
1225 TDF_Label aMatLab;
1226 if (theNbArgs == 4)
1227 {
1228 aMatLab = findVisMaterial (aDoc, theArgVec[3]);
1229 if (aMatLab.IsNull())
1230 {
d99f0355 1231 Message::SendFail() << "Syntax error: " << theArgVec[3] << " is not a material";
a4815d55 1232 return 1;
1233 }
1234 }
1235
1236 Handle(XCAFDoc_VisMaterialTool) aMatTool = XCAFDoc_DocumentTool::VisMaterialTool (aDoc->Main());
1237 aMatTool->SetShapeMaterial (aShapeLab, aMatLab);
1238 return 0;
1239}
1240
7fd59977 1241//=======================================================================
1242//function : InitCommands
1243//purpose :
1244//=======================================================================
1245
1246void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di)
1247{
7fd59977 1248 static Standard_Boolean initactor = Standard_False;
c48e2889 1249 if (initactor)
1250 {
1251 return;
1252 }
1253 initactor = Standard_True;
7fd59977 1254
1255 //=====================================
1256 // Work with colors
1257 //=====================================
1258
1259 Standard_CString g = "XDE color's commands";
9196ea9d 1260
1261 di.Add ("XSetColor","Doc {Label|Shape} R G B [alpha] [{generic|surface|curve}=gen]"
1262 "\t: Set color [R G B] to shape given by Label, "
ec99ba32 1263 "type of color 's' - for surface, 'c' - for curve (default generic)",
7fd59977 1264 __FILE__, setColor, g);
1265
9196ea9d 1266 di.Add ("XGetColor","Doc label"
1267 "\t: Return color defined on label in colortable",
7fd59977 1268 __FILE__, getColor, g);
1269
9196ea9d 1270 di.Add ("XGetShapeColor","Doc Label {generic|surface|curve}"
1271 "\t: Returns color defined by label",
7fd59977 1272 __FILE__, getShapeColor, g);
9196ea9d 1273
1274 di.Add ("XGetAllColors","Doc"
1275 "\t: Print all colors that defined in document",
7fd59977 1276 __FILE__, getAllColors, g);
1277
9196ea9d 1278 di.Add ("XAddColor","Doc R G B [alpha]"
1279 "\t: Add color in document to color table",
7fd59977 1280 __FILE__, addColor, g);
1281
9196ea9d 1282 di.Add ("XRemoveColor","Doc Label"
1283 "\t: Remove color in document from color table",
7fd59977 1284 __FILE__, removeColor, g);
1285
9196ea9d 1286 di.Add ("XFindColor","Doc R G B [alpha]"
1287 "\t: Find label where indicated color is situated",
7fd59977 1288 __FILE__, findColor, g);
1289
9196ea9d 1290 di.Add ("XUnsetColor","Doc {Label|Shape} {generic|surface|curve}"
1291 "\t: Unset color",
7fd59977 1292 __FILE__, unsetColor, g);
1293
1294 di.Add ("XSetObjVisibility","Doc {Label|Shape} (0\1) \t: Set the visibility of shape ",
1295 __FILE__, setVisibility, g);
1296
1297 di.Add ("XGetObjVisibility","Doc {Label|Shape} \t: Return the visibility of shape ",
1298 __FILE__, getVisibility, g);
1299
1300 di.Add ("XGetInstanceVisible","Doc Shape \t: Return the visibility of shape ",
1301 __FILE__, getStyledVisibility, g);
1302
9196ea9d 1303 di.Add ("XGetInstanceColor","Doc Shape [{generic|surface|curve}=gen]"
1304 "\t: Return the color of component shape",
7fd59977 1305 __FILE__, getStyledcolor, g);
1306
9196ea9d 1307 di.Add ("XSetInstanceColor","Doc Shape R G B [alpha] [{generic|surface|curve}=gen]"
1308 "\t: sets color for component of shape if SHUO structure exists already",
7fd59977 1309 __FILE__, setStyledcolor, g);
a4815d55 1310
1311 di.Add ("XGetAllVisMaterials","Doc [{-names|-labels}=-names]"
1312 "\t: Print all visualization materials defined in document",
1313 __FILE__, XGetAllVisMaterials, g);
1314 di.Add ("XGetVisMaterial","Doc {Material|Shape}"
1315 "\t: Print visualization material properties",
1316 __FILE__, XGetVisMaterial, g);
1317 di.Add ("XAddVisMaterial",
1318 "Doc Material"
0858125f 1319 "\n\t\t: [-transparency 0..1] [-alphaMode {Opaque|Mask|Blend|BlendAuto} CutOffValue] [-refractionIndex 1..3]"
a4815d55 1320 "\n\t\t: [-diffuse RGB] [-diffuseTexture ImagePath]"
1321 "\n\t\t: [-specular RGB] [-ambient RGB] [-emissive RGB] [-shininess 0..1]"
1322 "\n\t\t: [-baseColor RGB] [-baseColorTexture ImagePath]"
1323 "\n\t\t: [-emissiveFactor RGB] [-emissiveTexture ImagePath]"
1324 "\n\t\t: [-metallic 0..1] [-roughness 0..1] [-metallicRoughnessTexture ImagePath]"
1325 "\n\t\t: [-occlusionTexture ImagePath] [-normalTexture ImagePath]"
1326 "\n\t\t: [-doubleSided {0|1}]"
1327 "\n\t\t: Add material into Document's material table.",
1328 __FILE__, XAddVisMaterial, g);
1329 di.Add ("XRemoveVisMaterial","Doc Material"
1330 "\t: Remove material in document from material table",
1331 __FILE__, XRemoveVisMaterial, g);
1332 di.Add ("XSetVisMaterial", "Doc Shape Material"
1333 "\t: Set material to shape",
1334 __FILE__, XSetVisMaterial, g);
1335 di.Add ("XUnsetVisMaterial", "Doc Shape"
1336 "\t: Unset material from shape",
1337 __FILE__, XSetVisMaterial, g);
7fd59977 1338}