summaryrefslogtreecommitdiff
path: root/src/STEPConstruct/STEPConstruct_ValidationProps.cxx
blob: 001ef6d72da0702ade46ff1350385512f03e6263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
// File:	STEPConstruct_ValidationProps.cxx
// Created:	Thu Sep  9 09:25:31 1999
// Author:	Andrey BETENEV
//		<abv@doomox.nnov.matra-dtv.fr>

#include <STEPConstruct_ValidationProps.ixx>
#include <STEPConstruct_UnitContext.hxx>

#include <Message.hxx>
#include <Message_Messenger.hxx>
#include <Interface_Macros.hxx>
#include <Interface_Static.hxx>
#include <Interface_EntityIterator.hxx>

#include <Transfer_Binder.hxx>
#include <Transfer_SimpleBinderOfTransient.hxx>
#include <TransferBRep.hxx>
#include <TransferBRep_ShapeMapper.hxx>

#include <XSControl_TransferWriter.hxx>
#include <XSControl_TransferReader.hxx>

#include <TopLoc_Location.hxx>
#include <TopoDS_Iterator.hxx>
#include <TCollection_HAsciiString.hxx>

#include <APIHeaderSection_MakeHeader.hxx>
#include <StepGeom_CartesianPoint.hxx>
#include <StepGeom_GeometricRepresentationItem.hxx>
#include <StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx.hxx>
#include <StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext.hxx>
#include <StepRepr_ShapeAspect.hxx>
#include <StepRepr_CharacterizedDefinition.hxx>
#include <StepRepr_PropertyDefinition.hxx>
#include <StepRepr_HArray1OfRepresentationItem.hxx>
#include <StepRepr_MeasureRepresentationItem.hxx>
#include <StepRepr_GlobalUnitAssignedContext.hxx>
#include <StepShape_ShapeRepresentation.hxx>
#include <StepShape_ShapeDefinitionRepresentation.hxx>
#include <StepBasic_MeasureValueMember.hxx>
#include <StepBasic_SiUnitAndLengthUnit.hxx>
#include <StepBasic_DerivedUnitElement.hxx>
#include <StepBasic_HArray1OfDerivedUnitElement.hxx>
#include <StepBasic_DerivedUnit.hxx>
#include <StepBasic_MeasureWithUnit.hxx>
#include <StepBasic_Unit.hxx>
#include <StepBasic_SiUnitAndAreaUnit.hxx>
#include <StepShape_ContextDependentShapeRepresentation.hxx>
#include <StepRepr_ShapeRepresentationRelationship.hxx>
#include <STEPConstruct.hxx>

//=======================================================================
//function : STEPConstruct_ValidationProps
//purpose  : 
//=======================================================================

STEPConstruct_ValidationProps::STEPConstruct_ValidationProps () 
{
}
     
//=======================================================================
//function : STEPConstruct_ValidationProps
//purpose  : 
//=======================================================================

STEPConstruct_ValidationProps::STEPConstruct_ValidationProps (const Handle(XSControl_WorkSession) &WS)
     : STEPConstruct_Tool(WS)
{
}

//=======================================================================
//function : Init
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::Init (const Handle(XSControl_WorkSession) &WS)
{
  return SetWS ( WS );
}

//=======================================================================
//function : TransientResult   CORRECTED
//purpose  : 
//=======================================================================

static Handle(Transfer_SimpleBinderOfTransient) TransientResult (const Handle(Standard_Transient)& res)
{
  Handle(Transfer_SimpleBinderOfTransient) binder;
  if (res.IsNull()) return binder;
  binder = new Transfer_SimpleBinderOfTransient;
  binder->SetResult (res);
  return binder;
}

//=======================================================================
//function : FindTarget
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::FindTarget (const TopoDS_Shape &Shape,
							    StepRepr_CharacterizedDefinition &target,
							    Handle(StepRepr_RepresentationContext) &Context,
							    const Standard_Boolean instance)
{
  // find the target STEP entity corresponding to a shape
  Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FinderProcess(), Shape );
  Handle(Transfer_Binder) binder = FinderProcess()->Find ( mapper );
  
