Message 1 of 8
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
This is me getting my feet wet for the first time with this process so it would be awesome if somone could walk me through it.
I created my first ExternalCommand that I can now call from AddIns tab > External Commands. Can someone provide me with a good example of taking that External command and making it into its own Tab>Button on the Revit Ribbon. I want to eventually build more commands and add to my own tab.
Here's my Command:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
public class DetailLineFilter : ISelectionFilter
{
public bool AllowElement(Element e)
{
return (e.Category.Id.IntegerValue.Equals(
(int)BuiltInCategory.OST_Lines));
}
public bool AllowReference(Reference r, XYZ p)
{
return false;
}
}
[TransactionAttribute(TransactionMode.Manual)]
[RegenerationAttribute(RegenerationOption.Manual)]
public class CurveTotalLength : IExternalCommand
{
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
//Get application and document objects
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
UIDocument uidoc = uiApp.ActiveUIDocument;
try
{
IList<Element> pickedRef = null;
Selection sel = uiApp.ActiveUIDocument.Selection;
DetailLineFilter selFilter = new DetailLineFilter();
pickedRef = sel.PickElementsByRectangle(selFilter, "Select lines");
List<double> lengthList = new List<double>();
foreach(Element e in pickedRef)
{
DetailLine line = e as DetailLine;
if (line != null)
{
lengthList.Add(line.GeometryCurve.Length);
}
}
string lengthFeet = Math.Round(lengthList.Sum(), 2).ToString() + " ft";
string lengthMeters = Math.Round(lengthList.Sum() * 0.3048, 2).ToString() + " m";
string lengthMilimeters = Math.Round(lengthList.Sum() * 304.8, 2).ToString() + " mm";
string lengthInch = Math.Round(lengthList.Sum() * 12, 2).ToString() + " inch";
StringBuilder sb = new StringBuilder();
sb.AppendLine("Total Length is:");
sb.AppendLine(lengthFeet);
sb.AppendLine(lengthInch);
sb.AppendLine(lengthMeters);
sb.AppendLine(lengthMilimeters);
TaskDialog.Show("Line Length", sb.ToString());
return Result.Succeeded;
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException)
{
return Result.Cancelled;
}
catch (Exception ex)
{
message = ex.Message;
return Result.Failed;
}
}
}Here's my addin file:
<?xml version="1.0" encoding="utf-8"?>
<RevitAddIns>
<AddIn Type="Command">
<Assembly>
D:\Stuff\RevitVisualStudio\CurveTotalLength\CurveTotalLength\bin\Debug\CurveTotalLength.dll
</Assembly>
<ClientId>502fe383-2648-4e98-adf8-5e6047f9dc34</ClientId>
<FullClassName>CurveTotalLength</FullClassName>
<Text>MeasureLength</Text>
<VendorId>Grimshaw</VendorId>
<VisibilityMode>AlwaysVisible</VisibilityMode>
</AddIn>
</RevitAddIns>Also, any general C# coding good practice comments are welcome. Like I said, I am just getting my feet wet with this as I was previously mostly using Python (Dynamo or IronPythonShell).
Thanks!
Solved! Go to Solution.
