0023022: This is desirable to access OpenGl extensions and core API (1.2+) in one...
[occt.git] / src / XmlMNaming / XmlMNaming_Shape1.cxx
1 // File:      XmlMNaming_Shape1.cxx
2 // Created:   01.08.01 18:26:53
3 // Author:    Alexander GRIGORIEV
4 // Copyright: Open Cascade 2001
5 // History:
6
7 #include <XmlMNaming_Shape1.ixx>
8
9 #include <XmlObjMgt.hxx>
10 #include <TCollection_AsciiString.hxx>
11 #include <TopoDS_Vertex.hxx>
12 #include <TopoDS.hxx>
13 #include <gp_Pnt.hxx>
14 #include <BRep_Tool.hxx>
15
16 #include <stdio.h>
17
18 IMPLEMENT_DOMSTRING (TShapeString,   "tshape")
19 IMPLEMENT_DOMSTRING (LocationString, "location")
20
21 IMPLEMENT_DOMSTRING (XCoordString, "x")
22 IMPLEMENT_DOMSTRING (YCoordString, "y")
23 IMPLEMENT_DOMSTRING (ZCoordString, "z")
24
25 //=======================================================================
26 //function : XmlMNaming_Shape1
27 //purpose  : 
28 //=======================================================================
29 XmlMNaming_Shape1::XmlMNaming_Shape1 (XmlObjMgt_Document& theDoc)
30      : myTShapeID (0),
31        myLocID    (0),
32        myOrientation (TopAbs_FORWARD)
33 {
34   myElement = theDoc.createElement(XmlObjMgt_DOMString("shape"));
35 }
36
37 //=======================================================================
38 //function : XmlMNaming_Shape1
39 //purpose  : 
40 //=======================================================================
41 XmlMNaming_Shape1::XmlMNaming_Shape1 (const XmlObjMgt_Element& theEl)
42      : myElement(theEl),
43        myTShapeID (0),
44        myLocID    (0),
45        myOrientation (TopAbs_FORWARD)
46 {
47   if (myElement != NULL)
48   {
49     myElement.getAttribute(::LocationString()).GetInteger (myLocID);
50     XmlObjMgt_DOMString aString = myElement.getAttribute(::TShapeString());
51     const char * aPtr = aString.GetString();
52     switch (* aPtr) {
53     case '+' : myOrientation = TopAbs_FORWARD;  break;
54     case '-' : myOrientation = TopAbs_REVERSED; break;
55     case 'i' : myOrientation = TopAbs_INTERNAL; break;
56     case 'e' : myOrientation = TopAbs_EXTERNAL; break;
57     default:       Standard_DomainError::Raise
58       ("XmlMNaming_Shape1; orientation value without enum term equivalence");
59     }
60     Standard_CString anIntPtr = (Standard_CString) &aPtr[1];
61     if (XmlObjMgt::GetInteger (anIntPtr, myTShapeID) == Standard_False)
62       Standard_DomainError::Raise
63         ("XmlMNaming_Shape1; tshape value cannot be initialised by integer");
64   }
65 }
66
67 //=======================================================================
68 //function : Element
69 //purpose  : 
70 //=======================================================================
71 const XmlObjMgt_Element& XmlMNaming_Shape1::Element() const
72 {
73   return myElement;
74 }
75
76 //=======================================================================
77 //function : Element
78 //purpose  : 
79 //=======================================================================
80 XmlObjMgt_Element& XmlMNaming_Shape1::Element()
81 {
82   return myElement;
83 }
84
85 //=======================================================================
86 //function : TShapeId
87 //purpose  : 
88 //=======================================================================
89 Standard_Integer XmlMNaming_Shape1::TShapeId() const 
90 {
91   return myTShapeID;
92 }
93
94 //=======================================================================
95 //function : LocId
96 //purpose  : 
97 //=======================================================================
98 Standard_Integer XmlMNaming_Shape1::LocId() const 
99 {
100   return myLocID;
101 }
102
103 //=======================================================================
104 //function : Orientation
105 //purpose  : 
106 //=======================================================================
107 TopAbs_Orientation  XmlMNaming_Shape1::Orientation() const 
108 {
109   return myOrientation;
110 }
111
112 //=======================================================================
113 //function : SetShape
114 //purpose  : 
115 //=======================================================================
116 void  XmlMNaming_Shape1::SetShape (const Standard_Integer       theID,
117                                    const Standard_Integer       theLocID,
118                                    const TopAbs_Orientation     theOrient)
119 {
120   myTShapeID    = theID;
121   myLocID       = theLocID;
122   myOrientation = theOrient;
123
124   char aBuffer[16], anOr;
125   
126   switch (theOrient) {
127   case TopAbs_FORWARD   : anOr = '+'; break;
128   case TopAbs_REVERSED  : anOr = '-'; break;
129   case TopAbs_INTERNAL  : anOr = 'i'; break;
130   case TopAbs_EXTERNAL  : anOr = 'e'; break;
131   default               : anOr = '\0';
132   }
133   sprintf (aBuffer, "%c%i", anOr, theID);
134   Element().setAttribute (::TShapeString(), aBuffer);
135   if (theLocID > 0)
136     Element().setAttribute (::LocationString(), theLocID);
137 }
138
139 //=======================================================================
140 //function : SetVertex
141 //purpose  : 
142 //=======================================================================
143 void XmlMNaming_Shape1::SetVertex (const TopoDS_Shape& theVertex)
144 {
145   TopoDS_Vertex aV = TopoDS::Vertex(theVertex);
146   gp_Pnt aPos = BRep_Tool::Pnt(aV);
147
148   char buf [16];
149   sprintf (buf, "%.8g", aPos.X());
150   Element().setAttribute (::XCoordString(), buf);
151
152   sprintf (buf, "%.8g", aPos.Y());
153   Element().setAttribute (::YCoordString(), buf);
154
155   sprintf (buf, "%.8g", aPos.Z());
156   Element().setAttribute (::ZCoordString(), buf);
157 }