cgma
OCCAttribSet.cpp
Go to the documentation of this file.
00001 //-------------------------------------------------------------------------
00002 // Filename      : OCCAttribSet.cpp
00003 //
00004 // Purpose       : Common attribute functionality for MBG
00005 //
00006 // Special Notes : 
00007 //
00008 // Creator       : Jason Kraftcheck
00009 //
00010 // Creation Date : 03/01/03
00011 //-------------------------------------------------------------------------
00012 
00013 #include "OCCAttribSet.hpp"
00014 //#include "OCCAttrib.hpp"
00015 #include "OCCQueryEngine.hpp"
00016 #include "CubitSimpleAttrib.hpp"
00017 #include "CubitFileIOWrapper.hpp"
00018 
00019 #ifndef OCC_VERSION_MINOR
00020 #include "Standard_Version.hxx"
00021 #endif
00022 
00023 #if OCC_VERSION_MINOR < 5
00024   #include "TDataStd_Shape.hxx"
00025   typedef TDataStd_Shape TDataXtd_Shape;
00026   typedef Handle_TDataStd_Shape Handle_TDataXtd_Shape;
00027 #else
00028   #include "TDataXtd_Shape.hxx"
00029 #endif
00030 
00031 #include "TDF_Label.hxx"
00032 #include "TopTools_DataMapOfShapeInteger.hxx"
00033 #include "TCollection_ExtendedString.hxx"
00034 #include "Handle_TDataStd_Name.hxx"
00035 #include "Handle_TDataStd_ExtStringArray.hxx"
00036 #include "Handle_TDataStd_RealArray.hxx"
00037 #include "Handle_TDataStd_IntegerArray.hxx"
00038 #include "TDataStd_Name.hxx"
00039 #include "TDataStd_ExtStringArray.hxx"
00040 #include "TDataStd_RealArray.hxx"
00041 #include "TDataStd_IntegerArray.hxx"
00042 #include "TopoDS_Shape.hxx"
00043 #include "TDF_ChildIterator.hxx"
00044 #include <vector>
00045 typedef std::map<int, TDF_Label>::value_type labType;
00046 
00047 void OCCAttribSet::FindShape(TopoDS_Shape& shape,
00048                              TDF_Label& aLabel,
00049                              CubitBoolean& found) 
00050 {
00051   found = CUBIT_FALSE;
00052   if(!OCCQueryEngine::instance()->OCCMap->IsBound(shape))
00053     return;
00054 
00055   int k = OCCQueryEngine::instance()->OCCMap->Find(shape);
00056   std::map<int, TDF_Label>::iterator it = 
00057             OCCQueryEngine::instance()->Shape_Label_Map->find(k);
00058   if(it != OCCQueryEngine::instance()->Shape_Label_Map->end())
00059   {
00060     aLabel = (*it).second; 
00061     found = CUBIT_TRUE;
00062   }
00063 }
00064 
00065 void OCCAttribSet::append_attribute( const CubitSimpleAttrib& csa, TopoDS_Shape& shape )
00066 {
00067   if(shape.IsNull())
00068     return;
00069 
00070   //Add attributes on child of myLabel
00071   //1. add shape attribute, first check to make sure there's no shape attribute
00072   CubitBoolean found = CUBIT_FALSE;
00073   int result = 1;
00074   TDF_Label aLabel, lab;
00075   Handle_TDataStd_Name attr_name;
00076   CubitString type = csa.string_data_list()[0];
00077   TCollection_ExtendedString cstring( (Standard_CString)type.c_str() );
00078 
00079   FindShape(shape, aLabel, found);
00080 
00081   if(!found)
00082   { 
00083     aLabel = OCCQueryEngine::instance()->mainLabel.NewChild();
00084     Handle_TDataXtd_Shape attr_shape = TDataXtd_Shape::Set(aLabel, shape);
00085     //aLabel.AddAttribute(attr_shape);
00086     //test if the attribute has been attached
00087     assert(aLabel.IsAttribute(TDataXtd_Shape::GetID()));
00088 
00089     if(!OCCQueryEngine::instance()->OCCMap->IsBound(shape))
00090     {
00091       OCCQueryEngine::instance()->iTotalTBCreated++;
00092       int k = OCCQueryEngine::instance()->iTotalTBCreated;
00093       OCCQueryEngine::instance()->OCCMap->Bind(shape, k);
00094     }
00095  
00096     int k = OCCQueryEngine::instance()->OCCMap->Find(shape);
00097     OCCQueryEngine::instance()->Shape_Label_Map->insert(labType(k, aLabel));
00098   }
00099 
00100   //2. add type attribute , below attributes are added on child lable of myLabel
00101   //First check if the attributes are already exist, if so, create new data.
00102   else
00103   {
00104     TCollection_ExtendedString name_string;
00105     for (TDF_ChildIterator it(aLabel,CUBIT_FALSE); it.More(); it.Next()) 
00106     {
00107       lab = it.Value();
00108       result = find_attribute(lab, csa);
00109       if(result != 1)
00110         break;
00111     }
00112   }
00113   //if not exist,  create new child label
00114   Handle_TDataStd_ExtStringArray attr_string;
00115   const std::vector<CubitString>& strings = csa.string_data_list();
00116   int size = strings.size()-1;
00117  
00118   if (result == 1) 
00119   {
00120     lab = aLabel.NewChild();
00121     attr_name = TDataStd_Name::Set(lab, cstring);
00122     //lab.AddAttribute(attr_name);
00123     assert(lab.IsAttribute(TDataStd_Name::GetID()));
00124   }
00125      
00126   else if(result == 3)//nothing to do
00127     return;
00128 
00129   else if(result == 2) 
00130   //adding the name strings
00131   {
00132     lab.FindAttribute(TDataStd_ExtStringArray::GetID(), attr_string);
00133     TCollection_ExtendedString name1, name2;
00134     std::vector<CubitString> new_names;
00135     int length = 0;
00136     for(int j = 1; j < (int)strings.size();j++)
00137     { 
00138       CubitString name_string = strings[j];
00139       TCollection_ExtendedString cstring(name_string.c_str(), Standard_True );
00140       bool dup = false;
00141       length = attr_string->Length() ;
00142 
00143       for(int i = 0; i < length; i++)
00144       {
00145         name1 = attr_string->Value(i);
00146         //it could be string[0] = "volume 1", string[1] = "volume_1";
00147     
00148         if(name1.IsEqual(cstring))
00149         {
00150           dup = true;
00151           break;
00152         }
00153       }
00154       if(!dup) 
00155         new_names.push_back(name_string);
00156     }
00157     if(new_names.size() > 0)
00158     {
00159       lab.ForgetAttribute(attr_string);
00160       Handle_TDataStd_ExtStringArray attr_string_new; 
00161       attr_string_new = TDataStd_ExtStringArray::Set(lab, 0, 
00162                                        length+new_names.size()-1); 
00163       for(int i = 0; i < attr_string_new->Length(); i++)
00164       {
00165         if (i < length)
00166           attr_string_new->SetValue(i, attr_string->Value(i));
00167         else
00168         {
00169           TCollection_ExtendedString cstring(new_names[i-length].c_str(), Standard_True );
00170           attr_string_new->SetValue(i, cstring);
00171         }
00172       }
00173     } 
00174     return;
00175   } 
00176   //3. add string attribute
00177   if(size > 0)
00178   {
00179     //set the length of String Array .
00180     attr_string = TDataStd_ExtStringArray::Set(lab, 0, size-1);
00181   }
00182   for(int i = 1; i < (int)strings.size(); i++)
00183   {
00184     TCollection_ExtendedString cstring(strings[i].c_str(), Standard_True );
00185     attr_string->SetValue(i-1, cstring) ;
00186   }
00187   
00188   if (size > 0)
00189   {
00190     //lab.AddAttribute(attr_string);
00191     assert(lab.IsAttribute(TDataStd_ExtStringArray::GetID()));
00192   }
00193   //4. add double attribute
00194   const std::vector<double>& doubles = csa.double_data_list();
00195   Handle_TDataStd_RealArray attr_double;
00196   size = doubles.size();
00197   if(size > 0)
00198   {
00199     //set the length of double array .
00200     attr_double = TDataStd_RealArray::Set(lab,0, size-1);
00201   } 
00202   for(int i = 0; i < size; i++)
00203     attr_double->SetValue(i, doubles[i]);
00204 
00205   if(size > 0)
00206   {
00207     //lab.AddAttribute(attr_double);
00208     assert(lab.IsAttribute(TDataStd_RealArray::GetID()));
00209   }  
00210   //5. add int attribute
00211   const std::vector<int>& ints = csa.int_data_list();
00212   Handle_TDataStd_IntegerArray attr_int;
00213   size = ints.size();
00214   if(size > 0)
00215   {
00216     //set the length of int array be 11, can be extended.
00217     attr_int = TDataStd_IntegerArray::Set(lab, 0, size-1);
00218   }
00219   for(int i = 0; i < size; i++)
00220     attr_int->SetValue(i, ints[i]);
00221 
00222   if(size > 0)
00223   {
00224     //lab.AddAttribute(attr_int);
00225     assert(lab.IsAttribute(TDataStd_IntegerArray::GetID()));
00226   }
00227 }
00228 
00229 // return 1 = not found
00230 // return 2 = found ENTITY_NAME attribute
00231 // return 3 = found the same attribute
00232 int  OCCAttribSet::find_attribute(TDF_Label child,
00233                                   const CubitSimpleAttrib& csa)
00234 {
00235   const std::vector<int>& ints = csa.int_data_list();
00236   CubitString type = csa.character_type();
00237   Standard_Boolean isMultiByte = Standard_True;
00238   TCollection_ExtendedString cstring( type.c_str(), isMultiByte );
00239   const std::vector<double>& doubles = csa.double_data_list();
00240   const std::vector<CubitString>& strings = csa.string_data_list();
00241 
00242   //find the same type attribute first
00243   Handle_TDataStd_Name attr_name;
00244   TCollection_ExtendedString old_string;
00245   if(child.FindAttribute(TDataStd_Name::GetID(), attr_name))
00246   {
00247     old_string = attr_name->Get();
00248     Standard_Boolean isMultiByte = Standard_True;
00249     char const* string = "ENTITY_NAME";
00250     TCollection_ExtendedString string2( string, isMultiByte );
00251     if(old_string != cstring)
00252       return 1;
00253 
00254     else if(cstring.IsEqual(string2))
00255       return 2;  
00256   }
00257 
00258   else
00259     return 1;
00260 
00261   //continue to compare the rest attributes.
00262   CubitBoolean is_same = CUBIT_TRUE;
00263   if(ints.size()> 0 )
00264   {
00265     Handle_TDataStd_IntegerArray attr_ints;
00266     if(child.FindAttribute(TDataStd_IntegerArray::GetID(), attr_ints) &&
00267        attr_ints->Length() == (int)ints.size())
00268     {
00269       for(int i = 0; i < (int)ints.size(); i++)
00270         if(attr_ints->Value(i) != ints[i])
00271         {
00272           is_same = CUBIT_FALSE;
00273           break;
00274         }
00275     }
00276   }
00277   if(!is_same)
00278     return 1;
00279 
00280   if(doubles.size() > 0 )
00281   {
00282     Handle_TDataStd_RealArray attr_doubles;
00283     if(child.FindAttribute(TDataStd_RealArray::GetID(), attr_doubles) &&
00284        attr_doubles->Length() == (int)doubles.size())
00285     {
00286       for(int i = 0; i < (int)doubles.size(); i++)
00287         if(attr_doubles->Value(i) != doubles[i])
00288         {
00289           is_same = CUBIT_FALSE;
00290           break;
00291         }
00292     }
00293   }
00294 
00295   if(!is_same)
00296     return 1;
00297 
00298   if(strings.size() > 0 )
00299   {
00300     Handle_TDataStd_ExtStringArray attr_strings;
00301     if(child.FindAttribute(TDataStd_ExtStringArray::GetID(), attr_strings) &&
00302        attr_strings->Length() == (int)strings.size()-1)
00303     {
00304       for(int i = 1; i < (int)strings.size(); i++)
00305       {
00306         CubitString astring = strings[i];
00307         TCollection_ExtendedString string( astring.c_str() , Standard_True);
00308         if(attr_strings->Value(i-1) != string)
00309         {
00310           is_same = CUBIT_FALSE;
00311           break;
00312         }
00313       }
00314     }
00315     if(!is_same)
00316       return 1;
00317   }
00318   return 3  ;
00319 }
00320 
00321 void OCCAttribSet::remove_attribute( const CubitSimpleAttrib& csa)
00322 {
00323   CubitString type = csa.character_type();
00324   if(type.length() == 0)
00325       return;
00326 
00327   TDF_Label myLabel;
00328   for (TDF_ChildIterator it(OCCQueryEngine::instance()->mainLabel, CUBIT_FALSE); it.More(); it.Next())
00329   {
00330     //find the same type attribute first
00331     myLabel = it.Value();
00332 
00333     //forget csa attribute from the document
00334     for (TDF_ChildIterator it1(myLabel, CUBIT_FALSE); it1.More(); it1.Next())
00335     {
00336       //find the same type attribute first
00337       TDF_Label child = it1.Value();
00338 
00339       int found = find_attribute(child, csa);
00340 
00341       if(found == 3)
00342         child.ForgetAllAttributes(false );
00343     }
00344   }
00345 }
00346 
00347 void OCCAttribSet::remove_attribute( TopoDS_Shape& shape)
00348 {
00349   CubitBoolean found = CUBIT_FALSE;
00350   TDF_Label myLabel;
00351 
00352   FindShape(shape, myLabel, found);
00353 
00354   if(!found)
00355     return;
00356  
00357   myLabel.ForgetAllAttributes(false);
00358 /*
00359   if(OCCQueryEngine::instance()->OCCMap->IsBound(shape)) 
00360   {
00361     int k = OCCQueryEngine::instance()->OCCMap->Find(shape);
00362     OCCQueryEngine::instance()->Shape_Label_Map->erase(k);
00363   }
00364 */
00365 }
00366 
00367 void OCCAttribSet::remove_attribute( const CubitSimpleAttrib& csa,
00368                                      TopoDS_Shape& shape)
00369 {
00370   CubitBoolean found = CUBIT_FALSE;
00371   TDF_Label myLabel;
00372 
00373   FindShape(shape, myLabel, found);
00374 
00375   if(!found)
00376     return;
00377 
00378   int num_children = 0;
00379   int num_found = 0;
00380   for (TDF_ChildIterator it1(myLabel, CUBIT_FALSE); it1.More(); it1.Next())
00381   {
00382     //find the same type attribute first
00383     TDF_Label child = it1.Value();
00384     if (child.NbAttributes() == 0)
00385       continue;
00386 
00387     num_children ++;
00388 
00389     if(csa.isEmpty())
00390     {
00391       //only name attributes can't be forget here.
00392       Handle_TDataStd_Name attr_name;
00393       if(child.FindAttribute(TDataStd_Name::GetID(), attr_name))
00394       {
00395         TCollection_ExtendedString old_string = attr_name->Get();
00396         char const* string1 = "ENTITY_NAME";
00397         Standard_Boolean isMultiByte = Standard_True;
00398         TCollection_ExtendedString cstring( string1, isMultiByte );
00399         if( old_string == cstring)
00400           continue;
00401       }
00402       child.ForgetAllAttributes(false );
00403       num_found ++;
00404     }
00405 
00406     else
00407     {
00408       int found = find_attribute(child, csa);
00409 
00410       if(found == 3)
00411       {
00412         child.ForgetAllAttributes(false );
00413         num_found ++;
00414       } 
00415     }
00416   }
00417 /*
00418   if(num_children == num_found)
00419   {
00420     if(OCCQueryEngine::instance()->OCCMap->IsBound(shape))
00421     {
00422       int k = OCCQueryEngine::instance()->OCCMap->Find(shape);
00423       OCCQueryEngine::instance()->Shape_Label_Map->erase(k);
00424     }
00425   }
00426 */
00427 }
00428 
00429 void OCCAttribSet::get_attributes(TDF_Label &lab,
00430                                   DLIList<CubitSimpleAttrib>& list)
00431 {
00432   std::vector<CubitString> strings;
00433   CubitString string;
00434   std::vector<double> doubles;
00435   std::vector<int> ints;
00436 
00437   Handle_TDataStd_Name attr_name;
00438   TCollection_ExtendedString name_string;
00439   Handle_TDataStd_ExtStringArray attr_string;
00440   Handle_TDataStd_RealArray attr_doubles;
00441   Handle_TDataStd_IntegerArray attr_ints;
00442 
00443   if(lab.FindAttribute(TDataStd_Name::GetID(), attr_name))
00444   {
00445     name_string = attr_name->Get();
00446     int length = name_string.Length();
00447     std::vector<char> temp_type(length+1);
00448     for (int i = 1 ;  i <= length; i++)
00449     {
00450       Standard_ExtCharacter c = name_string.Value(i);
00451       temp_type[i-1] = ToCharacter(c);
00452     }
00453     temp_type[length] = '\0';
00454     string = CubitString(&temp_type[0]);
00455     strings.push_back(string);
00456   }
00457   if(lab.FindAttribute(TDataStd_ExtStringArray::GetID(), attr_string))
00458   {
00459     int length = attr_string->Length();
00460     for (int i = 0 ;  i < length; i++)
00461     {
00462       name_string = attr_string->Value(i);
00463       int length2 = name_string.Length();
00464       if (length2 == 0)
00465         continue;
00466       std::vector<char> temp_string(length2+1);
00467       for(int j = 1; j <= length2; j ++)
00468       {
00469         Standard_ExtCharacter c = name_string.Value(j);
00470         temp_string[j-1] = ToCharacter(c);
00471       }
00472       temp_string[length2] = '\0';
00473       const char *s = &temp_string[0];
00474       string = CubitString(s);
00475       strings.push_back(string);
00476     }
00477   }
00478   if(lab.FindAttribute(TDataStd_RealArray::GetID(), attr_doubles))
00479   {
00480     for(int i = 0; i < attr_doubles->Length(); i++)
00481     {
00482       double d = attr_doubles->Value(i);
00483       doubles.push_back(d);
00484     }
00485   }
00486   if(lab.FindAttribute(TDataStd_IntegerArray::GetID(), attr_ints))
00487   {
00488     for(int i = 0; i < attr_ints->Length(); i++)
00489     {
00490       int j = attr_ints->Value(i);
00491       ints.push_back(j);
00492     }
00493   }
00494   if(strings.size() > 0 || doubles.size() || ints.size())
00495   {
00496     list.append(CubitSimpleAttrib(&strings, &doubles, &ints));
00497   }
00498 }
00499 
00500 CubitStatus OCCAttribSet::get_attributes( TopoDS_Shape& shape,
00501                                           DLIList<CubitSimpleAttrib>& list)
00502 {
00503   CubitBoolean found = CUBIT_FALSE;
00504   TDF_Label myLabel;
00505 
00506   FindShape(shape, myLabel, found);
00507 
00508   if(!found)
00509     return CUBIT_FAILURE;
00510 
00511   TDF_Label lab;
00512   for (TDF_ChildIterator it(myLabel,CUBIT_FALSE); it.More(); it.Next())
00513   {
00514     lab = it.Value();
00515     get_attributes(lab, list);
00516   }     
00517   return CUBIT_SUCCESS;
00518 }
00519 
00520 CubitStatus OCCAttribSet::get_attributes( const CubitString& name,
00521                                           TopoDS_Shape& shape,
00522                                     DLIList<CubitSimpleAttrib>& list )
00523 {
00524   CubitBoolean found = CUBIT_FALSE;
00525   TDF_Label aLabel;
00526 
00527   FindShape(shape, aLabel, found);
00528 
00529   if(!found)
00530     return CUBIT_FAILURE;
00531 
00532   Handle_TDataStd_Name attr_name;
00533   TCollection_ExtendedString cstring( (Standard_CString)name.c_str() );
00534   TDF_Label lab;
00535   for (TDF_ChildIterator it(aLabel,CUBIT_FALSE); it.More(); it.Next())
00536   {
00537     lab = it.Value();
00538 
00539     //check if the type attribute is the same, disable it
00540     TCollection_ExtendedString old_string;
00541     if(lab.FindAttribute(TDataStd_Name::GetID(), attr_name))
00542     {
00543       old_string = attr_name->Get();
00544 
00545       if(old_string == cstring)
00546       {
00547         get_attributes(lab, list);
00548         break;
00549       }
00550     }
00551   }
00552  
00553   return CUBIT_SUCCESS;
00554 }
00555 
00556 
00557 int OCCAttribSet::attribute_count() 
00558 {
00559   int count = 0;
00560   
00561   return count;
00562 }
00563 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines