89ce9da326326872714b6b7a0e8e995b136a62e5
[occt.git] / src / XCAFDoc / XCAFDoc_Editor.cxx
1 // Created on: 2015-05-14
2 // Created by: data exchange team
3 // Copyright (c) 2000-2015 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 <XCAFDoc_Editor.hxx>
17 #include <XCAFDoc.hxx>
18
19 #include <TDF_Label.hxx>
20 #include <TDF_LabelSequence.hxx>
21 #include <TDF_ChildIterator.hxx>
22 #include <TDataStd_Name.hxx>
23 #include <TDataStd_UAttribute.hxx>
24 #include <TopLoc_Location.hxx>
25 #include <TopoDS_Shape.hxx>
26
27 #include <XCAFDoc_DocumentTool.hxx>
28 #include <XCAFDoc_ColorTool.hxx>
29 #include <XCAFDoc_LayerTool.hxx>
30 #include <XCAFDoc_ShapeTool.hxx>
31 #include <XCAFDoc_ShapeMapTool.hxx>
32 #include <XCAFDoc_Location.hxx>
33 #include <TNaming_NamedShape.hxx>
34
35 //=======================================================================
36 //function : Expand
37 //purpose  : Convert Shape(compound) to assambly
38 //=======================================================================
39
40 Standard_Boolean XCAFDoc_Editor::Expand (const TDF_Label& Doc, const TDF_Label& Shape,
41   const Standard_Boolean recursively)
42 {
43   if(Doc.IsNull() || Shape.IsNull())
44     return Standard_False;
45
46   Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(Doc);
47   Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc);
48   Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc);
49
50   TopoDS_Shape aS = aShapeTool->GetShape(Shape);
51   if (!aS.IsNull() && aS.ShapeType() == TopAbs_COMPOUND && !aShapeTool->IsAssembly(Shape))
52   {
53     //convert compound to assembly(without attributes)
54     aShapeTool->Expand(Shape);
55     //move attrributes
56     TDF_ChildIterator anIter(Shape, Standard_True);
57     for(; anIter.More(); anIter.Next())
58     {
59       TDF_Label aChild = anIter.Value();
60
61       TDF_LabelSequence aLayers;
62       TDF_LabelSequence aColors;
63       Handle(TDataStd_Name) aName;
64       getParams(Doc, aChild, aColors, aLayers, aName);
65       //get part
66       TDF_Label aPart;
67       if(aShapeTool->GetReferredShape(aChild, aPart))
68       {
69         setParams(Doc, aPart, aColors, aLayers, aName);
70         //remove unnecessary links
71         TopoDS_Shape aShape = aShapeTool->GetShape(aChild);
72         if(!aShapeTool->GetShape(aPart.Father()).IsNull())
73         {
74           TopLoc_Location nulloc;
75           {
76             aPart.ForgetAttribute(XCAFDoc::ShapeRefGUID());
77             if(aShapeTool->GetShape(aPart.Father()).ShapeType() == TopAbs_COMPOUND)
78             {
79               aShapeTool->SetShape(aPart, aShape);
80             }
81             aPart.ForgetAttribute(XCAFDoc_ShapeMapTool::GetID());
82             aChild.ForgetAllAttributes(Standard_False);
83           }
84         }
85         aChild.ForgetAttribute(TNaming_NamedShape::GetID());
86         aChild.ForgetAttribute(XCAFDoc_ShapeMapTool::GetID());
87       }
88     }
89     //if assembly contains compound, expand it recursively(if flag recursively is true)
90     if(recursively)
91     {
92       anIter.Initialize(Shape);
93       for(; anIter.More(); anIter.Next())
94       {
95         TDF_Label aChild = anIter.Value();
96         TDF_Label aPart;
97         if(aShapeTool->GetReferredShape(aChild, aPart))
98         {
99           Expand(Doc, aPart, recursively);
100         }
101       }
102     }
103     return Standard_True;
104   }
105   return Standard_False;
106 }
107
108 //=======================================================================
109 //function : Expand
110 //purpose  : Convert all compounds in Doc to assambly
111 //=======================================================================
112
113 Standard_Boolean XCAFDoc_Editor::Expand (const TDF_Label& Doc, const Standard_Boolean recursively)
114 {
115   Standard_Boolean result = Standard_False;
116   TDF_LabelSequence aLabels;
117   Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc);
118   aShapeTool->GetFreeShapes(aLabels);
119   for(Standard_Integer i = 1; i <= aLabels.Length(); i++)
120   {
121     TopoDS_Shape aS = aShapeTool->GetShape(aLabels(i));
122     if (!aS.IsNull() && aS.ShapeType() == TopAbs_COMPOUND && !aShapeTool->IsAssembly(aLabels(i)))
123       if (Expand(Doc, aLabels(i), recursively))
124       {
125         result = Standard_True;
126       }
127   }
128   return result;
129 }
130
131 //=======================================================================
132 //function : getParams
133 //purpose  : Get colors layers and name
134 //=======================================================================
135
136 Standard_Boolean XCAFDoc_Editor::getParams (const TDF_Label& Doc, const TDF_Label& Label,
137                                             TDF_LabelSequence& Colors, TDF_LabelSequence& Layers,
138                                             Handle(TDataStd_Name)& Name)
139 {
140   if(Doc.IsNull() || Label.IsNull())
141     return Standard_False;
142
143   Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(Doc);
144   Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc);
145
146   //get colors
147   XCAFDoc_ColorType aTypeColor = XCAFDoc_ColorGen;
148   for(;;)
149   {
150     TDF_Label aColor;
151     aColorTool->GetColor(Label, aTypeColor, aColor);
152     Colors.Append(aColor);
153     if(aTypeColor == XCAFDoc_ColorCurv)
154       break;
155     aTypeColor = (XCAFDoc_ColorType)(aTypeColor + 1);
156   }
157
158   //get layers
159   aLayerTool->GetLayers(Label, Layers);
160
161   //get name
162   Label.FindAttribute(TDataStd_Name::GetID(), Name);
163   return Standard_True;
164 }
165
166 //=======================================================================
167 //function : setParams
168 //purpose  : set colors layers and name
169 //=======================================================================
170
171 Standard_Boolean XCAFDoc_Editor::setParams (const TDF_Label& Doc, const TDF_Label& Label,
172                                             const TDF_LabelSequence& Colors, const TDF_LabelSequence& Layers,
173                                             const Handle(TDataStd_Name)& Name)
174 {
175   if(Doc.IsNull() || Label.IsNull())
176     return Standard_False;
177
178   Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(Doc);
179   Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc);
180   Handle(XCAFDoc_ShapeTool) aShapeTool = XCAFDoc_DocumentTool::ShapeTool(Doc);
181
182   //set layers
183   if(!Layers.IsEmpty())
184   {
185     for(Standard_Integer i = 1; i <= Layers.Length(); i++)
186     {
187       aLayerTool->SetLayer(Label, Layers.Value(i));
188     }
189   }
190   //set colors
191   if(!Colors.IsEmpty())
192   {
193     XCAFDoc_ColorType aTypeColor = XCAFDoc_ColorGen;
194     for(Standard_Integer i = 1; i <= Colors.Length(); i++)
195     {
196       if(!Colors.Value(i).IsNull())
197         aColorTool->SetColor(Label, Colors.Value(i), aTypeColor);
198       aTypeColor = (XCAFDoc_ColorType)(aTypeColor + 1);
199     }
200   }
201   //set name
202   if(!Name.IsNull())
203   {
204     if(Name->Get().Search("=>") < 0)
205       TDataStd_Name::Set(Label, Name->Get());
206   }
207   else
208   {
209     Standard_SStream Stream;
210     TopoDS_Shape aShape = aShapeTool->GetShape(Label);
211     TopAbs::Print(aShape.ShapeType(), Stream);
212     TCollection_AsciiString aName (Stream.str().c_str());
213     TDataStd_Name::Set(Label, TCollection_ExtendedString(aName));
214   }
215   return Standard_True;
216 }