pyRevit Script - LineStyle Change

Hi Everyone,

Objective: Change LineStyle

History;
I’m struggling here with changing the LineStyle of a line that is already drawn. I’m assuming I need to draw the line and than modify the linestyle of the 1 that is drawn.

I found this in the Revit API Documentary - LineStyle
I’m assuming I use the C# and than convert that into what I believe Python will be (Newbie Question - Is there a python dictionary similar to this Revit API C#? or do i just always take a calculated guess on the conversion? I’ve been searching all over the forums/google and haven’t stumbled across a full list yet)

So based on that I believe i’m supposed to use
Element.LineStyle(get, set.12404)

Attached is my script -.
script.py (727 Bytes)

I’ve searched all the forums & even tried pulling a piece off this;

Code Example

I’m very green and apologies in advanced for some a newbie question.

Thanks in advanced
Andy

1 Like

Not a pyRevit solution (then again this isn’t a pyRevit forum), but you could do this in Dynamo with all out of the box nodes (the design script in code blocks is just to illustrate the before and after):

Note sure if this is what you are after.

Save the below code as a py file. it will let you select a detail line and change its line style to “Medium Lines”

Thanks @Konrad_K_Sobon for the code to retrieve the Line styles (“Get Line Style by Name” Node)

import clr

# Import RevitAPI
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument

__window__.Close()

reference = uidoc.Selection.PickObject(Selection.ObjectType.Element, "Select Line")

Line = doc.GetElement(reference.ElementId)

cat = Category.GetCategory(doc, BuiltInCategory.OST_Lines)
gs = cat.GetGraphicsStyle(GraphicsStyleType.Projection)
gsCat = gs.GraphicsStyleCategory.SubCategories
lineStyles = [i.GetGraphicsStyle(GraphicsStyleType.Projection) for i in gsCat]

lStyle = []
try:
	lStyle.append(next(i for i in lineStyles if "Medium Lines"==i.Name))
except StopIteration:
	pass

 
t = Transaction(doc, "Change Line Style")
t.Start()

Line.LineStyle=lStyle[0]

t.Commit()
2 Likes

You are awesome, That managed to work with a few minor tweaks. I feel its very complicated tho, is that normal? or is there a simplier way of doing it.

I need to work out how to loop it now, So it does multiple lines next challenge for me…

Thank you heaps :slight_smile:

# pyRevit
import clr
import math
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application
view = doc.ActiveView

#----
__doc__ = '0.18 Continuous Line'

#Transaction1
t = Transaction(doc, "Draw Line")
t.Start()
	
#Pick Points
p1 = uidoc.Selection.PickPoint("Click to specify instance location.")
p2 = uidoc.Selection.PickPoint("Click to specify instance location.")
 
#Commands
Line = Line.CreateBound(p1, p2)
NewLine = doc.Create.NewDetailCurve(view,Line)

#Linestyle
gsCat = Category.GetCategory(doc, BuiltInCategory.OST_Lines).GetGraphicsStyle(GraphicsStyleType.Projection).GraphicsStyleCategory.SubCategories
lineStyles = [i.GetGraphicsStyle(GraphicsStyleType.Projection) for i in gsCat]
lStyle = []
try:
	lStyle.append(next(i for i in lineStyles if "Medium Lines"==i.Name))
except StopIteration:
	pass
NewLine.LineStyle=lStyle[0]
		
t.Commit()
1 Like

Hi @Andy2,

Great to hear you are experimenting further with this to implement looping/multiple element application. :+1:

You are correct in assuming simplified solutions exists, here is but one of them.

Change LineStyle
image
image

2 Likes