1 // Created on: 2015-05-14
2 // Created by: data exchange team
3 // Copyright (c) 2000-2015 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 <XCAFDoc_Editor.hxx>
17 #include <XCAFDoc.hxx>
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>
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>
35 //=======================================================================
37 //purpose : Convert Shape(compound) to assambly
38 //=======================================================================
40 Standard_Boolean XCAFDoc_Editor::Expand (const TDF_Label& Doc, const TDF_Label& Shape,
41 const Standard_Boolean recursively)
43 if(Doc.IsNull() || Shape.IsNull())
44 return Standard_False;
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);
50 TopoDS_Shape aS = aShapeTool->GetShape(Shape);
51 if (!aS.IsNull() && aS.ShapeType() == TopAbs_COMPOUND && !aShapeTool->IsAssembly(Shape))
53 //convert compound to assembly(without attributes)
54 aShapeTool->Expand(Shape);
56 TDF_ChildIterator anIter(Shape, Standard_True);
57 for(; anIter.More(); anIter.Next())
59 TDF_Label aChild = anIter.Value();
61 TDF_LabelSequence aLayers;
62 TDF_LabelSequence aColors;
63 Handle(TDataStd_Name) aName;
64 getParams(Doc, aChild, aColors, aLayers, aName);
67 if(aShapeTool->GetReferredShape(aChild, aPart))
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())
74 TopLoc_Location nulloc;
76 aPart.ForgetAttribute(XCAFDoc::ShapeRefGUID());
77 if(aShapeTool->GetShape(aPart.Father()).ShapeType() == TopAbs_COMPOUND)
79 aShapeTool->SetShape(aPart, aShape);
81 aPart.ForgetAttribute(XCAFDoc_ShapeMapTool::GetID());
82 aChild.ForgetAllAttributes(Standard_False);
85 aChild.ForgetAttribute(TNaming_NamedShape::GetID());
86 aChild.ForgetAttribute(XCAFDoc_ShapeMapTool::GetID());
89 //if assembly contains compound, expand it recursively(if flag recursively is true)
92 anIter.Initialize(Shape);
93 for(; anIter.More(); anIter.Next())
95 TDF_Label aChild = anIter.Value();
97 if(aShapeTool->GetReferredShape(aChild, aPart))
99 Expand(Doc, aPart, recursively);
103 return Standard_True;
105 return Standard_False;
108 //=======================================================================
110 //purpose : Convert all compounds in Doc to assambly
111 //=======================================================================
113 Standard_Boolean XCAFDoc_Editor::Expand (const TDF_Label& Doc, const Standard_Boolean recursively)
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++)
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))
125 result = Standard_True;
131 //=======================================================================
132 //function : getParams
133 //purpose : Get colors layers and name
134 //=======================================================================
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)
140 if(Doc.IsNull() || Label.IsNull())
141 return Standard_False;
143 Handle(XCAFDoc_ColorTool) aColorTool = XCAFDoc_DocumentTool::ColorTool(Doc);
144 Handle(XCAFDoc_LayerTool) aLayerTool = XCAFDoc_DocumentTool::LayerTool(Doc);
147 XCAFDoc_ColorType aTypeColor = XCAFDoc_ColorGen;
151 aColorTool->GetColor(Label, aTypeColor, aColor);
152 Colors.Append(aColor);
153 if(aTypeColor == XCAFDoc_ColorCurv)
155 aTypeColor = (XCAFDoc_ColorType)(aTypeColor + 1);
159 aLayerTool->GetLayers(Label, Layers);
162 Label.FindAttribute(TDataStd_Name::GetID(), Name);
163 return Standard_True;
166 //=======================================================================
167 //function : setParams
168 //purpose : set colors layers and name
169 //=======================================================================
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)
175 if(Doc.IsNull() || Label.IsNull())
176 return Standard_False;
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);
183 if(!Layers.IsEmpty())
185 for(Standard_Integer i = 1; i <= Layers.Length(); i++)
187 aLayerTool->SetLayer(Label, Layers.Value(i));
191 if(!Colors.IsEmpty())
193 XCAFDoc_ColorType aTypeColor = XCAFDoc_ColorGen;
194 for(Standard_Integer i = 1; i <= Colors.Length(); i++)
196 if(!Colors.Value(i).IsNull())
197 aColorTool->SetColor(Label, Colors.Value(i), aTypeColor);
198 aTypeColor = (XCAFDoc_ColorType)(aTypeColor + 1);
204 if(Name->Get().Search("=>") < 0)
205 TDataStd_Name::Set(Label, Name->Get());
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));
215 return Standard_True;