#ifdef DEB
//  cout << "\n>========------" << endl;
//  cout << "Binders for " << Shape.TShape()->DynamicType()->Name() << " " << *(void**)&Shape.TShape() << ", Loc = " << *(void**)&Shape.Location() << endl;
//  cout << "mapper = " << *(void**)&mapper << ", binder = " << *(void**)&binder << endl;
//  DumpBinder ( binder );
#endif
  
  // if requested, try to find instance of assembly
  if ( instance ) {
/*
    Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO;
    Standard_Boolean found = myAssemblyPD.IsNull()?
      FinderProcess()->FindTypedTransient (mapper,STANDARD_TYPE(StepRepr_NextAssemblyUsageOccurrence), NAUO) :
	 STEPConstruct::FindNAUO (binder,myAssemblyPD,NAUO);
    if ( found ) {
      //skl find CDSR using NAUO:
      Handle(StepShape_ContextDependentShapeRepresentation) CDSR
      Interface_EntityIterator subs1 = graph.Sharings(NAUO);
      for (subs1.Start(); subs1.More(); subs1.Next()) {
        Handle(StepRepr_ProductDefinitionShape) PDS = 
          Handle(StepRepr_ProductDefinitionShape)::DownCast(subs1.Value());
        if(PDS.IsNull()) continue;
        //IsPDS=Standard_True;
        Interface_EntityIterator subs2 = graph.Sharings(PDS);
        for (subs2.Start(); CDSR.IsNull() && subs2.More(); subs2.Next()) {
          CDSR = Handle(StepShape_ContextDependentShapeRepresentation)::DownCast(subs2.Value());
        }
      }
      if(!CDSR.IsNull()) {
        target.SetValue ( CDSR->RepresentedProductRelation() );
        Context = CDSR->RepresentationRelation()->Rep2()->ContextOfItems();
      }
#ifdef DEB
      else cout << "INSTANCE: CDRS from NAUO NOT found" << endl;
#endif
    }
#ifdef DEB
    else cout << "INSTANCE: NAUO NOT found" << endl;
#endif
*/
  }

  // for Compounds, search for SDR
  else if ( Shape.ShapeType() == TopAbs_COMPOUND ) {
    Handle(StepBasic_ProductDefinition) ProdDef;
    if ( FinderProcess()->FindTypedTransient (mapper,STANDARD_TYPE(StepBasic_ProductDefinition), ProdDef) ) {
      Handle(StepRepr_ProductDefinitionShape) PDS;
      Interface_EntityIterator subs1 = Graph().Sharings(ProdDef);
      for (subs1.Start(); PDS.IsNull() && subs1.More(); subs1.Next()) {
        PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(subs1.Value());
      }
      target.SetValue ( PDS );
#ifdef DEB
//      cout << "COMPOUND: SDR found: " << sdr->DynamicType()->Name() << endl;
#endif
    }
    else {
#ifdef DEB
      cout << "COMPOUND: ProdDef NOT found" << endl;
#endif
      Handle(StepShape_ShapeRepresentation) SR;
      if(FinderProcess()->FindTypedTransient(mapper,STANDARD_TYPE(StepShape_ShapeRepresentation),SR)) {
        Handle(StepRepr_ProductDefinitionShape) PDS;
        Interface_EntityIterator subs1 = Graph().Sharings(SR);
        for (subs1.Start(); PDS.IsNull() && subs1.More(); subs1.Next()) {
          Handle(StepShape_ShapeDefinitionRepresentation) SDR =
            Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs1.Value());
          if(SDR.IsNull()) continue;
          PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(SDR->Definition().PropertyDefinition());
        }
        if(PDS.IsNull()) {
          subs1 = Graph().Sharings(SR);
          for (subs1.Start(); PDS.IsNull() && subs1.More(); subs1.Next()) {
            Handle(StepRepr_RepresentationRelationship) RR =
              Handle(StepRepr_RepresentationRelationship)::DownCast(subs1.Value());
            if(RR.IsNull()) continue;
            Handle(StepShape_ShapeRepresentation) SR1;
            if(RR->Rep1()==SR)
              SR1 = Handle(StepShape_ShapeRepresentation)::DownCast(RR->Rep2());
            else SR1 = Handle(StepShape_ShapeRepresentation)::DownCast(RR->Rep1());
            if(!SR1.IsNull()) {
              Interface_EntityIterator subs2 = Graph().Sharings(SR1);
              for (subs2.Start(); PDS.IsNull() && subs2.More(); subs2.Next()) {
                Handle(StepShape_ShapeDefinitionRepresentation) SDR =
                  Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(subs2.Value());
                if(SDR.IsNull()) continue;
                PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast(SDR->Definition().PropertyDefinition());
              }
            }
          }
        }
        if(!PDS.IsNull()) {
          target.SetValue(PDS);
          Context = SR->ContextOfItems();
        }
      }
    }
  }

  // for others, search for GEOMETRIC_REPRESENTATION_ITEM
  else {
    Handle(StepGeom_GeometricRepresentationItem) item;
    if ( FinderProcess()->FindTypedTransient (mapper,STANDARD_TYPE(StepGeom_GeometricRepresentationItem), item) ) {
#ifdef DEB
//      cout << Shape.TShape()->DynamicType()->Name() << ": GeomRepItem found: " << item->DynamicType()->Name() << endl;
#endif
      // find PDS (GRI <- SR <- SDR -> PDS)
      Handle(StepRepr_ProductDefinitionShape) PDS;
      Interface_EntityIterator subs = Graph().Sharings(item);
      for (subs.Start(); PDS.IsNull() && subs.More(); subs.Next()) {
#ifdef DEB
//	cout << "Parsing back refs: found " << subs.Value()->DynamicType()->Name() << endl;
#endif
        if ( ! subs.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeRepresentation)) ) continue;
        Handle(StepShape_ShapeRepresentation) sr = 
          Handle(StepShape_ShapeRepresentation)::DownCast ( subs.Value() );
        Context = sr->ContextOfItems();
        Interface_EntityIterator sub2 = Graph().Sharings(subs.Value());
	for (sub2.Start(); sub2.More(); sub2.Next()) {
          if ( ! sub2.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation)) ) continue;
          Handle(StepShape_ShapeDefinitionRepresentation) sdr = 
            Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(sub2.Value());
          PDS = Handle(StepRepr_ProductDefinitionShape)::DownCast ( sdr->Definition().PropertyDefinition() );
        }
      }
      if ( ! PDS.IsNull() ) {
        // find SHAPE_ASPECT or create it with all associated info if not yet exists
        Handle(StepRepr_ShapeAspect) aspect;
        Handle(Transfer_Binder) bbb = binder;
        while ( ! bbb.IsNull() ) {
          Handle(Transfer_SimpleBinderOfTransient) bx = 
            Handle(Transfer_SimpleBinderOfTransient)::DownCast ( bbb );
          if ( ! bx.IsNull() ) {
            Handle(StepRepr_ShapeAspect) asp = 
              Handle(StepRepr_ShapeAspect)::DownCast ( bx->Result() );
            if ( ! asp.IsNull() && asp->OfShape() == PDS ) {
              aspect = asp;
              break;
            }
	  }
          bbb = bbb->NextResult();
        }
        if ( aspect.IsNull() ) {
//	if ( ! FinderProcess()->FindTypedTransient (mapper,STANDARD_TYPE(StepRepr_ShapeAspect), aspect ) ||
//	     aspect->OfShape() != PDS )
#ifdef DEB
          cout << Shape.TShape()->DynamicType()->Name() << ": SHAPE_ASPECT NOT found, creating" << endl;
#endif
	  // create aspect and all related data
          Handle(TCollection_HAsciiString) AspectName = new TCollection_HAsciiString ( "" );
          Handle(TCollection_HAsciiString) AspectDescr = new TCollection_HAsciiString ( "" );
          aspect = new StepRepr_ShapeAspect;
          aspect->Init ( AspectName, AspectDescr, PDS, StepData_LFalse );
			
          StepRepr_CharacterizedDefinition SA;
          SA.SetValue ( aspect );
	  
          Handle(TCollection_HAsciiString) PropDefName = 
            new TCollection_HAsciiString ( "shape with specific properties" );
          Handle(TCollection_HAsciiString) PropDefDescr = new TCollection_HAsciiString ( "properties for subshape" );
          Handle(StepRepr_PropertyDefinition) propdef = new StepRepr_PropertyDefinition;
          propdef->Init ( PropDefName, Standard_True, PropDefDescr, SA );
	  	  
          Handle(TCollection_HAsciiString) SRName = new TCollection_HAsciiString ( "" );
          Handle(StepShape_ShapeRepresentation) SR = new StepShape_ShapeRepresentation;
          Handle(StepRepr_HArray1OfRepresentationItem) SRItems = new StepRepr_HArray1OfRepresentationItem ( 1, 1 );
          SRItems->SetValue ( 1, item );
          SR->Init ( SRName, SRItems, Context );
	  
          Handle(StepShape_ShapeDefinitionRepresentation) SDR = new StepShape_ShapeDefinitionRepresentation;
          StepRepr_RepresentedDefinition RD;
          RD.SetValue ( propdef );
          SDR->Init ( RD, SR );
	  
	  // record SHAPE_ASPECT in the map
          binder->AddResult ( TransientResult ( aspect ) );

	  // add SDR and all the data into model
          Model()->AddWithRefs ( SDR );
        }
	// SHAPE_ASPECT found, but we also need context: FIND IT !!!!
        else { 
#ifdef DEB
          cout << Shape.TShape()->DynamicType()->Name() << ": SHAPE_ASPECT found" << endl;
#endif
          Handle(StepRepr_ProductDefinitionShape) aPDS = aspect->OfShape();
          Interface_EntityIterator asubs = Graph().Sharings(aPDS);
          for (asubs.Start(); Context.IsNull() && asubs.More(); asubs.Next()) {
            if ( ! asubs.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeDefinitionRepresentation)) ) continue;
            Handle(StepShape_ShapeDefinitionRepresentation) sdr = 
              Handle(StepShape_ShapeDefinitionRepresentation)::DownCast(asubs.Value());
            Context = sdr->UsedRepresentation()->ContextOfItems();
          }
        }
  
        if ( ! aspect.IsNull() ) target.SetValue ( aspect );
      }
