opennurbs_3dm_properties.cpp
Go to the documentation of this file.
00001 /* $NoKeywords: $ */
00002 /*
00003 //
00004 // Copyright (c) 1993-2012 Robert McNeel & Associates. All rights reserved.
00005 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
00006 // McNeel & Associates.
00007 //
00008 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
00009 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
00010 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
00011 //                              
00012 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
00013 //
00015 */
00016 
00017 #include "pcl/surface/3rdparty/opennurbs/opennurbs.h"
00018 
00020 //
00021 // ON_3dmRevisionHistory
00022 //
00023 
00024 ON_3dmRevisionHistory::ON_3dmRevisionHistory() : m_revision_count(0)
00025 {
00026   Default();
00027 }
00028 
00029 ON_3dmRevisionHistory::~ON_3dmRevisionHistory()
00030 {
00031   m_sCreatedBy.Destroy();
00032   m_sLastEditedBy.Destroy();
00033 }
00034 
00035 void ON_3dmRevisionHistory::Default()
00036 {
00037   m_sCreatedBy.Destroy();
00038   m_sLastEditedBy.Destroy();
00039   memset( &m_create_time,    0, sizeof(m_create_time) );
00040   memset( &m_last_edit_time, 0, sizeof(m_last_edit_time) );
00041   m_revision_count = 0;
00042 }
00043 
00044 static int ON_CompareRevisionHistoryTime( const struct tm* time0, const struct tm* time1 )
00045 {
00046   if ( 0 == time0 || 0 == time1 )
00047   {
00048     if ( 0 != time0 )
00049       return 1;
00050     if ( 0 != time1 )
00051       return -1;
00052     return 0;
00053   }
00054 
00055   if (time0->tm_year < time1->tm_year)
00056     return -1;
00057   if (time0->tm_year > time1->tm_year)
00058     return 1;
00059 
00060   if (time0->tm_mon < time1->tm_mon)
00061     return -1;
00062   if (time0->tm_mon > time1->tm_mon)
00063     return 1;
00064 
00065   if (time0->tm_mday < time1->tm_mday)
00066     return -1;
00067   if (time0->tm_mday > time1->tm_mday)
00068     return 1;
00069 
00070   if (time0->tm_hour < time1->tm_hour)
00071     return -1;
00072   if (time0->tm_hour > time1->tm_hour)
00073     return 1;
00074 
00075   if (time0->tm_min < time1->tm_min)
00076     return -1;
00077   if (time0->tm_min > time1->tm_min)
00078     return 1;
00079 
00080   if (time0->tm_sec < time1->tm_sec)
00081     return -1;
00082   if (time0->tm_sec > time1->tm_sec)
00083     return 1;
00084 
00085   return 0;
00086 }
00087 
00088 bool ON_3dmRevisionHistory::CreateTimeIsSet() const
00089 {
00090   struct tm jan_1_1970;
00091   memset(&jan_1_1970,0,sizeof(jan_1_1970));
00092   jan_1_1970.tm_mday = 1;    /* day of the month - [1,31] */
00093   jan_1_1970.tm_year = 70;   /* years since 1900 */
00094   return ( ON_CompareRevisionHistoryTime(&jan_1_1970,&m_create_time) >= 0 );
00095 }
00096 /*
00097 Returns:
00098   true 
00099     if m_last_edit_time is >= January 1, 1970
00100 */
00101 bool ON_3dmRevisionHistory::LastEditedTimeIsSet() const
00102 {
00103   struct tm jan_1_1970;
00104   memset(&jan_1_1970,0,sizeof(jan_1_1970));
00105   jan_1_1970.tm_mday = 1;    /* day of the month - [1,31] */
00106   jan_1_1970.tm_year = 70;   /* years since 1900 */
00107   return ( ON_CompareRevisionHistoryTime(&jan_1_1970,&m_last_edit_time) >= 0 );
00108 }
00109 
00110 
00111 ON_BOOL32 ON_3dmRevisionHistory::IsValid() const
00112 {
00113   return (     LastEditedTimeIsSet() 
00114             && ON_CompareRevisionHistoryTime(&m_create_time, &m_last_edit_time) <= 0
00115          );
00116 }
00117 
00118 int ON_3dmRevisionHistory::NewRevision()
00119 {
00120   struct tm current_time;
00121   memset(&current_time,0,sizeof(current_time));
00122   {
00123     time_t gmt = time(0);
00124     const struct tm* t = gmtime(&gmt);
00125     if ( t )
00126       current_time = *t;
00127   }
00128   m_last_edit_time = current_time;
00129 
00130 #if defined(ON_OS_WINDOWS)  
00131   // use Windows ::GetUserNameW() to get current user name
00132   wchar_t current_user[512];
00133   memset( current_user, 0, sizeof(current_user) );
00134   ULONG len = 510;
00135   if( !::GetUserNameW(current_user,&len) )
00136     current_user[0] = 0;
00137   m_sLastEditedBy = current_user;
00138 #endif
00139 
00140   if ( m_revision_count <= 0 ) 
00141   {
00142     m_revision_count = 0;
00143     m_sCreatedBy = m_sLastEditedBy;
00144     m_create_time = current_time;
00145   };
00146 
00147   m_revision_count++;
00148 
00149   return m_revision_count;
00150 }
00151 
00152 
00153 ON_BOOL32 ON_3dmRevisionHistory::Write( ON_BinaryArchive& file ) const
00154 {
00155   ON_BOOL32 rc = file.Write3dmChunkVersion( 1, 0 );
00156   if (rc) rc = file.WriteString( m_sCreatedBy );
00157   if (rc) rc = file.WriteTime( m_create_time );
00158   if (rc) rc = file.WriteString( m_sLastEditedBy );
00159   if (rc) rc = file.WriteTime(m_last_edit_time );
00160   if (rc) rc = file.WriteInt( m_revision_count );
00161   return rc;
00162 }
00163 
00164 ON_BOOL32 ON_3dmRevisionHistory::Read( ON_BinaryArchive& file )
00165 {
00166   Default();
00167   int major_version = 0;
00168   int minor_version = 0;
00169   ON_BOOL32 rc = file.Read3dmChunkVersion( &major_version, &minor_version );
00170   if ( rc && major_version == 1 ) {
00171     rc = file.ReadString( m_sCreatedBy );
00172     if (rc) rc = file.ReadTime( m_create_time );
00173     if (rc) rc = file.ReadString( m_sLastEditedBy );
00174     if (rc) rc = file.ReadTime(m_last_edit_time );
00175     if (rc) rc = file.ReadInt( &m_revision_count );
00176   }
00177   return rc;
00178 }
00179 
00180 void ON_3dmRevisionHistory::Dump( ON_TextLog& dump ) const
00181 {
00182   const wchar_t* ws = m_sCreatedBy;
00183   if ( !ws ) ws = L"";
00184   dump.Print("Created by: %ls\n", ws );
00185   dump.Print("Created on: "); dump.PrintTime(m_create_time); dump.Print("\n");
00186 
00187   
00188   ws = m_sLastEditedBy;
00189   if ( !ws ) ws = L"";
00190   dump.Print("Last edited by: %ls\n", ws );
00191   dump.Print("Last edited on: "); dump.PrintTime(m_last_edit_time); dump.Print("\n");
00192 
00193   dump.Print("Revision count: %d\n",m_revision_count);
00194 }
00195 
00197 //
00198 // ON_3dmNotes
00199 //
00200 
00201 ON_3dmNotes::ON_3dmNotes()
00202             : m_bVisible(0), 
00203               m_bHTML(0),
00204               m_window_left(0),
00205               m_window_top(0),
00206               m_window_right(0),
00207               m_window_bottom(0)
00208 {}
00209 
00210 ON_3dmNotes::ON_3dmNotes( const ON_3dmNotes& src )
00211             : m_bVisible(0), 
00212               m_bHTML(0),
00213               m_window_left(0),
00214               m_window_top(0),
00215               m_window_right(0),
00216               m_window_bottom(0)
00217 {
00218   *this = src;
00219 }
00220 
00221 ON_3dmNotes::~ON_3dmNotes()
00222 {
00223   m_notes.Destroy();
00224 }
00225 
00226 ON_3dmNotes& ON_3dmNotes::operator=(const ON_3dmNotes& src)
00227 {
00228   if ( this != &src ) {
00229     m_notes     = src.m_notes;
00230     m_bVisible  = src.m_bVisible;
00231     m_bHTML     = src.m_bHTML;
00232     m_window_left   = src.m_window_left;
00233     m_window_top    = src.m_window_top;
00234     m_window_right  = src.m_window_right;
00235     m_window_bottom = src.m_window_bottom;
00236   }
00237   return *this;
00238 }
00239 
00240 void ON_3dmNotes::Default()
00241 {
00242   m_notes.Destroy();
00243   m_bVisible = 0;
00244   m_bHTML = 0;
00245   m_window_left = 0;
00246   m_window_top = 0;
00247   m_window_right = 0;
00248   m_window_bottom = 0;
00249 }
00250 
00251 ON_BOOL32 ON_3dmNotes::IsValid() const
00252 {
00253   return m_notes.IsEmpty() ? false : true;
00254 }
00255 
00256 ON_BOOL32 ON_3dmNotes::Read( ON_BinaryArchive& file )
00257 {
00258   Default();
00259   int major_version = 0;
00260   int minor_version = 0;
00261   ON_BOOL32 rc = file.Read3dmChunkVersion( &major_version, &minor_version );
00262   if ( rc && major_version == 1 ) {
00263     m_notes.Destroy();
00264     rc = file.ReadInt( &m_bHTML );
00265     if ( rc ) rc = file.ReadString( m_notes );
00266     if ( rc ) rc = file.ReadInt( &m_bVisible );
00267     if ( rc ) rc = file.ReadInt( &m_window_left );
00268     if ( rc ) rc = file.ReadInt( &m_window_top );
00269     if ( rc ) rc = file.ReadInt( &m_window_right );
00270     if ( rc ) rc = file.ReadInt( &m_window_bottom );
00271   }
00272   return rc;
00273 }
00274 
00275 ON_BOOL32 ON_3dmNotes::Write( ON_BinaryArchive& file ) const
00276 {
00277   ON_BOOL32 rc = file.Write3dmChunkVersion(1,0);
00278   if ( rc ) rc = file.WriteInt( m_bHTML );
00279   if ( rc ) rc = file.WriteString( m_notes );
00280   if ( rc ) rc = file.WriteInt( m_bVisible );
00281   if ( rc ) rc = file.WriteInt( m_window_left );
00282   if ( rc ) rc = file.WriteInt( m_window_top );
00283   if ( rc ) rc = file.WriteInt( m_window_right );
00284   if ( rc ) rc = file.WriteInt( m_window_bottom );
00285   return rc;
00286 }
00287 
00288 void ON_3dmNotes::Dump(ON_TextLog& dump) const
00289 {
00290   const wchar_t* s = m_notes;
00291   if ( s )
00292     dump.PrintWrappedText(s);
00293   dump.Print("\n");
00294 }
00295 
00296 
00298 //
00299 // ON_3dmApplication
00300 //
00301 ON_3dmApplication::ON_3dmApplication()
00302 {
00303 }
00304 
00305 ON_3dmApplication::ON_3dmApplication( const ON_3dmApplication& src)
00306 {
00307   m_application_name = src.m_application_name;
00308   m_application_URL = src.m_application_URL;
00309   m_application_details = src.m_application_details;
00310 }
00311 
00312 
00313 ON_3dmApplication::~ON_3dmApplication()
00314 {
00315   m_application_name.Empty();
00316   m_application_URL.Empty();
00317   m_application_details.Empty();
00318 }
00319 
00320 ON_3dmApplication& ON_3dmApplication::operator=(const ON_3dmApplication& src)
00321 {
00322   if ( this != &src ) {
00323     m_application_name = src.m_application_name;
00324     m_application_URL = src.m_application_URL;
00325     m_application_details = src.m_application_details;
00326   }
00327   return *this;
00328 }
00329 
00330 void ON_3dmApplication::Default()
00331 {
00332   m_application_name.Empty();
00333   m_application_URL.Empty();
00334   m_application_details.Empty();
00335 }
00336 
00337 void ON_3dmApplication::Dump( ON_TextLog& dump ) const
00338 {
00339   const wchar_t* s = m_application_name;
00340   if ( s )
00341     dump.Print("Name: %ls\n",s);
00342   s = m_application_URL;
00343   if ( s )
00344     dump.Print("URL: %ls\n",s);
00345   s = m_application_details;
00346   if ( s )
00347     dump.Print("Details: %ls\n",s);
00348 }
00349 
00350 ON_BOOL32 ON_3dmApplication::IsValid() const
00351 {
00352   return m_application_name.IsEmpty() ? false : true;
00353 }
00354 
00355 ON_BOOL32 ON_3dmApplication::Read( ON_BinaryArchive& file )
00356 {
00357   int major_version = 0;
00358   int minor_version = 0;
00359   ON_BOOL32 rc = file.Read3dmChunkVersion( &major_version, &minor_version );
00360   if (rc) rc = file.ReadString( m_application_name );
00361   if (rc) rc = file.ReadString( m_application_URL );
00362   if (rc) rc = file.ReadString( m_application_details );
00363   return rc;
00364 }
00365 
00366 ON_BOOL32 ON_3dmApplication::Write( ON_BinaryArchive& file ) const
00367 {
00368   ON_BOOL32 rc = file.Write3dmChunkVersion( 1, 0 );
00369   if (rc) rc = file.WriteString( m_application_name );
00370   if (rc) rc = file.WriteString( m_application_URL );
00371   if (rc) rc = file.WriteString( m_application_details );
00372   return rc;
00373 }
00374 
00376 //
00377 // ON_3dmProperties
00378 //
00379 
00380 void ON_3dmProperties::Default()
00381 {
00382   // default properties
00383   m_RevisionHistory.Default();
00384   m_Notes.Default();
00385   m_PreviewImage.Destroy();
00386   m_Application.Default();
00387 }
00388 
00389 ON_3dmProperties::ON_3dmProperties()
00390 {
00391   Default();
00392 };
00393 
00394 ON_3dmProperties::~ON_3dmProperties()
00395 {
00396   Default();
00397 }
00398 
00399 ON_3dmProperties::ON_3dmProperties(const ON_3dmProperties& src)
00400 {
00401   Default();
00402   *this = src;
00403 }
00404 
00405 ON_3dmProperties& ON_3dmProperties::operator=(const ON_3dmProperties& src)
00406 {
00407   if ( this != &src ) {
00408     m_RevisionHistory = src.m_RevisionHistory;
00409     m_Notes = src.m_Notes;
00410     m_PreviewImage = src.m_PreviewImage;
00411     m_Application = src.m_Application;
00412   }
00413   return *this;
00414 }
00415 
00416 void ON_SetBinaryArchiveOpenNURBSVersion(ON_BinaryArchive& file, int value)
00417 {
00418   if ( value >= 200012210 )
00419   {
00420     file.m_3dm_opennurbs_version = value;
00421   }
00422   else
00423   {
00424     ON_ERROR("ON_SetBinaryArchiveOpenNURBSVersion - invalid opennurbs version");
00425     file.m_3dm_opennurbs_version = 0;
00426   }
00427 }
00428 
00429 ON_BOOL32 ON_3dmProperties::Read(ON_BinaryArchive& file )
00430 {
00431   Default();
00432 
00433   ON_BOOL32 rc = true;
00434 
00435   unsigned int tcode;
00436   ON__INT64 value;
00437 
00438   for(;;) {
00439 
00440     rc = file.BeginRead3dmBigChunk( &tcode, &value );
00441     if ( !rc )
00442       break;
00443 
00444     switch(tcode) {
00445 
00446     case TCODE_PROPERTIES_OPENNURBS_VERSION:
00447       { 
00448         int on_version = 0;
00449         if ( value > 299912319 || (value != 0 && value < 200101010) )
00450         {
00451           ON_ERROR("ON_3dmProperties::Read - TCODE_PROPERTIES_OPENNURBS_VERSION corrupt value");
00452           rc = false;
00453         }
00454         else
00455         {
00456           on_version = (int)value;
00457         }
00458         ON_SetBinaryArchiveOpenNURBSVersion(file,on_version);
00459       }
00460       break;
00461       
00462     case TCODE_PROPERTIES_REVISIONHISTORY: // file creation/revision information
00463       m_RevisionHistory.Read(file);
00464       break;
00465       
00466     case TCODE_PROPERTIES_NOTES: // file notes
00467       m_Notes.Read(file);
00468       break;
00469       
00470     case TCODE_PROPERTIES_PREVIEWIMAGE: // uncompressed preview image
00471       m_PreviewImage.ReadUncompressed(file);
00472       break;
00473       
00474     case TCODE_PROPERTIES_COMPRESSED_PREVIEWIMAGE: // compressed preview image
00475       m_PreviewImage.ReadCompressed(file);
00476       break;
00477       
00478     case TCODE_PROPERTIES_APPLICATION: // application that created 3dm file
00479       m_Application.Read(file);
00480       break;
00481       
00482     default:
00483       // information added in future will be skipped by file.EndRead3dmChunk()
00484       break;
00485     }
00486 
00487     if ( !file.EndRead3dmChunk() ) {
00488       rc = false;
00489       break;
00490     }
00491 
00492     if ( TCODE_ENDOFTABLE == tcode )
00493       break;
00494   }
00495 
00496   return rc;
00497 }
00498 
00499 ON_BOOL32 ON_3dmProperties::Write(ON_BinaryArchive& file) const
00500 {
00501   ON_BOOL32 rc = true;
00502 
00503   // This short chunk identifies the version of OpenNURBS that was used to write this file.
00504   rc = file.BeginWrite3dmChunk(TCODE_PROPERTIES_OPENNURBS_VERSION,ON::Version());
00505   if ( rc) rc = file.EndWrite3dmChunk();
00506 
00507   // optional TCODE_PROPERTIES_REVISIONHISTORY chunk - file creation/revision information
00508   if ( rc && m_RevisionHistory.IsValid() ) {
00509     rc = file.BeginWrite3dmChunk(TCODE_PROPERTIES_REVISIONHISTORY,0);
00510     if ( rc ) {
00511       rc = m_RevisionHistory.Write(file);
00512       if ( !file.EndWrite3dmChunk() )
00513         rc = false;
00514     }
00515   }
00516 
00517   // optional TCODE_PROPERTIES_NOTES chunk - file notes
00518   if ( rc && m_Notes.IsValid() ) {
00519     rc = file.BeginWrite3dmChunk(TCODE_PROPERTIES_NOTES,0);
00520     if ( rc ) {
00521       rc = m_Notes.Write(file);
00522       if ( !file.EndWrite3dmChunk() )
00523         rc = false;
00524     }
00525   }
00526 
00527   // optional TCODE_PROPERTIES_PREVIEWIMAGE chunk - bitmap preview
00528   //if ( rc && m_PreviewImage.IsValid() ) {
00529   //  rc = file.BeginWrite3dmChunk(TCODE_PROPERTIES_PREVIEWIMAGE,0);
00530   //  if ( rc ) {
00531   //    rc = m_PreviewImage.WriteUncompressed(file);
00532   //    if ( !file.EndWrite3dmChunk() )
00533   //      rc = false;
00534   //  }
00535   //}
00536 
00537   // optional TCODE_PROPERTIES_COMPRESSED_PREVIEWIMAGE chunk - bitmap preview
00538   if ( rc && m_PreviewImage.IsValid() ) {
00539     rc = file.BeginWrite3dmChunk(TCODE_PROPERTIES_COMPRESSED_PREVIEWIMAGE,0);
00540     if ( rc ) {
00541       rc = m_PreviewImage.WriteCompressed(file);
00542       if ( !file.EndWrite3dmChunk() )
00543         rc = false;
00544     }
00545   }
00546 
00547   // optional TCODE_PROPERTIES_APPLICATION chunk - application information
00548   if ( rc && m_Application.IsValid() ) {
00549     rc = file.BeginWrite3dmChunk(TCODE_PROPERTIES_APPLICATION,0);
00550     if ( rc ) {
00551       rc = m_Application.Write(file);
00552       if ( !file.EndWrite3dmChunk() )
00553         rc = false;
00554     }
00555   }
00556 
00557   // required TCODE_ENDOFTABLE chunk - marks end of properties table
00558   if ( rc ) {
00559     rc = file.BeginWrite3dmChunk( TCODE_ENDOFTABLE, 0 );
00560     if ( rc ) {
00561       if ( !file.EndWrite3dmChunk() )
00562         rc = false;
00563     }
00564   }
00565 
00566   return rc;
00567 }
00568 
00569 void ON_3dmProperties::Dump( ON_TextLog& dump ) const
00570 {
00571   dump.Print("Revision history:\n");
00572   dump.PushIndent();
00573   m_RevisionHistory.Dump(dump);
00574   dump.PopIndent();
00575 
00576   dump.Print("\n");
00577   dump.Print("Notes:\n");
00578   if ( m_Notes.m_notes.Length() > 0 ) {
00579     dump.PushIndent();
00580     m_Notes.Dump(dump);
00581     dump.PopIndent();
00582   }
00583 
00584   dump.Print("\n");
00585   dump.Print("Application information:\n");
00586   dump.PushIndent();
00587   m_Application.Dump(dump);
00588   dump.PopIndent();
00589 
00590   if ( m_PreviewImage.IsValid() ) {
00591     dump.Print("\n");
00592     dump.Print("Preview image:\n");
00593     dump.PushIndent();
00594     m_PreviewImage.Dump(dump);
00595     dump.PopIndent();
00596   }
00597 }
00598 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:26:58