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