Salome HOME
6d72fc8d3eec235d2317f53f090f1bb96b1c9cf2
[modules/geom.git] / src / GEOMImpl / GEOMImpl_ITransferData.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //NOTE: This is an interface to a function for the Transfer Data functionality.
24 //
25
26
27 #include "GEOMImpl_ITransferData.hxx"
28
29 #include <TDataStd_Comment.hxx>
30 #include <TDataStd_Name.hxx>
31 #include <TDF_ChildIDIterator.hxx>
32 #include <TNaming_Builder.hxx>
33 #include <TNaming_NamedShape.hxx>
34
35
36 //=============================================================================
37 /*!
38  *  SetName
39  */
40 //=============================================================================
41 void GEOMImpl_ITransferData::SetStringData
42                   (const TopoDS_Shape            &theSubShape,
43                    const TCollection_AsciiString &theData,
44                    const Standard_Boolean         IsName)
45 {
46   TDF_Label           aLabel;
47   TDF_ChildIDIterator anIt
48     (_func->GetNamingEntry(), TDataStd_Name::GetID(), Standard_True);
49
50   for (; anIt.More(); anIt.Next()) {
51     Handle(TDataStd_Name) aNameAttr =
52         Handle(TDataStd_Name)::DownCast(anIt.Value());
53
54     if (aNameAttr.IsNull()) {
55       continue;
56     }
57
58     TDF_Label aLab = aNameAttr->Label();
59     Handle(TNaming_NamedShape) aShAttr;
60
61     if (aLab.FindAttribute(TNaming_NamedShape::GetID(), aShAttr) &&
62         aShAttr->Get().IsEqual(theSubShape)) {
63       aLabel = aLab;
64       break;
65     }
66   }
67
68   if (aLabel.IsNull()) {
69     TDF_TagSource aTag;
70
71     aLabel = aTag.NewChild(_func->GetNamingEntry());
72
73     TNaming_Builder aTnBuild (aLabel);
74
75     aTnBuild.Generated(theSubShape);
76   }
77
78   // set a name or a material
79   TCollection_ExtendedString aDataExt(theData);
80
81   if (IsName) {
82     TDataStd_Name::Set(aLabel, aDataExt);
83   } else {
84     TDataStd_Comment::Set(aLabel, aDataExt);
85   }
86 }
87
88 //=============================================================================
89 /*!
90  *  GetStringData
91  */
92 //=============================================================================
93 TCollection_AsciiString GEOMImpl_ITransferData::GetStringData
94                   (const TopoDS_Shape     &theSubShape,
95                    const Standard_Boolean  IsName)
96 {
97   // check all named shapes using iterator
98   TCollection_AsciiString aResult;
99   TDF_ChildIDIterator     anIt
100     (_func->GetNamingEntry(), TNaming_NamedShape::GetID(), Standard_True);
101
102   for (; anIt.More(); anIt.Next()) {
103     Handle(TNaming_NamedShape) aShAttr =
104         Handle(TNaming_NamedShape)::DownCast(anIt.Value());
105
106     if (aShAttr.IsNull()) {
107       continue;
108     }
109
110     if (aShAttr->Get().IsEqual(theSubShape)) {
111       TDF_Label        aLabel  = aShAttr->Label();
112       Standard_Boolean isFound = Standard_False;
113
114       if (IsName) {
115         Handle(TDataStd_Name) aString;
116
117         if(aLabel.FindAttribute(TDataStd_Name::GetID(), aString)) {
118           aResult = TCollection_AsciiString(aString->Get());
119           isFound = Standard_True;
120         }
121       } else {
122         Handle(TDataStd_Comment) aString;
123
124         if(aLabel.FindAttribute(TDataStd_Comment::GetID(), aString)) {
125           aResult = TCollection_AsciiString(aString->Get());
126           isFound = Standard_True;
127         }
128       }
129
130       if (isFound) {
131         break;
132       }
133     }
134   }
135
136   return aResult;
137 }