Hi Jeremy
I've been searching up and down the web to find a solution on how to rename line styles in a Revit project file and - up so far did not find any solution yet. My first attempt was doing it in Dynamo - fail... So now I am just looking for any suggestion how to do this in either Python or C#... any pointer appreciated...
Cheers
Martin
Solved! Go to Solution.
Solved by Dale.Bartlett. Go to Solution.
I use this to list all linestyles, without testing, I'd guess that: lineStyle.Name = "TestName"; would probably work. Remember to put in a transaction. Please let us know. Regards, Dale
public IList<ElementId> ListLineStyles(Document pDoc) { IList<ElementId> ids = new List<ElementId>(); string s = string.Empty; Category catLines = pDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines); CategoryNameMap subCatLines = catLines.SubCategories; foreach (Category lineStyle in subCatLines) { if (s != string.Empty) s = s + Environment.NewLine; s = s + string.Format("{0} (id={1})", lineStyle.Name, lineStyle.Id.ToString()); ids.Add(lineStyle.Id); } return ids; }
Oops, stripped it a bit to far. Remember to return the s string in a message or something. Dale
Sorry for the late reply - and many thanks - indeed this seems to work. My previous approach was to record all names, lineweights and colors, delete the custom linestyles and recreate them with new names. But by that I was losing the linepattern information (in 2016). Your solution looks much better. I'll adjust my Python code for Dynamo accordingly.
It appears this code lists the line style data and doesn't set the name to a different name. This is the error I'm getting:
error Gridline can't assign to read-only property Name of type 'Category'
Using python if that maters.
This has not been resolved. Is there a solution in 2019?
Hi,
As this topic is already marked as solution, i'm not sure this macro of mine is useful to you.
To test the macro i would suggest you to use the Revit sample file like "rst_basic_sample_project.rvt"
For example, there are some Line Styles starting with "AutoCAD_Structural_Detailing_...." or "SW.."
To replace(rename) those names this macro will use a Rename helper function.
I hope you will get the point and how to use it for your own purpose.
Hope this helps.
Cheers,
So-Chong
public void RenameLineStyle()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
Category linesCat = doc.Settings.Categories.get_Item("Lines");
using (Transaction t = new Transaction(doc, "Rename Linestyle"))
{
t.Start();
IList<Category> categoryList = linesCat.SubCategories.Cast<Category>().Where(c => c.Name.StartsWith("AutoCAD") || c.Name.StartsWith("SW")).ToList();
foreach (Category cat in categoryList)
{
ElementId eid = cat.Id as ElementId;
Element type = doc.GetElement(eid);
if (null != type)
{
type.Name = Rename(cat.Name);
}
}
t.Commit();
}
}
public string Rename(string oldName)
{
StringBuilder newName = new StringBuilder(oldName);
newName.Replace("AutoCAD_Structural_Detailing_", "ASD-");
newName.Replace("SW", "SW-API");
return newName.ToString();
}
It was indeed useful; I can't imagine what I had been doing previously. Given that I was awarded the solution, and it seems one cannot be stripped of an award, and in the spirit of completeness, the following is the finished code based on your macro. Thanks, and you should have this one. Dale
public void RenameLineStyles(RvtDoc pDoc, string pstrFind, string pstrReplace, bool pblnMatchCase) { Category linesCat = pDoc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines); IList<Category> catList; using (Transaction tr = new Transaction(pDoc, "Rename Linestyle")) { tr.Start(); if (pblnMatchCase) { catList = linesCat.SubCategories.Cast<Category>().Where(c => c.Name.ToUpper().Contains(pstrFind.ToUpper())).ToList(); } else { catList = linesCat.SubCategories.Cast<Category>().Where(c => c.Name.Contains(pstrFind)).ToList(); } foreach (Category cat in catList) { ElementId eid = cat.Id as ElementId; Element type = pDoc.GetElement(eid); if (null != type) { type.Name = Rename(cat.Name, pstrFind, pstrReplace); } } tr.Commit(); } } public static string Rename(string pstrOldName, string pstrFind, string pstrReplace) { StringBuilder sbNewName = new StringBuilder(pstrOldName); sbNewName.Replace(pstrFind, pstrReplace); return sbNewName.ToString(); }
One disclaimer, my actual usage is a little different so this exact code has not been tested. Nevertheless, it should be ok. Hopefully someone finds it useful one day.
One last point. I was getting irregular results until realising that StringBuilder is case sensitive. A bit of Googling turned up some solutions. This is what I put together.
// note StringBuilder is not naturally case-sensitive public static string Rename(string pstrOldName, string pstrFind, string pstrReplace, bool pblnMatchCase) { StringBuilder sbNewName = new StringBuilder(pstrOldName); string lstrNewName = pstrOldName; if (pblnMatchCase) { sbNewName.Replace(pstrFind, pstrReplace); lstrNewName = sbNewName.ToString(); } else { lstrNewName = pstrOldName.ReplaceEx(pstrFind, pstrReplace, StringComparison.OrdinalIgnoreCase); } return lstrNewName; } public static string ReplaceEx(this string s, string oldValue, string newValue, StringComparison comparisonType) { if (s == null) return null; if (String.IsNullOrEmpty(oldValue)) return s; StringBuilder result = new StringBuilder(Math.Min(4096, s.Length)); int pos = 0; while (true) { int i = s.IndexOf(oldValue, pos, comparisonType); if (i < 0) break; result.Append(s, pos, i - pos); result.Append(newValue); pos = i + oldValue.Length; } result.Append(s, pos, s.Length - pos); return result.ToString(); }
just wanted to say thanks for this, you have saved me a painful morning of renaming linestyles!
Hi Ryan,
Thank you for your appreciation.
I'm glad it was useful for you and saved a lot of time for you😊!
Cheers,
So-chong
NOT work for me sir I have 1200 name to change please help me
I need to remove WME and put NEXT
can u help me pleasssssssssssssssssse
https://mega.nz/file/S5RlTaBL#b5GmckcmKy2dypOFAXW2AltGOuneqIFF3YRYOzRKDp4
u can download the model from here
@so-chong
Warning: Shameless self-promotion follows:
Realtime Toolkit for Revit ( 15 day fully functioning trial or $25 USD lifetime licence), does this and much more. Download a trial version from the Autodesk App Store (search Realtime Toolkit), or from our website:
https://www.realtimeinternational.com.au/
If this is a one-off need, the free 15 day trial will get you out of trouble. Hopefully you will find it soooo useful that you will tell all your friends.
Dale
Can't find what you're looking for? Ask the community or share your knowledge.