0032527: Data Exchange, RWGltf_CafWriter - make name format configurable
[occt.git] / src / RWMesh / RWMesh.cxx
CommitLineData
fd42e764 1// Copyright (c) 2021 OPEN CASCADE SAS
2//
3// This file is part of Open CASCADE Technology software library.
4//
5// This library is free software; you can redistribute it and/or modify it under
6// the terms of the GNU Lesser General Public License version 2.1 as published
7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
10//
11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
13
14#include <RWMesh.hxx>
15
16#include <TDataStd_Name.hxx>
17#include <TDF_Tool.hxx>
18
19// ================================================================
20// Function : ReadNameAttribute
21// Purpose :
22// ================================================================
23TCollection_AsciiString RWMesh::ReadNameAttribute (const TDF_Label& theLabel)
24{
25 Handle(TDataStd_Name) aNodeName;
26 return theLabel.FindAttribute (TDataStd_Name::GetID(), aNodeName)
27 ? TCollection_AsciiString (aNodeName->Get())
28 : TCollection_AsciiString();
29}
30
31// ================================================================
32// Function : FormatName
33// Purpose :
34// ================================================================
35TCollection_AsciiString RWMesh::FormatName (RWMesh_NameFormat theFormat,
36 const TDF_Label& theLabel,
37 const TDF_Label& theRefLabel)
38{
39 switch (theFormat)
40 {
41 case RWMesh_NameFormat_Empty:
42 {
43 return TCollection_AsciiString();
44 }
45 case RWMesh_NameFormat_Product:
46 {
47 Handle(TDataStd_Name) aRefNodeName;
48 return theRefLabel.FindAttribute (TDataStd_Name::GetID(), aRefNodeName)
49 ? TCollection_AsciiString (aRefNodeName->Get())
50 : TCollection_AsciiString();
51 }
52 case RWMesh_NameFormat_Instance:
53 {
54 Handle(TDataStd_Name) aNodeName;
55 return theLabel.FindAttribute (TDataStd_Name::GetID(), aNodeName)
56 ? TCollection_AsciiString (aNodeName->Get())
57 : TCollection_AsciiString();
58 }
59 case RWMesh_NameFormat_InstanceOrProduct:
60 {
61 Handle(TDataStd_Name) aNodeName;
62 if (theLabel.FindAttribute (TDataStd_Name::GetID(), aNodeName)
63 && !aNodeName->Get().IsEmpty())
64 {
65 return TCollection_AsciiString (aNodeName->Get());
66 }
67
68 Handle(TDataStd_Name) aRefNodeName;
69 return theRefLabel.FindAttribute (TDataStd_Name::GetID(), aRefNodeName)
70 ? TCollection_AsciiString (aRefNodeName->Get())
71 : TCollection_AsciiString();
72 }
73 case RWMesh_NameFormat_ProductOrInstance:
74 {
75 Handle(TDataStd_Name) aRefNodeName;
76 if (theRefLabel.FindAttribute (TDataStd_Name::GetID(), aRefNodeName)
77 && !aRefNodeName->Get().IsEmpty())
78 {
79 return TCollection_AsciiString (aRefNodeName->Get());
80 }
81
82 Handle(TDataStd_Name) aNodeName;
83 return theLabel.FindAttribute (TDataStd_Name::GetID(), aNodeName)
84 ? TCollection_AsciiString (aNodeName->Get())
85 : TCollection_AsciiString();
86 }
87 case RWMesh_NameFormat_ProductAndInstance:
88 {
89 const TCollection_AsciiString anInstName = ReadNameAttribute (theLabel);
90 const TCollection_AsciiString aProdName = ReadNameAttribute (theRefLabel);
91 return !anInstName.IsEmpty()
92 && aProdName != anInstName
93 ? aProdName + " [" + anInstName + "]"
94 : (!aProdName.IsEmpty()
95 ? aProdName
96 : TCollection_AsciiString(""));
97 }
98 case RWMesh_NameFormat_ProductAndInstanceAndOcaf:
99 {
100 const TCollection_AsciiString anInstName = ReadNameAttribute (theLabel);
101 const TCollection_AsciiString aProdName = ReadNameAttribute (theRefLabel);
102 TCollection_AsciiString anEntryId;
103 TDF_Tool::Entry (theLabel, anEntryId);
104 return !anInstName.IsEmpty()
105 && aProdName != anInstName
106 ? aProdName + " [" + anInstName + "]" + " [" + anEntryId + "]"
107 : aProdName + " [" + anEntryId + "]";
108 }
109 }
110
111 return TCollection_AsciiString();
112}