Help with iLogic (functions, write iProperties and new Parameters)

Help with iLogic (functions, write iProperties and new Parameters)

emanuel.c
Collaborator Collaborator
1,685 Views
9 Replies
Message 1 of 10

Help with iLogic (functions, write iProperties and new Parameters)

emanuel.c
Collaborator
Collaborator

I'm sorry the title is rather vague. There are probably lots of issues with the code, I kind of pasted it together with help from various sources so I'm sure it could be purified and condensed but in truth, it works mostly OK. My issues are:

 

1. If iProperty "Product" does not exist it should create it with value "xx". Unfortunately it doesn't create it, but throws the error on line 146 "Cannot find a property named "Product". It does create the other iProperties "GS Stock Number" and "STOCK SIZE" if "Product" exists.

 

2. If the first 2 digits of iProperty "Product" equals "02", "03"... etc. and Parameter FL doesn't exist it should create with value 0. But it doesn't create it. I get an error on line 603, could not find a parameter named: "FL"

 

The code points to GoExcel and so it may throw errors where it can't find those files, but hopefully someone can help out nonetheless. 

 

Thank you much!

 

'Code for creating iProperty "GS Stock Number" - which varies in length for each Product / Shape Type and "STOCK SIZE"

'digit 1 is for MATERIAL TYPE. Each content center and sheet metal template has a custome iProperty name "Product" with the specified product type already entered.
	'Otherwise and "xx" will be entered, which can then be modified.
'digits 2,3 is for product SHAPE
'digits 4,5,6 is for plate thickness or wall thickness or diam for round etc.
'digits 7,8,9 is for height dimension (not aplicable for sheet metal, round stock)
'digits 10,11,12 is for width dimension (not aplicable for sheet metals, round stock, flat bars)

Sub Main()
	Dim oPart As PartDocument = ThisDoc.Document
	Dim invdoc As Document
	invdoc = ThisDoc.Document
	Dim oSubType As String = invdoc.SubType
	
	Dim GS As String = GSNumber(oPart)
	Logger.Info(GS)
	
	Dim oSS As String = StockSize(oPart)
	Logger.Info(oSS)
	
	oUserParams = GetUserParams(invdoc) 'Function GetUserParams returns UserParameters Collection
	 
	paraFL = FindParameter(invdoc, "FL") 'Function FindParameter FL - "Final Length"
	
	Dim oDigit As String = Left(iProperties.Value("Inventor User Defined Properties", "Product"), 2) 'Get the first 2 digits of iProperty "Product"
	
'Write Parameter FL For certain Product Shapes, Not required For Sheet Metal Parts
Select Case oDigit
	Case "02", "03", "04", "05", "06", "07", "08", "09"
	If paraFL IsNot Nothing Then
		'It exists so move on
	Else			
		paraFL = oUserParams.AddByValue("FL", 0, UnitsTypeEnum.kInchLengthUnits)
	End If
End Select
	
End Sub

'Get user parameters collection
Public Function GetUserParams(invdoc As Inventor.Document) As UserParameters
	
	Dim cd As ComponentDefinition = Nothing
    Select Case invdoc.DocumentType
        Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject
            Dim docAssembly As Inventor.AssemblyDocument = invdoc
            cd = docAssembly.ComponentDefinition
        Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
            Dim docPart As Inventor.PartDocument = invdoc
            cd = docPart.ComponentDefinition
    End Select
   
   If cd IsNot Nothing Then

			Dim oParams As Parameters
			oParams = cd.Parameters
			
			Dim oUserParams As UserParameters
			oUserParams = oParams.UserParameters
            Return oUserParams
    End If
	 Return Nothing
End Function


'Create iProperty STOCK SIZE
Private Function StockSize(part As PartDocument) As String
	Dim propertyName As String = "STOCK SIZE"
	Dim PropertyValue As String = getstock(part)	
	Dim oCProps As PropertySet = part.PropertySets.Item("Inventor User Defined Properties")
	Dim oSSProp As Inventor.Property
	Dim oExists As Boolean = False
	For Each oCProp As Inventor.Property In oCProps
		If oCProp.Name = propertyName Then
			oSSProp = oCProp
			If PropertyValue = "" Then
				'Do not fill in STOCK SIZE
			Else
				oSSProp.Value = PropertyValue
			End If
			oExists = True
		End If
	Next
	If oExists = False Then
		oSSProp = oCProps.Add(PropertyValue, propertyName)
	End If

	StockSize = oSSProp.Value
End Function

'Define / Create iProperty: "Product"
Private Function Product_iProp(part As PartDocument) As String	
	Dim propertyName As String = "Product"
	Dim ProductValue As String = "xx"
	Dim oCProps As PropertySet = part.PropertySets.Item("Inventor User Defined Properties")
	Dim oProductProp As Inventor.Property	
	Dim oExists As Boolean = False
	For Each oCProp As Inventor.Property In oCProps
		If oCProp.Name = propertyName Then
			oProductProp = oCProp
			oProductProp.Value = propertyValue
			oExists = True
		End If
	Next
	If oExists = False Then
		oProductProp = oCProps.Add(ProductValue, propertyName)
	End If
End Function

'for writing the "GS Stock Number" iProperty if it doesn't exist
Private Function GSNumber(part As PartDocument) As String
	Dim mat As String = GetMaterialNumber 'used for digit 1
	Dim product As String = GetProduct(part) 'used for digits 2,3
	Dim dim_one As String = Getdim1(part) 'used for digits 4-6
	Dim dim_two As String = Getdim2(part) 'used for digits 7-9
	Dim dim_three As String = Getdim3(part) 'used for digits 10-12
	'Define iProperty: "GS Stock Number"
	Dim propertyName As String = "GS Stock Number"
	'define the value of GS Stock Number
	Dim propertyValue As String	
	If iProperties.Value("Inventor User Defined Properties", "Product") = "" Then
		propertyValue = ""
	Else
		propertyValue = mat & product & dim_one & dim_two & dim_three 'These are given by functions bellow
	End If
	
	Dim oCProps As PropertySet = part.PropertySets.Item("Inventor User Defined Properties")
	Dim oGSNumProp As Inventor.Property
	Dim oExists As Boolean = False
	For Each oCProp As Inventor.Property In oCProps
		If oCProp.Name = propertyName Then
			oGSNumProp = oCProp
			oGSNumProp.Value = propertyValue
			oExists = True
		End If
	Next
	If oExists = False Then
		oGSNumProp = oCProps.Add(propertyValue, propertyName)
	End If
	GSNumber = oGSNumProp.Value
End Function

'Get digit 1 for MATERIAL TYPE *************************************************************************************************

Private Function GetMaterialNumber() As String
	If iProperties.Value("Inventor User Defined Properties", "Product") = "" Then
		GetMaterialNumber = ""
	Else	
	'Data is read from Excel "Global Shop Stock Number.xlsx" Sheet "Material". Edit that if needed
	GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Material")
	Dim partmat As String
	Dim i As Integer
	For i = 3 To 50
		If (GoExcel.CellValue("A" & i)) = iProperties.Material
			partmat = GoExcel.CellValue("B" & i)
			Exit For
		End If
	Next
	If String.IsNullOrEmpty(partmat) = True Then
			GetMaterialNumber = "x"
'			MessageBox.Show("Part material is not defined in Excel Spreadsheet" _
'			& vbLf & "Please adjust it to include new material" _
'			& vbLf & "" _
'			& vbLf & "By default an 'x' will be entered", "Part Material Issue")
	End If
	GetMaterialNumber = partmat
End If
End Function

'Get digit 2,3 for PRODUCT TYPE ************************************************************************************************
Private Function GetProduct(part As PartDocument) As String
	
	GetProduct = Left(iProperties.Value("Inventor User Defined Properties", "Product"), 2)
			
End Function

'Get digitS 4,5,6 for PRODUCT TYPE **********************************************************************************************

Private Function Getdim1(part As PartDocument) As String
	'Get Digits 4,5,6 for SHEET METAL THICKNESS or WALL THICKNESS
	Dim dim1 As String
	Dim oProd As String = iProperties.Value("Inventor User Defined Properties", "Product")
	Select Case oProd
	
	'Get Thickness for Sheet Metal	
	Case "01 Sheet Metal", "26 Diamond Tread"
		GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Sheet Metal")
		For i = 3 To 100
			If (GoExcel.CellValue("A" & i)) = Parameter("Thickness")
				dim1 = GoExcel.CellValue("B" & i)
		Exit For
			End If
		Next		
		
		If String.IsNullOrEmpty(dim1) = True Then
			dim1 = "xxx"
		End If
	
	'Get wall thickness for pipe or DOM
	Case "02 Pipe", "02 DOM"
		GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Pipe and DOM")
		For i = 3 To 150
			If (GoExcel.CellValue("A" & i)) = Parameter("ParT")
				dim1 = GoExcel.CellValue("C" & i)
		Exit For
			End If
		Next		
		
		If String.IsNullOrEmpty(dim1) = True Then
			dim1 = "xxx"
		End If
	
	'Get wall thickness for Angles, Flat Bar, Tubes
	Case "03 Angle", "05 Tube", "05 R Tube", "05 Sq Tube"
		GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Standard Shape")
		For i = 3 To 100
			If (GoExcel.CellValue("A" & i)) = Parameter("G_T")
				dim1 = GoExcel.CellValue("B" & i)
		Exit For
			End If
		Next		
		
		If String.IsNullOrEmpty(dim1) = True Then
			dim1 = "xxx"
		End If
	
	'Get flat bar thickness
	Case "04 Flat Bar", "09 Sq Bar", "09 Hex"
		GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Standard Shape")
		For i = 3 To 100
			If (GoExcel.CellValue("A" & i)) = Parameter("G_H")
				dim1 = GoExcel.CellValue("B" & i)
		Exit For
			End If
		Next
		
		If String.IsNullOrEmpty(dim1) = True Then
			dim1 = "xxx"
		End If
		
	'Get Profile Height for Channels
	'It's 3 digits, so "00" is added before single digit sizes, "00" for double and 3 digits fills it
	Case "06 Channel"
	Dim paramGH As Integer
	paramGH = Parameter("G_H")
		If paramGH <=9 Then
			dim1 = "00" & paramGH
		Else
			If paramGH >=9 And paramGH<=100 Then
				dim1 = "0" & paramGH
			Else
				If paramGH >100 Then
					dim1=paramGH
				End If
			End If
		End If
	
	'Get Profile Height for Channel Bar Size
	Case "06 Channel Bar"
	GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Standard Shape")
		For i = 3 To 100
			If (GoExcel.CellValue("A" & i)) = Parameter("G_H")
				dim1 = GoExcel.CellValue("B" & i)
		Exit For
			End If
		Next
		
		If String.IsNullOrEmpty(dim1) = True Then
			dim1 = "xxx"
		End If
	
	'Get Profile Height for Beams
	'It's 3 digits, so "00" is added before single digit sizes, "00" for double and 3 digits fills it
	Case "07 W Beam", "07 S Beam", "07 M Beam", "07 HP Beam"
	Dim paramGH As Integer
	paramGH = Parameter("G_H_nom")
		If paramGH <=9 Then
			dim1 = "00" & paramGH
		Else
			If paramGH >=9 And paramGH <=100 Then
				dim1 = "0" & paramGH
			Else
				If paramGH >100 Then
					dim1=paramGH
				End If
			End If
		End If
						
		
	'Get Diameter for Round
	Case "08 Round"
		GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Standard Shape")
		For i = 3 To 100
			If (GoExcel.CellValue("A" & i)) = Parameter("Diam")
				dim1 = GoExcel.CellValue("B" & i)
		Exit For
			End If
		Next		
		
		If String.IsNullOrEmpty(dim1) = True Then
			dim1 = "xxx"
		End If
	
	End Select
	
	Return dim1
End Function

	'GET DIMENSION 2********************************************************************************************************
	
Private Function Getdim2(part As PartDocument) As String
	'Get Digit 7,8,9
	Dim dim2 As String
	Dim oProd As String = iProperties.Value("Inventor User Defined Properties", "Product")
		
	Select Case oProd
				
		'Get Pipe nominal diameter
		Case "02 Pipe"
			GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Pipe and DOM")
		For i = 3 To 200
			If (GoExcel.CellValue("D" & i)) = Parameter("G_D_Nom")
				dim2 = GoExcel.CellValue("E" & i)
		Exit For
			End If
		Next		
		
		If String.IsNullOrEmpty(dim2) = True Then
			dim2 = "xxx"
		End If
		
		'Get DOM diameter
		Case "02 DOM"
			GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Pipe and DOM")
		For i = 3 To 200
			If (GoExcel.CellValue("F" & i)) = Parameter("Diam")
				dim2 = GoExcel.CellValue("G" & i)
		Exit For
			End If
		Next		
		
		If String.IsNullOrEmpty(dim2) = True Then
			dim2 = "xxx"
		End If
		
		'Get Height dimension of Angles and Tubes
		Case "03 Angle", "05 Tube", "05 R Tube", "05 Sq Tube"
			GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Standard Shape")
		For i = 3 To 150
			If (GoExcel.CellValue("C" & i)) = Parameter("G_H")
				dim2 = GoExcel.CellValue("D" & i)
		Exit For
			End If
		Next		
		
		If String.IsNullOrEmpty(dim2) = True Then
			dim2 = "xxx"
		End If
		
		'Get Height dimension of Flat Bar
		Case "04 Flat Bar"
			GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Standard Shape")
		For i = 3 To 150
			If (GoExcel.CellValue("A" & i)) = Parameter("G_W")
				dim2 = GoExcel.CellValue("B" & i)
		Exit For
			End If
		Next		
		
		If String.IsNullOrEmpty(dim2) = True Then
			dim2 = "xxx"
		End If
		
		'Get Profile Mass for Channels and Beams
		'It only takes the integer part of the number
		'It's 3 digits, so "00" is added before single digit sizes, "00" for double and 3 digits fills it
		Case "06 Channel", "07 W Beam", "07 S Beam", "07 M Beam", "07 HP Beam"
		Dim paramGW As Integer
		paramGW = Parameter("G_Weight")
			If paramGW <=9 Then
				dim2 = "00" & paramGW
			Else
				If paramGW >=9 And paramGW<=100 Then
					dim2 = "0" & paramGW
				Else
					If paramGW >100 Then
						dim2=paramGW
					End If
				End If
			End If
			
		'Get Profile Width for Channel Bar Size
		Case "06 Channel Bar"
		GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Standard Shape")
			For i = 3 To 20
				
				If (GoExcel.CellValue("F" & i)) = Parameter("b")
					dim2 = GoExcel.CellValue("G" & i)
			Exit For
				End If
			Next
			
			If String.IsNullOrEmpty(dim2) = True Then
				dim2 = "xxx"
			End If
	
	End Select
	
	Return dim2
	
