Hmm, I forgot about the Try Catch, thank you. However I still get an error: Conversion from string "iLogic" to type "Integer" is not valid. Here is the code if there's something else related to it, though I suspected it was because there was no metal to flat bend yet, since it works OK otherwise. I guess if I'd be using VB I could run it line by line to see where the issue is... I'm still working on the code and it's kind of pieced together as best I knew how.
Basically it detects if parameter "FL" exists (which is not needed for flat metal shapes, so it shouldn't be created for sheet metal parts - still need to work on that) then if iProperty "Stock Size" exists it populates that according to iProperty "Product".
Thank you for helping out!
' Code to write "Stock Size" iProperty
Sub Main()
Dim invdoc As Document
invdoc = ThisDoc.Document
'Function GetUserParams returns UserParameters Collection
oUserParams = GetUserParams(invdoc)
'Function FindParameter returns the looked for parameter
paraFL = FindParameter(invdoc, "FL")
If paraFL IsNot Nothing Then
'MessageBox.Show("Parameter does exist", "Title")
Else
'MessageBox.Show("Parameter does not exist", "Title")
paraFL = oUserParams.AddByValue("FL", 0, UnitsTypeEnum.kInchLengthUnits)
End If
Dim oPart As PartDocument = ThisDoc.Document
Dim oSS As String = StockSize(oPart)
Logger.Info(oSS)
End Sub
'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
'MessageBox.Show("Is a Assembly", "Title")
Dim docAssembly As Inventor.AssemblyDocument = invdoc
cd = docAssembly.ComponentDefinition
Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
'MessageBox.Show("Is a Part", "Title")
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
'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
'MessageBox.Show("Is a Assembly", "Title")
Dim docAssembly As Inventor.AssemblyDocument = invdoc
cd = docAssembly.ComponentDefinition
Case Is = Inventor.DocumentTypeEnum.kPartDocumentObject
'MessageBox.Show("Is a Part", "Title")
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
Private Function StockSize(part As PartDocument) As String
Dim oStock As String = getstock(part)
Dim propertyName As String = "STOCK SIZE"
Dim PropertyValue As String = oStock
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
oSSProp.Value = PropertyValue
oExists = True
End If
Next
If oExists = False Then
oSSProp = oCProps.Add(propertyName, propertyName)
End If
StockSize = oSSProp.Value
End Function
'Write stock size for each part shape
Private Function getstock(part As PartDocument) As String
Dim oFL As Double = Parameter("FL")
Select Case iProperties.Value("Inventor User Defined Properties", "Product")
Case "01 Sheet Metal"
Try
Dim smlength As Double = SheetMetal.FlatExtentsLength 'sheet metal flat pattern length
Dim smwidth as Double = SheetMetal.FlatExtentsWidth 'sheet metal flat pattern width
Dim AreaPronest As Double = (smlength + Parameter("Thickness")) * (smwidth + Parameter("Thickness")) / 144
Dim oSMthk As Double = Parameter("Thickness")
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
Catch oEx As Exception
MsgBox("Failed to get SheetMetal.FlatExtents size.", "iLogic")
End Try
Case "02 Pipe"
Dim oPipeD As Double = Parameter("G_D_Nom")
oL = FormatAsFraction(oPipeD)
getstock = "Pipe, " & oL & " sch." & Parameter("Schedule") & " x " & Format(oFL, "0.00")
Case "03 Angle"
Dim Athk As Double = Parameter("G_T")
Dim AH As Double = Parameter("G_H")
Dim AW As Double = Parameter("G_W")
getstock = "L " & Format(AW, "0.00") & " x " & Format(AH, "0.00") & " x " & Format(Athk, "0.00") & " x " & Format(oFL, "0.00")
Case Else
getstock = "not defined"
End Select
End Function