Creating a Line Pattern with Python

I came across this (what I’m guessing is C#) code for creating a new line pattern type.

https://forums.autodesk.com/t5/revit-api-forum/create-line-pattern-type/m-p/3682176#M3293

I’m trying to put this in a Python Script node for use in Dynamo. I simplistically started with:

patternName = “test”
linePattern = LinePattern(patternName)
LinePatternElement.Create(doc, linePattern)

I’m sure it is obvious to many why this doesn’t work. The error indicates the Line Pattern is invalid parameter name linePattern. I assume that means my attempt at assigning linePattern = is wrong. Hoping someone can get me going in the right direction.

Hi @nelson.weeks,

If you translate the C# code snippet into python this gives :

import clr
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)

# Import DocumentManager and TransactionManager
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

patternName = IN[0]

#Create list of segments which define the line pattern
lstSegments =  List[LinePatternSegment]()
lstSegments.Add(LinePatternSegment(LinePatternSegmentType.Dot, 0.0));
lstSegments.Add(LinePatternSegment(LinePatternSegmentType.Space, 0.02));
lstSegments.Add(LinePatternSegment(LinePatternSegmentType.Dash, 0.03));
lstSegments.Add(LinePatternSegment(LinePatternSegmentType.Space, 0.02));

linePattern = LinePattern(patternName)
linePattern.SetSegments(lstSegments)

#Create a linepattern element
TransactionManager.Instance.EnsureInTransaction(doc)
linePatternElement = LinePatternElement.Create(doc, linePattern)
TransactionManager.Instance.TransactionTaskDone()
OUT=linePatternElement

image

3 Likes

@Alban_de_Chasteigner thank you for the response! Did you use the “Convert Code to” tool in the Macro Manager environment to translate?

I didn’t think about it.
The Python syntax is different in the macro environment.

I see, you used your knowledge of both C# and Python to make the translation. I asked about the “Convert Code to” because I attempted to do that, but couldn’t make the result work. Thanks again!

Hi,
I refined the script and created a node.

Create%20Line%20Pattern

1 Like

Pretty cool. At the risk of sounding greedy, it would be more useful for the node to take a list of Types and Lengths because every linepattern doesn’t have the same number of segments.

Hi,

I will not do a node with more than 21 inputs :sweat:

You can use the lacing longest if your line pattern is repetitive with input lists.
line%20patterns

2 Likes

This is what I had it mind…inputting lists of pattern names, segments types and lengths (varying number of segments from one pattern to next). I appreciate @Alban_de_Chasteigner for getting me started on the right path. I’m open to any ideas on how to improve it further.

import clr
clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
# Import DocumentManager and TransactionManager
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitAPIUI")
from Autodesk.Revit.UI import TaskDialog

patternName = IN[0]
segTypes = IN[1]
segLengths = IN[2]

#counter for number of patterns to create
 j=0

  #iterate through each pattern and create
   for j in range(len(patternName)):

pattName = str(patternName[j])

    #counter for segments in current pattern
i=0

   #Create list of segments defining the line pattern
lstSegments =  List[LinePatternSegment]()

for i in range(len(segTypes[j])):
	length = segLengths[j][i]
	strLength = str(length)
	
#determine current segment type and add to list of segments for current pattern
	curSegment = str(segTypes[j][i])

	
	if curSegment == "Dash":
		
		lstSegments.Add(LinePatternSegment(LinePatternSegmentType.Dash,segLengths[j][i]))
		
	elif curSegment == "Space":
		
		lstSegments.Add(LinePatternSegment(LinePatternSegmentType.Space,segLengths[j][i]))
	
	elif curSegment == "Dot":
		
		lstSegments.Add(LinePatternSegment(LinePatternSegmentType.Dot,segLengths[j][i]))
		

linePattern = LinePattern(pattName)
linePattern.SetSegments(lstSegments)

#Create a linepattern element
TransactionManager.Instance.EnsureInTransaction(doc)
linePatternElement = LinePatternElement.Create(doc, linePattern)
TransactionManager.Instance.TransactionTaskDone()

 OUT=[]
 OUT=linePatternElement

Hi Nelson,

You’re absolutely right that the node can be improved.

I’m not an expert in python but you can probably simplify your code.
Take a look at the new version of the node.

2 Likes

@Alban_de_Chasteigner Hi Alban, i am totally new to Python and i tried to copy the code from Nelson’s post but it didn’t work.
can you share with me the code of this new version of the node?
Thanks

Hi Hassan,

You can find this node in the Genius Loci package.

2 Likes

i tried to add numbers from excel sheet and here comes an error

Hi Alban, old topic here but do you have a technique for retrieving the line pattern segment definitions. I’ve got as far as the GetSegments Method for LinePattern but cannot find how to see the lengths of the dots, dashes and spaces etc.
Thanks in advance