Message 1 of 10
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am attempting to create a WinForm which lists all the views in the project as a viewTree. The WinForm run command is then packaged into a Dynamo ZeroTouch node, however the views are not displayed when the form launches, and when I relaunch the form I get an error that line 53 (the foreach statement that attempts to use the view name to populate the viewTree) is "not set to an instance of an object". This is my first attempt to raise and consume events and I've done everything I can to get it to work; what is the cause of this error?
The form successfully launches via the dynamo node:
The exception:
The code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.DesignScript.Runtime;
using Autodesk.DesignScript.Interfaces;
using Autodesk.DesignScript.Geometry;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using RevitServices.Persistence;
using RevitServices.Transactions;
using Revit.Elements;
using Revit.GeometryConversion;
namespace SelectSheetsAndViews
{
/// <summary> The form class</summary>
public partial class FormRevitSelect : System.Windows.Forms.Form
{
public FormRevitSelect()
{
InitializeComponent();
}
private void DynamoTreeListSelect_Activated(object sender, System.EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
List<Autodesk.Revit.DB.View> d1 = new ThresholdReachedEventArgs().Views;
//List<string> d = new List<string> { "A", "B", "C", "D"};
foreach (Autodesk.Revit.DB.View x in d1)
//foreach (string x in d)
{
treeView1.Nodes.Add(x.ToString());
}
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
}
}
/// <summary>A Revit class to get all the sheets and views in the document</summary>
public class RevitWinForm
{
private List<object> _sheetsAndViews;
private List<object> sheetsAndViewsToDelete;
internal List<object> GetSheetAndViewsToDelete { get { return sheetsAndViewsToDelete; } }
private RevitWinForm(List<object> sheetsAndViews)
{
_sheetsAndViews = sheetsAndViews;
}
internal List<object> GetSheetAndViewList { get { return _sheetsAndViews; } }
/// <summary>
/// Function to collect all the views and sheets in the document
/// </summary>
internal static List<Autodesk.Revit.DB.View> SheetsAndView()
{
Document doc = DocumentManager.Instance.CurrentDBDocument;
FilteredElementCollector collector = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views);
List<Autodesk.Revit.DB.View> viewList = collector.ToElements() as List<Autodesk.Revit.DB.View>;
return viewList;
}
/// <summary>
/// The MultiReturn attribute can be used to specify
/// the names of multiple output ports on a node that
/// returns a dictionary. The node must return a dictionary
/// to be recognized as a multi-out node.
/// </summary>
/// <param name="refresh">Refresh</param>
/// <returns>DynamoTreeListSelect</returns>
public static string CompactDocument(bool refresh)
{
System.Windows.Forms.Application.Run(new FormRevitSelect());
return "Process Complete";
}
}
class Program
{
static void Main(string[] args)
{
RevitSheetsAndViews v = new RevitSheetsAndViews();
v.GetRevitViews += v_ViewsOUT;
}
static void v_ViewsOUT(object sender, ThresholdReachedEventArgs e)
{
List<Autodesk.Revit.DB.View> d1 = e.Views;
}
}
class RevitSheetsAndViews
{
private List<Autodesk.Revit.DB.View> views;
/*public RevitSheetsAndViews(List<Autodesk.Revit.DB.View> viewsOUT)
{
views = viewsOUT;
}
*/
public RevitSheetsAndViews()
{
Document doc = DocumentManager.Instance.CurrentDBDocument;
FilteredElementCollector collector = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views);
views = collector.ToElements() as List<Autodesk.Revit.DB.View>;
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Views = views;
RevitViewsEventHandler(args);
}
protected virtual void RevitViewsEventHandler(ThresholdReachedEventArgs e) //raise the EventHandler delegate and associate GetRevitViews to it
{
EventHandler<ThresholdReachedEventArgs> handler = GetRevitViews;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler<ThresholdReachedEventArgs> GetRevitViews; // declare an event named GetRevitViews. The event is associated with the EventHandler delegate and raised in a method named OnThresholdReached.
}
public class ThresholdReachedEventArgs : EventArgs
{
public List<Autodesk.Revit.DB.View> Views { get; set; }
}
}
Solved! Go to Solution.