#ifdef DEB
      else  cout << Shape.TShape()->DynamicType()->Name() << ": PDS NOT found, fail to create SHAPE_ASPECT" << endl;
#endif
    }
#ifdef DEB
    else cout << Shape.TShape()->DynamicType()->Name() << ": GeomRepItem NOT found" << endl;
#endif
  }

  
  // if target not found and shape has location, try the same shape without location
  return ! target.IsNull();
}

//=======================================================================
//function : AddProp   CORRECTED
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::AddProp (const StepRepr_CharacterizedDefinition &target,
							 const Handle(StepRepr_RepresentationContext) &Context,
							 const Handle(StepRepr_RepresentationItem) &Prop,
							 const Standard_CString Descr)
{
  // FINALLY, create a structure of 5 entities describing a link between a shape and its property
  Handle(TCollection_HAsciiString) PropDefName = 
    new TCollection_HAsciiString ( "geometric_validation_property" );
  Handle(TCollection_HAsciiString) PropDefDescr = new TCollection_HAsciiString ( Descr );
  Handle(StepRepr_PropertyDefinition) propdef = new StepRepr_PropertyDefinition;
  propdef->Init ( PropDefName, Standard_True, PropDefDescr, target );
	  	  
  Handle(TCollection_HAsciiString) SRName = new TCollection_HAsciiString ( Descr );
  Handle(StepRepr_Representation) rep = new StepRepr_Representation;
  Handle(StepRepr_HArray1OfRepresentationItem) SRItems = new StepRepr_HArray1OfRepresentationItem ( 1, 1 );
  SRItems->SetValue ( 1, Prop );
  rep->Init ( SRName, SRItems, Context );
	  
  Handle(StepRepr_PropertyDefinitionRepresentation) PrDR = new StepRepr_PropertyDefinitionRepresentation;
  StepRepr_RepresentedDefinition RD;
  RD.SetValue ( propdef );
  PrDR->Init ( RD, rep );
	  
  // record SDR in order to have it written to the file
  Model()->AddWithRefs ( PrDR );

  // for AP203, add subschema name
  if ( Interface_Static::IVal("write.step.schema") ==3 ) {
    APIHeaderSection_MakeHeader mkHdr ( Handle(StepData_StepModel)::DownCast ( Model() ) );
    Handle(TCollection_HAsciiString) subSchema = 
      new TCollection_HAsciiString ( "GEOMETRIC_VALIDATION_PROPERTIES_MIM" );
    mkHdr.AddSchemaIdentifier ( subSchema );
  }
  
  return Standard_True;
}

