How to access picked object from another method

How to access picked object from another method

markjvlampad
Advocate Advocate
1,143 Views
3 Replies
Message 1 of 4

How to access picked object from another method

markjvlampad
Advocate
Advocate

Hi there,

 

i have this code:

//this code is to pickobject

public class sample : IExternalCommand
{

static AddInId appId = new AddInId(new Guid(""));
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
Reference r = uidoc.Selection.PickObject(ObjectType.Element);

Element e = doc.GetElement(r);

 

return Result.Succeeded;

}

 

//for some reasons, I want to access e from above method and do something with that object.

public void getdatafrompickobj()
{

}

 

having the above codes how can I do that?

 

cheers,

mark

 

0 Likes
Accepted solutions (1)
1,144 Views
3 Replies
Replies (3)
Message 2 of 4

RPTHOMAS108
Mentor
Mentor

Call 'getdatafrompickobj' in Execute method prior to 'return Result.Succeeded', passing 'e' as argument in 'getdatafrompickobj'.

 

You can also scope the 'e' variable to class level but that seems less wise, since object lifecycle is less clear to me.

 

0 Likes
Message 3 of 4

markjvlampad
Advocate
Advocate

 hi thank you,

when I pass e as my arguments it wont works. I cannot access my Element e.

0 Likes
Message 4 of 4

Anonymous
Not applicable
Accepted solution

As was hinted at you could try declaring the element somewhere more accessible to other methods, but you will need to be aware of the object lifecycle, which is more of a c# general issue and less of a Revit issue. Although to some degree, the API is going to affect it as well I suppose. You can also try making it public, static, etc.

 

//this code is to pickobject

public class sample : IExternalCommand
{

 Element e { get; set; }

 static AddInId appId = new AddInId(new Guid(""));
 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
 {

  UIDocument uidoc = commandData.Application.ActiveUIDocument;
  Document doc = uidoc.Document;
  Reference r = uidoc.Selection.PickObject(ObjectType.Element);

  e = doc.GetElement(r);

 

  return Result.Succeeded;

 }

 

 //for some reasons, I want to access e from above method and do something with that object.

 public void getdatafrompickobj()
 {

    if(e != null)

      {
        //Do some code with e

      }

     else

      {

         //Messagebox or something to say nothing was selected

      }
 }

}

0 Likes