opennurbs_layer.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 ON_OBJECT_IMPLEMENT(ON_Layer,ON_Object,"95809813-E985-11d3-BFE5-0010830122F0");
00020 
00021 #define ON_BOZO_VACCINE_3E4904E6E9304fbcAA42EBD407AEFE3B
00022 #define ON_BOZO_VACCINE_BFB63C094BC7472789BB7CC754118200
00023 
00024 ON_Layer::ON_Layer() 
00025 : m_extension_bits(0)
00026 {
00027   Default();
00028 }
00029 
00030 void ON_Layer::Default()
00031 {
00032   m_layer_id = ON_nil_uuid;
00033   m_parent_layer_id = ON_nil_uuid;
00034   m_layer_index = -1; // 10 March 2006 Dale Lear - changed from 0 to -1
00035   m_iges_level = -1; 
00036   m_material_index = -1; 
00037   m_rendering_attributes.Default();
00038   m_linetype_index = -1;
00039   m_color.SetRGB(0,0,0);
00040   m_display_material_id = ON_nil_uuid;
00041   m_plot_color = ON_UNSET_COLOR;
00042   m_plot_weight_mm = 0.0;
00043   m_name.Destroy();
00044   m_bVisible = true;
00045   m_bLocked = false;
00046   m_bExpanded = true;
00047   m_extension_bits = 0;
00048 }
00049 
00050 ON_Layer::~ON_Layer()
00051 {
00052   m_name.Destroy();
00053 }
00054 
00055 static void SetExtensionBit( unsigned char* layer_m_extension_bits, unsigned char mask )
00056 {
00057   *layer_m_extension_bits |= mask;
00058 }
00059 
00060 static void ClearExtensionBit(  unsigned char* layer_m_extension_bits, unsigned char mask )
00061 {
00062   unsigned char notmask = ~mask;
00063   *layer_m_extension_bits &= notmask;
00064 }
00065 
00066 static bool ExtensionBit( unsigned char layer_m_extension_bits, unsigned char mask )
00067 {
00068   return (0 != (layer_m_extension_bits & mask));
00069 }
00070 
00071 ON_BOOL32 ON_Layer::IsValid( ON_TextLog* text_log ) const
00072 {
00073   if ( m_name.IsEmpty() )
00074   {
00075     if ( text_log )
00076     {
00077       text_log->Print("Layer name is empty.\n");
00078     }
00079     return false;
00080   }
00081   return true;
00082 }
00083 
00084 const wchar_t* ON::NameReferenceDelimiter()
00085 {
00086   // If this string is changed, also update code
00087   // in ON::NameReferenceDelimiterLength().
00088   return L" : ";
00089 }
00090 
00091 unsigned int ON::NameReferenceDelimiterLength()
00092 {
00093   return 3;
00094 }
00095 
00096 const wchar_t* ON::IsNameReferenceDelimiter(const wchar_t* s)
00097 {
00098   const wchar_t* d = ON::NameReferenceDelimiter();
00099   if ( 0 != s )
00100   {
00101     while ( 0 != *d && *d == *s )
00102     {
00103       d++;
00104       s++;
00105     }
00106     if ( 0 == *d )
00107       return s;
00108   }
00109   return 0;
00110 }
00111 
00112 
00113 
00114 const wchar_t* ON_Layer::LayerNameReferenceDelimiter()
00115 {
00116   return ON::NameReferenceDelimiter();
00117 }
00118 
00119 const wchar_t* ON_Layer::LayerNamePathDelimiter()
00120 {
00121   return L"::";
00122 }
00123 
00124 static const wchar_t* LayerFullName( const wchar_t* s0 )
00125 {
00126   if ( 0 == s0 || 0 == s0[0] )
00127     return 0;
00128   const wchar_t* t;
00129   const wchar_t* d;
00130   const wchar_t* d0 = ON_Layer::LayerNameReferenceDelimiter();
00131   const wchar_t* s = s0;
00132 
00133   // start at the beginning and look for a reference delimiter
00134   while ( 0 != *s )
00135   {
00136     if ( *s == *d0 )
00137     {
00138       d = d0;
00139       t = s;
00140       while ( *t == *d)
00141       {
00142         t++;
00143         d++;
00144         if ( 0 == *d )
00145         {
00146           return ((0 != *t) ? t : 0);
00147         }
00148       }
00149     }
00150     s++;
00151   }
00152   return s0;
00153 }
00154 
00155 
00156 static const wchar_t* LayerLeafName( const wchar_t* s )
00157 {
00158   // this static helper function assumes s0 does not being with "reference : ".
00159   if ( 0 == s || 0 == s[0] )
00160     return 0;
00161   
00162   const wchar_t* t;
00163   const wchar_t* d;
00164   const wchar_t* d0 = ON_Layer::LayerNamePathDelimiter();
00165   const wchar_t* s0 = s;
00166   
00167   while ( 0 != *s0 )
00168   {
00169     if ( *s0 == *d0 )
00170     {
00171       // NOTE:
00172       //  This code must work for a delimiter of length one or more
00173       //  so the string returned by ON_Layer::LayerNamePathDelimiter()
00174       //  can be adjusted as needed.
00175       d = d0;
00176       t = s0;
00177       while ( *t == *d)
00178       {
00179         t++;
00180         d++;
00181         if ( 0 == *d )
00182         {
00183           if ( 0 == *t )
00184             return 0;
00185           s = t;
00186           s0 = t-1;
00187           break;
00188         }
00189       }
00190     }
00191     s0++;
00192   }
00193 
00194   return s;
00195 }
00196 
00197 
00198 
00199 bool ON_Layer::GetLeafName( const wchar_t* layer_name, ON_wString& leaf_name)
00200 {
00201   const wchar_t* s0 = LayerFullName(layer_name);
00202   const wchar_t* s1 = LayerLeafName( s0 );
00203   if ( 0 != s1 && 0 != *s1 )
00204   {
00205     leaf_name = s1;
00206     return true;
00207   }
00208   leaf_name.Empty();
00209   return false;
00210 }
00211 
00212 bool ON_Layer::GetParentName( const wchar_t* layer_name, ON_wString& parent_path_name)
00213 {
00214   const wchar_t* s0 = LayerFullName(layer_name);
00215   const wchar_t* s1 = LayerLeafName( s0 );
00216   if ( 0 != s1 && 0 != *s1 && s0 < s1 )
00217   {
00218     // back up over the delimiter
00219     const wchar_t* d0 = ON_Layer::LayerNamePathDelimiter();
00220     const wchar_t* d = d0;
00221     while (*d)
00222       d++;
00223     while ( d > d0 && s0 < s1 && d[-1] == s1[-1] )
00224     {
00225       d--;
00226       s1--;
00227     }
00228     if ( s0 < s1 )
00229     {
00230       parent_path_name = s0;
00231       parent_path_name.SetLength(s1-s0);
00232       return true;
00233     }
00234   }
00235   parent_path_name.Empty();
00236   return false;
00237 }
00238 
00239 bool ON_Layer::RemoveReferenceName( const wchar_t* layer_name, ON_wString& layer_path_name)
00240 {
00241   const wchar_t* s = LayerFullName(layer_name);
00242   if ( 0 != s && 0 != *s )
00243   {
00244     layer_path_name = s;
00245     return true;
00246   }
00247   layer_path_name.Empty();
00248   return false;
00249 }
00250 
00251 bool ON_Layer::GetReferenceName( const wchar_t* layer_name, ON_wString& reference_name)
00252 {
00253   const wchar_t* s0 = layer_name;
00254   const wchar_t* s1 = LayerFullName(layer_name);
00255   if ( 0 != s1 && 0 != *s1 && s0 < s1 )
00256   {
00257     const wchar_t* d = ON_Layer::LayerNameReferenceDelimiter();
00258     while ( *d++ && s0 < s1 )
00259       s1--;
00260     if ( 0 != *s1 && s0 < s1 )
00261     {
00262       reference_name = s0;
00263       reference_name.SetLength(s1-s0);
00264       return true;
00265     }
00266   }
00267   reference_name.Empty();
00268   return false;
00269 }
00270 
00271 void ON_Layer::Dump( ON_TextLog& dump ) const
00272 {
00273   const wchar_t* sName = LayerName();
00274   if ( !sName )
00275     sName = L"";
00276   dump.Print("index = %d\n",m_layer_index);
00277   dump.Print("name = \"%ls\"\n",sName);
00278   dump.Print("display = %s\n",m_bVisible?"visible":"hidden");
00279   dump.Print("picking = %s\n",m_bLocked?"locked":"unlocked");
00280   dump.Print("display color rgb = "); dump.PrintRGB(m_color); dump.Print("\n");
00281   dump.Print("plot color rgb = "); dump.PrintRGB(m_plot_color); dump.Print("\n");
00282   dump.Print("default material index = %d\n",m_material_index);
00283 }
00284 
00285 ON_BOOL32 ON_Layer::Write(
00286        ON_BinaryArchive& file // serialize definition to binary archive
00287      ) const
00288 {
00289   int i;
00290   bool rc = file.Write3dmChunkVersion(1,8);
00291   while(rc)
00292   {
00293     // Save the visibility state this layer has when its parent
00294     // is visible ignoring current parent visibility value.
00295     bool bVisible = PersistentVisibility();
00296 
00297     // Save the locked state this layer has when its parent
00298     // is unlocked ignoring current parenting locked setting.
00299     const bool bLocked = PersistentLocking();
00300 
00301     // Save OBSOLETE mode value so we don't break file format
00302     if ( bVisible )
00303       i = 0; // "normal" layer mode
00304     else if ( bLocked )
00305       i = 2; // "locked" layer mode
00306     else
00307       i = 1; // "hidden" layer mode
00308 
00309     rc = file.WriteInt( i );
00310     if (!rc) break;
00311 
00312     rc = file.WriteInt( m_layer_index );
00313     if (!rc) break;
00314 
00315     rc = file.WriteInt( m_iges_level );
00316     if (!rc) break;
00317 
00318     rc = file.WriteInt( m_material_index );
00319     if (!rc) break;
00320 
00321     // Starting with version 200312110, this value is zero.  For files written
00322     // with earlier versions, the number was a "model index" value that was
00323     // set to something >= 1, but never used.  We have to continue to 
00324     // read/write an integer here so that old/new versions of opennurbs can
00325     // read files written by new/old versions.
00326     i = 0;
00327     rc = file.WriteInt( i );
00328     if (!rc) break;
00329 
00330     rc = file.WriteColor( m_color );
00331     if (!rc) break;
00332     
00333     {
00334       // OBSOLETE LINE STYLE if ( rc ) rc = file.WriteLineStyle( LineStyle() );
00335       // Starting with version 200503170, this section is "officially" not used.
00336       // Prior to that, it was old ON_LineStyle information that has
00337       // never been used.
00338       short s = 0;
00339       if (rc) rc = file.WriteShort(s);    // default pattern
00340       if (rc) rc = file.WriteShort(s);    // default pattern index
00341       if (rc) rc = file.WriteDouble(0.0); // default thickness
00342       if (rc) rc = file.WriteDouble(1.0); // default scale
00343     }
00344     if (!rc) break;
00345 
00346     rc = file.WriteString( m_name );
00347     if (!rc) break;
00348 
00349     // 1.1 fields
00350     rc = file.WriteBool(bVisible);
00351     if (!rc) break;
00352 
00353     // 1.2 field
00354     rc = file.WriteInt( m_linetype_index);
00355     if (!rc) break;
00356 
00357     // 1.3 field - 23 March 2005 Dale Lear
00358     rc = file.WriteColor( m_plot_color);
00359     if (!rc) break;
00360     rc = file.WriteDouble( m_plot_weight_mm);
00361     if (!rc) break;
00362 
00363     // 1.4 field - 3 May 2005 Dale Lear 
00364     //           - locked and visible are independent settings
00365     rc = file.WriteBool( bLocked );
00366     if (!rc) break;
00367 
00368     // 1.5 field
00369     rc = file.WriteUuid( m_layer_id );
00370     if (!rc) break;
00371 
00372     // 1.6 field
00373     rc = file.WriteUuid( m_parent_layer_id );
00374     if (!rc) break;
00375 
00376     // 1.6 field
00377     rc = file.WriteBool( m_bExpanded );
00378     if (!rc) break;
00379 
00380     // 1.7 field - added 6 June 2006
00381     rc = m_rendering_attributes.Write(file);
00382     if (!rc) break;
00383 
00384     // 1.8 field - added 19 Sep 2006
00385     rc = file.WriteUuid(m_display_material_id);
00386 
00387     break;
00388   }
00389 
00390   return rc;
00391 }
00392 
00393 ON_BOOL32 ON_Layer::Read(
00394        ON_BinaryArchive& file // restore definition from binary archive
00395      )
00396 {
00397   int obsolete_value1 = 0; // see ON_Layer::Write
00398   int major_version=0;
00399   int minor_version=0;
00400   int mode = ON::normal_layer;
00401   Default();
00402   ON_BOOL32 rc = file.Read3dmChunkVersion(&major_version,&minor_version);
00403   if ( rc && major_version == 1 ) 
00404   {
00405     // common to all 1.x formats
00406     if ( rc ) rc = file.ReadInt( &mode );
00407     if ( rc ) 
00408     {
00409       switch(mode)
00410       {
00411       case 0: // OBSOLETE ON::normal_layer
00412         m_bVisible = true;
00413         m_bLocked = false;
00414         break;
00415       case 1: // OBSOLETE ON::hidden_layer
00416         m_bVisible = false;
00417         m_bLocked = false;
00418         break;
00419       case 2: // OBSOLETE ON::locked_layer
00420         m_bVisible = true;
00421         m_bLocked = true;
00422         break;
00423       default:
00424         m_bVisible = true;
00425         m_bLocked = false;
00426         break;
00427       }
00428     }
00429     if ( rc ) rc = file.ReadInt( &m_layer_index );
00430     if ( rc ) rc = file.ReadInt( &m_iges_level );
00431     if ( rc ) rc = file.ReadInt( &m_material_index );
00432     if ( rc ) rc = file.ReadInt( &obsolete_value1 );
00433     if ( rc ) rc = file.ReadColor( m_color );
00434 
00435     {
00436       // OBSOLETE line style was never used - read and discard the next 20 bytes
00437       short s;
00438       double x;
00439       if (rc) file.ReadShort(&s);
00440       if (rc) file.ReadShort(&s);
00441       if (rc) file.ReadDouble(&x);
00442       if (rc) file.ReadDouble(&x);
00443     }
00444 
00445     if ( rc ) rc = file.ReadString( m_name );
00446     if ( rc && minor_version >= 1 )
00447     {
00448       rc = file.ReadBool(&m_bVisible);
00449       if ( rc && minor_version >= 2 )
00450       {
00451         rc = file.ReadInt( &m_linetype_index);
00452         if (rc && minor_version >= 3 )
00453         {
00454           // 23 March 2005 Dale Lear
00455           rc = file.ReadColor( m_plot_color);
00456           if (rc) rc = file.ReadDouble( &m_plot_weight_mm);
00457 
00458           if (rc && minor_version >= 4 )
00459           {
00460             rc = file.ReadBool(&m_bLocked);
00461             if (rc && minor_version >= 5 )
00462             {
00463               rc = file.ReadUuid(m_layer_id);
00464               if ( rc 
00465                    && minor_version >= 6 
00466                    && file.ArchiveOpenNURBSVersion() > 200505110
00467                  )
00468               {
00469                 // Some files saved with opennurbs version 200505110 
00470                 // do not contain correctly written m_parent_layer_id
00471                 // and m_bExpanded values.
00472                 // It is ok to default these values.
00473                 rc = file.ReadUuid(m_parent_layer_id);
00474                 if (rc)
00475                 {
00476                   if ( ON_UuidIsNotNil(m_parent_layer_id) )
00477                   {
00478                     if ( m_bVisible )
00479                       SetPersistentVisibility(true);
00480                     if ( !m_bLocked )
00481                       SetPersistentLocking(false);
00482                   }
00483                   rc = file.ReadBool(&m_bExpanded);
00484                 }
00485               }
00486 
00487               if ( rc && minor_version >= 7 )
00488               {
00489                 // 1.7 field - added 6 June 2006
00490                 rc = m_rendering_attributes.Read(file);
00491 
00492                 if ( rc && minor_version >= 8 )
00493                 {
00494                   // 1.8 field - added 19 Sep 2006
00495                   rc = file.ReadUuid(m_display_material_id);
00496                 }
00497               }
00498             }
00499           }
00500         }
00501       }
00502     }
00503 
00504     if ( ON_UuidIsNil(m_layer_id) )
00505     {
00506       // old files didn't have layer ids and we need unique ones.
00507       ON_CreateUuid(m_layer_id);
00508     }
00509   }
00510   else {
00511     ON_ERROR("ON_Layer::Read() encountered a layer written by future code.");
00512     rc = false;
00513   }
00514 
00515   return rc;
00516 }
00517 
00518 ON::object_type ON_Layer::ObjectType() const
00519 {
00520   return ON::layer_object;
00521 }
00522 
00524 //
00525 // Interface
00526 
00527 bool ON_Layer::SetLayerName( const char* s )
00528 {
00529   m_name = s;
00530   return IsValid()?true:false;
00531 }
00532 
00533 bool ON_Layer::SetLayerName( const wchar_t* s )
00534 {
00535   m_name = s;
00536   return IsValid()?true:false;
00537 }
00538 
00539 const ON_wString& ON_Layer::LayerName() const
00540 {
00541   return m_name;
00542 }
00543 
00544 void ON_Layer::SetColor( ON_Color c)
00545 {
00546   m_color = c;
00547 }
00548 
00549 void ON_Layer::SetPlotColor( ON_Color c)
00550 {
00551   m_plot_color = c;
00552 }
00553 
00554 ON_Color ON_Layer::Color() const
00555 {
00556   return m_color;
00557 }
00558 
00559 ON_Color ON_Layer::PlotColor() const
00560 {
00561   return ((m_plot_color == ON_UNSET_COLOR) ? m_color : m_plot_color);
00562 }
00563 
00564 bool ON_Layer::SetLinetypeIndex( int index)
00565 {
00566   if( index >= -1)
00567   {
00568     m_linetype_index = index;
00569     return true;
00570   }
00571   return false;
00572 }
00573 
00574 int ON_Layer::LinetypeIndex() const
00575 {
00576   return m_linetype_index;
00577 }
00578 
00579 bool ON_Layer::IsVisible() const
00580 {
00581   return m_bVisible;
00582 }
00583 
00584 void ON_Layer::SetVisible( bool bVisible )
00585 {
00586   m_bVisible = ( bVisible ? true : false );
00587   if ( ON_UuidIsNil(m_parent_layer_id) )
00588     UnsetPersistentVisibility();
00589   else if ( bVisible )
00590   {
00591     // When a parent layer is turned off, the m_bVisible value for
00592     // every child layer layer is set to false. When a parent layer
00593     // is turned on and the visible child setting is true, the
00594     // child's m_bVisible value is set to true.
00595     //
00596     // This call ensures that if, at some point in the future, the
00597     // parent layer is turned off and then turned back on, this
00598     // layer will get turned back on as well.
00599     SetPersistentVisibility(true);
00600   }
00601 }
00602 
00603 void ON_Layer::SetLocked( bool bLocked )
00604 {
00605   m_bLocked = ( bLocked ? true : false );
00606   if ( ON_UuidIsNil(m_parent_layer_id) )
00607     UnsetPersistentLocking();
00608   else if ( !bLocked )
00609   {
00610     // When a parent layer is locked off, the m_bLocked value for
00611     // every child layer layer is set to true. When a parent layer
00612     // is unlocked on and the locked child setting is false, the
00613     // child's m_bLocked value is set to false.
00614     //
00615     // This call ensures that if, at some point in the future, the
00616     // parent layer is locked and then unlocked, this layer will 
00617     // get unlocked on as well.
00618     SetPersistentLocking(false);
00619   }
00620 }
00621 
00622 bool ON_Layer::IsLocked() const
00623 {
00624   return m_bLocked;
00625 }
00626 
00627 bool ON_Layer::IsVisibleAndNotLocked() const
00628 {
00629   return (m_bVisible && !m_bLocked);
00630 }
00631 
00632 bool ON_Layer::IsVisibleAndLocked() const
00633 {
00634   return (m_bVisible && m_bLocked);
00635 }
00636 
00637 bool ON_Layer::SetRenderMaterialIndex( int i )
00638 {
00639   m_material_index = i;
00640   return true;
00641 }
00642 
00643 int ON_Layer::RenderMaterialIndex() const
00644 {
00645   return m_material_index;
00646 }
00647 
00648 bool ON_Layer::SetLayerIndex( int i )
00649 {
00650   m_layer_index = i;
00651   return true;
00652 }
00653 
00654 int ON_Layer::LayerIndex() const
00655 {
00656   return m_layer_index;
00657 }
00658 
00659 
00660 bool ON_Layer::SetIgesLevel( int level )
00661 {
00662   m_iges_level = level;
00663   return true;
00664 }
00665 
00666 int ON_Layer::IgesLevel() const
00667 {
00668   return m_iges_level;
00669 }
00670 
00671 double ON_Layer::PlotWeight() const
00672 {
00673   return m_plot_weight_mm;
00674 }
00675 
00676 void ON_Layer::SetPlotWeight(double plot_weight_mm)
00677 {
00678   m_plot_weight_mm = (ON_IsValid(plot_weight_mm) && (plot_weight_mm>0.0 || -1.0==plot_weight_mm) ) 
00679                    ? plot_weight_mm 
00680                    : 0.0;
00681 }
00682 
00683 
00685 //
00686 // BEGIN ON__LayerPerViewSettings class
00687 //
00688 
00689 class /*NEVER EXPORT THIS CLASS DEFINITION*/ ON__LayerPerViewSettings
00690 {
00691 #if !defined(ON_BOZO_VACCINE_3E4904E6E9304fbcAA42EBD407AEFE3B)
00692 #error Never copy this class definition or put this definition in a header file!
00693 #endif
00694 public:
00695   ON__LayerPerViewSettings();
00696   void SetDefaultValues();
00697   bool Write( const ON_Layer& layer, ON_BinaryArchive& binary_archive ) const;
00698   bool Read( const ON_Layer& layer, ON_BinaryArchive& binary_archive);
00699 
00700   ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
00701 
00702   ON_UUID m_viewport_id;   // id of the viewport with custom layer settings
00703                            // if this id is nil, then the rest of the settings
00704                            // in this class are meaningless.
00705   ON_Color m_color;        // ON_UNSET_COLOR means use ON_Layer::m_color
00706   ON_Color m_plot_color;   // ON_UNSET_COLOR means use ON_Layer::m_plot_color
00707   double m_plot_weight_mm; // ON_UNSET_VALUE means use ON_Layer::m_plot_weight_mm
00708 
00709   unsigned char m_visible; // 0 means use ON_Layer::m_bVisible
00710                            // 1 = visible in viewport
00711                            // 2 = off in viewport
00712   unsigned char m_persistent_visibility; // 0 = unset, 1 = visible, 2 = not visible
00713 
00714   static
00715   int Compare(
00716       const ON__LayerPerViewSettings* a,
00717       const ON__LayerPerViewSettings* b 
00718       );
00719 
00720   static
00721   int CompareViewportId(
00722       const ON__LayerPerViewSettings* a, 
00723       const ON__LayerPerViewSettings* b
00724       );
00725 
00726   /*
00727   Returns:
00728     A bitfield that sets the bits if a layer setting is
00729     per viewport for the specified for the viewport. 
00730     The ON_Layer::PER_VIEWPORT_SETTINGS enum values 
00731     which bits correspond to which settings. 
00732   Remarks:
00733     If m_viewport_id is nil, this function returns 0. 
00734   */
00735   unsigned int SettingsMask() const;
00736 
00737   /*
00738   Description:
00739     Copy specified settings from src to this class.
00740   Parameters:
00741     src - [in]
00742       settings to copy
00743     settings_mask - [in]
00744       a bitfield that specifies which settings to copy.  The bits
00745       are defined in the ON_Layer::PER_VIEWPORT_SETTINGS enum.
00746   */
00747   void CopySettings( 
00748       const ON__LayerPerViewSettings* src, 
00749       unsigned int settings_mask 
00750       );
00751 };
00752 
00753 ON__UINT32 ON__LayerPerViewSettings::DataCRC(ON__UINT32 current_remainder) const
00754 {
00755   const unsigned int settings_mask = SettingsMask();
00756   if ( 0 != settings_mask )
00757   {
00758     if ( 0 != (settings_mask & ON_Layer::per_viewport_id) )
00759       current_remainder = ON_CRC32(current_remainder,sizeof(m_viewport_id),&m_viewport_id);
00760     if ( 0 != (settings_mask & ON_Layer::per_viewport_color) )
00761       current_remainder = ON_CRC32(current_remainder,sizeof(m_color),&m_color);
00762     if ( 0 != (settings_mask & ON_Layer::per_viewport_plot_color) )
00763       current_remainder = ON_CRC32(current_remainder,sizeof(m_plot_color),&m_plot_color);
00764     if ( 0 != (settings_mask & ON_Layer::per_viewport_plot_weight) )
00765       current_remainder = ON_CRC32(current_remainder,sizeof(m_plot_weight_mm),&m_plot_weight_mm);
00766     if ( 0 != (settings_mask & ON_Layer::per_viewport_visible) )
00767       current_remainder = ON_CRC32(current_remainder,sizeof(m_visible),&m_visible);
00768     if ( 0 != (settings_mask & ON_Layer::per_viewport_persistent_visibility) )
00769       current_remainder = ON_CRC32(current_remainder,sizeof(m_persistent_visibility),&m_persistent_visibility);
00770   }
00771   return current_remainder;
00772 }
00773 
00774 void ON__LayerPerViewSettings::CopySettings( const ON__LayerPerViewSettings* src, unsigned int settings_mask )
00775 {
00776   if ( 0 != src && this != src && 0 != settings_mask )
00777   {
00778     if ( 0 != (settings_mask & ON_Layer::per_viewport_id) )
00779       m_viewport_id = src->m_viewport_id;
00780     if ( 0 != (settings_mask & ON_Layer::per_viewport_color) )
00781       m_color = src->m_color;
00782     if ( 0 != (settings_mask & ON_Layer::per_viewport_plot_color) )
00783       m_plot_color = src->m_plot_color;
00784     if ( 0 != (settings_mask & ON_Layer::per_viewport_plot_weight) )
00785       m_plot_weight_mm = src->m_plot_weight_mm;
00786     if ( 0 != (settings_mask & ON_Layer::per_viewport_visible) )
00787       m_visible = src->m_visible;
00788     if ( 0 != (settings_mask & ON_Layer::per_viewport_persistent_visibility) )
00789       m_persistent_visibility = src->m_persistent_visibility;
00790   }
00791 }
00792 
00793 int ON__LayerPerViewSettings::Compare( const ON__LayerPerViewSettings* a, const ON__LayerPerViewSettings* b )
00794 {
00795   int rc = ON_UuidCompare(a->m_viewport_id,b->m_viewport_id);
00796   if ( 0 == rc )
00797   {
00798     unsigned int abits = a->SettingsMask();
00799     unsigned int bbits = b->SettingsMask();
00800     rc = ((int)abits) - ((int)bbits);
00801     if ( 0 == rc )
00802     {
00803       if ( 0 != (ON_Layer::per_viewport_visible & abits) )
00804       {
00805         rc = ((int)a->m_visible) - ((int)b->m_visible);
00806       }
00807       if ( 0 == rc && 0 != (ON_Layer::per_viewport_persistent_visibility & abits) )
00808       {
00809         rc = ((int)a->m_persistent_visibility) - ((int)b->m_persistent_visibility);
00810       }
00811       if ( 0 == rc && 0 != (ON_Layer::per_viewport_color & abits) )
00812       {
00813         rc = ((int)a->m_color) - ((int)b->m_color);
00814       }
00815       if ( 0 == rc && 0 != (ON_Layer::per_viewport_plot_color & abits) )
00816       {
00817         rc = ((int)a->m_plot_color) - ((int)b->m_plot_color);
00818       }
00819       if ( 0 == rc && 0 != (ON_Layer::per_viewport_plot_weight & abits) )
00820       {
00821         if ( a->m_plot_weight_mm < b->m_plot_weight_mm )
00822           rc = -1;
00823         else if ( a->m_plot_weight_mm > b->m_plot_weight_mm )
00824           rc = 1;
00825       }
00826     }
00827   }
00828   return rc;
00829 }
00830 
00831 int ON__LayerPerViewSettings::CompareViewportId( const ON__LayerPerViewSettings* a, const ON__LayerPerViewSettings* b )
00832 {
00833   return ON_UuidCompare(a->m_viewport_id,b->m_viewport_id);
00834 }
00835 
00836 unsigned int ON__LayerPerViewSettings::SettingsMask() const
00837 {
00838   // It is critical that this function returns
00839   // zero when m_viewport_id = nil and returns
00840   // zero when no layer properties are overridden
00841   // for the specified viewport.
00842   unsigned int bits = 0;
00843   if ( !ON_UuidIsNil(m_viewport_id) )
00844   {
00845     if ( ON_UNSET_COLOR != m_color )
00846       bits |= ON_Layer::per_viewport_color;
00847     if ( ON_UNSET_COLOR != m_plot_color )
00848       bits |= ON_Layer::per_viewport_plot_color;
00849     if ( (m_plot_weight_mm >= 0.0 || -1.0 == m_plot_weight_mm) && ON_IsValid(m_plot_weight_mm) )
00850       bits |= ON_Layer::per_viewport_plot_weight;
00851     if ( 1 == m_visible || 2 == m_visible )
00852       bits |= ON_Layer::per_viewport_visible;
00853     if ( 1 == m_persistent_visibility || 2 == m_persistent_visibility )
00854       bits |= ON_Layer::per_viewport_persistent_visibility;
00855     // It is critical that bit "1" is set only if
00856     // some layer property is overridden.  That's 
00857     // why the 0 != bits test is here.
00858     if ( 0 != bits )
00859       bits |= ON_Layer::per_viewport_id;
00860   }
00861 
00862   return bits;
00863 }
00864 
00865 ON__LayerPerViewSettings::ON__LayerPerViewSettings()
00866 {
00867   SetDefaultValues();
00868 }
00869 
00870 void ON__LayerPerViewSettings::SetDefaultValues()
00871 {
00872   memset(this,0,sizeof(*this));
00873   m_color = ON_UNSET_COLOR;
00874   m_plot_color = ON_UNSET_COLOR;
00875   m_plot_weight_mm = ON_UNSET_VALUE;
00876 }
00877 
00878 bool ON__LayerPerViewSettings::Write(const ON_Layer& layer, ON_BinaryArchive& binary_archive) const
00879 {
00880   if ( !binary_archive.BeginWrite3dmChunk(TCODE_ANONYMOUS_CHUNK,1,2) )
00881     return false;
00882 
00883   bool rcc = false;
00884   for(;;)
00885   {
00886     // This complicated "bits" stuff is to minimize number of bytes
00887     // written in the file.  Even though long term storage space is 
00888     // nearly free, we have lots of customers who complain about 
00889     // large file size and so ...
00890     unsigned int bits = SettingsMask();
00891     if ( !binary_archive.WriteInt(1,&bits) )
00892       break;
00893     
00894     if ( 0 == bits )
00895     {
00896       rcc = true;
00897       break; // all settings are defaults or viewport_id is nil
00898     }
00899 
00900     if ( !binary_archive.WriteUuid(m_viewport_id) )
00901       break;
00902 
00903     if ( 0 != (ON_Layer::per_viewport_color & bits) )
00904     {
00905       if  ( !binary_archive.WriteColor(m_color) )
00906         break;
00907     }
00908 
00909     if ( 0 != (ON_Layer::per_viewport_plot_color & bits) )
00910     {
00911       if ( !binary_archive.WriteColor(m_plot_color) )
00912         break;
00913     }
00914 
00915     if ( 0 != (ON_Layer::per_viewport_plot_weight & bits) )
00916     {
00917       if ( !binary_archive.WriteDouble(m_plot_weight_mm) )
00918         break;
00919     }
00920 
00921     if ( 0 != (ON_Layer::per_viewport_visible & bits) )
00922     {
00923       if ( !binary_archive.WriteChar(m_visible) )
00924         break;
00925       // version 1.1 addition
00926       if ( !binary_archive.WriteChar(m_visible) ) // (makes old a file old rhinos can read)
00927         break;
00928     }
00929 
00930     // 1.2 addition
00931     if ( 0 != (ON_Layer::per_viewport_persistent_visibility & bits) )
00932     {
00933       if ( !binary_archive.WriteChar(m_persistent_visibility) )
00934         break;
00935     }
00936 
00937     rcc = true;
00938     break;
00939   }
00940 
00941   if ( !binary_archive.EndWrite3dmChunk() )
00942     rcc = false;
00943 
00944   return rcc;
00945 }
00946 
00947 bool ON__LayerPerViewSettings::Read(const ON_Layer& layer, ON_BinaryArchive& binary_archive)
00948 {
00949   SetDefaultValues();
00950 
00951   int major_version = 0;
00952   int minor_version = 0;
00953   if ( !binary_archive.BeginRead3dmChunk(TCODE_ANONYMOUS_CHUNK,&major_version,&minor_version) )
00954     return false;
00955 
00956   bool rc = false;
00957   for(;;)
00958   {
00959     if (1 != major_version)
00960       break;
00961 
00962     // This complicated "bits" stuff is to minimize number of bytes
00963     // written in the file.  Even though long term storage space is 
00964     // nearly free, we have lots of customers who complain about 
00965     // large file size and so ...
00966     unsigned int bits = 0;
00967     if ( !binary_archive.ReadInt(1,&bits) )
00968       break;
00969     if ( 0 == bits )
00970     {
00971       rc = true;
00972       break;
00973     }
00974 
00975     if ( !binary_archive.ReadUuid(m_viewport_id) )
00976       break;
00977 
00978     if ( 0 != (ON_Layer::per_viewport_color & bits) )
00979     {
00980       if ( !binary_archive.ReadColor(m_color) )
00981         break;
00982     }
00983 
00984     if ( 0 != (ON_Layer::per_viewport_plot_color & bits) )
00985     {
00986       if ( !binary_archive.ReadColor(m_plot_color) )
00987         break;
00988     }
00989 
00990     if ( 0 != (ON_Layer::per_viewport_plot_weight & bits) )
00991     {
00992       if ( !binary_archive.ReadDouble(&m_plot_weight_mm) )
00993         break;
00994     }
00995 
00996     if ( 0 != (ON_Layer::per_viewport_visible & bits) )
00997     {
00998       if ( !binary_archive.ReadChar(&m_visible) )
00999         break;
01000       if ( minor_version >= 1 )
01001       {
01002         // for reading older Rhino files
01003         // Yes, writing m_visible and reading m_persistent_visibility is done on purpose.
01004         if ( !binary_archive.ReadChar(&m_persistent_visibility) )
01005           break;
01006       }
01007     }
01008 
01009     if ( minor_version >= 2 )
01010     {
01011       if ( 0 != (ON_Layer::per_viewport_persistent_visibility & bits) )
01012       {
01013         if ( !binary_archive.ReadChar(&m_persistent_visibility) )
01014           break;
01015       }
01016     }
01017 
01018     if ( ON_UuidIsNil(layer.m_parent_layer_id) )
01019       m_persistent_visibility = 0;
01020     rc = true;
01021     break;
01022   }
01023 
01024   if ( !binary_archive.EndRead3dmChunk() )
01025     rc = false;
01026 
01027   return rc;
01028 }
01029 
01030 
01031 //
01032 //
01033 // END ON__LayerPerViewSettings class
01034 //
01036 
01037 
01039 //
01040 // BEGIN ON__LayerExtensions user data class
01041 //
01042 
01043 class /*NEVER EXPORT THIS CLASS DEFINITION*/ ON__LayerExtensions : public ON_UserData
01044 {
01045 #if !defined(ON_BOZO_VACCINE_3E4904E6E9304fbcAA42EBD407AEFE3B)
01046 #error Never copy this class definition or put this definition in a header file!
01047 #endif
01048   ON_OBJECT_DECLARE(ON__LayerExtensions);
01049 
01050 public:
01051   ON__LayerExtensions();
01052   ~ON__LayerExtensions();
01053   // default copy constructor and operator= work fine.
01054 
01055 public:
01056   // virtual ON_Object override
01057   ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
01058   // virtual ON_Object override
01059   unsigned int SizeOf() const;
01060   // virtual ON_Object override
01061   ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
01062   // virtual ON_Object override
01063   ON_BOOL32 Write(ON_BinaryArchive& binary_archive) const;
01064   // virtual ON_Object override
01065   ON_BOOL32 Read(ON_BinaryArchive& binary_archive);
01066   // virtual ON_UserData override
01067   ON_BOOL32 Archive() const;
01068   // virtual ON_UserData override
01069   ON_BOOL32 GetDescription( ON_wString& description );
01070 
01071 public:
01072   bool IsEmpty() const;
01073 
01074   static
01075   ON__LayerPerViewSettings* ViewportSettings(
01076       const ON_Layer& layer, const unsigned char* layer_m_extension_bits, 
01077       ON_UUID viewport_id, 
01078       bool bCreate
01079       );
01080 
01081   static
01082   void DeleteViewportSettings(
01083       const ON_Layer& layer, const unsigned char* layer_m_extension_bits, 
01084       const ON__LayerPerViewSettings* vp_settings_to_delete
01085       );
01086 
01087   static
01088   ON__LayerExtensions* LayerExtensions(
01089       const ON_Layer& layer, const unsigned char* layer_m_extension_bits,
01090       bool bCreate
01091       );
01092 
01093   // per viewport overrides of color, linetype, plot color, plot weight, and visibility
01094   ON_SimpleArray<ON__LayerPerViewSettings> m_vp_settings;
01095 };
01096 
01097 #undef ON_BOZO_VACCINE_3E4904E6E9304fbcAA42EBD407AEFE3B
01098 
01099 ON_OBJECT_IMPLEMENT(ON__LayerExtensions,ON_UserData,"3E4904E6-E930-4fbc-AA42-EBD407AEFE3B");
01100 
01101 ON__LayerExtensions* ON__LayerExtensions::LayerExtensions(const ON_Layer& layer, const unsigned char* layer_m_extension_bits, bool bCreate)
01102 {
01103   ON__LayerExtensions* ud = ON__LayerExtensions::Cast(layer.GetUserData(ON__LayerExtensions::m_ON__LayerExtensions_class_id.Uuid()));
01104 
01105   if ( 0 == ud )
01106   {
01107     if ( bCreate )
01108     {
01109       ud = new ON__LayerExtensions();
01110       const_cast<ON_Layer&>(layer).AttachUserData(ud);
01111       // Clear 0x01 bit of ON_Layer::m_extension_bits so 
01112       // ON_Layer visibility and color queries will check
01113       // for ON__LayerExtensions userdata.
01114       ClearExtensionBit( const_cast<unsigned char*>(layer_m_extension_bits), 0x01 );
01115     }
01116     else
01117     {
01118       // Set 0x01 bit of ON_Layer::m_extension_bits so 
01119       // ON_Layer visibility and color queries will not
01120       // perform the expensive check for ON__LayerExtensions 
01121       // userdata. This speeds up visibility and color queries 
01122       // that occur millions of times when complicated models
01123       // are rendered.
01124       SetExtensionBit( const_cast<unsigned char*>(layer_m_extension_bits), 0x01 );
01125     }
01126   }
01127   else
01128   {
01129     // Clear 0x01 bit of ON_Layer::m_extension_bits so 
01130     // ON_Layer visibility and color queries will check
01131     // for ON__LayerExtensions userdata.
01132     ClearExtensionBit( const_cast<unsigned char*>(layer_m_extension_bits), 0x01 );
01133   }
01134 
01135   return ud;
01136 }
01137 
01138 ON__LayerExtensions::ON__LayerExtensions()
01139 {
01140   m_userdata_uuid = ON__LayerExtensions::m_ON__LayerExtensions_class_id.Uuid();
01141   m_application_uuid = ON_opennurbs5_id;
01142   m_userdata_copycount = 1;
01143 }
01144 
01145 ON__LayerExtensions::~ON__LayerExtensions()
01146 {
01147 }
01148 
01149 // virtual ON_Object override
01150 ON_BOOL32 ON__LayerExtensions::IsValid( ON_TextLog* text_log ) const
01151 {
01152   return true;
01153 }
01154 
01155 // virtual ON_Object override
01156 unsigned int ON__LayerExtensions::SizeOf() const
01157 {
01158   size_t sz = sizeof(*this) - sizeof(ON_UserData);
01159   sz += m_vp_settings.SizeOfArray();
01160   return (unsigned int)sz;
01161 }
01162 
01163 // virtual ON_Object override
01164 ON__UINT32 ON__LayerExtensions::DataCRC(ON__UINT32 current_remainder) const
01165 {
01166   ON__UINT32 crc = 0;
01167   crc = m_vp_settings.DataCRC(crc);
01168   return crc;
01169 }
01170 
01171 // virtual ON_Object override
01172 ON_BOOL32 ON__LayerExtensions::Write(ON_BinaryArchive& binary_archive) const
01173 {
01174   bool rc = binary_archive.BeginWrite3dmChunk(TCODE_ANONYMOUS_CHUNK,1,0);
01175   if ( !rc )
01176     return false;
01177 
01178   for(;;)
01179   {
01180     const ON_Layer* layer = ON_Layer::Cast( Owner() );
01181     if ( 0 == layer )
01182       break;
01183     int count = m_vp_settings.Count();
01184     rc = binary_archive.WriteInt(count);
01185     if ( !rc ) break;
01186     for ( int i = 0; i < count && rc; i++ )
01187     {
01188       rc = m_vp_settings[i].Write( *layer, binary_archive );
01189     }
01190     if (!rc) break;
01191 
01192     break;
01193   }
01194 
01195   if ( !binary_archive.EndWrite3dmChunk() )
01196     rc = false;
01197 
01198   return rc;
01199 }
01200 
01201 // virtual ON_Object override
01202 ON_BOOL32 ON__LayerExtensions::Read(ON_BinaryArchive& binary_archive)
01203 {
01204   m_vp_settings.SetCount(0);
01205 
01206   int major_version = 0;
01207   int minor_version = 0;
01208   bool rc = binary_archive.BeginRead3dmChunk(TCODE_ANONYMOUS_CHUNK,&major_version,&minor_version);
01209   if ( !rc )
01210     return false;
01211 
01212   for(;;)
01213   {
01214     const ON_Layer* layer = ON_Layer::Cast( Owner() );
01215     rc = ( 0 != layer );
01216     if (!rc) break;
01217 
01218     rc = (1 == major_version);
01219     if (!rc) break;
01220 
01221     int count = 0;
01222     rc = binary_archive.ReadInt(&count);
01223     if ( !rc ) break;
01224     m_vp_settings.Reserve(count);
01225     for ( int i = 0; i < count; i++ )
01226     {
01227       rc = m_vp_settings.AppendNew().Read(*layer,binary_archive);
01228       if (!rc) 
01229       {
01230         m_vp_settings.Remove();
01231         break;
01232       }
01233       if ( 0 == m_vp_settings.Last()->SettingsMask() )
01234         m_vp_settings.Remove();
01235     }
01236 
01237     // to make ON_Layer::PerViewportSettingsCRC() return
01238     // equal values for equal settings, it is critical
01239     // that m_vp_settings[] be sorted.
01240     m_vp_settings.QuickSort(ON__LayerPerViewSettings::Compare);
01241 
01242     if (!rc) break;
01243 
01244     break;
01245   }
01246 
01247   if ( !binary_archive.EndRead3dmChunk() )
01248     rc = false;
01249 
01250   return rc;
01251 }
01252 
01253 // virtual ON_UserData override
01254 ON_BOOL32 ON__LayerExtensions::Archive() const
01255 {
01256   return !IsEmpty();
01257 }
01258 
01259 // virtual ON_UserData override
01260 ON_BOOL32 ON__LayerExtensions::GetDescription( ON_wString& description )
01261 {
01262   description = L"Layer Extensions";
01263   return true;
01264 }
01265 
01266 ON__LayerPerViewSettings* ON__LayerExtensions::ViewportSettings( 
01267   const ON_Layer& layer,
01268   const unsigned char* layer_m_extension_bits,
01269   ON_UUID viewport_id,
01270   bool bCreate
01271   )
01272 {
01273   if ( !ON_UuidIsNil(viewport_id) )
01274   {
01275     ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(layer,layer_m_extension_bits,bCreate);
01276     if ( ud )
01277     {
01278       int i;
01279       const int vp_settings_count = ud->m_vp_settings.Count();
01280       ON__LayerPerViewSettings* vp_settings = ud->m_vp_settings.Array();
01281       for ( i = 0; i < vp_settings_count; i++ )
01282       {
01283         if ( 0 == memcmp(&viewport_id,&vp_settings[i].m_viewport_id,sizeof(ON_UUID)) )
01284           return (vp_settings+i);
01285       }
01286       if ( bCreate )
01287       {
01288         ON__LayerPerViewSettings& new_vp_settings = ud->m_vp_settings.AppendNew();
01289         vp_settings = ud->m_vp_settings.Array(); // appending can grow the array
01290         new_vp_settings.SetDefaultValues();
01291         new_vp_settings.m_viewport_id = viewport_id;
01292 
01293         // to make ON_Layer::PerViewportSettingsCRC() return
01294         // equal values for equal settings, it is critical
01295         // that m_vp_settings[] be sorted.
01296         ud->m_vp_settings.QuickSort(ON__LayerPerViewSettings::Compare);
01297 
01298         for ( i = 0; i <= vp_settings_count; i++ ) // "i <= ..." is correct because of the .AppendNew()
01299         {
01300           if ( 0 == memcmp(&viewport_id,&vp_settings[i].m_viewport_id,sizeof(ON_UUID)) )
01301             return (vp_settings+i);
01302         }
01303       }
01304     }
01305   }
01306   return 0;
01307 }
01308 
01309 void ON__LayerExtensions::DeleteViewportSettings( 
01310   const ON_Layer& layer, 
01311   const unsigned char* layer_m_extension_bits,
01312   const ON__LayerPerViewSettings* vp_settings_to_delete
01313   )
01314 {
01315   ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(layer,layer_m_extension_bits,false);
01316   if ( ud )
01317   {
01318     if ( 0 == vp_settings_to_delete )
01319     {
01320       delete ud;
01321       // Set bit 0x01 of ON_Layer::m_extension_bits to prevent
01322       // ON_Layer visibilty and color queries from wasting
01323       // time looking for userdata.
01324       SetExtensionBit( const_cast<unsigned char*>(layer_m_extension_bits), 0x01 );
01325     }
01326     else
01327     {
01328       const size_t vp_settings_count = ud->m_vp_settings.Count();
01329       if ( vp_settings_count > 0 )
01330       {
01331         const ON__LayerPerViewSettings* vp_settings0 = ud->m_vp_settings.Array();
01332         if ( vp_settings0 <= vp_settings_to_delete )
01333         {
01334           int i = (int)(vp_settings_to_delete-vp_settings0);
01335           ud->m_vp_settings.Remove(i);
01336         }
01337       }
01338       if ( ud->IsEmpty() )
01339       {
01340         delete ud;
01341         // Set bit 0x01 of ON_Layer::m_extension_bits to prevent
01342         // ON_Layer visibilty and color queries from wasting
01343         // time looking for userdata.
01344         SetExtensionBit( const_cast<unsigned char*>(layer_m_extension_bits), 0x01 );
01345       }
01346     }
01347   }
01348 }
01349 
01350 bool ON__LayerExtensions::IsEmpty() const
01351 {
01352   const int count = m_vp_settings.Count();
01353 
01354   for ( int i = 0; i < count; i++ )
01355     if ( 0 != m_vp_settings[i].SettingsMask() )
01356       return false;
01357 
01358   return true; // nothing of value in this user data
01359 }
01360 
01361 //
01362 // END ON__LayerExtensions user data class
01363 //
01365 
01366 
01367 
01369 //
01370 // BEGIN ON_Layer per viewport interface functions
01371 //
01372 
01373 void ON_Layer::SetPerViewportColor( ON_UUID viewport_id, ON_Color layer_color )
01374 {
01375   if ( ON_UuidIsNil(viewport_id) )
01376   {
01377     DeletePerViewportColor(viewport_id);
01378     if ( ON_Color::UnsetColor != layer_color )
01379       m_color = layer_color;
01380   }
01381   else
01382   {
01383     bool bSet = ( layer_color != ON_UNSET_COLOR );
01384     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, bSet );
01385     if ( vp_settings )
01386     {
01387       vp_settings->m_color = layer_color;
01388       if ( !bSet && 0 == vp_settings->SettingsMask() )
01389         ON__LayerExtensions::DeleteViewportSettings(*this, &m_extension_bits, vp_settings);
01390     }
01391   }
01392 }
01393 
01394 void ON_Layer::SetColor( ON_Color layer_color, const ON_UUID& viewport_id )
01395 {
01396   SetPerViewportColor( viewport_id, layer_color );
01397 }
01398 
01399 ON_Color ON_Layer::PerViewportColor( ON_UUID viewport_id ) const
01400 {
01401   if ( !ExtensionBit(m_extension_bits,0x01) )
01402   {
01403     const ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01404     if ( 0 != vp_settings && ON_UNSET_COLOR != vp_settings->m_color )
01405       return vp_settings->m_color;
01406   }
01407 
01408   return m_color;
01409 }
01410 
01411 ON_Color ON_Layer::Color( const ON_UUID& viewport_id ) const
01412 {
01413   return PerViewportColor( viewport_id );
01414 }
01415 
01416 void ON_Layer::SetPerViewportPlotColor( ON_UUID viewport_id, ON_Color plot_color )
01417 {
01418   if ( ON_UuidIsNil(viewport_id) )
01419   {
01420     DeletePerViewportPlotColor(viewport_id);
01421     SetPlotColor(plot_color);
01422   }
01423   else
01424   {
01425     bool bSet = ( plot_color != ON_UNSET_COLOR );
01426     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, bSet );
01427     if ( vp_settings )
01428     {
01429       vp_settings->m_plot_color = plot_color;
01430       if ( !bSet && 0 == vp_settings->SettingsMask() )
01431         ON__LayerExtensions::DeleteViewportSettings(*this, &m_extension_bits,vp_settings);
01432     }
01433   }
01434 }
01435 
01436 void ON_Layer::SetPlotColor( ON_Color plot_color, const ON_UUID& viewport_id )
01437 {
01438   return SetPerViewportPlotColor( viewport_id, plot_color );
01439 }
01440 
01441 ON_Color ON_Layer::PerViewportPlotColor( ON_UUID viewport_id ) const
01442 {
01443   if ( !ExtensionBit(m_extension_bits,0x01) )
01444   {
01445     const ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01446     if ( 0 != vp_settings && vp_settings->m_plot_color != ON_UNSET_COLOR )
01447       return vp_settings->m_plot_color;
01448   }
01449 
01450   // no per viewport settings
01451   // 2-Nov-2009 Dale Fugier, modified to call default PlotColor()
01452   return PlotColor();
01453 }
01454 
01455 ON_Color ON_Layer::PlotColor( const ON_UUID& viewport_id ) const
01456 {
01457   return PerViewportPlotColor(viewport_id);
01458 }
01459 
01460 void ON_Layer::SetPerViewportPlotWeight( ON_UUID viewport_id, double plot_weight_mm )
01461 {
01462   if ( ON_UuidIsNil(viewport_id) )
01463   {
01464     DeletePerViewportPlotWeight(viewport_id);
01465     SetPlotWeight(plot_weight_mm); // this call handles invalid plot weights
01466   }
01467   else
01468   {
01469     bool bSet = ( ON_IsValid(plot_weight_mm) && (plot_weight_mm>=0.0 || -1.0==plot_weight_mm) );
01470     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, bSet );
01471     if ( vp_settings )
01472     {
01473       vp_settings->m_plot_weight_mm = (bSet) ? plot_weight_mm : ON_UNSET_VALUE;
01474       if ( !bSet && 0 == vp_settings->SettingsMask() )
01475         ON__LayerExtensions::DeleteViewportSettings(*this, &m_extension_bits, vp_settings);
01476     }
01477   }
01478 }
01479 
01480 void ON_Layer::SetPlotWeight( double plot_weight_mm, const ON_UUID& viewport_id )
01481 {
01482   SetPerViewportPlotWeight( viewport_id, plot_weight_mm );
01483 }
01484 
01485 double ON_Layer::PerViewportPlotWeight( ON_UUID viewport_id ) const
01486 {
01487   if ( !ExtensionBit(m_extension_bits,0x01) )
01488   {
01489     const ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01490     if ( 0 != vp_settings && (vp_settings->m_plot_weight_mm >= 0.0 || -1.0 == vp_settings->m_plot_weight_mm) )
01491       return vp_settings->m_plot_weight_mm;
01492   }
01493   return PlotWeight();
01494 }
01495 
01496 double ON_Layer::PlotWeight( const ON_UUID& viewport_id ) const
01497 {
01498   return PerViewportPlotWeight(viewport_id);
01499 }
01500 
01501 
01502 bool ON_Layer::PerViewportIsVisible( ON_UUID viewport_id ) const
01503 {
01504   if ( !ExtensionBit(m_extension_bits,0x01) )
01505   {
01506     if ( ON_UuidIsNil(viewport_id) )
01507     {
01508       // see if layer is possibly visible in any viewport
01509       if ( !m_bVisible )
01510       {
01511         // default setting is off - check for per view visibility
01512         const ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01513         if ( 0 != ud )
01514         {
01515           int i, count = ud->m_vp_settings.Count();
01516           for ( i = 0; i < count; i++ )
01517           {
01518             if ( 1 == ud->m_vp_settings[i].m_visible )
01519               return true; // layer is visible in this viewport
01520           }
01521         }
01522       }
01523     }
01524     else 
01525     {
01526       const ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01527       if (vp_settings)
01528       {
01529         if ( 1 == vp_settings->m_visible )
01530           return true;  // per viewport ON setting overrides layer setting
01531         if ( 2 == vp_settings->m_visible )
01532           return false; // per viewport OFF setting overrides layer setting
01533       }
01534     }
01535   }
01536 
01537   return IsVisible(); // use layer setting
01538 }
01539 
01540 bool ON_Layer::IsVisible( const ON_UUID& viewport_id ) const
01541 {
01542   return PerViewportIsVisible(viewport_id);
01543 }
01544 
01545 void ON_Layer::SetVisible( bool bVisible, const ON_UUID& viewport_id )
01546 {
01547   SetPerViewportVisible(viewport_id,bVisible);
01548 }
01549 
01550 void ON_Layer::SetPerViewportVisible( ON_UUID viewport_id, bool bVisible )
01551 {
01552   if ( ON_UuidIsNil(viewport_id) )
01553   {
01554     // remove per view visible settings
01555     DeletePerViewportVisible(viewport_id);
01556 
01557     // set general visibility setting
01558     SetVisible(bVisible);
01559   }
01560   else 
01561   {
01562     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, true );
01563     if (vp_settings)
01564     {
01565       vp_settings->m_visible = (bVisible)
01566         ? 1  // layer is on in this viewport
01567         : 2; // layer is off in this viewport
01568       if ( ON_UuidIsNil(m_parent_layer_id) )
01569         vp_settings->m_persistent_visibility = 0;
01570       else if ( bVisible )
01571         vp_settings->m_persistent_visibility = 1;
01572     }
01573   }
01574 }
01575 
01576 bool ON_Layer::PerViewportPersistentVisibility( ON_UUID viewport_id ) const
01577 {
01578   // added to fix bug 82587
01579   if ( !ExtensionBit(m_extension_bits,0x01) && ON_UuidIsNotNil(viewport_id) )
01580   {
01581     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01582     if ( 0 != vp_settings )
01583     {
01584       if ( 1 == vp_settings->m_visible )
01585         return true;
01586       if ( ON_UuidIsNotNil(m_parent_layer_id) )
01587       {
01588         if ( 1 == vp_settings->m_persistent_visibility )
01589           return true;
01590         if ( 2 == vp_settings->m_persistent_visibility )
01591           return false;
01592       }
01593       if ( 2 == vp_settings->m_visible )
01594         return false;
01595     }
01596   }
01597 
01598   return PersistentVisibility();
01599 }
01600 
01601 void ON_Layer::SetPerViewportPersistentVisibility( ON_UUID viewport_id, bool bVisibleChild )
01602 {
01603   // added to fix bug 82587
01604   if ( ON_UuidIsNotNil(viewport_id) )
01605   {
01606     bool bCreate = false; // This "false" is correct because the per viewport visibility
01607                           // setting needs to be in existance for this call to make any
01608                           // sense in the first place.
01609     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, bCreate );
01610     if (vp_settings )
01611       vp_settings->m_persistent_visibility = bVisibleChild ? 1 : 2;
01612   }
01613 }
01614 
01615 void ON_Layer::UnsetPerViewportPersistentVisibility( ON_UUID viewport_id )
01616 {
01617   // added to fix bug 82587
01618   if ( ON_UuidIsNil(viewport_id) )
01619   {
01620     ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions( *this, &m_extension_bits, false );
01621     if ( 0 != ud )
01622     {
01623       for ( int i = 0; i < ud->m_vp_settings.Count(); i++ )
01624       {
01625         ud->m_vp_settings[i].m_persistent_visibility = 0;
01626       }
01627     }
01628   }
01629   else
01630   {
01631     bool bCreate = false; // This "false" is correct because the per viewport visibility
01632                           // setting needs to be in existance for this call to make any
01633                           // sense in the first place.
01634     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, bCreate );
01635     if (vp_settings )
01636       vp_settings->m_persistent_visibility = 0;
01637   }
01638 }
01639 
01640 void ON_Layer::DeletePerViewportColor( const ON_UUID& viewport_id )
01641 {
01642   if ( ON_UuidIsNil(viewport_id) )
01643   {
01644     ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01645     if ( 0 != ud )
01646     {
01647       for ( int i = ud->m_vp_settings.Count(); i--; /*empty iterator*/ )
01648       {
01649         ud->m_vp_settings[i].m_color = ON_Color::UnsetColor;
01650         if ( 0 == ud->m_vp_settings[i].SettingsMask() )
01651           ud->m_vp_settings.Remove(i);
01652       }
01653       if ( ud->IsEmpty() )
01654       {
01655         ON__LayerExtensions::DeleteViewportSettings( *this, &m_extension_bits, 0 );
01656         ud = 0;
01657       }
01658     }
01659   }
01660   else
01661   {
01662     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01663     if (vp_settings) 
01664     {
01665       vp_settings->m_color = ON_Color::UnsetColor;
01666       if ( 0 == vp_settings->SettingsMask() )
01667         ON__LayerExtensions::DeleteViewportSettings(*this,&m_extension_bits,vp_settings);
01668     }
01669   }
01670 }
01671 
01672 void ON_Layer::DeletePerViewportPlotColor( const ON_UUID& viewport_id )
01673 {
01674   if ( ON_UuidIsNil(viewport_id) )
01675   {
01676     ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01677     if ( 0 != ud )
01678     {
01679       for ( int i = ud->m_vp_settings.Count(); i--; /*empty iterator*/ )
01680       {
01681         ud->m_vp_settings[i].m_plot_color = ON_UNSET_COLOR;
01682         if ( 0 == ud->m_vp_settings[i].SettingsMask() )
01683           ud->m_vp_settings.Remove(i);
01684       }
01685       if ( ud->IsEmpty() )
01686       {
01687         ON__LayerExtensions::DeleteViewportSettings( *this, &m_extension_bits, 0 );
01688         ud = 0;
01689       }
01690     }
01691   }
01692   else
01693   {
01694     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01695     if (vp_settings) 
01696     {
01697       vp_settings->m_plot_color = ON_UNSET_COLOR;
01698       if ( 0 == vp_settings->SettingsMask() )
01699         ON__LayerExtensions::DeleteViewportSettings(*this,&m_extension_bits,vp_settings);
01700     }
01701   }
01702 }
01703 
01704 int ON_Layer::UpdateViewportIds( const ON_UuidPairList& viewport_id_map )
01705 {
01706   if ( viewport_id_map.Count() <= 0 )
01707     return 0;
01708   ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01709   if ( 0 == ud )
01710     return 0;
01711   int rc = 0;
01712   ON_UUID new_id;
01713   for ( int i = 0; i < ud->m_vp_settings.Count(); i++ )
01714   {
01715     ON__LayerPerViewSettings& s = ud->m_vp_settings[i];
01716     if ( viewport_id_map.FindId1(s.m_viewport_id,&new_id) && s.m_viewport_id != new_id )
01717     {
01718       s.m_viewport_id = new_id;
01719       rc++;
01720     }
01721   }
01722   return rc;
01723 }
01724 
01725 void ON_Layer::DeletePerViewportPlotWeight( const ON_UUID& viewport_id )
01726 {
01727   if ( ON_UuidIsNil(viewport_id) )
01728   {
01729     ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01730     if ( 0 != ud )
01731     {
01732       for ( int i = ud->m_vp_settings.Count(); i--; /*empty iterator*/ )
01733       {
01734         ud->m_vp_settings[i].m_plot_weight_mm = ON_UNSET_VALUE;
01735         if ( 0 == ud->m_vp_settings[i].SettingsMask() )
01736           ud->m_vp_settings.Remove(i);
01737       }
01738       if ( ud->IsEmpty() )
01739       {
01740         ON__LayerExtensions::DeleteViewportSettings( *this, &m_extension_bits, 0 );
01741         ud = 0;
01742       }
01743     }
01744   }
01745   else
01746   {
01747     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01748     if (vp_settings) 
01749     {
01750       vp_settings->m_plot_weight_mm = ON_UNSET_VALUE;
01751       if ( 0 == vp_settings->SettingsMask() )
01752         ON__LayerExtensions::DeleteViewportSettings(*this,&m_extension_bits,vp_settings);
01753     }
01754   }
01755 }
01756 
01757 void ON_Layer::DeletePerViewportVisible( const ON_UUID& viewport_id )
01758 {
01759   if ( ON_UuidIsNil(viewport_id) )
01760   {
01761     ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01762     if ( 0 != ud )
01763     {
01764       for ( int i = ud->m_vp_settings.Count(); i--; /*empty iterator*/ )
01765       {
01766         ud->m_vp_settings[i].m_visible = 0;
01767         ud->m_vp_settings[i].m_persistent_visibility = 0;
01768         if ( 0 == ud->m_vp_settings[i].SettingsMask() )
01769           ud->m_vp_settings.Remove(i);
01770       }
01771       if ( ud->IsEmpty() )
01772       {
01773         ON__LayerExtensions::DeleteViewportSettings( *this, &m_extension_bits, 0 );
01774         ud = 0;
01775       }
01776     }
01777   }
01778   else
01779   {
01780     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01781     if (vp_settings) 
01782     {
01783       vp_settings->m_visible = 0;
01784       vp_settings->m_persistent_visibility = 0;
01785       if ( 0 == vp_settings->SettingsMask() )
01786         ON__LayerExtensions::DeleteViewportSettings(*this,&m_extension_bits,vp_settings);
01787     }
01788   }
01789 }
01790 
01791 void ON_Layer::GetPerViewportVisibilityViewportIds(
01792     ON_SimpleArray<ON_UUID>& viewport_id_list
01793     ) const
01794 {
01795   viewport_id_list.SetCount(0);
01796   const ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01797   if ( 0 != ud )
01798   {
01799     const int count = ud->m_vp_settings.Count();
01800     if ( count > 0 )
01801     {
01802       viewport_id_list.Reserve(count);
01803       for( int i = 0; i < count; i++ )
01804       {
01805         const ON__LayerPerViewSettings& s = ud->m_vp_settings[i];
01806         if (    0 != ( ON_Layer::per_viewport_visible & s.SettingsMask() ) 
01807              || 0 != ( ON_Layer::per_viewport_persistent_visibility & s.SettingsMask() ) 
01808            )
01809         {
01810           viewport_id_list.Append(s.m_viewport_id);
01811         }
01812       }
01813     }
01814   }
01815 }
01816 
01817 bool ON_Layer::HasPerViewportSettings( const ON_UUID& viewport_id ) const
01818 {
01819   return HasPerViewportSettings( viewport_id, 0xFFFFFFFF );
01820 }
01821 
01822 bool ON_Layer::HasPerViewportSettings(
01823     ON_UUID viewport_id,
01824     unsigned int settings_mask
01825     ) const
01826 {
01827 
01828   if ( 0 != settings_mask )
01829   {
01830     if ( ON_UuidIsNil(viewport_id) )
01831     {
01832       const ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01833       if ( 0 != ud )
01834       {
01835         const int count = ud->m_vp_settings.Count();
01836         for ( int i = 0; i < count; i++ )
01837         {
01838           const ON__LayerPerViewSettings& s = ud->m_vp_settings[i];
01839           if ( 0 != (settings_mask & s.SettingsMask()) )
01840             return true;
01841         }
01842       }
01843     }
01844     else
01845     {
01846       const ON__LayerPerViewSettings* pvs = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01847       if ( 0 != pvs && 0 != (settings_mask & pvs->SettingsMask() ) )
01848         return true;
01849     }
01850   }
01851 
01852   return false;
01853 }
01854 
01855 bool ON_Layer::CopyPerViewportSettings(ON_UUID source_viewport_id, ON_UUID destination_viewport_id)
01856 {
01857   bool rc = false;
01858   if (    ON_UuidIsNotNil(source_viewport_id) 
01859        && ON_UuidIsNotNil(destination_viewport_id) 
01860        && 0 != ON_UuidCompare(source_viewport_id, destination_viewport_id)
01861      )
01862   {
01863     const ON__LayerPerViewSettings* src = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, source_viewport_id, false );
01864     if( 0 != src )
01865     {
01866       // Make a local copy of the source settings because
01867       // the pointer to the source settings may be invalid
01868       // after adding storage for the destination settings.
01869       const ON__LayerPerViewSettings local_src(*src);
01870       src = 0; // never use this pointer again in this function.
01871       ON__LayerPerViewSettings* dst = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, destination_viewport_id, true);
01872       if( 0 != dst )
01873       {
01874         *dst = local_src;
01875         dst->m_viewport_id = destination_viewport_id;
01876         rc = true;
01877       }
01878     }
01879   }
01880   return rc;
01881 }
01882 
01883 bool ON_Layer::CopyPerViewportSettings( 
01884     const ON_Layer& source_layer,
01885     ON_UUID viewport_id,
01886     unsigned int settings_mask
01887     )
01888 {
01889   bool rc = false;
01890   if ( 0 != settings_mask && this != &source_layer )
01891   {
01892     if ( ON_UuidIsNil(viewport_id) )
01893     {
01894       // copy per viwport settings for every viewport
01895       const ON__LayerExtensions* soruce_layer_ud = ON__LayerExtensions::LayerExtensions(source_layer,&source_layer.m_extension_bits,false);
01896       if ( 0 != soruce_layer_ud )
01897       {
01898         const int count = soruce_layer_ud->m_vp_settings.Count();
01899         for ( int i = 0; i < count; i++ )
01900         {
01901           const ON__LayerPerViewSettings& src = soruce_layer_ud->m_vp_settings[i];
01902           ON__LayerPerViewSettings* dst = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, src.m_viewport_id, true);
01903           if ( 0 != dst )
01904           {
01905             dst->CopySettings(&src,settings_mask);
01906             rc = true;
01907           }
01908         }
01909       }
01910     }
01911     else
01912     {
01913       // copy per viwport settings for a specified viewport.
01914       const ON__LayerPerViewSettings* src = ON__LayerExtensions::ViewportSettings( source_layer, &source_layer.m_extension_bits, viewport_id, false);
01915       if ( 0 != src )
01916       {
01917         ON__LayerPerViewSettings* dst = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, true);
01918         if ( 0 != dst )
01919         {
01920           dst->CopySettings(src,settings_mask);
01921           rc = true;
01922         }
01923       }
01924     }
01925   }
01926   return rc;
01927 }
01928 
01929 
01930 void ON_Layer::DeletePerViewportSettings( const ON_UUID& viewport_id ) const
01931 {
01932   if ( ON_UuidIsNil(viewport_id) )
01933   {
01934     ON__LayerExtensions::DeleteViewportSettings(*this,&m_extension_bits,0);
01935   }
01936   else
01937   {
01938     ON__LayerPerViewSettings* vp_settings = ON__LayerExtensions::ViewportSettings( *this, &m_extension_bits, viewport_id, false );
01939     if ( vp_settings )
01940       ON__LayerExtensions::DeleteViewportSettings(*this,&m_extension_bits,vp_settings);
01941   }
01942 }
01943 
01944 
01945 void ON_Layer::CullPerViewportSettings( int viewport_id_count, const ON_UUID* viewport_id_list )
01946 {
01947   ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01948   if ( 0 != ud )
01949   {
01950     if ( viewport_id_count <= 0 )
01951     {
01952       // delete all per viewport settings
01953       ON__LayerExtensions::DeleteViewportSettings( *this, &m_extension_bits, 0 );
01954       ud = 0;
01955     }
01956     else if ( viewport_id_count > 0 && 0 != viewport_id_list )
01957     {
01958       int i, j;
01959       for ( i = ud->m_vp_settings.Count(); i--; /*empty iterator*/ )
01960       {
01961         const ON_UUID vp_id = ud->m_vp_settings[i].m_viewport_id;
01962         for ( j = 0; j < viewport_id_count; j++ )
01963         {
01964           if ( 0 == memcmp(&viewport_id_list[i],&vp_id,sizeof(vp_id)) )
01965             break;
01966         }
01967         if ( j >= viewport_id_count )
01968         {
01969           // ud->m_vp_settings[i].m_viewport_id is NOT in viewport_id_list[]
01970           ud->m_vp_settings.Remove(i);
01971         }
01972       }
01973       if ( ud->IsEmpty() )
01974       {
01975         // nothing useful in ud
01976         ON__LayerExtensions::DeleteViewportSettings( *this, &m_extension_bits, 0 );
01977         ud = 0;
01978       }
01979     }
01980   }
01981 }
01982 
01983 ON__UINT32 ON_Layer::PerViewportSettingsCRC() const
01984 {
01985   ON__UINT32 crc = 0;
01986   if ( !ExtensionBit(m_extension_bits,0x01) )
01987   {
01988     ON__LayerExtensions* ud = ON__LayerExtensions::LayerExtensions(*this,&m_extension_bits,false);
01989     if ( 0 != ud )
01990     {
01991       for ( int i = 0; i < ud->m_vp_settings.Count(); i++ )
01992         crc = ud->m_vp_settings[i].DataCRC(crc);
01993     }
01994   }
01995   return crc;
01996 }
01997 
01998 //
01999 // END ON_Layer per viewport interface functions
02000 //
02002 
02003 
02004 
02005 unsigned int ON_Layer::Differences( const ON_Layer& layer0, const ON_Layer& layer1 )
02006 {
02007   /*
02008   enum
02009   {
02010     none = 0,
02011     userdata = 1,
02012     color = 2,
02013     plot_color = 4,
02014     plot_weight = 8,
02015     visible = 16,
02016     locked = 32,
02017     expanded = 64,
02018     all = 0xFFFFFFFF
02019   }
02020   */
02021   unsigned int differences = 0;
02022 
02023 
02024   const ON_UserData* ud0 = layer0.FirstUserData();
02025   const ON_UserData* ud1 = layer1.FirstUserData();
02026   while ( 0 != ud0 && 0 != ud1 )
02027   {
02028     if ( ud0->m_userdata_uuid != ud1->m_userdata_uuid )
02029       break;
02030     ud0 = ud0->Next();
02031     ud1 = ud1->Next();
02032   }
02033   if ( 0 != ud0 || 0 != ud1 )
02034     differences |= ON_Layer::userdata_settings;
02035 
02036   if ( layer0.m_color != layer1.m_color )
02037     differences |= ON_Layer::color_settings;
02038 
02039   if ( layer0.m_plot_color != layer1.m_plot_color )
02040     differences |= ON_Layer::plot_color_settings;
02041 
02042   if ( layer0.m_plot_weight_mm != layer1.m_plot_weight_mm )
02043     differences |= ON_Layer::plot_weight_settings;
02044 
02045   if ( layer0.m_bVisible != layer1.m_bVisible )
02046     differences |= ON_Layer::visible_settings;
02047 
02048   if ( layer0.m_bLocked != layer1.m_bLocked )
02049     differences |= ON_Layer::locked_settings;
02050 
02051   // Note:
02052   //  This function is used for comparing layers from different
02053   //  documents.  It does not make sense to compare values
02054   //  like m_linetype_index, m_material_index and so on because
02055   //  different indices may actually reference the same linetype
02056   //  or material.  If there is a compelling reason to compare
02057   //  other settings, they can be added.
02058 
02059   return differences;
02060 }
02061 
02062 
02063 void ON_Layer::Set( unsigned int settings, const ON_Layer& settings_values )
02064 {
02065   if ( 0 != (ON_Layer::userdata_settings & settings) )
02066   {
02067     // save original user data on this layer
02068     ON_UserDataHolder ud; 
02069     ud.MoveUserDataFrom(*this);
02070 
02071     // make a complete copy of the userdata on settings_values
02072     CopyUserData(settings_values);
02073 
02074     // now restore any original user data that was not present on settings_values.
02075     ud.MoveUserDataTo(*this,true);
02076   }
02077 
02078   if ( 0 != (ON_Layer::color_settings & settings) )
02079   {
02080     m_color = settings_values.m_color;
02081   }
02082 
02083   if ( 0 != (ON_Layer::plot_color_settings & settings) )
02084   {
02085     m_plot_color = settings_values.m_plot_color;
02086   }
02087 
02088   if ( 0 != (ON_Layer::plot_weight_settings & settings) )
02089   {
02090     m_plot_weight_mm = settings_values.m_plot_weight_mm;
02091   }
02092 
02093   if ( 0 != (ON_Layer::visible_settings & settings) )
02094   {
02095     m_bVisible = settings_values.m_bVisible ? true : false;
02096   }
02097 
02098   if ( 0 != (ON_Layer::locked_settings & settings) )
02099   {
02100     m_bLocked = settings_values.m_bLocked ? true : false;
02101   }
02102 }
02103 
02104 class /*NEVER EXPORT THIS CLASS DEFINITION*/ ON__LayerSettingsUserData : public ON_UserData
02105 {
02106 #if !defined(ON_BOZO_VACCINE_BFB63C094BC7472789BB7CC754118200)
02107 #error Never copy this class definition or put this definition in a header file!
02108 #endif
02109   ON_OBJECT_DECLARE(ON__LayerSettingsUserData);
02110 
02111 public:
02112   ON__LayerSettingsUserData();
02113   ~ON__LayerSettingsUserData();
02114   // default copy constructor and operator= work fine.
02115 
02116   static ON__LayerSettingsUserData* LayerSettings(const ON_Layer& layer,bool bCreate);
02117 
02118 
02119 public:
02120   // virtual ON_Object override
02121   ON_BOOL32 IsValid( ON_TextLog* text_log = NULL ) const;
02122   // virtual ON_Object override
02123   unsigned int SizeOf() const;
02124   // virtual ON_Object override
02125   ON__UINT32 DataCRC(ON__UINT32 current_remainder) const;
02126   // virtual ON_Object override
02127   ON_BOOL32 Write(ON_BinaryArchive& binary_archive) const;
02128   // virtual ON_Object override
02129   ON_BOOL32 Read(ON_BinaryArchive& binary_archive);
02130   // virtual ON_UserData override
02131   ON_BOOL32 Archive() const;
02132   // virtual ON_UserData override
02133   ON_BOOL32 GetDescription( ON_wString& description );
02134 
02135 public:
02136 
02137   enum 
02138   {
02139     valid_settings = 
02140       (
02141           ON_Layer::color_settings
02142         | ON_Layer::plot_color_settings
02143         | ON_Layer::visible_settings
02144         | ON_Layer::locked_settings
02145         | ON_Layer::plot_weight_settings
02146       )
02147   };
02148 
02149   bool HaveSettings() const {return 0 != (m_settings & ON__LayerSettingsUserData::valid_settings);}
02150 
02151   bool HaveColor() const {return 0 != (m_settings & ON_Layer::color_settings);}
02152   bool HavePlotColor() const {return 0 != (m_settings & ON_Layer::plot_color_settings);}
02153   bool HaveVisible() const {return 0 != (m_settings & ON_Layer::visible_settings);}
02154   bool HaveLocked() const {return 0 != (m_settings & ON_Layer::locked_settings);}
02155   bool HavePlotWeight() const {return 0 != (m_settings & ON_Layer::plot_weight_settings);}
02156 
02157   void Defaults()
02158   {
02159     m_settings = 0;
02160     m_color = 0;
02161     m_plot_color = 0;
02162     m_bVisible = 0;
02163     m_bLocked = 0;
02164     m_plot_weight_mm = 0.0;
02165   }
02166 
02167   unsigned int m_settings;
02168   ON_Color m_color;
02169   ON_Color m_plot_color;
02170   bool m_bVisible;
02171   bool m_bLocked;
02172   double m_plot_weight_mm;
02173 
02174 };
02175 
02176 #undef ON_BOZO_VACCINE_BFB63C094BC7472789BB7CC754118200
02177 
02178 ON_OBJECT_IMPLEMENT(ON__LayerSettingsUserData,ON_UserData,"BFB63C09-4BC7-4727-89BB-7CC754118200");
02179 
02180 ON__LayerSettingsUserData* ON__LayerSettingsUserData::LayerSettings(const ON_Layer& layer,bool bCreate)
02181 {
02182   ON__LayerSettingsUserData* ud = ON__LayerSettingsUserData::Cast(layer.GetUserData(ON__LayerSettingsUserData::m_ON__LayerSettingsUserData_class_id.Uuid()));
02183   if ( !ud && bCreate )
02184   {
02185     ud = new ON__LayerSettingsUserData();
02186     const_cast<ON_Layer&>(layer).AttachUserData(ud);
02187   }
02188   return ud;
02189 }
02190 
02191 ON__LayerSettingsUserData::ON__LayerSettingsUserData()
02192 {
02193   m_userdata_uuid = ON__LayerSettingsUserData::m_ON__LayerSettingsUserData_class_id.Uuid();
02194   m_application_uuid = ON_opennurbs5_id;
02195   m_userdata_copycount = 1;
02196   Defaults();
02197 }
02198 
02199 ON__LayerSettingsUserData::~ON__LayerSettingsUserData()
02200 {
02201 }
02202 
02203 // virtual ON_Object override
02204 ON_BOOL32 ON__LayerSettingsUserData::IsValid( ON_TextLog* text_log ) const
02205 {
02206   return true;
02207 }
02208 
02209 // virtual ON_Object override
02210 unsigned int ON__LayerSettingsUserData::SizeOf() const
02211 {
02212   return (unsigned int)(sizeof(*this));
02213 }
02214 
02215 // virtual ON_Object override
02216 ON__UINT32 ON__LayerSettingsUserData::DataCRC(ON__UINT32 current_remainder) const
02217 {
02218   ON__UINT32 crc = current_remainder;
02219   crc = ON_CRC32(crc,sizeof(m_settings),&m_settings);
02220   if ( HaveColor() ) crc = ON_CRC32(crc,sizeof(m_color),&m_color);
02221   if ( HavePlotColor() ) crc = ON_CRC32(crc,sizeof(m_plot_color),&m_plot_color);
02222   if ( HaveVisible() ) crc = ON_CRC32(crc,sizeof(m_bVisible),&m_bVisible);
02223   if ( HaveLocked() ) crc = ON_CRC32(crc,sizeof(m_bLocked),&m_bLocked);
02224   if ( HavePlotWeight() ) crc = ON_CRC32(crc,sizeof(m_plot_weight_mm),&m_plot_weight_mm);
02225   return crc;
02226 }
02227 
02228 // virtual ON_Object override
02229 ON_BOOL32 ON__LayerSettingsUserData::Write(ON_BinaryArchive& binary_archive) const
02230 {
02231   bool rc = binary_archive.BeginWrite3dmChunk(TCODE_ANONYMOUS_CHUNK,1,0);
02232   if ( !rc )
02233     return false;
02234 
02235   rc = false;
02236   for(;;)
02237   {
02238     if ( !binary_archive.WriteInt(m_settings) )
02239       break;
02240     if ( HaveColor() && !binary_archive.WriteColor(m_color) )
02241       break;
02242     if ( HavePlotColor() && !binary_archive.WriteColor(m_plot_color) )
02243       break;
02244     if ( HaveVisible() && !binary_archive.WriteBool(m_bVisible) )
02245       break;
02246     if ( HaveLocked() && !binary_archive.WriteBool(m_bLocked) )
02247       break;
02248     if ( HavePlotWeight() && !binary_archive.WriteDouble(m_plot_weight_mm) )
02249       break;
02250 
02251     rc = true;
02252     break;
02253   }
02254 
02255   if ( !binary_archive.EndWrite3dmChunk() )
02256     rc = false;
02257 
02258   return rc;
02259 }
02260 
02261 // virtual ON_Object override
02262 ON_BOOL32 ON__LayerSettingsUserData::Read(ON_BinaryArchive& binary_archive)
02263 {
02264   Defaults();
02265 
02266   int major_version = 0;
02267   int minor_version = 0;
02268   bool rc = binary_archive.BeginRead3dmChunk(TCODE_ANONYMOUS_CHUNK,&major_version,&minor_version);
02269   if ( !rc )
02270     return false;
02271 
02272   rc = false;
02273   while ( 1 == major_version )
02274   {
02275     if ( !binary_archive.ReadInt(&m_settings) )
02276       break;
02277     if ( HaveColor() && !binary_archive.ReadColor(m_color) )
02278       break;
02279     if ( HavePlotColor() && !binary_archive.ReadColor(m_plot_color) )
02280       break;
02281     if ( HaveVisible() && !binary_archive.ReadBool(&m_bVisible) )
02282       break;
02283     if ( HaveLocked() && !binary_archive.ReadBool(&m_bLocked) )
02284       break;
02285     if ( HavePlotWeight() && !binary_archive.ReadDouble(&m_plot_weight_mm) )
02286       break;
02287     rc = true;
02288     break;
02289   }
02290 
02291   if ( !binary_archive.EndRead3dmChunk() )
02292     rc = false;
02293 
02294   return rc;
02295 }
02296 
02297 // virtual ON_UserData override
02298 ON_BOOL32 ON__LayerSettingsUserData::Archive() const
02299 {
02300   // don't save empty settings
02301   return HaveSettings();
02302 }
02303 
02304 // virtual ON_UserData override
02305 ON_BOOL32 ON__LayerSettingsUserData::GetDescription( ON_wString& description )
02306 {
02307   description = L"Saved Layer Settings";
02308   return true;
02309 }
02310 
02311 
02312 
02313 void ON_Layer::SaveSettings( unsigned int settings, bool bUpdate )
02314 {
02315   ON__LayerSettingsUserData* ud;
02316   if ( 0 == (settings & ON__LayerSettingsUserData::valid_settings) )
02317   {
02318     if ( !bUpdate )
02319     {
02320       // delete any existing user data
02321       ud = ON__LayerSettingsUserData::LayerSettings(*this,false);
02322       if ( ud )
02323       {
02324         delete ud;
02325         ud = 0;
02326       }
02327     }
02328   }
02329   else
02330   {
02331     ud = ON__LayerSettingsUserData::LayerSettings(*this,true);
02332     if ( !bUpdate )
02333     {
02334       ud->Defaults();
02335       ud->m_settings = settings;
02336     }
02337     else
02338     {
02339       ud->m_settings |= settings;
02340     }
02341     if ( ud->HaveColor() )
02342       ud->m_color = m_color;
02343 
02344     if ( ud->HavePlotColor() )
02345       ud->m_plot_color = m_plot_color;
02346 
02347     if ( ud->HaveVisible() )
02348       ud->m_bVisible = PersistentVisibility();
02349 
02350     if ( ud->HaveLocked() )
02351       ud->m_bLocked = PersistentLocking();
02352 
02353     if ( ud->HavePlotWeight() )
02354       ud->m_plot_weight_mm = m_plot_weight_mm;
02355   }
02356 }
02357 
02358 unsigned int ON_Layer::SavedSettings() const
02359 {
02360   const ON__LayerSettingsUserData* ud = ON__LayerSettingsUserData::LayerSettings(*this,false);
02361   return ( 0 != ud ? ud->m_settings : 0 );
02362 }
02363 
02364 bool ON_Layer::GetSavedSettings( ON_Layer& layer, unsigned int& settings ) const
02365 {
02366   const ON__LayerSettingsUserData* ud = ON__LayerSettingsUserData::LayerSettings(*this,false);
02367   if ( 0 == ud )
02368     return false;
02369   bool rc = false;
02370 
02371   if ( ud->HaveColor() )
02372   {
02373     layer.m_color = ud->m_color;
02374     rc = true;
02375   }
02376 
02377   if ( ud->HavePlotColor() )
02378   {
02379     layer.m_plot_color = ud->m_plot_color;
02380     rc = true;
02381   }
02382 
02383   if ( ud->HaveVisible() )
02384   {
02385     layer.m_bVisible = ud->m_bVisible;
02386     rc = true;
02387   }
02388 
02389   if ( ud->HaveLocked() )
02390   {
02391     layer.m_bLocked = ud->m_bLocked;
02392     rc = true;
02393   }
02394 
02395   if ( ud->HavePlotWeight() )
02396   {
02397     layer.m_plot_weight_mm = ud->m_plot_weight_mm;
02398     rc = true;
02399   }
02400 
02401   return true;
02402 }
02403 
02404 bool ON_Layer::PersistentVisibility() const
02405 {
02406   if ( !m_bVisible && ON_UuidIsNotNil(m_parent_layer_id) )
02407   {
02408     switch ( 0x06 & m_extension_bits )
02409     {
02410     case 0x02:
02411       return true;
02412     case 0x04:
02413       return false;
02414     }
02415   }
02416 
02417   return m_bVisible;
02418 }
02419 
02420 void ON_Layer::SetPersistentVisibility(bool bVisibleChild)
02421 {
02422   const unsigned char and_mask = 0xF9;
02423   const unsigned char or_bit = ON_UuidIsNotNil(m_parent_layer_id) 
02424                              ? (bVisibleChild ? 0x02 : 0x04)
02425                              : 0x00;
02426   m_extension_bits &= and_mask;
02427   m_extension_bits |= or_bit;
02428 }
02429 
02430 void ON_Layer::UnsetPersistentVisibility()
02431 {
02432   const unsigned char and_mask = 0xF9;
02433   m_extension_bits &= and_mask;
02434 }
02435 
02436 bool ON_Layer::PersistentLocking() const
02437 {
02438   if ( m_bLocked && ON_UuidIsNotNil(m_parent_layer_id) )
02439   {
02440     switch ( 0x18 & m_extension_bits )
02441     {
02442     case 0x08:
02443       return true;
02444     case 0x10:
02445       return false;
02446     }
02447   }
02448 
02449   return m_bLocked;
02450 }
02451 
02452 void ON_Layer::SetPersistentLocking(bool bLockedChild)
02453 {
02454   const unsigned char and_mask = 0xE7;
02455   const unsigned char or_bit = ON_UuidIsNotNil(m_parent_layer_id)
02456                              ? (bLockedChild ? 0x08 : 0x10)
02457                              : 0x00;
02458   m_extension_bits &= and_mask;
02459   m_extension_bits |= or_bit;
02460 }
02461 
02462 void ON_Layer::UnsetPersistentLocking()
02463 {
02464   // a set bit means the child will be unlocked when the parent is unlocked
02465   const unsigned char and_mask = 0xE7;
02466   m_extension_bits &= and_mask;
02467 }
02468 


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