//=======================================================================
//function : AddProp   CORRECTED
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::AddProp (const TopoDS_Shape &Shape,
							 const Handle(StepRepr_RepresentationItem) &Prop,
							 const Standard_CString Descr,
							 const Standard_Boolean instance)
{
  StepRepr_CharacterizedDefinition target;
  Handle(StepRepr_RepresentationContext) Context;
  if ( ! FindTarget ( Shape, target, Context, instance ) ) return Standard_False;
  return AddProp ( target, Context, Prop, Descr );
}

//=======================================================================
//function : AddVolume   CORRECTED
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::AddVolume (const TopoDS_Shape &Shape, 
							   const Standard_Real Vol)
{
  Handle(StepBasic_MeasureValueMember) Val = new StepBasic_MeasureValueMember;
  Val->SetReal ( Vol );
  //Val->SetName ( "solid volume" );
  Val->SetName ( "VOLUME_MEASURE");

  // for volume unit, either take existing or create a new
  if ( volUnit.DerivedUnit().IsNull() ) {
    Handle(StepBasic_SiUnitAndLengthUnit) MM = new StepBasic_SiUnitAndLengthUnit;
    MM->Init ( Standard_True, StepBasic_spMilli, StepBasic_sunMetre );
  
    Handle(StepBasic_DerivedUnitElement) DUE = new StepBasic_DerivedUnitElement;
    DUE->Init ( MM, 3. );
  
    Handle(StepBasic_HArray1OfDerivedUnitElement) DUElems = 
      new StepBasic_HArray1OfDerivedUnitElement ( 1, 1 );
    DUElems->SetValue ( 1, DUE );

    Handle(StepBasic_DerivedUnit) DU = new StepBasic_DerivedUnit;
    DU->Init ( DUElems );

    volUnit.SetValue ( DU );
  }
  
  Handle(TCollection_HAsciiString) MRIName = new TCollection_HAsciiString ( "volume measure" );
  Handle(StepRepr_MeasureRepresentationItem) MRI = new StepRepr_MeasureRepresentationItem;
  MRI->Init ( MRIName, Val, volUnit );
  
  return AddProp ( Shape, MRI, "volume" );
}

