Revit API Forum
Welcome to Autodesk’s Revit API Forums. Share your knowledge, ask questions, and explore popular Revit API topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Rename Line Styles

16 REPLIES 16
SOLVED
Reply
Message 1 of 17
Anonymous
6239 Views, 16 Replies

Rename Line Styles

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

 

16 REPLIES 16
Message 2 of 17
Dale.Bartlett
in reply to: Anonymous

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;
        }



______________
Yes, I'm Satoshi.
Message 3 of 17

Oops, stripped it a bit to far. Remember to return the s string in a message or something. Dale




______________
Yes, I'm Satoshi.
Message 4 of 17
Anonymous
in reply to: Dale.Bartlett

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.

Message 5 of 17
Dale.Bartlett
in reply to: Anonymous

Mark as solution?




______________
Yes, I'm Satoshi.
Message 6 of 17
mike
in reply to: Dale.Bartlett

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.

Message 7 of 17
Dale.Bartlett
in reply to: mike

Hi, I only just saw this reply and have been attempting to build my suggestion. I must apologise as it does not seem possible. I could be mistaken but that is how it looks.



______________
Yes, I'm Satoshi.
Message 8 of 17

This has not been resolved. Is there a solution in 2019?




______________
Yes, I'm Satoshi.
Message 9 of 17
so-chong
in reply to: Dale.Bartlett

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();
        }

Rename-line-style.png

Message 10 of 17
Dale.Bartlett
in reply to: so-chong

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.




______________
Yes, I'm Satoshi.
Message 11 of 17

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();
}

 

 

 




______________
Yes, I'm Satoshi.
Message 12 of 17
ryan.lenihan
in reply to: so-chong

just wanted to say thanks for this, you have saved me a painful morning of renaming linestyles!

Message 13 of 17
so-chong
in reply to: ryan.lenihan

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

Message 14 of 17
Anonymous
in reply to: Anonymous

Is this same issue can be solved via Dynamo or Dynamo Python in some way. 

Message 15 of 17
Mouhammad_mazzaz
in reply to: 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  




 
Message 16 of 17

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




______________
Yes, I'm Satoshi.
Message 17 of 17

Thank you engineer
You saved my day
Thank you very much

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report