0024166: Unable to create file with "Save" menu of voxeldemo Qt sample
[occt.git] / src / XSDRAWSTLVRML / XSDRAWSTLVRML.cxx
CommitLineData
b311480e 1// Created on: 2000-05-30
2// Created by: Sergey MOZOKHIN
3// Copyright (c) 2000-2012 OPEN CASCADE SAS
4//
5// The content of this file is subject to the Open CASCADE Technology Public
6// License Version 6.5 (the "License"). You may not use the content of this file
7// except in compliance with the License. Please obtain a copy of the License
8// at http://www.opencascade.org and read it completely before using this file.
9//
10// The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11// main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12//
13// The Original Code and all software distributed under the License is
14// distributed on an "AS IS" basis, without warranty of any kind, and the
15// Initial Developer hereby disclaims all such warranties, including without
16// limitation, any warranties of merchantability, fitness for a particular
17// purpose or non-infringement. Please see the License for the specific terms
18// and conditions governing the rights and limitations under the License.
19
7fd59977 20
21
22#include <XSDRAWSTLVRML.ixx>
23#include <Draw_Interpretor.hxx>
24#include <TopoDS_Shape.hxx>
25#include <VrmlAPI.hxx>
26#include <OSD_Path.hxx>
27#include <StlAPI.hxx>
28#include <XSDRAW.hxx>
29#include <DBRep.hxx>
30#include <VrmlAPI_Writer.hxx>
31#include <Quantity_Color.hxx>
32#include <Quantity_HArray1OfColor.hxx>
33#include <StlAPI_Writer.hxx>
34#include <Draw_PluginMacro.hxx>
35#include <SWDRAW.hxx>
36#include <XSDRAW.hxx>
37#include <XSDRAWSTEP.hxx>
38#include <XSDRAWIGES.hxx>
39#include <AIS_InteractiveContext.hxx>
40#include <ViewerTest.hxx>
41#include <Draw.hxx>
9c6afe19 42#include <Draw_ProgressIndicator.hxx>
7fd59977 43#include <RWStl.hxx>
44#include <Quantity_Color.hxx>
45#include <V3d_View.hxx>
46#include <TCollection_AsciiString.hxx>
47
48#include <SelectMgr_SelectionManager.hxx>
49#include <StdSelect_ViewerSelector3d.hxx>
50
51#include <Aspect_TypeOfMarker.hxx>
52#include <Graphic3d_MaterialAspect.hxx>
53
54#include <StlMesh_Mesh.hxx>
55#include <StlMesh_SequenceOfMeshTriangle.hxx>
56
57#include <MeshVS_Mesh.hxx>
58#include <MeshVS_MeshPrsBuilder.hxx>
59#include <MeshVS_TextPrsBuilder.hxx>
60#include <MeshVS_Drawer.hxx>
61#include <MeshVS_DrawerAttribute.hxx>
62#include <MeshVS_MeshEntityOwner.hxx>
63#include <MeshVS_DataMapOfIntegerAsciiString.hxx>
64
65#include <XSDRAWSTLVRML_DataSource.hxx>
66#include <XSDRAWSTLVRML_DrawableMesh.hxx>
67#include <MeshVS_NodalColorPrsBuilder.hxx>
68#include <MeshVS_ElementalColorPrsBuilder.hxx>
69#include <Quantity_NameOfColor.hxx>
70#include <TColgp_SequenceOfXYZ.hxx>
71#include <TColStd_HPackedMapOfInteger.hxx>
72#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
73#include <Standard_ErrorHandler.hxx>
74#include <VrmlData_Scene.hxx>
75#include <VrmlData_ShapeConvert.hxx>
76#include <VrmlData_DataMapOfShapeAppearance.hxx>
3ba3388b
A
77#include <TColStd_Array1OfReal.hxx>
78#include <Bnd_Box.hxx>
7fd59977 79
80// avoid warnings on 'extern "C"' functions returning C++ classes
81#ifdef WNT
82#pragma warning(4:4190)
83#endif
84
85#ifndef _STDIO_H
86#include <stdio.h>
87#endif
88
89static Standard_Integer writestl
90(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
91{
416d4426 92 if (argc < 3 || argc > 5) {
93 di << "Use: " << argv[0]
94 << " shape file [ascii/binary (0/1) : 1 by default] [InParallel (0/1) : 0 by default]" << "\n";
95 } else {
96 TopoDS_Shape aShape = DBRep::Get(argv[1]);
48a06a30 97 Standard_Boolean isASCIIMode = Standard_False;
416d4426 98 Standard_Boolean isInParallel = Standard_False;
48a06a30 99 if (argc > 3) {
91322f44 100 isASCIIMode = (Draw::Atoi(argv[3]) == 0);
bd0c22ce 101 if (argc > 4)
91322f44 102 isInParallel = (Draw::Atoi(argv[4]) == 1);
68bc5ed7 103 }
48a06a30 104 StlAPI_Writer aWriter;
105 aWriter.ASCIIMode() = isASCIIMode;
416d4426 106 aWriter.Write (aShape, argv[2], isInParallel);
7fd59977 107 }
108 return 0;
109}
110
111static Standard_Integer readstl
112(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
113{
114 if (argc<3) di << "wrong number of parameters" << "\n";
115 else {
116 TopoDS_Shape shape ;
117 StlAPI::Read(shape,argv[2]);
118 DBRep::Set(argv[1],shape);
119 }
120 return 0;
121}
122
123static Standard_Integer writevrml
124(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
125{
126 if (argc<3) di << "wrong number of parameters" << "\n";
127 else {
128 TopoDS_Shape shape = DBRep::Get(argv[1]);
129 // VrmlAPI_Writer writer;
130 // writer.SetTransparencyToMaterial(writer.GetFrontMaterial(),0.0);
131 // Quantity_Color color;
132 // color.SetValues(Quantity_NOC_GOLD);
133 // Handle(Quantity_HArray1OfColor) Col = new Quantity_HArray1OfColor(1,1);
134 // Col->SetValue(1,color);
135 // writer.SetDiffuseColorToMaterial(writer.GetFrontMaterial(),Col);
136 // writer.SetRepresentation(VrmlAPI_ShadedRepresentation);
137 // writer.SetDeflection(0.01);
138 // writer.Write(shape, argv[2]);
139 VrmlAPI::Write(shape, argv[2]);
140 }
141 return 0;
142}
143
144//=======================================================================
145//function : loadvrml
9c6afe19 146//purpose :
7fd59977 147//=======================================================================
148
149static Standard_Integer loadvrml
150(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
151{
152 if (argc<3) di << "wrong number of parameters" << "\n";
153 else {
154 TopoDS_Shape shape ;
155 VrmlData_DataMapOfShapeAppearance ShapeAppMap;
156
157 //-----------------------------------------------------------
158 filebuf fic;
159 istream aStream (&fic);
160
161 if (fic.open(argv[2], ios::in)) {
162
b569f2a3 163 // Get path of the VRML file.
164 OSD_Path path(argv[2]);
165 TCollection_AsciiString vrmlDir(".");
166 TCollection_AsciiString disk = path.Disk();
167 TCollection_AsciiString trek = path.Trek();
168 if (!trek.IsEmpty())
169 {
170 if (!disk.IsEmpty())
171 vrmlDir = disk;
172 else
173 vrmlDir.Clear();
174 trek.ChangeAll('|', '/');
175 vrmlDir += trek;
176 }
177
7fd59977 178 VrmlData_Scene aScene;
179
b569f2a3 180 aScene.SetVrmlDir (vrmlDir);
7fd59977 181 aScene << aStream;
182 const char * aStr = 0L;
183 switch (aScene.Status()) {
184
185 case VrmlData_StatusOK:
186 {
187 shape = aScene.GetShape(ShapeAppMap);
188 break;
189 }
566f8441 190 case VrmlData_EmptyData: aStr = "EmptyData"; break;
191 case VrmlData_UnrecoverableError: aStr = "UnrecoverableError"; break;
192 case VrmlData_GeneralError: aStr = "GeneralError"; break;
193 case VrmlData_EndOfFile: aStr = "EndOfFile"; break;
194 case VrmlData_NotVrmlFile: aStr = "NotVrmlFile"; break;
195 case VrmlData_CannotOpenFile: aStr = "CannotOpenFile"; break;
196 case VrmlData_VrmlFormatError: aStr = "VrmlFormatError"; break;
197 case VrmlData_NumericInputError: aStr = "NumericInputError"; break;
198 case VrmlData_IrrelevantNumber: aStr = "IrrelevantNumber"; break;
199 case VrmlData_BooleanInputError: aStr = "BooleanInputError"; break;
200 case VrmlData_StringInputError: aStr = "StringInputError"; break;
201 case VrmlData_NodeNameUnknown: aStr = "NodeNameUnknown"; break;
202 case VrmlData_NonPositiveSize: aStr = "NonPositiveSize"; break;
203 case VrmlData_ReadUnknownNode: aStr = "ReadUnknownNode"; break;
204 case VrmlData_NonSupportedFeature: aStr = "NonSupportedFeature"; break;
205 case VrmlData_OutputStreamUndefined:aStr = "OutputStreamUndefined"; break;
206 case VrmlData_NotImplemented: aStr = "NotImplemented"; break;
207 default:
208 break;
7fd59977 209 }
210 if (aStr) {
211 di << " ++ VRML Error: " << aStr << " in line "
212 << aScene.GetLineError() << "\n";
213 }
214 else {
215 DBRep::Set(argv[1],shape);
216 }
217 }
218 else {
219 di << "cannot open file" << "\n";
220 }
221
222
223 //-----------------------------------------------------------
224 }
225 return 0;
226}
227
228//=======================================================================
229//function : storevrml
9c6afe19 230//purpose :
7fd59977 231//=======================================================================
232
233static Standard_Integer storevrml
234(Draw_Interpretor& di, Standard_Integer argc, const char** argv)
235{
236 if (argc < 4) {
237 di << "wrong number of parameters" << "\n";
238 di << "use: storevrml shape file defl type_of_conversion (0, 1, 2)" << "\n";
239 }
240 else {
7fd59977 241 TopoDS_Shape shape = DBRep::Get(argv[1]);
91322f44 242 Standard_Real defl = Draw::Atof(argv[3]);
7fd59977 243 Standard_Integer type = 1;
91322f44 244 if(argc > 4) type = Draw::Atoi(argv[4]);
7fd59977 245 type = Max(0, type);
246 type = Min(2, type);
247
248 Standard_Boolean ExtFace = Standard_False;
249 if(type == 0 || type == 2) ExtFace = Standard_True;
250 Standard_Boolean ExtEdge = Standard_False;
251 if(type == 1 || type == 2) ExtEdge = Standard_True;
252
253 VrmlData_Scene aScene;
254 VrmlData_ShapeConvert Conv(aScene);
255 Conv.AddShape(shape);
256 Conv.Convert(ExtFace, ExtEdge, defl);
257
258 filebuf foc;
259 ostream outStream (&foc);
260 if (foc.open (argv[2], ios::out))
9c6afe19 261 outStream << aScene;
7fd59977 262 }
263 return 0;
264}
265
266
267//-----------------------------------------------------------------------------
268static Standard_Integer createmesh
269(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
270{
271
272 if (argc<3)
273 {
274 di << "Use: " << argv[0] << " <mesh name> <stl file>" << "\n";
275 return 1;
276 }
277
9c6afe19 278 // Progress indicator
7fd59977 279 OSD_Path aFile( argv[2] );
9c6afe19
RK
280 Handle(Draw_ProgressIndicator) aProgress = new Draw_ProgressIndicator (di, 1);
281 Handle(StlMesh_Mesh) aSTLMesh = RWStl::ReadFile (aFile, aProgress);
7fd59977 282
283 di << "Reading OK..." << "\n";
284 Handle( XSDRAWSTLVRML_DataSource ) aDS = new XSDRAWSTLVRML_DataSource( aSTLMesh );
285 di << "Data source is created successful" << "\n";
286 Handle( MeshVS_Mesh ) aMesh = new MeshVS_Mesh();
287 di << "MeshVS_Mesh is created successful" << "\n";
288
289 aMesh->SetDataSource( aDS );
290 aMesh->AddBuilder( new MeshVS_MeshPrsBuilder( aMesh.operator->() ), Standard_True );
291 // Prepare triangle labels
292 MeshVS_DataMapOfIntegerAsciiString aLabels;
293 Standard_Integer anIndex = 1, aLen = aSTLMesh->Triangles().Length();
294 for ( ; anIndex <= aLen; anIndex++ ){
295 aLabels.Bind( anIndex, TCollection_AsciiString( anIndex ) );
296 }
297
298 Handle(MeshVS_TextPrsBuilder) aTextBuilder = new MeshVS_TextPrsBuilder( aMesh.operator->(), 20., Quantity_NOC_YELLOW );
299 aTextBuilder->SetTexts( Standard_True, aLabels );
300 aMesh->AddBuilder( aTextBuilder );
301
302 // Hide all nodes by default
303 Handle(TColStd_HPackedMapOfInteger) aNodes = new TColStd_HPackedMapOfInteger();
304 aLen = aSTLMesh->Vertices().Length();
305 for ( anIndex = 1; anIndex <= aLen; anIndex++ )
306 aNodes->ChangeMap().Add( anIndex );
307 aMesh->SetHiddenNodes( aNodes );
308
309 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
310
311 if ( aContext.IsNull() )
312 {
9c6afe19 313 ViewerTest::ViewerInit();
7fd59977 314 //To create a 3D view if it doesn't exist
315 aContext = ViewerTest::GetAISContext();
316 if( aContext.IsNull() )
317 {
318 di << "Cannot create 3D view" << "\n";
319 return 0;
320 }
321 }
322
323 aContext->Display( aMesh );
324 aContext->Deactivate( aMesh );
325
326 Draw::Set( argv[1], new XSDRAWSTLVRML_DrawableMesh( aMesh ) );
327 Handle( V3d_View ) V = ViewerTest::CurrentView();
328 if ( !V.IsNull() )
329 V->FitAll();
330
331 return 0;
332}
333//-----------------------------------------------------------------------------
334Handle( MeshVS_Mesh ) getMesh( const char* name, Draw_Interpretor& di)
335{
336 Handle( XSDRAWSTLVRML_DrawableMesh ) aDrawMesh =
337 Handle( XSDRAWSTLVRML_DrawableMesh )::DownCast( Draw::Get( name ) );
338
339 if( aDrawMesh.IsNull() )
340 {
341 di << "There is no such object" << "\n";
342 return NULL;
343 }
344 else
345 {
346 Handle( MeshVS_Mesh ) aMesh = aDrawMesh->GetMesh();
347 if( aMesh.IsNull() )
348 {
349 di << "There is invalid mesh" << "\n";
350 return NULL;
351 }
352 else
353 return aMesh;
354 }
355}
356//-----------------------------------------------------------------------------
357static Standard_Integer meshdm
358(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
359{
360 if (argc<3)
361 di << "wrong number of parameters" << "\n";
362 else
363 {
364 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
365 if( !aMesh.IsNull() )
366 {
91322f44 367 Standard_Integer DisplayMode = Draw::Atoi (argv[2]);
7fd59977 368
369 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
370
371 if( aContext.IsNull() )
372 di << "The context is null" << "\n";
373 else
374 {
375 Standard_Boolean HasLocal = aContext->HasOpenedContext();
376 if( HasLocal )
377 aContext->CloseLocalContext();
378
379 aContext->SetDisplayMode( aMesh, DisplayMode );
380 di << "Setting display mode: " << DisplayMode << "\n";
381
382 if( HasLocal )
383 aContext->OpenLocalContext();
384 }
385 }
386 }
387 return 0;
388}
389//-----------------------------------------------------------------------------
390static Standard_Integer meshsm
391(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
392{
393 if (argc<3)
394 di << "wrong number of parameters" << "\n";
395 else
396 {
397 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
398 if( !aMesh.IsNull() )
399 {
91322f44 400 Standard_Integer SelMode = Draw::Atoi (argv[2]);
7fd59977 401
402 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
403
404 if( aContext.IsNull() )
405 di << "The context is null" << "\n";
406 else
407 {
408 if( !aContext->HasOpenedContext() )
409 aContext->OpenLocalContext();
410
411 aContext->Load( aMesh, -1 );
412
413 if( SelMode==-1 )
414 aContext->CloseAllContexts();
415
416 else if( SelMode==0 )
417 aContext->Activate( aMesh, 0 );
418
419 else if( SelMode>0 )
420 {
421 aContext->Deactivate( aMesh, 0 );
422
423 if( SelMode & 1 )
424 aContext->Activate( aMesh, 1 );
425 else
426 aContext->Deactivate( aMesh, 1 );
427
428 if( SelMode & 4 )
429 aContext->Activate( aMesh, 4 );
430 else
431 aContext->Deactivate( aMesh, 4 );
432
433 if( SelMode & 8 )
434 aContext->Activate( aMesh, 8 );
435 else
436 aContext->Deactivate( aMesh, 8 );
437 }
438
439 di << "Setting selection mode: " << SelMode << "\n";
440 }
441 }
442 }
443 return 0;
444}
445//-----------------------------------------------------------------------------
446static Standard_Integer setcolor
447(Draw_Interpretor& di, Standard_Integer argc, const char** argv, Standard_Integer Param )
448{
449 if (argc<5)
450 di << "wrong number of parameters" << "\n";
451 else
452 {
453 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
454 if( !aMesh.IsNull() )
455 {
91322f44 456 Standard_Real r = Draw::Atof (argv[2]);
457 Standard_Real g = Draw::Atof (argv[3]);
458 Standard_Real b = Draw::Atof (argv[4]);
7fd59977 459 aMesh->GetDrawer()->SetColor( (MeshVS_DrawerAttribute)Param, Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
460
461 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
462
463 if( aContext.IsNull() )
464 di << "The context is null" << "\n";
465 else
466 aContext->Redisplay( aMesh );
467 }
468 }
469 return 0;
470}
471//-----------------------------------------------------------------------------
472static Standard_Integer meshcolor
473(Draw_Interpretor& interp, Standard_Integer argc, const char** argv )
474{
475 return setcolor( interp, argc, argv, MeshVS_DA_InteriorColor );
476}
477//-----------------------------------------------------------------------------
478static Standard_Integer linecolor
479(Draw_Interpretor& interp, Standard_Integer argc, const char** argv )
480{
481 return setcolor( interp, argc, argv, MeshVS_DA_EdgeColor );
482}
483//-----------------------------------------------------------------------------
484static Standard_Integer meshmat
485(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
486{
487 if (argc<3)
488 di << "wrong number of parameters" << "\n";
489 else
490 {
491 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
492 if( !aMesh.IsNull() )
493 {
91322f44 494 Standard_Integer mat = Draw::Atoi (argv[2]);
7fd59977 495
496 Graphic3d_MaterialAspect aMatAsp =
497 (Graphic3d_MaterialAspect)(Graphic3d_NameOfMaterial)mat;
498
499 aMesh->GetDrawer()->SetMaterial( MeshVS_DA_FrontMaterial, aMatAsp );
500 aMesh->GetDrawer()->SetMaterial( MeshVS_DA_BackMaterial, aMatAsp );
501
502 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
503
504 if( aContext.IsNull() )
505 di << "The context is null" << "\n";
506 else
507 aContext->Redisplay( aMesh );
508 }
509 }
510 return 0;
511}
512//-----------------------------------------------------------------------------
513static Standard_Integer shrink
514(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
515{
516 if (argc<3)
517 di << "wrong number of parameters" << "\n";
518 else
519 {
520 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
521 if( !aMesh.IsNull() )
522 {
91322f44 523 Standard_Real sh = Draw::Atof (argv[2]);
7fd59977 524 aMesh->GetDrawer()->SetDouble( MeshVS_DA_ShrinkCoeff, sh );
525
526 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
527
528 if( aContext.IsNull() )
529 di << "The context is null" << "\n";
530 else
531 aContext->Redisplay( aMesh );
532 }
533 }
534 return 0;
535}
536//-----------------------------------------------------------------------------
537
538static Standard_Integer mdisplay
539(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
540{
541 if (argc<2)
542 di << "wrong number of parameters" << "\n";
543 else
544 {
545 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
546 if( !aMesh.IsNull() )
547 {
548 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
549
550 if( aContext.IsNull() )
551 di << "The context is null" << "\n";
552 else
553 {
554 if( aContext->HasOpenedContext() )
555 aContext->CloseLocalContext();
556
557 aContext->Display( aMesh );
558 }
559 }
560 }
561 return 0;
562}
563//-----------------------------------------------------------------------------
564static Standard_Integer merase
565(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
566{
567 if (argc<2)
568 di << "wrong number of parameters" << "\n";
569 else
570 {
571 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
572 if( !aMesh.IsNull() )
573 {
574 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
575
576 if( aContext.IsNull() )
577 di << "The context is null" << "\n";
578 else
579 {
580 if( aContext->HasOpenedContext() )
581 aContext->CloseLocalContext();
582
583 aContext->Erase( aMesh );
584 }
585 }
586 else
587 di << "Mesh is null" << "\n";
588 }
589 return 0;
590}
591//-----------------------------------------------------------------------------
592static Standard_Integer hidesel
593(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
594{
595 if (argc<1)
596 {
597 di << "wrong number of parameters" << "\n";
598 return 0;
599 }
600
601
602 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
603 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
604 if( aMesh.IsNull() )
605 {
606 di << "The mesh is invalid" << "\n";
607 return 0;
608 }
609
610 if( aContext.IsNull() )
611 di << "The context is null" << "\n";
612 else
613 {
614 Handle(TColStd_HPackedMapOfInteger) aHiddenNodes = aMesh->GetHiddenNodes();
615 Handle(TColStd_HPackedMapOfInteger) aHiddenElements = aMesh->GetHiddenElems();
616 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
617 {
618 Handle( MeshVS_MeshEntityOwner ) anOwner =
619 Handle( MeshVS_MeshEntityOwner )::DownCast( aContext->SelectedOwner() );
620 if( !anOwner.IsNull() )
eafb234b 621 {
7fd59977 622 if( anOwner->Type()==MeshVS_ET_Node )
eafb234b 623 {
7fd59977 624 aHiddenNodes->ChangeMap().Add( anOwner->ID() );
eafb234b 625 }
7fd59977 626 else
eafb234b 627 {
7fd59977 628 aHiddenElements->ChangeMap().Add( anOwner->ID() );
eafb234b 629 }
630 }
7fd59977 631 }
632 aContext->ClearSelected();
633 aMesh->SetHiddenNodes( aHiddenNodes );
634 aMesh->SetHiddenElems( aHiddenElements );
635 aContext->Redisplay( aMesh );
636 }
637
638 return 0;
639}
640//-----------------------------------------------------------------------------
641static Standard_Integer showonly
642(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
643{
644 if (argc<1)
645 {
646 di << "wrong number of parameters" << "\n";
647 return 0;
648 }
649
650
651 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
652 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
653 if( aMesh.IsNull() )
654 {
655 di << "The mesh is invalid" << "\n";
656 return 0;
657 }
658
659 if( aContext.IsNull() )
660 di << "The context is null" << "\n";
661 else
662 {
9c6afe19 663 Handle(TColStd_HPackedMapOfInteger) aHiddenNodes =
7fd59977 664 new TColStd_HPackedMapOfInteger(aMesh->GetDataSource()->GetAllNodes());
9c6afe19 665 Handle(TColStd_HPackedMapOfInteger) aHiddenElements =
7fd59977 666 new TColStd_HPackedMapOfInteger(aMesh->GetDataSource()->GetAllElements());
667 for( aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected() )
668 {
669 Handle( MeshVS_MeshEntityOwner ) anOwner =
670 Handle( MeshVS_MeshEntityOwner )::DownCast( aContext->SelectedOwner() );
671 if( !anOwner.IsNull() )
eafb234b 672 {
7fd59977 673 if( anOwner->Type()==MeshVS_ET_Node )
eafb234b 674 {
7fd59977 675 aHiddenNodes->ChangeMap().Remove( anOwner->ID() );
eafb234b 676 }
7fd59977 677 else
eafb234b 678 {
7fd59977 679 aHiddenElements->ChangeMap().Remove( anOwner->ID() );
eafb234b 680 }
681 }
7fd59977 682 }
683 aMesh->SetHiddenNodes( aHiddenNodes );
684 aMesh->SetHiddenElems( aHiddenElements );
685 aContext->Redisplay( aMesh );
686 }
687
688 return 0;
689}
690//-----------------------------------------------------------------------------
691static Standard_Integer showall
692(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
693{
694 if (argc<1)
695 {
696 di << "wrong number of parameters" << "\n";
697 return 0;
698 }
699
700 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
701 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
702 if( aMesh.IsNull() )
703 {
704 di << "The mesh is invalid" << "\n";
705 return 0;
706 }
707
708 if( aContext.IsNull() )
709 di << "The context is null" << "\n";
710 else
711 {
712 aMesh->SetHiddenNodes( 0 );
713 aMesh->SetHiddenElems( 0 );
714 aContext->Redisplay( aMesh );
715 }
716
717 return 0;
718}
719//-----------------------------------------------------------------------------
720static Standard_Integer delmesh
721(Draw_Interpretor& di, Standard_Integer argc, const char** argv )
722{
723 if (argc<2)
724 {
725 di << "wrong number of parameters" << "\n";
726 return 0;
727 }
728
729 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[1], di );
730
731 if( aMesh.IsNull() )
732 {
733 di << "The mesh is invalid" << "\n";
734 return 0;
735 }
736 else
737 {
738 Handle( AIS_InteractiveContext ) aContext = ViewerTest::GetAISContext();
739
740 aContext->ClearSelected();
741
742 if( aContext->HasOpenedContext() )
743 aContext->CloseAllContexts();
744
745 aContext->Remove( aMesh );
746 aContext->SelectionManager()->Remove( aMesh );
747 aMesh->ClearSelections();
748 aContext->MainSelector()->Clear();
749
750 Draw::Set( argv[1], Handle(XSDRAWSTLVRML_DrawableMesh)() );
751
752 Standard::Purge();
753 }
754 return 0;
755}
756//-----------------------------------------------------------------------------
757
9c6afe19
RK
758static Standard_Integer meshcolors( Draw_Interpretor& di,
759 Standard_Integer argc,
7fd59977 760 const char** argv )
761{
762 try
763 {
764 OCC_CATCH_SIGNALS
765 if ( argc < 2 )
766 {
767 di << "Use : meshcolors meshname mode isreflect" << "\n";
3ba3388b 768 di << "mode : {elem1|elem2|nodal|nodaltex|none}"<< "\n";
7fd59977 769 di << " elem1 - different color for each element" << "\n";
770 di << " elem2 - one color for one side"<<"\n";
3ba3388b
A
771 di << " nodal - different color for each node"<< "\n";
772 di << " nodaltex - different color for each node with texture interpolation"<< "\n";
9c6afe19 773 di << " none - clear"<< "\n";
7fd59977 774 di << "isreflect : {0|1} "<< "\n";
9c6afe19 775
7fd59977 776 return 0;
777 }
778
779 Handle( MeshVS_Mesh ) aMesh = getMesh( argv[ 1 ], di );
780
781 if ( aMesh.IsNull() )
782 {
783 di << "Mesh not found" << "\n";
784 return 0;
785 }
786 Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
787 if ( anIC.IsNull() )
788 {
789 di << "The context is null" << "\n";
790 return 0;
791 }
792 if( !aMesh.IsNull() )
793 {
794 TCollection_AsciiString aMode = TCollection_AsciiString (argv[2]);
795 Quantity_Color aColor1( (Quantity_NameOfColor)( Quantity_NOC_BLUE1 ) );
796 Quantity_Color aColor2( (Quantity_NameOfColor)( Quantity_NOC_RED1 ) );
3ba3388b 797 if( aMode.IsEqual("elem1") || aMode.IsEqual("elem2") || aMode.IsEqual("nodal") || aMode.IsEqual("nodaltex") || aMode.IsEqual("none") )
7fd59977 798 {
799 Handle(MeshVS_PrsBuilder) aTempBuilder;
91322f44 800 Standard_Integer reflection = Draw::Atoi(argv[3]);
7fd59977 801
802 for (int count = 0 ; count < aMesh->GetBuildersCount(); count++ ){
803 aTempBuilder = Handle(MeshVS_PrsBuilder)::DownCast(aMesh->FindBuilder("MeshVS_ElementalColorPrsBuilder"));
804 if( !aTempBuilder.IsNull())
805 aMesh->RemoveBuilderById(aTempBuilder->GetId());
806
807 aTempBuilder = Handle(MeshVS_PrsBuilder)::DownCast(aMesh->FindBuilder("MeshVS_NodalColorPrsBuilder"));
808 if( !aTempBuilder.IsNull())
809 aMesh->RemoveBuilderById(aTempBuilder->GetId());
810 }
811
812 if( aMode.IsEqual("elem1") || aMode.IsEqual("elem2") )
813 {
9c6afe19 814 Handle(MeshVS_ElementalColorPrsBuilder) aBuilder = new MeshVS_ElementalColorPrsBuilder(
7fd59977 815 aMesh, MeshVS_DMF_ElementalColorDataPrs | MeshVS_DMF_OCCMask );
816 // Color
817 const TColStd_PackedMapOfInteger& anAllElements = aMesh->GetDataSource()->GetAllElements();
818 TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllElements );
819
820 if( aMode.IsEqual("elem1") )
821 for ( ; anIter.More(); anIter.Next() )
822 {
823 Quantity_Color aColor( (Quantity_NameOfColor)( anIter.Key() % Quantity_NOC_WHITE ) );
824 aBuilder->SetColor1( anIter.Key(), aColor );
825 }
826 else
827 for ( ; anIter.More(); anIter.Next() )
828 aBuilder->SetColor2( anIter.Key(), aColor1, aColor2 );
829
830 aMesh->AddBuilder( aBuilder, Standard_True );
831 }
832
9c6afe19 833
7fd59977 834 if( aMode.IsEqual("nodal") )
835 {
9c6afe19 836 Handle(MeshVS_NodalColorPrsBuilder) aBuilder = new MeshVS_NodalColorPrsBuilder(
7fd59977 837 aMesh, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask );
838 aMesh->AddBuilder( aBuilder, Standard_True );
839
840 // Color
841 const TColStd_PackedMapOfInteger& anAllNodes =
842 aMesh->GetDataSource()->GetAllNodes();
843 TColStd_MapIteratorOfPackedMapOfInteger anIter( anAllNodes );
844 for ( ; anIter.More(); anIter.Next() )
845 {
9c6afe19 846 Quantity_Color aColor( (Quantity_NameOfColor)(
7fd59977 847 anIter.Key() % Quantity_NOC_WHITE ) );
848 aBuilder->SetColor( anIter.Key(), aColor );
849 }
850 aMesh->AddBuilder( aBuilder, Standard_True );
851 }
852
3ba3388b
A
853 if(aMode.IsEqual("nodaltex"))
854 {
855 // assign nodal builder to the mesh
9c6afe19 856 Handle(MeshVS_NodalColorPrsBuilder) aBuilder = new MeshVS_NodalColorPrsBuilder(
3ba3388b
A
857 aMesh, MeshVS_DMF_NodalColorDataPrs | MeshVS_DMF_OCCMask);
858 aMesh->AddBuilder(aBuilder, Standard_True);
859 aBuilder->UseTexture(Standard_True);
860
861 // prepare color map for texture
862 Aspect_SequenceOfColor aColorMap;
863 aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_RED);
864 aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_YELLOW);
865 aColorMap.Append((Quantity_NameOfColor) Quantity_NOC_BLUE1);
866
867 // prepare scale map for mesh - it will be assigned to mesh as texture coordinates
868 // make mesh color interpolated from minimum X coord to maximum X coord
869 Handle(MeshVS_DataSource) aDataSource = aMesh->GetDataSource();
870 Standard_Real aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ;
871
872 // get bounding box for calculations
873 aDataSource->GetBoundingBox().Get(aMinX, aMinY, aMinZ, aMaxX, aMaxY, aMaxZ);
874 Standard_Real aDelta = aMaxX - aMinX;
875
876 // assign color scale map values (0..1) to nodes
877 TColStd_DataMapOfIntegerReal aScaleMap;
878 TColStd_Array1OfReal aCoords(1, 3);
879 Standard_Integer aNbNodes;
880 MeshVS_EntityType aType;
881
882 // iterate nodes
883 const TColStd_PackedMapOfInteger& anAllNodes =
884 aMesh->GetDataSource()->GetAllNodes();
885 TColStd_MapIteratorOfPackedMapOfInteger anIter(anAllNodes);
886 for (; anIter.More(); anIter.Next())
887 {
888 //get node coordinates to aCoord variable
889 aDataSource->GetGeom(anIter.Key(), Standard_False, aCoords, aNbNodes, aType);
890
891 Standard_Real aScaleValue;
892 try {
893 OCC_CATCH_SIGNALS
894 aScaleValue = (aCoords.Value(1) - (Standard_Real) aMinX) / aDelta;
895 } catch(Standard_Failure) {
896 aScaleValue = 0;
897 }
898
9c6afe19 899 aScaleMap.Bind(anIter.Key(), aScaleValue);
3ba3388b
A
900 }
901
902 //set color map for builder and a color for invalid scale value
903 aBuilder->SetColorMap(aColorMap);
904 aBuilder->SetInvalidColor(Quantity_NOC_BLACK);
905 aBuilder->SetTextureCoords(aScaleMap);
906 aMesh->AddBuilder(aBuilder, Standard_True);
9c6afe19 907
3ba3388b
A
908 //set viewer to display texures
909 const Handle(V3d_Viewer)& aViewer = anIC->CurrentViewer();
910 for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews())
911 aViewer->ActiveView()->SetSurfaceDetail(V3d_TEX_ALL);
912 }
913
7fd59977 914 aMesh->GetDrawer()->SetBoolean ( MeshVS_DA_ColorReflection, Standard_Boolean(reflection) );
915
916 anIC->Redisplay( aMesh );
917 }
918 else
9c6afe19 919 {
7fd59977 920 di << "Wrong mode name" << "\n";
921 return 0;
922 }
923 }
924 }
925 catch ( Standard_Failure )
926 {
927 di << "Error" << "\n";
928 }
929
930 return 0;
931}
932//-----------------------------------------------------------------------------
933
9c6afe19
RK
934static Standard_Integer mesh_edge_width( Draw_Interpretor& di,
935 Standard_Integer argc,
7fd59977 936 const char** argv )
937{
938 try
939 {
940 OCC_CATCH_SIGNALS
941 if ( argc < 3 )
942 {
943 di << "Wrong number of parameters. Use : mesh_edge_width mesh width" << "\n";
944 return 0;
945 }
946
947 Handle(MeshVS_Mesh) aMesh = getMesh( argv[ 1 ], di );
948 if ( aMesh.IsNull() )
949 {
950 di << "Mesh not found" << "\n";
951 return 0;
952 }
953
954 const char* aWidthStr = argv[ 2 ];
91322f44 955 if ( aWidthStr == 0 || Draw::Atof( aWidthStr ) <= 0 )
7fd59977 956 {
957 di << "Width must be real value more than zero" << "\n";
958 return 0;
959 }
960
91322f44 961 double aWidth = Draw::Atof( aWidthStr );
7fd59977 962
963 Handle(AIS_InteractiveContext) anIC = ViewerTest::GetAISContext();
964 if ( anIC.IsNull() )
965 {
966 di << "The context is null" << "\n";
967 return 0;
968 }
969
970 Handle(MeshVS_Drawer) aDrawer = aMesh->GetDrawer();
971 if ( aDrawer.IsNull() )
972 {
973 di << "The drawer is null" << "\n";
974 return 0;
975 }
976
977 aDrawer->SetDouble( MeshVS_DA_EdgeWidth, aWidth );
978 anIC->Redisplay( aMesh );
979 }
980 catch ( Standard_Failure )
981 {
982 di << "Error" << "\n";
983 }
984
985 return 0;
986}
987
988//-----------------------------------------------------------------------------
989
990void XSDRAWSTLVRML::InitCommands (Draw_Interpretor& theCommands)
991{
992 const char* g = "XSTEP-STL/VRML"; // Step transfer file commands
993 //XSDRAW::LoadDraw(theCommands);
994
995 theCommands.Add ("writevrml", "shape file",__FILE__,writevrml,g);
416d4426 996 theCommands.Add ("writestl", "shape file [ascii/binary (0/1) : 1 by default] [InParallel (0/1) : 0 by default]",__FILE__,writestl,g);
7fd59977 997 theCommands.Add ("readstl", "shape file",__FILE__,readstl,g);
998 theCommands.Add ("loadvrml" , "shape file",__FILE__,loadvrml,g);
999 theCommands.Add ("storevrml" , "shape file defl [type]",__FILE__,storevrml,g);
1000
1001 theCommands.Add ("meshfromstl", "creates MeshVS_Mesh from STL file", __FILE__, createmesh, g );
1002 theCommands.Add ("meshdispmode", "changes MeshVS_Mesh display mode", __FILE__, meshdm, g );
1003 theCommands.Add ("meshselmode", "changes MeshVS_Mesh selection mode", __FILE__, meshsm, g );
1004 theCommands.Add ("meshshadcolor", "change MeshVS_Mesh shading color", __FILE__, meshcolor, g );
1005 theCommands.Add ("meshlinkcolor", "change MeshVS_Mesh line color", __FILE__, linecolor, g );
1006 theCommands.Add ("meshmat", "change MeshVS_Mesh material", __FILE__, meshmat, g );
1007 theCommands.Add ("meshshrcoef", "change MeshVS_Mesh shrink coeff", __FILE__, shrink, g );
1008 theCommands.Add ("meshshow", "display MeshVS_Mesh object", __FILE__, mdisplay, g );
1009 theCommands.Add ("meshhide", "erase MeshVS_Mesh object", __FILE__, merase, g );
1010 theCommands.Add ("meshhidesel", "hide selected entities", __FILE__, hidesel, g );
1011 theCommands.Add ("meshshowsel", "show only selected entities", __FILE__, showonly, g );
1012 theCommands.Add ("meshshowall", "show all entities", __FILE__, showall, g );
1013 theCommands.Add ("meshdelete", "delete MeshVS_Mesh object", __FILE__, delmesh, g );
1014 theCommands.Add ("meshcolors", "display color presentation", __FILE__, meshcolors, g );
1015 theCommands.Add ("mesh_edge_width", "set width of edges", __FILE__, mesh_edge_width, g );
1016}
1017
1018//==============================================================================
1019// XSDRAWSTLVRML::Factory
1020//==============================================================================
1021void XSDRAWSTLVRML::Factory(Draw_Interpretor& theDI)
1022{
1023 XSDRAWIGES::InitSelect();
1024 XSDRAWIGES::InitToBRep(theDI);
1025 XSDRAWIGES::InitFromBRep(theDI);
1026 XSDRAWSTEP::InitCommands(theDI);
1027 XSDRAWSTLVRML::InitCommands(theDI);
1028 SWDRAW::Init(theDI);
1029 XSDRAW::LoadDraw(theDI);
1030#ifdef DEB
1031 theDI << "Draw Plugin : All TKXSDRAW commands are loaded" << "\n";
1032#endif
1033}
1034
1035// Declare entry point PLUGINFACTORY
1036DPLUGIN(XSDRAWSTLVRML)
1037