70001875 Flilter Select method on WebControl

Article 70001875
Type Wish
Product WebJS
Version 8005
Date Added 11/22/2020
Fixed 8.8006.0.5 (11/23/2020)
Submitted by VectorDraw Team

Summary

A method to create a selection by filtering the entities in the document

Solution

In version 8006.0.5 a new method FilterEntities of vdraw web control was added.

Enumerate all the entities in the selected document and calls the passed function for each entity.

parameters:

customdataObject A user object that will be passed to the filterdelegate arguments. May be null
filterdelegate A user function of 'function(args)' type that will be called for each entity
the args is an object with following properties:
vdraw: : The vdraw web control that caused the event.
document: : The selected document to the vdraw control
owner: The owner of the entity. It can be a document Layout or a Block
entity: An entity item in the owner collection.
User can check the entity properties and set the cancel = true if he does not want to add it in the selection that return by the FilterEntities method
If it is null or then the user must check if the owner is valid and if not set the cancel = true in order to ignore all the entities of the owner
customdata: A user object passed in the FilterEntities that may be usufull to be used by user checks
cancel: By default is false and the entity is added to the final selection.Set it true in order the entity not to be added in the selection

Returns: An array of entities depend on the code that the user use in the passed function

Example 1: Select all entities in the Model layout that reference the Layer with name "MyLayer1"

//get the handle of the layer with specific name
var layHandle = 'h_' + vdcanvas.FindLayer("MyLayer1").HandleId.toString();
var selection = vdcanvas.FilterEntities(layHandle, function (args) {
     if (args.owner != args.document.Model) {//filter owner of entity
         args.cancel = true;//do not Enumerate entites that are not in the Model layout
         return;
     }
     if (args.entity) {//filter entity
         if (args.entity.Layer != args.customdata) //do not add the entities that their Layer is other than 'MyLayer1' Layer(NOTE: this layer handle is passed to the FilterEntities customdataObject parameter
             args.cancel = true;
     }
 }//close the function delegate
 );//close the FilterEntities function
 //erase the selected entities( Entities that belong to Model layout and reference the layer with name 'MyLayer1'
 vdcanvas.scriptCommand.select(selection, function (_vdcanvas) { _vdcanvas.scriptCommand.erase(); _vdcanvas.redraw(); });
Example 2: Select only the entities of the "newlayout" layout and pass the selection to the move() method.
             var vddoc = vdcanvas.GetDocument();
             var layoutID = vdcanvas.FindLayout("newlayout");
             if (layoutID == -1) return;
             else {
                 var layout = vddoc.LayOuts.Items[layoutID];
                 var selection = vdcanvas.FilterEntities(layout, function (args) {
                     if (args.owner != layout) {
                         args.cancel = true;
                         return;
                     }

                 }
                );

                 vdcanvas.scriptCommand.select(selection, function (_vdcanvas) { _vdcanvas.scriptCommand.move(); });
             }
Example 3: Select only the the vdInserts object with Block name == "myblock" of the Model and pass the selected vdInsert to the move() method.
             var block = vdcanvas.FindBlock("myblock");
             var blockHandle = "h_" + block.HandleId.toString();
             var selection = vdcanvas.FilterEntities(blockHandle, function (args) {
                 if (args.owner != args.document.Model) {
                     args.cancel = true;
                     return;
                 }
                 if (args.entity) {//filter entity
                     if (args.entity._t != vdConst.vdInsert_code || args.entity.Block != args.customdata) {                    
                         args.cancel = true;
                     }
                 }
             }
                );

                 vdcanvas.scriptCommand.select(selection, function (_vdcanvas) { _vdcanvas.scriptCommand.move(); });

Send comments on this topic.