70001100 Find all the entities of a group

Article 70001100
Type HowTo
Product WebJS
Version 7
Date Added 6/14/2017
Fixed 7.7012.0.1 (6/14/2017)
Submitted by Brendan fry

Summary

How can I find all the entities of a group?

Solution

Here is an example where you can pass the name of a specific group and find all the entities that this group contains.

We create two groups; group1 which contains line1 and circle1 and group2 which contains line2 and circle2. See the code below:

        var line1 = vdcanvas.AddLine([3, 0, 0], [0, 0, 0], true);
        vdcanvas.GroupsManager.AddItem("group1", line1);
        var circle1 = vdcanvas.AddCircle([0, 0, 0], 2);
        vdcanvas.GroupsManager.AddItem("group1", circle1);
 
        vdcanvas.scriptCommand.color("0");
        var line2 = vdcanvas.AddLine([0, 0, 0], [0, 3, 0], true);
        vdcanvas.GroupsManager.AddItem("group2", line2);
        var circle2 = vdcanvas.AddCircle([-2, 0, 0], 2);
        vdcanvas.GroupsManager.AddItem("group2", circle2);
 
        setTimeout(vdcanvas.redraw);
 
        var gitems1 = GetGroupItems("group1");// the items of group1
 
        var gitems2 = GetGroupItems("group2");// the items of group2
 
           
        //get the items of a specific group name
        function GetGroupItems(groupname) {

             var ret = [];//an empty array where then we will add each item that is contained in the group
             var doc = vdcanvas.GetDocument();
             var groups = doc.Groups;
             if (!groups) return ret;
                for (var i = 0; i < groups.Items.length; i++) {
                    var group = groups.Items[i];
                    var lname = group.Name.toLowerCase();
                    if (lname == groupname && group.Items) {//if the group name matches the passed name
                        for (var k = 0; k < group.Items.length; k++) {//go throught all entities in group
                            var figHandle = group.Items[k];//items inside group are as handle string
                            var figobj = vdcanvas.GetEntityItem(figHandle);//get the real object from a string handle
                            if (!figobj) continue;
                            ret.push(figobj);//add the item to the return collection
                        }
 
                    }
 
                }
                return ret;
            }

 

Send comments on this topic.