End Function

'Get Dimension 3 ************************************************************************************************************************

Private Function Getdim3(part As PartDocument) As String
	'Get Digit 10,11,12
	Dim dim3 As String
	Dim oProd As String = iProperties.Value("Inventor User Defined Properties", "Product")
		
	Select Case oProd
			
		'Get width dimension for Angle or Tube
		Case "03 Angle", "05 Tube", "05 R Tube", "05 Sq Tube"
		GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Standard Shape")
		For i = 3 To 150
			If (GoExcel.CellValue("C" & i)) = Parameter("G_W")
				dim3 = GoExcel.CellValue("D" & i)
		Exit For
			End If
		Next		
		
		If String.IsNullOrEmpty(dim3) = True Then
			dim3 = "xxx"
		End If
		
		'Get Profile Width for Channel Bar Size
	
		Case "06 Channel Bar"
		GoExcel.Open("M:\Autodesk Inventor\Ilogic\Global Shop Stock Number.xlsx", "Standard Shape")
			For i = 3 To 20
				
				If (GoExcel.CellValue("A" & i)) = Parameter("t1")
					dim3 = GoExcel.CellValue("B" & i)
			Exit For
				End If
			Next
			
			If String.IsNullOrEmpty(dim3) = True Then
				dim3 = "xxx"
			End If
		
	Case Else
			dim3 =""
	
	End Select
	Return dim3
	
End Function



'See if parameter FL exists
Public Function FindParameter(invdoc As Inventor.Document, strParameter As String) As Parameter

Dim cd As ComponentDefinition = Nothing
Select Case invdoc.DocumentType
    Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject
        Dim docAssembly As Inventor.AssemblyDocument = invdoc
        cd = docAssembly.ComponentDefinition
    Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
        Dim docPart As Inventor.PartDocument = invdoc
        cd = docPart.ComponentDefinition
End Select
If cd IsNot Nothing Then
    For Each param As Inventor.Parameter In cd.Parameters
        If param.Name = strParameter Then
            Return param
		End If
    Next
End If
	Return Nothing
End Function



'Write STOCK SIZE for each part shape
Private Function getstock(part As PartDocument) As String

Select Case iProperties.Value("Inventor User Defined Properties", "Product")

	Case "01 Sheet Metal", "26 Diamond Tread"
	
		Dim opart As PartDocument = ThisDoc.Document()
		Dim smCompDef As SheetMetalComponentDefinition = opart.ComponentDefinition
		Dim smlength As Double
		Dim smwidth As Double
		Dim oSMthk As Double = Parameter("Thickness")
		If smCompDef.HasFlatPattern Then			
			smlength = (smCompDef.FlatPattern.Length() + oSMthk) / 2.54 'add thickness for plasma cut kerf. Divide cm to inches
			smwidth = (smCompDef.FlatPattern.Width() + oSMthk) / 2.54
		Else
			Try
			smCompDef.Unfold()
			smlength = (smCompDef.FlatPattern.Length() + oSMthk) / 2.54
			smwidth = (smCompDef.FlatPattern.Width() + oSMthk) / 2.54
			Catch
			End Try
		End If

		Dim AreaPronest As Double = smlength * smwidth / 144
				
		If Parameter("Thickness") >= 0.1875 Then
				getstock ="Plate, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
			Else
				getstock ="Sheet Metal, " & Format(oSMthk, "0.000") & " thk x " & Format(AreaPronest, "0.00") & " ft^2"
		End If
		
	Case "27 Expanded Metal"
		Dim emstock As String = Parameter("stock")
		Dim AreaEM As Single = ((SheetMetal.FlatExtentsLength + .5 in) * (SheetMetal.FlatExtentsWidth + .5 in)) / 144
		getstock = "Expanded, " & emstock & ", " & Format(AreaEM, "0.00") & " ft^2"
						
	Case "02 Pipe"
		Dim oPipeD As Double = Parameter("G_D_Nom")
		oL = FormatAsFraction(oPipeD)
		Dim pFL As Double = Parameter("FL")
		getstock = "Pipe, " & oL & " sch." & Parameter("Schedule") & " x " & RoundToFraction(pFL, 1/16, RoundingMethod.Round)

	Case "02 DOM"
		Dim domD As Double = Parameter("Diam")
		Dim domID As Double = Parameter("ID")
		Dim dFL As Double = Parameter("FL")
		getstock = "DOM, " & Format(domD, "0.00") & " OD x " & Format(domID, "0.00") & " ID x " & RoundToFraction(dFL, 1/16, RoundingMethod.Round)
		
	Case "03 Angle"
		Dim Athk As Double = Parameter("G_T")
		Dim AW As Double = Parameter("G_W")
		Dim AH As Double = Parameter("G_H")
		Dim AFL As Double = Parameter("FL")
		'getstock = "L " & Format(AW, "0.00") & " x " & Format(AH, "0.00") & " x " & Format(Athk, "0.00") & " x " & RoundToFraction(AFL, 1/16, RoundingMethod.Round)
		getstock = "L " & RoundToFraction(AW, 1 / 16, RoundingMethod.Round) & " x " & RoundToFraction(AH, 1 / 16, RoundingMethod.Round) & " x " & RoundToFraction(Athk, 1 / 16, RoundingMethod.Round) & " x " & RoundToFraction(AFL, 1/16, RoundingMethod.Round)
	
	Case "04 Flat Bar"
		Dim FBthk As Double = Parameter("G_H")
		Dim FBwidth As Double = Parameter("G_W")
		Dim FFL As Double = Parameter("FL")
		'getstock = "Flat Bar, " & Format(FBwidth, "0.00") & " x " & Format(FBthk, "0.00") & " x " & RoundToFraction(FFL, 1/16, RoundingMethod.Round)
		getstock = "Flat Bar, " & RoundToFraction(FBwidth, 1/16, RoundingMethod.Round) & " x " & RoundToFraction(FBthk, 1/16, RoundingMethod.Round) & " x " & RoundToFraction(FFL, 1/16, RoundingMethod.Round)
		
		
	Case "05 R Tube"
		Dim RTthk As Double = Parameter("G_T")
		Dim RTH As Double = Parameter("G_H")
		Dim RTW As Double = Parameter("G_W")
		Dim RFL As Double = Parameter("FL")
		If RTH < 2 Then
			getstock = "R Tube " & Format(RTH, "0.00") & " x " & Format(RTW, "0.00") & " x " & Format(RTthk, "0.00") & " x " & RoundToFraction(RFL, 1 / 16, RoundingMethod.Round)
		Else
			getstock = "R Tube " & RoundToFraction(RTH, 1 / 16, RoundingMethod.Round) & " x " & RoundToFraction(RTW, 1 / 16, RoundingMethod.Round) & " x " & RoundToFraction(RTthk, 1 / 16, RoundingMethod.Round) & " x " & RoundToFraction(RFL, 1 / 16, RoundingMethod.Round)
		End If
		
	Case "05 Sq Tube"
		Dim STthk As Double = Parameter("G_T")
		Dim STH As Double = Parameter("G_H")
		Dim STW As Double = Parameter("G_W")
		Dim SFL As Double = Parameter("FL")
		If STH < 2 Then
			getstock = "Sq Tube " & Format(STH, "0.00") & " x " & Format(STW, "0.00") & " x " & Format(STthk, "0.00") & " x " & RoundToFraction(SFL, 1 / 16, RoundingMethod.Round)
		Else
			getstock = "Sq Tube " & RoundToFraction(STH, 1/16, RoundingMethod.Round) & " x " & RoundToFraction(STW, 1/16, RoundingMethod.Round) & " x " & RoundToFraction(STthk, 1/16, RoundingMethod.Round) & " x " & RoundToFraction(SFL, 1/16, RoundingMethod.Round)
		End If
			
		
	Case "06 Channel"
		Dim CFL As Double = Parameter("FL")
		getstock = "C " & Parameter("G_H") & "x" & Parameter("G_Weight") & " x " & RoundToFraction(CFL, 1/16, RoundingMethod.Round)
		
	Case "06 Channel Bar"
		Dim CBH As Double = Parameter("G_H")
		Dim CBb As Double = Parameter("b")
		Dim CBt As Double = Parameter("t1")
		Dim cFL As Double = Parameter("FL")
		getstock = "C Bar " & Format(CBH, "0.00") & " x " & Format(CBb, "0.00") & " x " & Format(CBt, "0.00") & " x " & RoundToFraction(cFL, 1/16, RoundingMethod.Round)

	Case "07 W Beam"
		Dim WFL As Double = Parameter("FL")
		getstock = "W " & Parameter("G_H_nom") & "x" & Parameter("G_Weight") & " x " & RoundToFraction(WFL, 1/16, RoundingMethod.Round)
	
	Case "07 S Beam"	
		Dim SFL As Double = Parameter("FL")
		getstock = "S " & Parameter("G_H_nom") & "x" & Parameter("G_Weight") & " x " & RoundToFraction(SFL, 1/16, RoundingMethod.Round)
		
	Case "07 M Beam"
		Dim MFL As Double = Parameter("FL")
		getstock = "M " & Parameter("G_H_nom") & "x" & Parameter("G_Weight") & " x " & RoundToFraction(MFL, 1/16, RoundingMethod.Round)
		
	Case "07 HP Beam"
		Dim HPFL As Double = Parameter("FL")
		getstock = "HP " & Parameter("G_H_nom") & "x" & Parameter("G_Weight") & " x " & RoundToFraction(HPFL, 1/16, RoundingMethod.Round)
		
	Case "08 Round"
		Dim RDiam As Double = Parameter("Diam")
		Dim RFL As Double = Parameter("FL")
		getstock = "Round, " & Format(RDiam, "0.00") & " D x " & RoundToFraction(RFL, 1/16, RoundingMethod.Round)
	
	Case "09 Sq Bar"
		Dim HW As Double = Parameter("G_H")
		Dim sFL As Double = Parameter("FL")
		getstock = "Sq Bar, " & Format(HW, "0.00") & " x " & Format(HW, "0.00") & " x " & RoundToFraction(sFL, 1/16, RoundingMethod.Round)	
	
	Case "09 Hex"
		Dim HW As Double = Parameter("G_H")
		Dim hFL As Double = Parameter("FL")
		getstock = "Hex Bar, " & Format(HW, "0.00") & " x " & RoundToFraction(hFL, 1/16, RoundingMethod.Round)

	Case Else
		getstock = ""
	
	End Select

