1 // Created on: 2000-08-17
2 // Created by: Andrey BETENEV
3 // Copyright (c) 2000-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <IGESCAFControl.hxx>
17 #include <IGESCAFControl_Writer.hxx>
18 #include <IGESData_IGESEntity.hxx>
19 #include <IGESData_IGESModel.hxx>
20 #include <IGESData_NameEntity.hxx>
21 #include <IGESGraph_Color.hxx>
22 #include <IGESGraph_DefinitionLevel.hxx>
23 #include <IGESSolid_Face.hxx>
24 #include <NCollection_DataMap.hxx>
25 #include <Standard_Transient.hxx>
26 #include <TCollection_AsciiString.hxx>
27 #include <TCollection_HAsciiString.hxx>
28 #include <TColStd_HSequenceOfExtendedString.hxx>
29 #include <TDataStd_Name.hxx>
30 #include <TDF_ChildIterator.hxx>
31 #include <TDF_Label.hxx>
32 #include <TDF_LabelSequence.hxx>
33 #include <TDocStd_Document.hxx>
35 #include <TopExp_Explorer.hxx>
37 #include <TopoDS_Face.hxx>
38 #include <TopoDS_Iterator.hxx>
39 #include <TopoDS_Shape.hxx>
40 #include <TopTools_SequenceOfShape.hxx>
41 #include <Transfer_FinderProcess.hxx>
42 #include <Transfer_TransientListBinder.hxx>
43 #include <TransferBRep.hxx>
44 #include <TransferBRep_ShapeMapper.hxx>
45 #include <XCAFDoc_ColorTool.hxx>
46 #include <XCAFDoc_DocumentTool.hxx>
47 #include <XCAFDoc_LayerTool.hxx>
48 #include <XCAFDoc_ShapeTool.hxx>
49 #include <XCAFPrs.hxx>
50 #include <XCAFPrs_Style.hxx>
51 #include <XSControl_WorkSession.hxx>
55 typedef NCollection_DataMap<TopoDS_Shape, TCollection_ExtendedString> DataMapOfShapeNames;
57 void CollectShapeNames (const TDF_Label& theLabel,
58 const TopLoc_Location& theLocation,
59 const Handle(TDataStd_Name)& thePrevName,
60 DataMapOfShapeNames& theMapOfShapeNames)
62 Standard_Boolean hasReferredShape = Standard_False;
63 Standard_Boolean hasComponents = Standard_False;
64 TDF_Label aReferredLabel;
66 Handle(TDataStd_Name) aName;
67 theLabel.FindAttribute (TDataStd_Name::GetID(), aName);
69 if ( XCAFDoc_ShapeTool::GetReferredShape ( theLabel, aReferredLabel ) )
71 TopLoc_Location aSubLocation = theLocation.Multiplied ( XCAFDoc_ShapeTool::GetLocation ( theLabel ) );
72 CollectShapeNames (aReferredLabel, aSubLocation, aName, theMapOfShapeNames);
73 hasReferredShape = Standard_True;
76 TDF_LabelSequence aSeq;
77 if (XCAFDoc_ShapeTool::GetComponents (theLabel, aSeq))
79 for (Standard_Integer anIter = 1; anIter <= aSeq.Length(); anIter++)
81 CollectShapeNames (aSeq.Value (anIter), theLocation, aName, theMapOfShapeNames );
83 hasComponents = Standard_True;
87 if (XCAFDoc_ShapeTool::GetSubShapes (theLabel, aSeq))
89 for (Standard_Integer anIter = 1; anIter <= aSeq.Length(); anIter++)
92 if (!XCAFDoc_ShapeTool::GetShape (aSeq.Value (anIter), aShape)) continue;
93 if (!aSeq.Value (anIter).FindAttribute (TDataStd_Name::GetID(), aName)) continue;
94 theMapOfShapeNames.Bind (aShape, aName->Get());
98 if (!hasReferredShape && !hasComponents)
101 if (!XCAFDoc_ShapeTool::GetShape (theLabel, aShape)) return;
102 aShape.Move (theLocation);
103 theMapOfShapeNames.Bind (aShape, thePrevName->Get());
108 //=======================================================================
109 //function : IGESCAFControl_Writer
111 //=======================================================================
113 IGESCAFControl_Writer::IGESCAFControl_Writer () :
114 myColorMode( Standard_True ),
115 myNameMode ( Standard_True ),
116 myLayerMode( Standard_True )
120 //=======================================================================
121 //function : IGESCAFControl_Writer
123 //=======================================================================
125 IGESCAFControl_Writer::IGESCAFControl_Writer (const Handle(XSControl_WorkSession)& WS,
126 const Standard_Boolean /*scratch*/ )
128 // this code does things in a wrong way, it should be vice-versa
129 WS->SetModel ( Model() );
130 WS->SetMapWriter ( TransferProcess() );
131 myColorMode = Standard_True;
132 myNameMode = Standard_True;
133 myLayerMode = Standard_True;
135 // SetWS (WS,scratch); // this should be the only required command here
138 //=======================================================================
139 //function : Transfer
141 //=======================================================================
143 Standard_Boolean IGESCAFControl_Writer::Transfer (const Handle(TDocStd_Document) &doc)
145 // translate free top-level shapes of the DECAF document
146 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
147 if ( STool.IsNull() ) return Standard_False;
149 TDF_LabelSequence labels;
150 STool->GetFreeShapes ( labels );
151 return Transfer (labels);
154 //=======================================================================
155 //function : Transfer
157 //=======================================================================
159 Standard_Boolean IGESCAFControl_Writer::Transfer (const TDF_Label& label)
161 TDF_LabelSequence labels;
162 labels.Append( label );
163 return Transfer( labels );
166 //=======================================================================
167 //function : Transfer
169 //=======================================================================
171 Standard_Boolean IGESCAFControl_Writer::Transfer (const TDF_LabelSequence& labels)
173 if ( labels.Length() <=0 ) return Standard_False;
174 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
175 TopoDS_Shape shape = XCAFDoc_ShapeTool::GetShape ( labels.Value(i) );
176 if ( ! shape.IsNull() )
178 // IGESControl_Writer::Transfer ( shape );
182 if ( GetColorMode() )
183 WriteAttributes ( labels );
186 if ( GetLayerMode() )
187 WriteLayers ( labels );
191 WriteNames( labels );
194 // WS()->ComputeGraph ( Standard_True );
197 return Standard_True;
200 //=======================================================================
203 //=======================================================================
205 Standard_Boolean IGESCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
206 const Standard_CString filename)
208 if ( ! Transfer ( doc ) ) return Standard_False;
209 return Write ( filename ) == IFSelect_RetDone;
212 //=======================================================================
215 //=======================================================================
217 Standard_Boolean IGESCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
218 const TCollection_AsciiString &filename)
220 if ( ! Transfer ( doc ) ) return Standard_False;
221 return Write ( filename.ToCString() ) == IFSelect_RetDone;
224 //=======================================================================
225 //function : WriteAttributes
227 //=======================================================================
229 Standard_Boolean IGESCAFControl_Writer::WriteAttributes (const TDF_LabelSequence& labels)
232 if ( labels.Length() <=0 ) return Standard_False;
233 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
234 TDF_Label L = labels.Value(i);
236 // collect color settings
237 XCAFPrs_DataMapOfShapeStyle settings;
239 XCAFPrs::CollectStyleSettings ( L, loc, settings );
240 if ( settings.Extent() <=0 ) continue;
242 // get a target shape and try to find corresponding context
243 // (all the colors set under that label will be put into that context)
245 if ( ! XCAFDoc_ShapeTool::GetShape ( L, S ) ) continue;
247 // iterate on subshapes and create IGES styles
248 XCAFPrs_DataMapOfStyleTransient colors;
249 TopTools_MapOfShape Map;
250 const XCAFPrs_Style inherit;
251 MakeColors ( S, settings, colors, Map, inherit );
254 return Standard_True;
257 //=======================================================================
258 //function : MakeColors
260 //=======================================================================
262 void IGESCAFControl_Writer::MakeColors (const TopoDS_Shape &S,
263 const XCAFPrs_DataMapOfShapeStyle &settings,
264 XCAFPrs_DataMapOfStyleTransient &colors,
265 TopTools_MapOfShape &Map,
266 const XCAFPrs_Style &inherit)
268 // skip already processed shapes
269 if ( ! Map.Add ( S ) ) return;
271 // check if shape has its own style (or inherits from ancestor)
272 XCAFPrs_Style style = inherit;
273 if ( settings.IsBound(S) ) {
274 XCAFPrs_Style own = settings.Find(S);
275 if ( own.IsSetColorCurv() ) style.SetColorCurv ( own.GetColorCurv() );
276 if ( own.IsSetColorSurf() ) style.SetColorSurf ( own.GetColorSurf() );
279 // analyze whether current entity should get a color
280 Standard_Boolean hasColor = Standard_False;
282 if ( S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SOLID) {
283 if ( style.IsSetColorSurf() ) {
284 hasColor = Standard_True;
285 col = style.GetColorSurf();
288 else if ( S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE ) {
289 if ( style.IsSetColorCurv() ) {
290 hasColor = Standard_True;
291 col = style.GetColorCurv();
295 // if color has to be assigned, try to do this
297 Handle(IGESGraph_Color) colent;
298 Standard_Integer rank = IGESCAFControl::EncodeColor ( col );
300 XCAFPrs_Style c; // style used as key in the map
301 c.SetColorSurf ( col );
302 if ( colors.IsBound ( c ) ) {
303 colent = Handle(IGESGraph_Color)::DownCast ( colors.Find(c) );
306 Handle(TCollection_HAsciiString) str =
307 new TCollection_HAsciiString ( col.StringName ( col.Name() ) );
308 colent = new IGESGraph_Color;
309 colent->Init ( col.Red() * 100., col.Green() * 100., col.Blue() * 100., str );
310 AddEntity ( colent );
311 colors.Bind ( c, colent );
314 Handle(Transfer_FinderProcess) FP = TransferProcess();
315 Handle(IGESData_IGESEntity) ent;
316 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, S );
317 if ( FP->FindTypedTransient ( mapper, STANDARD_TYPE(IGESData_IGESEntity), ent ) ) {
318 ent->InitColor ( colent, rank );
319 Handle(IGESSolid_Face) ent_f = Handle(IGESSolid_Face)::DownCast(ent);
322 if (!ent_f->Surface().IsNull())
323 ent_f->Surface()->InitColor ( colent, rank );
327 // may be S was splited during shape process
328 Handle(Transfer_Binder) bnd = FP->Find ( mapper );
329 if ( ! bnd.IsNull() ) {
330 Handle(Transfer_TransientListBinder) TransientListBinder =
331 //Handle(Transfer_TransientListBinder)::DownCast( bnd->Next(Standard_True) );
332 Handle(Transfer_TransientListBinder)::DownCast( bnd );
333 Standard_Integer i=0, nb=0;
334 if (! TransientListBinder.IsNull() ) {
335 nb = TransientListBinder->NbTransients();
336 for (i=1; i<=nb; i++) {
337 Handle(Standard_Transient) t = TransientListBinder->Transient(i);
338 ent = Handle(IGESData_IGESEntity)::DownCast(t);
341 ent->InitColor ( colent, rank );
342 Handle(IGESSolid_Face) ent_f = Handle(IGESSolid_Face)::DownCast(ent);
345 if (!ent_f->Surface().IsNull())
346 ent_f->Surface()->InitColor ( colent, rank );
351 /* // alternative: consider recursive mapping S -> compound -> entities
353 TopoDS_Shape comp = TransferBRep::ShapeResult(bnd);
354 if ( ! comp.IsNull() && comp.ShapeType() < S.ShapeType() )
355 for ( TopoDS_Iterator it(comp); it.More(); it.Next() ) {
356 MakeColors ( it.Value(), settings, colors, Map, style );
364 // iterate on subshapes (except vertices :)
365 if ( S.ShapeType() == TopAbs_EDGE ) return;
366 for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
367 MakeColors ( it.Value(), settings, colors, Map, style );
372 static void AttachLayer (const Handle(Transfer_FinderProcess) &FP,
373 const Handle(XCAFDoc_LayerTool)& LTool,
374 const TopoDS_Shape& aSh,
375 const Standard_Integer localIntName)
378 TopTools_SequenceOfShape shseq;
379 if ( aSh.ShapeType() == TopAbs_COMPOUND ) {
380 TopoDS_Iterator aShIt(aSh);
381 for ( ; aShIt.More(); aShIt.Next() ) {
382 TopoDS_Shape newSh = aShIt.Value();
383 Handle(TColStd_HSequenceOfExtendedString) shLayers = new TColStd_HSequenceOfExtendedString;
384 if (! LTool->GetLayers( newSh, shLayers) || newSh.ShapeType() == TopAbs_COMPOUND )
385 AttachLayer(FP, LTool, newSh, localIntName);
388 } else if ( aSh.ShapeType() == TopAbs_SOLID || aSh.ShapeType() == TopAbs_SHELL ) {
389 for (TopExp_Explorer exp(aSh,TopAbs_FACE) ; exp.More(); exp.Next()) {
390 TopoDS_Face entSh = TopoDS::Face (exp.Current());
397 for (Standard_Integer i = 1; i <= shseq.Length(); i++ ) {
398 TopoDS_Shape localShape = shseq.Value(i);
399 Handle(IGESData_IGESEntity) Igesent;
400 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, localShape );
401 if ( FP->FindTypedTransient ( mapper, STANDARD_TYPE(IGESData_IGESEntity), Igesent ) ) {
402 Igesent->InitLevel( 0, localIntName );
405 else cout << "Warning: Can't find entity for shape in mapper" << endl;
411 static void MakeLayers (const Handle(Transfer_FinderProcess) &FP,
412 const Handle(XCAFDoc_ShapeTool)& STool,
413 const Handle(XCAFDoc_LayerTool)& LTool,
414 const TDF_LabelSequence& aShapeLabels,
415 const Standard_Integer localIntName)
417 for ( Standard_Integer j = 1; j <= aShapeLabels.Length(); j++ ) {
418 TDF_Label aShapeLabel = aShapeLabels.Value(j);
420 if ( ! STool->GetShape ( aShapeLabel, aSh ) ) continue;
421 AttachLayer (FP, LTool, aSh, localIntName);
427 //=======================================================================
428 //function : WriteLayers
430 //=======================================================================
432 Standard_Boolean IGESCAFControl_Writer::WriteLayers (const TDF_LabelSequence& labels)
434 if ( labels.Length() <=0 ) return Standard_False;
435 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( labels(1) );
436 if ( STool.IsNull() ) return Standard_False;
437 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( labels(1) );
438 if ( LTool.IsNull() ) return Standard_False;
440 Standard_Integer globalIntName = 0;
441 TDF_LabelSequence aLayerLabels;
442 LTool->GetLayerLabels( aLayerLabels );
444 Handle(Transfer_FinderProcess) FP = TransferProcess();
445 for ( Standard_Integer i = 1; i <= aLayerLabels.Length(); i++ ){
446 TDF_Label aOneLayerL = aLayerLabels.Value(i);
447 if ( aOneLayerL.IsNull() ) continue;
448 TCollection_ExtendedString localName;
449 LTool->GetLayer(aOneLayerL, localName);
450 Standard_Integer localIntName = 0;
451 TCollection_AsciiString asciiName(localName,'?');
452 if (asciiName.IsIntegerValue() ) {
453 localIntName = asciiName.IntegerValue();
454 if (globalIntName < localIntName) globalIntName = localIntName;
456 TDF_LabelSequence aShapeLabels;
457 LTool->GetShapesOfLayer( aOneLayerL, aShapeLabels );
458 if ( aShapeLabels.Length() <= 0 ) continue;
459 MakeLayers(FP, STool, LTool, aShapeLabels, localIntName);
463 for ( Standard_Integer i1 = 1; i1 <= aLayerLabels.Length(); i1++ ) {
464 TDF_Label aOneLayerL = aLayerLabels.Value(i1);
465 if ( aOneLayerL.IsNull() ) continue;
466 TCollection_ExtendedString localName;
467 LTool->GetLayer(aOneLayerL, localName);
468 Standard_Integer localIntName = 0;
469 TCollection_AsciiString asciiName(localName,'?');
470 if (asciiName.IsIntegerValue() ) continue;
471 TDF_LabelSequence aShapeLabels;
472 LTool->GetShapesOfLayer( aOneLayerL, aShapeLabels );
473 if ( aShapeLabels.Length() <= 0 ) continue;
474 localIntName = ++globalIntName;
475 MakeLayers (FP, STool, LTool, aShapeLabels, localIntName);
478 return Standard_True;
482 //=======================================================================
483 //function : WriteNames
485 //=======================================================================
487 Standard_Boolean IGESCAFControl_Writer::WriteNames (const TDF_LabelSequence& theLabels)
489 if (theLabels.Length() <= 0) return Standard_False;
491 DataMapOfShapeNames aMapOfShapeNames;
493 for (Standard_Integer anIter = 1; anIter <= theLabels.Length(); anIter++ )
495 TDF_Label aLabel = theLabels.Value (anIter);
498 Handle(TDataStd_Name) aName;
499 if (!XCAFDoc_ShapeTool::GetShape (aLabel, aShape)) continue;
500 if (!aLabel.FindAttribute (TDataStd_Name::GetID(), aName)) continue;
502 aMapOfShapeNames.Bind (aShape, aName->Get());
504 // Collect names for subshapes
505 TopLoc_Location aLocation;
506 CollectShapeNames (aLabel, aLocation, aName, aMapOfShapeNames);
509 for (DataMapOfShapeNames::Iterator anIter (aMapOfShapeNames);
510 anIter.More(); anIter.Next())
512 const TopoDS_Shape& aShape = anIter.Key();
513 const TCollection_ExtendedString& aName = anIter.Value();
515 Handle(Transfer_FinderProcess) aFinderProcess = TransferProcess();
516 Handle(IGESData_IGESEntity) anIGESEntity;
517 Handle(TransferBRep_ShapeMapper) aShapeMapper = TransferBRep::ShapeMapper (aFinderProcess, aShape);
519 if (aFinderProcess->FindTypedTransient (aShapeMapper, STANDARD_TYPE(IGESData_IGESEntity), anIGESEntity))
521 Handle(TCollection_HAsciiString) anAsciiName = new TCollection_HAsciiString (" ");
522 Standard_Integer aNameLength = 8 - aName.Length();
523 if (aNameLength < 0) aNameLength = 0;
524 for (Standard_Integer aCharPos = 1; aNameLength < 8; aCharPos++, aNameLength++)
526 anAsciiName->SetValue (aNameLength+1, IsAnAscii (aName.Value (aCharPos)) ? (Standard_Character )aName.Value (aCharPos) : '?');
528 anIGESEntity->SetLabel (anAsciiName);
532 return Standard_True;
535 //=======================================================================
536 //function : SetColorMode
538 //=======================================================================
540 void IGESCAFControl_Writer::SetColorMode (const Standard_Boolean colormode)
542 myColorMode = colormode;
545 //=======================================================================
546 //function : GetColorMode
548 //=======================================================================
550 Standard_Boolean IGESCAFControl_Writer::GetColorMode () const
555 //=======================================================================
556 //function : SetNameMode
558 //=======================================================================
560 void IGESCAFControl_Writer::SetNameMode (const Standard_Boolean namemode)
562 myNameMode = namemode;
565 //=======================================================================
566 //function : GetNameMode
568 //=======================================================================
570 Standard_Boolean IGESCAFControl_Writer::GetNameMode () const
575 //=======================================================================
576 //function : SetLayerMode
578 //=======================================================================
580 void IGESCAFControl_Writer::SetLayerMode (const Standard_Boolean layermode)
582 myLayerMode = layermode;
585 //=======================================================================
586 //function : GetLayerMode
588 //=======================================================================
590 Standard_Boolean IGESCAFControl_Writer::GetLayerMode () const