b311480e |
1 | // Created on: 1992-06-24 |
2 | // Created by: Gilles DEBARBOUILLE |
3 | // Copyright (c) 1992-1999 Matra Datavision |
4 | // Copyright (c) 1999-2012 OPEN CASCADE SAS |
5 | // |
6 | // The content of this file is subject to the Open CASCADE Technology Public |
7 | // License Version 6.5 (the "License"). You may not use the content of this file |
8 | // except in compliance with the License. Please obtain a copy of the License |
9 | // at http://www.opencascade.org and read it completely before using this file. |
10 | // |
11 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
12 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
13 | // |
14 | // The Original Code and all software distributed under the License is |
15 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
16 | // Initial Developer hereby disclaims all such warranties, including without |
17 | // limitation, any warranties of merchantability, fitness for a particular |
18 | // purpose or non-infringement. Please see the License for the specific terms |
19 | // and conditions governing the rights and limitations under the License. |
20 | |
7fd59977 |
21 | |
22 | |
23 | #include <Units_UnitSentence.ixx> |
24 | #include <Units_UnitsDictionary.hxx> |
25 | #include <Units_Quantity.hxx> |
26 | #include <Units.hxx> |
27 | #include <Units.hxx> |
28 | #include <Units_Token.hxx> |
29 | #include <Units_TokensSequence.hxx> |
30 | #include <Units_UnitsSequence.hxx> |
31 | #include <Units_Unit.hxx> |
32 | #include <Units_QuantitiesSequence.hxx> |
33 | #include <TCollection_AsciiString.hxx> |
34 | #include <Units_Operators.hxx> |
35 | |
36 | //======================================================================= |
37 | //function : Units_UnitSentence |
38 | //purpose : |
39 | //======================================================================= |
40 | |
41 | Units_UnitSentence::Units_UnitSentence (const Standard_CString astring) |
42 | : Units_Sentence(Units::LexiconUnits(), astring) |
43 | { |
44 | Analyse(); |
45 | SetConstants(); |
46 | SetUnits(Units::DictionaryOfUnits()->Sequence()); |
47 | |
48 | } |
49 | |
50 | |
51 | //======================================================================= |
52 | //function : Units_UnitSentence |
53 | //purpose : |
54 | //======================================================================= |
55 | |
56 | Units_UnitSentence::Units_UnitSentence |
57 | (const Standard_CString astring, |
58 | const Handle(Units_QuantitiesSequence)& aquantitiessequence) |
59 | : Units_Sentence(Units::LexiconUnits(Standard_False),astring) |
60 | { |
61 | Analyse(); |
62 | SetConstants(); |
63 | SetUnits(aquantitiessequence); |
64 | } |
65 | |
66 | |
67 | //======================================================================= |
68 | //function : Analyse |
69 | //purpose : |
70 | //======================================================================= |
71 | |
72 | void Units_UnitSentence::Analyse() |
73 | { |
74 | if(Sequence()->Length()==0) |
75 | return; |
76 | |
77 | Standard_Integer index; |
78 | TCollection_AsciiString s; |
79 | Handle(Units_Token) token; |
80 | Handle(Units_Token) previoustoken; |
81 | Handle(Units_Token) currenttoken; |
82 | Handle(Units_Token) nexttoken; |
83 | Handle(Units_Token) lasttoken; |
84 | Handle(Units_TokensSequence) sequence = Sequence(); |
85 | |
86 | currenttoken = sequence->Value(1); |
87 | |
88 | //cout<<endl; |
89 | //for(int ind=1; ind<=sequence->Length(); ind++) { |
90 | // Handle(Units_Token) tok = sequence->Value(ind); |
91 | // cout<<tok->Word()<<" "; |
92 | //} |
93 | //cout<<endl; |
94 | //for(ind=1; ind<=sequence->Length(); ind++) { |
95 | // Handle(Units_Token) tok = sequence->Value(ind); |
96 | // cout<<tok->Mean()<<" "; |
97 | //} |
98 | //cout<<endl; |
99 | |
100 | for(index=2;index<=sequence->Length();index++) { |
101 | previoustoken = currenttoken; |
102 | currenttoken = sequence->Value(index); |
103 | s = currenttoken->Mean(); |
104 | if( s=="MU" || s=="U" ) { |
105 | if( previoustoken->Mean()=="M" || previoustoken->Mean()=="MU" ) { |
106 | previoustoken->Mean("M"); |
107 | currenttoken->Mean("U"); |
108 | currenttoken->Value(0.); |
109 | } |
110 | } |
111 | } |
112 | |
113 | for(index=1;index<=sequence->Length();index++) { |
114 | currenttoken = sequence->Value(index); |
115 | s=currenttoken->Mean(); |
116 | if(s=="MU") { |
117 | currenttoken->Mean("U"); |
118 | currenttoken->Value(0.); |
119 | } |
120 | } |
121 | |
122 | currenttoken = sequence->Value(1); |
123 | index = 2; |
124 | |
125 | while(index<=sequence->Length()) { |
126 | previoustoken = currenttoken; |
127 | currenttoken = sequence->Value(index); |
128 | if( previoustoken->Mean()=="M" && currenttoken->Mean()=="U") { |
129 | token=new Units_Token("(", "S"); |
130 | sequence->InsertBefore(index-1,token); |
131 | token = new Units_Token("*", "O"); |
132 | sequence->InsertBefore(++index,token); |
133 | token = new Units_Token(")", "S"); |
134 | sequence->InsertAfter(++index,token); |
135 | index++; |
136 | } |
137 | index++; |
138 | } |
139 | |
140 | index = 1; |
141 | while(index <= sequence->Length()) { |
142 | currenttoken = sequence->Value(index); |
143 | if(currenttoken->Mean()=="P") { |
144 | if( currenttoken->Word()=="sq." || currenttoken->Word()=="cu." ) { |
145 | sequence->Exchange(index,index+1); |
146 | index++; |
147 | } |
148 | token = new Units_Token("**", "O"); |
149 | sequence->InsertBefore(index,token); |
150 | index++; |
151 | } |
152 | index++; |
153 | } |
154 | |
155 | currenttoken = sequence->Value(1); |
156 | index = 2; |
157 | |
158 | while(index<=sequence->Length()) { |
159 | previoustoken = currenttoken; |
160 | currenttoken=sequence->Value(index); |
161 | if(currenttoken->Word()=="(") { |
162 | if( !( previoustoken->Mean()=="O" || previoustoken->Word()=="(" ) ) { |
163 | token=new Units_Token("*", "O"); |
164 | sequence->InsertBefore(index,token); |
165 | index++; |
166 | } |
167 | } |
168 | else if(previoustoken->Word()==")") { |
169 | if( !( currenttoken->Mean()=="O" || currenttoken->Word()==")" ) ) { |
170 | token = new Units_Token("*", "O"); |
171 | sequence->InsertBefore(index,token); |
172 | index++; |
173 | } |
174 | } |
175 | index++; |
176 | } |
177 | |
178 | } |
179 | |
180 | |
181 | //======================================================================= |
182 | //function : SetUnits |
183 | //purpose : |
184 | //======================================================================= |
185 | |
186 | void Units_UnitSentence::SetUnits |
187 | (const Handle(Units_QuantitiesSequence)& aquantitiessequence) |
188 | { |
189 | Standard_Integer index,jindex,kindex; |
190 | Standard_Boolean istheend=0; |
191 | Handle(Units_Quantity) quantity; |
192 | Handle(Units_TokensSequence) sequenceoftokens; |
193 | Handle(Units_Token) currenttoken; |
194 | Handle(Units_UnitsSequence) unitssequence; |
195 | Handle(Units_Unit) unit; |
196 | TCollection_AsciiString symbol; |
197 | |
198 | Handle(Units_QuantitiesSequence) quantitiessequence = aquantitiessequence; |
199 | |
200 | for(index=1; index<=quantitiessequence->Length(); index++) { |
201 | quantity = quantitiessequence->Value(index); |
202 | unitssequence=quantity->Sequence(); |
203 | for(jindex=1; jindex<=unitssequence->Length(); jindex++) { |
204 | unit = unitssequence->Value(jindex); |
205 | sequenceoftokens = Sequence(); |
206 | istheend = Standard_True; |
207 | for(kindex=1; kindex<=sequenceoftokens->Length(); kindex++) { |
208 | currenttoken = sequenceoftokens->Value(kindex); |
209 | if(currenttoken->Mean()=="U") { |
210 | if(currenttoken->Value()==0.) { |
211 | symbol = currenttoken->Word(); |
212 | if(unit == symbol.ToCString()) |
213 | sequenceoftokens->SetValue(kindex,unit->Token()); |
214 | else |
215 | istheend = Standard_False; |
216 | } |
217 | } |
218 | } |
219 | if(istheend) |
220 | break; |
221 | } |
222 | if(istheend) |
223 | break; |
224 | } |
225 | } |