Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
emanuel.c
in reply to: Michael.Navara

Thank you Michael, I didn't know it had the API.

 

I still get errors, only when there isn't a flat pattern available and can't figure it out... Unspecified Error (Exception from HRESULT: 0x80004005 (E_FAIL)).

 

' 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
            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
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" Dim opart As PartDocument = ThisDoc.Document Dim smCompDef As SheetMetalComponentDefinition = opart.ComponentDefinition 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 smCompDef.HasFlatPattern Then 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 End If 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")

End Select
End Function