RevitiAPI: How to create a colored detail line in family?

中文链接

By Aaron Lu

As we know, to create a colored detail line in family, we have to:

  1. Create a new family project, e.g. based on “Generic Annotation.rft”
  2. Click “Create” > “Line”, draw a line
  3. Select the new line, click “Manage” > “Object Styles”, you will see a list of categories and patterns like this: GraphicsLineStyle_Category2
  4. Select “Generic Annotations”, “Modify Subcategories” > “New” is available now, click it and create a new sub-category, and set color and weight
  5. Close all dialogs, and select the line again
  6. 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?
 
  1. First, what we’ve drawn is a DetailLine, so we should use Document.FamilyCreate.NewDetailCurve to create it.
  2. To set the line style, use DetailLine.LineStyle property.
  3. Create a new subcategory, use method Categories.NewSubcategory(Category parentCategory, string name)
  4. 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:


Comments

2 responses to “RevitiAPI: How to create a colored detail line in family?”

  1. Alright, this alrticle is a great opportunity to follow up your work.hank you so much for this step-by-step guide. I’m doing a project for work and you made it SO MUCH EASIER. I expected WEEKS to be spent trying to figure out how to do what I wanted to do. Instead, in 20 minutes, I had exactly what I wanted. I love it.

  2. I’d like to thank the author for writing such an insightful and informative blog post about colored line in revit family that is not just useful to the readers but also revealing.

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading