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 <NCollection_DataMap.hxx>
26 #include <Standard_Transient.hxx>
27 #include <TCollection_AsciiString.hxx>
28 #include <TCollection_HAsciiString.hxx>
29 #include <TCollection_HExtendedString.hxx>
30 #include <TColStd_HSequenceOfExtendedString.hxx>
31 #include <TDataStd_Name.hxx>
32 #include <TDF_ChildIterator.hxx>
33 #include <TDF_Label.hxx>
34 #include <TDF_LabelSequence.hxx>
35 #include <TDocStd_Document.hxx>
37 #include <TopExp_Explorer.hxx>
39 #include <TopoDS_Face.hxx>
40 #include <TopoDS_Iterator.hxx>
41 #include <TopoDS_Shape.hxx>
42 #include <TopTools_SequenceOfShape.hxx>
43 #include <Transfer_FinderProcess.hxx>
44 #include <Transfer_TransientListBinder.hxx>
45 #include <TransferBRep.hxx>
46 #include <TransferBRep_ShapeMapper.hxx>
47 #include <XCAFDoc_ColorTool.hxx>
48 #include <XCAFDoc_DocumentTool.hxx>
49 #include <XCAFDoc_LayerTool.hxx>
50 #include <XCAFDoc_ShapeTool.hxx>
51 #include <XCAFPrs.hxx>
52 #include <XCAFPrs_Style.hxx>
53 #include <XSControl_WorkSession.hxx>
57 typedef NCollection_DataMap<TopoDS_Shape, TCollection_ExtendedString> DataMapOfShapeNames;
59 void CollectShapeNames (const TDF_Label& theLabel,
60 const TopLoc_Location& theLocation,
61 const Handle(TDataStd_Name)& thePrevName,
62 DataMapOfShapeNames& theMapOfShapeNames)
64 Standard_Boolean hasReferredShape = Standard_False;
65 Standard_Boolean hasComponents = Standard_False;
66 TDF_Label aReferredLabel;
68 Handle(TDataStd_Name) aName;
69 theLabel.FindAttribute (TDataStd_Name::GetID(), aName);
71 if ( XCAFDoc_ShapeTool::GetReferredShape ( theLabel, aReferredLabel ) )
73 TopLoc_Location aSubLocation = theLocation.Multiplied ( XCAFDoc_ShapeTool::GetLocation ( theLabel ) );
74 CollectShapeNames (aReferredLabel, aSubLocation, aName, theMapOfShapeNames);
75 hasReferredShape = Standard_True;
78 TDF_LabelSequence aSeq;
79 if (XCAFDoc_ShapeTool::GetComponents (theLabel, aSeq))
81 for (Standard_Integer anIter = 1; anIter <= aSeq.Length(); anIter++)
83 CollectShapeNames (aSeq.Value (anIter), theLocation, aName, theMapOfShapeNames );
85 hasComponents = Standard_True;
89 if (XCAFDoc_ShapeTool::GetSubShapes (theLabel, aSeq))
91 for (Standard_Integer anIter = 1; anIter <= aSeq.Length(); anIter++)
94 if (!XCAFDoc_ShapeTool::GetShape (aSeq.Value (anIter), aShape)) continue;
95 if (!aSeq.Value (anIter).FindAttribute (TDataStd_Name::GetID(), aName)) continue;
96 theMapOfShapeNames.Bind (aShape, aName->Get());
100 if (!hasReferredShape && !hasComponents)
103 if (!XCAFDoc_ShapeTool::GetShape (theLabel, aShape)) return;
104 aShape.Move (theLocation);
105 theMapOfShapeNames.Bind (aShape, thePrevName->Get());
110 //=======================================================================
111 //function : IGESCAFControl_Writer
113 //=======================================================================
115 IGESCAFControl_Writer::IGESCAFControl_Writer () :
116 myColorMode( Standard_True ),
117 myNameMode ( Standard_True ),
118 myLayerMode( Standard_True )
122 //=======================================================================
123 //function : IGESCAFControl_Writer
125 //=======================================================================
127 IGESCAFControl_Writer::IGESCAFControl_Writer (const Handle(XSControl_WorkSession)& WS,
128 const Standard_Boolean /*scratch*/ )
130 // this code does things in a wrong way, it should be vice-versa
131 WS->SetModel ( Model() );
132 WS->SetMapWriter ( TransferProcess() );
133 myColorMode = Standard_True;
134 myNameMode = Standard_True;
135 myLayerMode = Standard_True;
137 // SetWS (WS,scratch); // this should be the only required command here
140 //=======================================================================
141 //function : Transfer
143 //=======================================================================
145 Standard_Boolean IGESCAFControl_Writer::Transfer (const Handle(TDocStd_Document) &doc)
147 // translate free top-level shapes of the DECAF document
148 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( doc->Main() );
149 if ( STool.IsNull() ) return Standard_False;
151 TDF_LabelSequence labels;
152 STool->GetFreeShapes ( labels );
153 return Transfer (labels);
156 //=======================================================================
157 //function : Transfer
159 //=======================================================================
161 Standard_Boolean IGESCAFControl_Writer::Transfer (const TDF_Label& label)
163 TDF_LabelSequence labels;
164 labels.Append( label );
165 return Transfer( labels );
168 //=======================================================================
169 //function : Transfer
171 //=======================================================================
173 Standard_Boolean IGESCAFControl_Writer::Transfer (const TDF_LabelSequence& labels)
175 if ( labels.Length() <=0 ) return Standard_False;
176 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
177 TopoDS_Shape shape = XCAFDoc_ShapeTool::GetShape ( labels.Value(i) );
178 if ( ! shape.IsNull() )
180 // IGESControl_Writer::Transfer ( shape );
184 if ( GetColorMode() )
185 WriteAttributes ( labels );
188 if ( GetLayerMode() )
189 WriteLayers ( labels );
193 WriteNames( labels );
196 // WS()->ComputeGraph ( Standard_True );
199 return Standard_True;
202 //=======================================================================
205 //=======================================================================
207 Standard_Boolean IGESCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
208 const Standard_CString filename)
210 if ( ! Transfer ( doc ) ) return Standard_False;
211 return Write ( filename ) == IFSelect_RetDone;
214 //=======================================================================
217 //=======================================================================
219 Standard_Boolean IGESCAFControl_Writer::Perform (const Handle(TDocStd_Document) &doc,
220 const TCollection_AsciiString &filename)
222 if ( ! Transfer ( doc ) ) return Standard_False;
223 return Write ( filename.ToCString() ) == IFSelect_RetDone;
226 //=======================================================================
227 //function : WriteAttributes
229 //=======================================================================
231 Standard_Boolean IGESCAFControl_Writer::WriteAttributes (const TDF_LabelSequence& labels)
234 if ( labels.Length() <=0 ) return Standard_False;
235 for ( Standard_Integer i=1; i <= labels.Length(); i++ ) {
236 TDF_Label L = labels.Value(i);
238 // collect color settings
239 XCAFPrs_IndexedDataMapOfShapeStyle settings;
241 XCAFPrs::CollectStyleSettings ( L, loc, settings );
242 if ( settings.Extent() <=0 ) continue;
244 // get a target shape and try to find corresponding context
245 // (all the colors set under that label will be put into that context)
247 if ( ! XCAFDoc_ShapeTool::GetShape ( L, S ) ) continue;
249 // iterate on subshapes and create IGES styles
250 XCAFPrs_DataMapOfStyleTransient colors;
251 TopTools_MapOfShape Map;
252 const XCAFPrs_Style inherit;
253 MakeColors ( S, settings, colors, Map, inherit );
256 return Standard_True;
259 //=======================================================================
260 //function : MakeColors
262 //=======================================================================
264 void IGESCAFControl_Writer::MakeColors (const TopoDS_Shape &S,
265 const XCAFPrs_IndexedDataMapOfShapeStyle &settings,
266 XCAFPrs_DataMapOfStyleTransient &colors,
267 TopTools_MapOfShape &Map,
268 const XCAFPrs_Style &inherit)
270 // skip already processed shapes
271 if ( ! Map.Add ( S ) ) return;
273 // check if shape has its own style (or inherits from ancestor)
274 XCAFPrs_Style style = inherit;
275 if ( settings.Contains(S) ) {
276 XCAFPrs_Style own = settings.FindFromKey(S);
277 if ( own.IsSetColorCurv() ) style.SetColorCurv ( own.GetColorCurv() );
278 if ( own.IsSetColorSurf() ) style.SetColorSurf ( own.GetColorSurf() );
281 // analyze whether current entity should get a color
282 Standard_Boolean hasColor = Standard_False;
284 if ( S.ShapeType() == TopAbs_FACE || S.ShapeType() == TopAbs_SOLID) {
285 if ( style.IsSetColorSurf() ) {
286 hasColor = Standard_True;
287 col = style.GetColorSurf();
290 else if ( S.ShapeType() == TopAbs_EDGE || S.ShapeType() == TopAbs_WIRE ) {
291 if ( style.IsSetColorCurv() ) {
292 hasColor = Standard_True;
293 col = style.GetColorCurv();
297 // if color has to be assigned, try to do this
299 Handle(IGESGraph_Color) colent;
300 Standard_Integer rank = IGESCAFControl::EncodeColor ( col );
302 XCAFPrs_Style c; // style used as key in the map
303 c.SetColorSurf ( col );
304 if ( colors.IsBound ( c ) ) {
305 colent = Handle(IGESGraph_Color)::DownCast ( colors.Find(c) );
308 Handle(TCollection_HAsciiString) str =
309 new TCollection_HAsciiString ( col.StringName ( col.Name() ) );
310 colent = new IGESGraph_Color;
311 NCollection_Vec3<Standard_Real> aColor_sRGB;
312 col.Values (aColor_sRGB.r(), aColor_sRGB.g(), aColor_sRGB.b(), Quantity_TOC_sRGB);
313 colent->Init ( aColor_sRGB.r() * 100., aColor_sRGB.g() * 100., aColor_sRGB.b() * 100., str );
314 AddEntity ( colent );
315 colors.Bind ( c, colent );
318 Handle(Transfer_FinderProcess) FP = TransferProcess();
319 Handle(IGESData_IGESEntity) ent;
320 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, S );
321 if ( FP->FindTypedTransient ( mapper, STANDARD_TYPE(IGESData_IGESEntity), ent ) ) {
322 ent->InitColor ( colent, rank );
323 Handle(IGESSolid_Face) ent_f = Handle(IGESSolid_Face)::DownCast(ent);
326 if (!ent_f->Surface().IsNull())
327 ent_f->Surface()->InitColor ( colent, rank );
331 // may be S was splited during shape process
332 Handle(Transfer_Binder) bnd = FP->Find ( mapper );
333 if ( ! bnd.IsNull() ) {
334 Handle(Transfer_TransientListBinder) TransientListBinder =
335 //Handle(Transfer_TransientListBinder)::DownCast( bnd->Next(Standard_True) );
336 Handle(Transfer_TransientListBinder)::DownCast( bnd );
337 Standard_Integer i=0, nb=0;
338 if (! TransientListBinder.IsNull() ) {
339 nb = TransientListBinder->NbTransients();
340 for (i=1; i<=nb; i++) {
341 Handle(Standard_Transient) t = TransientListBinder->Transient(i);
342 ent = Handle(IGESData_IGESEntity)::DownCast(t);
345 ent->InitColor ( colent, rank );
346 Handle(IGESSolid_Face) ent_f = Handle(IGESSolid_Face)::DownCast(ent);
349 if (!ent_f->Surface().IsNull())
350 ent_f->Surface()->InitColor ( colent, rank );
355 /* // alternative: consider recursive mapping S -> compound -> entities
357 TopoDS_Shape comp = TransferBRep::ShapeResult(bnd);
358 if ( ! comp.IsNull() && comp.ShapeType() < S.ShapeType() )
359 for ( TopoDS_Iterator it(comp); it.More(); it.Next() ) {
360 MakeColors ( it.Value(), settings, colors, Map, style );
368 // iterate on subshapes (except vertices :)
369 if ( S.ShapeType() == TopAbs_EDGE ) return;
370 for ( TopoDS_Iterator it(S); it.More(); it.Next() ) {
371 MakeColors ( it.Value(), settings, colors, Map, style );
376 static void AttachLayer (const Handle(Transfer_FinderProcess) &FP,
377 const Handle(XCAFDoc_LayerTool)& LTool,
378 const TopoDS_Shape& aSh,
379 const Standard_Integer localIntName)
382 TopTools_SequenceOfShape shseq;
383 if ( aSh.ShapeType() == TopAbs_COMPOUND ) {
384 TopoDS_Iterator aShIt(aSh);
385 for ( ; aShIt.More(); aShIt.Next() ) {
386 TopoDS_Shape newSh = aShIt.Value();
387 Handle(TColStd_HSequenceOfExtendedString) shLayers = new TColStd_HSequenceOfExtendedString;
388 if (! LTool->GetLayers( newSh, shLayers) || newSh.ShapeType() == TopAbs_COMPOUND )
389 AttachLayer(FP, LTool, newSh, localIntName);
392 } else if ( aSh.ShapeType() == TopAbs_SOLID || aSh.ShapeType() == TopAbs_SHELL ) {
393 for (TopExp_Explorer exp(aSh,TopAbs_FACE) ; exp.More(); exp.Next()) {
394 TopoDS_Face entSh = TopoDS::Face (exp.Current());
401 for (Standard_Integer i = 1; i <= shseq.Length(); i++ ) {
402 TopoDS_Shape localShape = shseq.Value(i);
403 Handle(IGESData_IGESEntity) Igesent;
404 Handle(TransferBRep_ShapeMapper) mapper = TransferBRep::ShapeMapper ( FP, localShape );
405 if ( FP->FindTypedTransient ( mapper, STANDARD_TYPE(IGESData_IGESEntity), Igesent ) ) {
406 Igesent->InitLevel( 0, localIntName );
409 else std::cout << "Warning: Can't find entity for shape in mapper" << std::endl;
415 static void MakeLayers (const Handle(Transfer_FinderProcess) &FP,
416 const Handle(XCAFDoc_ShapeTool)& STool,
417 const Handle(XCAFDoc_LayerTool)& LTool,
418 const TDF_LabelSequence& aShapeLabels,
419 const Standard_Integer localIntName)
421 for ( Standard_Integer j = 1; j <= aShapeLabels.Length(); j++ ) {
422 TDF_Label aShapeLabel = aShapeLabels.Value(j);
424 if ( ! STool->GetShape ( aShapeLabel, aSh ) ) continue;
425 AttachLayer (FP, LTool, aSh, localIntName);
431 //=======================================================================
432 //function : WriteLayers
434 //=======================================================================
436 Standard_Boolean IGESCAFControl_Writer::WriteLayers (const TDF_LabelSequence& labels)
438 if ( labels.Length() <=0 ) return Standard_False;
439 Handle(XCAFDoc_ShapeTool) STool = XCAFDoc_DocumentTool::ShapeTool( labels(1) );
440 if ( STool.IsNull() ) return Standard_False;
441 Handle(XCAFDoc_LayerTool) LTool = XCAFDoc_DocumentTool::LayerTool( labels(1) );
442 if ( LTool.IsNull() ) return Standard_False;
444 Standard_Integer globalIntName = 0;
445 TDF_LabelSequence aLayerLabels;
446 LTool->GetLayerLabels( aLayerLabels );
448 Handle(Transfer_FinderProcess) FP = TransferProcess();
449 for ( Standard_Integer i = 1; i <= aLayerLabels.Length(); i++ ){
450 TDF_Label aOneLayerL = aLayerLabels.Value(i);
451 if ( aOneLayerL.IsNull() ) continue;
452 TCollection_ExtendedString localName;
453 LTool->GetLayer(aOneLayerL, localName);
454 Standard_Integer localIntName = 0;
455 TCollection_AsciiString asciiName(localName,'?');
456 if (asciiName.IsIntegerValue() ) {
457 localIntName = asciiName.IntegerValue();
458 if (globalIntName < localIntName) globalIntName = localIntName;
460 TDF_LabelSequence aShapeLabels;
461 LTool->GetShapesOfLayer( aOneLayerL, aShapeLabels );
462 if ( aShapeLabels.Length() <= 0 ) continue;
463 MakeLayers(FP, STool, LTool, aShapeLabels, localIntName);
467 for ( Standard_Integer i1 = 1; i1 <= aLayerLabels.Length(); i1++ ) {
468 TDF_Label aOneLayerL = aLayerLabels.Value(i1);
469 if ( aOneLayerL.IsNull() ) continue;
470 TCollection_ExtendedString localName;
471 LTool->GetLayer(aOneLayerL, localName);
472 Standard_Integer localIntName = 0;
473 TCollection_AsciiString asciiName(localName,'?');
474 if (asciiName.IsIntegerValue() ) continue;
475 TDF_LabelSequence aShapeLabels;
476 LTool->GetShapesOfLayer( aOneLayerL, aShapeLabels );
477 if ( aShapeLabels.Length() <= 0 ) continue;
478 localIntName = ++globalIntName;
479 MakeLayers (FP, STool, LTool, aShapeLabels, localIntName);
482 return Standard_True;
486 //=======================================================================
487 //function : WriteNames
489 //=======================================================================
491 Standard_Boolean IGESCAFControl_Writer::WriteNames (const TDF_LabelSequence& theLabels)
493 if (theLabels.Length() <= 0) return Standard_False;
495 DataMapOfShapeNames aMapOfShapeNames;
497 for (Standard_Integer anIter = 1; anIter <= theLabels.Length(); anIter++ )
499 TDF_Label aLabel = theLabels.Value (anIter);
502 Handle(TDataStd_Name) aName;
503 if (!XCAFDoc_ShapeTool::GetShape (aLabel, aShape)) continue;
504 if (!aLabel.FindAttribute (TDataStd_Name::GetID(), aName)) continue;
506 aMapOfShapeNames.Bind (aShape, aName->Get());
508 // Collect names for subshapes
509 TopLoc_Location aLocation;
510 CollectShapeNames (aLabel, aLocation, aName, aMapOfShapeNames);
513 for (DataMapOfShapeNames::Iterator anIter (aMapOfShapeNames);
514 anIter.More(); anIter.Next())
516 const TopoDS_Shape& aShape = anIter.Key();
517 const TCollection_ExtendedString& aName = anIter.Value();
519 Handle(Transfer_FinderProcess) aFinderProcess = TransferProcess();
520 Handle(IGESData_IGESEntity) anIGESEntity;
521 Handle(TransferBRep_ShapeMapper) aShapeMapper = TransferBRep::ShapeMapper (aFinderProcess, aShape);
523 if (aFinderProcess->FindTypedTransient (aShapeMapper, STANDARD_TYPE(IGESData_IGESEntity), anIGESEntity))
525 Handle(TCollection_HAsciiString) anAsciiName = new TCollection_HAsciiString (" ");
526 Standard_Integer aNameLength = 8 - aName.Length();
527 if (aNameLength < 0) aNameLength = 0;
528 for (Standard_Integer aCharPos = 1; aNameLength < 8; aCharPos++, aNameLength++)
530 anAsciiName->SetValue (aNameLength+1, IsAnAscii (aName.Value (aCharPos)) ? (Standard_Character )aName.Value (aCharPos) : '?');
532 anIGESEntity->SetLabel (anAsciiName);
534 // Set long IGES name using 406 form 15 entity
535 Handle(IGESBasic_Name) aLongNameEntity = new IGESBasic_Name;
536 Handle(TCollection_HExtendedString) aTmpStr = new TCollection_HExtendedString(aName);
537 aLongNameEntity->Init(1, new TCollection_HAsciiString(aTmpStr, '_'));
539 anIGESEntity->AddProperty(aLongNameEntity);
540 AddEntity(aLongNameEntity);
544 return Standard_True;
547 //=======================================================================
548 //function : SetColorMode
550 //=======================================================================
552 void IGESCAFControl_Writer::SetColorMode (const Standard_Boolean colormode)
554 myColorMode = colormode;
557 //=======================================================================
558 //function : GetColorMode
560 //=======================================================================
562 Standard_Boolean IGESCAFControl_Writer::GetColorMode () const
567 //=======================================================================
568 //function : SetNameMode
570 //=======================================================================
572 void IGESCAFControl_Writer::SetNameMode (const Standard_Boolean namemode)
574 myNameMode = namemode;
577 //=======================================================================
578 //function : GetNameMode
580 //=======================================================================
582 Standard_Boolean IGESCAFControl_Writer::GetNameMode () const
587 //=======================================================================
588 //function : SetLayerMode
590 //=======================================================================
592 void IGESCAFControl_Writer::SetLayerMode (const Standard_Boolean layermode)
594 myLayerMode = layermode;
597 //=======================================================================
598 //function : GetLayerMode
600 //=======================================================================
602 Standard_Boolean IGESCAFControl_Writer::GetLayerMode () const