1 // Created on: 1992-06-24
2 // Created by: Gilles DEBARBOUILLE
3 // Copyright (c) 1992-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
6 // This file is part of Open CASCADE Technology software library.
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
18 #include <TCollection_AsciiString.hxx>
20 #include <Units_Operators.hxx>
21 #include <Units_QuantitiesSequence.hxx>
22 #include <Units_Quantity.hxx>
23 #include <Units_Token.hxx>
24 #include <Units_TokensSequence.hxx>
25 #include <Units_Unit.hxx>
26 #include <Units_UnitsDictionary.hxx>
27 #include <Units_UnitSentence.hxx>
28 #include <Units_UnitsSequence.hxx>
30 //=======================================================================
31 //function : Units_UnitSentence
33 //=======================================================================
34 Units_UnitSentence::Units_UnitSentence (const Standard_CString astring)
35 : Units_Sentence(Units::LexiconUnits(), astring)
39 SetUnits(Units::DictionaryOfUnits()->Sequence());
44 //=======================================================================
45 //function : Units_UnitSentence
47 //=======================================================================
49 Units_UnitSentence::Units_UnitSentence
50 (const Standard_CString astring,
51 const Handle(Units_QuantitiesSequence)& aquantitiessequence)
52 : Units_Sentence(Units::LexiconUnits(Standard_False),astring)
56 SetUnits(aquantitiessequence);
60 //=======================================================================
63 //=======================================================================
65 void Units_UnitSentence::Analyse()
67 if(Sequence()->Length()==0)
70 Standard_Integer index;
71 TCollection_AsciiString s;
72 Handle(Units_Token) token;
73 Handle(Units_Token) previoustoken;
74 Handle(Units_Token) currenttoken;
75 Handle(Units_Token) nexttoken;
76 Handle(Units_Token) lasttoken;
77 Handle(Units_TokensSequence) sequence = Sequence();
79 currenttoken = sequence->Value(1);
81 //std::cout<<std::endl;
82 //for(int ind=1; ind<=sequence->Length(); ind++) {
83 // Handle(Units_Token) tok = sequence->Value(ind);
84 // std::cout<<tok->Word()<<" ";
86 //std::cout<<std::endl;
87 //for(ind=1; ind<=sequence->Length(); ind++) {
88 // Handle(Units_Token) tok = sequence->Value(ind);
89 // std::cout<<tok->Mean()<<" ";
91 //std::cout<<std::endl;
93 for(index=2;index<=sequence->Length();index++) {
94 previoustoken = currenttoken;
95 currenttoken = sequence->Value(index);
96 s = currenttoken->Mean();
97 if( s=="MU" || s=="U" ) {
98 if( previoustoken->Mean()=="M" || previoustoken->Mean()=="MU" ) {
99 previoustoken->Mean("M");
100 currenttoken->Mean("U");
101 currenttoken->Value(0.);
106 for(index=1;index<=sequence->Length();index++) {
107 currenttoken = sequence->Value(index);
108 s=currenttoken->Mean();
110 currenttoken->Mean("U");
111 currenttoken->Value(0.);
115 currenttoken = sequence->Value(1);
118 while(index<=sequence->Length()) {
119 previoustoken = currenttoken;
120 currenttoken = sequence->Value(index);
121 if( previoustoken->Mean()=="M" && currenttoken->Mean()=="U") {
122 token=new Units_Token("(", "S");
123 sequence->InsertBefore(index-1,token);
124 token = new Units_Token("*", "O");
125 sequence->InsertBefore(++index,token);
126 token = new Units_Token(")", "S");
127 sequence->InsertAfter(++index,token);
134 while(index <= sequence->Length()) {
135 currenttoken = sequence->Value(index);
136 if(currenttoken->Mean()=="P") {
137 if( currenttoken->Word()=="sq." || currenttoken->Word()=="cu." ) {
138 sequence->Exchange(index,index+1);
141 token = new Units_Token("**", "O");
142 sequence->InsertBefore(index,token);
148 currenttoken = sequence->Value(1);
151 while(index<=sequence->Length()) {
152 previoustoken = currenttoken;
153 currenttoken=sequence->Value(index);
154 if(currenttoken->Word()=="(") {
155 if( !( previoustoken->Mean()=="O" || previoustoken->Word()=="(" ) ) {
156 token=new Units_Token("*", "O");
157 sequence->InsertBefore(index,token);
161 else if(previoustoken->Word()==")") {
162 if( !( currenttoken->Mean()=="O" || currenttoken->Word()==")" ) ) {
163 token = new Units_Token("*", "O");
164 sequence->InsertBefore(index,token);
174 //=======================================================================
175 //function : SetUnits
177 //=======================================================================
179 void Units_UnitSentence::SetUnits
180 (const Handle(Units_QuantitiesSequence)& aquantitiessequence)
182 Standard_Integer index,jindex,kindex;
183 Standard_Boolean istheend=0;
184 Handle(Units_Quantity) quantity;
185 Handle(Units_TokensSequence) sequenceoftokens;
186 Handle(Units_Token) currenttoken;
187 Handle(Units_UnitsSequence) unitssequence;
188 Handle(Units_Unit) unit;
189 TCollection_AsciiString symbol;
191 Handle(Units_QuantitiesSequence) quantitiessequence = aquantitiessequence;
193 for(index=1; index<=quantitiessequence->Length(); index++) {
194 quantity = quantitiessequence->Value(index);
195 unitssequence=quantity->Sequence();
196 for(jindex=1; jindex<=unitssequence->Length(); jindex++) {
197 unit = unitssequence->Value(jindex);
198 sequenceoftokens = Sequence();
199 istheend = Standard_True;
200 for(kindex=1; kindex<=sequenceoftokens->Length(); kindex++) {
201 currenttoken = sequenceoftokens->Value(kindex);
202 if(currenttoken->Mean()=="U") {
203 if(currenttoken->Value()==0.) {
204 symbol = currenttoken->Word();
205 if(unit == symbol.ToCString())
206 sequenceoftokens->SetValue(kindex,unit->Token());
208 istheend = Standard_False;