Dimension lock (several)

Hi guys,

ive managed to lock 1 dimension line but im trying to lock a list of dimension with python. see graph. when i insert several dimensions i get a warning. can anyone help me on this one. thanks


You need to add a for loop to process each of the dimensions not the list of dimensions.

dims = UnwrapElement (IN[0])
for dim in dims:
    if dim.Segments.Count >0:
        for ds in dim.Segments:
            ds.IsLocked =True
    else:
        dim.IsLocked =True
1 Like

as the error says you are using NumberOfSegments Property with List. Try to use for loop for each element in list.

1 Like

I did as you sayd @SeanP but i stil get this warning

Just read a error message!

Segments Property returns a read only array of segments in the dimension -> https://www.revitapidocs.com/2020/d7fcdab2-ca81-0ed1-4813-f7aa092430d7.htm

use len(dim.Segments) instead of Count

1 Like

As noted before:

This works for one string of dimensions, or one individual dimension, but not for two dimensions or for two strings.

The code needs to ensure that a single dimension is converted into a list of dimensions (or strings), and then needs to handle every dimension/string in the list of dimensions.

Otherwise you’ll be pulling properties from an array all day long.

1 Like

@wihibyusuf Try this:

dims = UnwrapElement(IN[0])
out = []

if not isinstance(dims, list):
    dims = [dims]

TransactionManager.Instance.EnsureInTransaction(doc)

for dim in dims:
    if dim.NumberOfSegments>0:
        for ds in dim.Segments:
            ds.IsLocked = True
            out.append(dim)
    else:
        dim.IsLocked = True
        out.append(dim)
        
TransactionManager.Instance.TransactionTaskDone()

OUT = out

*EDITED after @jacob.small’s comment!

1 Like

This is close, but what happens if you feed it one dimension instead of 2?

Well done @AmolShah. :slight_smile:
I’ll add a bonus - This add sets the ‘true false’ which was previously only serving the purpose of forcing a regen, so that it either locks or unlocks the dimension:

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

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

clr.AddReference("RevitAPI")

import Autodesk
from  Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
dims = UnwrapElement(IN[0])
lock = IN[1]

if not hasattr(dims,'__iter__'):
	dims = [dims]

TransactionManager.Instance.EnsureInTransaction(doc)

for dim in dims:
	if dim.NumberOfSegments>0:
		for ds in dim.Segments:
			ds.IsLocked = lock
	else:
		dim.IsLocked = lock
        
TransactionManager.Instance.TransactionTaskDone()

OUT = dims

Likely worth reducing the imports there if this is all you want to do - this feels a bit heavy handed. :slight_smile:

4 Likes

Awesome guys… it has worked. Im thankful for this forum that made this possible.

Thank you guys.

I tried you script, as I have many locked dimns and I want to unlock them. But, when I run the script I got this warning

Warnung:IronPythonEvaluator.EvaluateIronPythonScript fehlgeschlagen.
Traceback (most recent call last):
File “”, line 19, in
IndexError: index out of range: 1

could you please help?

Any chance you only have one input in the Python node? If so you’ll need to hit the + button and add a new input of ‘false’ to unlock the dimension.

1 Like

It works … thanks a lot

1 Like