Revit API

Active Member
Posts: 7
Registered: ‎03-08-2011
Message 1 of 4 (366 Views)

Add results (C#)

366 Views, 3 Replies
12-08-2011 11:46 AM

Hello all. I'm very new to C#. I've been putting together bits of code to create a tool that gets the current selection (of pipe) and displays the "Length" parameter for each instance. The attached jpg shows the current result.


What I'd like to do next is provide a TOTAL length for all the pipes selected.


How can I append/add to my "p" value (below) for each instance?

(Sorry about the formatting of the code)


#region Namespaces

using System;

using System.Collections.Generic;

using Autodesk.Revit.ApplicationServices;

using Autodesk.Revit.Attributes;

using Autodesk.Revit.DB;

using Autodesk.Revit.UI;

using Autodesk.Revit.UI.Selection;

using System.Windows.Forms;

using System.Diagnostics;

using Autodesk.Revit.DB.Plumbing;

using System.Text;


namespace TryThis



public class Class1 : IExternalCommand


public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)




// Select some elements in Revit before invoking this command

// Get the handle of current document.

UIDocument uidoc = commandData.Application.ActiveUIDocument;


// Get the element selection of current document.

Selection selection = uidoc.Selection;


ElementSet collection = selection.Elements;

if (0 == collection.Size)


// If no elements selected.

TaskDialog.Show("Revit", "You haven't selected any elements.");




String info = "Pipe Lengths are: ";

foreach (Pipe pipe in collection)


//this is the code I would like to add to for each instance of pipe, so I can pull a TOTAL later

Parameter p = pipe.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH);

info += "\n\t" + p.AsValueString();


TaskDialog.Show("Revit", info);



catch (Exception e)


message = e.Message;

return Autodesk.Revit.UI.Result.Failed;


return Result.Succeeded;




Active Member
Posts: 7
Registered: ‎03-08-2011
Message 2 of 4 (354 Views)

Re: Add results (C#)

12-09-2011 06:03 AM in reply to: RG-ClearyZ



I figured out how to add the results and total it. This is the code I added:


double total = 0; (located in my else statement)

total = total += p.AsDouble(); (located in my foreach statment)


//Finally. Show results:

TaskDialog.Show("Total", total.ToString());


My variable "p" (which is "pipe.get_Parameter(BuiltInParameter.CURVE_ELEM_LENGTH)", if converted straight to a string, gives me a length in this format: 12'-6".


But, when I use the code above to add/total the pipe lengths it converts the 12'-6" to 12.5. Is there a way to convert this back to displaying feet and inches?


Thank you ahead of time.

Active Member
Posts: 10
Registered: ‎07-20-2011
Message 3 of 4 (342 Views)

Re: Add results (C#)

12-11-2011 08:36 PM in reply to: RG-ClearyZ

Hi, RG-ClearyZ

Recently I wrote simple class for unit converting. Jeremy Tammik wrote a post on his blog about unit converter and my class . You can read it here


Good luck, Victor 

Active Member
Posts: 7
Registered: ‎03-08-2011
Message 4 of 4 (336 Views)

Re: Add results (C#)

12-12-2011 02:40 PM in reply to: RG-ClearyZ

Thanks for the link. I'll check it out. =D

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community

Revit Exchange Apps

Created by the community for the community, Autodesk Exchange Apps for Revit helps you achieve greater speed, accuracy, and automation from concept to manufacturing.

Connect with Revit