ilogic add custom property and value to selected assembly parts

ilogic add custom property and value to selected assembly parts

DJFore
Advocate Advocate
5,649 Views
10 Replies
Message 1 of 11

ilogic add custom property and value to selected assembly parts

DJFore
Advocate
Advocate
I am trying to select multiple parts in a frame assembly and run a rule that will add a custom property (or change the value if it exists) to the part level.
The attached rule lets me add a custom property to the file level I am at but will not push it to the part level from the selected items in a assembly.
I am wanting to do this to quickly add note designations to the frame components to show as a column in my BOM.
This is probably going to involve making a oOccurrence but not sure how to map it to multiple selected items.

'SPL NOTE iPROPERTY Dim propertyName1 As String = "SPL NOTE" Dim propertyValue1 As String = "-" customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties") Try prop = customPropertySet.Item(propertyName1) Catch 'Assume Error means Not found customPropertySet.Add("", propertyName1) End Try iProperties.Value("Custom", propertyName1) = propertyValue1
0 Likes
Accepted solutions (1)
5,650 Views
10 Replies
Replies (10)
Message 2 of 11

marcin_otręba
Advisor
Advisor

doc = ThisDoc.Document
Dim oSelectSet As SelectSet
 oSelectSet = doc.SelectSet
If oSelectSet.Count > 1 Then
For i =1 To oSelectSet.Count
If TypeOf oSelectSet.Item(i) Is ComponentOccurrence Then

'your code here


End If
Next

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 3 of 11

DJFore
Advocate
Advocate

I believe i am still missing something linking the two here is my updated code but it does not do anything that I can tell. 

doc = ThisDoc.Document
Dim oSelectSet As SelectSet
 oSelectSet = doc.SelectSet
If oSelectSet.Count > 1 Then
For i =1 To oSelectSet.Count
If TypeOf oSelectSet.Item(i) Is ComponentOccurrence Then

'SPL NOTE iPROPERTY
Dim propertyName1 As String = "SPL NOTE"
Dim propertyValue1 As String = "-"
customPropertySet = ThisDoc.Document.PropertySets.Item("Inventor User Defined Properties")
Try
prop = customPropertySet.Item(propertyName1)
Catch
 'Assume Error means Not found
customPropertySet.Add("", propertyName1)
End Try
iProperties.Value("Custom", propertyName1) = propertyValue1

End If
Next
End If
0 Likes
Message 4 of 11

marcin_otręba
Advisor
Advisor
doc = ThisDoc.Document
Dim oSelectSet As SelectSet
Dim Occ as ComponentOccurrence oSelectSet = doc.SelectSet If oSelectSet.Count > 1 Then For i =1 To oSelectSet.Count If TypeOf oSelectSet.Item(i) Is ComponentOccurrence Then Occ=oSelectSet.item(i) 'SPL NOTE iPROPERTY Dim propertyName1 As String = "SPL NOTE" Dim propertyValue1 As String = "-" customPropertySet = Occ.Definition.Document.PropertySets.Item("Inventor User Defined Properties") Try prop = customPropertySet.Item(propertyName1) Catch 'Assume Error means Not found customPropertySet.Add("", propertyName1) End Try iProperties.Value("Custom", propertyName1) = propertyValue1 End If Next End If

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 5 of 11

DJFore
Advocate
Advocate

I think that is closer but it is still not doing it I get a message stating "The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))" it creates the iproperty in the first instance but does not push the propertyvalue1 into the part. Then on the rest of the occurrences it does nothing. I don't think it is iterating through the occurrences correctly

 

0 Likes
Message 6 of 11

marcin_otręba
Advisor
Advisor

I write From mobile...try:

Dim propertyName1 As String = "SPL NOTE"
Dim propertyValue1 As String = "-"
customPropertySet = Occ.Definition.Document.PropertySets.Item("Inventor User Defined Properties")
Try
prop = customPropertySet.Item(propertyName1)
Prop.value=propertyvalue1 Catch 'Assume Error means Not found customPropertySet.Add("propertyvalue1", propertyName1) End Try End If

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

0 Likes
Message 7 of 11

DJFore
Advocate
Advocate

With that modification I am still getting an error "The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))" when selecting more than one part at a time and thus it is defaulting to the 

 'Assume Error means Not found
customPropertySet.Add("propertyvalue1", propertyName1)

thus it is putting in the value of "propertyvalue1" and not the "-".  the first time. If I rerun the rule it is giving me the same error but on on the first occurrence it is changing the value to "-". But it never goes on through the selected parts in the assembly and does anything with the rest of the occurrences.

0 Likes
Message 8 of 11

JamieVJohnson2
Collaborator
Collaborator

"The parameter is incorrect" means just that.  You are telling Inventor to do something it does not want to do.  Start with the iProperty itself:

  1. Is the file you are trying to modify (sup part/assembly) read only.  If so, Inventor will stop you there.
  2. Is the parameter set to use they correct data type, are you attempting to put a string value in a number data type iProperty?
  3. Also the practice of catching an error to check to see if property exists is not a good one.  Use if statements to verify property exists, then choose to add or update property.  Use Try Catch (ex as system exception) End Try to protect each item in the loop.  The stuff between catch and end try should not be able to cause error (use it for simple stuff such as display a message box) so that the for loop can continue on to the next part file.
  4. Another thought, are you properly separating part from assembly or working around that.  Perhaps the parameter is not the iProperty value, but something else in the call stack.
Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 9 of 11

marcin_otręba
Advisor
Advisor
Accepted solution

There was problem because when code run selected parts goes to be unselected (i think it is issue from ilogic ?). Try now:

Dim oDoc As Document
oDoc = ThisApplication.ActiveDocument

'selected components collection
Dim oSelected As ObjectCollection
oSelected = ThisApplication.TransientObjects.CreateObjectCollection


'Check that at least 1 is selected
If oDoc.SelectSet.Count = 0 Then
    'MessageBox.Show("Please select a component.", "iLogic")
    Exit Sub 'bail out
End If


'add to Object Collection

For i = 1 To oDoc.SelectSet.Count
    If TypeOf oDoc.SelectSet.Item(i) Is ComponentOccurrence Then
        oSelected.Add (oDoc.selectSet.Item(i))
    End If
Next

Dim Occ as ComponentOccurrence


    If oSelected.Count >= 1 Then
        For i =1 To oSelected.Count
        
            Occ=oSelected.item(i)
            
            'SPL NOTE iPROPERTY
            Dim propertyName1 As String = "SPL NOTE"
            Dim propertyValue1 As String = "-"
            customPropertySet = Occ.Definition.Document.PropertySets.Item("Inventor User Defined Properties")
            
            Try
            prop = customPropertySet.Item(propertyName1)
            prop.Value=propertyValue1
            Catch
            'Assume Error means Not found
            customPropertySet.Add("", propertyName1)
            prop = customPropertySet.Item(propertyName1)
            prop.Value=propertyValue1
            End Try
        Next
    End If

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 10 of 11

DJFore
Advocate
Advocate

That worked the only thing I had to do was add a input box to allow my user to set what the iproperty value needed to be.

Thanks!

0 Likes
Message 11 of 11

thuvuphamanh
Explorer
Explorer

Help me write the code that works like the video

0 Likes