1 // File: MeshVS_Mesh.cxx
2 // Created: Tue Sep 9 2003
3 // Author: Alexander SOLOVYOV
4 // Copyright: Open CASCADE 2003
6 #include <MeshVS_Mesh.ixx>
8 #include <AIS_InteractiveContext.hxx>
10 #include <Prs3d_Root.hxx>
12 #include <TColgp_Array1OfPnt.hxx>
14 #include <Select3D_SensitivePoint.hxx>
15 #include <SelectMgr_Selection.hxx>
17 #include <Aspect_MarkerStyle.hxx>
18 #include <Aspect_LineStyle.hxx>
19 #include <Aspect_InteriorStyle.hxx>
21 #include <Prs3d_PointAspect.hxx>
22 #include <Prs3d_LineAspect.hxx>
23 #include <Prs3d_ShadingAspect.hxx>
24 #include <Prs3d_Presentation.hxx>
25 #include <PrsMgr_ModedPresentation.hxx>
26 #include <PrsMgr_PresentationManager3d.hxx>
28 #include <SelectBasics_SensitiveEntity.hxx>
29 #include <SelectMgr_SequenceOfOwner.hxx>
30 #include <Select3D_SensitiveGroup.hxx>
31 #include <Select3D_SensitiveBox.hxx>
33 #include <Graphic3d_MaterialAspect.hxx>
34 #include <Graphic3d_AspectFillArea3d.hxx>
35 #include <Graphic3d_NameOfMaterial.hxx>
37 #include <TColStd_Array1OfReal.hxx>
38 #include <TColStd_Array1OfInteger.hxx>
39 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
40 #include <TColStd_ListIteratorOfListOfReal.hxx>
41 #include <TColStd_SequenceOfInteger.hxx>
42 #include <TColStd_ListIteratorOfListOfInteger.hxx>
44 #include <Bnd_Box.hxx>
46 #include <MeshVS_DataSource.hxx>
47 #include <MeshVS_MeshEntityOwner.hxx>
48 #include <MeshVS_MeshOwner.hxx>
49 #include <MeshVS_SensitiveMesh.hxx>
50 #include <MeshVS_DummySensitiveEntity.hxx>
51 #include <MeshVS_SensitiveSegment.hxx>
52 #include <MeshVS_SensitiveFace.hxx>
53 #include <MeshVS_DrawerAttribute.hxx>
54 #include <MeshVS_DataMapIteratorOfDataMapOfIntegerOwner.hxx>
55 #include <MeshVS_SensitivePolyhedron.hxx>
56 #include <MeshVS_SelectionModeFlags.hxx>
57 #include <MeshVS_Buffer.hxx>
59 #include <OSD_Timer.hxx>
61 //================================================================
62 // Function : Constructor MeshVS_Mesh
64 //================================================================
65 MeshVS_Mesh::MeshVS_Mesh (const Standard_Boolean theIsAllowOverlapped )
67 myDataSource.Nullify();
68 myHilighter.Nullify();
69 myWholeMeshOwner.Nullify();
70 mySelectionMethod = MeshVS_MSM_NODES;
72 SetAutoHilight ( Standard_False );
74 SetDisplayMode( MeshVS_DMF_WireFrame ); // Mode as defaut
75 SetHilightMode( MeshVS_DMF_WireFrame ); // Wireframe as default hilight mode
77 SetColor ( Quantity_NOC_WHITE );
78 SetMaterial ( Graphic3d_NOM_PLASTIC );
80 myCurrentDrawer = new MeshVS_Drawer();
81 myCurrentDrawer->SetColor ( MeshVS_DA_InteriorColor, Quantity_NOC_BLUE4 );
82 myCurrentDrawer->SetInteger ( MeshVS_DA_InteriorStyle, Aspect_IS_SOLID );
83 myCurrentDrawer->SetInteger ( MeshVS_DA_MaxFaceNodes, 10 );
84 myCurrentDrawer->SetBoolean ( MeshVS_DA_IsAllowOverlapped, theIsAllowOverlapped );
85 myCurrentDrawer->SetBoolean ( MeshVS_DA_Reflection, Standard_True );
86 myCurrentDrawer->SetDouble ( MeshVS_DA_ShrinkCoeff, 0.8 );
87 myCurrentDrawer->SetBoolean ( MeshVS_DA_ComputeTime, Standard_False );
88 myCurrentDrawer->SetBoolean ( MeshVS_DA_ComputeSelectionTime, Standard_False );
89 myCurrentDrawer->SetBoolean ( MeshVS_DA_DisplayNodes, Standard_True );
90 myCurrentDrawer->SetDouble ( MeshVS_DA_EdgeWidth, 1.0 );
91 myCurrentDrawer->SetInteger ( MeshVS_DA_EdgeType, Aspect_TOL_SOLID );
92 myCurrentDrawer->SetInteger ( MeshVS_DA_MarkerType, Aspect_TOM_O );
93 myCurrentDrawer->SetColor ( MeshVS_DA_MarkerColor, Quantity_NOC_WHITE );
94 myCurrentDrawer->SetDouble ( MeshVS_DA_MarkerScale, 1.0 );
95 myCurrentDrawer->SetInteger ( MeshVS_DA_BeamType, Aspect_TOL_SOLID );
96 myCurrentDrawer->SetDouble ( MeshVS_DA_BeamWidth, 1.0 );
97 myCurrentDrawer->SetBoolean ( MeshVS_DA_SmoothShading, Standard_False );
98 myCurrentDrawer->SetBoolean ( MeshVS_DA_SupressBackFaces, Standard_False );
100 mySelectionDrawer = new MeshVS_Drawer();
101 mySelectionDrawer->Assign ( myCurrentDrawer );
102 mySelectionDrawer->SetInteger ( MeshVS_DA_MarkerType, Aspect_TOM_STAR );
103 mySelectionDrawer->SetColor ( MeshVS_DA_MarkerColor, Quantity_NOC_GRAY80 );
104 mySelectionDrawer->SetDouble ( MeshVS_DA_MarkerScale, 2.0 );
106 mySelectionDrawer->SetColor ( MeshVS_DA_BeamColor, Quantity_NOC_GRAY80 );
107 mySelectionDrawer->SetInteger ( MeshVS_DA_BeamType, Aspect_TOL_SOLID );
108 mySelectionDrawer->SetDouble ( MeshVS_DA_BeamWidth, 3.0 );
110 myHilightDrawer = new MeshVS_Drawer ();
111 myHilightDrawer->Assign ( myCurrentDrawer );
112 myHilightDrawer->SetDouble ( MeshVS_DA_ShrinkCoeff, 0.7 );
113 myHilightDrawer->SetInteger ( MeshVS_DA_InteriorStyle, Aspect_IS_SOLID );
114 myHilightDrawer->SetColor ( MeshVS_DA_InteriorColor, Quantity_NOC_YELLOW );
115 myHilightDrawer->SetColor ( MeshVS_DA_BackInteriorColor, Quantity_NOC_YELLOW );
116 myHilightDrawer->SetColor ( MeshVS_DA_EdgeColor, Quantity_NOC_GREEN );
117 myHilightDrawer->SetInteger ( MeshVS_DA_EdgeType, Aspect_TOL_SOLID );
118 myHilightDrawer->SetDouble ( MeshVS_DA_EdgeWidth, 1.0 );
119 myHilightDrawer->SetMaterial ( MeshVS_DA_FrontMaterial, Graphic3d_NOM_PLASTIC );
120 myHilightDrawer->SetMaterial ( MeshVS_DA_BackMaterial, Graphic3d_NOM_PLASTIC );
122 myHilightDrawer->SetColor ( MeshVS_DA_BeamColor, Quantity_NOC_GRAY80 );
123 myHilightDrawer->SetInteger ( MeshVS_DA_BeamType, Aspect_TOL_SOLID );
124 myHilightDrawer->SetDouble ( MeshVS_DA_BeamWidth, 3.0 );
126 myHilightDrawer->SetInteger ( MeshVS_DA_MarkerType, Aspect_TOM_STAR );
127 myHilightDrawer->SetColor ( MeshVS_DA_MarkerColor, Quantity_NOC_GRAY80 );
128 myHilightDrawer->SetDouble ( MeshVS_DA_MarkerScale, 2.0 );
131 void MeshVS_Mesh::Destroy()
135 //================================================================
136 // Function : Compute
138 //================================================================
139 void MeshVS_Mesh::Compute ( const Handle(PrsMgr_PresentationManager3d)& thePrsMgr,
140 const Handle(Prs3d_Presentation)& thePresentation,
141 const Standard_Integer theMode )
145 Standard_Boolean ShowComputeTime = Standard_True;
146 myCurrentDrawer->GetBoolean( MeshVS_DA_ComputeTime, ShowComputeTime );
148 if ( ShowComputeTime )
154 // Repair Ids in map if necessary
155 Handle( MeshVS_DataSource ) aDS = GetDataSource();
159 const TColStd_PackedMapOfInteger& aNodes = aDS->GetAllNodes();
160 const TColStd_PackedMapOfInteger& aElems = aDS->GetAllElements();
161 Standard_Boolean HasNodes = !aNodes.IsEmpty(),
162 HasElements = !aElems.IsEmpty();
164 TColStd_PackedMapOfInteger aNodesToExclude, aElemsToExclude;
166 thePresentation->Clear();
167 Standard_Integer len = myBuilders.Length();
169 for ( Standard_Integer i=1; i<=len; i++ )
171 Handle (MeshVS_PrsBuilder) aCurrent = myBuilders.Value ( i );
172 if ( !aCurrent.IsNull() && aCurrent->TestFlags ( theMode ) )
174 aCurrent->SetPresentationManager( thePrsMgr );
176 aCurrent->Build ( thePresentation, aNodes, aNodesToExclude, Standard_False, theMode );
178 aCurrent->Build ( thePresentation, aElems, aElemsToExclude, Standard_True, theMode );
183 if ( ShowComputeTime )
185 Standard_Real sec, cpu;
186 Standard_Integer min, hour;
188 gTimer.Show ( sec, min, hour, cpu );
189 cout << "DisplayMode : " << theMode << endl;
190 cout << "Compute : " << sec << " sec" << endl;
191 cout << "Compute CPU : " << cpu << " sec" << endl << endl;
196 //================================================================
197 // Function : ComputeSelection
199 //================================================================
200 void MeshVS_Mesh::ComputeSelection ( const Handle(SelectMgr_Selection)& theSelection,
201 const Standard_Integer theMode )
205 Standard_Boolean ShowComputeSelectionTime = Standard_True;
206 myCurrentDrawer->GetBoolean( MeshVS_DA_ComputeSelectionTime, ShowComputeSelectionTime );
208 if ( ShowComputeSelectionTime )
214 Standard_Integer aMaxFaceNodes;
215 Handle( MeshVS_DataSource ) aSource = GetDataSource();
217 if ( aSource.IsNull() || myCurrentDrawer.IsNull() || !myCurrentDrawer->GetInteger
218 ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) || aMaxFaceNodes<=0 )
221 MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
222 TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
223 Standard_Integer NbNodes;
225 MeshVS_EntityType aType;
226 Standard_Address anAddr;
227 TColStd_MapIteratorOfPackedMapOfInteger anIter;
228 Standard_Real x, y, z;
230 Standard_Integer aMode = HasDisplayMode() ? DisplayMode() : DefaultDisplayMode();
231 if ( myHilighter.IsNull() || ( aMode & MeshVS_DMF_OCCMask ) == 0 )
233 const TColStd_PackedMapOfInteger& anAllNodesMap = aSource->GetAllNodes();
234 const TColStd_PackedMapOfInteger& anAllElementsMap= aSource->GetAllElements();
235 //agv Standard_Integer i;
237 // skt: advanced mesh selection
238 if( aSource->IsAdvancedSelectionEnabled() ) {
239 Handle(MeshVS_MeshOwner) anOwner;
240 // Get the owner if it is already created
241 MeshVS_DataMapIteratorOfDataMapOfIntegerOwner anIt( GetOwnerMaps( Standard_False ) );
242 for( ; anIt.More(); anIt.Next() )
244 anOwner = Handle(MeshVS_MeshOwner)::DownCast( anIt.Value() );
245 // clear selected entities
246 if( !anOwner.IsNull() )
249 if( anOwner.IsNull() )
250 // Create one owner for the whole mesh and for all selection modes
251 anOwner = new MeshVS_MeshOwner( this, aSource, 5 );
252 // Save the owner. It will be available via GetOwnerMaps method
253 if( !myMeshOwners.IsBound( 1 ) )
254 myMeshOwners.Bind( 1, anOwner );
255 // Create one sensitive entity. It should detect mesh entities correspondingly
257 Handle(MeshVS_SensitiveMesh) aSensMesh = new MeshVS_SensitiveMesh( anOwner, theMode );
258 theSelection->Add ( aSensMesh );
263 case MeshVS_SMF_Node: // Nodes
265 myNodeOwners.Clear();
267 for ( anIter.Initialize( anAllNodesMap ); anIter.More(); anIter.Next() )
269 Standard_Integer aKey = anIter.Key();
270 if ( !myDataSource->GetGeom ( aKey, Standard_False, aCoords, NbNodes, aType ) )
273 anAddr = myDataSource->GetAddr ( aKey, Standard_False );
275 Handle (MeshVS_MeshEntityOwner) anOwner = new MeshVS_MeshEntityOwner
276 ( this, aKey, anAddr, aType, 5 );
278 myNodeOwners.Bind ( aKey, anOwner );
280 if ( IsSelectableNode( aKey ) )
285 Handle (Select3D_SensitivePoint) aPoint = new Select3D_SensitivePoint
286 ( anOwner, gp_Pnt (x, y, z) );
287 theSelection->Add ( aPoint );
290 theSelection->Add ( new MeshVS_DummySensitiveEntity ( anOwner ) );
294 case MeshVS_SMF_Mesh:
296 if( myWholeMeshOwner.IsNull() )
297 myWholeMeshOwner = new SelectMgr_EntityOwner( this );
299 switch( mySelectionMethod )
303 Standard_Real min[3], max[3]; Standard_Boolean first = Standard_True;
304 TColStd_MapIteratorOfPackedMapOfInteger anIterN( anAllNodesMap );
306 for( ; anIterN.More(); anIterN.Next() )
307 if( myDataSource->GetGeom( anIterN.Key(), Standard_False, aCoords, NbNodes, aType ) )
310 for( Standard_Integer i=1; i<=3; i++ )
311 min[i-1] = max[i-1] = aCoords( i );
312 first = Standard_False;
315 for( Standard_Integer i=1; i<=3; i++ )
317 if( aCoords( i )<min[i-1] )
318 min[i-1] = aCoords( i );
319 if( aCoords( i )>max[i-1] )
320 max[i-1] = aCoords( i );
323 box.Add( gp_Pnt( min[0], min[1], min[2] ) );
324 box.Add( gp_Pnt( max[0], max[1], max[2] ) );
326 theSelection->Add( new Select3D_SensitiveBox( myWholeMeshOwner, box ) );
330 case MeshVS_MSM_NODES:
332 TColStd_MapIteratorOfPackedMapOfInteger anIterN( anAllNodesMap );
333 for( ; anIterN.More(); anIterN.Next() )
334 if( myDataSource->GetGeom( anIterN.Key(), Standard_False, aCoords, NbNodes, aType ) &&
335 !IsHiddenNode( anIterN.Key() ) )
336 theSelection->Add( new Select3D_SensitivePoint( myWholeMeshOwner, gp_Pnt ( aCoords(1), aCoords(2), aCoords(3) ) ) );
340 case MeshVS_MSM_PRECISE:
342 Handle( Select3D_SensitiveEntity ) anEnt;
343 TColStd_MapIteratorOfPackedMapOfInteger anIterMV( anAllElementsMap );
345 TColStd_PackedMapOfInteger aSharedNodes;
346 for( ; anIterMV.More(); anIterMV.Next() )
348 Standard_Integer aKey = anIterMV.Key();
350 if( IsSelectableElem( aKey ) &&
351 myDataSource->GetGeomType( aKey, Standard_True, aType ) &&
352 aType==MeshVS_ET_Face )
354 myDataSource->GetGeom ( aKey, Standard_True, aCoords, NbNodes, aType );
358 MeshVS_Buffer aNodesBuf (NbNodes*sizeof(Standard_Integer));
359 TColStd_Array1OfInteger aElemNodes(aNodesBuf, 1, NbNodes);
360 if ( !myDataSource->GetNodesByElement ( aKey, aElemNodes, NbNodes ) )
363 TColgp_Array1OfPnt Points( 1, NbNodes );
364 for ( Standard_Integer i=1; i<=NbNodes; i++ )
366 Points (i) = gp_Pnt ( aCoords (3*i-2), aCoords (3*i-1), aCoords (3*i) );
367 aSharedNodes.Add( aElemNodes( i ) );
370 anEnt = new Select3D_SensitiveFace( myWholeMeshOwner, Points );
371 theSelection->Add( anEnt );
374 for( anIterMV.Initialize( anAllElementsMap ); anIterMV.More(); anIterMV.Next() )
376 Standard_Integer aKey = anIterMV.Key();
378 if( IsSelectableElem( aKey ) &&
379 myDataSource->GetGeomType( aKey, Standard_True, aType ) &&
380 aType==MeshVS_ET_Link )
382 myDataSource->GetGeom ( aKey, Standard_True, aCoords, NbNodes, aType );
386 MeshVS_Buffer aNodesBuf (NbNodes*sizeof(Standard_Integer));
387 TColStd_Array1OfInteger aElemNodes(aNodesBuf, 1, NbNodes);
388 if ( !myDataSource->GetNodesByElement ( aKey, aElemNodes, NbNodes ) )
391 TColgp_Array1OfPnt Points( 1, NbNodes );
392 Standard_Boolean all_shared = Standard_True;
393 for ( Standard_Integer i=1; i<=NbNodes; i++ )
395 Points (i) = gp_Pnt ( aCoords (3*i-2), aCoords (3*i-1), aCoords (3*i) );
396 all_shared = all_shared && aSharedNodes.Contains( aElemNodes( i ) );
397 aSharedNodes.Add( aElemNodes( i ) );
402 anEnt = new Select3D_SensitiveSegment( myWholeMeshOwner, Points.Value( 1 ), Points.Value( 2 ) );
403 theSelection->Add( anEnt );
407 for( anIterMV.Initialize( anAllNodesMap ); anIterMV.More(); anIterMV.Next() )
409 Standard_Integer aKey = anIterMV.Key();
410 if( IsSelectableElem( aKey ) &&
411 myDataSource->GetGeom ( aKey, Standard_False, aCoords, NbNodes, aType ) &&
412 !aSharedNodes.Contains( aKey ) )
414 anEnt = new Select3D_SensitivePoint( myWholeMeshOwner, gp_Pnt ( aCoords(1), aCoords(2), aCoords(3) ) ) ;
415 theSelection->Add( anEnt );
423 case MeshVS_SMF_Group:
425 myGroupOwners.Clear();
427 TColStd_PackedMapOfInteger anAllGroupsMap;
428 aSource->GetAllGroups( anAllGroupsMap );
430 Handle( MeshVS_HArray1OfSequenceOfInteger ) aTopo;
432 for ( anIter.Initialize( anAllGroupsMap ); anIter.More(); anIter.Next() )
434 Standard_Integer aKeyGroup = anIter.Key();
436 MeshVS_EntityType aGroupType;
437 TColStd_PackedMapOfInteger aGroupMap;
438 if ( !myDataSource->GetGroup( aKeyGroup, aGroupType, aGroupMap ) )
441 anAddr = myDataSource->GetGroupAddr ( aKeyGroup );
442 Standard_Integer aPrior = 0;
444 if( aGroupType == MeshVS_ET_Volume )
446 if ( aGroupType == MeshVS_ET_Face )
448 else if ( aGroupType == MeshVS_ET_Link )
450 else if ( aGroupType == MeshVS_ET_0D )
452 else if ( aGroupType == MeshVS_ET_Node )
455 Handle (MeshVS_MeshEntityOwner) anOwner = new MeshVS_MeshEntityOwner
456 ( this, aKeyGroup, anAddr, aGroupType, aPrior, Standard_True );
458 myGroupOwners.Bind( aKeyGroup, anOwner );
460 Standard_Boolean added = Standard_False;
461 for ( TColStd_MapIteratorOfPackedMapOfInteger anIterMG (aGroupMap);
462 anIterMG.More(); anIterMG.Next() )
464 Standard_Integer aKey = anIterMG.Key();
465 if ( aGroupType == MeshVS_ET_Node ) {
466 if( myDataSource->GetGeom( aKey, Standard_False, aCoords, NbNodes, aType ) &&
467 IsSelectableNode/*!IsHiddenNode*/( aKey ) ) {
468 theSelection->Add( new Select3D_SensitivePoint( anOwner, gp_Pnt ( aCoords(1), aCoords(2), aCoords(3) ) ) );
469 added = Standard_True;
472 else if ( myDataSource->GetGeomType ( aKey, Standard_True, aType ) &&
473 IsSelectableElem/*!IsHiddenElem*/( aKey ) ) {
474 myDataSource->GetGeom ( aKey, Standard_True, aCoords, NbNodes, aType );
476 TColgp_Array1OfPnt anArr( 1, NbNodes );
477 for ( Standard_Integer i=1; i<=NbNodes; i++ )
482 anArr.SetValue ( i, gp_Pnt ( x, y, z ) );
485 if ( aType == MeshVS_ET_Face && NbNodes > 0 ) // Faces: 2D-elements
487 Handle (MeshVS_SensitiveFace) aFace =
488 new MeshVS_SensitiveFace( anOwner, anArr );
489 theSelection->Add ( aFace );
490 added = Standard_True;
492 else if ( aType == MeshVS_ET_Link && NbNodes > 0 ) // Links: 1D-elements
494 Handle (MeshVS_SensitiveSegment) aSeg = new MeshVS_SensitiveSegment
495 ( anOwner, anArr(1), anArr(2) );
496 theSelection->Add ( aSeg );
497 added = Standard_True;
499 else if( aType == MeshVS_ET_Volume && aSource->Get3DGeom( aKey, NbNodes, aTopo ) )
501 Handle( MeshVS_SensitivePolyhedron ) aPolyhedron =
502 new MeshVS_SensitivePolyhedron( anOwner, anArr, aTopo );
503 theSelection->Add( aPolyhedron );
504 added = Standard_True;
506 else //if ( aType == MeshVS_ET_0D ) // Custom : not only 0D-elements !!!
508 Handle (SelectBasics_SensitiveEntity) anEnt =
509 myHilighter->CustomSensitiveEntity ( anOwner, aKey );
510 if (!anEnt.IsNull()) {
511 theSelection->Add ( anEnt );
512 added = Standard_True;
518 theSelection->Add ( new MeshVS_DummySensitiveEntity ( anOwner ) );
523 default: // all residuary modes
525 Handle( MeshVS_HArray1OfSequenceOfInteger ) aTopo;
527 myElementOwners.Clear();
529 MeshVS_DataMapOfIntegerOwner* CurMap = &my0DOwners;
530 if ( theMode == MeshVS_ET_Link )
531 CurMap = &myLinkOwners;
532 else if ( theMode == MeshVS_ET_Face )
533 CurMap = &myFaceOwners;
534 else if ( theMode == MeshVS_ET_Volume )
535 CurMap = &myVolumeOwners;
539 for (TColStd_MapIteratorOfPackedMapOfInteger anIterMV (anAllElementsMap);
540 anIterMV.More(); anIterMV.Next() )
542 Standard_Integer aKey = anIterMV.Key();
544 if ( myDataSource->GetGeomType ( aKey, Standard_True, aType ) && theMode == aType )
546 myDataSource->GetGeom ( aKey, Standard_True, aCoords, NbNodes, aType );
547 anAddr = myDataSource->GetAddr ( aKey, Standard_True );
549 Standard_Integer aPrior = 0;
551 if( aType == MeshVS_ET_Volume )
553 if ( aType == MeshVS_ET_Face )
555 else if ( aType == MeshVS_ET_Link )
557 else if ( aType == MeshVS_ET_0D )
560 Handle (MeshVS_MeshEntityOwner) anOwner = new MeshVS_MeshEntityOwner
561 ( this, aKey, anAddr, aType, aPrior );
563 CurMap->Bind ( aKey, anOwner );
565 if ( IsSelectableElem( aKey ) ) //The element is selectable
567 TColgp_Array1OfPnt anArr( 1, NbNodes );
568 for ( Standard_Integer i=1; i<=NbNodes; i++ )
573 anArr.SetValue ( i, gp_Pnt ( x, y, z ) );
576 if ( aType == MeshVS_ET_Face && NbNodes > 0 ) // Faces: 2D-elements
578 Handle (MeshVS_SensitiveFace) aFace =
579 new MeshVS_SensitiveFace( anOwner, anArr );
580 theSelection->Add ( aFace );
582 else if ( aType == MeshVS_ET_Link && NbNodes > 0 ) // Links: 1D-elements
584 Handle (MeshVS_SensitiveSegment) aSeg = new MeshVS_SensitiveSegment
585 ( anOwner, anArr(1), anArr(2) );
586 theSelection->Add ( aSeg );
588 else if( aType == MeshVS_ET_Volume && aSource->Get3DGeom( aKey, NbNodes, aTopo ) )
590 Handle( MeshVS_SensitivePolyhedron ) aPolyhedron =
591 new MeshVS_SensitivePolyhedron( anOwner, anArr, aTopo );
592 theSelection->Add( aPolyhedron );
594 else //if ( aType == MeshVS_ET_0D ) // Custom : not only 0D-elements !!!
596 Handle (SelectBasics_SensitiveEntity) anEnt =
597 myHilighter->CustomSensitiveEntity ( anOwner, aKey );
599 theSelection->Add ( anEnt );
603 theSelection->Add ( new MeshVS_DummySensitiveEntity ( anOwner ) );
611 if ( ShowComputeSelectionTime )
613 Standard_Real sec, cpu;
614 Standard_Integer min, hour;
616 gTimer.Show ( sec, min, hour, cpu );
617 cout << "SelectionMode : " << theMode << endl;
618 cout << "Compute selection: " << sec << " sec" << endl;
619 cout << "Compute selection CPU : " << cpu << " sec" << endl << endl;
624 //================================================================
625 // Function : GetBuildersCount
627 //================================================================
628 Standard_Integer MeshVS_Mesh::GetBuildersCount () const
630 return myBuilders.Length();
633 //================================================================
634 // Function : GetFreeId
636 //================================================================
637 Standard_Integer MeshVS_Mesh::GetFreeId () const
639 TColStd_PackedMapOfInteger Ids;
640 Standard_Integer i, len = myBuilders.Length(), curId;
642 for ( i=1; i<=len; i++ )
643 Ids.Add( myBuilders.Value(i)->GetId () );
646 while ( Ids.Contains( curId ) )
652 //================================================================
653 // Function : GetBuilder
655 //================================================================
656 Handle (MeshVS_PrsBuilder) MeshVS_Mesh::GetBuilder ( const Standard_Integer Index ) const
658 if ( Index>=1 && Index<=myBuilders.Length() )
659 return myBuilders.Value( Index );
664 //================================================================
665 // Function : GetBuilderById
667 //================================================================
668 Handle (MeshVS_PrsBuilder) MeshVS_Mesh::GetBuilderById ( const Standard_Integer Id ) const
670 Handle (MeshVS_PrsBuilder) Result;
672 Standard_Integer i, len = myBuilders.Length();
673 for ( i=1; i<=len; i++ )
674 if ( myBuilders.Value(i)->GetId () == Id )
676 Result = myBuilders.Value(i);
682 //================================================================
683 // Function : AddBuilder
685 //================================================================
686 void MeshVS_Mesh::AddBuilder ( const Handle (MeshVS_PrsBuilder)& theBuilder,
687 const Standard_Boolean TreatAsHilighter )
689 if ( theBuilder.IsNull() )
692 Standard_Integer i, n = myBuilders.Length();
693 for ( i = 1; i<=n; i++ )
694 if ( myBuilders(i)->GetPriority() < theBuilder->GetPriority() )
698 myBuilders.Append ( theBuilder );
700 myBuilders.InsertBefore ( i, theBuilder );
702 if( TreatAsHilighter )
703 myHilighter = theBuilder;
706 //================================================================
707 // Function : RemoveBuilder
709 //================================================================
710 void MeshVS_Mesh::RemoveBuilder ( const Standard_Integer theIndex )
712 Handle( MeshVS_PrsBuilder ) aBuild = GetBuilder( theIndex );
713 if ( !aBuild.IsNull() )
715 if ( aBuild == myHilighter )
716 myHilighter.Nullify();
717 myBuilders.Remove ( theIndex );
721 //================================================================
722 // Function : RemoveBuilderById
724 //================================================================
725 void MeshVS_Mesh::RemoveBuilderById ( const Standard_Integer Id )
727 Standard_Integer i, n = myBuilders.Length();
728 for ( i=1; i<=n; i++ )
730 Handle( MeshVS_PrsBuilder ) aCur = myBuilders(i);
731 if ( !aCur.IsNull() && aCur->GetId()==Id )
736 if ( GetBuilder( i )==myHilighter )
737 myHilighter.Nullify();
742 //================================================================
743 // Function : SetHiddenElems
745 //================================================================
746 void MeshVS_Mesh::SetHiddenElems ( const Handle(TColStd_HPackedMapOfInteger)& theMap )
748 myHiddenElements = theMap;
750 // Note: update of list of selectable nodes -- this is not optimal!
751 Standard_Boolean AutoSelUpdate = Standard_False;
752 if ( !GetDrawer().IsNull() && GetDrawer()->GetBoolean( MeshVS_DA_SelectableAuto, AutoSelUpdate ) &&
754 UpdateSelectableNodes();
757 //================================================================
758 // Function : SetHiddenNodes
760 //================================================================
761 void MeshVS_Mesh::SetHiddenNodes ( const Handle(TColStd_HPackedMapOfInteger)& theMap )
763 myHiddenNodes = theMap;
765 // Note: update of list of selectable nodes -- this is not optimal!
766 Standard_Boolean AutoSelUpdate = Standard_False;
767 if ( !GetDrawer().IsNull() && GetDrawer()->GetBoolean( MeshVS_DA_SelectableAuto, AutoSelUpdate ) &&
769 UpdateSelectableNodes();
772 //================================================================
773 // Function : GetHiddenElems
775 //================================================================
776 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_Mesh::GetHiddenElems () const
778 return myHiddenElements;
781 //================================================================
782 // Function : GetHiddenNodes
784 //================================================================
785 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_Mesh::GetHiddenNodes () const
787 return myHiddenNodes;
790 //================================================================
791 // Function : AddToMap
793 //================================================================
794 void AddToMap ( MeshVS_DataMapOfIntegerOwner& Result, const MeshVS_DataMapOfIntegerOwner& Addition )
796 MeshVS_DataMapIteratorOfDataMapOfIntegerOwner anIt ( Addition );
797 for ( ; anIt.More(); anIt.Next() )
798 if ( Result.IsBound ( anIt.Key() ) )
799 Result.ChangeFind ( anIt.Key() ) = anIt.Value();
801 Result.Bind( anIt.Key(), anIt.Value() );
804 //================================================================
805 // Function : GetOwnerMaps
807 //================================================================
808 const MeshVS_DataMapOfIntegerOwner& MeshVS_Mesh::GetOwnerMaps ( const Standard_Boolean IsElements )
810 Handle(MeshVS_DataSource) aDS = GetDataSource();
811 if( !aDS.IsNull() && aDS->IsAdvancedSelectionEnabled() )
815 if ( myElementOwners.IsEmpty() )
817 AddToMap ( myElementOwners, my0DOwners );
818 AddToMap ( myElementOwners, myLinkOwners );
819 AddToMap ( myElementOwners, myFaceOwners );
820 AddToMap ( myElementOwners, myVolumeOwners );
822 return myElementOwners;
828 //================================================================
829 // Function : IsHiddenElem
831 //================================================================
832 Standard_Boolean MeshVS_Mesh::IsHiddenElem ( const Standard_Integer theID ) const
834 return ! myHiddenElements.IsNull() && myHiddenElements->Map().Contains( theID );
837 //================================================================
838 // Function : IsHiddenNode
840 //================================================================
841 Standard_Boolean MeshVS_Mesh::IsHiddenNode ( const Standard_Integer theID ) const
843 // note that by default all nodes are hidden
844 return myHiddenNodes.IsNull() || myHiddenNodes->Map().Contains( theID );
847 //================================================================
848 // Function : GetDrawer
850 //================================================================
851 Handle( MeshVS_Drawer ) MeshVS_Mesh::GetDrawer() const
853 return myCurrentDrawer;
856 //================================================================
857 // Function : SetDrawer
859 //================================================================
860 void MeshVS_Mesh::SetDrawer(const Handle(MeshVS_Drawer)& aDrawer)
862 myCurrentDrawer = aDrawer;
865 //================================================================
866 // Function : GetDataSource
868 //================================================================
869 Handle(MeshVS_DataSource) MeshVS_Mesh::GetDataSource() const
874 //================================================================
875 // Function : SetDataSource
877 //================================================================
878 void MeshVS_Mesh::SetDataSource( const Handle(MeshVS_DataSource)& theDataSource )
880 myDataSource = theDataSource;
883 //================================================================
884 // Function : HilightSelected
886 //================================================================
887 void MeshVS_Mesh::HilightSelected ( const Handle(PrsMgr_PresentationManager3d)& thePM,
888 const SelectMgr_SequenceOfOwner& theOwners )
890 if ( myHilighter.IsNull() )
893 // if ( mySelectionPrs.IsNull() )
894 // mySelectionPrs = new Prs3d_Presentation ( thePM->StructureManager() );
898 Handle( Prs3d_Presentation ) aSelectionPrs;
900 aSelectionPrs = GetSelectPresentation( thePM );
902 if( HasPresentation() )
903 aSelectionPrs->SetTransformPersistence( Presentation()->TransformPersistenceMode(), Presentation()->TransformPersistencePoint() );
906 // It is very important to call this parent method, because it check whether
907 // mySelectionPrs is created and if not, create it.
915 Standard_Integer len = theOwners.Length(), i;
917 Handle (MeshVS_MeshEntityOwner) anOwner;
918 TColStd_PackedMapOfInteger aSelNodes, aSelElements;
920 for( i=1; i<=len; i++ )
922 anOwner = Handle (MeshVS_MeshEntityOwner)::DownCast ( theOwners.Value ( i ) );
923 if ( !anOwner.IsNull() )
925 // nkv: add support of mesh groups
926 if ( anOwner->IsGroup() ) {
927 MeshVS_EntityType aGroupType;
928 TColStd_PackedMapOfInteger aGroupMap;
929 if ( GetDataSource()->GetGroup( anOwner->ID(), aGroupType, aGroupMap ) ) {
930 if ( aGroupType == MeshVS_ET_Node ) {
931 for( TColStd_MapIteratorOfPackedMapOfInteger anIt(aGroupMap); anIt.More(); anIt.Next() )
932 if( IsSelectableNode/*!IsHiddenNode*/( anIt.Key() ) )
933 aSelNodes.Add( anIt.Key() );
936 for( TColStd_MapIteratorOfPackedMapOfInteger anIt(aGroupMap); anIt.More(); anIt.Next() )
937 if( IsSelectableElem/*!IsHiddenElem*/( anIt.Key() ) )
938 aSelElements.Add( anIt.Key() );
943 if( anOwner->Type() == MeshVS_ET_Node )
944 aSelNodes.Add( anOwner->ID() );
946 aSelElements.Add( anOwner->ID() );
949 else if( GetDataSource()->IsAdvancedSelectionEnabled() )
951 Handle(MeshVS_MeshOwner) aMeshOwner = Handle(MeshVS_MeshOwner)::DownCast ( theOwners.Value ( i ) );
952 if( !aMeshOwner.IsNull() )
954 Handle(TColStd_HPackedMapOfInteger) aNodes = aMeshOwner->GetSelectedNodes();
955 Handle(TColStd_HPackedMapOfInteger) aElems = aMeshOwner->GetSelectedElements();
956 if( !aNodes.IsNull() )
957 aSelNodes.Assign( aNodes->Map() );
958 if( !aElems.IsNull() )
959 aSelElements.Assign( aElems->Map() );
962 //agv else if( theOwners.Value ( i )==myWholeMeshOwner )
963 else if (IsWholeMeshOwner (theOwners.Value ( i )))
965 TColStd_MapIteratorOfPackedMapOfInteger anIt(GetDataSource()->GetAllNodes());
966 for( ; anIt.More(); anIt.Next() )
967 if( !IsHiddenNode( anIt.Key() ) )
968 aSelNodes.Add( anIt.Key() );
970 anIt = TColStd_MapIteratorOfPackedMapOfInteger( GetDataSource()->GetAllElements() );
971 for( ; anIt.More(); anIt.Next() )
972 if( !IsHiddenElem( anIt.Key() ) )
973 aSelElements.Add( anIt.Key() );
979 Standard_Boolean IsNeedToRedisplay = Standard_False;
981 aSelectionPrs->Clear();
983 myHilighter->SetDrawer ( mySelectionDrawer );
985 if( aSelNodes.Extent()>0 )
987 TColStd_PackedMapOfInteger tmp;
988 myHilighter->Build ( aSelectionPrs, aSelNodes, tmp, Standard_False, MeshVS_DMF_SelectionPrs );
990 if( aSelElements.Extent()>0 )
992 TColStd_PackedMapOfInteger tmp;
993 myHilighter->Build( aSelectionPrs, aSelElements, tmp, Standard_True, MeshVS_DMF_SelectionPrs );
996 myHilighter->SetDrawer ( 0 );
998 IsNeedToRedisplay = Standard_True;
1000 if ( IsNeedToRedisplay )
1002 aSelectionPrs->SetDisplayPriority(9);
1003 aSelectionPrs->Display();
1007 Standard_Real sec, cpu;
1008 Standard_Integer min, hour;
1010 gTimer.Show ( sec, min, hour, cpu );
1011 cout << "HilightSelected : " << endl;
1012 cout << aSelNodes.Extent() << " nodes " << endl;
1013 cout << aSelElements.Extent() << " elements " << endl;
1014 cout << "Time : " << sec << " sec" << endl;
1015 cout << "CPU time : " << cpu << " sec" << endl << endl;
1020 //================================================================
1021 // Function : HilightOwnerWithColor
1023 //================================================================
1024 void MeshVS_Mesh::HilightOwnerWithColor ( const Handle(PrsMgr_PresentationManager3d)& PM,
1025 const Quantity_NameOfColor Color,
1026 const Handle(SelectMgr_EntityOwner)& Owner)
1028 if ( myHilighter.IsNull() )
1031 SelectMgr_SequenceOfOwner theOwners;
1034 //if ( myHilightPrs.IsNull() )
1035 // myHilightPrs = new Prs3d_Presentation ( PM->StructureManager() );
1037 Handle( Prs3d_Presentation ) aHilightPrs;
1038 aHilightPrs = GetHilightPresentation( PM );
1040 aHilightPrs->Clear();
1043 if( HasPresentation() )
1044 aHilightPrs->SetTransformPersistence( Presentation()->TransformPersistenceMode(), Presentation()->TransformPersistencePoint() );
1047 if( Owner.IsNull() ) return;
1049 const Standard_Boolean isMeshEntityOwner = Owner->IsKind ( STANDARD_TYPE ( MeshVS_MeshEntityOwner ) );
1050 const Standard_Boolean isWholeMeshOwner =
1051 //agv !Owner.IsNull() && Owner==myWholeMeshOwner;
1052 IsWholeMeshOwner (Owner);
1054 Standard_Integer aDispMode = MeshVS_DMF_Shading;
1055 if ( HasDisplayMode() && ( DisplayMode() & MeshVS_DMF_OCCMask ) > MeshVS_DMF_WireFrame )
1056 aDispMode = ( DisplayMode() & MeshVS_DMF_OCCMask );
1057 //It because we draw hilighted owners only in shading or shrink (not in wireframe)
1059 myHilightDrawer->SetColor( MeshVS_DA_InteriorColor, Color );
1060 myHilightDrawer->SetColor( MeshVS_DA_BackInteriorColor, Color );
1061 myHilightDrawer->SetColor( MeshVS_DA_EdgeColor, Color );
1062 myHilightDrawer->SetColor( MeshVS_DA_BeamColor, Color );
1063 myHilightDrawer->SetColor( MeshVS_DA_MarkerColor, Color );
1064 myHilighter->SetDrawer( myHilightDrawer );
1066 if( isMeshEntityOwner )
1068 Handle ( MeshVS_MeshEntityOwner ) theAISOwner = Handle ( MeshVS_MeshEntityOwner )::DownCast ( Owner );
1069 MeshVS_EntityType aType = theAISOwner->Type();
1070 Standard_Integer anID = theAISOwner->ID();
1072 if ( theAISOwner->IsGroup() ) {
1073 MeshVS_EntityType aGroupType;
1074 TColStd_PackedMapOfInteger aGroupMap;
1075 if ( myDataSource->GetGroup( anID, aGroupType, aGroupMap ) ) {
1076 TColStd_PackedMapOfInteger tmp;
1077 myHilighter->Build( aHilightPrs, aGroupMap, tmp, aType!=MeshVS_ET_Node,
1078 aDispMode | MeshVS_DMF_HilightPrs );
1082 TColStd_PackedMapOfInteger anOne, tmp;
1084 myHilighter->Build( aHilightPrs, anOne, tmp, aType!=MeshVS_ET_Node,
1085 aDispMode | MeshVS_DMF_HilightPrs );
1088 else if( isWholeMeshOwner )
1090 if( ! GetDataSource().IsNull() )
1092 TColStd_PackedMapOfInteger tmp;
1093 myHilighter->Build( aHilightPrs, GetDataSource()->GetAllElements(), tmp,
1094 Standard_True, MeshVS_DMF_WireFrame );
1099 Handle(MeshVS_MeshOwner) aMeshOwner = Handle(MeshVS_MeshOwner)::DownCast ( Owner );
1100 if( !aMeshOwner.IsNull() )
1102 Handle(TColStd_HPackedMapOfInteger) aNodes = aMeshOwner->GetDetectedNodes();
1103 Handle(TColStd_HPackedMapOfInteger) aElems = aMeshOwner->GetDetectedElements();
1104 // hilight detected entities
1105 if( !aNodes.IsNull() )
1107 TColStd_PackedMapOfInteger tmp;
1108 myHilighter->Build( aHilightPrs, aNodes->Map(), tmp, Standard_False,
1109 aDispMode | MeshVS_DMF_HilightPrs );
1111 if( !aElems.IsNull() )
1113 TColStd_PackedMapOfInteger tmp;
1114 myHilighter->Build( aHilightPrs, aElems->Map(), tmp, Standard_True,
1115 aDispMode | MeshVS_DMF_HilightPrs );
1120 if( PM->IsImmediateModeOn() )
1121 PM->AddToImmediateList( aHilightPrs );
1122 myHilighter->SetDrawer ( 0 );
1125 //=======================================================================
1126 //function : ClearSelected
1128 //=======================================================================
1129 void MeshVS_Mesh::ClearSelected ()
1131 Handle( Prs3d_Presentation ) aSelectionPrs = GetSelectPresentation( NULL );
1132 if( !aSelectionPrs.IsNull() )
1133 aSelectionPrs->Clear();
1136 //=======================================================================
1137 //function : FindBuilder
1139 //=======================================================================
1140 Handle (MeshVS_PrsBuilder) MeshVS_Mesh::FindBuilder ( const Standard_CString theTypeName ) const
1142 Standard_Integer len = myBuilders.Length();
1143 Handle(MeshVS_PrsBuilder) aBuilder;
1144 Standard_Boolean IsExist = Standard_False;
1146 for ( Standard_Integer i=1; i<=len && !IsExist; i++)
1147 if ( myBuilders.Value (i)->IsKind ( theTypeName ) )
1149 aBuilder = myBuilders.Value (i);
1150 IsExist = Standard_True;
1156 //=======================================================================
1157 //function : SetHilighter
1159 //=======================================================================
1160 void MeshVS_Mesh::SetHilighter ( const Handle( MeshVS_PrsBuilder )& Builder )
1162 myHilighter = Builder;
1165 //=======================================================================
1166 //function : SetHilighter
1168 //=======================================================================
1169 Standard_Boolean MeshVS_Mesh::SetHilighter ( const Standard_Integer Index )
1171 Handle( MeshVS_PrsBuilder ) aBuild = GetBuilder( Index );
1172 Standard_Boolean aRes = ( !aBuild.IsNull() );
1174 myHilighter = aBuild;
1178 //=======================================================================
1179 //function : SetHilighter
1181 //=======================================================================
1182 Standard_Boolean MeshVS_Mesh::SetHilighterById ( const Standard_Integer Id )
1184 Handle( MeshVS_PrsBuilder ) aBuild = GetBuilderById( Id );
1185 Standard_Boolean aRes = ( !aBuild.IsNull() );
1187 myHilighter = aBuild;
1191 //=======================================================================
1192 //function : GetHilighter
1194 //=======================================================================
1195 Handle( MeshVS_PrsBuilder ) MeshVS_Mesh::GetHilighter () const
1200 //=======================================================================
1201 //function : IsSelectableElem
1203 //=======================================================================
1204 Standard_Boolean MeshVS_Mesh::IsSelectableElem ( const Standard_Integer ID ) const
1206 return ! IsHiddenElem ( ID );
1209 //=======================================================================
1210 //function : IsSelectableNode
1212 //=======================================================================
1213 Standard_Boolean MeshVS_Mesh::IsSelectableNode ( const Standard_Integer ID ) const
1215 return mySelectableNodes.IsNull() ? ! IsHiddenNode ( ID ) :
1216 mySelectableNodes->Map().Contains( ID );
1219 //=======================================================================
1220 //function : GetSelectableNodes
1222 //=======================================================================
1223 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_Mesh::GetSelectableNodes () const
1225 return mySelectableNodes;
1228 //=======================================================================
1229 //function : SetSelectableNodes
1231 //=======================================================================
1232 void MeshVS_Mesh::SetSelectableNodes ( const Handle(TColStd_HPackedMapOfInteger)& Ids )
1234 mySelectableNodes = Ids;
1237 //=======================================================================
1238 //function : UpdateSelectableNodes
1240 //=======================================================================
1241 void MeshVS_Mesh::UpdateSelectableNodes()
1243 mySelectableNodes = new TColStd_HPackedMapOfInteger;
1245 Standard_Integer aMaxFaceNodes;
1246 Handle( MeshVS_DataSource ) aSource = GetDataSource();
1247 if ( aSource.IsNull() || myCurrentDrawer.IsNull() || !myCurrentDrawer->GetInteger
1248 ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) || aMaxFaceNodes<=0 )
1251 // all non-hidden nodes are selectable;
1252 // by default (i.e. if myHiddenNodes.IsNull()) all nodes are hidden
1253 if ( ! myHiddenNodes.IsNull() )
1255 mySelectableNodes->ChangeMap().Subtraction (aSource->GetAllNodes(),
1256 myHiddenNodes->Map());
1259 // add all nodes belonging to non-hidden elements
1260 TColStd_MapIteratorOfPackedMapOfInteger anIter( aSource->GetAllElements() );
1261 for ( ; anIter.More(); anIter.Next() )
1263 Standard_Integer aKey = anIter.Key();
1264 if ( IsHiddenElem (aKey) )
1267 MeshVS_Buffer aNodesBuf (aMaxFaceNodes*sizeof(Standard_Integer));
1268 TColStd_Array1OfInteger aNodes (aNodesBuf, 1, aMaxFaceNodes);
1269 Standard_Integer NbNodes;
1270 if ( !aSource->GetNodesByElement ( aKey, aNodes, NbNodes ) )
1272 for ( Standard_Integer i=1; i<=NbNodes; i++ )
1273 mySelectableNodes->ChangeMap().Add ( aNodes(i) );
1277 //=======================================================================
1278 //function : GetMeshSelMethod
1280 //=======================================================================
1281 MeshVS_MeshSelectionMethod MeshVS_Mesh::GetMeshSelMethod() const
1283 return mySelectionMethod;
1286 //=======================================================================
1287 //function : SetMeshSelMethod
1289 //=======================================================================
1290 void MeshVS_Mesh::SetMeshSelMethod( const MeshVS_MeshSelectionMethod M )
1292 mySelectionMethod = M;
1295 //=======================================================================
1296 //function : IsWholeMeshOwner
1298 //=======================================================================
1300 Standard_Boolean MeshVS_Mesh::IsWholeMeshOwner
1301 (const Handle(SelectMgr_EntityOwner)& theOwn) const
1303 return theOwn.IsNull() ? Standard_False : (theOwn == myWholeMeshOwner);