How to define Document ParameterBindings Definition is shared parameter and get GUID?

How to define Document ParameterBindings Definition is shared parameter and get GUID?

s04049
Enthusiast Enthusiast
1,259 Views
6 Replies
Message 1 of 7

How to define Document ParameterBindings Definition is shared parameter and get GUID?

s04049
Enthusiast
Enthusiast

Hello 

I am trying to define shared Parameter and project parameter form doc.ParameterBindings function,

however I got Object reference not set to an instance of an object error, even is shared parameter object.

I am following an old blog post , is there any change on revit 2021?

https://spiderinnet.typepad.com/blog/2011/05/parameter-of-revit-api-30-project-parameter-information...

 

	        BindingMap I_BM=_doc.ParameterBindings;
            DefinitionBindingMapIterator it = I_BM.ForwardIterator();
            it.Reset();
            while(it.MoveNext())
            {
                Definition def = it.Key;
                try
                {
                    ExternalDefinition extDef = def as ExternalDefinition;
                    MessageBox.Show(extDef.GUID.ToString());
                }
                catch(Exception ex)
                {
                    //Normal Project Parameter
                    MessageBox.Show(ex.Message+ex.StackTrace);
                }
            }

 

0 Likes
Accepted solutions (1)
1,260 Views
6 Replies
Replies (6)
Message 2 of 7

RPTHOMAS108
Mentor
Mentor

Instances of the ExternalDefinition class only exist in the shared parameter file (DefinitionsFile) and are only used for methods that deal with that.

 

You therefore can't cast to one from the bindings keys, these I think are of type InternalDefinition. If so this has the method InternalDefinition.GetTypeId that will hold the GUID (if definition is for a shared parameter).

0 Likes
Message 3 of 7

s04049
Enthusiast
Enthusiast

Getting into the Building Map with Revit Lookup, there are only show Instance Binding and Type Binding.

nothing to get from there.

s04049_0-1637835975660.png

 

0 Likes
Message 4 of 7

RPTHOMAS108
Mentor
Mentor

Yes, but you were iterating the map with DefinitionBindingMapIterator and getting the key values (Keys are the Definitions not the values). RevitLookup doesn't do that.

 

i.e. BindingMap.Item property takes a Definition to return a Binding.

0 Likes
Message 5 of 7

RPTHOMAS108
Mentor
Mentor

 

Public Function Obj_211125a(commandData As ExternalCommandData, ByRef message As String, elements As ElementSet) As Result

        Dim app = commandData.Application
        Dim uidoc = commandData.Application.ActiveUIDocument
        Dim IntDoc = uidoc.Document

        Dim PB As BindingMap = IntDoc.ParameterBindings

        Dim PBM_FwdIt As DefinitionBindingMapIterator = PB.ForwardIterator

        PBM_FwdIt.Reset()
        While PBM_FwdIt.MoveNext = True
            Dim IDef As InternalDefinition = PBM_FwdIt.Key
            Dim Ty As String = ""
            If PBM_FwdIt.Current.GetType = GetType(InstanceBinding) Then
                Ty = "(Instance)"
            ElseIf PBM_FwdIt.Current.GetType = GetType(TypeBinding) Then
                Ty = "(Type)"
            End If

            Debug.WriteLine(IDef.Name & " " & IDef.GetTypeId.TypeId & " " & Ty)

        End While

        Return Result.Succeeded


    End Function

 

Sample output for project parameter:
rpt autodesk.parameter.aec.revit.project:610094-1.0.0 (Instance)

 

Sample output for shared parameter:
ProfileAngle autodesk.parameter.aec.revit.external.-1:e544d5d1a0cb490bb276d191cd8b1456-1.0.0 (Type)

 

So can be seen from above you can get the Guid of the shared parameter or the ElementId (presumed) of the non-shared project parameter using the above method. The Guid is not in a convenient form but you can get it. Not sure if there is an easier way because the SharedParameterElement class is not just for those bound in the project.

 

Although you can't cast an InternalDefinition to an ExternalDefinition you can feed an ExternalDefinition into BindingMap.Item to get the binding (which is more common).

0 Likes
Message 6 of 7

s04049
Enthusiast
Enthusiast

I am sorry, I can't find the GetTypeId method on InternalDefinitions class.

can you explain in more detail to define the external parameter in BindingMap Class?

https://www.revitapidocs.com/2021.1/1f5f93dd-0ddc-c7d6-9ac1-332f0f14a5ae.htm

0 Likes
Message 7 of 7

RPTHOMAS108
Mentor
Mentor
Accepted solution

Sorry didn't see you were limited to 2021, it was probably introduced in 2022.

 

Here is a better alternative for both anyway:

 

Public Function Obj_211126a(commandData As ExternalCommandData, ByRef message As String, elements As ElementSet) As Result

        Dim app = commandData.Application
        Dim uidoc = commandData.Application.ActiveUIDocument
        Dim IntDoc = uidoc.Document

        Dim PB As BindingMap = IntDoc.ParameterBindings

        Dim PBM_FwdIt As DefinitionBindingMapIterator = PB.ForwardIterator

        PBM_FwdIt.Reset()
        While PBM_FwdIt.MoveNext = True
            Dim IDef As InternalDefinition = PBM_FwdIt.Key
            Dim Ty As String = ""
            If PBM_FwdIt.Current.GetType = GetType(InstanceBinding) Then
                Ty = "(Instance)"
            ElseIf PBM_FwdIt.Current.GetType = GetType(TypeBinding) Then
                Ty = "(Type)"
            End If

            Dim IdStr As String = ""
            Dim El As Element = IntDoc.GetElement(IDef.Id)
            If El.GetType = GetType(SharedParameterElement) Then
                Dim SPE As SharedParameterElement = El
                IdStr = SPE.GuidValue.ToString
            ElseIf El.GetType = GetType(ParameterElement) Then
                IdStr = CStr(IDef.Id.IntegerValue)
            End If

            Debug.WriteLine(IDef.Name & " " & IdStr & " " & Ty)

        End While

        Return Result.Succeeded


    End Function

Sample output:

 

rpt 610094 (Instance)

ProfileAngle e544d5d1-a0cb-490b-b276-d191cd8b1456 (Type)

 

0 Likes