0027104: DownCast() cannot return null for mismatched handle
[occt.git] / src / STEPSelections / STEPSelections_Counter.cxx
1 // Created on: 1999-02-11
2 // Created by: Pavel DURANDIN
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_Macros.hxx>
21 #include <Standard_Transient.hxx>
22 #include <StepBasic_ProductRelatedProductCategory.hxx>
23 #include <STEPConstruct_Assembly.hxx>
24 #include <StepGeom_CompositeCurve.hxx>
25 #include <StepGeom_CompositeCurveSegment.hxx>
26 #include <StepGeom_Curve.hxx>
27 #include <StepGeom_Surface.hxx>
28 #include <StepRepr_MappedItem.hxx>
29 #include <StepRepr_RepresentationMap.hxx>
30 #include <StepRepr_RepresentationRelationship.hxx>
31 #include <StepRepr_ShapeRepresentationRelationship.hxx>
32 #include <STEPSelections_Counter.hxx>
33 #include <StepShape_BrepWithVoids.hxx>
34 #include <StepShape_ClosedShell.hxx>
35 #include <StepShape_ConnectedFaceSet.hxx>
36 #include <StepShape_ContextDependentShapeRepresentation.hxx>
37 #include <StepShape_FaceSurface.hxx>
38 #include <StepShape_FacetedBrep.hxx>
39 #include <StepShape_FacetedBrepAndBrepWithVoids.hxx>
40 #include <StepShape_GeometricCurveSet.hxx>
41 #include <StepShape_GeometricSet.hxx>
42 #include <StepShape_GeometricSetSelect.hxx>
43 #include <StepShape_ManifoldSolidBrep.hxx>
44 #include <StepShape_OpenShell.hxx>
45 #include <StepShape_OrientedClosedShell.hxx>
46 #include <StepShape_ShapeDefinitionRepresentation.hxx>
47 #include <StepShape_ShapeRepresentation.hxx>
48 #include <StepShape_Shell.hxx>
49 #include <StepShape_ShellBasedSurfaceModel.hxx>
50
51 STEPSelections_Counter::STEPSelections_Counter()
52 {
53   myNbFaces = 0;
54   myNbShells = 0;
55   myNbSolids = 0;
56   myNbWires = 0;
57   myNbEdges =0;
58 }
59
60 void STEPSelections_Counter::Count(const Interface_Graph& graph,
61                                    const Handle(Standard_Transient)& start)
62 {
63   if(start.IsNull()) return;
64   
65   if (start->IsKind(STANDARD_TYPE(StepBasic_ProductRelatedProductCategory))) return;
66   
67   if (start->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation))) {
68     DeclareAndCast(StepShape_ShapeDefinitionRepresentation,sdr,start);
69     Count(graph,sdr->UsedRepresentation());
70     Interface_EntityIterator subs = graph.Shareds(start);
71     for (subs.Start(); subs.More(); subs.Next()) {
72       DeclareAndCast(StepShape_ContextDependentShapeRepresentation,anitem,subs.Value());
73       if (anitem.IsNull()) continue;
74       Count(graph,anitem);
75     }
76     return;
77   }
78   
79   if (start->IsKind(STANDARD_TYPE(StepShape_ShapeRepresentation))) {
80     DeclareAndCast(StepShape_ShapeRepresentation,sr,start);
81     Standard_Integer nb = sr->NbItems();
82     for (Standard_Integer i = 1; i <= nb; i++) {
83       Handle(StepRepr_RepresentationItem) anitem = sr->ItemsValue(i);
84       Count(graph,anitem);
85     }
86     return;
87   }
88     
89   if (start->IsKind(STANDARD_TYPE(StepShape_FacetedBrep))) {
90     DeclareAndCast(StepShape_FacetedBrep,fbr,start);
91     myMapOfSolids.Add(start);
92     myNbSolids++;
93     AddShell(fbr->Outer());
94     return;
95   }
96   
97   if (start->IsKind(STANDARD_TYPE(StepShape_BrepWithVoids))) {
98     DeclareAndCast(StepShape_BrepWithVoids,brwv,start);
99     myMapOfSolids.Add(start);
100     myNbSolids++;
101     AddShell(brwv->Outer());
102     Standard_Integer nbvoids = brwv->NbVoids();
103     for(Standard_Integer i = 1; i <= nbvoids; i++)
104       AddShell(brwv->VoidsValue(i));
105     return;
106   }
107   
108   if (start->IsKind(STANDARD_TYPE(StepShape_ManifoldSolidBrep))) {
109     DeclareAndCast(StepShape_ManifoldSolidBrep,msbr,start);
110     myMapOfSolids.Add(start);
111     myNbSolids++;
112     AddShell(msbr->Outer());
113     return;
114   }
115   
116   if (start->IsKind(STANDARD_TYPE(StepShape_ShellBasedSurfaceModel))) {
117     DeclareAndCast(StepShape_ShellBasedSurfaceModel,sbsm,start);
118     Standard_Integer nbItems = sbsm->NbSbsmBoundary();
119     for(Standard_Integer i = 1; i <= nbItems; i++) {
120       Handle(StepShape_OpenShell) osh = sbsm->SbsmBoundaryValue(i).OpenShell();
121       if(!osh.IsNull()) AddShell(osh);
122       Handle(StepShape_ClosedShell) csh = sbsm->SbsmBoundaryValue(i).ClosedShell();
123       if(!csh.IsNull()) AddShell(csh);
124     }
125     return;  
126   }
127   
128   if (start->IsKind(STANDARD_TYPE(StepShape_FacetedBrepAndBrepWithVoids))) {
129     DeclareAndCast(StepShape_FacetedBrepAndBrepWithVoids,fbwv,start);
130     myMapOfSolids.Add(start);
131     myNbSolids++;
132     AddShell(fbwv->Outer());
133     Standard_Integer nbvoids = fbwv->NbVoids();
134     for(Standard_Integer i = 1; i <= nbvoids; i++)
135       AddShell(fbwv->VoidsValue(i));
136     return;
137   }
138   
139   if (start->IsKind(STANDARD_TYPE(StepShape_GeometricSet))) {
140     DeclareAndCast(StepShape_GeometricSet,gs,start);
141     Standard_Integer nbElem = gs->NbElements();
142     for (Standard_Integer i = 1; i <= nbElem ; i++) {
143       StepShape_GeometricSetSelect aGSS = gs->ElementsValue(i);
144       Handle(Standard_Transient) ent = aGSS.Value();
145       Handle(StepGeom_CompositeCurve) ccurve = Handle(StepGeom_CompositeCurve)::DownCast(ent);
146       if(!ccurve.IsNull()) {
147         myNbWires++;
148         myMapOfWires.Add(ccurve);
149         AddCompositeCurve(ccurve);
150       } else 
151         if(ent->IsKind(STANDARD_TYPE(StepGeom_Curve))) {
152           myNbEdges++;
153           myMapOfEdges.Add(ent);
154         } else
155           if(ent->IsKind(STANDARD_TYPE(StepGeom_Surface))) {
156             myNbFaces++;
157             myMapOfFaces.Add(ent);
158           }
159     } 
160   }
161
162   
163   if (start->IsKind(STANDARD_TYPE(StepRepr_MappedItem))) {
164     DeclareAndCast(StepRepr_MappedItem,mi,start);
165     Count(graph,mi->MappingTarget());
166     Handle(StepRepr_RepresentationMap) map =  mi->MappingSource();
167     if(map.IsNull()) return;
168     Count(graph,map->MappedRepresentation());
169     return;
170   }
171   
172   if (start->IsKind(STANDARD_TYPE(StepShape_FaceSurface))) {
173     myNbFaces++;
174     myMapOfFaces.Add(start);
175     return;
176   }
177   
178   if (start->IsKind(STANDARD_TYPE(StepShape_ContextDependentShapeRepresentation))) {
179     DeclareAndCast(StepShape_ContextDependentShapeRepresentation,CDSR,start);
180     Handle(StepRepr_RepresentationRelationship) SRR = CDSR->RepresentationRelation();
181     if ( SRR.IsNull() ) return ;
182     
183     Handle(StepRepr_Representation) rep;
184     Standard_Boolean SRRReversed = STEPConstruct_Assembly::CheckSRRReversesNAUO ( graph.Model(), CDSR );
185     if(SRRReversed)
186       rep = SRR->Rep2();
187     else
188       rep = SRR->Rep1();
189     
190     Interface_EntityIterator subs = graph.Sharings(rep);
191     for (subs.Start(); subs.More(); subs.Next()) 
192       if ( subs.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation))) {
193         DeclareAndCast(StepShape_ShapeDefinitionRepresentation,SDR,subs.Value());
194         Count(graph,SDR);
195       }
196     //???
197     return;
198   }
199   
200   if (start->IsKind (STANDARD_TYPE(StepRepr_ShapeRepresentationRelationship)) ) {
201     DeclareAndCast(StepRepr_ShapeRepresentationRelationship,und,start);
202     for (Standard_Integer i = 1; i <= 2; i ++) {
203       Handle(Standard_Transient) anitem;
204       if (i == 1) anitem = und->Rep1();
205       if (i == 2) anitem = und->Rep2();
206       Count(graph,anitem);
207     } 
208     return;
209   } 
210 }
211
212 void STEPSelections_Counter::Clear()
213 {
214   myMapOfFaces.Clear();
215   myMapOfShells.Clear();
216   myMapOfSolids.Clear();
217   myMapOfWires.Clear();
218   myMapOfEdges.Clear();
219   myNbFaces = 0;
220   myNbShells = 0;
221   myNbSolids = 0;
222   myNbWires = 0;
223   myNbEdges =0;
224 }
225
226 void STEPSelections_Counter::AddShell(const Handle(StepShape_ConnectedFaceSet)& cfs)
227 {
228   myMapOfShells.Add(cfs);
229   myNbShells++;
230   Standard_Integer nbf = cfs->NbCfsFaces();
231   for(Standard_Integer i =1; i <= nbf; i++)
232     myMapOfFaces.Add(cfs->CfsFacesValue(i));
233   myNbFaces+=nbf;
234   return;
235 }
236
237 void STEPSelections_Counter::AddCompositeCurve(const Handle(StepGeom_CompositeCurve)& ccurve)
238 {
239   Standard_Integer nbs = ccurve->NbSegments();
240   for ( Standard_Integer i=1; i <= nbs; i++ ) {
241 //  #ifdef AIX   CKY : common code for all platforms: Handle() not Handle()&
242     Handle(StepGeom_CompositeCurveSegment) ccs = ccurve->SegmentsValue ( i );
243     Handle(StepGeom_Curve) crv = ccs->ParentCurve();
244
245     if(crv->IsKind(STANDARD_TYPE(StepGeom_CompositeCurve)))
246       AddCompositeCurve(Handle(StepGeom_CompositeCurve)::DownCast(crv));
247     else {
248       myNbEdges++;
249       myMapOfEdges.Add(crv);
250     }
251   }
252 }