- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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
Solved! Go to Solution.