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 <Quantity_Color.hxx>
21 #include <TCollection_AsciiString.hxx>
22 #include <TDF_Label.hxx>
23 #include <TDF_LabelSequence.hxx>
24 #include <TDF_Tool.hxx>
25 #include <TDocStd_Document.hxx>
26 #include <TopoDS_Shape.hxx>
27 #include <XCAFDoc_ColorTool.hxx>
28 #include <XCAFDoc_DocumentTool.hxx>
29 #include <XCAFDoc_ShapeTool.hxx>
30 #include <XDEDRAW_Colors.hxx>
32 //=======================================================================
33 // Section: Work with colors
34 //=======================================================================
35 static Standard_Integer setColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
38 di<<"Use: "<<argv[0]<<" Doc {Label|Shape} R G B [curve|surf]\n";
41 Handle(TDocStd_Document) Doc;
42 DDocStd::GetDocument(argv[1], Doc);
43 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
46 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
47 Quantity_Color Col ( Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atof(argv[5]), Quantity_TOC_RGB );
49 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
50 const XCAFDoc_ColorType ctype = ( argc <= 6 ? XCAFDoc_ColorGen : ( argv[6][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv ) );
51 if ( !aLabel.IsNull() ) {
52 myColors->SetColor ( aLabel, Col, ctype );
55 TopoDS_Shape aShape= DBRep::Get(argv[2]);
56 if ( !aShape.IsNull() ) {
57 myColors->SetColor ( aShape, Col, ctype );
63 static Standard_Integer getColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
66 di<<"Use: "<<argv[0]<<" Doc Label\n";
69 Handle(TDocStd_Document) Doc;
70 DDocStd::GetDocument(argv[1], Doc);
71 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
74 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
75 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
77 if ( !myColors->GetColor(aLabel, col) ) return 0;
79 di << col.StringName ( col.Name() );
84 static Standard_Integer getShapeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
87 di<<"Use: "<<argv[0]<<" Doc Label [curve|surf]\n";
90 Handle(TDocStd_Document) Doc;
91 DDocStd::GetDocument(argv[1], Doc);
92 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
95 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
96 if ( aLabel.IsNull() ) {
97 di << " no such label in document\n";
101 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
102 const XCAFDoc_ColorType ctype = ( argc <= 3 ? XCAFDoc_ColorGen : ( argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv ) );
105 if ( !myColors->GetColor(aLabel, ctype, col) ) return 0;
107 TCollection_AsciiString Entry;
108 Entry = col.StringName ( col.Name() );
109 di << Entry.ToCString();
114 static Standard_Integer getAllColors (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
117 di<<"Use: "<<argv[0]<<" Doc \n";
120 Handle(TDocStd_Document) Doc;
121 DDocStd::GetDocument(argv[1], Doc);
122 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
125 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
127 TDF_LabelSequence Labels;
128 myColors->GetColors(Labels);
129 if (Labels.Length() >= 1) {
130 for ( Standard_Integer i = 1; i<= Labels.Length(); i++) {
131 aLabel = Labels.Value(i);
132 if ( !myColors->GetColor(aLabel, col) ) continue;
133 di << col.StringName ( col.Name() );
141 static Standard_Integer addColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
144 di<<"Use: "<<argv[0]<<" DocName R G B\n";
147 Handle(TDocStd_Document) Doc;
148 DDocStd::GetDocument(argv[1], Doc);
149 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
152 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
154 Quantity_Color Col ( Draw::Atof(argv[2]), Draw::Atof(argv[3]), Draw::Atof(argv[4]), Quantity_TOC_RGB );
155 aLabel = myColors->AddColor(Col);
157 TCollection_AsciiString Entry;
158 TDF_Tool::Entry(aLabel, Entry);
159 di << Entry.ToCString();
163 static Standard_Integer removeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
166 di<<"Use: "<<argv[0]<<" DocName Label\n";
169 Handle(TDocStd_Document) Doc;
170 DDocStd::GetDocument(argv[1], Doc);
171 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
174 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
175 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
176 myColors->RemoveColor(aLabel);
181 static Standard_Integer findColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
184 di<<"Use: "<<argv[0]<<" DocName R G B\n";
187 Handle(TDocStd_Document) Doc;
188 DDocStd::GetDocument(argv[1], Doc);
189 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
191 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
193 Quantity_Color Col ( Draw::Atof(argv[2]), Draw::Atof(argv[3]), Draw::Atof(argv[4]), Quantity_TOC_RGB );
195 TCollection_AsciiString Entry;
196 TDF_Tool::Entry(myColors->FindColor(Col), Entry);
197 di << Entry.ToCString();
201 static Standard_Integer unsetColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
204 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} ColorType\n";
207 Handle(TDocStd_Document) Doc;
208 DDocStd::GetDocument(argv[1], Doc);
209 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
212 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
213 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
214 if ( !aLabel.IsNull() ) {
215 myColors->UnSetColor(aLabel, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv);
217 TopoDS_Shape aShape= DBRep::Get(argv[2]);
218 if ( !aShape.IsNull() ) {
219 myColors->UnSetColor(aShape, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv);
224 static Standard_Integer setVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
227 di<<"Use: "<<argv[0]<<"DocName {Lable|Shape} [isvisible(1/0)]\n";
230 Handle(TDocStd_Document) Doc;
231 DDocStd::GetDocument(argv[1], Doc);
232 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
233 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
234 Standard_Boolean isvisible = Standard_False;
235 if ( (argc==4) && (Draw::Atoi(argv[3])==1) ) isvisible = Standard_True;
238 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
239 if ( aLabel.IsNull() ) {
240 // get label by shape
241 TopoDS_Shape aShape= DBRep::Get(argv[2]);
242 if ( !aShape.IsNull() ) {
243 aLabel = localTool->ShapeTool()->FindShape( aShape, Standard_True );
246 if ( aLabel.IsNull() ) {
247 di << " cannot find indicated label in document\n";
250 localTool->SetVisibility( aLabel, isvisible );
254 static Standard_Integer getVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
257 di<<"Use: "<<argv[0]<<"DocName {Lable|Shape}\n";
260 Handle(TDocStd_Document) Doc;
261 DDocStd::GetDocument(argv[1], Doc);
262 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
263 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
265 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
266 if ( aLabel.IsNull() ) {
267 // get label by shape
268 TopoDS_Shape aShape= DBRep::Get(argv[2]);
269 if ( !aShape.IsNull() ) {
270 aLabel = localTool->ShapeTool()->FindShape( aShape, Standard_True );
273 if ( aLabel.IsNull() ) {
274 di << " cannot find indicated label in document\n";
277 if (localTool->IsVisible( aLabel) ) di << 1;
282 static Standard_Integer getStyledVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
285 di<<"Use: "<<argv[0]<<"DocName Shape\n";
288 Handle(TDocStd_Document) Doc;
289 DDocStd::GetDocument(argv[1], Doc);
290 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
291 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
293 aShape = DBRep::Get(argv[2]);
294 if (localTool->IsInstanceVisible( aShape) ) di << 1;
299 static Standard_Integer getStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
302 di<<"Use: "<<argv[0]<<" Doc shape colortype(s/c)\n";
305 Handle(TDocStd_Document) Doc;
306 DDocStd::GetDocument(argv[1], Doc);
307 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
309 aShape = DBRep::Get(argv[2]);
312 XCAFDoc_ColorType type;
313 if ( argv[3] && argv[3][0] == 's' )
314 type = XCAFDoc_ColorSurf;
315 else if ( argv[3] && argv[3][0] == 'c' )
316 type = XCAFDoc_ColorCurv;
318 type = XCAFDoc_ColorGen;
319 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
320 if (localTool->GetInstanceColor( aShape, type, col) )
322 TCollection_AsciiString Entry;
323 Entry = col.StringName ( col.Name() );
324 di << Entry.ToCString();
329 static Standard_Integer setStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
332 di<<"Use: "<<argv[0]<<" Doc shape R G B type(s/c)\n";
335 Handle(TDocStd_Document) Doc;
336 DDocStd::GetDocument(argv[1], Doc);
337 if ( Doc.IsNull() ) { di << argv[1] << " is not a document\n"; return 1; }
339 aShape = DBRep::Get(argv[2]);
341 Quantity_Color col ( Draw::Atof(argv[3]), Draw::Atof(argv[4]), Draw::Atof(argv[5]), Quantity_TOC_RGB );
342 XCAFDoc_ColorType type;
343 if ( argv[6] && argv[6][0] == 's' )
344 type = XCAFDoc_ColorSurf;
345 else if ( argv[6] && argv[6][0] == 'c' )
346 type = XCAFDoc_ColorCurv;
348 type = XCAFDoc_ColorGen;
349 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
350 if (!localTool->SetInstanceColor( aShape, type, col) )
352 di << "cannot set color for the indicated component\n";
358 //=======================================================================
359 //function : InitCommands
361 //=======================================================================
363 void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di)
366 static Standard_Boolean initactor = Standard_False;
367 if (initactor) return; initactor = Standard_True;
369 //=====================================
371 //=====================================
373 Standard_CString g = "XDE color's commands";
375 di.Add ("XSetColor","Doc {Label|Shape} R G B [c|s]\t: Set color [R G B] to shape given by Label, "
376 "type of color 's' - for surface, 'c' - for curve (default generic)",
377 __FILE__, setColor, g);
379 di.Add ("XGetColor","Doc label \t: Return color defined on label in colortable",
380 __FILE__, getColor, g);
382 di.Add ("XGetShapeColor","Doc Label ColorType \t: Returns color defined by label",
383 __FILE__, getShapeColor, g);
385 di.Add ("XGetAllColors","Doc \t: Print all colors that defined in document",
386 __FILE__, getAllColors, g);
388 di.Add ("XAddColor","Doc R G B \t: Add color in document to color table",
389 __FILE__, addColor, g);
391 di.Add ("XRemoveColor","Doc Label \t: Remove color in document from color table",
392 __FILE__, removeColor, g);
394 di.Add ("XFindColor","Doc R G B \t: Find label where indicated color is situated",
395 __FILE__, findColor, g);
397 di.Add ("XUnsetColor","Doc {Label|Shape} ColorType \t: Unset color ",
398 __FILE__, unsetColor, g);
400 di.Add ("XSetObjVisibility","Doc {Label|Shape} (0\1) \t: Set the visibility of shape ",
401 __FILE__, setVisibility, g);
403 di.Add ("XGetObjVisibility","Doc {Label|Shape} \t: Return the visibility of shape ",
404 __FILE__, getVisibility, g);
406 di.Add ("XGetInstanceVisible","Doc Shape \t: Return the visibility of shape ",
407 __FILE__, getStyledVisibility, g);
409 di.Add ("XGetInstanceColor","Doc Shape \t: Return the color of component shape ",
410 __FILE__, getStyledcolor, g);
412 di.Add ("XSetInstanceColor","Doc Shape color type \t: sets color for component of shape if SHUO structure exists already ",
413 __FILE__, setStyledcolor, g);