0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / STEPConstruct / STEPConstruct_Assembly.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 //:k8 abv 06.01.99: TR10: writing unique names for NAUOs
15 // :j4 16.03.99 gka S4134
16 // abv 18.11.99 renamed from StepPDR_MakeItem
17
18 #include <Interface_EntityIterator.hxx>
19 #include <Interface_Graph.hxx>
20 #include <Interface_InterfaceModel.hxx>
21 #include <Standard_Transient.hxx>
22 #include <StepBasic_ApplicationContext.hxx>
23 #include <StepBasic_DesignContext.hxx>
24 #include <StepBasic_HArray1OfProductContext.hxx>
25 #include <StepBasic_MechanicalContext.hxx>
26 #include <StepBasic_Product.hxx>
27 #include <StepBasic_ProductDefinition.hxx>
28 #include <StepBasic_ProductDefinitionFormationWithSpecifiedSource.hxx>
29 #include <STEPConstruct_Assembly.hxx>
30 #include <StepGeom_Axis2Placement3d.hxx>
31 #include <StepRepr_CharacterizedDefinition.hxx>
32 #include <StepRepr_HArray1OfRepresentationItem.hxx>
33 #include <StepRepr_ItemDefinedTransformation.hxx>
34 #include <StepRepr_NextAssemblyUsageOccurrence.hxx>
35 #include <StepRepr_ProductDefinitionShape.hxx>
36 #include <StepRepr_RepresentationContext.hxx>
37 #include <StepRepr_ShapeRepresentationRelationshipWithTransformation.hxx>
38 #include <StepRepr_Transformation.hxx>
39 #include <StepShape_ContextDependentShapeRepresentation.hxx>
40 #include <StepShape_ShapeDefinitionRepresentation.hxx>
41 #include <StepShape_ShapeRepresentation.hxx>
42 #include <TCollection_HAsciiString.hxx>
43
44 //  ProductDefinition (pour Relationship)
45 //  ContextDependentShapeRepresentation qui contient la Relationship
46 //  Relationship
47 //=======================================================================
48 //function : STEPConstruct_Assembly
49 //purpose  : 
50 //=======================================================================
51 STEPConstruct_Assembly::STEPConstruct_Assembly ()
52 {  
53 }
54
55 //=======================================================================
56 //function : Init
57 //purpose  : 
58 //=======================================================================
59
60 void STEPConstruct_Assembly::Init (const Handle(StepShape_ShapeDefinitionRepresentation)& aSDR,
61                                    const Handle(StepShape_ShapeDefinitionRepresentation)& SDR0,
62                                    const Handle(StepGeom_Axis2Placement3d)& Ax0,
63                                    const Handle(StepGeom_Axis2Placement3d)& AxLoc)
64 {
65   thesdr = aSDR;
66   thesdr0 = SDR0;
67   thesr  = Handle(StepShape_ShapeRepresentation)::DownCast(aSDR->UsedRepresentation());
68   thesr0 = Handle(StepShape_ShapeRepresentation)::DownCast(SDR0->UsedRepresentation());
69   theval.Nullify();
70   theax0 = Ax0;
71   theloc = AxLoc;
72 }
73
74 //=======================================================================
75 //function : MakeMappedItem
76 //purpose  : 
77 //=======================================================================
78
79 //void STEPConstruct_Assembly::MakeMappedItem ()
80 //{
81   //  not yet implemented
82 //}
83
84 //=======================================================================
85 //function : MakeRelationship
86 //purpose  : 
87 //=======================================================================
88
89 void STEPConstruct_Assembly::MakeRelationship ()
90 {
91   // get PDs for assembly (sdr0) and component (sdr)
92   Handle(StepBasic_ProductDefinition) PDED  =
93     thesdr->Definition().PropertyDefinition()->Definition().ProductDefinition();
94   Handle(StepBasic_ProductDefinition) PDING =
95     thesdr0->Definition().PropertyDefinition()->Definition().ProductDefinition();
96
97   // create NAUO
98   //:k8 abv 06 Jan 99: TR10: writing unique names for NAUOs  !!!!!
99   Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO =
100     new StepRepr_NextAssemblyUsageOccurrence;
101   static Standard_Integer id = 0;
102   Handle(TCollection_HAsciiString) ocid   = new TCollection_HAsciiString(++id);
103   Handle(TCollection_HAsciiString) ocname = new TCollection_HAsciiString("");
104   Handle(TCollection_HAsciiString) ocdesc = new TCollection_HAsciiString("");
105   Handle(TCollection_HAsciiString) refdes;  // reste nulle
106   NAUO->Init (ocid,ocname,Standard_True,ocdesc,PDING,PDED,Standard_False,refdes);
107
108   // create PDS for link CDSR->PDS->NAUO
109   Handle(StepRepr_ProductDefinitionShape) PDS =
110     new StepRepr_ProductDefinitionShape;
111   Handle(TCollection_HAsciiString) pdsname = new TCollection_HAsciiString("Placement");
112   Handle(TCollection_HAsciiString) pdsdesc = new TCollection_HAsciiString("Placement of an item");
113   StepRepr_CharacterizedDefinition CD;
114   CD.SetValue(NAUO);
115   PDS->Init (pdsname,Standard_True,pdsdesc,CD);
116
117   // create transformation
118   Handle(StepRepr_ItemDefinedTransformation) ItemDef =
119     new StepRepr_ItemDefinedTransformation;
120   Handle(TCollection_HAsciiString) idname = new TCollection_HAsciiString("");
121   Handle(TCollection_HAsciiString) idescr = new TCollection_HAsciiString("");
122   ItemDef->Init (idname,idescr,theax0,theloc);
123
124   // create SRRWT
125   Handle(StepRepr_ShapeRepresentationRelationshipWithTransformation) SRRWT =
126     new StepRepr_ShapeRepresentationRelationshipWithTransformation;
127   Handle(TCollection_HAsciiString) stname = new TCollection_HAsciiString("");
128   Handle(TCollection_HAsciiString) stescr = new TCollection_HAsciiString("");
129   StepRepr_Transformation StepTrans;
130   StepTrans.SetValue (ItemDef);
131   SRRWT->Init (stname,stescr,thesr,thesr0,StepTrans);
132
133   // create CDSR (final result, root)
134   Handle(StepShape_ContextDependentShapeRepresentation) CDSR =
135     new StepShape_ContextDependentShapeRepresentation;
136   CDSR->Init (SRRWT,PDS);
137
138   theval = CDSR;
139 }
140
141 //=======================================================================
142 //function : ItemValue
143 //purpose  : 
144 //=======================================================================
145
146 Handle(Standard_Transient)  STEPConstruct_Assembly::ItemValue () const
147 {
148   if (theval.IsNull()) return Handle(Standard_Transient) (thesr);
149   return theval;
150 }
151
152 //=======================================================================
153 //function : ItemLocation
154 //purpose  : 
155 //=======================================================================
156
157 Handle(StepGeom_Axis2Placement3d)  STEPConstruct_Assembly::ItemLocation () const
158 {
159   return theloc;
160 }
161
162 //=======================================================================
163 //function : GetNAUO
164 //purpose  : 
165 //=======================================================================
166
167 Handle(StepRepr_NextAssemblyUsageOccurrence) STEPConstruct_Assembly::GetNAUO () const
168 {
169   Handle(StepShape_ContextDependentShapeRepresentation) CDSR = 
170     Handle(StepShape_ContextDependentShapeRepresentation)::DownCast ( ItemValue() );
171   if ( ! CDSR.IsNull() ) {
172     Handle(StepBasic_ProductDefinitionRelationship) PDR = 
173       CDSR->RepresentedProductRelation()->Definition().ProductDefinitionRelationship();
174     return Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast ( PDR );
175   }
176   return 0;
177 }
178
179 //=======================================================================
180 //function : CheckSRRReversesNAUO
181 //purpose  : 
182 //=======================================================================
183
184 Standard_Boolean STEPConstruct_Assembly::CheckSRRReversesNAUO(const Interface_Graph& theGraph,
185                                                               const Handle(StepShape_ContextDependentShapeRepresentation) &CDSR)
186 {
187   Handle(StepRepr_NextAssemblyUsageOccurrence) nauo = 
188     Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast 
189           ( CDSR->RepresentedProductRelation()->Definition().ProductDefinitionRelationship() );
190   if ( nauo.IsNull() ) {
191 #ifdef OCCT_DEBUG
192     std::cout << "Warning: No NAUO found in CDSR !" << std::endl;
193 #endif
194     return Standard_False;
195   }
196
197   Handle(StepBasic_ProductDefinition) pd1, pd2;
198   Handle(StepRepr_Representation) rep1 = CDSR->RepresentationRelation()->Rep1();
199   Handle(StepRepr_Representation) rep2 = CDSR->RepresentationRelation()->Rep2();
200   if( rep1.IsNull() || rep2.IsNull())
201     return Standard_False;
202   
203   // find SDRs corresponding to Rep1 and Rep2 and remember their PDs
204   Handle(Standard_Type) tSDR = STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation);
205   Interface_EntityIterator anIter = theGraph.Sharings(rep1);
206   for (; anIter.More() && pd1.IsNull(); anIter.Next()) {
207       Handle(Standard_Transient) enti = anIter.Value();
208     if (enti->DynamicType() == tSDR) {
209       Handle(StepShape_ShapeDefinitionRepresentation) SDR =
210         Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(enti);
211       if ( SDR->UsedRepresentation() == rep1 ) 
212         pd1 = SDR->Definition().PropertyDefinition()->Definition().ProductDefinition();
213     }
214   }
215   
216   anIter = theGraph.Sharings(rep2);
217   for (; anIter.More() && pd2.IsNull(); anIter.Next()) {
218       Handle(Standard_Transient) enti = anIter.Value();
219     if (enti->DynamicType() == tSDR) {
220       Handle(StepShape_ShapeDefinitionRepresentation) SDR =
221         Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(enti);
222       if ( SDR->UsedRepresentation() == rep2 ) 
223         pd2 = SDR->Definition().PropertyDefinition()->Definition().ProductDefinition();
224     }
225   }
226
227   // checks..
228   
229   if ( pd1 == nauo->RelatedProductDefinition() && // OK
230        pd2 == nauo->RelatingProductDefinition() ) return Standard_False;
231
232   if ( pd2 == nauo->RelatedProductDefinition() && // Reversed
233        pd1 == nauo->RelatingProductDefinition() ) {
234     return Standard_True;
235   }
236
237 #ifdef OCCT_DEBUG
238   std::cout << "Warning: SRR and NAUO are incompatible" << std::endl;
239 //  std::cout << "NAUO = " << Model->StringLabel(nauo)->ToCString() << 
240 //       ",\tCDSR = " << Model->StringLabel(CDSR)->ToCString() << std::endl;
241 //  std::cout << "Rep1 = " << Model->StringLabel(rep1)->ToCString() << 
242 //       ",\tRep2 = " << Model->StringLabel(rep2)->ToCString() << std::endl;
243 //  std::cout << "PD1  = " << Model->StringLabel(pd1)->ToCString() << 
244 //       ",\tPD2  = " << Model->StringLabel(pd2)->ToCString() << std::endl;
245 //  std::cout << "Rel1 = " << Model->StringLabel(nauo->RelatingProductDefinition())->ToCString() << 
246 //       ",\tRel2 = " << Model->StringLabel(nauo->RelatedProductDefinition())->ToCString() << std::endl;
247 #endif
248
249   if ( pd2 == nauo->RelatedProductDefinition() || //:k3 abv 25 Nov 98: rp1sd.stp - bad assemblies
250        pd1 == nauo->RelatingProductDefinition() ) {
251     return Standard_True;
252   }
253
254   return Standard_False;
255 }