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