Issue
I'm creating a detail line and I want to change its line type. But I don't know how to obtain a list of available line styles. How can I do that?
Solution
CurveElement has a property called GetLineStyleIds. You can use this to retrieve a set of available line styles' element ids. A trick is that you will need to have a curve element in order to access GetLineStyleLds. If you are interested in access them without actually drawing a line, you can use transaction to draw a line, then rollback before the command end. The following sample code demonstrate the usage.
[Transaction(TransactionMode.Manual)]
public class rvtCmd_FilterLineStyles : IExternalCommand
{
// Member variables
Application m_rvtApp;
Document m_rvtDoc;
public Result Execute(
ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
// Get the access to the top most objects.
UIApplication rvtUIApp = commandData.Application;
UIDocument rvtUIDoc = rvtUIApp.ActiveUIDocument;
m_rvtApp = rvtUIApp.Application;
m_rvtDoc = rvtUIDoc.Document;
// Get the list of line styles
ICollection<ElementId> ls = GetLineStyles();
// Let's see what we got
Utils.ShowElementList(m_rvtDoc, ls, "Line Styles");
return Result.Succeeded;
}
// Get a list of line styles from CurveElement.LineStyles()
ICollection<ElementId> GetLineStyles()
{
Transaction tr =
new Transaction(m_rvtDoc, "Get Line Styles");
tr.Start();
// Create a detail line
View view = m_rvtDoc.ActiveView;
XYZ pt1 = XYZ.Zero;
XYZ pt2 = new XYZ(10.0, 0.0, 0.0);
Line geomLine = m_rvtApp.Create.NewLine(pt1, pt2, true);
DetailCurve dc =
m_rvtDoc.Create.NewDetailCurve(view, geomLine);
// Check the available line styels
ICollection<ElementId> lineStyles = dc.GetLineStyleIds();
// We are only interested in the lineStyles.
// Roll back.
tr.RollBack();
return lineStyles;
}
}
public class Utils
{
// Helper function: summarize an element information as
// a line of text, which is composed of: class, category,
// name and id. Name will be "Family: Type" if a given
// element is ElementType. Intended for quick viewing of
// list of element, for example.
public static string ElementToString(Element e)
{
if (e == null)
{
return "none";
}
string name = "";
if (e is ElementType)
{
Parameter param =
e.get_Parameter(
BuiltInParameter.
SYMBOL_FAMILY_AND_TYPE_NAMES_PARAM);
if (param != null)
{
name = param.AsString();
}
}
else
{
name = e.Name;
}
string cat = " <null> ";
if (e.Category != null)
{
cat = e.Category.Name;
}
return e.GetType().Name + "; "
+ cat + "; "
+ name + "; "
+ e.Id.IntegerValue.ToString() + "\r\n";
}
// Helper function to display info. from a list of elements
// that are passed into.
public static void ShowElementList(
Document doc,
ICollection<ElementId> elems,
string header)
{
string s =
" - Class - Category - Name (or Family: Type Name)" +
" - Id - \r\n";
foreach (ElementId id in elems)
{
Element e = doc.GetElement(id);
s += ElementToString(e);
}
TaskDialog.Show(
header + "(" + elems.Count.ToString() + "):", s);
}
}