Line pattern name

Line pattern name

damyan_lyaskov
Contributor Contributor
4,896 Views
8 Replies
Message 1 of 9

Line pattern name

damyan_lyaskov
Contributor
Contributor

Hello,

 

I am trying to find all the line pattern that have/start with "IMPORT" in their name.  If the Name is "IMPORT-XXXXXXX", the following code capture's it, but if the Name is "IMPORT:XXXXXXX"- it does not.

 

Its like ignoring anything before the semicolon and consider the line pattern name the rest of the string after the semicolon.

 

 

UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;

FilteredElementCollector collector = new FilteredElementCollector(doc);
ICollection<Element> collection = collector.OfClass(typeof(LinePatternElement)).ToElements();

if (e.Name.Contains("IMPORT"))

{...}

 

Thank you.

0 Likes
Accepted solutions (2)
4,897 Views
8 Replies
Replies (8)
Message 2 of 9

Anonymous
Not applicable

Dear damyan,

 

Revit do not allow ":" to Make line pattern name might this is the reason your code is not working properly but your code is pretty good.

 

AA.png

 

if you share .rvt file so i can also try to find out the way. These line pattern imported from CAD. 

For the same work i did.

 

List<LinePatternElement> lines = coll.Cast<LinePatternElement>().Where(e=> e.Name.ToLower().StartsWith("import")).ToList(); 

 

Note:
 1. In my case there line pattern not contain ":

 2. You can watch name value using Revit Look Up this will help a lot.

 

Happy New Year.

 

 

 

 

0 Likes
Message 3 of 9

damyan_lyaskov
Contributor
Contributor

Hello Manish Singh,

 

Please find attached the file with the mentioned Line Patterns.

 

Thank you.

0 Likes
Message 4 of 9

Anonymous
Not applicable

Dear,

 

I am on holiday and on my personal system revit 2015 is available so sorry to say i am not able to check this file.
I will check this after 2nd Jan. if you have urgency please provide 2015 version file so that i will try.

0 Likes
Message 5 of 9

Anonymous
Not applicable

Dear ,

 

Use below code.

 

 Dictionary<ElementId,string> lines = coll.Cast<LinePatternElement>().Select(x => new { x.Id, x.GetLinePattern().Name }).ToDictionary(t=> t.Id,t=> t.Name);
0 Likes
Message 6 of 9

damyan_lyaskov
Contributor
Contributor

Hello Manish,

 

I hope you had very nice holidays.

 

When I change my code as you suggested:

 

Dictionary<ElementId,string> lines = coll.Cast<LinePatternElement>().Select(x => new { x.Id, x.GetLinePattern().Name }).ToDictionary(t=> t.Id,t=> t.Name);


                foreach (var e in lines)
                {
                    try
                    {
                        if (e.Value.Contains("IMPORT-"))
                            {                        
                                doc.Delete(e.Key);
                                x += 1;
                            }

                        }
                    catch(Exception ex)
                    {
                    }
                }

 

I have run time exception thrown.  "A problem has been detected.  Autodesk.Revit.Exceptions.InvalidOperataionException: The collector does not have a filter applied.  Extraction or iteration of elements is not permitted without a filter. ......  "See jpg for more.

Thank you.

0 Likes
Message 7 of 9

Anonymous
Not applicable
Accepted solution

Dear Damyan,

 

Thank you.

 

Yes i spend splendid holidays. 

 

 

As i go through your code you simply want to delete those line patter which is start with "IMPORT-". For this use below code and let me know if you find any issue.

 

 FilteredElementCollector coll = new FilteredElementCollector(doc).OfClass(typeof(LinePatternElement));

            List<ElementId> lstIds=   coll.Cast<LinePatternElement>().Where(x=>  x.GetLinePattern().Name.Contains("IMPORT-")).Select(x=> x.Id).ToList();

            using(Transaction trans=new Transaction(doc,"del"))
            {
                trans.Start();
                doc.Delete(lstIds);
                trans.Commit();

            }

In previous code provide by me to get information of Line Pattern. You can also use your code just use Make Ids list in foreach loop and do delete like above.

 

Since your are using foreach  to delete Line Pattern thus why you receive exception.

 

I hope this resolve your problem.

 

0 Likes
Message 8 of 9

damyan_lyaskov
Contributor
Contributor
Accepted solution

You nail it, Manish.

 

Thanks a lot.

0 Likes
Message 9 of 9

Anonymous
Not applicable

Gentleman,

I want to apply this to my Revit template.  Can you help add this?  I don't know anything about code.  Did you create a macro? if so, what language? I have the same issue of trying to delete imported line patterns.  I am hoping that you get this, thanks.

 

MJ

 

0 Likes