By Aaron Lu
As we know, to create a colored detail line in family, we have to:
- Create a new family project, e.g. based on "Generic Annotation.rft"
- Click "Create" > "Line", draw a line
- Select the new line, click "Manage" > "Object Styles", you will see a list of categories and patterns like this:
- Select "Generic Annotations", "Modify Subcategories" > "New" is available now, click it and create a new sub-category, and set color and weight
- Close all dialogs, and select the line again
- Under "Modify | Lines", you can see the new category appears under the combobox in "Subcategory" group. We've done the job :)
So how to do that via API?
- First, what we've drawn is a DetailLine, so we should use Document.FamilyCreate.NewDetailCurve to create it.
- To set the line style, use DetailLine.LineStyle property.
- Create a new subcategory, use method Categories.NewSubcategory(Category parentCategory, string name)
- So the only question left is how to get the parentCategory argument in the above method.
Now we can make use of RevitLookup tool, as the line has the LineStyle property, we can inspect what its category is, let's select it, and run "RevitLookup" > "Snoop Current Selection..."
As we can see, OST_GenericAnnotation is the "Generic Annotations" listed in the first image, we can use Categories.get_Item(BuiltInCategory.OST_GenericAnnotation) to get the category.
Here comes the code, remember put it in a transaction:
public void CreateSubCategoryAndDetailLine(Document doc) { var categories = doc.Settings.Categories; var subCategoryName = "MySubCategory"; Category category = doc.Settings.Categories. get_Item(BuiltInCategory.OST_GenericAnnotation); Category subCategory = null; if (!category.SubCategories.Contains(subCategoryName)) { subCategory = categories.NewSubcategory(category, subCategoryName); var newcolor = new Color(250, 10, 0); subCategory.LineColor = newcolor; subCategory.SetLineWeight(10, GraphicsStyleType.Projection); } else subCategory = category.SubCategories.get_Item(subCategoryName); Line newLine = Line.CreateBound( new XYZ(0, 1, 0), new XYZ(-1, 0, 0)); var detailLine = doc.FamilyCreate.NewDetailCurve( doc.ActiveView, newLine); detailLine.LineStyle = subCategory.GetGraphicsStyle( GraphicsStyleType.Projection); }
Result: