cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Change Structural Shape Size From Content Center via API

Change Structural Shape Size From Content Center via API

I would love the functionality to be able to select a different table row for changing the size of structural shapes via the API. This would function similarly to the "Change Size" command that is already in the Inventor interface.

 

iLogic already has this functionality for fasteners, but I would like to see it implemented for structural shapes as well.

1 Comment
el_jefe_de_steak
Collaborator

I figured out how to do this using existing API commands. The code is relatively involved but works very well. 

It would be awesome if this functionality were added to the iLogic API, but until then, here's the workaround.

 

 

 

 

'' This document can be used in a part document to update the size based on selection
'' Steps to take:
''   1. Copy-Paste this code into the part document
''   2. Comment the line that is marked below with a "2 - "
''   3. Run this rule one time to build the stock number list
''   4. Comment the lines that are marked below with a "3 - "
''   5. Uncomment the line below that is marked with "5 - "
''       - 5b. Follow the instructions listed at the end of the line you just uncommented
''   6. (optional) Add a Configure form to allow the user to change beam size easily.


Class thisRule
	
	Dim oFamily As ContentFamily

#Region "User Interface Functions"

	Sub Main()
		
		Break 'used for debugging with VisualStudio
		
		
		Dim oPart As PartDocument
		Try
			oPart = ThisDoc.Document
			oFamily = GetContentFamily(oPart)
		Catch
			Exit Sub
		End Try
		
		'' 5 - uncomment this line for use inside a part
		'Dim newStockNumber As String = BeamSize 'change this to match your new size parameter's name
		''
		
		
		'' 4 - comment these lines for use inside a part
		Dim stockNumberListParam As UserParameter = BuildStockNumberList(oPart)
		Dim newStockNumber As String
		newStockNumber = InputListBox("Choose a new size!", MultiValue.List(stockNumberListParam.Name), newStockNumber, Title := "Change Size", ListName := "Available Sizes")
		If InStr(newStockNumber, "cancel") Then Exit Sub
		''
		
		ChangeSize(oPart, newStockNumber)	
		
		''2 - comment this out for running inside a part
		stockNumberListParam.Delete
		''
		
		InventorVb.DocumentUpdate()
	End Sub
	
	
	''' <summary>
	''' Builds a stock number list in a user parameter for display to the user
	''' </summary>
	''' <param name="oPart">the part to get the content center table from</param>
	''' <returns>multivalue user parameter</returns>
	Private Function BuildStockNumberList(oPart As PartDocument) As UserParameter
		
		
		Dim stockParamName As String = "AvaialableStockNumbers"
		Dim initialValue As String = "(cancel)"
		
		Dim oUserParams As UserParameters = oPart.ComponentDefinition.Parameters.UserParameters
		Dim stockNumberListParam As UserParameter = oUserParams.AddByValue(stockParamName, initialValue, kTextUnits)
		
		
		'Dim oFamily As ContentFamily = GetContentFamily(oPart)
		
		Dim stockNumbers As New ArrayList
		
		For i As Integer = 1 To oFamily.TableRows.Count
			'skips items that are suppressed
			If oFamily.TableRows.Item(i).Item("Status").Value <> "Suppressed" Then
				stockNumbers.Add(oFamily.TableRows.Item(i).Item("STOCKNUMBER").Value)
			End If
		Next 	
		
		MultiValue.List(stockNumberListParam.Name) = stockNumbers
		
		''couldn't figure out how to make this one work right...
		'stockNumberListParam.ExpressionList.SetExpressionList(stockNumbers, False)
		
		Return stockNumberListParam	
		
	End Function

#End Region

#Region "Material Size Change"

	''' <summary>
	''' Change the size of the part. Works similarly to the "change size" command in assembly environment.
	''' </summary>
	''' <param name="oPart">the part to change size for</param>
	''' <param name="newStockNumber">uses Stock Number to find new parameter values from content center table</param>
	Private Sub ChangeSize(oPart As PartDocument, newStockNumber As String)
		
		'get current values from the part for comparing later
		Dim oldStockNumber As String = oPart.PropertySets.Item("Design Tracking Properties")("Stock Number").Value
		
		'does nothing if new size is not selected
		If newStockNumber = oldStockNumber Then Exit Sub
		
		'update parameters from table
		For Each oUserParam As UserParameter In oPart.ComponentDefinition.Parameters.UserParameters
			Try
				Select Case oUserParam.Name
				Case "G_L_CUT", "G_OFFSET_START", "G_OFFSET_END"
					'these parameters don't get changed
				Case "GAGE"
						oUserParam.Expression = GetCellValueByStockNumber(oFamily, newStockNumber, oUserParam.Name)
				Case Else
					If InStr(oUserParam.Name, "_") And Left(oUserParam.Name, 1) = "G" Then
						
						oUserParam.Expression = GetCellValueByStockNumber(oFamily, newStockNumber, oUserParam.Name)
						
					End If
				End Select			
			Catch
				
			End Try
		Next
		
		'change member ID so that the "change size" command in assemblies shows the current beam size
		oPart.PropertySets.Item("Content Library Component Properties")("MemberID").Value = GetCellValueByStockNumber(oFamily, newStockNumber, "RID")
		
		'change the stock number
		oPart.PropertySets.Item("Design Tracking Properties")("Stock Number").Value = GetCellValueByStockNumber(oFamily, newStockNumber, "STOCKNUMBER")
		

	End Sub
	