End Function

 

 

0 Likes
Accepted solutions (2)
1,686 Views
9 Replies
Replies (9)
Message 2 of 10

WCrihfield
Mentor
Mentor

Hi @emanuel.c.  Your function for creating that custom iProperty looks OK to me.  But after some review, I believe the problem is that you are actually attempting to get the value of that iProperty earlier in the timeline flow of your code than that function is being used to ensure its existence.

Edit:  In fact, I don't see anywhere within the rest of your code where you are calling that function (Product_iProp) to run at all.

Edit 2:  Also, you have that set-up as a Function, that is supposed to return a String, but you don't have any 'Return' lines within that Function to return a String.  So, if you don't really need to return anything from that routine, you could just switch it to a Sub, then get rid of the 'As String' at the end of its definition line.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 10

emanuel.c
Collaborator
Collaborator

I know, I'm attempting to get the "Product" value in various functions later on, but I can't figure out why it's not creating it when it doesn't exist... So I got rid of most of the functions to simplify things, if you would take a look at it now. And it still doesn't create the iProperty "Product" even though it's not referenced later on. Thank you!

 

Sub Main()
	Dim oPart As PartDocument = ThisDoc.Document
	Dim GS As String = GSNumber(oPart)
	Logger.Info(GS)
End Sub

'Define / Create iProperty: "Product"
Private Function Product_iProp(part As PartDocument) As String	
	Dim propertyName As String = "Product"
	Dim ProductValue As String = "xx"
	Dim oCProps As PropertySet = part.PropertySets.Item("Inventor User Defined Properties")
	Dim oProductProp As Inventor.Property	
	Dim oExists As Boolean = False
	For Each oCProp As Inventor.Property In oCProps
		If oCProp.Name = propertyName Then
			oProductProp = oCProp
			oProductProp.Value = propertyValue
			oExists = True
		End If
	Next
	If oExists = False Then
		oProductProp = oCProps.Add(ProductValue, propertyName)
	End If
End Function

'for writing the "GS Stock Number" iProperty if it doesn't exist
Private Function GSNumber(part As PartDocument) As String
	Dim mat As String = "00" 'used for digit 1
	Dim product As String = "22" 'used for digits 2,3
	Dim dim_one As String = "333" 'used for digits 4-6
	Dim dim_two As String = "444" 'used for digits 7-9
	Dim dim_three As String = "555" 'used for digits 10-12
	'Define iProperty: "GS Stock Number"
	Dim propertyName As String = "GS Stock Number"
	'define the value of GS Stock Number
	Dim propertyValue As String	
	If iProperties.Value("Inventor User Defined Properties", "Product") = "" Then
		propertyValue = ""
	Else
		propertyValue = mat & product & dim_one & dim_two & dim_three 'These are given by functions bellow
	End If
	
	Dim oCProps As PropertySet = part.PropertySets.Item("Inventor User Defined Properties")
	Dim oGSNumProp As Inventor.Property
	Dim oExists As Boolean = False
	For Each oCProp As Inventor.Property In oCProps
		If oCProp.Name = propertyName Then
			oGSNumProp = oCProp
			oGSNumProp.Value = propertyValue
			oExists = True
		End If
	Next
	If oExists = False Then
		oGSNumProp = oCProps.Add(propertyValue, propertyName)
	End If
	GSNumber = oGSNumProp.Value
End Function

 

 

0 Likes
Message 4 of 10

jjstr8
Collaborator
Collaborator

I think what @WCrihfield initially referred to is the first line in your GetMaterialNumber function.  Should it really be the "Product" custom iProperty or actually something else?

0 Likes
Message 5 of 10

emanuel.c
Collaborator
Collaborator

