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 <IGESBasic_Name.hxx>
25 #include <Message_ProgressScope.hxx>
26 #include <NCollection_DataMap.hxx>
27 #include <Standard_Transient.hxx>
28 #include <TCollection_AsciiString.hxx>
29 #include <TCollection_HAsciiString.hxx>
30 #include <TCollection_HExtendedString.hxx>
31 #include <TColStd_HSequenceOfExtendedString.hxx>
32 #include <TDataStd_Name.hxx>
33 #include <TDF_ChildIterator.hxx>
34 #include <TDF_Label.hxx>
35 #include <TDF_LabelSequence.hxx>
36 #include <TDocStd_Document.hxx>
38 #include <TopExp_Explorer.hxx>
40 #include <TopoDS_Face.hxx>
41 #include <TopoDS_Iterator.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopTools_SequenceOfShape.hxx>
44 #include <Transfer_FinderProcess.hxx>
45 #include <Transfer_TransientListBinder.hxx>
46 #include <TransferBRep.hxx>
47 #include <TransferBRep_ShapeMapper.hxx>
48 #include <XCAFDoc_ColorTool.hxx>
49 #include <XCAFDoc_DocumentTool.hxx>
50 #include <XCAFDoc_LayerTool.hxx>
51 #include <XCAFDoc_ShapeTool.hxx>
52 #include <XCAFPrs.hxx>
53 #include <XCAFPrs_Style.hxx>
54 #include <XSControl_WorkSession.hxx>
58 typedef NCollection_DataMap<TopoDS_Shape, TCollection_ExtendedString> DataMapOfShapeNames;
60 void CollectShapeNames (const TDF_Label& theLabel,
61 const TopLoc_Location& theLocation,
62 const Handle(TDataStd_Name)& thePrevName,
63 DataMapOfShapeNames& theMapOfShapeNames)
65 Standard_Boolean hasReferredShape = Standard_False;
66 Standard_Boolean hasComponents = Standard_False;
67 TDF_Label aReferredLabel;
69 Handle(TDataStd_Name) aName;
70 theLabel.FindAttribute (TDataStd_Name::GetID(), aName);
72 if ( XCAFDoc_ShapeTool::GetReferredShape ( theLabel, aReferredLabel ) )
74 TopLoc_Location aSubLocation = theLocation.Multiplied ( XCAFDoc_ShapeTool::GetLocation ( theLabel ) );
75 CollectShapeNames (aReferredLabel, aSubLocation, aName, theMapOfShapeNames);
76 hasReferredShape = Standard_True;
79 TDF_LabelSequence aSeq;
80 if (XCAFDoc_ShapeTool::GetComponents (theLabel, aSeq))
82 for (Standard_Integer anIter = 1; anIter <= aSeq.Length(); anIter++)
84 CollectShapeNames (aSeq.Value (anIter), theLocation, aName, theMapOfShapeNames );
86 hasComponents = Standard_True;
90 if (XCAFDoc_ShapeTool::GetSubShapes (theLabel, aSeq))
92 for (Standard_Integer anIter = 1; anIter <= aSeq.Length(); anIter++)
95 if (!XCAFDoc_ShapeTool::GetShape (aSeq.Value (anIter), aShape)) continue;
96 if (!aSeq.Value (anIter).FindAttribute (TDataStd_Name::GetID(), aName)) continue;
97 theMapOfShapeNames.Bind (aShape, aName->Get());
101 if (!hasReferredShape && !hasComponents)
104 if (!XCAFDoc_ShapeTool::GetShape (theLabel, aShape)) return;
105 aShape.Move (theLocation);
106 theMapOfShapeNames.Bind (aShape, thePrevName->Get());
111 //=======================================================================
112 //function : IGESCAFControl_Writer
114 //=======================================================================
116 IGESCAFControl_Writer::IGESCAFControl_Writer () :
117 myColorMode( Standard_True ),
118 myNameMode ( Standard_True ),
119 myLayerMode( Standard_True )
123 //=======================================================================
124 //function : IGESCAFControl_Writer
126 //=======================================================================
128 IGESCAFControl_Writer::IGESCAFControl_Writer (const Handle(XSControl_WorkSession)& WS,
129 const Standard_Boolean /*scratch*/ )
131 // this code does things in a wrong way, it should be vice-versa
132 WS->SetModel ( Model() );
133 WS->SetMapWriter ( TransferProcess() );
134 myColorMode = Standard_True;
135 myNameMode = Standard_True;
136 myLayerMode = Standard_True;
138 // SetWS (WS,scratch); // this should be the only required command here
141 //=======================================================================
142 //function : Transfer
144 //=======================================================================
146 Standard_Boolean IGESCAFControl_Writer::Transfer (const Handle(TDocStd_Document) &doc,
147 const Message_ProgressRange& theProgress)
149 // translate free top-level shapes of the DECAF document
150 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
151 if ( STool.IsNull() ) return Standard_False;
153 TDF_LabelSequence labels;
154 STool->GetFreeShapes ( labels );
155 return Transfer (labels, theProgress);
158 //=======================================================================
159 //function : Transfer
161 //=======================================================================
163 Standard_Boolean IGESCAFControl_Writer::Transfer (const TDF_Label& label,
164 const Message_ProgressRange& theProgress)
166 TDF_LabelSequence labels;
167 labels.Append( label );
168 return Transfer( labels, theProgress );
171 //=======================================================================
172 //function : Transfer
174 //=======================================================================
176 Standard_Boolean IGESCAFControl_Writer::Transfer (const TDF_LabelSequence& labels,
177 const Message_ProgressRange& theProgress)
179 if ( labels.Length() <=0 ) return Standard_False;
180 Message_ProgressScope aPS(theProgress, "Labels", labels.Length());
181 for ( Standard_Integer i=1; i <= labels.Length() && aPS.More(); i++ )
183 TopoDS_Shape shape = XCAFDoc_ShapeTool::GetShape ( labels.Value(i) );
184 if ( ! shape.IsNull() )
185 AddShape (shape, aPS.Next());
186 // IGESControl_Writer::Transfer ( shape );
190 if ( GetColorMode() )
191 WriteAttributes ( labels );
194 if ( GetLayerMode() )
195 WriteLayers ( labels );
199 WriteNames( labels );
202 // WS()->ComputeGraph ( Standard_True );
205 return Standard_True;
208 //=======================================================================
211 //=======================================================================
213 Standard_Boolean IGESCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
214 const Standard_CString filename,
215 const Message_ProgressRange& theProgress)
217 if ( ! Transfer ( doc, theProgress ) ) return Standard_False;
218 return Write ( filename ) == IFSelect_RetDone;
221 //=======================================================================
224 //=======================================================================
226 Standard_Boolean IGESCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
227 const TCollection_AsciiString &filename,
228 const Message_ProgressRange& theProgress)
230 if ( ! Transfer ( doc, theProgress ) ) return Standard_False;
231 return Write ( filename.ToCString() ) == IFSelect_RetDone;
234 //=======================================================================
235 //function : WriteAttributes
237 //=======================================================================
239 Standard_Boolean IGESCAFControl_Writer::WriteAttributes (const TDF_LabelSequence& labels)
242 if ( labels.Length() <=0 ) return Standard_False;
243 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
244 TDF_Label L = labels.Value(i);
246 // collect color settings
247 XCAFPrs_IndexedDataMapOfShapeStyle settings;
249 XCAFPrs::CollectStyleSettings ( L, loc, settings );
250 if ( settings.Extent() <=0 ) continue;
252 // get a target shape and try to find corresponding context
253 // (all the colors set under that label will be put into that context)
255 if ( ! XCAFDoc_ShapeTool::GetShape ( L, S ) ) continue;
257 // iterate on subshapes and create IGES styles
258 XCAFPrs_DataMapOfStyleTransient colors;
259 TopTools_MapOfShape Map;
260 const XCAFPrs_Style inherit;
261 MakeColors ( S, settings, colors, Map, inherit );
264 return Standard_True;
267 //=======================================================================
268 //function : MakeColors
270 //=======================================================================
272 void IGESCAFControl_Writer::MakeColors (const TopoDS_Shape &S,
273 const XCAFPrs_IndexedDataMapOfShapeStyle &settings,
274 XCAFPrs_DataMapOfStyleTransient &colors,
275 TopTools_MapOfShape &Map,
276 const XCAFPrs_Style &inherit)
278 // skip already processed shapes
279 if ( ! Map.Add ( S ) ) return;
281 // check if shape has its own style (or inherits from ancestor)
282 XCAFPrs_Style style = inherit;
283 if ( settings.Contains(S) ) {
284 XCAFPrs_Style own = settings.FindFromKey(S);
285 if ( own.IsSetColorCurv() ) style.SetColorCurv ( own.GetColorCurv() );
286 if ( own.IsSetColorSurf() ) style.SetColorSurf ( own.GetColorSurf() );
287 style.SetMaterial (own.Material());
290 // analyze whether current entity should get a color
291 Standard_Boolean hasColor = Standard_False;
293 if ( S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SOLID) {
294 if ( style.IsSetColorSurf() ) {
295 hasColor = Standard_True;
296 col = style.GetColorSurf();
298 else if (!style.Material().IsNull()
299 && !style.Material()->IsEmpty())
301 hasColor = Standard_True;
302 col = style.Material()->BaseColor().GetRGB();
305 else if ( S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE ) {
306 if ( style.IsSetColorCurv() ) {
307 hasColor = Standard_True;
308 col = style.GetColorCurv();
312 // if color has to be assigned, try to do this
314 Handle(IGESGraph_Color) colent;
315 Standard_Integer rank = IGESCAFControl::EncodeColor ( col );
317 XCAFPrs_Style c; // style used as key in the map
318 c.SetColorSurf ( col );
319 if ( colors.IsBound ( c ) ) {
320 colent = Handle(IGESGraph_Color)::DownCast ( colors.Find(c) );
323 Handle(TCollection_HAsciiString) str =
324 new TCollection_HAsciiString ( col.StringName ( col.Name() ) );
325 colent = new IGESGraph_Color;
326 NCollection_Vec3<Standard_Real> aColor_sRGB;
327 col.Values (aColor_sRGB.r(), aColor_sRGB.g(), aColor_sRGB.b(), Quantity_TOC_sRGB);
328 colent->Init ( aColor_sRGB.r() * 100., aColor_sRGB.g() * 100., aColor_sRGB.b() * 100., str );
329 AddEntity ( colent );
330 colors.Bind ( c, colent );
333 Handle(Transfer_FinderProcess) FP = TransferProcess();
334 Handle(IGESData_IGESEntity) ent;
335 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, S );
336 if ( FP->FindTypedTransient ( mapper, STANDARD_TYPE(IGESData_IGESEntity), ent ) ) {
337 ent->InitColor ( colent, rank );
338 Handle(IGESSolid_Face) ent_f = Handle(IGESSolid_Face)::DownCast(ent);
341 if (!ent_f->Surface().IsNull())
342 ent_f->Surface()->InitColor ( colent, rank );
346 // may be S was splited during shape process
347 Handle(Transfer_Binder) bnd = FP->Find ( mapper );
348 if ( ! bnd.IsNull() ) {
349 Handle(Transfer_TransientListBinder) TransientListBinder =
350 //Handle(Transfer_TransientListBinder)::DownCast( bnd->Next(Standard_True) );
351 Handle(Transfer_TransientListBinder)::DownCast( bnd );
352 Standard_Integer i=0, nb=0;
353 if (! TransientListBinder.IsNull() ) {
354 nb = TransientListBinder->NbTransients();
355 for (i=1; i<=nb; i++) {
356 Handle(Standard_Transient) t = TransientListBinder->Transient(i);
357 ent = Handle(IGESData_IGESEntity)::DownCast(t);
360 ent->InitColor ( colent, rank );
361 Handle(IGESSolid_Face) ent_f = Handle(IGESSolid_Face)::DownCast(ent);
364 if (!ent_f->Surface().IsNull())
365 ent_f->Surface()->InitColor ( colent, rank );
370 /* // alternative: consider recursive mapping S -> compound -> entities
372 TopoDS_Shape comp = TransferBRep::ShapeResult(bnd);
373 if ( ! comp.IsNull() && comp.ShapeType() < S.ShapeType() )
374 for ( TopoDS_Iterator it(comp); it.More(); it.Next() ) {
375 MakeColors ( it.Value(), settings, colors, Map, style );
383 // iterate on subshapes (except vertices :)
384 if ( S.ShapeType() == TopAbs_EDGE ) return;
385 for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
386 MakeColors ( it.Value(), settings, colors, Map, style );
391 static void AttachLayer (const Handle(Transfer_FinderProcess) &FP,
392 const Handle(XCAFDoc_LayerTool)& LTool,
393 const TopoDS_Shape& aSh,
394 const Standard_Integer localIntName)
397 TopTools_SequenceOfShape shseq;
398 if ( aSh.ShapeType() == TopAbs_COMPOUND ) {
399 TopoDS_Iterator aShIt(aSh);
400 for ( ; aShIt.More(); aShIt.Next() ) {
401 TopoDS_Shape newSh = aShIt.Value();
402 Handle(TColStd_HSequenceOfExtendedString) shLayers = new TColStd_HSequenceOfExtendedString;
403 if (! LTool->GetLayers( newSh, shLayers) || newSh.ShapeType() == TopAbs_COMPOUND )
404 AttachLayer(FP, LTool, newSh, localIntName);
407 } else if ( aSh.ShapeType() == TopAbs_SOLID || aSh.ShapeType() == TopAbs_SHELL ) {
408 for (TopExp_Explorer exp(aSh,TopAbs_FACE) ; exp.More(); exp.Next()) {
409 TopoDS_Face entSh = TopoDS::Face (exp.Current());
416 for (Standard_Integer i = 1; i <= shseq.Length(); i++ ) {
417 TopoDS_Shape localShape = shseq.Value(i);
418 Handle(IGESData_IGESEntity) Igesent;
419 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, localShape );
420 if ( FP->FindTypedTransient ( mapper, STANDARD_TYPE(IGESData_IGESEntity), Igesent ) ) {
421 Igesent->InitLevel( 0, localIntName );
424 else std::cout << "Warning: Can't find entity for shape in mapper" << std::endl;
430 static void MakeLayers (const Handle(Transfer_FinderProcess) &FP,
431 const Handle(XCAFDoc_ShapeTool)& STool,
432 const Handle(XCAFDoc_LayerTool)& LTool,
433 const TDF_LabelSequence& aShapeLabels,
434 const Standard_Integer localIntName)
436 for ( Standard_Integer j = 1; j <= aShapeLabels.Length(); j++ ) {
437 TDF_Label aShapeLabel = aShapeLabels.Value(j);
439 if ( ! STool->GetShape ( aShapeLabel, aSh ) ) continue;
440 AttachLayer (FP, LTool, aSh, localIntName);
446 //=======================================================================
447 //function : WriteLayers
449 //=======================================================================
451 Standard_Boolean IGESCAFControl_Writer::WriteLayers (const TDF_LabelSequence& labels)
453 if ( labels.Length() <=0 ) return Standard_False;
454 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( labels(1) );
455 if ( STool.IsNull() ) return Standard_False;
456 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( labels(1) );
457 if ( LTool.IsNull() ) return Standard_False;
459 Standard_Integer globalIntName = 0;
460 TDF_LabelSequence aLayerLabels;
461 LTool->GetLayerLabels( aLayerLabels );
463 Handle(Transfer_FinderProcess) FP = TransferProcess();
464 for ( Standard_Integer i = 1; i <= aLayerLabels.Length(); i++ ){
465 TDF_Label aOneLayerL = aLayerLabels.Value(i);
466 if ( aOneLayerL.IsNull() ) continue;
467 TCollection_ExtendedString localName;
468 LTool->GetLayer(aOneLayerL, localName);
469 Standard_Integer localIntName = 0;
470 TCollection_AsciiString asciiName(localName,'?');
471 if (asciiName.IsIntegerValue() ) {
472 localIntName = asciiName.IntegerValue();
473 if (globalIntName < localIntName) globalIntName = localIntName;
475 TDF_LabelSequence aShapeLabels;
476 LTool->GetShapesOfLayer( aOneLayerL, aShapeLabels );
477 if ( aShapeLabels.Length() <= 0 ) continue;
478 MakeLayers(FP, STool, LTool, aShapeLabels, localIntName);
482 for ( Standard_Integer i1 = 1; i1 <= aLayerLabels.Length(); i1++ ) {
483 TDF_Label aOneLayerL = aLayerLabels.Value(i1);
484 if ( aOneLayerL.IsNull() ) continue;
485 TCollection_ExtendedString localName;
486 LTool->GetLayer(aOneLayerL, localName);
487 Standard_Integer localIntName = 0;
488 TCollection_AsciiString asciiName(localName,'?');
489 if (asciiName.IsIntegerValue() ) continue;
490 TDF_LabelSequence aShapeLabels;
491 LTool->GetShapesOfLayer( aOneLayerL, aShapeLabels );
492 if ( aShapeLabels.Length() <= 0 ) continue;
493 localIntName = ++globalIntName;
494 MakeLayers (FP, STool, LTool, aShapeLabels, localIntName);
497 return Standard_True;
501 //=======================================================================
502 //function : WriteNames
504 //=======================================================================
506 Standard_Boolean IGESCAFControl_Writer::WriteNames (const TDF_LabelSequence& theLabels)
508 if (theLabels.Length() <= 0) return Standard_False;
510 DataMapOfShapeNames aMapOfShapeNames;
512 for (Standard_Integer anIter = 1; anIter <= theLabels.Length(); anIter++ )
514 TDF_Label aLabel = theLabels.Value (anIter);
517 Handle(TDataStd_Name) aName;
518 if (!XCAFDoc_ShapeTool::GetShape (aLabel, aShape)) continue;
519 if (!aLabel.FindAttribute (TDataStd_Name::GetID(), aName)) continue;
521 aMapOfShapeNames.Bind (aShape, aName->Get());
523 // Collect names for subshapes
524 TopLoc_Location aLocation;
525 CollectShapeNames (aLabel, aLocation, aName, aMapOfShapeNames);
528 for (DataMapOfShapeNames::Iterator anIter (aMapOfShapeNames);
529 anIter.More(); anIter.Next())
531 const TopoDS_Shape& aShape = anIter.Key();
532 const TCollection_ExtendedString& aName = anIter.Value();
534 Handle(Transfer_FinderProcess) aFinderProcess = TransferProcess();
535 Handle(IGESData_IGESEntity) anIGESEntity;
536 Handle(TransferBRep_ShapeMapper) aShapeMapper = TransferBRep::ShapeMapper (aFinderProcess, aShape);
538 if (aFinderProcess->FindTypedTransient (aShapeMapper, STANDARD_TYPE(IGESData_IGESEntity), anIGESEntity))
540 Handle(TCollection_HAsciiString) anAsciiName = new TCollection_HAsciiString (" ");
541 Standard_Integer aNameLength = 8 - aName.Length();
542 if (aNameLength < 0) aNameLength = 0;
543 for (Standard_Integer aCharPos = 1; aNameLength < 8; aCharPos++, aNameLength++)
545 anAsciiName->SetValue (aNameLength+1, IsAnAscii (aName.Value (aCharPos)) ? (Standard_Character )aName.Value (aCharPos) : '?');
547 anIGESEntity->SetLabel (anAsciiName);
549 // Set long IGES name using 406 form 15 entity
550 Handle(IGESBasic_Name) aLongNameEntity = new IGESBasic_Name;
551 Handle(TCollection_HExtendedString) aTmpStr = new TCollection_HExtendedString(aName);
552 aLongNameEntity->Init(1, new TCollection_HAsciiString(aTmpStr, '_'));
554 anIGESEntity->AddProperty(aLongNameEntity);
555 AddEntity(aLongNameEntity);
559 return Standard_True;
562 //=======================================================================
563 //function : SetColorMode
565 //=======================================================================
567 void IGESCAFControl_Writer::SetColorMode (const Standard_Boolean colormode)
569 myColorMode = colormode;
572 //=======================================================================
573 //function : GetColorMode
575 //=======================================================================
577 Standard_Boolean IGESCAFControl_Writer::GetColorMode () const
582 //=======================================================================
583 //function : SetNameMode
585 //=======================================================================
587 void IGESCAFControl_Writer::SetNameMode (const Standard_Boolean namemode)
589 myNameMode = namemode;
592 //=======================================================================
593 //function : GetNameMode
595 //=======================================================================
597 Standard_Boolean IGESCAFControl_Writer::GetNameMode () const
602 //=======================================================================
603 //function : SetLayerMode
605 //=======================================================================
607 void IGESCAFControl_Writer::SetLayerMode (const Standard_Boolean layermode)
609 myLayerMode = layermode;
612 //=======================================================================
613 //function : GetLayerMode
615 //=======================================================================
617 Standard_Boolean IGESCAFControl_Writer::GetLayerMode () const