70001874 How to delete all the entities of a specific layer

Article 70001874
Type HowTo
Product WebJS
Version 8
Date Added 11/20/2020
Fixed 8.8006.0.5 (11/20/2020)
Submitted by Bogumil Styczen


How to delete all the entities of the Layer1 for example.


In the example bellow we delete all the entities that reference to Layer1 from all the layouts and blocks so if a block contais entities from several layers, the entities that reference to Layer 1 will be deleted.

       //returns the handle of the layer of the passed entity
       function GetEntityLayerHandle(vdrawobj, document, entity) {
           var ret = entity.Layer;
           if (!ret) {//if not exist asume that it belongs to Layer '0'
               ret = vdrawobj.FindLayer("0");
               if (!ret) ret = 'h_';
               else ret = "h_" + ret.HandleId.toString();
           return ret;
       //set the Deleted property of the entities that belongs to layer with passed layerHandle, to true
       //Note: Entities with Deleted == true are not draw and they are not saved
       function deleteEntitiesOnLayer(vdrawobj,document, handlecollection, layerHandle) {
           for (var i = 0; i < handlecollection.Items.length; i++) {
               var ent = vdrawobj.GetDictItem(handlecollection, handlecollection.Items[i]);
               if (!ent) continue;
               var layerentity = GetEntityLayerHandle(vdrawobj,document,ent);
               if (layerentity == layerHandle)
                   ent.Deleted = true;
       function test() {
           //Mark all the entities, of all layouts and all blocks, that have property Layer == Layer 1 handle as Deleted = true
           var layer1 = vdcanvas.FindLayer('Layer1');
           var layerHandle = "h_" + layer1.HandleId.toString();
           var vddoc = vdcanvas.GetDocument();
           deleteEntitiesOnLayer(vdcanvas, vddoc, vddoc.Model.Entities, layerHandle);
           if (vddoc.LayOuts) {
               for (var i = 0; i < vddoc.LayOuts.Items.length; i++) {
                   deleteEntitiesOnLayer(vdcanvas, vddoc, vddoc.LayOuts.Items[i].Entities, layerHandle);
           if (vddoc.Blocks) {
               for (var i = 0; i < vddoc.Blocks.Items.length; i++) {
                   var block = vdcanvas.GetDictItem(vddoc.Blocks, vddoc.Blocks.Items[i]);
                   if (!block) continue;
                   deleteEntitiesOnLayer(vdcanvas, vddoc, block.Entities, layerHandle);

Send comments on this topic.