I think that works OK. Basically if "Product" is empty then GetMaterialNumber is left empty too. But I got rid of all the functions to weed out the error and it still doesn't write the new iproperty "Product".

0 Likes
Message 6 of 10

WCrihfield
Mentor
Mentor
Accepted solution

Hi @emanuel.c.  That code you just posted, even though it 'contains' the function called "Product_iProp", you are not calling that function to run anywhere within the rest of you code.  In order for that function to work (do something), you need to call it to run somewhere either within the Sub Main area, or within the GSNumber function code area.  That iLogic only snippet 'iProperties.Value()' will only create an iProperty automatically for you if you are directly setting an initial value to it.  So, when you are using that iProperties.Value line within your GSNumber function, it is trying to get the value from an iProperty which does not exist yet, and that line will not automatically create it for you, because it is checking its 'existing' value, instead of setting a value to it.

 

I modified your short version code here to show you the changes I believe you need.  I changed the "Product_iProp" function to a Sub, because we were not making use of the String it was supposed to return, and I don't know what String you were intending to return from it (maybe its Value?).  Then I included a line within your Sub Main area to call that Sub to run, before your GSNumber function runs, so that it will have already created the needed iProperty.

Sub Main()
	Dim oPart As PartDocument = ThisDoc.Document
	'this next line is going to 'run' or 'call' the custom Sub below to run
	Product_iProp(oPart)
	'now that iProperty should exist when calling the other Subs/Functions to run later
	
	'much the same way that this next line is causing your 'GSNumber' function to run
	Dim GS As String = GSNumber(oPart)
	Logger.Info(GS)
End Sub

'Define / Create iProperty: "Product"
Private Sub Product_iProp(part As PartDocument)
	Dim propertyName As String = "Product"
	Dim ProductValue As String = "xx"
	Dim oCProps As PropertySet = part.PropertySets.Item("Inventor User Defined Properties")
	Dim oProductProp As Inventor.Property	
	Dim oExists As Boolean = False
	For Each oCProp As Inventor.Property In oCProps
		If oCProp.Name = propertyName Then
			oProductProp = oCProp
			oProductProp.Value = propertyValue
			oExists = True
		End If
	Next
	If oExists = False Then
		oProductProp = oCProps.Add(ProductValue, propertyName)
	End If
End Sub

'for writing the "GS Stock Number" iProperty if it doesn't exist
Private Function GSNumber(part As PartDocument) As String
	Dim mat As String = "00" 'used for digit 1
	Dim product As String = "22" 'used for digits 2,3
	Dim dim_one As String = "333" 'used for digits 4-6
	Dim dim_two As String = "444" 'used for digits 7-9
	Dim dim_three As String = "555" 'used for digits 10-12
	'Define iProperty: "GS Stock Number"
	Dim propertyName As String = "GS Stock Number"
	'define the value of GS Stock Number
	Dim propertyValue As String	
	If iProperties.Value("Inventor User Defined Properties", "Product") = "" Then
		propertyValue = ""
	Else
		propertyValue = mat & product & dim_one & dim_two & dim_three 'These are given by functions bellow
	End If
	
	Dim oCProps As PropertySet = part.PropertySets.Item("Inventor User Defined Properties")
	Dim oGSNumProp As Inventor.Property
	Dim oExists As Boolean = False
	For Each oCProp As Inventor.Property In oCProps
		If oCProp.Name = propertyName Then
			oGSNumProp = oCProp
			oGSNumProp.Value = propertyValue
			oExists = True
		End If
	Next
	If oExists = False Then
		oGSNumProp = oCProps.Add(propertyValue, propertyName)
	End If
	GSNumber = oGSNumProp.Value
End Function

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 7 of 10

emanuel.c
Collaborator
Collaborator

Yes, thanks so much for running through it. I was not seeing it but that part works well now.

Do you see where the issue is on number 2?

0 Likes
Message 8 of 10

WCrihfield
Mentor
Mentor

I wasn't 100% sure what you meant by 'number 2' there, but I assumed you meant the second main problem mentioned at the top of this post, where you were having trouble finding the parameter named "FL".  I was going to ask what version of Inventor you were using, but I looked at a couple of your other posts and saw a screen captured image you posted which included a ModelStates folder in the model browser tree, which answered my question (you are using 2022).  So, knowing that, I suspected that there may be model states involved, and so we might have to take that into account in various places within this code.  So, to start with, I changed your first line of code, which defines the part document.  I changed it from using 'ThisDoc.Document' to using 'ThisDoc.FactoryDocument'.  Then I focused on your function called 'FindParameter', to get it updated a bit with this in mind.  To show the resulting code in a usable way, I created a new rule with just those two parts in it, so you can test their functionality, before adapting them into your main larger code.

Sub Main
	Dim oPart As PartDocument = ThisDoc.FactoryDocument
	Dim paraFL As Inventor.Parameter = FindParameter(oPart, "FL") 'FL - "Final Length"
	If paraFL Is Nothing Then
		MsgBox("Could not find the parameter named 'FL'.", vbExclamation, "")
		'Exit Sub
	Else
		MsgBox("The current Value of the parameter named 'FL' is:  " & paraFL.Expression, , "")
	End If
