3.2.9.5 Reorder Layers


Version Info

Minimum Origin Version Required: Origin 8 SR0

Example

This example shows how to reorder layers in worksheet page. Copy the following functions and compile. Run wks_reorder_ex in Command Window.

void wks_reorder_ex()
{
	WorksheetPage wksPage;		
	wksPage.Create("Origin");
	
	int nIndexToken = 1;
	wksPage.Layers(0).SetName("Sheet 1");
	wksPage.AddLayer("Sheet 3");
	wksPage.AddLayer("Sheet 5");
	wksPage.AddLayer("Sheet 2");
	wksPage.AddLayer("Sheet 4");
	wksPage.AddLayer("Sheet 6");
	printf("Before reorder...\n");
	print_out_layer_names(wksPage);
	
	// current the index in sheet name is 1, 3, 5, 2, 4, 6, after the following loop to reorder, the index in name should be 1, 2, 3, 4, 5, 6. 
	while(1)
	{
		vector<int> vn;
		_get_layer_index_from_name(wksPage, vn, nIndexToken);
		
		// if vn already be increment, then no need reorder
		vector vData;
		vData = vn;
		if(MONO_INCREASE  == ocmath_is_monotonic(vData, vData.GetSize()))
		{
			out_str("Reorder layers done!");
			break;
		}
		
		vector<uint> vnIndeces;
		vn.Sort(SORT_ASCENDING, true, vnIndeces);
		
		int nn = 0;
		while( vnIndeces[nn] == nn && nn < vnIndeces.GetSize())
			nn++;
		
		// adjust layer index vnIndeces[nn] from nn
		if( nn < vnIndeces.GetSize() )
		{
			wksPage.Reorder(nn, vnIndeces[nn]);					
		}	
	}
	printf("After reorder...\n");
	print_out_layer_names(wksPage);
	
}

static void _get_layer_index_from_name(WorksheetPage& wksPage, vector<int>& vn, int nToken)
{
	foreach(Layer lay in wksPage.Layers)
	{
		string strLayerName = lay.GetName();
		int nn = atoi(strLayerName.GetToken(nToken)); // get the 3rd item
		vn.Add( nn );
	}	
}

static void print_out_layer_names(WorksheetPage& wksPage)
{
	int ii = 0;
	printf("Layers in %s page: \n", wksPage.GetName());
	foreach(Layer lay in wksPage.Layers)
	{
		printf("\tLayer %d = %s\n", ++ii, lay.GetName())
	}
}