This week I got the video of the presentation I did 2 weeks ago at the AEC Hackathon in NYC. It was a great experience to break the ice in front of a good number of developers as a Technical Evangelist, showing them about our new and really cool View and Data API. If you haven’t learned anything new today, here is an option for you to get some knowledge in your day.
This week I will share with all of you a question that came from our Revit API forum, regarding a simple but useful way to change a type Reference to a Viewport one. I know it sounds simple and it is, but is always good to share knowledge of our Revit API users with all of you. Here is what the question was.
Question: I want to make a selection of multiple elements(viewports), and this selection, I want it to be by "PickObjects" and not "PickByRectangle". So, when trying to implement the following lines of code in order to make the desired selection, I come up with a conversion error. This error states the following:
Error CS0039: Cannot convert type 'Autodesk.Revit.DB.Reference' to 'Autodesk.Revit.DB.Viewport' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion
Build failed.
The code is:
ISelectionFilter viewportfilter = new ViewPortPickFilter();
Selection choices = uidoc.Selection;
IList<Reference> refa = choices.PickObjects
(ObjectType.Element, viewportfilter,
"Pick multiple elements one by one");
foreach (Reference vp in refa)
{
var e = vp as Viewport;
}
I have tried changing IList<Reference> for <Viewport> and the pertinent modifications, however, then the issue is on choices, stating that it cannot convert type reference to viewport once again...
Thus, If someone could give me a generic way of PickObjects. It would be really appreciated.
Thanks in advance!
Answer: You could try this, Get the Element from that reference.
foreach (Reference vp in refa)
{
Viewport viewp = doc.GetElement(vp) as Viewport;
//do something...
}
Thank you to Maer6427 for the quick and simple solution.
Cheers and thanks for reading.