0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / XCAFDoc / XCAFDoc_Note.cxx
1 // Copyright (c) 2017-2018 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 <XCAFDoc_Note.hxx>
15
16 #include <gp_Pln.hxx>
17 #include <Standard_GUID.hxx>
18 #include <TDataXtd_Geometry.hxx>
19 #include <TDataXtd_Plane.hxx>
20 #include <TDataXtd_Point.hxx>
21 #include <TDF_AttributeIterator.hxx>
22 #include <TDF_ChildIterator.hxx>
23 #include <TDF_Label.hxx>
24 #include <TNaming_Builder.hxx>
25 #include <TNaming_NamedShape.hxx>
26 #include <TNaming_Tool.hxx>
27 #include <XCAFDoc.hxx>
28 #include <XCAFDoc_GraphNode.hxx>
29
30 IMPLEMENT_STANDARD_RTTIEXT(XCAFDoc_Note, TDF_Attribute)
31
32 enum ChildLab
33 {
34   ChildLab_PntText = 1,
35   ChildLab_Plane,
36   ChildLab_Pnt,
37   ChildLab_Presentation
38 };
39
40 // =======================================================================
41 // function : IsMine
42 // purpose  :
43 // =======================================================================
44 Standard_Boolean
45 XCAFDoc_Note::IsMine(const TDF_Label& theLabel)
46 {
47   return !Get(theLabel).IsNull();
48 }
49
50 // =======================================================================
51 // function : XCAFDoc_Note
52 // purpose  :
53 // =======================================================================
54 XCAFDoc_Note::XCAFDoc_Note()
55 {
56 }
57
58 // =======================================================================
59 // function : Get
60 // purpose  :
61 // =======================================================================
62 Handle(XCAFDoc_Note)
63 XCAFDoc_Note::Get(const TDF_Label& theLabel)
64 {
65   Handle(XCAFDoc_Note) aNote;
66   for (TDF_AttributeIterator anIt(theLabel); anIt.More(); anIt.Next())
67   {
68     aNote = Handle(XCAFDoc_Note)::DownCast(anIt.Value());
69     if (!aNote.IsNull())
70       break;
71   }
72   return aNote;
73 }
74
75 // =======================================================================
76 // function : Set
77 // purpose  :
78 // =======================================================================
79 void
80 XCAFDoc_Note::Set(const TCollection_ExtendedString& theUserName,
81                   const TCollection_ExtendedString& theTimeStamp)
82 {
83   Backup();
84
85   myUserName = theUserName;
86   myTimeStamp = theTimeStamp;
87 }
88
89 // =======================================================================
90 // function : IsOrphan
91 // purpose  :
92 // =======================================================================
93 Standard_Boolean XCAFDoc_Note::IsOrphan() const
94 {
95   Handle(XCAFDoc_GraphNode) aFather;
96   return !Label().FindAttribute(XCAFDoc::NoteRefGUID(), aFather) ||
97          (aFather->NbChildren() == 0);
98 }
99
100 // =======================================================================
101 // function : GetObject
102 // purpose  :
103 // =======================================================================
104 Handle(XCAFNoteObjects_NoteObject) XCAFDoc_Note::GetObject() const
105 {
106   Handle(XCAFNoteObjects_NoteObject) anObj = new XCAFNoteObjects_NoteObject();
107
108   Handle(TDataXtd_Point) aPnt;
109   if (Label().FindChild(ChildLab_Pnt).FindAttribute(TDataXtd_Point::GetID(), aPnt))
110   {
111     gp_Pnt aP;
112     if (TDataXtd_Geometry::Point(aPnt->Label(), aP))
113     {
114       anObj->SetPoint(aP);
115     }
116   }
117
118   Handle(TDataXtd_Plane) aPln;
119   if (Label().FindChild(ChildLab_Plane).FindAttribute(TDataXtd_Plane::GetID(), aPln))
120   {
121     gp_Pln aP;
122     if (TDataXtd_Geometry::Plane(aPln->Label(), aP))
123     {
124       anObj->SetPlane(aP.Position().Ax2());
125     }
126   }
127
128   Handle(TDataXtd_Point) aPntText;
129   if (Label().FindChild(ChildLab_PntText).FindAttribute(TDataXtd_Point::GetID(), aPntText))
130   {
131     gp_Pnt aP;
132     if (TDataXtd_Geometry::Point(aPntText->Label(), aP))
133     {
134       anObj->SetPointText(aP);
135     }
136   }
137
138   Handle(TNaming_NamedShape) aNS;
139   TDF_Label aLPres = Label().FindChild(ChildLab_Presentation);
140   if (aLPres.FindAttribute(TNaming_NamedShape::GetID(), aNS))
141   {
142     TopoDS_Shape aPresentation = TNaming_Tool::GetShape(aNS);
143     if (!aPresentation.IsNull())
144     {
145       anObj->SetPresentation(aPresentation);
146     }
147   }
148
149   return anObj;
150 }
151
152 // =======================================================================
153 // function : SetObject
154 // purpose  :
155 // =======================================================================
156 void XCAFDoc_Note::SetObject (const Handle(XCAFNoteObjects_NoteObject)& theObject)
157 {
158   Backup();
159
160   for (TDF_ChildIterator anIter(Label()); anIter.More(); anIter.Next())
161   {
162     anIter.Value().ForgetAllAttributes();
163   }
164
165   if (theObject->HasPoint())
166   {
167     gp_Pnt aPnt1 = theObject->GetPoint();
168     TDataXtd_Point::Set (Label().FindChild (ChildLab_Pnt), aPnt1);
169   }
170
171   if (theObject->HasPlane())
172   {
173     gp_Ax2 anAx = theObject->GetPlane();
174
175     gp_Pln aP (anAx);
176     TDataXtd_Plane::Set (Label().FindChild (ChildLab_Plane), aP);
177   }
178
179   if (theObject->HasPointText())
180   {
181     gp_Pnt aPntText = theObject->GetPointText();
182     TDataXtd_Point::Set (Label().FindChild (ChildLab_PntText), aPntText);
183   }
184
185   TopoDS_Shape aPresentation = theObject->GetPresentation();
186   if (!aPresentation.IsNull())
187   {
188     TDF_Label aLPres = Label().FindChild (ChildLab_Presentation);
189     TNaming_Builder aBuilder (aLPres);
190     aBuilder.Generated (aPresentation);
191   }
192 }
193
194 // =======================================================================
195 // function : Restore
196 // purpose  :
197 // =======================================================================
198 void
199 XCAFDoc_Note::Restore(const Handle(TDF_Attribute)& theAttr)
200 {
201   myUserName = Handle(XCAFDoc_Note)::DownCast(theAttr)->myUserName;
202   myTimeStamp = Handle(XCAFDoc_Note)::DownCast(theAttr)->myTimeStamp;
203 }
204
205 // =======================================================================
206 // function : Paste
207 // purpose  :
208 // =======================================================================
209 void
210 XCAFDoc_Note::Paste(const Handle(TDF_Attribute)&       theAttrInto,
211                     const Handle(TDF_RelocationTable)& /*theRT*/) const
212 {
213   Handle(XCAFDoc_Note)::DownCast(theAttrInto)->Set(myUserName, myTimeStamp);
214 }
215
216 // =======================================================================
217 // function : Dump
218 // purpose  :
219 // =======================================================================
220 Standard_OStream&
221 XCAFDoc_Note::Dump(Standard_OStream& theOS) const
222 {
223   TDF_Attribute::Dump(theOS);
224   theOS 
225     << "Note : " 
226     << (myUserName.IsEmpty() ? myUserName : "<anonymous>")
227     << " on "
228     << (myTimeStamp.IsEmpty() ? myTimeStamp : "<unknown>")
229     ;
230   return theOS;
231 }
232
233 //=======================================================================
234 //function : DumpJson
235 //purpose  : 
236 //=======================================================================
237 void XCAFDoc_Note::DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth) const
238 {
239   OCCT_DUMP_TRANSIENT_CLASS_BEGIN (theOStream)
240
241   OCCT_DUMP_BASE_CLASS (theOStream, theDepth, TDF_Attribute)
242
243   OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myUserName)
244   OCCT_DUMP_FIELD_VALUE_STRING (theOStream, myTimeStamp)
245 }