0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / STEPConstruct / STEPConstruct_Styles.cxx
1 // Created on: 1999-09-10
2 // Created by: Andrey BETENEV
3 // Copyright (c) 1999-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17
18 #include <APIHeaderSection_MakeHeader.hxx>
19 #include <Interface_EntityIterator.hxx>
20 #include <Interface_Static.hxx>
21 #include <Quantity_Color.hxx>
22 #include <StepBasic_MeasureValueMember.hxx>
23 #include <STEPConstruct.hxx>
24 #include <STEPConstruct_Styles.hxx>
25 #include <StepData_StepModel.hxx>
26 #include <StepGeom_GeometricRepresentationItem.hxx>
27 #include <StepRepr_ItemDefinedTransformation.hxx>
28 #include <StepRepr_ProductDefinitionShape.hxx>
29 #include <StepRepr_RepresentationContext.hxx>
30 #include <StepRepr_RepresentationItem.hxx>
31 #include <StepRepr_RepresentationRelationshipWithTransformation.hxx>
32 #include <StepRepr_ShapeRepresentationRelationship.hxx>
33 #include <StepRepr_Transformation.hxx>
34 #include <StepShape_ContextDependentShapeRepresentation.hxx>
35 #include <StepShape_ShapeDefinitionRepresentation.hxx>
36 #include <StepShape_ShapeRepresentation.hxx>
37 #include <StepVisual_ColourRgb.hxx>
38 #include <StepVisual_CurveStyle.hxx>
39 #include <StepVisual_DraughtingModel.hxx>
40 #include <StepVisual_DraughtingPreDefinedColour.hxx>
41 #include <StepVisual_DraughtingPreDefinedCurveFont.hxx>
42 #include <StepVisual_FillAreaStyle.hxx>
43 #include <StepVisual_FillAreaStyleColour.hxx>
44 #include <StepVisual_FillStyleSelect.hxx>
45 #include <StepVisual_HArray1OfFillStyleSelect.hxx>
46 #include <StepVisual_Invisibility.hxx>
47 #include <StepVisual_InvisibleItem.hxx>
48 #include <StepVisual_MechanicalDesignGeometricPresentationRepresentation.hxx>
49 #include <StepVisual_OverRidingStyledItem.hxx>
50 #include <StepVisual_PreDefinedColour.hxx>
51 #include <StepVisual_PreDefinedItem.hxx>
52 #include <StepVisual_PresentationStyleAssignment.hxx>
53 #include <StepVisual_PresentationStyleByContext.hxx>
54 #include <StepVisual_PresentationStyleSelect.hxx>
55 #include <StepVisual_StyledItem.hxx>
56 #include <StepVisual_SurfaceSideStyle.hxx>
57 #include <StepVisual_SurfaceStyleBoundary.hxx>
58 #include <StepVisual_SurfaceStyleElementSelect.hxx>
59 #include <StepVisual_SurfaceStyleFillArea.hxx>
60 #include <StepVisual_SurfaceStyleRenderingWithProperties.hxx>
61 #include <StepVisual_RenderingPropertiesSelect.hxx>
62 #include <StepVisual_SurfaceStyleTransparent.hxx>
63 #include <StepVisual_SurfaceStyleUsage.hxx>
64 #include <TCollection_HAsciiString.hxx>
65 #include <TColStd_HSequenceOfTransient.hxx>
66 #include <TransferBRep.hxx>
67 #include <TransferBRep_ShapeMapper.hxx>
68 #include <XSControl_TransferReader.hxx>
69 #include <XSControl_TransferWriter.hxx>
70 #include <XSControl_WorkSession.hxx>
71 #include <StepVisual_ContextDependentOverRidingStyledItem.hxx>
72 #include <StepShape_ShapeRepresentation.hxx>
73
74 //=======================================================================
75 //function : STEPConstruct_Styles
76 //purpose  : 
77 //=======================================================================
78 STEPConstruct_Styles::STEPConstruct_Styles () 
79 {
80 }
81      
82
83 //=======================================================================
84 //function : STEPConstruct_Styles
85 //purpose  : 
86 //=======================================================================
87
88 STEPConstruct_Styles::STEPConstruct_Styles (const Handle(XSControl_WorkSession) &WS)
89      : STEPConstruct_Tool ( WS )
90 {
91 }
92
93
94 //=======================================================================
95 //function : Init
96 //purpose  : 
97 //=======================================================================
98
99 Standard_Boolean STEPConstruct_Styles::Init (const Handle(XSControl_WorkSession) &WS)
100 {
101   myMapOfStyles.Clear();
102   myStyles.Clear();
103   myPSA.Clear();
104   return SetWS ( WS );
105 }
106
107
108 //=======================================================================
109 //function : NbStyles
110 //purpose  : 
111 //=======================================================================
112
113 Standard_Integer STEPConstruct_Styles::NbStyles () const
114 {
115   return myStyles.Extent();
116 }
117
118
119 //=======================================================================
120 //function : Style
121 //purpose  : 
122 //=======================================================================
123
124 Handle(StepVisual_StyledItem) STEPConstruct_Styles::Style (const Standard_Integer i) const
125 {
126   return Handle(StepVisual_StyledItem)::DownCast ( myStyles.FindKey(i) );
127 }
128
129 //=======================================================================
130 //function : NbRootStyles
131 //purpose  : 
132 //=======================================================================
133
134 Standard_Integer STEPConstruct_Styles::NbRootStyles () const
135 {
136   return myRootStyles.Extent();
137 }
138
139 //=======================================================================
140 //function : RootStyle
141 //purpose  : 
142 //=======================================================================
143
144 Handle(StepVisual_StyledItem) STEPConstruct_Styles::RootStyle (const Standard_Integer i) const
145 {
146   return Handle(StepVisual_StyledItem)::DownCast ( myRootStyles.FindKey(i) );
147 }
148
149
150 //=======================================================================
151 //function : ClearStyles
152 //purpose  : 
153 //=======================================================================
154
155 void STEPConstruct_Styles::ClearStyles () 
156 {
157   myStyles.Clear();
158   myPSA.Clear();
159   myRootStyles.Clear();
160 }
161
162
163 //=======================================================================
164 //function : AddStyle
165 //purpose  : 
166 //=======================================================================
167
168 void STEPConstruct_Styles::AddStyle (const Handle(StepVisual_StyledItem) &style)
169 {
170   myStyles.Add ( style );
171 }
172
173   
174 //=======================================================================
175 //function : AddStyle
176 //purpose  : 
177 //=======================================================================
178
179 Handle(StepVisual_StyledItem) STEPConstruct_Styles::AddStyle (const Handle(StepRepr_RepresentationItem) &item,
180                                                               const Handle(StepVisual_PresentationStyleAssignment) &PSA,
181                                                               const Handle(StepVisual_StyledItem) &Override)
182 {
183   Handle(StepVisual_StyledItem) Style;
184   
185   Handle(StepVisual_HArray1OfPresentationStyleAssignment) Styles = 
186     new StepVisual_HArray1OfPresentationStyleAssignment ( 1, 1 );
187   Styles->SetValue ( 1, PSA );
188   
189   if ( Override.IsNull() ) {
190     Handle(TCollection_HAsciiString) StyName = new TCollection_HAsciiString ( "color" );
191     Style = new StepVisual_StyledItem;
192     Style->Init ( StyName, Styles, item );
193   }
194   else {
195     Handle(TCollection_HAsciiString) StyName = new TCollection_HAsciiString ( "overriding color" );
196     Handle(StepVisual_OverRidingStyledItem) OStyle = new StepVisual_OverRidingStyledItem;
197     OStyle->Init ( StyName, Styles, item, Override );
198     Style = OStyle;
199   }
200   
201   myStyles.Add ( Style );
202   // for future using
203   myPSA.Append( PSA );
204   
205   return Style;
206 }
207
208
209 //=======================================================================
210 //function : AddStyle
211 //purpose  : 
212 //=======================================================================
213
214 Handle(StepVisual_StyledItem) STEPConstruct_Styles::AddStyle (const TopoDS_Shape &Shape,
215                                                               const Handle(StepVisual_PresentationStyleAssignment) &PSA,
216                                                               const Handle(StepVisual_StyledItem) &Override)
217 {
218   Handle(StepRepr_RepresentationItem) item = STEPConstruct::FindEntity ( FinderProcess(), Shape );
219   Handle(StepVisual_StyledItem) Style;
220   if ( ! item.IsNull() ) Style = AddStyle ( item, PSA, Override );
221   return Style;
222 }
223
224
225 //=======================================================================
226 //function : CreateMDGPR
227 //purpose  : 
228 //=======================================================================
229
230 Standard_Boolean STEPConstruct_Styles::CreateMDGPR (const Handle(StepRepr_RepresentationContext) &Context,
231                                                     Handle(StepVisual_MechanicalDesignGeometricPresentationRepresentation)& Repr,
232                                                     Handle(StepData_StepModel)& theStepModel)
233 {
234   if ( myStyles.Extent() <1 ) return Standard_False;
235   
236   // create MECHANICAL_DESIGN_GEOMETRIC_PRESENTATION_REPRESENTATION
237   Handle(StepRepr_HArray1OfRepresentationItem) elems = 
238     new StepRepr_HArray1OfRepresentationItem ( 1, myStyles.Extent() );
239   for ( Standard_Integer i=1; i <= myStyles.Extent(); i++ ) 
240     elems->SetValue ( i, Handle(StepRepr_RepresentationItem)::DownCast ( myStyles.FindKey(i) ) );
241   // create new MDGPR
242   Repr = new StepVisual_MechanicalDesignGeometricPresentationRepresentation;
243   Handle(TCollection_HAsciiString) ReprName = new TCollection_HAsciiString ( "" );
244   Repr->Init ( ReprName, elems, Context );
245
246   // record Repr in order to have it written to the file
247 //   Model()->AddWithRefs ( Repr ); add into the model upper
248
249   // for AP203, add subschema name
250   if ( theStepModel->InternalParameters.WriteSchema == 3 ) {
251     APIHeaderSection_MakeHeader mkHdr ( Handle(StepData_StepModel)::DownCast ( Model() ) );
252     Handle(TCollection_HAsciiString) subSchema = 
253       new TCollection_HAsciiString ( "SHAPE_APPEARANCE_LAYER_MIM" );
254     mkHdr.AddSchemaIdentifier ( subSchema );
255   }
256   
257   return Standard_True;
258 }
259
260
261 //=======================================================================
262 //function : CreateNAUOSRD
263 //purpose  : 
264 //=======================================================================
265
266 Standard_Boolean STEPConstruct_Styles::CreateNAUOSRD (const Handle(StepRepr_RepresentationContext) &Context,
267                                                       const Handle(StepShape_ContextDependentShapeRepresentation)& CDSR,
268                                                       const Handle(StepRepr_ProductDefinitionShape)& initPDS)
269 {
270   Handle(StepShape_ShapeDefinitionRepresentation) aSDR =
271     new StepShape_ShapeDefinitionRepresentation;
272   Handle(StepShape_ShapeRepresentation) aSR =
273     new StepShape_ShapeRepresentation;
274   Handle(TCollection_HAsciiString) ReprName = new TCollection_HAsciiString ( "" );
275   // element for axis 2 placement
276   Handle(StepRepr_HArray1OfRepresentationItem) elems = 
277     new StepRepr_HArray1OfRepresentationItem ( 1, 1 );
278   // get PDS
279   Handle(StepRepr_ProductDefinitionShape) aPDS;
280   if ( initPDS.IsNull() )
281     aPDS = CDSR->RepresentedProductRelation();
282   else
283     aPDS = initPDS; // for SHUO
284   Handle(StepRepr_ShapeRepresentationRelationship) aRepRelationShip = CDSR->RepresentationRelation();
285   Handle(StepRepr_RepresentationRelationshipWithTransformation) aRRwTRSF =
286     Handle(StepRepr_RepresentationRelationshipWithTransformation)::DownCast(aRepRelationShip);
287   StepRepr_Transformation SetReprTRSF;
288   if (!aRRwTRSF.IsNull())
289     SetReprTRSF = aRRwTRSF->TransformationOperator();
290   else
291     return Standard_False;
292   // take Item defined transformation
293   Handle(StepRepr_ItemDefinedTransformation) anItDT = SetReprTRSF.ItemDefinedTransformation();
294   elems->SetValue( 1, anItDT->TransformItem2() );
295   // init Shape representation.
296   aSR->Init ( ReprName, elems, Context );
297   // register reference between PresentationStyleByContext and ShapeRepresentation
298   for (Standard_Integer psbci = 1; psbci <= myPSA.Length(); psbci++) {
299     Handle(StepVisual_PresentationStyleByContext) PSA =
300       Handle(StepVisual_PresentationStyleByContext)::DownCast(myPSA.Value(psbci));
301     if (PSA.IsNull())
302       continue;
303     // register the reference
304     StepVisual_StyleContextSelect aStyleCntxSlct;
305     aStyleCntxSlct.SetValue( aSR );
306     PSA->SetStyleContext( aStyleCntxSlct );
307   }
308   
309   StepRepr_RepresentedDefinition aPDSselect;
310   aPDSselect.SetValue(aPDS);
311   aSDR->Init( aPDSselect, aSR );
312   Model()->AddWithRefs ( aSDR );
313   
314   return Standard_True;
315 }
316
317
318 //=======================================================================
319 //function : FindContext
320 //purpose  : 
321 //=======================================================================
322
323 Handle(StepRepr_RepresentationContext) STEPConstruct_Styles::FindContext (const TopoDS_Shape &Shape) const
324 {
325   // find context of items
326   Handle(StepRepr_RepresentationContext) Context;
327   Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FinderProcess(), Shape );
328   Handle(StepShape_ShapeRepresentation) sr;
329   if ( FinderProcess()->FindTypedTransient (mapper,STANDARD_TYPE(StepShape_ShapeRepresentation), sr) ) {
330 #ifdef OCCT_DEBUG
331 //    std::cout << "Context of " << Shape.TShape()->DynamicType()->Name() << ": SR found: " << sr->DynamicType()->Name() << std::endl;
332 #endif
333     Context = sr->ContextOfItems();
334   }
335   else {
336     Handle(StepGeom_GeometricRepresentationItem) item;
337     if ( FinderProcess()->FindTypedTransient (mapper,STANDARD_TYPE(StepGeom_GeometricRepresentationItem), item) ) {
338 #ifdef OCCT_DEBUG
339 //      std::cout << "Context of " << Shape.TShape()->DynamicType()->Name() << ": GeomRepItem found: " << item->DynamicType()->Name() << std::endl;
340 #endif
341       Interface_EntityIterator subs = Graph().Sharings(item);
342       for (subs.Start(); Context.IsNull() && subs.More(); subs.Next()) {
343 #ifdef OCCT_DEBUG
344 //      std::cout << "Parsing back refs: found " << subs.Value()->DynamicType()->Name() << std::endl;
345 #endif
346         if ( ! subs.Value()->IsKind(STANDARD_TYPE(StepShape_ShapeRepresentation)) ) continue;
347         sr = Handle(StepShape_ShapeRepresentation)::DownCast ( subs.Value() );
348         Context = sr->ContextOfItems();
349       }
350     }
351   }
352 #ifdef OCCT_DEBUG
353   if ( Context.IsNull() ) {
354     std::cout << Shape.TShape()->DynamicType()->Name() << ": Cannot find context" << std::endl;
355   }
356 #endif
357
358   return Context;
359 }
360
361
362 //=======================================================================
363 //function : LoadStyles
364 //purpose  : 
365 //=======================================================================
366
367 Standard_Boolean STEPConstruct_Styles::LoadStyles ()
368 {
369   myStyles.Clear();
370   myPSA.Clear();
371   myRootStyles.Clear();
372     
373   // find all MDGPRs and DMs and collect all defined styles in myStyles
374   Handle(Interface_InterfaceModel) model = Model();
375   Standard_Integer nb = model->NbEntities();
376   Handle(Standard_Type) tMDGPR = STANDARD_TYPE(StepVisual_MechanicalDesignGeometricPresentationRepresentation);
377   Handle(Standard_Type) tDM = STANDARD_TYPE(StepVisual_DraughtingModel);
378   Handle(Standard_Type) tSI = STANDARD_TYPE(StepVisual_StyledItem);
379   Handle(Standard_Type) tSR = STANDARD_TYPE(StepShape_ShapeRepresentation);
380   for (Standard_Integer i = 1; i <= nb; i ++)
381   {
382     Handle(Standard_Transient) enti = model->Value(i);
383     if ( enti->DynamicType() == tMDGPR || enti->DynamicType() == tDM )
384     {
385       Handle(StepRepr_Representation) container = Handle(StepRepr_Representation)::DownCast ( enti );
386
387       Standard_Integer nbi = container->NbItems();
388       for ( Standard_Integer j=1; j <= nbi; j++ )
389       {
390         Handle(StepVisual_StyledItem) style = 
391           Handle(StepVisual_StyledItem)::DownCast ( container->ItemsValue(j) );
392         if ( style.IsNull() ) continue;
393         auto anItem = style->ItemAP242 ().Value ();
394         if (!anItem.IsNull() && anItem->IsKind(tSR))
395         {
396           myRootStyles.Add (style);
397         }
398         else
399         {
400           myStyles.Add (style);
401         }
402       }
403     }
404     else if (enti->IsKind (STANDARD_TYPE(StepVisual_StyledItem)))
405     {
406       Handle(StepVisual_StyledItem) aStyledItem = Handle(StepVisual_StyledItem)::DownCast (enti);
407       auto anItem = aStyledItem->ItemAP242 ().Value ();
408       if (!anItem.IsNull() && anItem->IsKind(tSR) && !myRootStyles.Contains (aStyledItem))
409       {
410         myRootStyles.Add (aStyledItem);
411       }
412       else if (!myStyles.Contains (aStyledItem))
413       {
414         myStyles.Add (aStyledItem);
415       }
416     }
417   }
418   return !myStyles.IsEmpty();
419 }
420
421
422 //=======================================================================
423 //function : LoadInvisStyles
424 //purpose  : 
425 //=======================================================================
426
427 Standard_Boolean STEPConstruct_Styles::LoadInvisStyles (Handle(TColStd_HSequenceOfTransient)& theInvStyles) const
428 {
429   Handle(Interface_InterfaceModel) model = Model();
430   Standard_Integer nb = model->NbEntities();
431   Handle(Standard_Type) tInvisibility = STANDARD_TYPE(StepVisual_Invisibility);
432   // search for invisibility
433   for (Standard_Integer i = 1; i <= nb; i ++) {
434     Handle(Standard_Transient) enti = model->Value(i);
435     if ( enti->DynamicType() != tInvisibility )
436       continue;
437     // search for styled items
438     Handle(StepVisual_Invisibility) container = Handle(StepVisual_Invisibility)::DownCast ( enti );
439     Standard_Integer nbi = container->NbInvisibleItems();
440     for ( Standard_Integer j=1; j <= nbi; j++ ) {
441       StepVisual_InvisibleItem anInvItem = container->InvisibleItemsValue(j);
442       Handle(StepVisual_StyledItem) style = anInvItem.StyledItem();
443       if ( style.IsNull() ) 
444         continue;
445       // collect the invisible styled items
446       if ( theInvStyles.IsNull() )
447         theInvStyles = new TColStd_HSequenceOfTransient;
448       theInvStyles->Append( style );
449     }
450   }
451   return ( !theInvStyles.IsNull() && (theInvStyles->Length() > 0) );
452 }
453
454
455 //=======================================================================
456 //function : MakeColorPSA
457 //purpose  : 
458 //=======================================================================
459
460 Handle(StepVisual_PresentationStyleAssignment) STEPConstruct_Styles::MakeColorPSA (const Handle(StepRepr_RepresentationItem) &/*item*/,
461                                                                                    const Handle(StepVisual_Colour) &SurfCol, 
462                                                                                    const Handle(StepVisual_Colour) &CurveCol,
463                                                                                    const Handle(StepVisual_Colour) &RenderCol,
464                                                                                    const Standard_Real RenderTransp,
465                                                                                    const Standard_Boolean isForNAUO) const
466 {  
467   Handle(StepVisual_PresentationStyleAssignment) PSA;
468   TColStd_SequenceOfTransient items;
469   
470   // surface color
471   if ( ! SurfCol.IsNull() ) {
472     Handle(TCollection_HAsciiString) FASCName = new TCollection_HAsciiString ( "" );
473     Handle(StepVisual_FillAreaStyleColour) FASC = new StepVisual_FillAreaStyleColour;
474     FASC->Init ( FASCName, SurfCol );
475   
476     StepVisual_FillStyleSelect FSS;
477     FSS.SetValue ( FASC );
478   
479     Handle(StepVisual_HArray1OfFillStyleSelect) FASSs = new StepVisual_HArray1OfFillStyleSelect ( 1, 1 );
480     FASSs->SetValue ( 1, FSS );
481   
482     Handle(TCollection_HAsciiString) FASName = new TCollection_HAsciiString ( "" );
483     Handle(StepVisual_FillAreaStyle) FAS = new StepVisual_FillAreaStyle;
484     FAS->Init ( FASName, FASSs );
485
486     Handle(StepVisual_SurfaceStyleFillArea) SSFA = new StepVisual_SurfaceStyleFillArea;
487     SSFA->Init ( FAS );
488     
489     StepVisual_SurfaceStyleElementSelect SES;
490     SES.SetValue ( SSFA );
491   
492     Handle(StepVisual_HArray1OfSurfaceStyleElementSelect) SSESs;
493     if (RenderTransp == 0.0) {
494       SSESs = new StepVisual_HArray1OfSurfaceStyleElementSelect ( 1, 1 );
495     } else {
496         Handle(StepVisual_SurfaceStyleTransparent) SST = new StepVisual_SurfaceStyleTransparent;
497         SST->Init(RenderTransp);
498         StepVisual_RenderingPropertiesSelect RPS;
499         RPS.SetValue(SST);
500         Handle(StepVisual_HArray1OfRenderingPropertiesSelect) HARP = new
501                StepVisual_HArray1OfRenderingPropertiesSelect (1, 1);
502         HARP->SetValue(1, RPS);
503         Handle(StepVisual_SurfaceStyleRenderingWithProperties) SSRWP = new StepVisual_SurfaceStyleRenderingWithProperties;
504
505         SSRWP->Init(StepVisual_ssmNormalShading, RenderCol, HARP);
506
507         StepVisual_SurfaceStyleElementSelect SESR;
508         SESR.SetValue (SSRWP);
509
510         SSESs = new StepVisual_HArray1OfSurfaceStyleElementSelect ( 1, 2 );
511         SSESs->SetValue ( 2, SESR );
512     }
513     SSESs->SetValue ( 1, SES );
514
515     Handle(TCollection_HAsciiString) SSSName = new TCollection_HAsciiString ( "" );
516     Handle(StepVisual_SurfaceSideStyle) SSS = new StepVisual_SurfaceSideStyle;
517     SSS->Init ( SSSName, SSESs );
518   
519     Handle(StepVisual_SurfaceStyleUsage) SSU = new StepVisual_SurfaceStyleUsage;
520     SSU->Init ( StepVisual_ssBoth, SSS );
521     
522     items.Append ( SSU );
523   }
524   
525   // curve color
526   if ( ! CurveCol.IsNull() ) {
527     Handle(TCollection_HAsciiString) fontName = new TCollection_HAsciiString("continuous");
528     Handle(StepVisual_DraughtingPreDefinedCurveFont) SDPDCF = new StepVisual_DraughtingPreDefinedCurveFont;
529     SDPDCF->Init(fontName);
530   
531     Handle(StepBasic_MeasureValueMember) Val = new StepBasic_MeasureValueMember;
532     Val->SetReal ( 0.1 );
533     Val->SetName ( "POSITIVE_LENGTH_MEASURE");
534     
535     StepVisual_CurveStyleFontSelect SCSFS;
536     SCSFS.SetValue(SDPDCF);
537     
538     StepBasic_SizeSelect SSSelect;
539     SSSelect.SetValue(Val);
540   
541     Handle(TCollection_HAsciiString) SCSName = new TCollection_HAsciiString ( "" );
542     Handle(StepVisual_CurveStyle) SCS = new StepVisual_CurveStyle;
543     SCS->Init(SCSName,SCSFS,SSSelect,CurveCol);
544
545     items.Append ( SCS );
546   }
547   
548   if ( items.Length() <1 ) {
549 #ifdef OCCT_DEBUG
550     std::cout << "Error: no color is supplied" << std::endl;
551 #endif
552     return PSA;
553   }
554   
555   // general part
556   Handle(StepVisual_HArray1OfPresentationStyleSelect) PSSs = 
557     new StepVisual_HArray1OfPresentationStyleSelect ( 1, items.Length() );
558   for ( Standard_Integer i=1; i <= items.Length(); i++ ) {
559     StepVisual_PresentationStyleSelect PSS;
560     PSS.SetValue ( items.Value(i) );
561     PSSs->SetValue ( i, PSS );
562   }
563   if (!isForNAUO)
564     PSA = new StepVisual_PresentationStyleAssignment;
565   else 
566     PSA = new StepVisual_PresentationStyleByContext;
567   PSA->Init ( PSSs );
568   
569   return PSA;
570 }
571
572
573 //=======================================================================
574 //function : GetColorPSA
575 //purpose  : 
576 //=======================================================================
577
578 Handle(StepVisual_PresentationStyleAssignment) STEPConstruct_Styles::GetColorPSA (const Handle(StepRepr_RepresentationItem) &item,
579                                                                                   const Handle(StepVisual_Colour) &Col) 
580 {
581   // if this color already was processed, just use the same PSA, else create new and add it to map
582   Handle(StepVisual_PresentationStyleAssignment) PSA;
583   if ( myMapOfStyles.Contains(Col) ) {
584     PSA = Handle(StepVisual_PresentationStyleAssignment)::
585       DownCast(myMapOfStyles.FindFromKey(Col));
586   }
587   else {
588     PSA = MakeColorPSA ( item, Col, Col, Col, 0.0 );
589     myMapOfStyles.Add(Col,PSA);
590   }
591   return PSA;
592 }
593   
594
595 //=======================================================================
596 //function : GetColors
597 //purpose  : 
598 //=======================================================================
599
600 Standard_Boolean STEPConstruct_Styles::GetColors (const Handle(StepVisual_StyledItem) &style,
601                                                   Handle(StepVisual_Colour) &SurfCol,
602                                                   Handle(StepVisual_Colour) &BoundCol,
603                                                   Handle(StepVisual_Colour) &CurveCol,
604                                                   Handle(StepVisual_Colour) &RenderCol,
605                                                   Standard_Real& RenderTransp,
606                                                   Standard_Boolean& IsComponent) const
607 {
608   SurfCol.Nullify();
609   BoundCol.Nullify();
610   CurveCol.Nullify();
611   RenderCol.Nullify();
612     
613   // parse on styles
614   for(Standard_Integer j=1; j<=style->NbStyles(); j++ ) {
615     Handle(StepVisual_PresentationStyleAssignment) PSA = style->StylesValue ( j );
616     if(PSA.IsNull() || PSA->Styles().IsNull()) continue;
617     IsComponent = Standard_True;
618     
619     for(Standard_Integer k=1; k<=PSA->NbStyles(); k++ ) {
620       StepVisual_PresentationStyleSelect PSS = PSA->StylesValue(k);
621
622       // try surface_style_usage
623       Handle(StepVisual_SurfaceStyleUsage) SSU = PSS.SurfaceStyleUsage();
624       if( !SSU.IsNull() ) {
625         Handle(StepVisual_SurfaceSideStyle) SSS = SSU->Style();
626         for(Standard_Integer l=1; l<=SSS->NbStyles(); l++ ) {
627           StepVisual_SurfaceStyleElementSelect SSES = SSS->StylesValue(l);
628           // try fill color
629           Handle(StepVisual_SurfaceStyleFillArea) SSFA = SSES.SurfaceStyleFillArea();
630           if ( !SSFA.IsNull() ) {
631             Handle(StepVisual_FillAreaStyle) FAS = SSFA->FillArea();
632       if (FAS.IsNull())
633         continue;
634             for ( Standard_Integer m=1; m <= FAS->NbFillStyles(); m++ ) {
635               StepVisual_FillStyleSelect FSS = FAS->FillStylesValue ( m );
636               Handle(StepVisual_FillAreaStyleColour) FASC = FSS.FillAreaStyleColour();
637               if ( SurfCol.IsNull() || SSU->Side() != StepVisual_ssNegative ) //abv 30 Mar 00: trj3_s1-pe.stp
638                 SurfCol = FASC->FillColour();
639             }
640             continue;
641           }
642           // try boundary color
643           Handle(StepVisual_SurfaceStyleBoundary) SSB = SSES.SurfaceStyleBoundary();
644           if(!SSB.IsNull()) {
645             Handle(StepVisual_CurveStyle) CS = SSB->StyleOfBoundary();
646             if ( ! CS.IsNull() ) BoundCol = CS->CurveColour();
647             continue;
648           }
649       // try rendering color and transparency
650       Handle(StepVisual_SurfaceStyleRendering) SSR = SSES.SurfaceStyleRendering();
651       if (!SSR.IsNull()) {
652           RenderCol = SSR->SurfaceColour();
653           RenderTransp = 0.0;
654           Handle(StepVisual_SurfaceStyleRenderingWithProperties) SSRWP =
655               Handle(StepVisual_SurfaceStyleRenderingWithProperties)::DownCast(SSR);
656           if (!SSRWP.IsNull()) {
657               Handle(StepVisual_HArray1OfRenderingPropertiesSelect) HARP = SSRWP->Properties();
658               if (!HARP.IsNull())
659               {
660                   for (Standard_Integer aPropIndex = 1; aPropIndex <= HARP->Length(); ++aPropIndex) {
661                       Handle(StepVisual_SurfaceStyleTransparent) SST = HARP->Value(aPropIndex).SurfaceStyleTransparent();
662                       if (!SST.IsNull()) {
663                           RenderTransp = SST->Transparency();
664                       }
665                   }
666                }
667           }
668       }
669
670         }
671         continue;
672       }
673       
674       // try curve_style
675       Handle(StepVisual_CurveStyle) CS = PSS.CurveStyle();
676       if ( ! CS.IsNull() ) CurveCol = CS->CurveColour();
677     }
678   }
679   return ! SurfCol.IsNull() || ! BoundCol.IsNull() || ! CurveCol.IsNull() || ! RenderCol.IsNull();
680 }
681
682
683 //=======================================================================
684 //function : EncodeColor
685 //purpose  : 
686 //=======================================================================
687
688 Handle(StepVisual_Colour) STEPConstruct_Styles::EncodeColor(const Quantity_Color &C)
689 {
690   // detect if color corresponds to one of pre-defined colors
691   Standard_CString cName = 0;
692   if      ( C == Quantity_Color(Quantity_NOC_GREEN) )    cName = "green";
693   else if ( C == Quantity_Color(Quantity_NOC_RED) )      cName = "red";
694   else if ( C == Quantity_Color(Quantity_NOC_BLUE1) )    cName = "blue";
695   else if ( C == Quantity_Color(Quantity_NOC_YELLOW) )   cName = "yellow";
696   else if ( C == Quantity_Color(Quantity_NOC_MAGENTA1) ) cName = "magenta";
697   else if ( C == Quantity_Color(Quantity_NOC_CYAN1) )    cName = "cyan";
698   else if ( C == Quantity_Color(Quantity_NOC_BLACK) )    cName = "black";
699   else if ( C == Quantity_Color(Quantity_NOC_WHITE) )    cName = "white";
700   
701   if ( cName ) {
702     Handle(StepVisual_DraughtingPreDefinedColour) ColPr = new StepVisual_DraughtingPreDefinedColour;
703     Handle(StepVisual_PreDefinedItem) preDef = new StepVisual_PreDefinedItem;
704     preDef->Init(new TCollection_HAsciiString(cName));
705     ColPr->SetPreDefinedItem(preDef);
706     return ColPr;
707   }
708   else {
709     Handle(TCollection_HAsciiString) ColName = new TCollection_HAsciiString ( "" );
710     Handle(StepVisual_ColourRgb) ColRGB = new StepVisual_ColourRgb;
711     NCollection_Vec3<Standard_Real> aColor_sRGB;
712     C.Values (aColor_sRGB.r(), aColor_sRGB.g(), aColor_sRGB.b(), Quantity_TOC_sRGB);
713     ColRGB->Init ( ColName, aColor_sRGB.r(), aColor_sRGB.g(), aColor_sRGB.b() );
714     return ColRGB;
715   }
716 }
717
718
719 //=======================================================================
720 //function : EncodeColor
721 //purpose  : 
722 //=======================================================================
723
724 Handle(StepVisual_Colour) STEPConstruct_Styles::EncodeColor
725        (const Quantity_Color &C,
726         STEPConstruct_DataMapOfAsciiStringTransient &DPDCs,
727         STEPConstruct_DataMapOfPointTransient &ColRGBs)
728 {
729   // detect if color corresponds to one of pre-defined colors
730   Standard_CString cName = 0;
731   if      ( C == Quantity_Color(Quantity_NOC_GREEN) )    cName = "green";
732   else if ( C == Quantity_Color(Quantity_NOC_RED) )      cName = "red";
733   else if ( C == Quantity_Color(Quantity_NOC_BLUE1) )    cName = "blue";
734   else if ( C == Quantity_Color(Quantity_NOC_YELLOW) )   cName = "yellow";
735   else if ( C == Quantity_Color(Quantity_NOC_MAGENTA1) ) cName = "magenta";
736   else if ( C == Quantity_Color(Quantity_NOC_CYAN1) )    cName = "cyan";
737   else if ( C == Quantity_Color(Quantity_NOC_BLACK) )    cName = "black";
738   else if ( C == Quantity_Color(Quantity_NOC_WHITE) )    cName = "white";
739   
740   if ( cName ) {
741     Handle(StepVisual_DraughtingPreDefinedColour) ColPr;
742     TCollection_AsciiString aName(cName);
743     if(DPDCs.IsBound(aName)) {
744       ColPr = Handle(StepVisual_DraughtingPreDefinedColour)::DownCast(DPDCs.Find(aName));
745       if(!ColPr.IsNull()) return ColPr;
746     }
747     ColPr = new StepVisual_DraughtingPreDefinedColour;
748     Handle(StepVisual_PreDefinedItem) preDef = new StepVisual_PreDefinedItem;
749     preDef->Init(new TCollection_HAsciiString(cName));
750     ColPr->SetPreDefinedItem(preDef);
751     DPDCs.Bind(aName,ColPr);
752     return ColPr;
753   }
754   else {
755     Handle(StepVisual_ColourRgb) ColRGB;
756     gp_Pnt P;
757     C.Values (P.ChangeCoord().ChangeData()[0],
758               P.ChangeCoord().ChangeData()[1],
759               P.ChangeCoord().ChangeData()[2],
760               Quantity_TOC_sRGB);
761     if(ColRGBs.IsBound(P)) {
762       ColRGB = Handle(StepVisual_ColourRgb)::DownCast(ColRGBs.Find(P));
763       if(!ColRGB.IsNull()) return ColRGB;
764     }
765     Handle(TCollection_HAsciiString) ColName = new TCollection_HAsciiString ( "" );
766     ColRGB = new StepVisual_ColourRgb;
767     ColRGB->Init ( ColName, P.Coord (1), P.Coord (2), P.Coord (3) );
768     ColRGBs.Bind(P,ColRGB);
769     return ColRGB;
770   }
771 }
772
773
774 //=======================================================================
775 //function : DecodeColor
776 //purpose  : 
777 //=======================================================================
778
779 Standard_Boolean STEPConstruct_Styles::DecodeColor (const Handle(StepVisual_Colour) &Colour, Quantity_Color &Col) 
780 {
781   if ( Colour->IsKind (STANDARD_TYPE(StepVisual_ColourRgb)) ) {
782     Handle(StepVisual_ColourRgb) rgb = Handle(StepVisual_ColourRgb)::DownCast ( Colour );
783     if( rgb->Red()>1. || rgb->Green()>1. || rgb->Blue()>1. ) {
784       Standard_Real norm = rgb->Red();
785       if(norm<rgb->Green()) norm = rgb->Green();
786       if(norm<rgb->Blue()) norm = rgb->Blue();
787       Col.SetValues(rgb->Red()/norm, rgb->Green()/norm,
788                     rgb->Blue()/norm, Quantity_TOC_sRGB);
789     }
790     else
791       Col.SetValues(rgb->Red(), rgb->Green(), rgb->Blue(), Quantity_TOC_sRGB);
792     return Standard_True;
793   }
794   else if ( Colour->IsKind (STANDARD_TYPE(StepVisual_PreDefinedColour)) ) {
795     Handle(StepVisual_PreDefinedColour) pdc = 
796       Handle(StepVisual_PreDefinedColour)::DownCast ( Colour );
797     Handle(StepVisual_PreDefinedItem) pdi = pdc->GetPreDefinedItem();
798     const TCollection_AsciiString name = pdi->Name()->String();
799     if      ( name.IsEqual ( "red"     ) ) Col.SetValues ( Quantity_NOC_RED );
800     else if ( name.IsEqual ( "green"   ) ) Col.SetValues ( Quantity_NOC_GREEN );
801     else if ( name.IsEqual ( "blue"    ) ) Col.SetValues ( Quantity_NOC_BLUE1 );
802     else if ( name.IsEqual ( "yellow"  ) ) Col.SetValues ( Quantity_NOC_YELLOW );
803     else if ( name.IsEqual ( "magenta" ) ) Col.SetValues ( Quantity_NOC_MAGENTA1 );
804     else if ( name.IsEqual ( "cyan"    ) ) Col.SetValues ( Quantity_NOC_CYAN1 );
805     else if ( name.IsEqual ( "black"   ) ) Col.SetValues ( Quantity_NOC_BLACK );
806     else if ( name.IsEqual ( "white"   ) ) Col.SetValues ( Quantity_NOC_WHITE );
807     else {
808 #ifdef OCCT_DEBUG
809       std::cout << "Error: color name \"" << name << "\" is not recognized" << std::endl;
810 #endif
811       return Standard_False;
812     }
813     return Standard_True;
814   }
815   return Standard_False;
816 }
817