#End Region

#Region "Content Center Table Interaction"

	''' <summary>
	''' Gets the content center family from the part
	''' </summary>
	''' <param name="oPart"></param>
	''' <returns></returns>
	Private Function GetContentFamily(oPart As PartDocument) As ContentFamily
		Dim oPropSets As PropertySets = oPart.PropertySets
		Dim oProps As PropertySet = oPropSets.Item("Content Library Component Properties")

		'find family ID
		Dim oProp As Inventor.Property = oProps.Item("FamilyId")
		Dim FamilyId As String = oProp.Value

		'get family table
		Dim oContentCenter As ContentCenter = ThisApplication.ContentCenter
		Dim oFamily As ContentFamily = oContentCenter.GetContentObject("v3#" + FamilyId + "#") 
		
		Return oFamily
	End Function
	
	
	''' <summary>
	''' Gets the value of the designated column that is in the same row as the given stock number. Returns nothing if there is an error.
	''' </summary>
	''' <param name="myFamily">the famliy that contains the table to search</param>
	''' <param name="stockNumber">the stock number to look up</param>
	''' <param name="valueColumnName">the column name to retieve the value from</param>
	''' <returns></returns>
	Private Function GetCellValueByStockNumber(myFamily As ContentFamily, stockNumber As String, valueColumnName As String) As String
			
		Dim result As String
			
		Try
			result = myFamily.TableRows.Item(GetFamilyTableRowNumber(myFamily, "STOCKNUMBER", stockNumber)).Item(valueColumnName).Value
			Return result
		Catch
			Return Nothing
		End Try
		
	End Function
	
	
	''' <summary>
	''' Gets the row that contains the specified column value. Returns 0 if value is not found.
	''' </summary>
	''' <param name="oFamily">the family that contains the table to search</param>
	''' <param name="lookupColumnName">the column name to search for the given value</param>
	''' <param name="lookupColumnValue">the value to search for</param>
	''' <returns>the row number as an integer</returns>
	Private Function GetFamilyTableRowNumber(myFamily As ContentFamily, lookupColumnName As String, lookupColumnValue As String) As Integer
		
		Dim rowNumber As Integer = 0
		
		For i As Integer = 1 To myFamily.TableRows.Count
			Try
				If InStr(myFamily.TableRows.Item(i).Item(lookupColumnName).Value, lookupColumnValue) > 0 Then
					rowNumber = i 
					Exit For
				End If	
			Catch
				'MsgBox("An error occurred. Please debug.")
			End Try			
		Next
		
		Return rowNumber
		
	End Function
	
#End Region

End Class

 

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Submit Idea