Integration of OCCT 6.5.0 from SVN
[occt.git] / src / XDEDRAW / XDEDRAW_Colors.cxx
CommitLineData
7fd59977 1// File: XDEDRAW_Colors.cxx
2// Created: Fri Aug 4 14:38:55 2000
3// Author: Pavel TELKOV
4// <ptv@zamox.nnov.matra-dtv.fr>
5
6#include <XDEDRAW_Colors.ixx>
7
8#include <DBRep.hxx>
9#include <DDocStd.hxx>
10
11#include <TCollection_AsciiString.hxx>
12#include <TopoDS_Shape.hxx>
13#include <Quantity_Color.hxx>
14
15#include <TDF_Tool.hxx>
16#include <TDF_Label.hxx>
17#include <TDF_LabelSequence.hxx>
18#include <TDocStd_Document.hxx>
19
20#include <XCAFDoc_DocumentTool.hxx>
21#include <XCAFDoc_ShapeTool.hxx>
22#include <XCAFDoc_ColorTool.hxx>
23
24//=======================================================================
25// Section: Work with colors
26//=======================================================================
27
28static Standard_Integer setColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
29{
30 if (argc <6) {
31 di<<"Use: "<<argv[0]<<" Doc {Label|Shape} R G B [curve|surf]"<<"\n";
32 return 1;
33 }
34 Handle(TDocStd_Document) Doc;
35 DDocStd::GetDocument(argv[1], Doc);
36 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
37
38 TDF_Label aLabel;
39 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
40 Quantity_Color Col ( atof(argv[3]), atof(argv[4]), atof(argv[5]), Quantity_TOC_RGB );
41
42 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
43 XCAFDoc_ColorType ctype = ( argc <=6 ? XCAFDoc_ColorGen :
44 argv[6][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv );
45 if ( !aLabel.IsNull() ) {
46 myColors->SetColor ( aLabel, Col, ctype );
47 }
48 else {
49 TopoDS_Shape aShape= DBRep::Get(argv[2]);
50 if ( !aShape.IsNull() ) {
51 myColors->SetColor ( aShape, Col, ctype );
52 }
53 }
54 return 0;
55}
56
57static Standard_Integer getColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
58{
59 if (argc!=3) {
60 di<<"Use: "<<argv[0]<<" Doc Label"<<"\n";
61 return 1;
62 }
63 Handle(TDocStd_Document) Doc;
64 DDocStd::GetDocument(argv[1], Doc);
65 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
66
67 TDF_Label aLabel;
68 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
69 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
70 Quantity_Color col;
71 if ( !myColors->GetColor(aLabel, col) ) return 0;
72
73 di << col.StringName ( col.Name() );
74
75 return 0;
76}
77
78static Standard_Integer getShapeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
79{
80 if (argc!=4) {
81 di<<"Use: "<<argv[0]<<" Doc Label ColorType(s/c)"<<"\n";
82 return 1;
83 }
84 Handle(TDocStd_Document) Doc;
85 DDocStd::GetDocument(argv[1], Doc);
86 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
87
88 TDF_Label aLabel;
89 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
90 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
91 Quantity_Color col;
92
93 if ( !myColors->GetColor(aLabel, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv, col) ) return 0;
94
95 TCollection_AsciiString Entry;
96 Entry = col.StringName ( col.Name() );
97 di << Entry.ToCString();
98
99 return 0;
100}
101
102static Standard_Integer getAllColors (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
103{
104 if (argc!=2) {
105 di<<"Use: "<<argv[0]<<" Doc "<<"\n";
106 return 1;
107 }
108 Handle(TDocStd_Document) Doc;
109 DDocStd::GetDocument(argv[1], Doc);
110 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
111
112 TDF_Label aLabel;
113 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
114 Quantity_Color col;
115 TDF_LabelSequence Labels;
116 myColors->GetColors(Labels);
117 if (Labels.Length() >= 1) {
118 for ( Standard_Integer i = 1; i<= Labels.Length(); i++) {
119 aLabel = Labels.Value(i);
120 if ( !myColors->GetColor(aLabel, col) ) continue;
121 di << col.StringName ( col.Name() );
122 di << " ";
123 }
124 }
125 return 0;
126}
127
128
129static Standard_Integer addColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
130{
131 if (argc!=5) {
132 di<<"Use: "<<argv[0]<<" DocName R G B"<<"\n";
133 return 1;
134 }
135 Handle(TDocStd_Document) Doc;
136 DDocStd::GetDocument(argv[1], Doc);
137 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
138
139 TDF_Label aLabel;
140 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
141
142 Quantity_Color Col ( atof(argv[2]), atof(argv[3]), atof(argv[4]), Quantity_TOC_RGB );
143 aLabel = myColors->AddColor(Col);
144
145 TCollection_AsciiString Entry;
146 TDF_Tool::Entry(aLabel, Entry);
147 di << Entry.ToCString();
148 return 0;
149}
150
151static Standard_Integer removeColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
152{
153 if (argc!=3) {
154 di<<"Use: "<<argv[0]<<" DocName Label"<<"\n";
155 return 1;
156 }
157 Handle(TDocStd_Document) Doc;
158 DDocStd::GetDocument(argv[1], Doc);
159 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
160
161 TDF_Label aLabel;
162 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
163 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
164 myColors->RemoveColor(aLabel);
165
166 return 0;
167}
168
169static Standard_Integer findColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
170{
171 if (argc!=5) {
172 di<<"Use: "<<argv[0]<<" DocName R G B"<<"\n";
173 return 1;
174 }
175 Handle(TDocStd_Document) Doc;
176 DDocStd::GetDocument(argv[1], Doc);
177 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
178
179 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
180
181 Quantity_Color Col ( atof(argv[2]), atof(argv[3]), atof(argv[4]), Quantity_TOC_RGB );
182
183 TCollection_AsciiString Entry;
184 TDF_Tool::Entry(myColors->FindColor(Col), Entry);
185 di << Entry.ToCString();
186 return 0;
187}
188
189static Standard_Integer unsetColor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
190{
191 if (argc!=4) {
192 di<<"Use: "<<argv[0]<<" DocName {Label|Shape} ColorType"<<"\n";
193 return 1;
194 }
195 Handle(TDocStd_Document) Doc;
196 DDocStd::GetDocument(argv[1], Doc);
197 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
198
199 TDF_Label aLabel;
200 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
201 Handle(XCAFDoc_ColorTool) myColors = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
202 if ( !aLabel.IsNull() ) {
203 myColors->UnSetColor(aLabel, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv);
204 }
205 TopoDS_Shape aShape= DBRep::Get(argv[2]);
206 if ( !aShape.IsNull() ) {
207 myColors->UnSetColor(aShape, argv[3][0] == 's' ? XCAFDoc_ColorSurf : XCAFDoc_ColorCurv);
208 }
209 return 0;
210}
211
212static Standard_Integer setVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
213{
214 if (argc<3) {
215 di<<"Use: "<<argv[0]<<"DocName {Lable|Shape} [isvisible(1/0)]"<<"\n";
216 return 1;
217 }
218 Handle(TDocStd_Document) Doc;
219 DDocStd::GetDocument(argv[1], Doc);
220 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
221 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
222 Standard_Boolean isvisible = Standard_False;
223 if ( (argc==4) && (atoi(argv[3])==1) ) isvisible = Standard_True;
224
225 TDF_Label aLabel;
226 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
227 if ( aLabel.IsNull() ) {
228 // get label by shape
229 TopoDS_Shape aShape= DBRep::Get(argv[2]);
230 if ( !aShape.IsNull() ) {
231 aLabel = localTool->ShapeTool()->FindShape( aShape, Standard_True );
232 }
233 }
234 if ( aLabel.IsNull() ) {
235 di << " cannot find indicated label in document" << "\n";
236 return 1;
237 }
238 localTool->SetVisibility( aLabel, isvisible );
239 return 0;
240}
241
242static Standard_Integer getVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
243{
244 if (argc<3) {
245 di<<"Use: "<<argv[0]<<"DocName {Lable|Shape}"<<"\n";
246 return 1;
247 }
248 Handle(TDocStd_Document) Doc;
249 DDocStd::GetDocument(argv[1], Doc);
250 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
251 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
252 TDF_Label aLabel;
253 TDF_Tool::Label(Doc->GetData(), argv[2], aLabel);
254 if ( aLabel.IsNull() ) {
255 // get label by shape
256 TopoDS_Shape aShape= DBRep::Get(argv[2]);
257 if ( !aShape.IsNull() ) {
258 aLabel = localTool->ShapeTool()->FindShape( aShape, Standard_True );
259 }
260 }
261 if ( aLabel.IsNull() ) {
262 di << " cannot find indicated label in document" << "\n";
263 return 1;
264 }
265 if (localTool->IsVisible( aLabel) ) di << 1;
266 else di << 0;
267 return 0;
268}
269
270static Standard_Integer getStyledVisibility (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
271{
272 if (argc<3) {
273 di<<"Use: "<<argv[0]<<"DocName Shape"<<"\n";
274 return 1;
275 }
276 Handle(TDocStd_Document) Doc;
277 DDocStd::GetDocument(argv[1], Doc);
278 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
279 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
280 TopoDS_Shape aShape;
281 aShape = DBRep::Get(argv[2]);
282 if (localTool->IsInstanceVisible( aShape) ) di << 1;
283 else di << 0;
284 return 0;
285}
286
287static Standard_Integer getStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
288{
289 if (argc<3) {
290 di<<"Use: "<<argv[0]<<" Doc shape colortype(s/c)"<<"\n";
291 return 1;
292 }
293 Handle(TDocStd_Document) Doc;
294 DDocStd::GetDocument(argv[1], Doc);
295 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
296 TopoDS_Shape aShape;
297 aShape = DBRep::Get(argv[2]);
298
299 Quantity_Color col;
300 XCAFDoc_ColorType type;
301 if ( argv[3] && argv[3][0] == 's' )
302 type = XCAFDoc_ColorSurf;
303 else if ( argv[3] && argv[3][0] == 'c' )
304 type = XCAFDoc_ColorCurv;
305 else
306 type = XCAFDoc_ColorGen;
307 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
308 if (localTool->GetInstanceColor( aShape, type, col) )
309 {
310 TCollection_AsciiString Entry;
311 Entry = col.StringName ( col.Name() );
312 di << Entry.ToCString();
313 }
314 return 0;
315}
316
317static Standard_Integer setStyledcolor (Draw_Interpretor& di, Standard_Integer argc, const char** argv)
318{
319 if (argc<6) {
320 di<<"Use: "<<argv[0]<<" Doc shape R G B type(s/c)"<<"\n";
321 return 1;
322 }
323 Handle(TDocStd_Document) Doc;
324 DDocStd::GetDocument(argv[1], Doc);
325 if ( Doc.IsNull() ) { di << argv[1] << " is not a document" << "\n"; return 1; }
326 TopoDS_Shape aShape;
327 aShape = DBRep::Get(argv[2]);
328
329 Quantity_Color col ( atof(argv[3]), atof(argv[4]), atof(argv[5]), Quantity_TOC_RGB );
330 XCAFDoc_ColorType type;
331 if ( argv[6] && argv[6][0] == 's' )
332 type = XCAFDoc_ColorSurf;
333 else if ( argv[6] && argv[6][0] == 'c' )
334 type = XCAFDoc_ColorCurv;
335 else
336 type = XCAFDoc_ColorGen;
337 Handle(XCAFDoc_ColorTool) localTool = XCAFDoc_DocumentTool::ColorTool(Doc->Main());
338 if (!localTool->SetInstanceColor( aShape, type, col) )
339 {
340 di << "cannot set color for the indicated component" << "\n";
341 return 1;
342 }
343 return 0;
344}
345
346//=======================================================================
347//function : InitCommands
348//purpose :
349//=======================================================================
350
351void XDEDRAW_Colors::InitCommands(Draw_Interpretor& di)
352{
353
354 static Standard_Boolean initactor = Standard_False;
355 if (initactor) return; initactor = Standard_True;
356
357 //=====================================
358 // Work with colors
359 //=====================================
360
361 Standard_CString g = "XDE color's commands";
362
363 di.Add ("XSetColor","Doc {Label|Shape} R G B \t: Set color [R G B] to shape given by Label",
364 __FILE__, setColor, g);
365
366 di.Add ("XGetColor","Doc label \t: Return color defined on label in colortable",
367 __FILE__, getColor, g);
368
369 di.Add ("XGetShapeColor","Doc Label ColorType \t: Returns color defined by label",
370 __FILE__, getShapeColor, g);
371
372 di.Add ("XGetAllColors","Doc \t: Print all colors that defined in document",
373 __FILE__, getAllColors, g);
374
375 di.Add ("XAddColor","Doc R G B \t: Add color in document to color table",
376 __FILE__, addColor, g);
377
378 di.Add ("XRemoveColor","Doc Label \t: Remove color in document from color table",
379 __FILE__, removeColor, g);
380
381 di.Add ("XFindColor","Doc R G B \t: Find label where indicated color is situated",
382 __FILE__, findColor, g);
383
384 di.Add ("XUnsetColor","Doc {Label|Shape} ColorType \t: Unset color ",
385 __FILE__, unsetColor, g);
386
387 di.Add ("XSetObjVisibility","Doc {Label|Shape} (0\1) \t: Set the visibility of shape ",
388 __FILE__, setVisibility, g);
389
390 di.Add ("XGetObjVisibility","Doc {Label|Shape} \t: Return the visibility of shape ",
391 __FILE__, getVisibility, g);
392
393 di.Add ("XGetInstanceVisible","Doc Shape \t: Return the visibility of shape ",
394 __FILE__, getStyledVisibility, g);
395
396 di.Add ("XGetInstanceColor","Doc Shape \t: Return the color of component shape ",
397 __FILE__, getStyledcolor, g);
398
399 di.Add ("XSetInstanceColor","Doc Shape color type \t: sets color for component of shape if SHUO structure exists already ",
400 __FILE__, setStyledcolor, g);
401
402}