0

I want to select a list of entities by mouse click and then require their properties. I know one can select entity by using (please view code)

but how one can get the properties of this list of entities (for example if entity is cylinder and its start and end points are required)

controlDrawing.ViewportLayoutTower.ActionMode = devDept.Eyeshot.actionType.SelectByPick
Anas Ahmed
  • 123
  • 9

2 Answers2

0

You can simply in the MouseDown event of the control just find all entities under the mouse and get what you want from them.

private void myViewport_MouseDown(object sender, MouseEventArgs e)
{
    // if left click
    if (e.Button == MouseButtons.Left)
    {
        // get all entities under the mouse cursor
        var entityIndexes = myViewport.GetAllEntitiesUnderMouseCursor(e.Location);
        
        // if there is any entities
        if entityIndexes.Any())
        {
            foreach(var index in entityIndexes)
            {
                 // get the entity and do something
                 var entity = myViewport.Entities[index];
            }
        }        
    }
}
Franck
  • 4,438
  • 1
  • 28
  • 55
0

You are looking for casting. When putting objects into and pulling objects out of the viewport they are always cast as Entity as this is the parent class of all entities. If you want the cylinder properties you need to cast it to whichever child class you added to the viewport. Unfortunately there is no Cylinder child class of Entity, so I am assuming you made Mesh.CreateCylinder(). This is a Mesh class, and the variables (start and end points) you passed into the function are locally scoped to the function and are no longer available to be accessed by the Mesh class.

One approach to bypass this is to add this info into EntityData property of your entity. This property can hold any object you make.

        public class myCylinderEntData
        {
            public double radius = 2d;
            public double height = 5d;
            public int slices = 10;
        }

        double dRadius = 2d;
        double dHeight = 5d;
        int iSlices = 10;
        myCylinderEntData cyl1EntData = new myCylinderEntData() { radius = dRadius, height = dHeight, slices = iSlices };
        Mesh cylinder1 =  Mesh.CreateCylinder(dRadius, dHeight, iSlices);
        cylinder1.EntityData = cyl1EntData;
        vp1.Entities.Add(cylinder1); // Add to viewport

        Entity retrivedEnt = vp1.Entities[0];
        myCylinderEntData myRetrievedEntData = (myCylinderEntData)retrivedEnt.EntityData; // get Data back after clicked


        int[] clickedEntsIndex = vp1.GetAllEntitiesUnderMouseCursor(Cursor.Position);// e.Location) ;  // retrieve from viewport

Also if it is an available property of a class that inherits Entity you can retrieve the property like this.

        int[] clickedEntsIndex = vp1.GetAllEntitiesUnderMouseCursor(Cursor.Position);// e.Location) ;  // retrieve from viewport
        foreach(int index in clickedEntsIndex)
        {
             if(vp1.Entities[index] is Mesh) // Check object type for whichever child class you want to convert to
            {
                Mesh myClickedMesh = (Mesh)vp1.Entities[index];
                // Here you access to all of your Meshes public Variables
            }
            else if (vp1.Entities[index] is Solid)
            {
                Solid myClickedSolid = (Solid)vp1.Entities[index];
                // Here you access to all of your Solid public Variables
            }
        }
Daniel Lord
  • 754
  • 5
  • 18
  • 2
    For your sake of learning programming you should never do `new Mesh().GetType()` you should use `typeof(Mesh)` period. Anyhow the whole line should not be `if(vp1.Entities[index].GetType() == new Mesh().GetType())` but instead `if(vp1.Entities[index] is Mesh)` – Franck Sep 25 '20 at 16:19