//=======================================================================
//function : AddArea   CORRECTED
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::AddArea (const TopoDS_Shape &Shape, 
							 const Standard_Real Area)
{
  Handle(StepBasic_MeasureValueMember) Val = new StepBasic_MeasureValueMember;
  Val->SetReal ( Area );
  //Val->SetName ( "surface area" );
  Val->SetName ( "AREA_MEASURE" );

  // for area unit, either take existing or create a new
  if ( areaUnit.DerivedUnit().IsNull() ) {
    Handle(StepBasic_SiUnitAndLengthUnit) MM = new StepBasic_SiUnitAndLengthUnit;
    MM->Init ( Standard_True, StepBasic_spMilli, StepBasic_sunMetre );
  
    Handle(StepBasic_DerivedUnitElement) DUE = new StepBasic_DerivedUnitElement;
    DUE->Init ( MM, 2. );
  
    Handle(StepBasic_HArray1OfDerivedUnitElement) DUElems = 
      new StepBasic_HArray1OfDerivedUnitElement ( 1, 1 );
    DUElems->SetValue ( 1, DUE );

    Handle(StepBasic_DerivedUnit) DU = new StepBasic_DerivedUnit;
    DU->Init ( DUElems );

    areaUnit.SetValue ( DU );
  }
  
  Handle(TCollection_HAsciiString) MRIName = new TCollection_HAsciiString ( "surface area measure" );
  Handle(StepRepr_MeasureRepresentationItem) MRI = new StepRepr_MeasureRepresentationItem;
  MRI->Init ( MRIName, Val, areaUnit );
  
  return AddProp ( Shape, MRI, "surface area" );
}

//=======================================================================
//function : AddCentroid   CORRECTED
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::AddCentroid (const TopoDS_Shape &Shape, 
							     const gp_Pnt &Pnt,
							     const Standard_Boolean instance)
{
  Handle(TCollection_HAsciiString) CPName = new TCollection_HAsciiString ( "centre point" );
  Handle(StepGeom_CartesianPoint) CP = new StepGeom_CartesianPoint;
  CP->Init3D ( CPName, Pnt.X(), Pnt.Y(), Pnt.Z() );
  
  return AddProp ( Shape, CP, "centroid", instance );
}

//=======================================================================
//function : LoadProps   CORRECTED
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::LoadProps (TColStd_SequenceOfTransient &seq) const
{
  // parse on PropertyDefinitionRepresentations
  Standard_Integer nb = Model()->NbEntities();
  Handle(Standard_Type) tPDR = STANDARD_TYPE(StepRepr_PropertyDefinitionRepresentation);
  for (Standard_Integer i = 1; i <= nb; i ++) {
    Handle(Standard_Transient) enti = Model()->Value(i);
    if ( ! enti->IsKind (tPDR) ) continue;
    
    Handle(StepRepr_PropertyDefinitionRepresentation) PDR = 
      Handle(StepRepr_PropertyDefinitionRepresentation)::DownCast ( enti );
    
    // check that PDR is for validation props
    Handle(StepRepr_PropertyDefinition) PD = PDR->Definition().PropertyDefinition();
    if ( PD.IsNull() || PD->Name().IsNull() 
         || PD->Name()->String() != "geometric_validation_property" ) 
      continue;

    seq.Append ( PDR );
  }
  return seq.Length() >0;
}
    
//=======================================================================
//function : GetPropPD   CORRECTED
//purpose  : 
//=======================================================================

Handle(StepBasic_ProductDefinition) STEPConstruct_ValidationProps::GetPropPD (const Handle(StepRepr_PropertyDefinition) &PD) const
{
  StepRepr_CharacterizedDefinition CD = PD->Definition();
    
  // detect target entity of valprop
  Handle(StepBasic_ProductDefinition) ProdDef;
  Handle(StepRepr_PropertyDefinition) PDS = CD.ProductDefinitionShape();
  if ( PDS.IsNull() ) {
    Handle(StepRepr_ShapeAspect) SA = CD.ShapeAspect();
    if ( SA.IsNull() ) {
#ifdef DEB
      Handle(Message_Messenger) sout = Message::DefaultMessenger();
      sout << "Error: Cannot find target entity (SA) for geometric_validation_property "; 
      Model()->PrintLabel ( PD, sout ); 
      sout << endl;
#endif
      return ProdDef;
    }
    Interface_EntityIterator subs = Graph().Sharings(SA);
    for (subs.Start(); subs.More(); subs.Next()) {
      PDS = Handle(StepRepr_PropertyDefinition)::DownCast ( subs.Value() );
      if ( PDS.IsNull() ) return ProdDef;
      Interface_EntityIterator subs1 = Graph().Shareds(PDS);
      for (subs1.Start(); ProdDef.IsNull() && subs1.More(); subs1.Next()) {
	ProdDef = Handle(StepBasic_ProductDefinition)::DownCast ( subs1.Value() );
      }
    }
  }
  else {
    Interface_EntityIterator subs = Graph().Shareds(PDS);
    for (subs.Start(); ProdDef.IsNull() && subs.More(); subs.Next()) {
      ProdDef = Handle(StepBasic_ProductDefinition)::DownCast ( subs.Value() );
    }
  }
#ifdef DEB
  if ( ProdDef.IsNull() ) {
    Handle(Message_Messenger) sout = Message::DefaultMessenger();
    sout << "Error: Cannot find target entity (SDR) for geometric_validation_property "; 
    Model()->PrintLabel ( PD, sout ); 
    sout << endl;
  }
#endif
  return ProdDef;
}

