Hi again @matthew_taylor,
I've managed to execute the Revit Add-in thanks to the template provided by Jeremy Tammik.
At first I thought that everything worked fine, until I realized the contrary.
The code below should Select a door shown in the ListBox of the windows form.
The ListBox successfully loads all the doors included in the revit project, only when i press the Select button, nothing happens.
Here is the code:
namespace Test3
{
[Transaction(TransactionMode.Manual)]
class WinForm: System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox lb_doors;
private System.Windows.Forms.Button bt_select;
private ExternalCommandData commandData;
private UIApplication uiapp;
private UIDocument uidoc;
private Document doc;
public WinForm(ExternalCommandData commandData)
{
InitializeComponent();
this.commandData = commandData;
}
private void InitializeComponent()
{
this.lb_doors = new System.Windows.Forms.ListBox();
this.bt_select = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// lb_doors
//
this.lb_doors.FormattingEnabled = true;
this.lb_doors.Location = new System.Drawing.Point(12, 12);
this.lb_doors.Name = "lb_doors";
this.lb_doors.Size = new System.Drawing.Size(260, 199);
this.lb_doors.TabIndex = 0;
//
// bt_select
//
this.bt_select.Location = new System.Drawing.Point(197, 226);
this.bt_select.Name = "bt_select";
this.bt_select.Size = new System.Drawing.Size(75, 23);
this.bt_select.TabIndex = 1;
this.bt_select.Text = "Select";
this.bt_select.UseVisualStyleBackColor = true;
//
// WinForm
//
this.ClientSize = new System.Drawing.Size(284, 261);
this.Controls.Add(this.bt_select);
this.Controls.Add(this.lb_doors);
this.Name = "WinForm";
this.Load += new System.EventHandler(this.WinForm_Load);
this.ResumeLayout(false);
}
private void WinForm_Load(object sender, EventArgs e)
{
uiapp = commandData.Application;
uidoc = uiapp.ActiveUIDocument;
doc = uidoc.Document;
ShowInListBox(CreateLogicAndFilter(doc));
}
private void bt_select_Click(object sender, EventArgs e)
{
List<Element> doors = new List<Element>();
foreach (Element door in lb_doors.Items)
{
doors.Add(door);
}
InAppDoorSelection(doors);
}
private void InAppDoorSelection(ICollection<Element> doors)
{
List<ElementId> doorsID = new List<ElementId>();
foreach (Element door in doors)
{
doorsID.Add(door.Id);
}
uidoc.Selection.SetElementIds(doorsID);
}
private void ShowInListBox(ICollection<Element> elements)
{
lb_doors.Items.Clear();
foreach (Element element in elements)
{
lb_doors.Items.Add(element.Id + " " + element.Name);
}
}
//CreateLogicAndFilter source:
//http://help.autodesk.com/view/RVT/2017/ENU/?guid=GUID-5231B53E-DD08-4F05-8BF2-E23E11C314D4
private ICollection<Element> CreateLogicAndFilter(Document document)
{
// Find all door instances in the project by finding all elements that both belong to the door
// category and are family instances.
ElementClassFilter familyInstanceFilter = new ElementClassFilter(typeof(FamilyInstance));
// Create a category filter for Doors
ElementCategoryFilter doorsCategoryfilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
// Create a logic And filter for all Door FamilyInstances
LogicalAndFilter doorInstancesFilter = new LogicalAndFilter(familyInstanceFilter, doorsCategoryfilter);
// Apply the filter to the elements in the active document
FilteredElementCollector collector = new FilteredElementCollector(document);
ICollection<Element> doors = collector.WherePasses(doorInstancesFilter).ToElements();
return doors;
}
}
}and the command.cs file:
namespace Test3
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
WinForm wf = new WinForm(commandData);
wf.ShowDialog();
wf.Close();
return Result.Succeeded;
}
}
}I debugged the script, and had some errors/warning in them, some are probably unimportant, but here they are anyways (they are all recurrent so I only included them once):
...
System.Windows.Data Error: 40 : BindingExpression path error: 'Current' property not found on 'object' ''QuickAccessUndoButton' (HashCode=123050)'. BindingExpression:Path=Current; DataItem='QuickAccessUndoButton' (HashCode=123050); target element is 'ContentPresenter' (Name=''); target property is 'NoTarget' (type 'Object')
...
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Border', AncestorLevel='3''. BindingExpression:Path=ActualHeight; DataItem=null; target element is 'ContentPresenter' (Name=''); target property is 'NoTarget' (type 'Object')
...
System.Windows.Media.Animation Warning: 6 : Unable to perform action because the specified Storyboard was never applied to this object for interactive control.; Action='Stop'; Storyboard='System.Windows.Media.Animation.Storyboard'; Storyboard.HashCode='30643766'; Storyboard.Type='System.Windows.Media.Animation.Storyboard'; TargetElement='Autodesk.Private.Windows.DropDownToolTip'; TargetElement.HashCode='7358445'; TargetElement.Type='Autodesk.Private.Windows.DropDownToolTip'
...
System.Windows.Data Error: 23 : Cannot convert '{DisconnectedItem}' from type 'NamedObject' to type 'Autodesk.Windows.RibbonItem' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: TypeConverter cannot convert from MS.Internal.NamedObject.