Need help with debugging a Ilogic code

Need help with debugging a Ilogic code

khantheking1995
Contributor Contributor
552 Views
5 Replies
Message 1 of 6

Need help with debugging a Ilogic code

khantheking1995
Contributor
Contributor

Hi,

 

I'm using Inventor professional 2023, I have a ilogic which does the following function.

It helps me to select a part from an assembly (Its basically a assembly made from content center structural parts), so when i slect a part using the Iloic code 1 it assign weight data and pulls size data from content center and also it create a custom user paramater and assign the list of values available in the size column and mass columns to its respective parameter. then with ilogic code 2 when I select a specific size from dropdown in the form. it will calculate the new weight for the new size. see below images

Form 

khantheking1995_0-1697380210953.png

Created user paramters with dropdown

khantheking1995_1-1697380254245.png

 

Ilogic code 1:Partselect

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oAsmDoc As AssemblyDocument = oInvApp.ActiveDocument
	Dim oContentCenter As ContentCenter = oInvApp.ContentCenter
	Dim oUsParams As UserParameters = oAsmDoc.ComponentDefinition.Parameters.UserParameters
	
	Dim oUOfM As UnitsOfMeasure = oAsmDoc.UnitsOfMeasure
	Dim oCustom As PropertySet = oAsmDoc.PropertySets("Inventor User Defined Properties")
	Dim oDesignProps As PropertySet = oAsmDoc.PropertySets("Design Tracking Properties")
	
	Dim oWeigt_KG, oWeigt_Lbs, oStockNumb As Inventor.Property
	Try : oWeigt_KG = oCustom("Existing size Weight Kg")
	Catch : oWeigt_KG = oCustom.Add("", "Existing size Weight Kg") : End Try
	Try : oWeigt_Lbs = oCustom("Existing Part Weight Lbs")
	Catch : oWeigt_Lbs = oCustom.Add("", "Existing Part Weight Lbs") : End Try
	Try : oCustom("CAM-Weight Kg").Value = ""
	Catch : oCustom.Add("", "CAM-Weight Kg") : End Try
	Try : oCustom("CAM-Weight Lbs").Value = ""
	Catch : oCustom.Add("", "CAM-Weight Lbs") : End Try
	oStockNumb = oDesignProps("Stock Number")
	oWeigt_KG.Value = ""
	oWeigt_Lbs.Value = ""
	oStockNumb.Value = ""
	
	Dim oOcc As ComponentOccurrence
	oOcc = oInvApp.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, _
										"Select component...")
	If oOcc Is Nothing Then Exit Sub
		
	Dim oDef As PartComponentDefinition = oOcc.Definition
	Dim oCCPropSet As PropertySet
	Try : oCCPropSet = oDef.Document.PropertySets.Item("Content Library Component Properties")
	Catch : MessageBox.Show("The Part is not a CC part.") : End Try
	
	Dim dMass As Double = oOcc.MassProperties.Mass
	oWeigt_KG.Value = Round(dMass, 2)
	oWeigt_Lbs.Value = Round(oUOfM.ConvertUnits(dMass, UnitsTypeEnum.kKilogramMassUnits, _
												UnitsTypeEnum.kLbMassMassUnits), 2)
	oStockNumb.Value = oDef.Document.PropertySets("Design Tracking Properties") _
												("Stock Number").Value	
	
	Dim familyId As Inventor.Property = oCCPropSet.Item("FamilyId")
	Dim MemberId As String = oCCPropSet.Item("MemberId").Value
	Dim oFamily As ContentFamily
	oFamily = oContentCenter.GetContentObject("v3#" & familyId.Value & "#")
	Dim oContentNode As ContentTreeViewNode = oContentCenter.TreeViewTopNode
	Dim oLengthM As UserParameter
	Dim oRow As ContentTableRow = oFamily.TableRows.Item(MemberId)
	Dim iCount As Integer = oFamily.TableRows.Count - 1
    Dim iStockNumb As Integer = GetNumbColomn(oFamily.TableColumns, {"Stock Number" })
    If iStockNumb = -1 Then
        MessageBox.Show("Failed to get column - Stock Number", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If

    Dim iWeight As Integer = GetNumbColomn(oFamily.TableColumns, {"Specific mass", "Mass","Mass [Kg/mm]"})
    If iWeight = -1 Then
        MessageBox.Show("Failed to get column - Mass", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
	Dim sStockNumb(iCount), sWeight(iCount) As String
	For i As Integer = 0 To iCount
		sStockNumb(i) = """" & oFamily.TableRows(i + 1)(iStockNumb).Value & """"
		sWeight(i) = """" & oFamily.TableRows(i + 1)(iWeight).Value & """"
	Next i
	Dim oColumn As ContentTableColumn = oFamily.TableColumns("STOCKNUMBER")
	Call SetParamList(oUsParams, "StockNumbBeamList", sStockNumb)
	Call SetParamList(oUsParams, "WeightKg", sWeight)
	Try : oLengthM = oUsParams("LengthM") : Catch
	oLengthM = oUsParams.AddByValue("LengthM", Nothing, _
										UnitsTypeEnum.kMeterLengthUnits) : End Try
	oLengthM.Value = oDef.Parameters.UserParameters("B_L").Value
End Sub

Private Function SetParamList(ByVal oUsParams As UserParameters,
								ByVal sNameParam As String,
								ByVal sList() As String)
	Dim oUsParam As UserParameter
	Try : oUsParam = oUsParams(sNameParam)
	Catch
		oUsParam = oUsParams.AddByValue(sNameParam, "", _
										UnitsTypeEnum.kTextUnits)
	End Try
	Call oUsParam.ExpressionList.ClearAll()
	oExpList = oUsParam.ExpressionList
	Call oExpList.SetExpressionList(sList, True, 1)
	If oExpList.AllowCustomValues Then oExpList.AllowCustomValues = False
	If oExpList.CustomOrder Then oExpList.CustomOrder = False
End Function

Private Function GetNumbColomn(ByVal oColomn As ContentTableColumns, ByVal sName() As String) As Integer
    For i As Integer = 1 To oColomn.Count
        If sName.Any(Function(name) oColomn(i).DisplayHeading.ToLower().Contains(name.ToLower())) Then
            Return i
        End If
    Next i
    Return -1
End Function

Ilogic code 2: ChangeBeam

Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oUOfM As UnitsOfMeasure = oDoc.UnitsOfMeasure
Dim oCustom As PropertySet = oDoc.PropertySets("Inventor User Defined Properties")
Dim oUsParams As UserParameters = oDoc.ComponentDefinition.Parameters.UserParameters
Dim oListBeam, oWeightList, oLengthM As UserParameter
Try 
	oListBeam = oUsParams("StockNumbBeamList")
	oWeightList = oUsParams("WeightKg")
	oLengthM = oUsParams("LengthM")
Catch : Exit Sub : End Try
If String.IsNullOrEmpty(oListBeam.Value) Then Exit Sub
Dim oWeigt_KG, oWeigt_Lbs, oStockNumb As Inventor.Property
Try : oWeigt_KG = oCustom("CAM-Weight Kg")
Catch : oWeigt_KG = oCustom.Add("", "CAM-Weight Kg") : End Try
Try : oWeigt_Lbs = oCustom("CAM-Weight Lbs")
Catch : oWeigt_Lbs = oCustom.Add("", "CAM-Weight Lbs") : End Try
For i As Integer = 1 To oListBeam.ExpressionList.Count
	If oListBeam.ExpressionList(i) = oListBeam.Expression Then
		Dim dLengthM As Double
		dLengthM = oUOfM.ConvertUnits(oLengthM.Value, kCentimeterLengthUnits, kMeterLengthUnits)
		Dim sWeight As String = oWeightList.ExpressionList(i)
		Dim dKG As Double = sWeight.Substring(1, sWeight.Length-2) * dLengthM
		oWeigt_KG.Value = Round(dKG, 2)
		oWeigt_Lbs.Value = Round(oUOfM.ConvertUnits(dKG, UnitsTypeEnum.kKilogramMassUnits, _
												UnitsTypeEnum.kLbMassMassUnits), 2)
	End If
Next

but I have a problem here , the mass data is reordered in ascending from 1 to  9 but I want to assign the data in the same way as it is in content center so that the mass will be assigned correctly for its respective size.

Image 1 :order in content center

khantheking1995_2-1697380545778.png

 

Image 2:order in my dropdown paramter

khantheking1995_3-1697380607554.png

It reshuffle   in increase from 1 to 9 , i want the same order as in content center, kindly help me
Also In the form, I want to add the total weight of assembly also come in the form and also it show show new total weight because of the change of new size . Refer image below

khantheking1995_4-1697380923196.png

 

@Andrii_Humeniuk , hope you can help me on this

Accepted solutions (2)
553 Views
5 Replies
Replies (5)
Message 2 of 6

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

Hi @khantheking1995 . Sorry. I haven't answered you before, I've been very busy. I hope the following changes to the rules will fix your problems:

Rule PartSelect:

 

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oAsmDoc As AssemblyDocument = oInvApp.ActiveDocument
	Dim oContentCenter As ContentCenter = oInvApp.ContentCenter
	Dim oUsParams As UserParameters = oAsmDoc.ComponentDefinition.Parameters.UserParameters
	
	Dim oUOfM As UnitsOfMeasure = oAsmDoc.UnitsOfMeasure
	Dim oCustom As PropertySet = oAsmDoc.PropertySets("Inventor User Defined Properties")
	Dim oDesignProps As PropertySet = oAsmDoc.PropertySets("Design Tracking Properties")
	
	Dim oWeigt_KG, oWeigt_Lbs, oStockNumb As Inventor.Property
	Try : oWeigt_KG = oCustom("Existing size Weight Kg")
	Catch : oWeigt_KG = oCustom.Add("", "Existing size Weight Kg") : End Try
	Try : oWeigt_Lbs = oCustom("Existing Part Weight Lbs")
	Catch : oWeigt_Lbs = oCustom.Add("", "Existing Part Weight Lbs") : End Try
	Try : oCustom("CAM-Weight Kg").Value = ""
	Catch : oCustom.Add("", "CAM-Weight Kg") : End Try
	Try : oCustom("CAM-Weight Lbs").Value = ""
	Catch : oCustom.Add("", "CAM-Weight Lbs") : End Try
	oStockNumb = oDesignProps("Stock Number")
	oWeigt_KG.Value = ""
	oWeigt_Lbs.Value = ""
	oStockNumb.Value = ""
	
	Dim oOcc As ComponentOccurrence
	oOcc = oInvApp.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, _
										"Select component...")
	If oOcc Is Nothing Then Exit Sub
		
	Dim oDef As PartComponentDefinition = oOcc.Definition
	Dim oCCPropSet As PropertySet
	Try : oCCPropSet = oDef.Document.PropertySets.Item("Content Library Component Properties")
	Catch : MessageBox.Show("The Part is not a CC part.") : End Try
	
	Dim dMass As Double = oOcc.MassProperties.Mass
	oWeigt_KG.Value = Round(dMass, 2)
	oWeigt_Lbs.Value = Round(oUOfM.ConvertUnits(dMass, UnitsTypeEnum.kKilogramMassUnits, _
												UnitsTypeEnum.kLbMassMassUnits), 2)
	oStockNumb.Value = oDef.Document.PropertySets("Design Tracking Properties") _
												("Stock Number").Value	
	
	Dim familyId As Inventor.Property = oCCPropSet.Item("FamilyId")
	Dim MemberId As String = oCCPropSet.Item("MemberId").Value
	Dim oFamily As ContentFamily
	oFamily = oContentCenter.GetContentObject("v3#" & familyId.Value & "#")
	Dim oContentNode As ContentTreeViewNode = oContentCenter.TreeViewTopNode
	Dim oLengthM As UserParameter
	Dim oRow As ContentTableRow = oFamily.TableRows.Item(MemberId)
	Dim iCount As Integer = oFamily.TableRows.Count - 1
    Dim iStockNumb As Integer = GetNumbColomn(oFamily.TableColumns, {"Stock Number" })
    If iStockNumb = -1 Then
        MessageBox.Show("Failed to get column - Stock Number", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If

    Dim iWeight As Integer = GetNumbColomn(oFamily.TableColumns, {"Specific mass", "Mass","Mass [Kg/mm]"})
    If iWeight = -1 Then
        MessageBox.Show("Failed to get column - Mass", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
	Dim sStockNumb(iCount), sWeight(iCount) As String
	For i As Integer = 0 To iCount
		sStockNumb(i) = """" & oFamily.TableRows(i + 1)(iStockNumb).Value & """"
		sWeight(i) = """" & oFamily.TableRows(i + 1)(iWeight).Value & """"
	Next i
	Dim oColumn As ContentTableColumn = oFamily.TableColumns("STOCKNUMBER")
	Call SetParamList(oUsParams, "StockNumbBeamList", sStockNumb)
	Call SetParamList(oUsParams, "WeightKg", sWeight)
	Try : oLengthM = oUsParams("LengthM") : Catch
	oLengthM = oUsParams.AddByValue("LengthM", Nothing, _
										UnitsTypeEnum.kMeterLengthUnits) : End Try
	oLengthM.Value = oDef.Parameters.UserParameters("B_L").Value
End Sub

Private Function SetParamList(ByVal oUsParams As UserParameters,
								ByVal sNameParam As String,
								ByVal sList() As String)
	Dim oUsParam As UserParameter
	Try : oUsParam = oUsParams(sNameParam)
	Catch
		oUsParam = oUsParams.AddByValue(sNameParam, "", _
										UnitsTypeEnum.kTextUnits)
	End Try
	Call oUsParam.ExpressionList.ClearAll()
	oExpList = oUsParam.ExpressionList
	Call oExpList.SetExpressionList(sList, True, 1)
	If oExpList.AllowCustomValues Then oExpList.AllowCustomValues = False
	If Not oExpList.CustomOrder Then oExpList.CustomOrder = True
End Function

Private Function GetNumbColomn(ByVal oColomn As ContentTableColumns, ByVal sName() As String) As Integer
    For i As Integer = 1 To oColomn.Count
        If sName.Any(Function(name) oColomn(i).DisplayHeading.ToLower().Contains(name.ToLower())) Then
            Return i
        End If
    Next i
    Return -1
End Function

 

Rule ChangeBeam:

 

Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oUOfM As UnitsOfMeasure = oDoc.UnitsOfMeasure
Dim oCustom As PropertySet = oDoc.PropertySets("Inventor User Defined Properties")
Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim dMassN As Double = oDef.MassProperties.Mass
dMass = Round(oUOfM.ConvertUnits(dMassN, UnitsTypeEnum.kKilogramMassUnits, _
								UnitsTypeEnum.kLbMassMassUnits), 2)
Dim oUsParams As UserParameters = oDef.Parameters.UserParameters
Dim oListBeam, oWeightList, oLengthM As UserParameter
Try 
	oListBeam = oUsParams("StockNumbBeamList")
	oWeightList = oUsParams("WeightKg")
	oLengthM = oUsParams("LengthM")
Catch : Exit Sub : End Try
If String.IsNullOrEmpty(oListBeam.Value) Then Exit Sub
Dim oWeigt_KG, oWeigt_Lbs, oNewAsm_KG, oStockNumb As Inventor.Property
Try : oCustom("Existing Assembly weight(Kg)").Value = dMass
Catch : oCustom.Add(dMass, "Existing Assembly weight(Kg)") : End Try
Try : oNewAsm_KG = oCustom("New Assembly weight(Kg)")
Catch : oNewAsm_KG = oCustom.Add("", "New Assembly weight(Kg)") : End Try
Try : oExWeigt_KG = oCustom("Existing size Weight Kg")
Catch : oExWeigt_KG = oCustom.Add("", "Existing size Weight Kg") : End Try
Try : oWeigt_KG = oCustom("CAM-Weight Kg")
Catch : oWeigt_KG = oCustom.Add("", "CAM-Weight Kg") : End Try
Try : oWeigt_Lbs = oCustom("CAM-Weight Lbs")
Catch : oWeigt_Lbs = oCustom.Add("", "CAM-Weight Lbs") : End Try
For i As Integer = 1 To oListBeam.ExpressionList.Count
	If oListBeam.ExpressionList(i) = oListBeam.Expression Then
		Dim dLengthM As Double
		dLengthM = oUOfM.ConvertUnits(oLengthM.Value, kCentimeterLengthUnits, kMeterLengthUnits)
		Dim sWeight As String = oWeightList.ExpressionList(i)
		Dim dKG As Double = sWeight.Substring(1, sWeight.Length-2) * dLengthM
		oWeigt_KG.Value = Round(dKG, 2)
		oWeigt_Lbs.Value = Round(oUOfM.ConvertUnits(dKG, UnitsTypeEnum.kKilogramMassUnits, _
												UnitsTypeEnum.kLbMassMassUnits), 2)
		oNewAsm_KG.Value = Round(dMassN - oExWeigt_KG.Value + dKG, 2)
	End If
Next

 

You will have new custom properties added (Existing Assembly weight(Kg), New Assembly weight(Kg)).

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 3 of 6

khantheking1995
Contributor
Contributor

Hi, 
Require a small update in the above code as follows.

Once it identifies the mass column, I want the code to get the name/description o the mass column and assign it to a variable, for example, X is the variable then (as [per image)

X = mASS (Kg/m) [Kg/mm] 

khantheking1995_0-1697438999239.png

Then from the variable it should extract the unit of mass whatever mentioned between [], here it is mentioned Kg/mm between [] then it should convert the value to equivalent kg/m.

The unit can be anything that represent Mass/Length (kg/cm, kg/mm, kg/m, lb/ft, lbf/ft, lb/in,lbf/in) to kg/m

 

Also, I have save the recent code but it didnt showed up the total existing assembly weight and new assembly weight in my form like below image, is there any setting I have to change in my form?

khantheking1995_2-1697439972354.png

 

@Andrii_Humeniuk 

0 Likes
Message 4 of 6

Andrii_Humeniuk
Advisor
Advisor
Accepted solution

There is a small inaccuracy associated with the rounding of masses.

FormMass.png

Rule PartSelect:

Sub main
	Dim oInvApp As Inventor.Application = ThisApplication
	Dim oAsmDoc As AssemblyDocument = oInvApp.ActiveDocument
	Dim oContentCenter As ContentCenter = oInvApp.ContentCenter
	Dim oUsParams As UserParameters = oAsmDoc.ComponentDefinition.Parameters.UserParameters
	
	Dim oUOfM As UnitsOfMeasure = oAsmDoc.UnitsOfMeasure
	Dim oCustom As PropertySet = oAsmDoc.PropertySets("Inventor User Defined Properties")
	Dim oDesignProps As PropertySet = oAsmDoc.PropertySets("Design Tracking Properties")
	
	Dim oWeigt_KG, oWeigt_Lbs, oStockNumb As Inventor.Property
	Try : oWeigt_KG = oCustom("Existing size Weight Kg")
	Catch : oWeigt_KG = oCustom.Add("", "Existing size Weight Kg") : End Try
	Try : oWeigt_Lbs = oCustom("Existing Part Weight Lbs")
	Catch : oWeigt_Lbs = oCustom.Add("", "Existing Part Weight Lbs") : End Try
	Try : oCustom("CAM-Weight Kg").Value = ""
	Catch : oCustom.Add("", "CAM-Weight Kg") : End Try
	Try : oCustom("CAM-Weight Lbs").Value = ""
	Catch : oCustom.Add("", "CAM-Weight Lbs") : End Try
	oStockNumb = oDesignProps("Stock Number")
	oWeigt_KG.Value = ""
	oWeigt_Lbs.Value = ""
	oStockNumb.Value = ""
	
	Dim oOcc As ComponentOccurrence
	oOcc = oInvApp.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, _
										"Select component...")
	If oOcc Is Nothing Then Exit Sub
		
	Dim oDef As PartComponentDefinition = oOcc.Definition
	Dim oCCPropSet As PropertySet
	Try : oCCPropSet = oDef.Document.PropertySets.Item("Content Library Component Properties")
	Catch : MessageBox.Show("The Part is not a CC part.") : End Try
	
	Dim dMass As Double = oOcc.MassProperties.Mass
	oWeigt_KG.Value = Round(dMass, 2)
	oWeigt_Lbs.Value = Round(oUOfM.ConvertUnits(dMass, UnitsTypeEnum.kKilogramMassUnits, _
												UnitsTypeEnum.kLbMassMassUnits), 2)
	oStockNumb.Value = oDef.Document.PropertySets("Design Tracking Properties") _
												("Stock Number").Value	
	
	Dim familyId As Inventor.Property = oCCPropSet.Item("FamilyId")
	Dim MemberId As String = oCCPropSet.Item("MemberId").Value
	Dim oFamily As ContentFamily
	oFamily = oContentCenter.GetContentObject("v3#" & familyId.Value & "#")
	Dim oContentNode As ContentTreeViewNode = oContentCenter.TreeViewTopNode
	Dim oLengthM As UserParameter
	Dim oRow As ContentTableRow = oFamily.TableRows.Item(MemberId)
	Dim iCount As Integer = oFamily.TableRows.Count - 1
    Dim iStockNumb As Integer = GetNumbColomn(oFamily.TableColumns, {"Stock Number" })
    If iStockNumb = -1 Then
        MessageBox.Show("Failed to get column - Stock Number", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If

    Dim iWeight As Integer = GetNumbColomn(oFamily.TableColumns, {"Specific mass", "Mass","Mass [Kg/mm]"})
    If iWeight = -1 Then
        MessageBox.Show("Failed to get column - Mass", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Exit Sub
    End If
	Dim sStockNumb(iCount), sWeight(iCount) As String
	For i As Integer = 0 To iCount
		sStockNumb(i) = """" & oFamily.TableRows(i + 1)(iStockNumb).Value & """"		
		sWeight(i) = """" & oUOfM.ConvertUnits(oFamily.TableRows(i + 1)(iWeight).Value,
							oFamily.TableColumns(iWeight).Units, "kg/m") & """"
	Next i
	Dim oColumn As ContentTableColumn = oFamily.TableColumns("STOCKNUMBER")
	Call SetParamList(oUsParams, "StockNumbBeamList", sStockNumb)
	Call SetParamList(oUsParams, "WeightKg", sWeight)
	Try : oLengthM = oUsParams("LengthM") : Catch
	oLengthM = oUsParams.AddByValue("LengthM", Nothing, _
										UnitsTypeEnum.kMeterLengthUnits) : End Try
	oLengthM.Value = oDef.Parameters.UserParameters("B_L").Value
End Sub

Private Function SetParamList(ByVal oUsParams As UserParameters,
								ByVal sNameParam As String,
								ByVal sList() As String)
	Dim oUsParam As UserParameter
	Try : oUsParam = oUsParams(sNameParam)
	Catch
		oUsParam = oUsParams.AddByValue(sNameParam, "", _
										UnitsTypeEnum.kTextUnits)
	End Try
	Call oUsParam.ExpressionList.ClearAll()
	oExpList = oUsParam.ExpressionList
	Call oExpList.SetExpressionList(sList, True, 1)
	If oExpList.AllowCustomValues Then oExpList.AllowCustomValues = False
	If Not oExpList.CustomOrder Then oExpList.CustomOrder = True
End Function

Private Function GetNumbColomn(ByVal oColomn As ContentTableColumns, ByVal sName() As String) As Integer
    For i As Integer = 1 To oColomn.Count
        If sName.Any(Function(name) oColomn(i).DisplayHeading.ToLower().Contains(name.ToLower())) Then
            Return i
        End If
    Next i
    Return -1
End Function

Rule ChangeBeam:

Dim oDoc As AssemblyDocument = ThisDoc.Document
Dim oUOfM As UnitsOfMeasure = oDoc.UnitsOfMeasure
Dim oCustom As PropertySet = oDoc.PropertySets("Inventor User Defined Properties")
Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
Dim dMassN As Double = Round(oDef.MassProperties.Mass, 2)
Dim oUsParams As UserParameters = oDef.Parameters.UserParameters
Dim oListBeam, oWeightList, oLengthM As UserParameter
Try 
	oListBeam = oUsParams("StockNumbBeamList")
	oWeightList = oUsParams("WeightKg")
	oLengthM = oUsParams("LengthM")
Catch : Exit Sub : End Try
If String.IsNullOrEmpty(oListBeam.Value) Then Exit Sub
Dim oWeigt_KG, oWeigt_Lbs, oNewAsm_KG, oStockNumb As Inventor.Property
Try : oCustom("Existing Assembly weight(Kg)").Value = dMassN
Catch : oCustom.Add(dMassN, "Existing Assembly weight(Kg)") : End Try
Try : oNewAsm_KG = oCustom("New Assembly weight(Kg)")
Catch : oNewAsm_KG = oCustom.Add("", "New Assembly weight(Kg)") : End Try
Try : oExWeigt_KG = oCustom("Existing size Weight Kg")
Catch : oExWeigt_KG = oCustom.Add("", "Existing size Weight Kg") : End Try
Try : oWeigt_KG = oCustom("CAM-Weight Kg")
Catch : oWeigt_KG = oCustom.Add("", "CAM-Weight Kg") : End Try
Try : oWeigt_Lbs = oCustom("CAM-Weight Lbs")
Catch : oWeigt_Lbs = oCustom.Add("", "CAM-Weight Lbs") : End Try
For i As Integer = 1 To oListBeam.ExpressionList.Count
	If oListBeam.ExpressionList(i) = oListBeam.Expression Then
		Dim dLengthM As Double
		dLengthM = oUOfM.ConvertUnits(oLengthM.Value, kCentimeterLengthUnits, kMeterLengthUnits)
		Dim sWeight As String = oWeightList.ExpressionList(i)
		Dim dKG As Double = sWeight.Substring(1, sWeight.Length-2) * dLengthM
		oWeigt_KG.Value = Round(dKG, 2)
		oWeigt_Lbs.Value = Round(oUOfM.ConvertUnits(dKG, UnitsTypeEnum.kKilogramMassUnits, _
												UnitsTypeEnum.kLbMassMassUnits), 2)
		oNewAsm_KG.Value = Round(dMassN - oExWeigt_KG.Value + dKG, 2)
	End If
Next

Andrii Humeniuk - CAD Coordinator, Autodesk Certified Instructor

LinkedIn | My free Inventor Addin | My Repositories

Did you find this reply helpful ? If so please use the Accept as Solution/Like.

EESignature

0 Likes
Message 5 of 6

khantheking1995
Contributor
Contributor

HI @Andrii_Humeniuk 

most of the issues resolved now except for the rounding of inaccuracy
I see that the code for rounding off works fine but i see the inaccuracy comes from the Length.

 

The program pulls the length as follows(see below example)

In the below image I selected the marked part and I got the length as 33.02 and this comes from the sketch (see image 2)

khantheking1995_0-1697528357003.png

In the image the length is 1300 in thats comes to 33.02 m when converted, this cause inaccuracy.

khantheking1995_1-1697528439952.png

To avoid this , we need to pull length as shown in below image

The Frame member data show accurate length that the shape utilize but in different units(mm, m, in, ft etc) so the length data should be pulled from here and should be converted to meter and should be used in our code.

 

khantheking1995_2-1697528573044.png

Hope you can do this, also i need a code to automatically generate the form(below image) in global forms as shown below when the Partselect code is pasted and clicked save and run.

khantheking1995_3-1697528810025.png

 

0 Likes
Message 6 of 6

khantheking1995
Contributor
Contributor

@Andrii_Humeniuk Any update on my request?

0 Likes