0027104: DownCast() cannot return null for mismatched handle
[occt.git] / src / STEPSelections / STEPSelections_SelectInstances.cxx
1 // Created on: 1999-03-23
2 // Created by: data exchange team
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <Interface_EntityIterator.hxx>
19 #include <Interface_Graph.hxx>
20 #include <Interface_HGraph.hxx>
21 #include <Interface_Macros.hxx>
22 #include <Standard_Transient.hxx>
23 #include <Standard_Type.hxx>
24 #include <STEPConstruct_Assembly.hxx>
25 #include <StepRepr_MappedItem.hxx>
26 #include <StepRepr_RepresentationItem.hxx>
27 #include <StepRepr_ShapeRepresentationRelationship.hxx>
28 #include <STEPSelections_SelectInstances.hxx>
29 #include <StepShape_BrepWithVoids.hxx>
30 #include <StepShape_ContextDependentShapeRepresentation.hxx>
31 #include <StepShape_FaceSurface.hxx>
32 #include <StepShape_FacetedBrep.hxx>
33 #include <StepShape_FacetedBrepAndBrepWithVoids.hxx>
34 #include <StepShape_GeometricSet.hxx>
35 #include <StepShape_ManifoldSolidBrep.hxx>
36 #include <StepShape_ShapeDefinitionRepresentation.hxx>
37 #include <StepShape_ShapeRepresentation.hxx>
38 #include <StepShape_ShellBasedSurfaceModel.hxx>
39 #include <TCollection_AsciiString.hxx>
40 #include <TColStd_IndexedMapOfTransient.hxx>
41
42 IMPLEMENT_STANDARD_RTTIEXT(STEPSelections_SelectInstances,IFSelect_SelectExplore)
43
44 static Handle(Interface_HGraph) myGraph;
45 static Interface_EntityIterator myEntities;
46
47 STEPSelections_SelectInstances::STEPSelections_SelectInstances():IFSelect_SelectExplore (-1){ }
48
49 static void AddAllSharings(const Handle(Standard_Transient)& start,
50                             const Interface_Graph& graph,
51                             Interface_EntityIterator& explored) 
52 {
53   if(start.IsNull()) return;
54   Interface_EntityIterator subs = graph.Shareds(start);
55   for (subs.Start(); subs.More(); subs.Next()) {
56     explored.AddItem(subs.Value()); 
57     AddAllSharings(subs.Value(), graph, explored);
58   }
59 }
60     
61
62 static void AddInstances(const Handle(Standard_Transient)& start,
63                          const Interface_Graph& graph,
64                          Interface_EntityIterator& explored)
65 {
66   if(start.IsNull()) return;
67   
68   explored.AddItem(start);
69   if (start->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation))) {
70     DeclareAndCast(StepShape_ShapeDefinitionRepresentation,sdr,start);
71     AddInstances(sdr->UsedRepresentation(),graph,explored);
72     Interface_EntityIterator subs = graph.Shareds(start);
73     for (subs.Start(); subs.More(); subs.Next()) {
74       DeclareAndCast(StepShape_ContextDependentShapeRepresentation,anitem,subs.Value());
75       if (anitem.IsNull()) continue;
76       AddInstances(anitem,graph,explored);
77     }
78     return;
79   }
80   
81   if (start->IsKind(STANDARD_TYPE(StepShape_ShapeRepresentation))) {
82     DeclareAndCast(StepShape_ShapeRepresentation,sr,start);
83     Standard_Integer nb = sr->NbItems();
84     for (Standard_Integer i = 1; i <= nb; i++) {
85       Handle(StepRepr_RepresentationItem) anitem = sr->ItemsValue(i);
86       AddInstances(anitem,graph,explored);
87     }
88     return;
89   }
90   
91   if (start->IsKind(STANDARD_TYPE(StepShape_FacetedBrep))||
92       start->IsKind(STANDARD_TYPE(StepShape_BrepWithVoids))||
93       start->IsKind(STANDARD_TYPE(StepShape_ManifoldSolidBrep))||
94       start->IsKind(STANDARD_TYPE(StepShape_ShellBasedSurfaceModel))||
95       start->IsKind(STANDARD_TYPE(StepShape_FacetedBrepAndBrepWithVoids))||
96       start->IsKind(STANDARD_TYPE(StepShape_GeometricSet))||
97       start->IsKind(STANDARD_TYPE(StepShape_FaceSurface))||
98       start->IsKind(STANDARD_TYPE(StepRepr_MappedItem))) {
99     AddAllSharings(start, graph, explored);
100     return;
101   }
102
103   if (start->IsKind(STANDARD_TYPE(StepShape_ContextDependentShapeRepresentation))) {
104     DeclareAndCast(StepShape_ContextDependentShapeRepresentation,CDSR,start);
105     Handle(StepRepr_RepresentationRelationship) SRR = CDSR->RepresentationRelation();
106     if ( SRR.IsNull() ) return ;
107     
108     Handle(StepRepr_Representation) rep;
109     Standard_Boolean SRRReversed = STEPConstruct_Assembly::CheckSRRReversesNAUO ( graph.Model(), CDSR );
110     if(SRRReversed)
111       rep = SRR->Rep2();
112     else
113       rep = SRR->Rep1();
114     
115     Interface_EntityIterator subs = graph.Sharings(rep);
116     for (subs.Start(); subs.More(); subs.Next()) 
117       if ( subs.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation))) {
118         DeclareAndCast(StepShape_ShapeDefinitionRepresentation,SDR,subs.Value());
119         AddInstances(SDR,graph,explored);
120       }
121     //???
122     return;
123   } 
124   
125   if (start->IsKind (STANDARD_TYPE(StepRepr_ShapeRepresentationRelationship)) ) {
126     DeclareAndCast(StepRepr_ShapeRepresentationRelationship,und,start);
127     for (Standard_Integer i = 1; i <= 2; i ++) {
128       Handle(Standard_Transient) anitem;
129       if (i == 1) anitem = und->Rep1();
130       if (i == 2) anitem = und->Rep2();
131       AddInstances(anitem,graph,explored);
132     } 
133     return;
134   }
135   
136 }
137      
138      
139 Interface_EntityIterator STEPSelections_SelectInstances::RootResult(const Interface_Graph &G) const
140 {
141   if(myGraph.IsNull()||(G.Model()!=myGraph->Graph().Model()))
142     {
143       
144       Interface_EntityIterator roots = G.RootEntities();
145       myGraph = new Interface_HGraph(G);
146       myEntities.Destroy();
147       for (roots.Start(); roots.More(); roots.Next())
148         AddInstances(roots.Value(), G, myEntities);
149     }
150
151   if(HasInput()||HasAlternate()) {
152     Interface_EntityIterator select = InputResult(G);
153     Standard_Integer nbSelected = select.NbEntities();
154     TColStd_IndexedMapOfTransient filter (nbSelected);
155     for(select.Start(); select.More(); select.Next())
156       filter.Add(select.Value());
157     Interface_EntityIterator result;
158     for(myEntities.Start(); myEntities.More(); myEntities.Next()) 
159       if(filter.Contains(myEntities.Value()))
160         result.AddItem(myEntities.Value());
161     return result;
162   }
163   else
164     return myEntities;
165 }
166
167
168 Standard_Boolean STEPSelections_SelectInstances::Explore(const Standard_Integer,
169                                                         const Handle(Standard_Transient)&,
170                                                         const Interface_Graph&,
171                                                         Interface_EntityIterator&) const
172 {
173   return Standard_False;
174 }
175
176 Standard_Boolean STEPSelections_SelectInstances::HasUniqueResult() const
177 {
178   return Standard_True;
179 }
180
181 TCollection_AsciiString STEPSelections_SelectInstances::ExploreLabel() const
182 {
183   return TCollection_AsciiString ("Instances");
184 }