End Sub

'See if parameter FL exists
Function FindParameter(invdoc As Inventor.Document, strParameter As String) As Inventor.Parameter
	Dim oParams As Parameters
	If invdoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
		invdoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		If invdoc.ComponentDefinition.IsModelStateMember Then
			invdoc = invdoc.ComponentDefinition.FactoryDocument
		End If
		oParams = invdoc.ComponentDefinition.Parameters
	Else
		Return Nothing
	End If
    For Each oParam As Inventor.Parameter In oParams
        If oParam.Name = strParameter Then
            Return oParam
		End If
    Next
	Return Nothing
End Function

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 10

emanuel.c
Collaborator
Collaborator

Thank you for helping. Yup I'm on the latest 2022 version of Inventor 🙂 So the find parameter function works well, but unfortunately the FL parameter is still not created if it doesn't find it. (I'd like for it to be automatically added, even if I have to later modify it's value, to save me the little time of creating the new parameter, which can add up if I have many parts).

I'm not sure where it's calling it, or why the line under the first Else doesn't write the "FL", 0 parameter. Please see below:

 

Sub Main
	Dim oPart As PartDocument = ThisDoc.FactoryDocument
	Dim paraFL As Inventor.Parameter = FindParameter(oPart, "FL") 'FL - "Final Length"
	If paraFL Is Nothing Then
		MsgBox("Could not find the parameter named 'FL'.", vbExclamation, "")
		'Exit Sub
	Else
		MsgBox("The current Value of the parameter named 'FL' is:  " & paraFL.Expression, , "")
		oUserParams = GetUserParams(invdoc)
		paraFL = oUserParams.AddByValue("FL", 0, UnitsTypeEnum.kInchLengthUnits)
	End If
End Sub

Public Function GetUserParams(invdoc As Inventor.Document) As UserParameters
	
	Dim cd As ComponentDefinition = Nothing
    Select Case invdoc.DocumentType
        Case Is = Inventor.DocumentTypeEnum.kAssemblyDocumentObject
            Dim docAssembly As Inventor.AssemblyDocument = invdoc
            cd = docAssembly.ComponentDefinition
        Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
            Dim docPart As Inventor.PartDocument = invdoc
            cd = docPart.ComponentDefinition
    End Select
   
   If cd IsNot Nothing Then

			Dim oParams As Parameters
			oParams = cd.Parameters
			
			Dim oUserParams As UserParameters
			oUserParams = oParams.UserParameters
            Return oUserParams
    End If
	 Return Nothing
End Function

'See if parameter FL exists
Function FindParameter(invdoc As Inventor.Document, strParameter As String) As Inventor.Parameter
	Dim oParams As Parameters
	If invdoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Or _
		invdoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then
		If invdoc.ComponentDefinition.IsModelStateMember Then
			invdoc = invdoc.ComponentDefinition.FactoryDocument
		End If
		oParams = invdoc.ComponentDefinition.Parameters
	Else
		Return Nothing
	End If
    For Each oParam As Inventor.Parameter In oParams
        If oParam.Name = strParameter Then
            Return oParam
		End If
    Next
	Return Nothing
End Function

 

0 Likes
Message 10 of 10

emanuel.c
Collaborator
Collaborator
Accepted solution

Well, I'm not sure where my mistake was. Maybe I was calling GSNumber and StockSize before the paraFL? Not sure... but I edited the first Sub Main as such and it works perfectly.

 

Thank you Wesley for your help!

 

Sub Main()
	Dim oPart As PartDocument = ThisDoc.FactoryDocument
	Product_iProp(oPart) 'calling the Product_iProp function
	Dim invdoc As Document = ThisDoc.FactoryDocument
	Dim oSubType As String = invdoc.SubType
	oUserParams = GetUserParams(invdoc) 'Function GetUserParams returns UserParameters Collection
	paraFL = FindParameter(invdoc, "FL") 'Function FindParameter returns the looked for parameter: FL - "Final Length"
	
	'Get the first 2 digits of iProperty "Product"
	Dim oDigit As String = Left(iProperties.Value("Inventor User Defined Properties", "Product"), 2)
	
	'Write parameter FL for certain Product Shapes, not required for all parts
	Select Case oDigit
		Case "02", "03", "04", "05", "06", "07", "08", "09"
		If paraFL Is Nothing Then
			paraFL = oUserParams.AddByValue("FL", 0, UnitsTypeEnum.kInchLengthUnits)
		Else			
			'It already exists
		End If
	End Select
	
	Dim GS As String = GSNumber(oPart)
	Logger.Info(GS)	
	Dim oSS As String = StockSize(oPart)
	Logger.Info(oSS)
		
End Sub

 

0 Likes