Change color of a linestyle

Is there a way to change the Line Color of a line style through Dynamo?

The only parameters I get are
Design Option
Category
Type Name
Category
Family Name

I want to change the color :sweat_smile:

this might help you a little - Get all project Line Styles and attributes

1 Like

Hmm interesting.

It seems to be a read only value?

Could you post a screenshot showing this?

Please post where you have got up to with your dynamo script and associated python code.

Also have you looked at the revit sdk for how you set line colours(color in sdk).

http://www.revitapidocs.com/2017.1/775ac9cd-9bde-568f-76d5-afeb370344a1.htm

Will post my code shortly … pretty much ruined it, must rebuild it a bit again :slight_smile:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

# Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

# Start Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

lineStyle = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines)
lineStyleSubTypes = lineStyle.SubCategories

listNames = []
listRGB = []

#string
linetobechanged = IN[0]
#rgb (0,0,0)
linenewcolor = IN[1]
for i in lineStyleSubTypes:
	name = i.Name
	# append only if 'name' matches IN[0]
	if linetobechanged is name:
		rgb = [i.LineColor.Red, i.LineColor.Green, i.LineColor.Blue]
		weight = i.GetLineWeight(GraphicsStyleType.Projection)
		listNames.append(name)
		listRGB.append(rgb)
		set(i.LineColor.Red) = IN[1][1]
		set(i.LineColor.Green) = IN[1][2]
		set(i.LineColor.Blue) = IN[1][3]
		
		

# End Transaction
TransactionManager.Instance.TransactionTaskDone()

OUT = lineStyleSubTypes, listNames, listRGB, lineWeight

Knipsel

Not sure it would work. Did you try to create and apply a new line style instead (see Bimorph LineStyle.Create node for instance)

I Haven’t @Yna_Db because the lines are in blocks etc. figured I was better off changing the color rather than creating a new line and swapping it (fix groups issues etc.)

To set a line colour you do not input the colours separately as you have you do it as follows

<element>.LineColor=Color(<Red>,<Green>,<Blue>)

therefore your 3 lines will become:

i.LineColor = color(IN[1][1],IN[1][2],IN[1][3])

1 Like

@3Pinter has the above fixed your issue if yes please highlight it as solution or reply back if any other issues.

@Brendan_Cassidy,

Not yet, but that has more to do that my script is still faulty.
Your i.LineColor might work though!

First do some actual workstuff, then Im getting back to it!

@Brendan_Cassidy, @Yna_Db, others,

k, fiddle time.

My if condition doesn’t seem to work and I guess therefor the set color too.

IN[0] = string (name of linestyle)
IN[1] = array {0,128,0};

Any thoughts what goes wrong?

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

# Import ToDSType(bool) extension method
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

# Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

# Start Transaction
TransactionManager.Instance.EnsureInTransaction(doc)

lineStyle = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines)
lineStyleSubTypes = lineStyle.SubCategories

listNames = []
listRGBold = []
listRGBnew = []

#string
linetobechanged = IN[0]
#rgb (0,0,0)
linenewcolor = IN[1]
for i in lineStyleSubTypes:
	name = i.Name
	# append only if 'name' matches IN[0]
	if name is linetobechanged:
		rgb_old = [i.LineColor.Red, i.LineColor.Green, i.LineColor.Blue]	
		listNames.append(name)
		listRGBold.append(rgb_old)
		i.LineColor = color(IN[1][1],IN[1][2],IN[1][3])
		listRGBnew.append(i.LineColor)
		break

# End Transaction
TransactionManager.Instance.TransactionTaskDone()

OUT = linetobechanged, lineStyleSubTypes, listNames, listRGBold

It seems from the link below that you could need to use the GraphicsStyleCategory.LineColor in order to change the color of a line style:

@3Pinter Could you post the error you are getting? but try the below first.

Also if you are checking if one thing matches another i would make the if statement do the following:

if name == linetobechanged:

Though if it is partial then it would be:

if linetobechanged in name:

This should do the trick:

# Copyright(c) 2017, Konrad K Sobon
# @arch_laboratory, http://archi-lab.net

import clr

# Import Element wrapper extension methods
clr.AddReference("RevitNodes")
import Revit

clr.ImportExtensions(Revit.Elements)

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

# Import RevitAPI
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *

import sys

pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

try:
	errorReport = None
	
	TransactionManager.Instance.EnsureInTransaction(doc)
	
	cat = Category.GetCategory(doc, BuiltInCategory.OST_Lines)
	gs = cat.GetGraphicsStyle(GraphicsStyleType.Projection)
	gsCat = gs.GraphicsStyleCategory.SubCategories
	
	style = next((i for i in gsCat if i.Name == IN[0]), None)
	if style != None:
		style.LineColor = Color(IN[1].Red, IN[1].Green, IN[1].Blue)
	
	TransactionManager.Instance.TransactionTaskDone()
	
except:
	# if error accurs anywhere in the process catch it
	import traceback
	errorReport = traceback.format_exc()

#Assign your output to the OUT variable
if errorReport == None:
	OUT = style.GetGraphicsStyle(GraphicsStyleType.Projection)
else:
	OUT = errorReport

Couple of sidenotes:

@Yna_Db please refrain from answering the question with multiple links that don’t really provide an answer, nor bring the OP closer to an answer.
@Tom_Kunsman Your link was a good place to start. Following the code in that post the OP would have been able to get the line styles and their properties so that’s for sure a step in the right direction. Thanks for sharing.
@3Pinter Your assessment that the LineColor was a read only property was wrong because you were looking at the wrong property. It’s not the GraphicsStyleCategory that holds the LineColor but the Category itself.

image

Cheers!

6 Likes

I took the time to look at it before making a suggestion that does not seem contradictory at all to the solution you provided… :smirk:

@Yna_Db,

Like I have stated the GraphicsStyleCategory.LineColor is read only property hence you cannot use that to set the line color. The OP himself stated that at the beginning of the post, so this suggestion not only does not provide a valid solution but further confuses the issue.

Cheers!

1 Like

@Konrad_K_Sobon,

Grabbing my second coffee now, and diving into that post of yours!

First thing I noticed (and hence why I created an array for my rgb-color) is that there is no Color Picker node available. I know it should be at core > color > color picker … but nothing here.

Running 1.3.2.2480

Knipsel