By Aaron Lu
Question:
I have the code that extracts all the Line Style names that are shown in the Revit: Manage tab->Settings pane->Additional Settings button->Line Styles menu item.
If you start the creation of a Detail Line in Revit (Annotation tab->Detail pane->Detail Line button), in the Properties window click the Line Styles item (right) you will see there are less Line Styles in this list. How do I only get this list of Line Styles not all of them?
private ListLineStyleNamesGet(Document doc) { List lineStyles = new List (); List All_Categories = doc.Settings.Categories.Cast ().ToList(); Category Line_Category = All_Categories[1]; foreach (Category one_cat in All_Categories) { if (one_cat.Name == "Lines") Line_Category = one_cat; } if (Line_Category.CanAddSubcategory) { CategoryNameMap All_Styles = Line_Category.SubCategories; foreach (Category one_category in All_Styles) { lineStyles.Add(one_category.Name); } } lineStyles.Sort(); return lineStyles; }
Answer: What about using detailLine.GetLineStyleIds()?
Reply: I’m not creating a detail line at this point in my logic (I need the user to select a valid Line Style first). I do not see a way to get GetLineStyleIds() to work in this case. Please advise.
Answer (by Miroslav): You may have to do a "dummy-transaction" (the one you always Abort at the end) and use detailLine.GetLineStyleIds() therein. It’s a valid technique we use in a few other Revit API sage patterns…
Example Code:
private ListLineStyleNamesGet(Document doc) { List lineStyles = new List (); Transaction transaction = new Transaction(doc, "Create detail line"); transaction.Start(); try { var view = doc.ActiveView; // make sure the view is 2D view var modelLine = doc.Create.NewDetailCurve(view, Line.CreateBound(new XYZ(0, 0, 0), new XYZ(10, 0, 0))); var styles = modelLine.GetLineStyleIds(); foreach (var styleId in styles) { var styleEle = doc.GetElement(styleId); lineStyles.Add(styleEle.Name); } transaction.RollBack(); } catch (Exception ex) { transaction.RollBack(); } return lineStyles; }