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