//=======================================================================
//function : GetPropCDSR   CORRECTED
//purpose  : 
//=======================================================================

Handle(StepRepr_NextAssemblyUsageOccurrence) STEPConstruct_ValidationProps::GetPropNAUO (const Handle(StepRepr_PropertyDefinition) &PD) const
{
  StepRepr_CharacterizedDefinition CD = PD->Definition();
    
  // detect target entity of valprop
  Handle(StepRepr_NextAssemblyUsageOccurrence) NAUO;
  Handle(StepRepr_PropertyDefinition) PDS = CD.ProductDefinitionShape();
  if ( PDS.IsNull() ) return NAUO; // not found
  Interface_EntityIterator subs = Graph().Shareds(PDS);
  for (subs.Start(); NAUO.IsNull() && subs.More(); subs.Next()) {
    NAUO = Handle(StepRepr_NextAssemblyUsageOccurrence)::DownCast ( subs.Value() );
  }
  return NAUO;
}

//=======================================================================
//function : GetPropShape   CORRECTED
//purpose  : 
//=======================================================================

TopoDS_Shape STEPConstruct_ValidationProps::GetPropShape (const Handle(StepBasic_ProductDefinition) &ProdDef) const
{
  // find target shape
  TopoDS_Shape S;
  Handle(Transfer_Binder) binder = TransientProcess()->Find(ProdDef);
  if ( ! binder.IsNull() && binder->HasResult() ) {
    S = TransferBRep::ShapeResult ( TransientProcess(), binder );
  }
  //if ( S.IsNull() ) { // for subshape (via shape_aspect)
  //  Handle(StepRepr_Representation) rep = SDR->UsedRepresentation();
  //  for ( Standard_Integer j=1; S.IsNull() && j <= rep->NbItems(); j++ ) {
  //    binder = TransientProcess()->Find(rep->ItemsValue(j));
  //    if ( ! binder.IsNull() && binder->HasResult() ) {
  //	S = TransferBRep::ShapeResult ( TransientProcess(), binder );
  //    }
  //  }
  //}
#ifdef DEB
  if ( S.IsNull() ) {
    Handle(Message_Messenger) sout = Message::DefaultMessenger();
    sout << "Warning: Entity "; 
    Model()->PrintLabel ( ProdDef, sout ); 
    sout << " is not mapped to shape" << endl;
  }
#endif
  return S;
}
    
//=======================================================================
//function : GetPropShape   CORRECTED
//purpose  : 
//=======================================================================

TopoDS_Shape STEPConstruct_ValidationProps::GetPropShape (const Handle(StepRepr_PropertyDefinition) &PD) const
{
  Handle(StepBasic_ProductDefinition) ProdDef = GetPropPD ( PD );
  TopoDS_Shape S;
  if ( ! ProdDef.IsNull() ) S = GetPropShape ( ProdDef );
  return S;
}

