Message 1 of 13
Execute External Command via Button on a Form

Not applicable
12-04-2019
06:28 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi, I want to modiy the thickness of an object so naturally I need to do it with a Transaction and so on, so I need to do an External Command which I have defined here:
using Autodesk.Revit.Attributes; using Autodesk.Revit.DB; using Autodesk.Revit.UI; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using Toolbar.Commands.Base; using Toolbar.UI; using Autodesk.Revit; using Autodesk.Revit.ApplicationServices; using Application = Autodesk.Revit.ApplicationServices.Application; namespace Toolbar.Commands { [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)] class ExternalCommandFloorThickness : ExternalCommandBase, IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { UIApplication uiapp = commandData.Application; UIDocument uidoc = uiapp.ActiveUIDocument; Application app = uiapp.Application; Document doc = uidoc.Document; InitializeMembers(commandData); FloorThickness formFloor = new FloorThickness(revitApplication, revitActiveDocument); // Retrieve elements from database FilteredElementCollector collector = new FilteredElementCollector(doc).WhereElementIsElementType().OfClass(typeof(FloorType)); var query = from element in collector where element.Name == "Generic 150mm" select element; // Modify document within a transaction using (Transaction tx = new Transaction(doc)) { tx.Start("Transaction ModifyFloorThickness"); double width = UnitUtils.ConvertToInternalUnits(100, DisplayUnitType.DUT_MILLIMETERS); foreach (Element e in query) { FloorType floorType = e as FloorType; CompoundStructure compoundStructure = floorType.GetCompoundStructure(); compoundStructure.SetLayerWidth(0, width); floorType.SetCompoundStructure(compoundStructure); } tx.Commit(); } return Result.Succeeded; } } }
But now the problem is that I want to run/execute this code from the click of a button in a Form. My best idea so far is doing this:
private void button2_Click(object sender, EventArgs e) { ExternalCommandFloorThickness extCommFT = new ExternalCommandFloorThickness(); extCommFT.Execute(); }
But obviously this doesn't work because I will need to pass the parameters to the .Execute() function, but I cannot pass it a commandData because I'm inside the code of a Form not an External Command and so on.
So any ideas are highly appreciated it!
Many thanks in advance!