Setting Custom iProperties in SelectSet using vb.net

Setting Custom iProperties in SelectSet using vb.net

8013068
Enthusiast Enthusiast
1,193 Views
6 Replies
Message 1 of 7

Setting Custom iProperties in SelectSet using vb.net

8013068
Enthusiast
Enthusiast

I'd like to set a custom iProperty for all the parts I've highlighted in an assembly, but I can't get it to work. Here's what I have so far:

Public Sub SetiProperties(ipropertyval As String, ipropertyname As String)
        Try
            'set reference to inventor
            Dim ThisApplication As Inventor.Application
            ThisApplication = GetObject(, "Inventor.Application")

            'Make a ref to active doc
            Dim oDoc As Document
            oDoc = ThisApplication.ActiveDocument

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

            For Each part In oDoc.SelectSet
                Dim invCustomPropertySet As PropertySet
                invCustomPropertySet = part.PropertySets.Item("Inventor User Defined Properties")
                Dim invProperty As Inventor.Property
                invProperty = invCustomPropertySet.Add(ipropertyval, ipropertyname)
            Next
        Catch
            MessageBox.Show("Error Setting iProperties", "Error")
        End Try
    End Sub

It seems to have trouble at this line:

invCustomPropertySet = part.PropertySets.Item("Inventor User Defined Properties")

Any ideas? Thanks in advance for any help

0 Likes
Accepted solutions (1)
1,194 Views
6 Replies
Replies (6)
Message 2 of 7

dgreatice
Collaborator
Collaborator

Hi,

 

how you select documents more than 1, and collected as selectedset?

are documents that you select are components(part/sub assy) in Main Assy? it will became component occurrence type, not a document type. 

Please use the ACCEPT AS SOLUTION or KUDOS button if my Idea helped you to solve the problem.

Autodesk Inventor Professional Certified 2014
0 Likes
Message 3 of 7

mr_ensing
Advocate
Advocate

For one i would define part.

 

Dim part As PartDocument
For Each part In oDoc.SelectSet
	Dim invCustomPropertySet As PropertySet
	invCustomPropertySet = part.PropertySets.Item("Inventor User Defined Properties")
	Dim invProperty As Inventor.Property
	invProperty = invCustomPropertySet.Add(ipropertyval, ipropertyname)
Next

 

Here some quick and dirty (VBA!) code on handling a selection:

 

Sub TEST()
	Dim oAssy As AssemblyDocument
    Dim oSet As SelectSet
	Set oAssy = ThisApplication.ActiveDocument
	Set oSet = oAssy.SelectSet
    If oSet Is Nothing Then Exit Sub
    If Not oSet.count > 0 Then
        ' selection empty
        Exit Sub
    End If
    ' cycle selection
	Dim part As PartDocument
    Dim oObj As Object
    For Each oObj In oSet
        If Not oObj Is Nothing Then
            If oObj.Type = kComponentOccurrenceObject _
               Or oObj.Type = kComponentOccurrenceProxyObject Then
                If oObj.DefinitionDocumentType = kPartDocumentObject Then
					Set part = oObj.ReferencedDocumentDescriptor.ReferencedDocument
					' do stuff with 'part'
                End If
            End If
        End If
    Next oObj
End Sub

 

0 Likes
Message 4 of 7

8013068
Enthusiast
Enthusiast

Thanks for the reply. I have selection priority set to "Part Priority".

0 Likes
Message 5 of 7

8013068
Enthusiast
Enthusiast

Thanks for your reply. It looks like the code is running and iterating through the parts correctly (I can see that if I add a message box showing the name of the selected part to the for loop, I don't think I need to define the part like that in vb.net. The problem I'm running into is in the ' do stuff with 'part' section.

Here's the equivalent code in VBA:

Sub Main()
    
            Dim ipropertyval As String
            ipropertyval = "testval"
            
            Dim iproertyname As String
            ipropertyname = "testname"
    
            'Make a ref to active doc
            Dim oDoc As Document
            Set oDoc = ThisApplication.ActiveDocument

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

            MsgBox (oDoc.SelectSet.Count() & " parts are selected")
            
            Dim part As Object
            For Each part In oDoc.SelectSet
                MsgBox (part.Name) 'added to show it is selecting parts correctly
                Dim invCustomPropertySet As PropertySet
                Set invCustomPropertySet = part.PropertySets.Item("Inventor User Defined Properties") 'error on this line
                Dim invProperty As Inventor.Property
                Set invProperty = invCustomPropertySet.Add(ipropertyval, ipropertyname)
            Next

End Sub
0 Likes
Message 6 of 7

omartin
Advocate
Advocate
Accepted solution

When your are doing the loop, the 'part' is actually a componentOcc., you can check that by printing, part.type instead of .name. the componentOcc has a name property so it is being displayed. but it does not have a propertyset.

you have to define a document object to get the property set (or you can use part.Definition.Document...)

Was my reply Helpful ? send a Kudos or accept as solution
0 Likes
Message 7 of 7

8013068
Enthusiast
Enthusiast

Thanks! That did the trick. For anyone who comes across this in the future, here's the code:

Public Sub SetiProperties(ipropertyval As String, ipropertyname As String)
        Try
            'set reference to inventor
            Dim ThisApplication As Inventor.Application
            ThisApplication = GetObject(, "Inventor.Application")

            'Make a ref to active doc
            Dim oDoc As Document
            oDoc = ThisApplication.ActiveDocument

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

            'MessageBox.Show("There are " & oDoc.SelectSet.Count() & " parts selected")

            For Each partocc In oDoc.SelectSet
                Dim part As Document = partocc.Definition.Document
                'MessageBox.Show(part.Type)
                Dim invCustomPropertySet As PropertySet
                invCustomPropertySet = part.PropertySets.Item("Inventor User Defined Properties")
                Dim invProperty As Inventor.Property
                invProperty = invCustomPropertySet.Add(ipropertyval, ipropertyname)
            Next
        Catch
            MessageBox.Show("Error Setting iProperties", "Error")
        End Try
    End Sub