//=======================================================================
//function : GetPropReal
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::GetPropReal (const Handle(StepRepr_RepresentationItem) &item,
							     Standard_Real &Val, Standard_Boolean &isArea) const
{
  // decode volume & area
  if ( ! item->IsKind(STANDARD_TYPE(StepRepr_MeasureRepresentationItem)) ) 
    return Standard_False;
  
  Handle(StepRepr_MeasureRepresentationItem) mri = 
    Handle(StepRepr_MeasureRepresentationItem)::DownCast ( item );

  Handle(StepBasic_MeasureWithUnit) M = mri->Measure();
  TCollection_AsciiString Name = M->ValueComponentMember()->Name();
  StepBasic_Unit Unit = M->UnitComponent();

  Standard_Real scale = 1.;
  Handle(StepBasic_DerivedUnit) DU = Unit.DerivedUnit();
  if ( ! DU.IsNull() ) {
    for(Standard_Integer ind = 1; ind <= DU->NbElements(); ind++) {
      Handle(StepBasic_DerivedUnitElement) DUE = DU->ElementsValue(ind);
      Standard_Real exp = DUE->Exponent();
      Handle(StepBasic_NamedUnit) NU = DUE->Unit();
      STEPConstruct_UnitContext unit;
      unit.ComputeFactors(NU);
      if(unit.LengthDone()) {
	Standard_Real lengthFactor = unit.LengthFactor();
	scale *= pow(lengthFactor,exp);
      }
    }
  }
  else {
    Handle(StepBasic_NamedUnit) NU = Unit.NamedUnit();
    if(!NU.IsNull()) {
      STEPConstruct_UnitContext unit;
      unit.ComputeFactors(NU);
      if(unit.AreaDone())
	scale =  unit.AreaFactor();
      if(unit.VolumeDone())
	scale =  unit.VolumeFactor();
    }
  }
  
  Val = M->ValueComponent() * scale;
  
  if ( Name == "AREA_MEASURE" ) isArea = Standard_True;
  else if ( Name == "VOLUME_MEASURE" ) isArea = Standard_False; 
  else {
#ifdef DEB
    cout << "Warning: Measure " << (const void*) Model()->StringLabel ( M ) << " is neither area not volume" << endl;
#endif
    return Standard_False;
  }
  return Standard_True;
}

//=======================================================================
//function : GetPropPnt
//purpose  : 
//=======================================================================

Standard_Boolean STEPConstruct_ValidationProps::GetPropPnt (const Handle(StepRepr_RepresentationItem) &item,
							    const Handle(StepRepr_RepresentationContext) &Context,
							    gp_Pnt &Pnt) const
{
  // centroid
  if ( ! item->IsKind(STANDARD_TYPE(StepGeom_CartesianPoint)) ) 
    return Standard_False;
  
  Handle(StepGeom_CartesianPoint) P = Handle(StepGeom_CartesianPoint)::DownCast ( item );
  if ( P.IsNull() || P->NbCoordinates() != 3 ) {
#ifdef DEB
    cout << "Warning: Point " << (const void*) Model()->StringLabel ( P ) << " is not valid for centroid" << endl;
#endif
    return Standard_False;;
  }

  gp_Pnt pos ( P->CoordinatesValue(1), P->CoordinatesValue(2), P->CoordinatesValue(3) );
	
  // scale according to units
  if ( ! Context.IsNull() ) {
    Handle(StepRepr_GlobalUnitAssignedContext) theGUAC;
    if (Context->IsKind(STANDARD_TYPE(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext))) {
      DeclareAndCast(StepGeom_GeometricRepresentationContextAndGlobalUnitAssignedContext, theGRCAGAUC,Context);
      theGUAC = theGRCAGAUC->GlobalUnitAssignedContext();
    }
    else if (Context->IsKind(STANDARD_TYPE(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx))) {
      DeclareAndCast(StepGeom_GeomRepContextAndGlobUnitAssCtxAndGlobUncertaintyAssCtx,
		     theGRCAGAUC,Context);
      theGUAC = theGRCAGAUC->GlobalUnitAssignedContext();
    }
    if ( ! theGUAC.IsNull() ) {
      STEPConstruct_UnitContext UnitTool;
      UnitTool.ComputeFactors(theGUAC);
      gp_Pnt zero(0,0,0);
      pos.Scale ( zero, UnitTool.LengthFactor() );
    }
  }
  Pnt = pos;

  return Standard_True;
}

//=======================================================================
//function : SetAssemblyShape   CORRECTED
//purpose  : 
//=======================================================================

void STEPConstruct_ValidationProps::SetAssemblyShape (const TopoDS_Shape& shape)
{
  Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper(FinderProcess(),shape);
  FinderProcess()->FindTypedTransient(mapper,STANDARD_TYPE(StepBasic_ProductDefinition),myAssemblyPD);
}