0033022: Coding - get rid of unused headers [ShapeBuild to STEPControl]
[occt.git] / src / STEPConstruct / STEPConstruct_ExternRefs.cxx
1 // Created on: 2000-09-29
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <Interface_EntityIterator.hxx>
18 #include <StepAP203_CcDesignApproval.hxx>
19 #include <StepAP203_CcDesignDateAndTimeAssignment.hxx>
20 #include <StepAP203_CcDesignPersonAndOrganizationAssignment.hxx>
21 #include <StepAP203_DateTimeItem.hxx>
22 #include <StepAP203_HArray1OfApprovedItem.hxx>
23 #include <StepAP203_HArray1OfDateTimeItem.hxx>
24 #include <StepAP203_HArray1OfPersonOrganizationItem.hxx>
25 #include <StepAP203_PersonOrganizationItem.hxx>
26 #include <StepAP214_AppliedDocumentReference.hxx>
27 #include <StepAP214_AppliedExternalIdentificationAssignment.hxx>
28 #include <StepAP214_ExternalIdentificationItem.hxx>
29 #include <StepAP214_HArray1OfDocumentReferenceItem.hxx>
30 #include <StepAP214_HArray1OfExternalIdentificationItem.hxx>
31 #include <StepBasic_ApplicationContext.hxx>
32 #include <StepBasic_ApplicationProtocolDefinition.hxx>
33 #include <StepBasic_DocumentFile.hxx>
34 #include <StepBasic_DocumentProductEquivalence.hxx>
35 #include <StepBasic_DocumentRepresentationType.hxx>
36 #include <StepBasic_DocumentType.hxx>
37 #include <StepBasic_ExternalSource.hxx>
38 #include <StepBasic_IdentificationRole.hxx>
39 #include <StepBasic_ObjectRole.hxx>
40 #include <StepBasic_ProductDefinitionContext.hxx>
41 #include <StepBasic_ProductDefinitionFormation.hxx>
42 #include <StepBasic_ProductDefinitionWithAssociatedDocuments.hxx>
43 #include <StepBasic_ProductOrFormationOrDefinition.hxx>
44 #include <StepBasic_ProductRelatedProductCategory.hxx>
45 #include <StepBasic_RoleAssociation.hxx>
46 #include <StepBasic_SourceItem.hxx>
47 #include <STEPConstruct_ExternRefs.hxx>
48 #include <StepData_SelectNamed.hxx>
49 #include <StepRepr_DescriptiveRepresentationItem.hxx>
50 #include <StepRepr_NextAssemblyUsageOccurrence.hxx>
51 #include <StepRepr_ProductDefinitionShape.hxx>
52 #include <StepRepr_PropertyDefinition.hxx>
53 #include <StepRepr_PropertyDefinitionRepresentation.hxx>
54 #include <StepRepr_RepresentationContext.hxx>
55 #include <StepRepr_RepresentedDefinition.hxx>
56 #include <StepShape_ShapeRepresentation.hxx>
57 #include <TCollection_HAsciiString.hxx>
58 #include <XSControl_WorkSession.hxx>
59 #include <XSControl_TransferReader.hxx>
60 #include <OSD_File.hxx>
61 #include <OSD_Path.hxx>
62
63 //=======================================================================
64 //function : STEPConstruct_ExternRefs
65 //purpose  : 
66 //=======================================================================
67 STEPConstruct_ExternRefs::STEPConstruct_ExternRefs ()
68 {
69 }
70      
71 //=======================================================================
72 //function : STEPConstruct_ExternRefs
73 //purpose  : 
74 //=======================================================================
75
76 STEPConstruct_ExternRefs::STEPConstruct_ExternRefs (const Handle(XSControl_WorkSession) &WS)
77      : STEPConstruct_Tool ( WS )
78 {
79 }
80
81 //=======================================================================
82 //function : Init
83 //purpose  : 
84 //=======================================================================
85
86 Standard_Boolean STEPConstruct_ExternRefs::Init (const Handle(XSControl_WorkSession) &WS)
87 {
88   Clear();
89   return SetWS ( WS );
90 }
91
92 //=======================================================================
93 //function : Clear
94 //purpose  : 
95 //=======================================================================
96
97 void STEPConstruct_ExternRefs::Clear ()
98 {
99   myAEIAs.Clear();
100   myRoles.Clear();
101   myFormats.Clear();
102   myShapes.Clear();
103   myTypes.Clear();
104   myIsAP214.Clear();
105   // PTV 30.01.2003 TRJ11
106   myDocFiles.Clear();
107   mySharedPRPC.Nullify();
108   mySharedDocType.Nullify();
109   mySharedPDC.Nullify();
110   mySharedPC.Nullify();
111   myAPD.Nullify();
112 }
113   
114 //=======================================================================
115 //function : LoadExternRefs
116 //purpose  : 
117 //=======================================================================
118
119 static Standard_Boolean findPDWADandExcludeExcess (Handle(StepAP214_AppliedDocumentReference)& ADR,
120                                                    TColStd_SequenceOfTransient& aSeqOfPDWAD,
121                                                    const Interface_Graph& Graph,
122                                                    Handle(StepBasic_ProductDefinitionWithAssociatedDocuments)& aPDWAD)
123 {
124   // WARNING! do not add check for aSeqOfPDWAD.Length() and exit if it < 1,
125   // because this methods invokes with an empty sequence too to find PDWAD by ADR
126   Interface_EntityIterator subsADR = Graph.Shareds(ADR);
127   for ( subsADR.Start(); subsADR.More(); subsADR.Next() ) {
128     if ( !subsADR.Value()->IsKind (STANDARD_TYPE(StepBasic_Document)) )
129       continue;
130     Handle(StepBasic_Document) aDoc = Handle(StepBasic_Document)::DownCast(subsADR.Value());
131     // looking for Document Product Equivalence
132     Interface_EntityIterator subsD = Graph.Sharings(aDoc);
133     for ( subsD.Start(); subsD.More(); subsD.Next() ) {
134       if ( !subsD.Value()->IsKind (STANDARD_TYPE(StepBasic_DocumentProductEquivalence)) )
135         continue;
136       Handle(StepBasic_DocumentProductEquivalence) aDPE =
137         Handle(StepBasic_DocumentProductEquivalence)::DownCast(subsD.Value());
138       // take PDF and search the same PDF by PDWAD chain
139       Interface_EntityIterator subsDPE = Graph.Shareds(aDPE);
140       for ( subsDPE.Start(); subsDPE.More(); subsDPE.Next() ) {
141         if ( !subsDPE.Value()->IsKind (STANDARD_TYPE(StepBasic_ProductDefinitionFormation)) )
142           continue;
143         Handle(StepBasic_ProductDefinitionFormation) aPDF =
144           Handle(StepBasic_ProductDefinitionFormation)::DownCast(subsDPE.Value());
145         Interface_EntityIterator subs = Graph.Sharings(aPDF);
146         for ( subs.Start(); subs.More(); subs.Next() ) {
147           if ( !subs.Value()->IsKind (STANDARD_TYPE(StepBasic_ProductDefinitionWithAssociatedDocuments)) )
148             continue;
149           aPDWAD = Handle(StepBasic_ProductDefinitionWithAssociatedDocuments)::DownCast(subs.Value());
150         }
151         // now searching for PDWAD that refer to the same PDF
152         for (Standard_Integer pdwadi = 1; pdwadi <= aSeqOfPDWAD.Length(); pdwadi++) {
153           Handle(StepBasic_ProductDefinitionWithAssociatedDocuments) aCurPDWAD =
154             Handle(StepBasic_ProductDefinitionWithAssociatedDocuments)::DownCast(aSeqOfPDWAD(pdwadi));
155           if ( !aCurPDWAD.IsNull() && aPDWAD == aCurPDWAD ) {
156             // found the same Product Definition Formation
157             aSeqOfPDWAD.Remove( pdwadi );
158             return Standard_True;
159           }
160         }
161       } // end of looking for PDF by ADR chain
162     } // end of looking for DPE
163   } // end iterations on Shareds(ADR)
164   return Standard_False;
165 }
166
167
168 Standard_Boolean STEPConstruct_ExternRefs::LoadExternRefs ()
169 {
170   // iterate on entities in the model and find AEIAs
171   // or PDWADs (for AP203)
172   Handle(Interface_InterfaceModel) model = Model();
173   Handle(Standard_Type) tADR = STANDARD_TYPE(StepAP214_AppliedDocumentReference);
174   Handle(Standard_Type) tPDWAD = STANDARD_TYPE(StepBasic_ProductDefinitionWithAssociatedDocuments);
175   Standard_Integer nb = model->NbEntities();
176   
177   // PTV 28.01.2003 CAX-IF TRJ11, file ext_ref_master.stp 
178   // search all ADR and PDWAD and exclude excess PDWADs
179   TColStd_SequenceOfTransient aSeqOfADR, aSeqOfPDWAD;
180   for (Standard_Integer ient = 1; ient <= nb; ient ++) {
181     Handle(Standard_Transient) enti = model->Value(ient);
182     if ( enti->DynamicType() == tPDWAD )
183       aSeqOfPDWAD.Append( enti );
184     else if ( enti->DynamicType() == tADR )
185       aSeqOfADR.Append( enti );
186   }
187   Standard_Integer IsAP214 = 0;
188   // run on sequence aSeqOfADR of ADR and remove excess PDWAD from aSeqOfPDWAD
189   for (Standard_Integer adri = 1; adri <= aSeqOfADR.Length(); adri++) {
190     Handle(StepAP214_AppliedDocumentReference) ADR = 
191       Handle(StepAP214_AppliedDocumentReference)::DownCast(aSeqOfADR.Value(adri));
192     // looking for Product Definition Formation and exclude excess PDWAD from aSeqOfPDWAD
193     Handle(StepBasic_ProductDefinitionWithAssociatedDocuments) aPDWAD;    
194     findPDWADandExcludeExcess( ADR, aSeqOfPDWAD, Graph(), aPDWAD );
195     
196     // now add all necessary information as original implementation.
197     IsAP214 = 1;
198     Handle(StepBasic_RoleAssociation) Role;
199     Handle(StepBasic_ProductDefinition) Shape;
200     Handle(StepRepr_PropertyDefinitionRepresentation) Format;
201     Handle(StepBasic_DocumentRepresentationType) Type;
202     // AppliedDocumentReference with RoleAssociation...
203     Interface_EntityIterator subs4 = Graph().Sharings(ADR);
204     for (subs4.Start(); subs4.More(); subs4.Next()) {
205       if ( subs4.Value()->IsKind ( STANDARD_TYPE(StepBasic_RoleAssociation) ) )
206         Role = Handle(StepBasic_RoleAssociation)::DownCast(subs4.Value());
207     }
208     
209     subs4 = Graph().Shareds(ADR);
210     for (subs4.Start(); subs4.More(); subs4.Next()) {
211       if ( subs4.Value()->IsKind ( STANDARD_TYPE(StepBasic_ProductDefinition) ) )
212         Shape = Handle(StepBasic_ProductDefinition)::DownCast(subs4.Value());
213     }
214     // search for Document file
215     Handle(StepBasic_DocumentFile) DocFile;
216     if ( aPDWAD.IsNull() ) { // shouldn't begin from TRJ11
217       // lookinf from ADR
218       subs4 = Graph().Shareds(ADR);
219     } else 
220       // looking from PDWAD
221       subs4 = Graph().Shareds(aPDWAD);
222     
223     for (subs4.Start(); subs4.More(); subs4.Next()) {
224       if ( !subs4.Value()->IsKind ( STANDARD_TYPE(StepBasic_DocumentFile) ) )
225         continue;
226       DocFile = Handle(StepBasic_DocumentFile)::DownCast(subs4.Value());
227       if ( DocFile.IsNull() )
228         continue;
229       // for each DocumentFile, find associated with it data:
230       Interface_EntityIterator subs = Graph().Sharings(DocFile);
231       for (subs.Start(); subs.More(); subs.Next()) {
232         Handle(Standard_Transient) sub = subs.Value();
233         
234         // FORMAT - ???????
235         //
236         // PDRs of a shape and of a file format
237         if ( sub->IsKind ( STANDARD_TYPE(StepRepr_PropertyDefinition) ) ) {
238           Handle(StepRepr_PropertyDefinition) PD = Handle(StepRepr_PropertyDefinition)::DownCast(sub);
239           Interface_EntityIterator subs2 = Graph().Sharings(PD);
240           for (subs2.Start(); subs2.More(); subs2.Next()) {
241             Handle(StepRepr_PropertyDefinitionRepresentation) PDR =
242               Handle(StepRepr_PropertyDefinitionRepresentation)::DownCast(subs2.Value());
243             if ( PDR.IsNull() ) continue;
244             if (  PDR->UsedRepresentation()->IsKind(STANDARD_TYPE(StepShape_ShapeRepresentation)) )
245               Format = PDR;
246           }
247         }
248         // DocumentRepresentationType
249         if ( sub->IsKind ( STANDARD_TYPE(StepBasic_DocumentRepresentationType) ) ) {
250           Type = Handle(StepBasic_DocumentRepresentationType)::DownCast(sub);
251         }
252         if ( !Type.IsNull() && !Format.IsNull() )
253           break;
254       }
255       if ( !Type.IsNull() && !Format.IsNull() )
256         break;
257     }
258     if ( DocFile.IsNull() )
259       continue;
260     myAEIAs.Append ( ADR );
261     myRoles.Append ( Role );
262     myFormats.Append ( Format );
263     myShapes.Append ( Shape );
264     myTypes.Append ( Type );
265     myIsAP214.Append ( IsAP214 );
266     myDocFiles.Append( DocFile );
267   } // end iterations on aSeqOfADR
268   
269   // now iterates on sequence aSeqOfPDWAD of Product Definition With Associated Documents
270   for (Standard_Integer pdwadi = 1; pdwadi <= aSeqOfPDWAD.Length(); pdwadi++) {
271     IsAP214 = 0;
272     Handle(StepBasic_ProductDefinitionWithAssociatedDocuments) aPDWAD =
273       Handle(StepBasic_ProductDefinitionWithAssociatedDocuments)::DownCast(aSeqOfPDWAD(pdwadi));
274     myShapes.Append(aPDWAD);
275     myIsAP214.Append ( IsAP214 );
276     Handle(StepAP214_AppliedExternalIdentificationAssignment) AEIA;
277     Handle(StepBasic_RoleAssociation) Role;
278     Handle(StepRepr_PropertyDefinitionRepresentation) Format;
279     Handle(StepBasic_DocumentRepresentationType) Type;
280     Handle(StepBasic_DocumentFile) DocFile;
281     myAEIAs.Append ( AEIA );
282     myRoles.Append ( Role );
283     myFormats.Append ( Format );
284     myTypes.Append ( Type );
285     myDocFiles.Append( DocFile );
286   }
287   
288   return myShapes.Length() >0;
289 }
290
291 //=======================================================================
292 //function : NbExternRefs
293 //purpose  : 
294 //=======================================================================
295
296 Standard_Integer STEPConstruct_ExternRefs::NbExternRefs () const
297 {
298   return myShapes.Length();
299 }
300
301 //=======================================================================
302 //function : FileName
303 //purpose  : 
304 //=======================================================================
305
306 Standard_CString STEPConstruct_ExternRefs::FileName (const Standard_Integer num) const
307 {
308   Handle(StepBasic_DocumentFile) DocFile;
309   Handle(StepAP214_AppliedExternalIdentificationAssignment) AEIA;
310   Standard_CString aCStringFileName = 0;
311   if ( myDocFiles.Length() >= num && !myDocFiles.Value(num).IsNull() )
312     DocFile = Handle(StepBasic_DocumentFile)::DownCast(myDocFiles.Value( num ));
313   else if (myIsAP214(num)==1)
314   {
315     Handle(StepAP214_AppliedDocumentReference) ADR = 
316       Handle(StepAP214_AppliedDocumentReference)::DownCast ( myAEIAs(num) );
317
318     // PTV 28.01.2003 CAX-IF TRJ11, file ext_ref_master.stp 
319     // search document file name by long chain ADR->D<-DPE->PDF<-PDWAD->DF
320     Handle(StepBasic_ProductDefinitionWithAssociatedDocuments) aPDWAD;
321     // create an empty aSeqOfPDWAD
322     TColStd_SequenceOfTransient aSeqOfPDWAD; 
323     // we do not need to exclude, just find PDWAD
324     findPDWADandExcludeExcess( ADR, aSeqOfPDWAD, Graph(), aPDWAD );
325     
326     // search for Document file
327     Interface_EntityIterator subs4;
328     if ( aPDWAD.IsNull() ) { // shouldn't begin from TRJ11
329       // lookinf from ADR
330       subs4 = Graph().Shareds(ADR);
331     } else 
332       // looking from PDWAD
333       subs4 = Graph().Shareds(aPDWAD);
334     for (subs4.Start(); subs4.More(); subs4.Next()) {
335       if ( !subs4.Value()->IsKind ( STANDARD_TYPE(StepBasic_DocumentFile) ) )
336         continue;
337       DocFile = Handle(StepBasic_DocumentFile)::DownCast(subs4.Value());
338       if ( DocFile.IsNull() ) continue;
339     }
340   }
341   else  {
342     Handle(StepBasic_ProductDefinitionWithAssociatedDocuments) aPDWAD =
343       Handle(StepBasic_ProductDefinitionWithAssociatedDocuments)::DownCast(myShapes(num));
344     if ( aPDWAD.IsNull() || aPDWAD->DocIds().IsNull() )
345       return "";
346     Standard_Integer i;
347     for ( i=1; i <= aPDWAD->NbDocIds(); i++ ) {
348       Handle(StepBasic_Document) Doc = aPDWAD->DocIdsValue(i);
349       Handle(TCollection_HAsciiString) aFilename = Doc->Name();
350       if (!aFilename.IsNull() && !aFilename->IsEmpty()) return aFilename->ToCString();
351     }
352     return "";
353   }
354   // take name from AEIA and from DF  
355   if(!DocFile.IsNull()) {
356     Interface_EntityIterator subs3 = Graph().Sharings(DocFile);
357     for (subs3.Start(); subs3.More(); subs3.Next()) {
358       if (subs3.Value()->IsKind(STANDARD_TYPE(StepAP214_AppliedExternalIdentificationAssignment))) {
359         AEIA = Handle(StepAP214_AppliedExternalIdentificationAssignment)::DownCast(subs3.Value());
360         if (!AEIA.IsNull())
361           break;
362       }
363     }
364   }
365   if(!AEIA.IsNull()) {
366     Handle(TCollection_HAsciiString) aFilename;
367     aFilename = AEIA->AssignedId();
368     if (!aFilename.IsNull() && !aFilename->IsEmpty()) {
369       aCStringFileName = aFilename->ToCString();
370       // ptv 29.01.2003 file trj4_xr1-tc-214.stp entity #71 have id "#71"
371       if ( aCStringFileName && aCStringFileName[0] == '#')
372         aCStringFileName = 0;
373     }
374     if ( ! aCStringFileName || ! aCStringFileName[0] ) {
375       // try to take name from external source 
376       Handle(StepBasic_ExternalSource) theSource = AEIA->Source();
377       if (!theSource.IsNull()) {
378         StepBasic_SourceItem theSourceId = theSource->SourceId();
379         if (!theSourceId.IsNull()) {
380           Handle(StepData_SelectNamed) theFileName;
381           theFileName = Handle(StepData_SelectNamed)::DownCast (theSourceId.Value());
382           if (theFileName.IsNull() || theFileName->Kind()!=6 ) {
383             // nothing to do, hope could take name later.
384           }
385           else
386             aCStringFileName = theFileName->String();
387         }
388       }
389     }
390   }
391   Standard_CString oldFileName = 0;
392   // compute true path to the extern file
393   OSD_Path mainfile(WS()->LoadedFile());
394   mainfile.SetName("");
395   mainfile.SetExtension("");
396   TCollection_AsciiString dpath;
397   mainfile.SystemName(dpath);
398   if (aCStringFileName && aCStringFileName[0]) {
399     TCollection_AsciiString fullname = OSD_Path::AbsolutePath(dpath, aCStringFileName);
400     if (fullname.Length() <= 0) fullname = aCStringFileName;
401     if (!OSD_File(fullname).Exists()) {
402       oldFileName = aCStringFileName;
403       aCStringFileName = 0;
404     }
405   }
406   if (!aCStringFileName || !aCStringFileName[0]) {
407     // try to find name of the directory from DocFile
408     if ( !DocFile.IsNull() ) {
409       Handle(TCollection_HAsciiString) aFilename = DocFile->Id();
410       if (!aFilename.IsNull() && !aFilename->IsEmpty())
411         aCStringFileName = aFilename->ToCString();
412       if ( ! aCStringFileName || ! aCStringFileName[0] ) {
413         aFilename = DocFile->Name();
414       if (!aFilename.IsNull() && !aFilename->IsEmpty())
415         aCStringFileName = aFilename->ToCString();
416       }
417       if ( ! aCStringFileName || ! aCStringFileName[0] ) {
418         if (oldFileName) {
419           aCStringFileName = oldFileName;
420         }
421         else {
422           return "";
423         }
424       }
425     }
426   }
427   TCollection_AsciiString fullname = OSD_Path::AbsolutePath(dpath, aCStringFileName);
428   if (fullname.Length() <= 0) fullname = aCStringFileName;
429   if (!OSD_File(fullname).Exists()) {
430     if (oldFileName) {
431       aCStringFileName = oldFileName;
432     }
433     Handle(Transfer_TransientProcess) aTP = WS()->TransferReader()->TransientProcess();
434     TCollection_AsciiString aMess("Can not read external file ");
435     aMess.AssignCat(aCStringFileName);
436     aTP->AddFail(DocFile, aMess.ToCString());
437   }
438   else {
439     if (oldFileName && strcmp(oldFileName, aCStringFileName) != 0) {
440       Handle(Transfer_TransientProcess) aTP = WS()->TransferReader()->TransientProcess();
441       TCollection_AsciiString aMess("External file with name from entity AEIA (");
442       aMess.AssignCat(oldFileName);
443       aMess.AssignCat(") not existed => use file name from DocumentFile entity - ");
444       aMess.AssignCat(aCStringFileName);
445       aTP->AddWarning(DocFile, aMess.ToCString());
446     }
447   }
448   return aCStringFileName;
449 }
450
451 //=======================================================================
452 //function : ProdDef
453 //purpose  : 
454 //=======================================================================
455
456 Handle(StepBasic_ProductDefinition) STEPConstruct_ExternRefs::ProdDef (const Standard_Integer num) const
457 {
458   return Handle(StepBasic_ProductDefinition)::DownCast( myShapes(num) );
459 }
460
461 //=======================================================================
462 //function : DocFile
463 //purpose  : 
464 //=======================================================================
465
466 Handle(StepBasic_DocumentFile) STEPConstruct_ExternRefs::DocFile(const Standard_Integer num) const
467 {
468   return Handle(StepBasic_DocumentFile)::DownCast(myDocFiles.Value(num));
469 }
470
471 //=======================================================================
472 //function : Format
473 //purpose  : 
474 //=======================================================================
475
476 Handle(TCollection_HAsciiString) STEPConstruct_ExternRefs::Format (const Standard_Integer num) const
477 {
478   Handle(TCollection_HAsciiString) Format;
479   
480   if (myIsAP214(num)==0) return Format;
481   
482   Handle(StepRepr_PropertyDefinitionRepresentation) PDR =
483     Handle(StepRepr_PropertyDefinitionRepresentation)::DownCast ( myFormats(num) );
484   if (PDR.IsNull()) return Format;
485   
486   Handle(StepRepr_Representation) rep = PDR->UsedRepresentation();
487   for ( Standard_Integer i=1; i <= rep->NbItems(); i++ ) {
488     if ( rep->ItemsValue(i)->IsKind ( STANDARD_TYPE(StepRepr_DescriptiveRepresentationItem) ) ) {
489       Handle(StepRepr_DescriptiveRepresentationItem) DRI =
490         Handle(StepRepr_DescriptiveRepresentationItem)::DownCast ( rep->ItemsValue(i) );
491       Format = DRI->Description();
492       break;
493     }
494   }
495   
496   return Format;
497 }
498
499 //=======================================================================
500 //function : AddExternRef
501 //purpose  : 
502 //=======================================================================
503
504 Standard_Integer STEPConstruct_ExternRefs::AddExternRef (const Standard_CString filename,
505                                                          const Handle(StepBasic_ProductDefinition) &PD,
506                                                          const Standard_CString format)
507 {
508   Handle(TCollection_HAsciiString) EmptyString = new TCollection_HAsciiString("");
509   Handle(TCollection_HAsciiString) fmt = new TCollection_HAsciiString(format);
510   Handle(TCollection_HAsciiString) tmp = new TCollection_HAsciiString("203");
511   Standard_Integer np = fmt->Location(tmp,1,fmt->Length());
512
513 //  if( !(fmt==tmp) ) {
514   if( !(np>0) ) {
515   
516     // create core entity DocumentFile
517     Handle(StepBasic_DocumentType) DT = new StepBasic_DocumentType;
518     DT->Init(EmptyString);
519     Handle(TCollection_HAsciiString) DFid = new TCollection_HAsciiString(filename);
520     // PTV 30.01.2003 TRJ11 -  copy external filename as is
521 //     DFid->AssignCat ( " file id" );
522     Handle(StepBasic_DocumentFile) DF = new StepBasic_DocumentFile;
523     DF->Init(DFid, EmptyString, Standard_False, EmptyString, DT, EmptyString, Standard_False, EmptyString);
524   
525     // create AppliedExternalIdentificationAssignment et al
526     Handle(StepBasic_IdentificationRole) IR = new StepBasic_IdentificationRole;
527     // PTV 30.01.2003 TRJ11 
528     //    - set the ("external document id and location", $) without unmeaning description
529     Handle(TCollection_HAsciiString) aName = 
530       new TCollection_HAsciiString("external document id and location");
531     IR->SetName( aName );
532 //     Handle(TCollection_HAsciiString) aIRdescr = new TCollection_HAsciiString("source system");
533 //     IR->Init(aName, Standard_True, aIRdescr);
534       
535     Handle(StepData_SelectNamed) SDS = new StepData_SelectNamed;
536     SDS->SetString ( filename );
537     SDS->SetName("IDENTIFIER");
538     StepBasic_SourceItem SID;
539     SID.SetValue(SDS);
540     Handle(StepBasic_ExternalSource) ES = new StepBasic_ExternalSource;
541     ES->Init(SID);
542     
543     StepAP214_ExternalIdentificationItem Item;
544     Item.SetValue(DF);
545     Handle(StepAP214_HArray1OfExternalIdentificationItem) Items = 
546       new StepAP214_HArray1OfExternalIdentificationItem(1,1);
547     Items->SetValue(1, Item);
548
549     Handle(StepAP214_AppliedExternalIdentificationAssignment) ExtIdent = 
550       new StepAP214_AppliedExternalIdentificationAssignment;
551 //     ExtIdent->Init(EmptyString, IR, ES, Items);
552     // PTV 30.01.2003 TRJ11 - store filename in AEIA
553     Handle(TCollection_HAsciiString) aFName = new TCollection_HAsciiString(filename);
554     ExtIdent->Init(aFName, IR, ES, Items);
555     // create DocumentRepresentationType
556     Handle(TCollection_HAsciiString) Dig = new TCollection_HAsciiString("digital");
557     Handle(StepBasic_DocumentRepresentationType) Type = new StepBasic_DocumentRepresentationType;
558     Type->Init(Dig, DF);
559     
560     // create AppliedDocumentReference, 
561     Handle(StepAP214_AppliedDocumentReference) ADR = new StepAP214_AppliedDocumentReference;
562     // PTV 30.01.2003 TRJ11 - create additional entities for AP214
563     addAP214ExterRef( ADR, PD, DF, filename );
564     
565     // create RoleAssociation etc.
566     Handle(StepBasic_ObjectRole) OR = new StepBasic_ObjectRole;
567     Handle(TCollection_HAsciiString) mandatory = new TCollection_HAsciiString("mandatory");
568     OR->Init(mandatory, Standard_False, EmptyString);
569     StepBasic_RoleSelect RS;
570     RS.SetValue(ADR);
571     Handle(StepBasic_RoleAssociation) Role = new StepBasic_RoleAssociation;
572     Role->Init(OR, RS);
573       
574     // create PDR for association with SR
575     StepRepr_CharacterizedDefinition CD; 
576     CD.SetValue(DF);
577     Handle(TCollection_HAsciiString) PDname = new TCollection_HAsciiString("external definition");
578     Handle(StepRepr_PropertyDefinition) PropD = new StepRepr_PropertyDefinition;
579     PropD->Init(PDname, Standard_True, EmptyString, CD);
580     StepRepr_RepresentedDefinition RD;
581     RD.SetValue(PropD);
582 //    Handle(StepRepr_PropertyDefinitionRepresentation) PDRshape = new StepRepr_PropertyDefinitionRepresentation;
583 //    PDRshape->Init ( RD, SDR->UsedRepresentation() );
584   
585     // create PDR for definition of document format (if defined)
586     Handle(StepRepr_PropertyDefinitionRepresentation) PDRformat;
587     if ( format && format[0] ) {
588
589       Handle(TCollection_HAsciiString) RCftype = new TCollection_HAsciiString ( "document parameters" );
590       Handle(StepRepr_RepresentationContext) RCf = new StepRepr_RepresentationContext;
591       RCf->Init ( EmptyString, RCftype );
592       
593       Handle(TCollection_HAsciiString) DRIname = new TCollection_HAsciiString ( "data format" );
594       Handle(TCollection_HAsciiString) DRIdscr = new TCollection_HAsciiString ( format );
595       Handle(StepRepr_DescriptiveRepresentationItem) DRI = new StepRepr_DescriptiveRepresentationItem;
596       DRI->Init ( DRIname, DRIdscr );
597       Handle(StepRepr_HArray1OfRepresentationItem) fItems = new StepRepr_HArray1OfRepresentationItem(1,1);
598       fItems->SetValue ( 1, DRI );
599     
600       Handle(TCollection_HAsciiString) SRfname = new TCollection_HAsciiString ( "document format" );
601       Handle(StepRepr_Representation) SRformat = new StepRepr_Representation;
602       SRformat->Init(SRfname, fItems, RCf);
603     
604       StepRepr_CharacterizedDefinition CDf; 
605       CDf.SetValue(DF);
606       Handle(TCollection_HAsciiString) PDfname = new TCollection_HAsciiString("document property");
607       Handle(StepRepr_PropertyDefinition) PDf = new StepRepr_PropertyDefinition;
608       PDf->Init(PDfname, Standard_True, EmptyString, CDf);
609       StepRepr_RepresentedDefinition RDf;
610       RDf.SetValue(PDf);
611       
612       PDRformat = new StepRepr_PropertyDefinitionRepresentation;
613       PDRformat->Init ( RDf, SRformat );
614     }
615   
616     // add all the created root entities to sequences
617     myAEIAs.Append ( ExtIdent );     //StepAP214_AppliedExternalIdentificationAssignment
618     myRoles.Append ( Role );         //StepBasic_RoleAssociation
619     myFormats.Append ( PDRformat );  //StepRepr_PropertyDefinitionRepresentation
620 //    myShapes.Append ( PDRshape );    //StepRepr_PropertyDefinitionRepresentation
621     myShapes.Append ( PD );          //StepBasic_ProductDefinition
622     myTypes.Append ( Type );         //StepBasic_DocumentRepresentationType
623
624   }
625
626   else { // format=="AP203"
627     
628 //    StepRepr_RepresentedDefinition aRD = SDR->Definition();
629 //    Handle(StepRepr_PropertyDefinition) aPD = aRD.PropertyDefinition();
630 //    StepRepr_CharacterizedDefinition aCD = aPD->Definition();
631 //    Handle(StepBasic_ProductDefinition) aProdDef = aCD.ProductDefinition();
632     Handle(StepBasic_ProductDefinitionFormation) ProdDefForm = PD->Formation();
633     Handle(StepBasic_ProductDefinitionContext) ProdDefCont = PD->FrameOfReference();
634
635     // create document
636     Handle(TCollection_HAsciiString) fname = new TCollection_HAsciiString(filename);
637     Handle(StepBasic_DocumentType) aDocType = new StepBasic_DocumentType;
638     Handle(TCollection_HAsciiString) aDT = new TCollection_HAsciiString("cad_filename");
639     aDocType->Init(aDT);
640     Handle(StepBasic_Document) aDoc = new StepBasic_Document;
641     Handle(TCollection_HAsciiString) aDescription = 
642       new TCollection_HAsciiString("CAD Model associated to the part");
643     aDoc->Init(EmptyString,fname,Standard_True,aDescription,aDocType);
644     Handle(StepBasic_HArray1OfDocument) aDocIds = new StepBasic_HArray1OfDocument(1,1);
645     aDocIds->SetValue(1,aDoc);
646     
647     // create ProductDefinitionWithAssociatedDocuments
648     aDescription = PD->Description();
649     Handle(StepBasic_ProductDefinitionWithAssociatedDocuments) PDWAD = 
650       new StepBasic_ProductDefinitionWithAssociatedDocuments;
651     PDWAD->Init(EmptyString,aDescription,ProdDefForm,ProdDefCont,aDocIds);
652     //Handle(StepBasic_ProductDefinitionWithAssociatedDocuments) PDWAD = 
653     //  Handle(StepBasic_ProductDefinitionWithAssociatedDocuments)::DownCast(PD);
654     
655     // searh in graph for replace
656 //    Standard_Integer numProdDef;
657 //    Interface_EntityIterator subs = Graph().Shareds(SDR);
658 //    for (subs.Start(); subs.More(); subs.Next()) {
659 //      Handle(Standard_Transient) sub = subs.Value();
660     Interface_EntityIterator subs = Graph().Sharings(PD);
661     for (subs.Start(); subs.More(); subs.Next()) {
662       Handle(Standard_Transient) sub = subs.Value();
663       if (!sub->IsKind(STANDARD_TYPE(StepRepr_ProductDefinitionShape))) continue;
664       Handle(StepRepr_ProductDefinitionShape) ProdDefSh = 
665         Handle(StepRepr_ProductDefinitionShape)::DownCast ( sub );
666       if(ProdDefSh.IsNull()) continue;
667       StepRepr_CharacterizedDefinition CDf;
668       CDf.SetValue(PDWAD);
669       ProdDefSh->SetDefinition(CDf);
670     }
671
672 //      Interface_EntityIterator subs1 = Graph().Shareds(ProdDefSh);
673 //      for (subs1.Start(); subs1.More(); subs1.Next()) {
674
675 //        Handle(Standard_Transient) sub1 = subs1.Value();
676 //        if (!sub1->IsKind(STANDARD_TYPE(StepBasic_ProductDefinition))) continue;
677 //        Handle(StepBasic_ProductDefinition) ProdDef = 
678 //          Handle(StepBasic_ProductDefinition)::DownCast ( sub1 );
679 //        numProdDef = Model()->Number(ProdDef);
680     Standard_Integer numProdDef = Model()->Number(PD);
681
682 //        Interface_EntityIterator subs2 = Graph().Sharings(ProdDef);
683     Interface_EntityIterator subs2 = Graph().Sharings(PD);
684     for (subs2.Start(); subs2.More(); subs2.Next()) {
685       Handle(Standard_Transient) sub2 = subs2.Value();
686
687       if (sub2->IsKind(STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence))) {
688         Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO = 
689           Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast ( sub2 );
690         NAUO->SetRelatedProductDefinition(PDWAD);
691       }
692
693       if (sub2->IsKind(STANDARD_TYPE(StepAP203_CcDesignPersonAndOrganizationAssignment))) {
694         Handle(StepAP203_CcDesignPersonAndOrganizationAssignment) CDPAOA = 
695           Handle(StepAP203_CcDesignPersonAndOrganizationAssignment)::DownCast ( sub2 );
696         Handle(StepAP203_HArray1OfPersonOrganizationItem) HAPOI = CDPAOA->Items();
697         for(Standard_Integer i=1; i<=HAPOI->Length(); i++) {
698           StepAP203_PersonOrganizationItem POI = HAPOI->Value(i);
699           Handle(StepBasic_ProductDefinition) PDtmp = POI.ProductDefinition();
700           Standard_Integer numPDtmp = Model()->Number(PDtmp);
701           if(numProdDef==numPDtmp) {
702             POI.SetValue(PDWAD);
703             HAPOI->SetValue(i,POI);
704           }
705         }
706       }
707
708       if (sub2->IsKind(STANDARD_TYPE(StepAP203_CcDesignDateAndTimeAssignment))) {
709         Handle(StepAP203_CcDesignDateAndTimeAssignment) CDDATA = 
710           Handle(StepAP203_CcDesignDateAndTimeAssignment)::DownCast ( sub2 );
711         Handle(StepAP203_HArray1OfDateTimeItem) HADTI = CDDATA->Items();
712         for(Standard_Integer i=1; i<=HADTI->Length(); i++) {
713           StepAP203_DateTimeItem DTI = HADTI->Value(i);
714           Handle(StepBasic_ProductDefinition) PDtmp = DTI.ProductDefinition();
715           Standard_Integer numPDtmp = Model()->Number(PDtmp);
716           if(numProdDef==numPDtmp) {
717             DTI.SetValue(PDWAD);
718             HADTI->SetValue(i,DTI);
719           }
720         }
721       }
722
723       if (sub2->IsKind(STANDARD_TYPE(StepAP203_CcDesignApproval))) {
724         Handle(StepAP203_CcDesignApproval) CDA = 
725           Handle(StepAP203_CcDesignApproval)::DownCast ( sub2 );
726         Handle(StepAP203_HArray1OfApprovedItem) HAAI = CDA->Items();
727         for(Standard_Integer i=1; i<=HAAI->Length(); i++) {
728           StepAP203_ApprovedItem AI = HAAI->Value(i);
729           Handle(StepBasic_ProductDefinition) PDtmp = AI.ProductDefinition();
730           Standard_Integer numPDtmp = Model()->Number(PDtmp);
731           if(numProdDef==numPDtmp) {
732             AI.SetValue(PDWAD);
733             HAAI->SetValue(i,AI);
734           }
735         }
736       }
737     }
738 //      }
739 //      
740 //      StepRepr_CharacterizedDefinition ChartDef;
741 //      ChartDef.SetValue(PDWAD);
742 //      ProdDefSh->SetDefinition(ChartDef);
743 //    }
744              
745     myAEIAs.Append ( PDWAD );
746     myReplaceNum.Append(numProdDef);
747     myRoles.Append ( aDoc );
748     myTypes.Append ( aDocType );
749
750   }
751
752   return myAEIAs.Length();
753
754 }
755
756 //=======================================================================
757 //function : WriteExternRefs
758 //purpose  : 
759 //=======================================================================
760
761 Standard_Integer STEPConstruct_ExternRefs::WriteExternRefs (const Standard_Integer num) const
762 {
763   if(num==3) {
764     for ( Standard_Integer i=1; i <= myAEIAs.Length(); i++ ) {
765       Model()->ReplaceEntity(myReplaceNum(i),myAEIAs(i));
766       if ( ! myRoles(i).IsNull() ) 
767         Model()->AddWithRefs ( myRoles(i) );
768       if ( ! myTypes(i).IsNull() ) 
769         Model()->AddWithRefs ( myTypes(i) );
770     }
771   }
772   else {
773     for ( Standard_Integer i=1; i <= myAEIAs.Length(); i++ ) {
774       Model()->AddWithRefs ( myAEIAs(i) );
775       if ( ! myRoles(i).IsNull() ) 
776         Model()->AddWithRefs ( myRoles(i) );
777       if ( ! myFormats(i).IsNull() ) 
778         Model()->AddWithRefs ( myFormats(i) );
779       if ( ! myShapes(i).IsNull() ) 
780         Model()->AddWithRefs ( myShapes(i) );
781       if ( ! myTypes(i).IsNull() ) 
782         Model()->AddWithRefs ( myTypes(i) );
783     }
784   }
785   // PTV 30.01.2003 TRJ11
786   if ( !myAPD.IsNull() )
787     Model()->AddWithRefs( myAPD );
788   if ( !mySharedPRPC.IsNull() )
789     Model()->AddWithRefs( mySharedPRPC );
790   
791   return myAEIAs.Length();
792 }
793
794 //=======================================================================
795 //function : addAP214ExterRef
796 //purpose  : PTV 30.01.2003 TRJ11
797 //=======================================================================
798
799 Standard_Boolean STEPConstruct_ExternRefs::addAP214ExterRef (const Handle(StepAP214_AppliedDocumentReference)& ADR,
800                                                              const Handle(StepBasic_ProductDefinition)& PD,
801                                                              const Handle(StepBasic_DocumentFile)& DF,
802                                                              const Standard_CString filename )
803 {
804   Handle(StepAP214_HArray1OfDocumentReferenceItem) DRIs = new StepAP214_HArray1OfDocumentReferenceItem(1,1);
805   StepAP214_DocumentReferenceItem aDRI;
806   aDRI.SetValue(PD);
807   DRIs->SetValue(1, aDRI);
808   Handle(TCollection_HAsciiString) EmptyString = new TCollection_HAsciiString("");
809
810   // create/get created shared entities: 
811   // DocumentType, ProductDefinitionContext, ProductRelatedProductCategory, ProductContext
812   checkAP214Shared();
813   
814   // create document
815   Handle(StepBasic_Document) aDocument = new StepBasic_Document;
816   aDocument->Init( EmptyString, EmptyString, Standard_False, EmptyString, mySharedDocType );
817   ADR->Init(aDocument, EmptyString, DRIs);
818   
819   // create new product 
820   Handle(StepBasic_Product) Product = new StepBasic_Product;
821   Handle(StepBasic_HArray1OfProduct) HProducts = mySharedPRPC->Products();
822   Standard_Integer nbProducts = 0;
823   if (!HProducts.IsNull())
824     nbProducts = HProducts->Length();
825   Standard_Integer intProdId = 20001 + nbProducts;
826   Handle(TCollection_HAsciiString) ProductID = new TCollection_HAsciiString( intProdId );
827   Handle(TCollection_HAsciiString) ProductName = new TCollection_HAsciiString(filename);
828   ProductName->AssignCat( "-Doc" );
829   Handle(StepBasic_HArray1OfProductContext) aHProdContext = new StepBasic_HArray1OfProductContext(1, 1);
830   aHProdContext->SetValue( 1, mySharedPC );
831   Product->Init( ProductID, ProductName, EmptyString, aHProdContext );
832
833   // create new product definition formation
834   Handle(StepBasic_ProductDefinitionFormation) PDF = new StepBasic_ProductDefinitionFormation;
835   // name id taked from example Standard_ExtString_ref_master.stp
836   Handle(TCollection_HAsciiString) PDF_ID = new TCollection_HAsciiString("1");
837   PDF->Init( PDF_ID, EmptyString, Product );
838   
839   Handle(StepBasic_DocumentProductEquivalence) DPE = new StepBasic_DocumentProductEquivalence;
840   Handle(TCollection_HAsciiString) DPEname = new TCollection_HAsciiString("equivalence");
841   StepBasic_ProductOrFormationOrDefinition aPOFOD;
842   aPOFOD.SetValue( PDF );
843   DPE->Init( DPEname, Standard_False, EmptyString, aDocument, aPOFOD );
844   // add to the model with references
845   Model()->AddWithRefs( DPE );
846    
847   // add products to shared PRPC
848   Handle(StepBasic_HArray1OfProduct) newHProducts = new StepBasic_HArray1OfProduct(1, nbProducts + 1);
849   for (Standard_Integer pi = 1; pi <= nbProducts; pi++)
850     newHProducts->SetValue( pi, HProducts->Value( pi ) );
851   newHProducts->SetValue( nbProducts + 1, Product );
852   // set the hArray to the PRPC
853   mySharedPRPC->SetProducts( newHProducts );
854   
855   // create new PDWAD
856   Handle(StepBasic_ProductDefinitionWithAssociatedDocuments) PDWAD =
857     new StepBasic_ProductDefinitionWithAssociatedDocuments;
858   Handle(StepBasic_HArray1OfDocument) aDocIds = new StepBasic_HArray1OfDocument(1,1);
859   aDocIds->SetValue( 1, DF );
860   Handle(TCollection_HAsciiString) PDWAD_ID = new TCollection_HAsciiString("1");
861   PDWAD->Init( PDWAD_ID, EmptyString, PDF, mySharedPDC, aDocIds );
862   // add to the model with references
863   Model()->AddWithRefs( PDWAD );
864   
865   return Standard_True;
866 }
867
868 //=======================================================================
869 //function : SetAP214APD
870 //purpose  : 
871 //=======================================================================
872
873 void STEPConstruct_ExternRefs::SetAP214APD (const Handle(StepBasic_ApplicationProtocolDefinition)& APD)
874 {
875   myAPD = APD;
876 }
877
878 //=======================================================================
879 //function : GetAP214APD
880 //purpose  : 
881 //=======================================================================
882
883 Handle(StepBasic_ApplicationProtocolDefinition) STEPConstruct_ExternRefs::GetAP214APD()
884 {
885   if (myAPD.IsNull()) {
886     // create new APD with new Application Context
887     myAPD = new StepBasic_ApplicationProtocolDefinition;
888     // examples of the values taken from ext_ref_master.stp
889     Handle(TCollection_HAsciiString) status =
890       new TCollection_HAsciiString("version 1.1");
891     Handle(TCollection_HAsciiString) appSchemaName =
892       new TCollection_HAsciiString("pdm_schema");
893     Standard_Integer intProtocolYear = 1999;
894     Handle(StepBasic_ApplicationContext) aApplication = new StepBasic_ApplicationContext;
895     Handle(TCollection_HAsciiString) EmptyString = new TCollection_HAsciiString("");
896     aApplication->Init( EmptyString );
897     myAPD->Init( status, appSchemaName, intProtocolYear, aApplication );
898   }
899   return myAPD;
900 }
901
902 void STEPConstruct_ExternRefs::checkAP214Shared ()
903 {
904   Handle(TCollection_HAsciiString) EmptyString = new TCollection_HAsciiString("");
905   if ( mySharedPRPC.IsNull() ) {
906     // create new ProductRelatedProductCategory for all extern files.
907     Handle(TCollection_HAsciiString) PRPCname = new TCollection_HAsciiString("document");
908     mySharedPRPC = new StepBasic_ProductRelatedProductCategory;
909     mySharedPRPC->Init( PRPCname, Standard_False, EmptyString, 0 );
910   }
911   if ( mySharedDocType.IsNull() ) {
912     // create new shared Document Type
913     mySharedDocType = new StepBasic_DocumentType;
914     Handle(TCollection_HAsciiString) prod_dat_type =
915       new TCollection_HAsciiString("configuration controlled document version");
916     mySharedDocType->Init( prod_dat_type );
917   }
918   if ( mySharedPDC.IsNull() ) {
919     // create new shared Product Definition Context
920     mySharedPDC = new StepBasic_ProductDefinitionContext;
921     Handle(TCollection_HAsciiString) aPDCname =
922       new TCollection_HAsciiString("digital document definition");
923     Handle(StepBasic_ApplicationContext) anAppContext = GetAP214APD()->Application();
924     mySharedPDC->Init( aPDCname, anAppContext, EmptyString );
925   }
926   if ( mySharedPC.IsNull() ) {
927     // create new shared ProductContext
928     mySharedPC = new StepBasic_ProductContext;
929     Handle(StepBasic_ApplicationContext) anAppContext = GetAP214APD()->Application();
930     mySharedPC->Init( EmptyString, anAppContext, EmptyString );
931   }
932   
933 }