Here's the entire code. The form is completely bank.
The .ShowDialog() line at the end errors out with "Attempted to read or write protected memory..."
Thanks.
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
//Access the document
Document doc = uiDoc.Document;
Document DbDoc = uiDoc.Document;
Selection sel = uiDoc.Selection;
if (uiDoc.ActiveView.Category.Id.IntegerValue == (int)BuiltInCategory.OST_Sheets)
{
Autodesk.Revit.DB.View sheet = uiDoc.ActiveView;
try
{
foreach (ElementId eleId in sel.GetElementIds())
{
List<ElementId> selElements = new List<ElementId>();
Autodesk.Revit.DB.Viewport vp = doc.GetElement(eleId) as Autodesk.Revit.DB.Viewport;
FilteredElementCollector collector
= new FilteredElementCollector(doc)
.OfClass(typeof(Autodesk.Revit.DB.View));
foreach (Autodesk.Revit.DB.View v in collector)
{
if (vp != null)
{
if (v.Id == vp.ViewId)
{
uiDoc.ActiveView = v;
FilteredElementCollector fic = new FilteredElementCollector(doc, uiDoc.ActiveView.Id);
foreach (ElementId elId in fic.ToElementIds())
{
selElements.Add(elId);
}
uiDoc.ActiveView = sheet;
foreach (Autodesk.Revit.UI.UIView ov in uiDoc.GetOpenUIViews())
{
if (ov.ViewId == v.Id)
{
ov.Close(); //Commenting this line out will let the form open
break;
}
}
break;
}
}
}
if (selElements.Count > 0)
{
sel.SetElementIds(selElements);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
try
{
//Open the form
frmMyForm varForm = new frmMyForm();
varForm.ShowDialog();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
return Result.Succeeded;
}