0024166: Unable to create file with "Save" menu of voxeldemo Qt sample
[occt.git] / src / HLRTest / HLRTest.cxx
CommitLineData
b311480e 1// Created on: 1995-04-05
2// Created by: Christophe MARION
3// Copyright (c) 1995-1999 Matra Datavision
4// Copyright (c) 1999-2012 OPEN CASCADE SAS
5//
6// The content of this file is subject to the Open CASCADE Technology Public
7// License Version 6.5 (the "License"). You may not use the content of this file
8// except in compliance with the License. Please obtain a copy of the License
9// at http://www.opencascade.org and read it completely before using this file.
10//
11// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13//
14// The Original Code and all software distributed under the License is
15// distributed on an "AS IS" basis, without warranty of any kind, and the
16// Initial Developer hereby disclaims all such warranties, including without
17// limitation, any warranties of merchantability, fitness for a particular
18// purpose or non-infringement. Please see the License for the specific terms
19// and conditions governing the rights and limitations under the License.
20
7fd59977 21
22#include <HLRTest.ixx>
23#include <HLRTest_Projector.hxx>
24#include <HLRTest_OutLiner.hxx>
25#include <TColStd_ListIteratorOfListOfInteger.hxx>
26#include <gp_Ax3.hxx>
27#include <Draw_Appli.hxx>
28#include <Draw.hxx>
29#include <DBRep.hxx>
30#include <HLRBRep_Algo.hxx>
31#include <HLRBRep_HLRToShape.hxx>
bda83605 32#include <HLRAppli_ReflectLines.hxx>
33
7fd59977 34static Handle(HLRBRep_Algo) hider;
35#ifdef WNT
36Standard_IMPORT Draw_Viewer dout;
37#endif
38
39#include <BRepTopAdaptor_MapOfShapeTool.hxx>
40
41//=======================================================================
42//function : Set
43//purpose :
44//=======================================================================
45void HLRTest::Set (const Standard_CString Name,
46 const HLRAlgo_Projector& P)
47{
48 Draw::Set(Name,new HLRTest_Projector(P));
49}
50
51//=======================================================================
52//function : GetProjector
53//purpose :
54//=======================================================================
55Standard_Boolean HLRTest::GetProjector (Standard_CString& Name,
56 HLRAlgo_Projector& P)
57{
58 Handle(HLRTest_Projector) HP =
59 Handle(HLRTest_Projector)::DownCast(Draw::Get(Name));
60 if (HP.IsNull()) return Standard_False;
61 P = HP->Projector();
62 return Standard_True;
63
64}
65
66//=======================================================================
67//function : Set
68//purpose :
69//=======================================================================
70
71void HLRTest::Set (const Standard_CString Name,
72 const TopoDS_Shape& S)
73{
74 Draw::Set(Name,new HLRTest_OutLiner(S));
75}
76
77//=======================================================================
78//function : GetOutLiner
79//purpose :
80//=======================================================================
81Handle(HLRTopoBRep_OutLiner) HLRTest::GetOutLiner (Standard_CString& Name)
82{
83 Handle(Draw_Drawable3D) D = Draw::Get(Name);
84 Handle(HLRTest_OutLiner) HS = Handle(HLRTest_OutLiner)::DownCast(D);
85 if (!HS.IsNull()) return HS->OutLiner();
86 Handle(HLRTopoBRep_OutLiner) HO;
87 return HO;
88}
89
90//=======================================================================
91//function : hprj
92//purpose :
93//=======================================================================
94
95static Standard_Integer
96hprj (Draw_Interpretor& , Standard_Integer n, const char** a)
97{
98 Standard_Integer id = 1;
99 if (n < 2) return 1;
91322f44 100 if (n > 2) id = Draw::Atoi(a[2]);
7fd59977 101 //
102 gp_Ax2 anAx2 = gp::XOY();
103 if (n == 11)
104 {
91322f44 105 Standard_Real x = Draw::Atof(a[2]);
106 Standard_Real y = Draw::Atof(a[3]);
107 Standard_Real z = Draw::Atof(a[4]);
7fd59977 108
91322f44 109 Standard_Real dx = Draw::Atof(a[5]);
110 Standard_Real dy = Draw::Atof(a[6]);
111 Standard_Real dz = Draw::Atof(a[7]);
7fd59977 112
91322f44 113 Standard_Real dx1 = Draw::Atof(a[8]);
114 Standard_Real dy1 = Draw::Atof(a[9]);
115 Standard_Real dz1 = Draw::Atof(a[10]);
7fd59977 116
117 gp_Pnt anOrigin (x, y, z);
118 gp_Dir aNormal (dx, dy, dz);
119 gp_Dir aDX (dx1, dy1, dz1);
120 anAx2 = gp_Ax2(anOrigin, aNormal, aDX);
121 }
122
123 HLRAlgo_Projector P(anAx2);
124 HLRTest::Set(a[1],P);
125 return 0;
126}
127
128//=======================================================================
129//function : hout
130//purpose :
131//=======================================================================
132
133static Standard_Integer
134hout (Draw_Interpretor& di, Standard_Integer n, const char** a)
135{
136 if (n < 2) return 1;
137 const char *name = a[2];
138 TopoDS_Shape S = DBRep::Get(name);
139 if (S.IsNull()) {
140 di << name << " is not a shape." << "\n";
141 return 1;
142 }
143 HLRTest::Set(a[1],S);
144 return 0;
145}
146
147//=======================================================================
148//function : hfil
149//purpose :
150//=======================================================================
151
152static Standard_Integer
153hfil (Draw_Interpretor& di, Standard_Integer n, const char** a)
154{
155 Standard_Integer nbIso = 0;
156 if (n < 3) return 1;
91322f44 157 if (n > 3) nbIso = Draw::Atoi(a[3]);
7fd59977 158 const char *name1 = a[1];
159 Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name1);
160 if (HS.IsNull()) {
161 di << name1 << " is not an OutLiner." << "\n";
162 return 1;
163 }
164 const char *name2 = a[2];
165 HLRAlgo_Projector P;
166 if (!HLRTest::GetProjector(name2,P)) {
167 di << name2 << " is not a projector." << "\n";
168 return 1;
169 }
170 BRepTopAdaptor_MapOfShapeTool MST;
171 HS->Fill(P,MST,nbIso);
172 return 0;
173}
174
175//=======================================================================
176//function : sori
177//purpose :
178//=======================================================================
179
180static Standard_Integer
181sori (Draw_Interpretor& di, Standard_Integer n, const char** a)
182{
183 if (n < 3) return 1;
184 const char *name1 = a[1];
185 const char *name2 = a[2];
186 Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name2);
187 if (HS.IsNull()) {
188 di << name2 << " is not an OutLiner." << "\n";
189 return 1;
190 }
191 DBRep::Set(name1,HS->OriginalShape());
192 return 0;
193}
194
195//=======================================================================
196//function : sout
197//purpose :
198//=======================================================================
199
200static Standard_Integer
201sout (Draw_Interpretor& di, Standard_Integer n, const char** a)
202{
203 if (n < 3) return 1;
204 const char *name1 = a[1];
205 const char *name2 = a[2];
206 Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name2);
207 if (HS.IsNull()) {
208 di << name2 << " is not an OutLiner." << "\n";
209 return 1;
210 }
211 if (HS->OutLinedShape().IsNull()) {
212 di << name2 << " has no OutLinedShape." << "\n";
213 return 1;
214 }
215 DBRep::Set(name1,HS->OutLinedShape());
216 return 0;
217}
218
219//=======================================================================
220//function : hloa
221//purpose :
222//=======================================================================
223
224static Standard_Integer
225hloa (Draw_Interpretor& di, Standard_Integer n, const char** a)
226{
227 if (n < 2) return 1;
228 const char *name1 = a[1];
229 Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name1);
230 if (HS.IsNull()) {
231 di << name1 << " is not an OutLiner." << "\n";
232 return 1;
233 }
234 hider->Load(HS);
235 return 0;
236}
237
238//=======================================================================
239//function : hrem
240//purpose :
241//=======================================================================
242
243static Standard_Integer
244hrem (Draw_Interpretor& di, Standard_Integer n, const char** a)
245{
246 if (n > 1) {
247 const char *name = a[1];
248 Standard_Integer index;
249 Handle(HLRTopoBRep_OutLiner) HS = HLRTest::GetOutLiner(name);
250 if (HS.IsNull()) {
251 TopoDS_Shape S = DBRep::Get(name);
252 if (S.IsNull()) {
253 di << name << " is not an OutLiner and not a shape." << "\n";
254 return 1;
255 }
256 else {
257 index = hider->Index(S);
258 if (index == 0) {
259 di << name << " not loaded shape." << "\n";
260 return 1;
261 }
262 }
263 }
264 else {
265 index = hider->Index(HS->OriginalShape());
266 if (index == 0) {
267 di << name << " not loaded outliner." << "\n";
268 return 1;
269 }
270 }
271 hider->Remove(index);
272 di << name << " removed" << "\n";
273 }
274 else {
275 while (hider->NbShapes() > 0) {
276 hider->Remove(1);
277 }
278 di << " all shapes removed" << "\n";
279 }
280 return 0;
281}
282
283//=======================================================================
284//function : sprj
285//purpose :
286//=======================================================================
287
288static Standard_Integer
289sprj (Draw_Interpretor& di, Standard_Integer n, const char** a)
290{
291 if (n < 2) return 1;
292 const char *name = a[1];
293 HLRAlgo_Projector P;
294 if (!HLRTest::GetProjector(name,P)) {
295 di << name << " is not a projector." << "\n";
296 return 1;
297 }
298 hider->Projector(P);
299 return 0;
300}
301
302//=======================================================================
303//function : upda
304//purpose :
305//=======================================================================
306
307static Standard_Integer
308upda (Draw_Interpretor& , Standard_Integer, const char**)
309{
310 hider->Update();
311 return 0;
312}
313
314//=======================================================================
315//function : hide
316//purpose :
317//=======================================================================
318
319static Standard_Integer
320hide (Draw_Interpretor& , Standard_Integer, const char**)
321{
322 hider->Hide();
323 return 0;
324}
325
326//=======================================================================
327//function : show
328//purpose :
329//=======================================================================
330
331static Standard_Integer
332show (Draw_Interpretor& , Standard_Integer, const char**)
333{
334 hider->ShowAll();
335 return 0;
336}
337
338//=======================================================================
339//function : hdbg
340//purpose :
341//=======================================================================
342
343static Standard_Integer
344hdbg (Draw_Interpretor& di, Standard_Integer, const char**)
345{
346 hider->Debug(!hider->Debug());
347 if (hider->Debug())
348 di << "debug" << "\n";
349 else
350 di << "no debug" << "\n";
351 return 0;
352}
353
354//=======================================================================
355//function : hnul
356//purpose :
357//=======================================================================
358
359static Standard_Integer
360hnul (Draw_Interpretor& , Standard_Integer, const char**)
361{
362 hider->OutLinedShapeNullify();
363 return 0;
364}
365
366//=======================================================================
367//function : hres
368//purpose :
369//=======================================================================
370
371static Standard_Integer
372hres (Draw_Interpretor& , Standard_Integer n, const char** a)
373{
374 TopoDS_Shape S,V,V1,VN,VO,VI,H,H1,HN,HO,HI;
375 if (n > 1) {
376 const char *name = a[1];
377 S = DBRep::Get(name);
378 }
379 HLRBRep_HLRToShape HS(hider);
380
381 if (S.IsNull()) {
382 V = HS.VCompound();
383 V1 = HS.Rg1LineVCompound();
384 VN = HS.RgNLineVCompound();
385 VO = HS.OutLineVCompound();
386 VI = HS.IsoLineVCompound();
387 H = HS.HCompound();
388 H1 = HS.Rg1LineHCompound();
389 HN = HS.RgNLineHCompound();
390 HO = HS.OutLineHCompound();
391 HI = HS.IsoLineHCompound();
392 }
393 else {
394 V = HS.VCompound(S);
395 V1 = HS.Rg1LineVCompound(S);
396 VN = HS.RgNLineVCompound(S);
397 VO = HS.OutLineVCompound(S);
398 VI = HS.IsoLineVCompound(S);
399 H = HS.HCompound(S);
400 H1 = HS.Rg1LineHCompound(S);
401 HN = HS.RgNLineHCompound(S);
402 HO = HS.OutLineHCompound(S);
403 HI = HS.IsoLineHCompound(S);
404 }
405 if (!V .IsNull()) DBRep::Set("vl",V);
406 if (!V1.IsNull()) DBRep::Set("v1l",V1);
407 if (!VN.IsNull()) DBRep::Set("vnl",VN);
408 if (!VO.IsNull()) DBRep::Set("vol",VO);
409 if (!VI.IsNull()) DBRep::Set("vil",VI);
410 if (!H .IsNull()) DBRep::Set("hl",H);
411 if (!H1.IsNull()) DBRep::Set("h1l",H1);
412 if (!HN.IsNull()) DBRep::Set("hnl",HN);
413 if (!HO.IsNull()) DBRep::Set("hol",HO);
414 if (!HI.IsNull()) DBRep::Set("hil",HI);
415 return 0;
416}
417
418//=======================================================================
bda83605 419//function : reflectlines
420//purpose :
421//=======================================================================
422
423static Standard_Integer reflectlines(Draw_Interpretor& , Standard_Integer n, const char** a)
424{
425 if (n < 6)
426 return 1;
427
428 TopoDS_Shape aShape = DBRep::Get(a[2]);
429 if (aShape.IsNull())
430 return 1;
431
432 Standard_Real anAISViewProjX = atof(a[3]);
433 Standard_Real anAISViewProjY = atof(a[4]);
434 Standard_Real anAISViewProjZ = atof(a[5]);
435
436 gp_Pnt anOrigin(0.,0.,0.);
437 gp_Dir aNormal(anAISViewProjX, anAISViewProjY, anAISViewProjZ);
438 gp_Ax2 theAxes(anOrigin, aNormal);
439 gp_Dir aDX = theAxes.XDirection();
440
441 HLRAppli_ReflectLines Reflector(aShape);
442
443 Reflector.SetAxes(aNormal.X(), aNormal.Y(), aNormal.Z(),
444 anOrigin.X(), anOrigin.Y(), anOrigin.Z(),
445 aDX.X(), aDX.Y(), aDX.Z());
446
447 Reflector.Perform();
448
449 TopoDS_Shape Result = Reflector.GetResult();
450 DBRep::Set(a[1], Result);
451
452 return 0;
453}
454
455//=======================================================================
7fd59977 456//function : Commands
457//purpose :
458//=======================================================================
459
460void HLRTest::Commands (Draw_Interpretor& theCommands)
461{
462 const char* g = "ADVALGOS HLR Commands";
463
464 theCommands.Add("hprj" ,"hprj name [view-id = 1]" ,__FILE__,hprj,g);
465 theCommands.Add("houtl" ,"houtl name shape" ,__FILE__,hout,g);
466 theCommands.Add("hfill" ,"hfill name proj [nbIso]" ,__FILE__,hfil,g);
467 theCommands.Add("hsin" ,"hsin name outliner" ,__FILE__,sori,g);
468 theCommands.Add("hsout" ,"hsout name outliner" ,__FILE__,sout,g);
469 theCommands.Add("hload" ,"hload outliner" ,__FILE__,hloa,g);
470 theCommands.Add("hremove" ,"hremove [name]" ,__FILE__,hrem,g);
471 theCommands.Add("hsetprj" ,"hsetprj [name]" ,__FILE__,sprj,g);
472 theCommands.Add("hupdate" ,"hupdate" ,__FILE__,upda,g);
473 theCommands.Add("hhide" ,"hhide" ,__FILE__,hide,g);
474 theCommands.Add("hshowall" ,"hshowall" ,__FILE__,show,g);
475 theCommands.Add("hdebug" ,"hdebug" ,__FILE__,hdbg,g);
476 theCommands.Add("hnullify" ,"hnullify" ,__FILE__,hnul,g);
477 theCommands.Add("hres2d" ,"hres2d" ,__FILE__,hres,g);
bda83605 478
479 theCommands.Add("reflectlines",
480 "reflectlines res shape proj_X proj_Y proj_Z",
481 __FILE__, reflectlines, g);
482
7fd59977 483 hider = new HLRBRep_Algo();
484}
485
486//=======================================================================
487//function : save and restore projector
488//purpose :
489//=======================================================================
490
491static Standard_Boolean stest(const Handle(Draw_Drawable3D)& d)
492{
493 return d->IsInstance(STANDARD_TYPE(HLRTest_Projector));
494}
495
496//=======================================================================
497//function : ssave
498//purpose :
499//=======================================================================
500
501static void ssave (const Handle(Draw_Drawable3D)&d, ostream& OS)
502{
503 Handle(HLRTest_Projector) HP =
504 Handle(HLRTest_Projector)::DownCast(d);
505
506 const HLRAlgo_Projector& P = HP->Projector();
507 OS << (P.Perspective() ? "1" : "0") << "\n";
508 if (P.Perspective())
509 OS << P.Focus() << "\n";
510
511 gp_Trsf T = P.Transformation();
512 gp_XYZ V = T.TranslationPart();
513 gp_Mat M = T.VectorialPart();
514
515 OS << M(1,1) << " ";
516 OS << M(1,2) << " ";
517 OS << M(1,3) << " ";
518 OS << V.Coord(1) << " ";
519 OS << "\n";
520 OS << M(2,1) << " ";
521 OS << M(2,2) << " ";
522 OS << M(2,3) << " ";
523 OS << V.Coord(2) << " ";
524 OS << "\n";
525 OS << M(3,1) << " ";
526 OS << M(3,2) << " ";
527 OS << M(3,3) << " ";
528 OS << V.Coord(3) << " ";
529 OS << "\n";
530
531}
532
533//=======================================================================
534//function : srestore
535//purpose :
536//=======================================================================
537
538static Handle(Draw_Drawable3D) srestore (istream& IS)
539{
540 Standard_Boolean pers;
541 IS >> pers;
542 Standard_Real focus = 1;
543 if (pers) IS >> focus;
544
545 gp_Trsf T;
546 Standard_Real V1[3],V2[3],V3[3];
547 Standard_Real V[3];
548
549 IS >> V1[0] >> V1[1] >> V1[2] >> V[0];
550 IS >> V2[0] >> V2[1] >> V2[2] >> V[1];
551 IS >> V3[0] >> V3[1] >> V3[2] >> V[2];
552
553 gp_Dir D1(V1[0],V1[1],V1[2]);
554 gp_Dir D2(V2[0],V2[1],V2[2]);
555 gp_Dir D3(V3[0],V3[1],V3[2]);
556 gp_Ax3 axes(gp_Pnt(0,0,0),D3,D1);
557 D3.Cross(D1);
558 if (D3.Dot(D2) < 0) axes.YReverse();
559 T.SetTransformation(axes);
560
561 T.SetTranslationPart(gp_Vec(V[0],V[1],V[2]));
562
563 HLRAlgo_Projector P(T,pers,focus);
564 Handle(HLRTest_Projector) HP = new HLRTest_Projector(P);
565 return HP;
566}
567
568//=======================================================================
569//function : ssr
570//purpose :
571//=======================================================================
572
573static Draw_SaveAndRestore ssr("HLRTest_Projector",stest,ssave,srestore);
574