Change color of a linestyle

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

@3Pinter Looks good. Are you running Dynamo for Revit?
image

image

off-topic (oh wow, printscreen + ctrl-v works!)

This is downloaded from dynamo website since the original 0.9 installed with revit 2017 causes instability for the 1.3 version.

But the color picker is missing as you see :sweat_smile:

Hmmm seeing this now,

image

@Konrad_K_Sobon,

Nice, color picker aside, your script does the trick.

thanks!

the color picker is from a package called UI++ Since it was merged into core Dynamo so it will be available in next release if I am not mistaken that will be Dynamo 2.0
your issue with the archi-lab package should be resolved if you download the latest. I removed that dependency on Dynamo 2.0

Hello Konrad, I’m attempting to use this Python script to accept a list of strings instead of just one string - but I get an error when I feed a list of strings into it. Would there be a simple adjustment to make it work on a list of line type names? Thanks for any help you’re able to provide.

I have turned the Python code above into the C# node and added it to archi-lab.net packages for convenience. It works like this now.

You can get the node by simply downloading the package from Package Manager.

Cheers!

-K

2 Likes

Amazing, Thank you so much Konrad.