0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / STEPConstruct / STEPConstruct_ContextTool.cxx
1 // Created on: 1993-09-20
2 // Created by: Martine LANGLOIS
3 // Copyright (c) 1993-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 // :j4 16.03.99 gka S4134
18 // abv 18.11.99 renamed from StepPDR_ContextTool
19 #include <STEPConstruct_ContextTool.ixx>
20 #include <StepData_StepModel.hxx>
21
22 #include <Interface_Macros.hxx>
23
24 #include <StepBasic_ProductDefinition.hxx>
25 //#include <StepBasic_ProductDefinitionContext.hxx>
26 #include <StepBasic_ProductDefinitionFormation.hxx>
27 #include <StepBasic_Product.hxx>
28 #include <StepBasic_ProductContext.hxx>
29 #include <StepBasic_HArray1OfProductContext.hxx>
30 #include <StepBasic_ApplicationContext.hxx>
31 #include <StepBasic_ApplicationProtocolDefinition.hxx>
32 #include <StepRepr_PropertyDefinition.hxx>
33
34 #include <StepBasic_HArray1OfProduct.hxx>
35 #include <StepBasic_ProductType.hxx> //:i3
36 #include <Interface_Static.hxx> //:j4
37 #include <GeomToStep_MakeAxis2Placement3d.hxx>
38 #include <StepBasic_ProductRelatedProductCategory.hxx>
39
40 #include "stdio.h"
41
42 //=======================================================================
43 //function : STEPConstruct_ContextTool
44 //purpose  :
45 //=======================================================================
46
47 STEPConstruct_ContextTool::STEPConstruct_ContextTool ()
48 {
49 }
50
51 //=======================================================================
52 //function : STEPConstruct_ContextTool
53 //purpose  :
54 //=======================================================================
55
56 STEPConstruct_ContextTool::STEPConstruct_ContextTool (const Handle(StepData_StepModel)& aStepModel)
57 {
58   SetModel(aStepModel);
59 }
60
61 //=======================================================================
62 //function : SetModel
63 //purpose  :
64 //=======================================================================
65
66 void STEPConstruct_ContextTool::SetModel (const Handle(StepData_StepModel)& aStepModel)
67 {
68   theAPD.Nullify();  //thePRPC.Nullify();
69
70   Standard_Integer i, nb = aStepModel->NbEntities();
71   for(i = 1; i<=nb && theAPD.IsNull(); i ++) {
72     Handle(Standard_Transient) ent = aStepModel->Value(i);
73     if (ent->IsKind(STANDARD_TYPE(StepBasic_ApplicationProtocolDefinition))) {
74       if (theAPD.IsNull())  theAPD  = GetCasted(StepBasic_ApplicationProtocolDefinition, ent);
75     }
76     //if (ent->IsKind(STANDARD_TYPE(StepBasic_ProductRelatedProductCategory))) {
77     //  if (thePRPC.IsNull()) thePRPC = GetCasted(StepBasic_ProductRelatedProductCategory, ent);
78     //}
79   }
80 }
81
82 //=======================================================================
83 //function : GetAPD
84 //purpose  :
85 //=======================================================================
86
87 Handle(StepBasic_ApplicationProtocolDefinition)  STEPConstruct_ContextTool::GetAPD()
88 {
89   return theAPD;
90 }
91
92 //=======================================================================
93 //function : AddAPD
94 //purpose  :
95 //=======================================================================
96
97 void STEPConstruct_ContextTool::AddAPD (const Standard_Boolean enforce)
98 {
99   Standard_Boolean noapd = theAPD.IsNull();
100   if (noapd || enforce) theAPD  = new StepBasic_ApplicationProtocolDefinition;
101
102   switch (Interface_Static::IVal("write.step.schema")) { //j4
103   default:
104   case 1:
105     theAPD->SetApplicationProtocolYear (1997);
106     theAPD->SetStatus (new TCollection_HAsciiString("committee draft"));
107     theAPD->SetApplicationInterpretedModelSchemaName
108       (new TCollection_HAsciiString("automotive_design"));
109     break;
110   case 2:
111     theAPD->SetApplicationProtocolYear (1998);
112     theAPD->SetStatus (new TCollection_HAsciiString("draft international standard"));
113     theAPD->SetApplicationInterpretedModelSchemaName
114       (new TCollection_HAsciiString("automotive_design"));
115     break;
116   case 3:
117     theAPD->SetApplicationProtocolYear (1994);
118     theAPD->SetStatus (new TCollection_HAsciiString("international standard"));
119     theAPD->SetApplicationInterpretedModelSchemaName
120       (new TCollection_HAsciiString("config_control_design"));
121     break;
122     
123   case 4: theAPD->SetApplicationProtocolYear (2000);
124     theAPD->SetStatus (new TCollection_HAsciiString("international standard"));
125     theAPD->SetApplicationInterpretedModelSchemaName
126       (new TCollection_HAsciiString("automotive_design"));
127     break;
128   }
129
130   if (theAPD->Application().IsNull())
131     theAPD->SetApplication (new StepBasic_ApplicationContext);
132   Handle(TCollection_HAsciiString) appl;
133   switch (Interface_Static::IVal("write.step.schema")) { //j4
134   default:
135   case 1:
136   case 2: appl = new TCollection_HAsciiString ( "core data for automotive mechanical design processes" );
137           break;
138   case 3: appl = new TCollection_HAsciiString ( "configuration controlled 3D designs of mechanical parts and assemblies" );
139           break;
140   }
141   theAPD->Application()->SetApplication ( appl );
142 //  if (noapd || enforce) aStepModel->AddWithRefs (theAPD);
143 }
144
145 //=======================================================================
146 //function : IsAP203
147 //purpose  : 
148 //=======================================================================
149
150 Standard_Boolean STEPConstruct_ContextTool::IsAP203 () const
151 {
152   if ( theAPD.IsNull() ) return Standard_False;
153   Handle(TCollection_HAsciiString) schema = theAPD->ApplicationInterpretedModelSchemaName();
154   if ( schema.IsNull() ) return Standard_False;
155   TCollection_AsciiString sch = schema->String();
156   sch.LowerCase();
157   return sch == "config_control_design";
158 }
159
160 //=======================================================================
161 //function : IsAP214
162 //purpose  : 
163 //=======================================================================
164
165 Standard_Boolean STEPConstruct_ContextTool::IsAP214 () const
166 {
167   if ( theAPD.IsNull() ) return Standard_False;
168   Handle(TCollection_HAsciiString) schema = theAPD->ApplicationInterpretedModelSchemaName();
169   if ( schema.IsNull() ) return Standard_False;
170   TCollection_AsciiString sch = schema->String();
171   sch.LowerCase();
172   return sch == "automotive_design";
173 }
174
175 // ================================================================
176 // Data Section : Basic Product Information (level S1)
177 //                   * Get methods
178 //                   * Set methods
179 // ================================================================
180
181 //=======================================================================
182 //function : GetACstatus
183 //purpose  :
184 //=======================================================================
185
186 Handle(TCollection_HAsciiString)  STEPConstruct_ContextTool::GetACstatus()
187 {
188   if (GetAPD().IsNull()) return new TCollection_HAsciiString("");
189   return GetAPD()->Status();
190 }
191
192 //=======================================================================
193 //function : GetACschemaName
194 //purpose  :
195 //=======================================================================
196
197 Handle(TCollection_HAsciiString)  STEPConstruct_ContextTool::GetACschemaName()
198 {
199   if (GetAPD().IsNull()) return new TCollection_HAsciiString("");
200   return  GetAPD()->ApplicationInterpretedModelSchemaName();
201 }
202
203 //=======================================================================
204 //function : GetACyear
205 //purpose  :
206 //=======================================================================
207
208 Standard_Integer  STEPConstruct_ContextTool::GetACyear()
209 {
210   return (GetAPD().IsNull() ? 1998 :
211           GetAPD()->ApplicationProtocolYear());
212 }
213
214 /*
215 //=======================================================================
216 //function : GetACapplication
217 //purpose  :
218 //=======================================================================
219
220 Handle(TCollection_HAsciiString)  STEPConstruct_ContextTool::GetACapplication()
221 {
222   return GetPDC()->Formation()->OfProduct()->FrameOfReferenceValue(1)
223     ->FrameOfReference()->Application();
224 }
225 */
226
227 //=======================================================================
228 //function : GetACname
229 //purpose  :
230 //=======================================================================
231
232 Handle(TCollection_HAsciiString)  STEPConstruct_ContextTool::GetACname()
233 {
234   if (GetAPD().IsNull()) return new TCollection_HAsciiString("");
235   if (GetAPD()->Application().IsNull()) return new TCollection_HAsciiString("");
236   return GetAPD()->Application()->Application();
237 }
238
239 //=======================================================================
240 //function : SetACstatus
241 //purpose  :
242 //=======================================================================
243
244 void STEPConstruct_ContextTool::SetACstatus (const Handle(TCollection_HAsciiString)& status)
245 {
246   if (GetAPD().IsNull()) return;
247   GetAPD()->SetStatus(status);
248 }
249
250 //=======================================================================
251 //function : SetACschemaName
252 //purpose  :
253 //=======================================================================
254
255 void STEPConstruct_ContextTool::SetACschemaName (const Handle(TCollection_HAsciiString)& schemaName)
256 {
257   if (GetAPD().IsNull()) return;
258   GetAPD()->SetApplicationInterpretedModelSchemaName(schemaName);
259 }
260
261 //=======================================================================
262 //function : SetACyear
263 //purpose  :
264 //=======================================================================
265
266 void STEPConstruct_ContextTool::SetACyear (const Standard_Integer year)
267 {
268   if (GetAPD().IsNull()) return;
269   GetAPD()->SetApplicationProtocolYear(year);
270 }
271
272 //=======================================================================
273 //function : SetACname
274 //purpose  :
275 //=======================================================================
276
277 void STEPConstruct_ContextTool::SetACname (const Handle(TCollection_HAsciiString)& name)
278 {
279   if (GetAPD().IsNull()) return;
280   if (GetAPD()->Application().IsNull()) GetAPD()->SetApplication
281     (new StepBasic_ApplicationContext);
282   GetAPD()->Application()->SetApplication (name);
283 }
284
285 /*
286 //=======================================================================
287 //function : SetACapplication
288 //purpose  :
289 //=======================================================================
290
291 void STEPConstruct_ContextTool::SetACapplication (const Handle(TCollection_HAsciiString)& application)
292 {
293   GetPDC()->Formation()->OfProduct()->FrameOfReferenceValue(1)
294     ->FrameOfReference()->SetApplication(application);
295
296 }
297 */
298
299 // --------------------------------
300 // Product Related Product Category
301 // --------------------------------
302
303 /*
304
305 //=======================================================================
306 //function : GetPRPC
307 //purpose  :
308 //=======================================================================
309
310 Handle(StepBasic_ProductRelatedProductCategory) STEPConstruct_ContextTool::GetPRPC()
311 {
312   return thePRPC;
313 }
314
315 //=======================================================================
316 //function : AddPRPC
317 //purpose  :
318 //=======================================================================
319
320 void STEPConstruct_ContextTool::AddPRPC (const Standard_Boolean enforce)
321 {
322   Standard_Boolean noprpc = thePRPC.IsNull();
323   if (noprpc || enforce) {
324     //:i3 abv 1 Sep 98: ProSTEP TR9: generate PRODUCT_TYPE (derived) instead of PRPC
325     switch (Interface_Static::IVal("write.step.schema")) { //j4
326     default:
327     case 1:
328       thePRPC = new StepBasic_ProductType;
329       thePRPC->SetName (new TCollection_HAsciiString("part"));
330       break;
331     case 4:
332     case 2:
333       thePRPC = new StepBasic_ProductRelatedProductCategory;
334       thePRPC->SetName (new TCollection_HAsciiString("part"));
335       break;
336     case 3:
337       thePRPC = new StepBasic_ProductRelatedProductCategory;
338       thePRPC->SetName (new TCollection_HAsciiString("detail")); // !!!!! or "assembly"
339       break;
340     }
341     thePRPC->UnSetDescription(); //:i3
342   }
343 //  if (noprpc || enforce) aStepModel->AddWithRefs(thePRPC);
344 }
345
346 //=======================================================================
347 //function : SetPRPCName
348 //purpose  :
349 //=======================================================================
350
351 void STEPConstruct_ContextTool::SetPRPCName(const Handle(TCollection_HAsciiString)& aName)
352 {
353   GetPRPC()->SetName(aName);
354 }
355
356 //=======================================================================
357 //function : GetPRPCName
358 //purpose  :
359 //=======================================================================
360
361 Handle(TCollection_HAsciiString)  STEPConstruct_ContextTool::GetPRPCName()
362 {
363   if (GetPRPC().IsNull()) return new TCollection_HAsciiString("");
364   return GetPRPC()->Name();
365 }
366
367 //=======================================================================
368 //function : SetPRPCDescription
369 //purpose  :
370 //=======================================================================
371
372 void STEPConstruct_ContextTool::SetPRPCDescription (const Handle(TCollection_HAsciiString)& aDescription)
373 {
374   Handle(StepBasic_ProductRelatedProductCategory) aPRPC = GetPRPC();
375   aPRPC->SetDescription(aDescription);
376 //  aPRPC->HasDescription(Standard_True);
377 }
378
379 //=======================================================================
380 //function : GetPRPCDescription
381 //purpose  :
382 //=======================================================================
383
384 Handle(TCollection_HAsciiString) STEPConstruct_ContextTool::GetPRPCDescription()
385 {
386   Handle(StepBasic_ProductRelatedProductCategory) aPRPC = GetPRPC();
387   if (!aPRPC.IsNull() && aPRPC->HasDescription())
388     return aPRPC->Description();
389   else {
390     return new TCollection_HAsciiString("");
391   }
392 }
393
394 // ================================================================
395 //
396 // Settings from an already done SDR (see SDRtool)
397 //
398 // ================================================================
399
400 void STEPConstruct_ContextTool::SetSDR (const Handle(StepShape_ShapeDefinitionRepresentation)& sdr)
401 {
402 //  SDR partage des choses avec le contexte ... On raccroche les wagons
403   Handle(StepBasic_Product) theProduct =
404     sdr->Definition()->Definition().ProductDefinition()->Formation()->OfProduct();
405   Handle(StepBasic_ApplicationContext) theAppli =
406     theProduct->FrameOfReferenceValue(1)->FrameOfReference();
407   Handle(StepBasic_HArray1OfProduct) ProdList =
408     new StepBasic_HArray1OfProduct(1,1);
409   ProdList->SetValue(1,theProduct);
410   thePRPC->SetProducts (ProdList);
411   theAPD->SetApplication (theAppli);
412 }
413 */
414
415 //=======================================================================
416 //function : GetDefaultAxis
417 //purpose  :
418 //=======================================================================
419
420 Handle(StepGeom_Axis2Placement3d) STEPConstruct_ContextTool::GetDefaultAxis ()
421 {
422   if ( myAxis.IsNull() ) {
423     GeomToStep_MakeAxis2Placement3d mkax;
424     myAxis = mkax.Value();
425   }
426   return myAxis;
427 }
428
429 //=======================================================================
430 //function : AP203Context
431 //purpose  :
432 //=======================================================================
433
434 STEPConstruct_AP203Context &STEPConstruct_ContextTool::AP203Context ()
435 {
436   return theAP203;
437 }
438
439 //=======================================================================
440 //function : Level
441 //purpose  :
442 //=======================================================================
443
444 Standard_Integer STEPConstruct_ContextTool::Level () const
445 {
446   return myLevel.Length();
447 }
448
449 //=======================================================================
450 //function : NextLevel
451 //purpose  :
452 //=======================================================================
453
454 void STEPConstruct_ContextTool::NextLevel ()
455 {
456   myLevel.Append ( 1 );
457 }
458
459 //=======================================================================
460 //function : PrevLevel
461 //purpose  :
462 //=======================================================================
463
464 void STEPConstruct_ContextTool::PrevLevel ()
465 {
466   if ( myLevel.Length() >0 ) myLevel.Remove ( myLevel.Length() );
467 }
468
469 //=======================================================================
470 //function : SetLevel
471 //purpose  :
472 //=======================================================================
473
474 void STEPConstruct_ContextTool::SetLevel (const Standard_Integer lev)
475 {
476   if ( lev < myLevel.Length() ) {
477     while ( lev < myLevel.Length() && myLevel.Length() >0 )
478       myLevel.Remove ( myLevel.Length() );
479   }
480   else {
481     while ( myLevel.Length() < lev ) myLevel.Append ( 1 );
482   }
483 }
484
485 //=======================================================================
486 //function : Index
487 //purpose  :
488 //=======================================================================
489
490 Standard_Integer STEPConstruct_ContextTool::Index () const
491 {
492   return ( myLevel.Length() >0 ? myLevel.Last() : 0 );
493 }
494
495 //=======================================================================
496 //function : NextIndex
497 //purpose  :
498 //=======================================================================
499
500 void STEPConstruct_ContextTool::NextIndex ()
501 {
502   if ( myLevel.Length() >0 )
503     myLevel.SetValue ( myLevel.Length(), myLevel.Last() + 1 );
504 }
505
506 //=======================================================================
507 //function : PrevIndex
508 //purpose  :
509 //=======================================================================
510
511 void STEPConstruct_ContextTool::PrevIndex ()
512 {
513   if ( myLevel.Length() >0 )
514     myLevel.SetValue ( myLevel.Length(), myLevel.Last() - 1 );
515 }
516
517 //=======================================================================
518 //function : SetIndex
519 //purpose  :
520 //=======================================================================
521
522 void STEPConstruct_ContextTool::SetIndex (const Standard_Integer ind)
523 {
524   if ( myLevel.Length() >0 )
525     myLevel.SetValue ( myLevel.Length(), ind );
526 }
527
528 //=======================================================================
529 //function : GetProductName
530 //purpose  :
531 //=======================================================================
532
533 Handle(TCollection_HAsciiString) STEPConstruct_ContextTool::GetProductName () const
534 {
535   Handle(TCollection_HAsciiString) PdtName;
536   if (Interface_Static::IsSet("write.step.product.name"))
537     PdtName = new TCollection_HAsciiString(Interface_Static::CVal("write.step.product.name"));
538   else PdtName = new TCollection_HAsciiString("Product");
539
540   for ( Standard_Integer i=1; i <= myLevel.Length(); i++ ) {
541     PdtName->AssignCat ((char*)( i >1 ? "." : " " ));
542     char buf[100];
543     sprintf ( buf, "%d", myLevel.Value(i) );
544     PdtName->AssignCat ( buf );
545   }
546
547   return PdtName;
548 }
549
550 //=======================================================================
551 //function : GetRootsForPart
552 //purpose  : 
553 //=======================================================================
554
555 Handle(TColStd_HSequenceOfTransient) STEPConstruct_ContextTool::GetRootsForPart (const STEPConstruct_Part &SDRTool)
556 {
557   Handle(TColStd_HSequenceOfTransient) seq = new TColStd_HSequenceOfTransient;
558
559   seq->Append ( SDRTool.SDRValue() );
560 //  seq->Append ( GetAPD() );
561
562   if ( ! SDRTool.PRPC().IsNull() ) seq->Append ( SDRTool.PRPC() );
563
564   // for AP203, add required product management data
565   if ( Interface_Static::IVal("write.step.schema") == 3 ) {
566     theAP203.Init ( SDRTool );
567     seq->Append (theAP203.GetProductCategoryRelationship());
568     seq->Append (theAP203.GetCreator());
569     seq->Append (theAP203.GetDesignOwner());
570     seq->Append (theAP203.GetDesignSupplier());
571     seq->Append (theAP203.GetClassificationOfficer());
572     seq->Append (theAP203.GetSecurity());
573     seq->Append (theAP203.GetCreationDate());
574     seq->Append (theAP203.GetClassificationDate());
575     seq->Append (theAP203.GetApproval());
576     seq->Append (theAP203.GetApprover());
577     seq->Append (theAP203.GetApprovalDateTime());
578   }
579
580   return seq;
581 }
582
583 //=======================================================================
584 //function : GetRootsForAssemblyLink
585 //purpose  : 
586 //=======================================================================
587
588 Handle(TColStd_HSequenceOfTransient) STEPConstruct_ContextTool::GetRootsForAssemblyLink (const STEPConstruct_Assembly &assembly)
589 {
590   Handle(TColStd_HSequenceOfTransient) seq = new TColStd_HSequenceOfTransient;
591
592   seq->Append ( assembly.ItemValue() );
593   
594   // for AP203, write required product management data
595   if ( Interface_Static::IVal("write.step.schema") == 3 ) {
596     theAP203.Init ( assembly.GetNAUO() );
597     seq->Append (theAP203.GetSecurity());
598     seq->Append (theAP203.GetClassificationOfficer());
599     seq->Append (theAP203.GetClassificationDate());
600     seq->Append (theAP203.GetApproval());
601     seq->Append (theAP203.GetApprover());
602     seq->Append (theAP203.GetApprovalDateTime());
603   }
604
605   return seq;
606 }
607
608
609
610
611