Code sample Home

Order of layers and entities by layers

Drawing "layer_order_ents.lcd" contains 4 layers, named "0", "Red", "Green", "Blue"
Each layer has 3 filled rectangles.
Each call of this function resorts the layers and resorts the entities depend on layers order.

void DemoSortLayers (HANDLE hLcWnd)
{
  static int iOrder = 0;  // static variable, will be incremented on each call of this function
  int Variant[24][4]={    // all variants for sorting of 4 layers
    {1,2,3,4}, {1,2,4,3}, {1,3,4,2}, {1,3,2,4}, {1,4,2,3}, {1,4,3,2},
    {2,1,3,4}, {2,1,4,3}, {2,3,1,4}, {2,3,4,1}, {2,4,1,3}, {2,4,3,1},
    {3,1,2,4}, {3,1,4,2}, {3,2,1,4}, {3,2,4,1}, {3,4,1,2}, {3,4,2,1},
    {4,1,2,3}, {4,1,3,2}, {4,2,3,1}, {4,2,1,3}, {4,3,2,1}, {4,3,1,2}
  };
  HANDLE hDrw, hBlock, hLayer[4];
  int i;

  // get drawing and block, linked with CAD window
  hDrw = lcPropGetHandle( hLcWnd, LC_PROP_WND_DRW );
  hBlock = lcPropGetHandle( hLcWnd, LC_PROP_WND_VIEWBLOCK );
  
  // get layers handles
  hLayer[0] = lcDrwGetObjectByName( hDrw, LC_OBJ_LAYER, L"0" );
  hLayer[1] = lcDrwGetObjectByName( hDrw, LC_OBJ_LAYER, L"Red" );
  hLayer[2] = lcDrwGetObjectByName( hDrw, LC_OBJ_LAYER, L"Green" );
  hLayer[3] = lcDrwGetObjectByName( hDrw, LC_OBJ_LAYER, L"Blue" );
  for (i=0; i<4; ++i){
    if (hLayer[i] == 0){
      // error, layer with specified name is not found
      return;
    }
  }

  // set layer priority
  for (i=0; i<4; ++i){
    lcPropPutInt( hLayer[i], LC_PROP_TABLE_PRIORITY, Variant[iOrder][i] );
  }
  // sort layer by priority
  lcDrwSortObjects( hDrw, LC_OBJ_LAYER );
  // sort entities in a block according the layers order
  lcBlockSortEnts( hBlock, true, 0 );

  lcWndRedraw( hLcWnd );

  // set order for next call of this function
  ++iOrder;
  if (iOrder == 24){
    iOrder = 0;
  }
}
This function will change the drawing as shown on the pictures below: