iLogic - Angle constraint between occurences

iLogic - Angle constraint between occurences

Lewis.Young
Collaborator Collaborator
298 Views
2 Replies
Message 1 of 3

iLogic - Angle constraint between occurences

Lewis.Young
Collaborator
Collaborator

Hi All,

 

Struggling to get this code to work, so hoping someone on here can help.

 

  • I have an assembly with a number of truss components in it called T1, T2, T3 and so on. (This is what they're renamed to in the browser tree, not the actual file names)
  • Within that same assembly, I have a number of linked parameters called A1, A2, A3 and so on.
  • I would like the code to run through each pairing and apply the constraints between their respective XY planes until it runs out of trusses, in this manner:

T1 & T2, apply A1 angle constraint between occurrence XY planes

T2 & T3 apply A2 angle constraint between occurrence XY planes

T3 & T4 apply A3 angle constraint between occurrence XY planes

 

When I run the code I get the error message saying "Conversion from string "T1" to type 'Integer' is not valid"

 

' Main logic
Dim asmDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim compDef As AssemblyComponentDefinition = asmDoc.ComponentDefinition

Dim i As Integer = 1

' Loop through the components named T1, T2, T3, etc.
Do While True
    Try
        ' Check if the angle parameter exists
        Dim angleParamName As String = "A" & i
        Dim paramExists As Boolean = False

        ' Loop through all the parameters to see if it exists
        For Each param In compDef.Parameters
            If param.Name = angleParamName Then
                paramExists = True
                Exit For
            End If
        Next

        If Not paramExists Then
            MessageBox.Show("Parameter " & angleParamName & " not found. Stopping the rule.")
            Exit Sub ' Stop the rule if the parameter is not found
        End If

        ' Get the angle parameter value
        Dim angleValue As Double = compDef.Parameters.Item(angleParamName).Value


       	' Get the current and next components in the browser tree
        Dim compCurrent As ComponentOccurrence = compDef.Occurrences.Item("T" & i)
       	Dim compNext As ComponentOccurrence = compDef.Occurrences.Item("T" & (i + 1))

        If compCurrent Is Nothing Or compNext Is Nothing Then
            MessageBox.Show("Could not find components T" & i & " or T" & (i + 1))
            Exit Do
        End If
		

		'Setup Proxies
		Dim wPlane1 As WorkPlaneProxy
		Dim wPlane2 As WorkPlaneProxy
	
		'Set proxies to occurence XY planes:
		compCurrent  = compDef.Occurrences.Item("T" & i)
		compNext= compDef.Occurrences.Item("T" & (i + 1))

		compCurrent.CreateGeometryProxy(compCurrent.Definition.WorkPlanes.Item(3), wPlane1)
		compNext.CreateGeometryProxy(compNext.Definition.WorkPlanes.Item(3), wPlane2)
		
		Dim flushConst As AssemblyConstraint = compDef.Constraints.AddAngleConstraint(wPlane1, wPlane2, angleValue) 

        ' Provide feedback
        MessageBox.Show("Angle constraint applied between " & compCurrent.Name & " and " & compNext.Name & " with value " & angleValue)

        ' Move to the next set of components
        i += 1

    Catch ex As Exception
        MessageBox.Show("Finished applying constraints or encountered an error: " & ex.Message)
        Exit Do
    End Try
Loop

 

If someone could help that would be great 😀

 

Thanks

 

0 Likes
Accepted solutions (1)
299 Views
2 Replies
Replies (2)
Message 2 of 3

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Lewis.Young.  After reading your explanation, then quickly glancing down your code, I think I see where the problem is.  It has to do with how you are doing String concatenation.  You should always use the & character for String concatenation, and never the + character, especially when numerical values are included in the String.

So,

 

compDef.Occurrences.Item("T" & i)

 

...is 'sort of' OK, even though it is concatenating an Integer directly into a String, due to the use of only the & character between them.  However, this:

 

compDef.Occurrences.Item("T" & (i + 1))

 

...is pushing the limits, because it is doing both math and String concatenation in the same space.  You should either separate that out into multiple lines of code, or maybe use the '(i+1).ToString' conversion, or 'CStr(i+1)' conversion, or something like that.  It does not like when you combine math and String concatenation together in the same condensed line like that, due to Type casting/conversions.

 

PS... On second thought, that was not actually the main problem.  The main problem was that you are attempting to get those components by their component names, but using the regular 'Item' property to do so, which only accepts Integer type values.  Because of that restriction, it was trying to convert your String (component name) into an Integer, which is needed as input for that property, and it can not do that.  If you want to get a component by its component name, then you must use the ComponentOccurrences.ItemByName property instead.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

Lewis.Young
Collaborator
Collaborator

Working perfectly now, thanks for that!

0 Likes