opennurbs_instance.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 
00019 #define ON_BOZO_VACCINE_11EE2C1FF90D4C6AA7CDEC8532E1E32D
00020 #define ON_BOZO_VACCINE_F42D967121EB46929B9ABC3507FF28F5
00021 
00022 ON_OBJECT_IMPLEMENT( ON_InstanceDefinition, ON_Geometry, "26F8BFF6-2618-417f-A158-153D64A94989" );
00023 
00024 ON_InstanceDefinition::ON_InstanceDefinition()
00025 {
00026   m_uuid = ON_nil_uuid;
00027   m_idef_update_type = ON_InstanceDefinition::static_def;
00028   m_idef_update_depth = 0;
00029   m_us.m_unit_system = ON::no_unit_system;
00030   m_us.m_custom_unit_scale = 0.0;
00031   m_us.m_custom_unit_name.Destroy();
00032   m_source_bRelativePath = false;
00033   m_reserved1 = 0;
00034   m_idef_layer_style = 0;
00035   m_reserved2[0] = 0;
00036   m_reserved2[1] = 0;
00037 }
00038 
00039 ON_InstanceDefinition::~ON_InstanceDefinition()
00040 {
00041 }
00042 
00043 
00044 void ON_InstanceDefinition::Dump( ON_TextLog& text_log ) const
00045 {
00046   const wchar_t* wsIDefName = m_name;
00047   if ( 0 == wsIDefName ) 
00048     wsIDefName = L"";
00049   text_log.Print("Name: \"%ls\"\n",wsIDefName);
00050 
00051 
00052   text_log.Print("Type: ");
00053   switch(m_idef_update_type)
00054   {
00055   case ON_InstanceDefinition::static_def:
00056     text_log.Print("embedded.");
00057     break;
00058   case ON_InstanceDefinition::embedded_def:
00059     if ( m_source_archive.Length() > 0 )
00060       text_log.Print("OBSOLETE embedded_def with non-empty source archive - should be linked_and_embedded_def.");
00061     else
00062       text_log.Print("OBSOLETE embedded_def with empty source archive - should be static_def.");
00063     break;
00064   case ON_InstanceDefinition::linked_and_embedded_def:
00065     text_log.Print("embedded and linked - definition from source archive.");
00066     break;
00067   case ON_InstanceDefinition::linked_def:
00068     text_log.Print("linked - definition from source archive.");
00069     break;
00070   default:
00071     text_log.Print("not valid");
00072     break;
00073   }
00074   text_log.Print("\n");
00075 
00076 
00077   text_log.Print("Id: "); text_log.Print(m_uuid); text_log.Print("\n");
00078 
00079   const wchar_t* wsDescription = m_description;
00080   if ( 0 != wsDescription && 0 != wsDescription[0]) 
00081     text_log.Print("Description: \"%ls\"\n",wsDescription);
00082 
00083   const wchar_t* wsURL = m_url;
00084   if ( 0 != wsURL && 0 != wsURL[0]) 
00085     text_log.Print("URL: \"%ls\"\n",wsURL);
00086 
00087   const wchar_t* wsTag = m_url_tag;
00088   if ( 0 != wsTag && 0 != wsTag[0]) 
00089     text_log.Print("URL tag: \"%ls\"\n",wsTag);
00090 
00091   m_us.Dump(text_log);
00092 
00093   const wchar_t* wsSourceArchive = SourceArchive();
00094   text_log.Print("Source archive: ");
00095   if ( 0 == wsSourceArchive || 0 == wsSourceArchive[0] )
00096   {
00097     text_log.Print("none.\n");
00098   }
00099   else
00100   {
00101     bool bRel = m_source_bRelativePath;
00102     text_log.Print("\"%ls\" (%s)\n",wsSourceArchive,bRel?"relative":"absolute");
00103 
00104     text_log.PushIndent();
00105     ON_wString str;
00106     bRel = false;
00107     if ( GetAlternateSourceArchivePath(str,bRel) )
00108     {
00109       const wchar_t* wsAlternateArchive = str;
00110       if ( 0 == wsAlternateArchive || 0 == wsAlternateArchive[0] )
00111         wsAlternateArchive = L"";
00112       text_log.Print("Alternate archive: \"%ls\" (%s)\n",wsAlternateArchive,bRel?"relative":"absolute");
00113     }
00114 
00115     text_log.Print("Update depth: %d\n",m_idef_update_type);
00116 
00117     text_log.Print("Archive ");
00118     m_source_archive_checksum.Dump(text_log);    
00119     text_log.PopIndent();
00120   }
00121 
00122   const int id_count = m_object_uuid.Count();
00123   text_log.Print("Contains: %d objects\n",id_count);
00124 
00125   if ( id_count > 0 )
00126   {
00127     text_log.PushIndent();
00128 
00129     text_log.Print(m_object_uuid[0]); 
00130     text_log.Print("\n");
00131 
00132     if ( id_count > 4 )
00133     {
00134       text_log.Print("...\n");
00135     }
00136     else
00137     {
00138       for ( int i = 1; i < id_count; i++ )
00139       {
00140         text_log.Print(m_object_uuid[i]);
00141         text_log.Print("\n");
00142       }
00143     }
00144 
00145     text_log.PopIndent();
00146   }
00147 
00148   m_bbox.Dump(text_log);
00149 }
00150 
00151 ON_BOOL32 ON_InstanceDefinition::IsValid( ON_TextLog* text_log ) const
00152 {
00153   if ( 0 == ON_UuidCompare( m_uuid, ON_nil_uuid) )
00154   {
00155     if (text_log)
00156     {
00157       text_log->Print("ON_InstanceDefinition has nil uuid.\n");
00158     }
00159     return false;
00160   }
00161   if ( !m_bbox.IsValid() )
00162   {
00163     if (text_log)
00164     {
00165       text_log->Print("ON_InstanceDefinition has invalid bounding box.\n");
00166     }
00167     return false;
00168   }
00169 
00170   switch( m_idef_update_type)
00171   {
00172     case static_def:
00173       // no source archive information should be present
00174       if ( m_source_archive.Length() > 0 )
00175       {
00176         if (text_log)
00177         {
00178           text_log->Print("ON_InstanceDefinition is static but m_source_archive is not empty.\n");
00179         }
00180         return false;
00181       }
00182       if ( m_source_archive_checksum.IsSet() )
00183       {
00184         if (text_log)
00185         {
00186           text_log->Print("ON_InstanceDefinition is static but m_source_archive_checksum is set.\n");
00187         }
00188         return false;
00189       }
00190 
00191       if ( 0 != m_idef_layer_style )
00192       {
00193         if (text_log)
00194         {
00195           text_log->Print("ON_InstanceDefinition is static but m_idef_layer_style is not zero.\n");
00196         }
00197         return false;
00198       }
00199       break;
00200 
00201     case embedded_def: // embedded_def is obsolete - 
00202       if ( text_log )
00203       {
00204         text_log->Print("ON_InstanceDefinition.m_idef_update_type = obsolete \"embedded_idef\". Use \"static_def\" or \"linked_and_embedded_def\".\n");
00205       }
00206       return false;
00207       break;
00208 
00209     case linked_and_embedded_def:
00210     case linked_def:
00211       // source archive information is required
00212       if( m_source_archive.IsEmpty())
00213       {
00214         if (text_log)
00215         {
00216           text_log->Print("ON_InstanceDefinition is linked or embedded but m_source_archive is empty.\n");
00217         }
00218         return false;
00219       }
00220       if( !m_source_archive_checksum.IsSet())
00221       {
00222         if (text_log)
00223         {
00224           text_log->Print("ON_InstanceDefinition is linked or embedded but m_source_archive_checksum is zero.\n");
00225         }
00226         return false;
00227       }
00228 
00229       if ( linked_def == m_idef_update_type )
00230       {
00231         if ( 1 != m_idef_layer_style && 2 != m_idef_layer_style )
00232         {
00233           if (text_log)
00234           {
00235             text_log->Print("ON_InstanceDefinition is linked_def but m_idef_layer_style is not 1 or 2.\n");
00236           }
00237           return false;
00238         }
00239       }
00240       else
00241       {
00242         if ( 0 != m_idef_layer_style )
00243         {
00244           if (text_log)
00245           {
00246             text_log->Print("ON_InstanceDefinition is linked_and_embedded_def but m_idef_layer_style is not zero.\n");
00247           }
00248           return false;
00249         }
00250       }
00251 
00252       break;
00253 
00254     default:
00255       if ( text_log )
00256       {
00257         text_log->Print("ON_InstanceDefinition.m_idef_update_type value is invalid.\n");
00258       }
00259       return false;
00260       break;
00261   }
00262 
00263   // TODO
00264   return true;
00265 }
00266 
00267 
00268 unsigned int ON_InstanceDefinition::SizeOf() const
00269 {
00270   unsigned int sz = sizeof(*this) - sizeof(ON_Geometry);
00271   sz += ON_Geometry::SizeOf();
00272   sz += this->m_object_uuid.SizeOfArray();
00273   sz += this->m_name.SizeOf();
00274   sz += this->m_description.SizeOf();
00275   sz += this->m_url.SizeOf();
00276   sz += this->m_url_tag.SizeOf();
00277   sz += this->m_source_archive.SizeOf();
00278   return sz;
00279 }
00280 
00281 
00282 ON_BOOL32 ON_InstanceDefinition::Write(
00283        ON_BinaryArchive& binary_archive
00284      ) const
00285 {
00286   bool rc = binary_archive.Write3dmChunkVersion(1,6);
00287 
00288   // version 1.0 fields
00289   if ( rc )
00290     rc = binary_archive.WriteUuid( m_uuid );
00291   if ( rc )
00292   {
00293     if (    binary_archive.Archive3dmVersion() >= 4
00294          && ON_InstanceDefinition::linked_def == m_idef_update_type )
00295     {
00296       // linked instance definition geometry is never in the file
00297       ON_SimpleArray<ON_UUID> empty_uuid_list;
00298       rc = binary_archive.WriteArray( empty_uuid_list );
00299     }
00300     else
00301     {
00302       rc = binary_archive.WriteArray( m_object_uuid );
00303     }
00304   }
00305   if ( rc )
00306     rc = binary_archive.WriteString( m_name );
00307   if ( rc )
00308     rc = binary_archive.WriteString( m_description );
00309   if ( rc )
00310     rc = binary_archive.WriteString( m_url );
00311   if ( rc )
00312     rc = binary_archive.WriteString( m_url_tag );
00313   if ( rc )
00314     rc = binary_archive.WriteBoundingBox( m_bbox );
00315 
00316   // m_idef_update_type was an unsigned int and got changed to an enum.  Read and write
00317   // as an unsigned int to support backwards compatibility
00318   const unsigned int idef_update_type = (unsigned int)this->IdefUpdateType();
00319   if ( rc )
00320     rc = binary_archive.WriteInt( idef_update_type );
00321   if ( rc )
00322   {
00323     // 7 February 2012
00324     //   Purge source archive information from static_defs
00325     if ( ON_InstanceDefinition::static_def == idef_update_type )
00326     {
00327       ON_wString empty_string;
00328       rc = binary_archive.WriteString( empty_string );
00329     }
00330     else
00331       rc = binary_archive.WriteString( m_source_archive );
00332   }
00333   
00334   // version 1.1 fields
00335   if (rc)
00336   {
00337     // 7 February 2012
00338     //   Purge source archive information from static_defs
00339     if ( ON_InstanceDefinition::static_def == idef_update_type )
00340       ON_CheckSum::UnsetCheckSum.Write(binary_archive);
00341     else
00342       rc = m_source_archive_checksum.Write( binary_archive );
00343   }
00344   
00345   // version 1.2 fields
00346   if (rc)
00347     rc = binary_archive.WriteInt( m_us.m_unit_system );
00348 
00349   // version 1.3 fields - added 6 March 2006
00350   if (rc)
00351     rc = binary_archive.WriteDouble( m_us.m_custom_unit_scale );
00352 
00353   if ( rc )
00354   {
00355     bool b = (ON_InstanceDefinition::static_def == idef_update_type)
00356            ? false
00357            : m_source_bRelativePath;
00358     rc = binary_archive.WriteBool( b );
00359   }
00360 
00361   // version 1.4 fields
00362   if (rc)
00363     rc = m_us.Write(binary_archive);
00364 
00365   // version 1.5 fields
00366   if (rc)
00367     rc = binary_archive.WriteInt(m_idef_update_depth);
00368 
00369   // version 1.6 fields ( added 14 February 2012 )
00370   if (rc)
00371     rc = binary_archive.WriteInt(  m_idef_layer_style );
00372 
00373   return rc;
00374 }
00375 
00376 ON_InstanceDefinition::IDEF_UPDATE_TYPE ON_InstanceDefinition::IdefUpdateType() const
00377 {
00378   if ( ON_InstanceDefinition::embedded_def == m_idef_update_type )
00379   {
00380     ON_ERROR("Using obsolete ON_InstanceDefinition::embedded_def value - fix code.");
00381     const_cast< ON_InstanceDefinition* >(this)->m_idef_update_type 
00382                                   = ( m_source_archive.Length() > 0 )
00383                                   ? ON_InstanceDefinition::linked_and_embedded_def
00384                                   : ON_InstanceDefinition::static_def;
00385   }
00386   return m_idef_update_type;
00387 }
00388 
00389 ON_InstanceDefinition::IDEF_UPDATE_TYPE ON_InstanceDefinition::IdefUpdateType(int i)
00390 {
00391   IDEF_UPDATE_TYPE t;
00392   switch(i)
00393   {
00394   case ON_InstanceDefinition::static_def:
00395     t = ON_InstanceDefinition::static_def;
00396     break;
00397   case ON_InstanceDefinition::embedded_def:
00398     t = ON_InstanceDefinition::embedded_def;
00399     break;
00400   case ON_InstanceDefinition::linked_and_embedded_def:
00401     t = ON_InstanceDefinition::linked_and_embedded_def;
00402     break;
00403   case ON_InstanceDefinition::linked_def:
00404     t = ON_InstanceDefinition::linked_def;
00405     break;
00406   default:
00407     t = ON_InstanceDefinition::static_def;
00408     break;
00409   }
00410   return t;
00411 }
00412 
00413 
00414 ON_BOOL32 ON_InstanceDefinition::Read(
00415        ON_BinaryArchive& binary_archive
00416      )
00417 {
00418   int major_version = 0;
00419   int minor_version = 0;
00420 
00421   m_idef_layer_style = 0;
00422 
00423   m_us.m_custom_unit_scale = 0.0;
00424   m_us.m_custom_unit_name.Destroy();
00425   m_us.m_unit_system = ON::no_unit_system;
00426   m_source_bRelativePath = false;
00427   m_source_archive.Destroy();
00428 
00429   bool rc = binary_archive.Read3dmChunkVersion(&major_version,&minor_version);
00430   if ( rc )
00431   {
00432     if ( major_version != 1 )
00433       rc = false;
00434     // version 1.0 fields
00435     if ( rc )
00436       rc = binary_archive.ReadUuid( m_uuid );
00437     if ( rc )
00438       rc = binary_archive.ReadArray( m_object_uuid );
00439     if ( rc )
00440       rc = binary_archive.ReadString( m_name );
00441     if ( rc )
00442       rc = binary_archive.ReadString( m_description );
00443     if ( rc )
00444       rc = binary_archive.ReadString( m_url );
00445     if ( rc )
00446       rc = binary_archive.ReadString( m_url_tag );
00447     if ( rc )
00448       rc = binary_archive.ReadBoundingBox( m_bbox );
00449     // m_idef_update_type was an unsigned int and got changed to an enum.  Read and write
00450     // as an unsigned int to support backwards compatibility
00451     unsigned int source = m_idef_update_type;
00452     if ( rc )
00453       rc = binary_archive.ReadInt( &source );
00454     if( rc)
00455       m_idef_update_type = ON_InstanceDefinition::IdefUpdateType(source);
00456     if ( rc )
00457       rc = binary_archive.ReadString( m_source_archive );
00458 
00459     // version 1.1 fields
00460     if ( minor_version >= 1 )
00461     {
00462       if ( rc )
00463         rc = m_source_archive_checksum.Read( binary_archive );
00464     }
00465 
00466     // version 1.2 fields
00467     if ( minor_version >= 2 )
00468     {
00469       int us = ON::no_unit_system;
00470       if ( rc )
00471         rc = binary_archive.ReadInt( &us );
00472       m_us.m_unit_system = ON::UnitSystem(us);
00473       if ( ON::custom_unit_system != m_us.m_unit_system && ON::no_unit_system != m_us.m_unit_system )
00474       {
00475         m_us.m_custom_unit_scale = ON::UnitScale( m_us.m_unit_system, ON::meters );
00476       }
00477       else
00478       {
00479         m_us.m_custom_unit_scale = 0.0;
00480       }
00481 
00482       if ( minor_version >= 3 )
00483       {
00484         // version 1.3 fields - added 6 March 2006
00485         //int us = ON::no_unit_system;
00486         if ( rc )
00487           rc = binary_archive.ReadDouble( &m_us.m_custom_unit_scale );
00488         if ( rc )
00489           rc = binary_archive.ReadBool( &m_source_bRelativePath );
00490         if ( rc && minor_version >= 4 )
00491         {
00492           rc = m_us.Read(binary_archive);
00493           if (rc && minor_version >= 5 )
00494           {
00495             rc = binary_archive.ReadInt(&m_idef_update_depth);
00496 
00497             if ( rc && minor_version >= 6 )
00498             {
00499               unsigned int i = 0;
00500               rc = binary_archive.ReadInt(&i);
00501               if ( i && i > 0 && i < 256 )
00502                 m_idef_layer_style = (unsigned char)i;
00503             }
00504           }
00505         }
00506       }
00507     }
00508 
00509     if ( ON_InstanceDefinition::embedded_def == m_idef_update_type )
00510     {
00511       // 7 February 2012
00512       //   "embedded_def" is obsolete.
00513       if (m_source_archive.Length() > 0 )
00514         m_idef_update_type = ON_InstanceDefinition::linked_and_embedded_def;
00515       else
00516         DestroySourceArchive(); // convert to static
00517     }
00518 
00519     if ( ON_InstanceDefinition::linked_def == m_idef_update_type )
00520     {
00521       if ( m_idef_layer_style < 1 || m_idef_layer_style > 2 )
00522       {
00523         // The goal of the next if/else clause is for Rhino users
00524         // to see what they saw when they created the file.
00525         if ( binary_archive.Archive3dmVersion() < 50 )
00526         {
00527           // V4 linked blocks and early V5 linked blocks treated
00528           // layers and materials the way newer "active" idefs work,
00529           // so when I read an archive with version < 50, the
00530           // default will be 1 for "active".  
00531           m_idef_layer_style = 1;
00532         }
00533         else
00534         {
00535           // The more recent V5 linked blocks treated layers and materials
00536           // the way "reference" style idefs work, so when I read an
00537           // archive with version >= 50 (meaning recent V5), the default
00538           // will be 2 for "reference".
00539           m_idef_layer_style = 2;
00540         }
00541       }
00542     }
00543     else
00544     {
00545       m_idef_layer_style= 0;
00546     }
00547   }
00548   return rc;
00549 }
00550 
00551 ON::object_type ON_InstanceDefinition::ObjectType() const
00552 {
00553   return ON::instance_definition;
00554 }
00555 
00556 
00557 // virtual ON_Geometry overrides
00558 int ON_InstanceDefinition::Dimension() const
00559 {
00560   return 3;
00561 }
00562 
00563 ON_BOOL32 ON_InstanceDefinition::GetBBox(
00564        double* boxmin,
00565        double* boxmax,
00566        ON_BOOL32 bGrowBox
00567        ) const
00568 {
00569   if ( boxmin )
00570   {
00571     boxmin[0] = m_bbox.m_min.x;
00572     boxmin[1] = m_bbox.m_min.y;
00573     boxmin[2] = m_bbox.m_min.z;
00574   }
00575   if ( boxmax )
00576   {
00577     boxmax[0] = m_bbox.m_max.x;
00578     boxmax[1] = m_bbox.m_max.y;
00579     boxmax[2] = m_bbox.m_max.z;
00580   }
00581   return m_bbox.IsValid();
00582 }
00583 
00584 ON_BOOL32 ON_InstanceDefinition::Transform( 
00585        const ON_Xform& xform
00586        )
00587 {
00588   // instance DEFs cannot be transformed
00589   return false;
00590 }
00591 
00592 const wchar_t* ON_InstanceDefinition::Name() const
00593 {
00594   return m_name;
00595 }
00596 
00597 void ON_InstanceDefinition::SetName( const wchar_t* name )
00598 {
00599   ON_wString s(name);
00600   s.TrimLeftAndRight();
00601   if ( s.IsEmpty() )
00602     m_name.Destroy();
00603   else
00604     m_name = s;
00605 }
00606 
00607 
00608 const wchar_t* ON_InstanceDefinition::URL() const
00609 {
00610   return m_url;
00611 }
00612 
00613 void ON_InstanceDefinition::SetURL( const wchar_t* url )
00614 {
00615   ON_wString s(url);
00616   s.TrimLeftAndRight();
00617   if ( s.IsEmpty() )
00618     m_url.Destroy();
00619   else
00620     m_url = s;
00621 }
00622 
00623 const wchar_t* ON_InstanceDefinition::URL_Tag() const
00624 {
00625   return m_url_tag;
00626 }
00627 
00628 void ON_InstanceDefinition::SetURL_Tag( const wchar_t* url_tag )
00629 {
00630   ON_wString s(url_tag);
00631   s.TrimLeftAndRight();
00632   if ( s.IsEmpty() )
00633     m_url_tag.Destroy();
00634   else
00635     m_url_tag = s;
00636 }
00637 
00638 ON_UUID ON_InstanceDefinition::Uuid() const
00639 {
00640   return m_uuid;
00641 }
00642 
00643 void ON_InstanceDefinition::SetUuid( ON_UUID uuid )
00644 {
00645   m_uuid = uuid;
00646 }
00647 
00648 const wchar_t* ON_InstanceDefinition::Description() const
00649 {
00650   return m_description;
00651 }
00652 
00653 void ON_InstanceDefinition::SetDescription( const wchar_t* description )
00654 {
00655   ON_wString s(description);
00656   s.TrimLeftAndRight();
00657   if ( s.IsEmpty() )
00658     m_description.Destroy();
00659   else
00660     m_description = s;
00661 }
00662 
00663 void ON_InstanceDefinition::SetBoundingBox( ON_BoundingBox bbox )
00664 {
00665   m_bbox = bbox;
00666 }
00667 
00668 void ON_InstanceDefinition::SetSourceArchive(
00669         const wchar_t* source_archive, 
00670         ON_CheckSum checksum,
00671         ON_InstanceDefinition::IDEF_UPDATE_TYPE idef_update_type
00672         )
00673 {
00674   ON_wString s(source_archive);
00675   s.TrimLeftAndRight();  
00676   if ( s.IsEmpty() )
00677   {
00678     DestroySourceArchive();
00679   }
00680   else
00681   {
00682     SetAlternateSourceArchivePath(0,false);
00683     m_source_archive = s;
00684     m_source_bRelativePath = false;
00685     m_source_archive_checksum = checksum;
00686     m_idef_update_type = ON_InstanceDefinition::IdefUpdateType(idef_update_type);
00687     if ( ON_InstanceDefinition::linked_def != m_idef_update_type )
00688     {
00689       m_idef_layer_style = 0;
00690     }
00691   }
00692 }
00693 
00694 void ON_InstanceDefinition::DestroySourceArchive()
00695 {
00696   m_source_archive.Destroy();
00697   m_source_archive_checksum.Zero();
00698   m_source_bRelativePath = false;
00699   m_idef_update_type = ON_InstanceDefinition::static_def;
00700   m_idef_layer_style = 0;
00701   m_idef_update_depth = 0;
00702   SetAlternateSourceArchivePath(0,false);
00703 }
00704 
00705 const wchar_t* ON_InstanceDefinition::SourceArchive() const
00706 {
00707   return m_source_archive;
00708 }
00709 
00710 ON_CheckSum ON_InstanceDefinition::SourceArchiveCheckSum() const
00711 {
00712   return m_source_archive_checksum;
00713 }
00714 
00715 const ON_UnitSystem& ON_InstanceDefinition::UnitSystem() const
00716 {
00717   return m_us;
00718 }
00719 
00720 void ON_InstanceDefinition::SetUnitSystem( ON::unit_system us )
00721 {
00722   // make sure we are not getting garbage cast as an ON::unit_system
00723   if ( us == ON::UnitSystem(us) )
00724   {
00725     m_us.m_unit_system = us;
00726     if ( ON::custom_unit_system != m_us.m_unit_system )
00727     {
00728       m_us.m_custom_unit_scale = ( ON::no_unit_system == m_us.m_unit_system )
00729                                ? 0.0
00730                                : ON::UnitScale(ON::meters,m_us.m_unit_system);
00731     }
00732   }
00733 }
00734 
00735 void ON_InstanceDefinition::SetUnitSystem( const ON_UnitSystem& us )
00736 {
00737   // make sure we are not getting garbage cast as an ON::unit_system
00738   if ( us.IsValid() )
00739   {
00740     m_us = us;
00741     if ( ON::custom_unit_system != m_us.m_unit_system )
00742     {
00743       m_us.m_custom_unit_scale = ( ON::no_unit_system == m_us.m_unit_system )
00744                                ? 0.0
00745                                : ON::UnitScale(ON::meters,m_us.m_unit_system);
00746     }
00747   }
00748 }
00749 
00750 ON_OBJECT_IMPLEMENT( ON_InstanceRef, ON_Geometry, "F9CFB638-B9D4-4340-87E3-C56E7865D96A" );
00751 
00752 const double ON_InstanceRef::m_singular_xform_tol = 1.0e-6;
00753 
00754 ON_InstanceRef::ON_InstanceRef()
00755 {
00756   m_instance_definition_uuid = ON_nil_uuid;
00757   m_xform.Identity();
00758 }
00759 
00760 ON_BOOL32 ON_InstanceRef::IsValid( ON_TextLog* text_log ) const
00761 {
00762   if ( 0 == ON_UuidCompare( m_instance_definition_uuid, ON_nil_uuid) )
00763   {
00764     if ( text_log )
00765       text_log->Print("ON_InstanceRef has nil m_instance_definition_uuid.\n");
00766     return false;
00767   }
00768 
00769   ON_Xform tmp = m_xform.Inverse()*m_xform;
00770   if ( !tmp.IsIdentity( ON_InstanceRef::m_singular_xform_tol ) )
00771   {
00772     if ( text_log )
00773       text_log->Print("ON_InstanceRef has singular m_xform.\n");
00774     return false;
00775   }
00776   return true;
00777 }
00778 
00779 ON_BOOL32 ON_InstanceRef::Write(
00780        ON_BinaryArchive& binary_archive
00781      ) const
00782 {
00783   bool rc = binary_archive.Write3dmChunkVersion(1,0);
00784   if ( rc )
00785     rc = binary_archive.WriteUuid( m_instance_definition_uuid );
00786   if ( rc )
00787     rc = binary_archive.WriteXform( m_xform );
00788   if ( rc )
00789     rc = binary_archive.WriteBoundingBox( m_bbox );
00790   return rc;
00791 }
00792 
00793 ON_BOOL32 ON_InstanceRef::Read(
00794        ON_BinaryArchive& binary_archive
00795      )
00796 {
00797   int major_version = 0;
00798   int minor_version = 0;
00799   bool rc = binary_archive.Read3dmChunkVersion(&major_version,&minor_version);
00800   if ( rc )
00801   {
00802     if ( major_version != 1 )
00803       rc = false;
00804     if (rc )
00805       rc = binary_archive.ReadUuid( m_instance_definition_uuid );
00806     if ( rc )
00807       rc = binary_archive.ReadXform( m_xform );
00808     if ( rc )
00809       rc = binary_archive.ReadBoundingBox( m_bbox );
00810   }
00811   return rc;
00812 }
00813 
00814 ON::object_type ON_InstanceRef::ObjectType() const
00815 {
00816   return ON::instance_reference;
00817 }
00818 
00819 
00820 // virtual ON_Geometry overrides
00821 int ON_InstanceRef::Dimension() const
00822 {
00823   return 3;
00824 }
00825 
00826 ON_BOOL32 ON_InstanceRef::GetBBox(
00827        double* boxmin,
00828        double* boxmax,
00829        ON_BOOL32 bGrowBox
00830        ) const
00831 {
00832   if ( !boxmin || !boxmax )
00833   {
00834     bGrowBox = false;
00835   }
00836   else if ( bGrowBox )
00837   {
00838     bGrowBox = ON_BoundingBox(ON_3dPoint(boxmin),ON_3dPoint(boxmax)).IsValid();
00839   }
00840 
00841   if( m_bbox.IsValid() )
00842   {
00843     if( bGrowBox )
00844     {
00845       if( boxmin[0] > m_bbox.m_min.x ) boxmin[0] = m_bbox.m_min.x;
00846       if( boxmin[1] > m_bbox.m_min.y ) boxmin[1] = m_bbox.m_min.y;
00847       if( boxmin[2] > m_bbox.m_min.z ) boxmin[2] = m_bbox.m_min.z;
00848 
00849       if( boxmax[0] < m_bbox.m_max.x ) boxmax[0] = m_bbox.m_max.x;
00850       if( boxmax[1] < m_bbox.m_max.y ) boxmax[1] = m_bbox.m_max.y;
00851       if( boxmax[2] < m_bbox.m_max.z ) boxmax[2] = m_bbox.m_max.z;
00852     }
00853     else
00854     {
00855       if( boxmin )
00856       {
00857         boxmin[0] = m_bbox.m_min.x;
00858         boxmin[1] = m_bbox.m_min.y;
00859         boxmin[2] = m_bbox.m_min.z;
00860       }
00861       if( boxmax )
00862       {
00863         boxmax[0] = m_bbox.m_max.x;
00864         boxmax[1] = m_bbox.m_max.y;
00865         boxmax[2] = m_bbox.m_max.z;
00866       }
00867       bGrowBox = true;
00868     }
00869   }
00870 
00871   return bGrowBox;
00872 }
00873 
00874 ON_BOOL32 ON_InstanceRef::Transform( 
00875        const ON_Xform& xform
00876        )
00877 {
00878   ON_Geometry::Transform(xform);
00879   m_xform = xform*m_xform;
00880   m_bbox.Transform(xform);
00881   return true;
00882 }
00883 
00884 bool ON_InstanceRef::IsDeformable() const
00885 {
00886   // 25 Feb 2006 Dale Lear - this seems wrong to me.
00887   return true;
00888 }
00889 
00890 bool ON_InstanceRef::MakeDeformable()
00891 {
00892   // 25 Feb 2006 Dale Lear - this seems wrong to me.
00893   return true;
00894 }
00895 
00896 
00897 class /*NEVER EXPORT THIS CLASS DEFINITION*/ ON__IDefLayerSettingsUserData : public ON_UserData
00898 {
00899 #if !defined(ON_BOZO_VACCINE_11EE2C1FF90D4C6AA7CDEC8532E1E32D)
00900 #error Never copy this class definition or put this definition in a header file!
00901 #endif
00902   ON_OBJECT_DECLARE(ON__IDefLayerSettingsUserData);
00903 
00904 public:
00905   ON__IDefLayerSettingsUserData();
00906   ~ON__IDefLayerSettingsUserData();
00907   // default copy constructor and operator= work fine.
00908 
00909   ON__IDefLayerSettingsUserData(const ON__IDefLayerSettingsUserData& src);
00910   ON__IDefLayerSettingsUserData& operator=(const ON__IDefLayerSettingsUserData& src);
00911 
00912 
00913   static ON__IDefLayerSettingsUserData* FindOrCreate(const ON_InstanceDefinition& idef,bool bCreate);
00914   
00915 private:
00916   void CreateHelper()
00917   {
00918     m_layers.Destroy();
00919     m_idef_layer_table_parent_layer = 0;
00920   }
00921 
00922   void CopyHelper(const ON__IDefLayerSettingsUserData& src)
00923   {
00924     m_layers.Reserve(src.m_layers.Count());
00925     for ( int i = 0; i < src.m_layers.Count(); i++ )
00926     {
00927       const ON_Layer* src_layer = src.m_layers[i];
00928       if ( 0 != src_layer )
00929       {
00930         m_layers.Append( new ON_Layer( *src_layer ) );
00931       }
00932     }
00933     
00934     if ( 0 != src.m_idef_layer_table_parent_layer )
00935     {
00936       m_idef_layer_table_parent_layer = new ON_Layer( *src.m_idef_layer_table_parent_layer );
00937     }
00938 
00939     m_runtime_layer_id_map = src.m_runtime_layer_id_map;
00940     m_runtime_layer_id_map.ImproveSearchSpeed();
00941   }
00942 
00943   void DestroyHelper()
00944   {
00945     for ( int i = 0; i < m_layers.Count(); i++ )
00946     {
00947       delete m_layers[i];
00948       m_layers[i] = 0;
00949     }
00950     m_layers.Destroy();
00951     if ( 0 != m_idef_layer_table_parent_layer )
00952     {
00953       delete m_idef_layer_table_parent_layer;
00954       m_idef_layer_table_parent_layer = 0;
00955     }
00956     m_runtime_layer_id_map.Empty();
00957   }
00958 
00959 public:
00960   // virtual ON_Object override
00961   ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
00962   // virtual ON_Object override
00963   unsigned int SizeOf() const;
00964   // virtual ON_Object override
00965   ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
00966   // virtual ON_Object override
00967   ON_BOOL32 Write(ON_BinaryArchive& binary_archive) const;
00968   // virtual ON_Object override
00969   ON_BOOL32 Read(ON_BinaryArchive& binary_archive);
00970   // virtual ON_UserData override
00971   ON_BOOL32 Archive() const;
00972   // virtual ON_UserData override
00973   ON_BOOL32 GetDescription( ON_wString& description );
00974 
00975 public:
00976   // m_layers[] satisfies
00977   //  * has no null members
00978   //  * is always sorted by layer id
00979   //  * has no duplicate layer ids
00980   //  * has no nil layer ids
00981   //  * the m_layer_id and m_parent_layer_id
00982   //    values are from the linked file.
00983   ON_SimpleArray<ON_Layer*> m_layers; 
00984 
00985   // Settings for the layer that is the
00986   // parent of the layers in the linked
00987   // files layer table.  This layer is
00988   // not in the linked file and is not
00989   // saved in the layer table of file containing
00990   // the idef.  If null, it is created
00991   ON_Layer* m_idef_layer_table_parent_layer;
00992 
00993   // When a linked idef is inserted and a layer id 
00994   // collision occures, the runtime id of the layer
00995   // has to be changed. This list keeps track of the
00996   // changes so we can determine which runtime layer
00997   // correspondes to a layer in m_layers[].
00998   // The first index id in the pair is the runtime
00999   // id and the second id in the pair is the m_layer[]
01000   // id.
01001   ON_UuidPairList m_runtime_layer_id_map;
01002 };
01003 
01004 #undef ON_BOZO_VACCINE_11EE2C1FF90D4C6AA7CDEC8532E1E32D
01005 
01006 ON_OBJECT_IMPLEMENT(ON__IDefLayerSettingsUserData,ON_UserData,"11EE2C1F-F90D-4C6A-A7CD-EC8532E1E32D");
01007 
01008 ON__IDefLayerSettingsUserData* ON__IDefLayerSettingsUserData::FindOrCreate(const ON_InstanceDefinition& idef,bool bCreate)
01009 {
01010   ON__IDefLayerSettingsUserData* ud = ON__IDefLayerSettingsUserData::Cast(idef.GetUserData(ON__IDefLayerSettingsUserData::m_ON__IDefLayerSettingsUserData_class_id.Uuid()));
01011   if ( !ud && bCreate )
01012   {
01013     ud = new ON__IDefLayerSettingsUserData();
01014     const_cast<ON_InstanceDefinition&>(idef).AttachUserData(ud);
01015   }
01016   return ud;
01017 }
01018 
01019 ON__IDefLayerSettingsUserData::ON__IDefLayerSettingsUserData()
01020 {
01021   m_userdata_uuid = ON__IDefLayerSettingsUserData::m_ON__IDefLayerSettingsUserData_class_id.Uuid();
01022   m_application_uuid = ON_opennurbs5_id;
01023   m_userdata_copycount = 1;
01024   CreateHelper();
01025 }
01026 
01027 ON__IDefLayerSettingsUserData::~ON__IDefLayerSettingsUserData()
01028 {
01029   DestroyHelper();
01030 }
01031 
01032 ON__IDefLayerSettingsUserData::ON__IDefLayerSettingsUserData(const ON__IDefLayerSettingsUserData& src)
01033 : ON_UserData(src)
01034 {
01035   m_userdata_uuid = ON__IDefLayerSettingsUserData::m_ON__IDefLayerSettingsUserData_class_id.Uuid();
01036   m_application_uuid = ON_opennurbs5_id;
01037   CreateHelper();
01038   CopyHelper(src);
01039 }
01040 
01041 ON__IDefLayerSettingsUserData& ON__IDefLayerSettingsUserData::operator=(const ON__IDefLayerSettingsUserData& src)
01042 {
01043   if ( this != &src )
01044   {
01045     DestroyHelper();
01046     ON_UserData::operator=(src);
01047     CopyHelper(src);
01048   }
01049   return *this;
01050 }
01051 
01052 // virtual ON_Object override
01053 ON_BOOL32 ON__IDefLayerSettingsUserData::IsValid( ON_TextLog* text_log ) const
01054 {
01055   return true;
01056 }
01057 
01058 // virtual ON_Object override
01059 unsigned int ON__IDefLayerSettingsUserData::SizeOf() const
01060 {
01061   return (unsigned int)(sizeof(*this));
01062 }
01063 
01064 // virtual ON_Object override
01065 ON__UINT32 ON__IDefLayerSettingsUserData::DataCRC(ON__UINT32 current_remainder) const
01066 {
01067   ON__UINT32 crc = current_remainder;
01068   for ( int i = 0; i < m_layers.Count(); i++ )
01069     crc = m_layers.DataCRC(crc);
01070   return crc;
01071 }
01072 
01073 // virtual ON_Object override
01074 ON_BOOL32 ON__IDefLayerSettingsUserData::Write(ON_BinaryArchive& binary_archive) const
01075 {
01076   bool rc = binary_archive.BeginWrite3dmChunk(TCODE_ANONYMOUS_CHUNK,1,1);
01077   if ( !rc )
01078     return false;
01079 
01080   rc = false;
01081   for(;;)
01082   {
01083     if ( !binary_archive.WriteArray(m_layers.Count(),m_layers.Array()) )
01084       break;
01085 
01086     // added in version 1.1 chunks
01087     bool bHaveParentLayer = ( 0 != m_idef_layer_table_parent_layer );
01088     if ( !binary_archive.WriteBool(bHaveParentLayer) )
01089       break;
01090 
01091     if ( bHaveParentLayer )
01092     {
01093       if ( !binary_archive.WriteObject(m_idef_layer_table_parent_layer) )
01094         break;
01095     }
01096 
01097     rc = true;
01098     break;
01099   }
01100 
01101   if ( !binary_archive.EndWrite3dmChunk() )
01102     rc = false;
01103 
01104   return rc;
01105 }
01106 
01107 // virtual ON_Object override
01108 ON_BOOL32 ON__IDefLayerSettingsUserData::Read(ON_BinaryArchive& binary_archive)
01109 {
01110   DestroyHelper();
01111 
01112   int major_version = 0;
01113   int minor_version = 0;
01114   bool rc = binary_archive.BeginRead3dmChunk(TCODE_ANONYMOUS_CHUNK,&major_version,&minor_version);
01115   if ( !rc )
01116     return false;
01117 
01118   rc = false;
01119   while ( 1 == major_version )
01120   {
01121     if ( !binary_archive.ReadArray(m_layers) )
01122       break;
01123 
01124     if ( minor_version <= 0 )
01125     {
01126       rc = true;
01127       break;
01128     }
01129 
01130     // added in version 1.1 chunks
01131     bool bHaveParentLayer = false;
01132     if ( !binary_archive.ReadBool(&bHaveParentLayer) )
01133       break;
01134 
01135     if ( bHaveParentLayer )
01136     {
01137       ON_Object* p = 0;
01138       if ( !binary_archive.ReadObject(&p) || 0 == p )
01139       {
01140         if (p)
01141         {
01142           delete p;
01143           break;
01144         }
01145       }
01146 
01147       m_idef_layer_table_parent_layer = ON_Layer::Cast(p);
01148       if ( 0 == m_idef_layer_table_parent_layer )
01149       {
01150         delete p;
01151         break;
01152       }
01153     }
01154 
01155     rc = true;
01156     break;
01157   }
01158 
01159   if ( !binary_archive.EndRead3dmChunk() )
01160     rc = false;
01161 
01162   return rc;
01163 }
01164 
01165 // virtual ON_UserData override
01166 ON_BOOL32 ON__IDefLayerSettingsUserData::Archive() const
01167 {
01168   // don't save empty settings
01169   return m_layers.Count() > 0;
01170 }
01171 
01172 // virtual ON_UserData override
01173 ON_BOOL32 ON__IDefLayerSettingsUserData::GetDescription( ON_wString& description )
01174 {
01175   description = L"Linked Instance Definition Layer Settings";
01176   return true;
01177 }
01178 
01179 
01180 
01181 bool ON_InstanceDefinition::HasLinkedIdefLayerSettings() const
01182 {
01183   const ON__IDefLayerSettingsUserData* ud = ON__IDefLayerSettingsUserData::FindOrCreate(*this,false);
01184   return ud ? (ud->m_layers.Count() > 0) : false;
01185 }
01186 
01254 
01255 static int compareLayerPtrId(const void* A, const void*B)
01256 {
01257   if ( 0 == A )
01258   {
01259     return 0 == B ? 0 : -1;
01260   }
01261   if ( 0 == B )
01262   {
01263     return 1;
01264   }
01265 
01266   const ON_Layer* a = (0!=A) ? ( *((ON_Layer**)A) ) : 0;
01267   const ON_Layer* b = (0!=B) ? ( *((ON_Layer**)B) ) : 0;
01268   if ( 0 == a )
01269   {
01270     return (0 == b) ? 0 : -1;
01271   }
01272   if ( 0 == b )
01273   {
01274     return 1;
01275   }
01276 
01277   // NOTE WELL:
01278   //   Compare only m_layer_id.  Other values may differ and
01279   //   adding compares to them will break the code that uses
01280   //   this function.
01281   return ON_UuidCompare(a->m_layer_id,b->m_layer_id);
01282 }
01283 
01284 static int compareUuidIndexId(const void* a, const void* b)
01285 {
01286   return ON_UuidIndex::CompareId((const ON_UuidIndex*)a,(const ON_UuidIndex*)b);
01287 }
01288 
01289 void ON_InstanceDefinition::UpdateLinkedIdefReferenceFileLayerRuntimeId( const ON_UuidPairList& id_map )
01290 {
01291   ON__IDefLayerSettingsUserData* ud = ON__IDefLayerSettingsUserData::FindOrCreate(*this,false);
01292   if ( 0 == ud || ud->m_layers.Count() <= 0 )
01293     return;
01294   ud->m_runtime_layer_id_map = id_map;
01295   ud->m_runtime_layer_id_map.ImproveSearchSpeed();
01296 }
01297 
01298 void ON_InstanceDefinition::UpdateLinkedIdefParentLayerSettings( const ON_Layer* linked_idef_parent_layer )
01299 {
01300   bool bCreate = ( 0 != linked_idef_parent_layer );
01301   ON__IDefLayerSettingsUserData* ud = ON__IDefLayerSettingsUserData::FindOrCreate(*this,bCreate);
01302   if ( 0 != ud && ud->m_idef_layer_table_parent_layer != linked_idef_parent_layer )
01303   {
01304     if ( ud->m_idef_layer_table_parent_layer )
01305     {
01306       delete ud->m_idef_layer_table_parent_layer;
01307       ud->m_idef_layer_table_parent_layer = 0;
01308     }
01309     if ( 0 != linked_idef_parent_layer )
01310     {
01311       ud->m_idef_layer_table_parent_layer = new ON_Layer( *linked_idef_parent_layer );
01312     }
01313   }
01314 }
01315 
01316 const ON_Layer* ON_InstanceDefinition::LinkedIdefParentLayerSettings() const
01317 {
01318   ON__IDefLayerSettingsUserData* ud = ON__IDefLayerSettingsUserData::FindOrCreate(*this,false);
01319   return (0 != ud) ? ud->m_idef_layer_table_parent_layer : 0;
01320 }
01321 
01322 void ON_InstanceDefinition::UpdateLinkedIdefReferenceFileLayerSettings( unsigned int layer_count, ON_Layer** layer_settings )
01323 {
01324   ON__IDefLayerSettingsUserData* ud;
01325 
01326   if ( layer_count <= 0 || 0 == layer_settings )
01327   {
01328     // delete linked idef layer settings
01329     ud = ON__IDefLayerSettingsUserData::FindOrCreate(*this,false);
01330     if ( 0 != ud )
01331       delete ud;
01332     return;
01333   }
01334 
01335   // Create an index_map[] into the layer_settings[] array that is sorted
01336   // by layer_settings[]->m_layer_id
01337   ON_Workspace ws;
01338   int* index_map = (int*)ws.GetMemory(layer_count*sizeof(index_map[0]));
01339   ON_Sort(ON::quick_sort,index_map,layer_settings,layer_count,sizeof(layer_settings[0]),compareLayerPtrId);
01340 
01341   // Use index_map[] to get a unique list of layers with valid ids
01342   ON_UuidIndex* iddex = (ON_UuidIndex*)ws.GetMemory(layer_count*sizeof(iddex[0]));
01343   unsigned int iddex_count = 0;
01344   unsigned int i;
01345   ON_Layer* layer;
01346   for ( i = 0; i < layer_count; i++ )
01347   {
01348     layer = layer_settings[index_map[i]];
01349     if ( 0 == layer )
01350       continue;
01351     layer->SaveSettings(0,false); // remove any saved settings on input layers
01352     if ( ON_UuidIsNil(layer->m_layer_id) )
01353       continue;
01354     if ( iddex_count > 0 && iddex[iddex_count-1].m_id == layer->m_layer_id )
01355       continue;
01356     iddex[iddex_count].m_i = index_map[i];
01357     iddex[iddex_count].m_id = layer->m_layer_id;
01358     iddex_count++;
01359   }
01360 
01361   if ( iddex_count <= 0 )
01362   {
01363     // delete settings
01364     UpdateLinkedIdefReferenceFileLayerSettings(0,0);
01365     return;
01366   }
01367 
01368   // Create or get user data where the saved layer settings
01369   // are stored.
01370   ud = ON__IDefLayerSettingsUserData::FindOrCreate(*this,true);
01371   if ( 0 == ud )
01372     return;
01373     
01374   // Go through the saved settings that were previously
01375   // on this idef apply those settings to the layer_settings[]
01376   // list. Then delete the information from ud->m_layers[].
01377   ON_UuidIndex idx;
01378   idx.m_i = 0;
01379   unsigned int settings;
01380   for ( i = 0; i < ud->m_layers.UnsignedCount(); i++ )
01381   {
01382     if ( 0 == ud->m_layers[i] )
01383       continue;
01384     layer = ud->m_layers[i];
01385     ud->m_layers[i] = 0;
01386     for(;;)
01387     {
01388       settings = layer->SavedSettings();
01389       if ( 0 == settings )
01390         break; // no settings were modified
01391       idx.m_id = layer->m_layer_id;
01392       const ON_UuidIndex* idx0 = (const ON_UuidIndex*)bsearch(&idx,iddex,iddex_count,sizeof(iddex[0]),compareUuidIndexId);
01393       if ( 0 == idx0)
01394         break; // this layer is not in the current layer_settings[] list
01395       layer_settings[idx0->m_i]->SaveSettings(settings,false); // saves the layer settings found in linked file
01396       layer_settings[idx0->m_i]->Set(settings,*layer);   // applies modifications found on idef
01397       break;
01398     }
01399     delete layer;
01400   }
01401 
01402   // Save a copy of this information on the user data
01403   // so it will persist in the file containing the idef.
01404   ud->m_layers.SetCount(0);
01405   ud->m_layers.Reserve(iddex_count);
01406   for ( i = 0; i < iddex_count; i++ )
01407   {
01408     layer = new ON_Layer( *layer_settings[iddex[i].m_i] );
01409     ud->m_layers.Append(layer);
01410   }
01411 }
01412 
01413 
01414 
01415 void ON_InstanceDefinition::UpdateLinkedIdefLayerSettings( unsigned int layer_count, const ON_Layer*const* layer_settings )
01416 {
01417   if ( layer_count <= 0 || 0 == layer_settings )
01418   {
01419     // delete linked idef layer settings
01420     UpdateLinkedIdefReferenceFileLayerSettings(0,0);
01421     return;
01422   }
01423 
01424   // Get layer information (saved on this idef) from the linked file 
01425   ON__IDefLayerSettingsUserData* ud = ON__IDefLayerSettingsUserData::FindOrCreate(*this,false);
01426   if ( 0 == ud )
01427     return;
01428   if ( ud->m_layers.Count() <= 0 )
01429   {
01430     delete ud;
01431     return;
01432   }
01433 
01434   // Apply any saved settings
01435   ON_Layer** ud_layers = ud->m_layers.Array();
01436   size_t ud_layers_count = ud->m_layers.Count();
01437   ON_Layer layerId;
01438   const ON_Layer* layerPtrId = &layerId;
01439   for ( unsigned int i = 0; i < layer_count; i++ )
01440   {
01441     const ON_Layer* layer1 = layer_settings[i];
01442     if ( !ud->m_runtime_layer_id_map.FindId1(layer1->m_layer_id,&layerId.m_layer_id) )
01443       layerId.m_layer_id = layer1->m_layer_id;
01444     ON_Layer** pp = (ON_Layer**)bsearch(&layerPtrId,ud_layers,ud_layers_count,sizeof(ud_layers[0]),compareLayerPtrId);
01445     ON_Layer* layer0 = (0 != pp) ? *pp : 0;
01446     if ( 0 == layer0 )
01447       continue;
01448     unsigned int settings0 = layer0->SavedSettings();
01449     unsigned int settings1 = ON_Layer::Differences(*layer0,*layer1);
01450     // settings0 = settings changes
01451     // new_settings = settings changed since we opened the model
01452     unsigned int new_settings = (settings1 ^ (settings0 & settings1));
01453     if ( 0 != new_settings )
01454       layer0->SaveSettings( new_settings, true );
01455     layer0->Set((settings0|settings1),*layer1);
01456   }
01457 }
01458 
01459 
01460 
01462 
01463 
01464 class /*NEVER EXPORT THIS CLASS DEFINITION*/ ON__IDefAlternativePathUserData : public ON_UserData
01465 {
01466 #if !defined(ON_BOZO_VACCINE_F42D967121EB46929B9ABC3507FF28F5)
01467 #error Never copy this class definition or put this definition in a header file!
01468 #endif
01469   ON_OBJECT_DECLARE(ON__IDefAlternativePathUserData);
01470 
01471 public:
01472   ON__IDefAlternativePathUserData();
01473   ~ON__IDefAlternativePathUserData();
01474   // default copy constructor and operator= work fine.
01475 
01476   ON__IDefAlternativePathUserData(const ON__IDefAlternativePathUserData& src);
01477   ON__IDefAlternativePathUserData& operator=(const ON__IDefAlternativePathUserData& src);
01478 
01479 
01480   static ON__IDefAlternativePathUserData* FindOrCreate(const ON_InstanceDefinition& idef,bool bCreate);
01481   
01482 private:
01483   void CreateHelper()
01484   {
01485     m_alternate_path.Destroy();
01486     m_bRelativePath = false;
01487   }
01488 
01489   void CopyHelper(const ON__IDefAlternativePathUserData& src)
01490   {
01491     m_alternate_path = src.m_alternate_path;
01492     m_bRelativePath = src.m_bRelativePath;
01493   }
01494 
01495   void DestroyHelper()
01496   {
01497     m_alternate_path.Destroy();
01498     m_bRelativePath = false;
01499   }
01500 
01501 public:
01502   // virtual ON_Object override
01503   ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
01504   // virtual ON_Object override
01505   unsigned int SizeOf() const;
01506   // virtual ON_Object override
01507   ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
01508   // virtual ON_Object override
01509   ON_BOOL32 Write(ON_BinaryArchive& binary_archive) const;
01510   // virtual ON_Object override
01511   ON_BOOL32 Read(ON_BinaryArchive& binary_archive);
01512   // virtual ON_UserData override
01513   ON_BOOL32 Archive() const;
01514   // virtual ON_UserData override
01515   ON_BOOL32 GetDescription( ON_wString& description );
01516 
01517 public:
01518   ON_wString m_alternate_path;
01519   bool m_bRelativePath;
01520 };
01521 
01522 #undef ON_BOZO_VACCINE_F42D967121EB46929B9ABC3507FF28F5
01523 
01524 ON_OBJECT_IMPLEMENT(ON__IDefAlternativePathUserData,ON_UserData,"F42D9671-21EB-4692-9B9A-BC3507FF28F5");
01525 
01526 ON__IDefAlternativePathUserData* ON__IDefAlternativePathUserData::FindOrCreate(const ON_InstanceDefinition& idef,bool bCreate)
01527 {
01528   ON__IDefAlternativePathUserData* ud = ON__IDefAlternativePathUserData::Cast(idef.GetUserData(ON__IDefAlternativePathUserData::m_ON__IDefAlternativePathUserData_class_id.Uuid()));
01529   if ( !ud && bCreate )
01530   {
01531     ud = new ON__IDefAlternativePathUserData();
01532     const_cast<ON_InstanceDefinition&>(idef).AttachUserData(ud);
01533   }
01534   return ud;
01535 }
01536 
01537 ON__IDefAlternativePathUserData::ON__IDefAlternativePathUserData()
01538 {
01539   m_userdata_uuid = ON__IDefAlternativePathUserData::m_ON__IDefAlternativePathUserData_class_id.Uuid();
01540   m_application_uuid = ON_opennurbs5_id;
01541   m_userdata_copycount = 1;
01542   CreateHelper();
01543 }
01544 
01545 ON__IDefAlternativePathUserData::~ON__IDefAlternativePathUserData()
01546 {
01547   DestroyHelper();
01548 }
01549 
01550 ON__IDefAlternativePathUserData::ON__IDefAlternativePathUserData(const ON__IDefAlternativePathUserData& src)
01551 : ON_UserData(src)
01552 {
01553   m_userdata_uuid = ON__IDefAlternativePathUserData::m_ON__IDefAlternativePathUserData_class_id.Uuid();
01554   m_application_uuid = ON_opennurbs5_id;
01555   CreateHelper();
01556   CopyHelper(src);
01557 }
01558 
01559 ON__IDefAlternativePathUserData& ON__IDefAlternativePathUserData::operator=(const ON__IDefAlternativePathUserData& src)
01560 {
01561   if ( this != &src )
01562   {
01563     DestroyHelper();
01564     ON_UserData::operator=(src);
01565     CopyHelper(src);
01566   }
01567   return *this;
01568 }
01569 
01570 // virtual ON_Object override
01571 ON_BOOL32 ON__IDefAlternativePathUserData::IsValid( ON_TextLog* text_log ) const
01572 {
01573   return !m_alternate_path.IsEmpty();
01574 }
01575 
01576 // virtual ON_Object override
01577 unsigned int ON__IDefAlternativePathUserData::SizeOf() const
01578 {
01579   return (unsigned int)(sizeof(*this) + m_alternate_path.SizeOf());
01580 }
01581 
01582 // virtual ON_Object override
01583 ON__UINT32 ON__IDefAlternativePathUserData::DataCRC(ON__UINT32 current_remainder) const
01584 {
01585   ON__UINT32 crc = ON_CRC32(current_remainder,sizeof(m_bRelativePath),&m_bRelativePath);
01586   crc = m_alternate_path.DataCRC(crc);
01587   return crc;
01588 }
01589 
01590 // virtual ON_Object override
01591 ON_BOOL32 ON__IDefAlternativePathUserData::Write(ON_BinaryArchive& binary_archive) const
01592 {
01593   bool rc = binary_archive.BeginWrite3dmChunk(TCODE_ANONYMOUS_CHUNK,1,0);
01594   if ( !rc )
01595     return false;
01596 
01597   rc = false;
01598   for(;;)
01599   {
01600     if ( !binary_archive.WriteString(m_alternate_path) )
01601       break;
01602     if ( !binary_archive.WriteBool(m_bRelativePath) )
01603       break;
01604     rc = true;
01605     break;
01606   }
01607 
01608   if ( !binary_archive.EndWrite3dmChunk() )
01609     rc = false;
01610 
01611   return rc;
01612 }
01613 
01614 // virtual ON_Object override
01615 ON_BOOL32 ON__IDefAlternativePathUserData::Read(ON_BinaryArchive& binary_archive)
01616 {
01617   DestroyHelper();
01618 
01619   int major_version = 0;
01620   int minor_version = 0;
01621   bool rc = binary_archive.BeginRead3dmChunk(TCODE_ANONYMOUS_CHUNK,&major_version,&minor_version);
01622   if ( !rc )
01623     return false;
01624 
01625   rc = false;
01626   while ( 1 == major_version )
01627   {
01628     if ( !binary_archive.ReadString(m_alternate_path) )
01629       break;
01630     if ( !binary_archive.ReadBool(&m_bRelativePath) )
01631       break;
01632     rc = true;
01633     break;
01634   }
01635 
01636   if ( !binary_archive.EndRead3dmChunk() )
01637     rc = false;
01638 
01639   return rc;
01640 }
01641 
01642 // virtual ON_UserData override
01643 ON_BOOL32 ON__IDefAlternativePathUserData::Archive() const
01644 {
01645   // don't save empty settings
01646   return !m_alternate_path.IsEmpty();
01647 }
01648 
01649 // virtual ON_UserData override
01650 ON_BOOL32 ON__IDefAlternativePathUserData::GetDescription( ON_wString& description )
01651 {
01652   description = L"Linked Instance Definition Alternate Path";
01653   return true;
01654 }
01655 
01656 
01657 void ON_InstanceDefinition::SetAlternateSourceArchivePath( 
01658       const wchar_t* alternate_source_archive_path,
01659       bool bRelativePath
01660       )
01661 {
01662   ON_wString s;
01663   if ( 0 != alternate_source_archive_path )
01664   {
01665     s = alternate_source_archive_path;
01666     s.TrimLeftAndRight();
01667     alternate_source_archive_path = s;
01668     if ( 0 != alternate_source_archive_path && 0 == alternate_source_archive_path[0] )
01669       alternate_source_archive_path = 0;
01670   }
01671   ON__IDefAlternativePathUserData* ud = ON__IDefAlternativePathUserData::FindOrCreate(*this,0!=alternate_source_archive_path);
01672   if ( 0 != ud )
01673   {
01674     if ( 0 == alternate_source_archive_path )
01675       delete ud;
01676     else
01677     {
01678       ud->m_alternate_path = alternate_source_archive_path;
01679       ud->m_bRelativePath = bRelativePath;
01680     }
01681   }
01682 }
01683 
01684 bool ON_InstanceDefinition::GetAlternateSourceArchivePath( 
01685       ON_wString& alternate_source_archive_path,
01686       bool& bRelativePath
01687       ) const
01688 {
01689   const ON__IDefAlternativePathUserData* ud = ON__IDefAlternativePathUserData::FindOrCreate(*this,false);
01690   const wchar_t* s = (0 != ud) ? ((const wchar_t*)ud->m_alternate_path) : 0;
01691   if ( 0 != s && 0 != s[0] )
01692   {
01693     alternate_source_archive_path = s;
01694     bRelativePath = ud->m_bRelativePath;
01695   }
01696   else
01697   {
01698     alternate_source_archive_path.Destroy();
01699     bRelativePath = false;
01700   }
01701   return !alternate_source_archive_path.IsEmpty();
01702 }


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