Rename Line Patterns

Revit API Macro

Description

The following piece of code retrieves all line patterns matching a naming prefix and modifies their name. This code is an evolution of Matthew Nelson's example. Thanks Matt!

Use at your own risk. Share your thoughts.

		public void renameLinePatterns()
		{
			//Get the current document

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

			//Get all line patters starting with PREFIX

			var collector = new FilteredElementCollector(doc)
				.OfClass(typeof(LinePatternElement))
				.Where(i => i.Name.StartsWith("PREFIX")).ToList();

			//Add them to a list of ids - this is redundant but makes this code reusable for other purposes

			List<ElementId> ids = new List<ElementId>();
			for (int i = 0; i < collector.Count(); i++)
			{
				ids.Add(collector[i].Id);
			}

			//Start the transaction that will modify your document

			using(Transaction t = new Transaction(doc,"Rename LinePatterns"))
			{
				t.Start();

				try
				{
					foreach (ElementId id in ids)
					{
						//Get the line pattern element

						LinePatternElement lpe = doc.GetElement(id) as LinePatternElement;
						if (lpe != null)
						{
							//Get the line pattern name

							string lpeName = lpe.Name;

							//Modify the name of the line pattern

							lpe.Name = lpeName + "-FOO";
						}
					}

				}
				catch (Exception)
				{
					t.RollBack();
					TaskDialog.Show("Rename LinePatterns","Renaming failed");
					return;
				}
				t.Commit();
				TaskDialog.Show("Rename LinePatterns","Renaming complete");
			}
		}

Platform

Revit.

Type

Revit API Macro.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit exceeded. Please complete the captcha once again.

  • Before submitting your inquiry, take a look at the basic information on data protection here.

    Modelical.com informs you that the personal data you provide will be processed by MODELICAL CONSULTORIA S.L. as the party responsible for this website.

    Purpose of the collection and processing of personal data: To send the information that the user requires through the website. - Legitimation: Consent of the interested party. - Recipients: Hosting: Gigas, 100% Spanish and 100% secure hosting. - Rights: You may exercise your rights of access, rectification, limitation and deletion of unsubscribe@modelical.com data as well as the right to lodge a complaint with a supervisory authority.