70002440 Copy entities from one layer to an other

Article 70002440
Type HowTo
Product WebJS
Version 10
Date Added 11/17/2023 12:00:00 AM
Fixed 10.1005.0.3 (11/17/2023 12:00:00 AM)
Submitted by sefa


How can I copy entities from one layer to an other in Web Javascript control?


Try a code like:

            //following code will clone all entities in model layout that belong to the layer 'mylayerName' 
            //Set a new layer to 'myNewlayerName' for the cone entities
            //Add them to the end of the Model layout entities collection (list)
            var sourceLayer = vdcanvas.FindLayer("mylayerName");
            var sourcelayerId = 'h_' + sourceLayer.HandleId.toString();
            var destLayer = vdcanvas.FindLayer("myNewlayerName");
            var destLayerId =  'h_' + destLayer.HandleId.toString();
            var layerdefault = vdcanvas.FindLayer("0");
            var document = vdcanvas.GetDocument();

            var layerref;

            var entities = document.Model.Entities;//select model entities
            var cloneEntities = [];// a temporary collection  to keep the new clone-entities
            for (var i = 0; i < entities.Items.length; i++) {

                var obj = vdcanvas.GetEntityItem(entities.Items[i]);
                if (!obj || obj.Deleted) continue;

                //if obj has the default layer name  '0' then may the obj.Layer is undefine so we calculate it
                if (!obj.Layer) layerref = layerdefault;
                else layerref = vdcanvas.GetEntityItem(obj.Layer);
                var layerrefId = 'h_' + layerref.HandleId.toString();

                //select only entities that are in the sourceLayer
                if (layerrefId != sourcelayerId) continue;

                var ent = vdConst.cloneEntity(obj);
                ent.HandleId = undefined; //set the HandleId to undefined in order a new one to be calculated later by AddFigureToCollection call

                ent.Layer = destLayerId; //change the layer to the destLayer
            //add and register the cone entities to the source model entities
            for (var i = 0; i < cloneEntities.length; i++) {
                vdcanvas.AddFigureToCollection(entities, cloneEntities[i]);
            //post a redraw

Send comments on this topic.