ba95fb85b90743f4cae2ae3bd34e99bf5287bb1a
[occt.git] / src / MNaming / MNaming_NamingRetrievalDriver_1.cxx
1 // Created on: 2008-08-15
2 // Created by: Sergey ZARITCHNY
3 // Copyright (c) 2008-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <MNaming_NamingRetrievalDriver_1.ixx>
17 #include <PNaming_Naming_1.hxx>
18 #include <PNaming_NamedShape.hxx>
19 #include <PNaming_HArray1OfNamedShape.hxx>
20 #include <PNaming_Name_1.hxx>
21 #include <PCollection_HAsciiString.hxx>
22 #include <TCollection_AsciiString.hxx>
23 #include <TDF_Tool.hxx>
24 #include <TNaming_NamedShape.hxx>
25 #include <Standard_NoSuchObject.hxx>
26 #include <TNaming_Naming.hxx>
27 #include <TNaming_NameType.hxx>
28 #include <TNaming_Iterator.hxx>
29 #include <TopAbs_ShapeEnum.hxx>
30 #include <CDM_MessageDriver.hxx>
31
32 //=======================================================================
33 //function : IntegerToShapeEnum
34 //purpose  : 
35 //=======================================================================
36
37 static TopAbs_ShapeEnum IntegerToShapeEnum (const Standard_Integer I)
38 {
39   switch(I)
40     {
41     case  0 : return TopAbs_COMPOUND;
42     case  1 : return TopAbs_COMPSOLID;
43     case  2 : return TopAbs_SOLID;
44     case  3 : return TopAbs_SHELL;
45     case  4 : return TopAbs_FACE;
46     case  5 : return TopAbs_WIRE;
47     case  6 : return TopAbs_EDGE;
48     case  7 : return TopAbs_VERTEX;
49     default : return TopAbs_SHAPE;
50     }
51 }
52
53 //=======================================================================
54 //function : IntegerToNameType
55 //purpose  : 
56 //=======================================================================
57
58 static  TNaming_NameType IntegerToNameType (const Standard_Integer I) 
59 {
60   switch(I)
61     {
62     case  0 : return TNaming_UNKNOWN;  
63     case  1 : return TNaming_IDENTITY;
64     case  2 : return TNaming_MODIFUNTIL;
65     case  3 : return TNaming_GENERATION;
66     case  4 : return TNaming_INTERSECTION;
67     case  5 : return TNaming_UNION;
68     case  6 : return TNaming_SUBSTRACTION;
69     case  7 : return TNaming_CONSTSHAPE; 
70     case  8 : return TNaming_FILTERBYNEIGHBOURGS;
71     case  9 : return TNaming_ORIENTATION;
72     case  10 : return TNaming_WIREIN;
73       default :
74         Standard_DomainError::Raise("TNaming_NameType; enum term unknown ");
75     }
76   return TNaming_INTERSECTION;
77 }
78
79 //=======================================================================
80 //function : MNaming_Naming_1
81 //purpose  : 
82 //=======================================================================
83
84 MNaming_NamingRetrievalDriver_1::MNaming_NamingRetrievalDriver_1(
85  const Handle(CDM_MessageDriver)& theMsgDriver):MDF_ARDriver(theMsgDriver)
86 {
87 }
88
89 //=======================================================================
90 //function : VersionNumber
91 //purpose  : 
92 //=======================================================================
93
94 Standard_Integer MNaming_NamingRetrievalDriver_1::VersionNumber() const
95 { return 0; }
96
97 //=======================================================================
98 //function : SourceType
99 //purpose  : 
100 //=======================================================================
101
102 Handle(Standard_Type) MNaming_NamingRetrievalDriver_1::SourceType() const
103 { return STANDARD_TYPE(PNaming_Naming_1);  }
104
105
106 //=======================================================================
107 //function : NewEmpty
108 //purpose  : 
109 //=======================================================================
110
111 Handle(TDF_Attribute) MNaming_NamingRetrievalDriver_1::NewEmpty() const
112 { return new TNaming_Naming (); }
113
114 //=======================================================================
115 //function : PNamingToTNaming
116 //purpose  : 
117 //=======================================================================
118
119 static void PNamingToTNaming (const Handle(PNaming_Name_1)& PN,
120                               TNaming_Name&                 TN,
121                               const Handle(MDF_RRelocationTable)& RelocTable)
122
123 {
124   TN.Type     (IntegerToNameType (PN->Type     ()));
125   TN.ShapeType(IntegerToShapeEnum(PN->ShapeType()));
126   Handle(TNaming_NamedShape) NS;
127   Handle(TDF_Data) aData;
128   if (!PN->Arguments().IsNull()) {
129     Standard_Integer NbArgs = PN->Arguments()->Length();
130     for (Standard_Integer i = 1; i <= NbArgs; i++) {
131       RelocTable->HasRelocation(PN->Arguments()->Value(i),NS);
132       TN.Append(NS);
133       if(aData.IsNull() && !NS.IsNull())
134         aData = NS->Label().Data();
135     }
136   }
137
138   Handle(TNaming_NamedShape) StopNS;
139   if (!PN->StopNamedShape().IsNull()) 
140     RelocTable->HasRelocation(PN->StopNamedShape(),StopNS);
141   TN.StopNamedShape(StopNS);  
142   TN.Index(PN->Index());
143
144   TDF_Label tLab; // Null label.  
145   Handle(PCollection_HAsciiString) pEntry = PN->ContextLabel();
146   if (!pEntry.IsNull()) {
147     TCollection_AsciiString entry = pEntry->Convert();
148     if(!aData.IsNull())
149       TDF_Tool::Label(aData,entry,tLab);
150   }
151   TN.ContextLabel(tLab);
152 }
153
154 //=======================================================================
155 //function : Paste
156 //purpose  : 
157 //=======================================================================
158 //#define DEB_ORIENT
159 void MNaming_NamingRetrievalDriver_1::Paste (
160   const Handle(PDF_Attribute)&        Source,
161   const Handle(TDF_Attribute)&        Target,
162   const Handle(MDF_RRelocationTable)& RelocTable) const
163 {
164   Handle(PNaming_Naming_1) PF = Handle(PNaming_Naming_1)::DownCast(Source);
165   Handle(TNaming_Naming) F  = Handle(TNaming_Naming)::DownCast(Target); 
166   PNamingToTNaming (PF->GetName(),F->ChangeName(),RelocTable);
167   // Orientation processing
168   Handle(TNaming_NamedShape) aNS;
169   if(F->Label().FindAttribute(TNaming_NamedShape::GetID(), aNS)) {
170 #ifdef OCCT_DEBUG_ORIENT
171     const TDF_Label& aLab = aNS->Label();
172         TCollection_AsciiString entry;
173         TDF_Tool::Entry(aLab, entry);
174         cout << "Label = " << entry << " Evolution = " <<aNS->Evolution() << " IsEmpty = " << aNS->IsEmpty() <<endl;
175 #endif
176         if(!aNS->IsEmpty()) {
177       TNaming_Iterator itL (aNS);
178       for (; itL.More(); itL.Next()) {
179         const TopoDS_Shape& S = itL.NewShape();
180         if (S.IsNull()) continue;
181         if(aNS->Evolution() == TNaming_SELECTED) {
182           if (itL.More() && itL.NewShape().ShapeType() != TopAbs_VERTEX &&
183                 !itL.OldShape().IsNull() && itL.OldShape().ShapeType() == TopAbs_VERTEX ) {//OR-N
184             TopAbs_Orientation OrientationToApply = itL.OldShape().Orientation();
185 #ifdef OCCT_DEBUG_ORIENT
186                         cout <<"Retrieved Orientation = " << OrientationToApply <<endl;
187 #endif 
188             F->ChangeName().Orientation(OrientationToApply);
189           }
190         }
191       }
192         }
193   }
194   // 
195 }
196