0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / STEPSelections / STEPSelections_AssemblyExplorer.cxx
1 // Created on: 1999-03-24
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 #include <STEPSelections_AssemblyExplorer.ixx>
18 #include <Interface_Macros.hxx>
19 #include <StepRepr_ProductDefinitionShape.hxx>
20 #include <Interface_EntityIterator.hxx>
21 #include <StepShape_ContextDependentShapeRepresentation.hxx>
22 #include <StepShape_ShapeRepresentation.hxx>
23 #include <StepRepr_MappedItem.hxx>
24 #include <STEPSelections_AssemblyLink.hxx>
25 #include <STEPSelections_HSequenceOfAssemblyLink.hxx>
26 #include <StepBasic_Product.hxx>
27 #include <StepBasic_ProductDefinitionFormation.hxx>
28 #include <Interface_InterfaceModel.hxx>
29 #include <TCollection_HAsciiString.hxx>
30 #include <StepShape_ShapeDefinitionRepresentation.hxx>
31 #include <StepRepr_PropertyDefinition.hxx>
32 #include <StepRepr_NextAssemblyUsageOccurrence.hxx>
33 #include <STEPSelections_AssemblyComponent.hxx>
34
35
36 STEPSelections_AssemblyExplorer::STEPSelections_AssemblyExplorer(const Interface_Graph &G):myGraph(G)
37 {
38   Init(G);
39 }
40
41 Handle(Standard_Transient) STEPSelections_AssemblyExplorer::
42        FindItemWithNAUO(const Handle(StepRepr_NextAssemblyUsageOccurrence)& nauo) const
43 {
44   Handle(Standard_Transient) item;
45   Handle(StepRepr_ProductDefinitionShape) pds;
46   Interface_EntityIterator subs = myGraph.Sharings(nauo);
47   for(subs.Start(); subs.More()&&pds.IsNull(); subs.Next())
48     if(subs.Value()->IsKind(STANDARD_TYPE(StepRepr_ProductDefinitionShape)))
49       pds = Handle(StepRepr_ProductDefinitionShape)::DownCast(subs.Value());
50   if(pds.IsNull()) return item;
51   
52   subs = myGraph.Sharings(pds);
53   Handle(StepShape_ContextDependentShapeRepresentation) cdsr;
54   Handle(StepShape_ShapeDefinitionRepresentation) shdefrep;
55   Handle(Standard_Transient) itmp;
56   for(subs.Start(); subs.More()&&shdefrep.IsNull(); subs.Next()) {
57     itmp = subs.Value();
58     if(itmp->IsKind(STANDARD_TYPE(StepShape_ContextDependentShapeRepresentation)))
59       return itmp;
60     if(itmp->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation)))
61       shdefrep = Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(itmp);
62   }
63   if(shdefrep.IsNull()) return shdefrep ;
64   Handle(StepShape_ShapeRepresentation) srep = Handle(StepShape_ShapeRepresentation)::DownCast(shdefrep->UsedRepresentation());
65   if(srep.IsNull()) return srep;
66   for(Standard_Integer i = 1; i <= srep->NbItems(); i++) {
67     Handle(StepRepr_RepresentationItem) repitem = srep->ItemsValue(i);
68     if(repitem->IsKind(STANDARD_TYPE(StepRepr_MappedItem)))
69       return repitem;
70   }   
71   return item;
72 }
73
74 Handle(StepShape_ShapeDefinitionRepresentation) STEPSelections_AssemblyExplorer::
75        FindSDRWithProduct(const Handle(StepBasic_ProductDefinition)& product) const
76 {
77   Interface_EntityIterator subs = myGraph.Sharings(product);
78   for(subs.Start(); subs.More(); subs.Next())
79     if(subs.Value()->IsKind(STANDARD_TYPE(StepRepr_PropertyDefinition))) {
80       Interface_EntityIterator subs1 = myGraph.Sharings(subs.Value());
81       for(subs1.Start(); subs1.More(); subs1.Next())
82         if(subs1.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation))) {
83           DeclareAndCast(StepShape_ShapeDefinitionRepresentation,SDR,subs1.Value());
84           return SDR;
85         }
86     }
87   Handle(StepShape_ShapeDefinitionRepresentation) sdr;
88   return sdr;
89 }
90
91 void STEPSelections_AssemblyExplorer::FillListWithGraph(const Handle(STEPSelections_AssemblyComponent)& cmp)
92 {
93   Handle(StepShape_ShapeDefinitionRepresentation) SDR = cmp->GetSDR();
94   Handle(STEPSelections_HSequenceOfAssemblyLink) list = cmp->GetList();
95   Handle(StepRepr_ProductDefinitionShape) pdsh = 
96     Handle(StepRepr_ProductDefinitionShape)::DownCast ( SDR->Definition().PropertyDefinition() );
97   if(pdsh.IsNull()) return;
98   Handle(StepBasic_ProductDefinition) pdf = pdsh->Definition().ProductDefinition();
99   if(pdf.IsNull()) return;
100   Interface_EntityIterator subs = myGraph.Sharings(pdf);
101   for(subs.Start(); subs.More(); subs.Next()) 
102     if(subs.Value()->IsKind(STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence))) {
103       DeclareAndCast(StepRepr_NextAssemblyUsageOccurrence,nauo,subs.Value());
104       if(pdf==nauo->RelatingProductDefinition()) {
105         Handle(STEPSelections_AssemblyLink) link = new STEPSelections_AssemblyLink;
106         link->SetNAUO(nauo);
107         link->SetItem(FindItemWithNAUO(nauo));
108         Handle(StepBasic_ProductDefinition) pdrComponent = nauo->RelatedProductDefinition();
109         if(pdrComponent.IsNull()) continue;
110         Handle(StepShape_ShapeDefinitionRepresentation) subSDR = FindSDRWithProduct(pdrComponent);
111         if(subSDR.IsNull()) continue;
112         Standard_Integer index = myMap.FindIndex(subSDR);
113         if(index)
114           link->SetComponent(Handle(STEPSelections_AssemblyComponent)::DownCast(myMap.FindFromIndex(index)));
115         else {
116           Handle(STEPSelections_HSequenceOfAssemblyLink) sublist = new STEPSelections_HSequenceOfAssemblyLink;
117           Handle(STEPSelections_AssemblyComponent) subCmp = new STEPSelections_AssemblyComponent(subSDR,sublist);
118           FillListWithGraph(subCmp);
119           link->SetComponent(subCmp);
120           myMap.Add(subSDR,subCmp);
121         }
122         list->Append(link);
123       }
124     } 
125 }
126                        
127
128 void STEPSelections_AssemblyExplorer::Init(const Interface_Graph &G)
129 {
130   myGraph = G;
131   myRoots.Clear();
132   myMap.Clear();
133   Interface_EntityIterator roots = myGraph.RootEntities();
134   for(roots.Start(); roots.More(); roots.Next()) 
135     if(roots.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation))) {
136       Handle(STEPSelections_AssemblyComponent) cmp = new STEPSelections_AssemblyComponent;
137       cmp->SetSDR(Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(roots.Value()));
138       cmp->SetList(new STEPSelections_HSequenceOfAssemblyLink);
139       FillListWithGraph(cmp);
140       myRoots.Append(cmp);
141     }
142 }
143
144
145 static Standard_CString GetProductName(const Handle(StepShape_ShapeDefinitionRepresentation) &SDR)
146 {
147   Standard_CString str = "";
148   Handle(StepBasic_Product) empty;
149   Handle(StepRepr_PropertyDefinition) PropDf = SDR->Definition().PropertyDefinition();
150   if ( PropDf.IsNull() ) return str;
151   Handle(StepBasic_ProductDefinition) PD = PropDf->Definition().ProductDefinition();
152   if ( PD.IsNull() ) return str;
153   Handle(StepBasic_ProductDefinitionFormation) PDF = PD->Formation();
154   if ( PDF.IsNull() ) return str;
155   return PDF->OfProduct()->Name()->ToCString();
156 }
157
158 static void PrintSubAssembly(Standard_OStream &os,
159                              const Handle(STEPSelections_AssemblyComponent)& cmp,
160                              const Handle(Interface_InterfaceModel) &Model,
161                              Standard_Integer level)
162 {
163   //for ( Standard_Integer j=0; j < level; j++ ) os << "\t";
164   os << "SDR: " <<Model->StringLabel(cmp->GetSDR())->ToCString()<<"\t";
165   
166   os << "Product: "<<GetProductName(cmp->GetSDR())<<endl;
167   for ( Standard_Integer i = 1; i <= cmp->GetList()->Length(); i++) {
168     for ( Standard_Integer j=0; j < level+1; j++ ) os << "\t";
169     os << "NAUO :"<<Model->StringLabel(cmp->GetList()->Value(i)->GetNAUO())->ToCString()<<";\t";
170     if(cmp->GetList()->Value(i)->GetItem()->IsKind(STANDARD_TYPE(StepRepr_MappedItem))) 
171       os <<"MI ";
172     else
173       if(cmp->GetList()->Value(i)->GetItem()->IsKind(STANDARD_TYPE(StepShape_ContextDependentShapeRepresentation)))
174         os <<"CDSR ";
175       else
176         os <<"UNKNOWN LINK!!!";
177     PrintSubAssembly(os,cmp->GetList()->Value(i)->GetComponent(),Model,level+1);
178   }
179 }
180   
181
182 void STEPSelections_AssemblyExplorer::Dump(Standard_OStream &os) const
183 {
184   Handle(Interface_InterfaceModel) model = myGraph.Model();
185   for(Standard_Integer i = 1 ; i <=myRoots.Length(); i++) {
186     os<<"Assembly N: "<<i<<endl<<endl;
187     PrintSubAssembly(os,myRoots.Value(i),model,0);
188   }
189 }