Up Down fold count properties

Up Down fold count properties

ch_giacomo
Enthusiast Enthusiast
286 Views
3 Replies
Message 1 of 4

Up Down fold count properties

ch_giacomo
Enthusiast
Enthusiast

Good morning everyone, I have modified this rule so that it is compatible with my work (I took the basic rule from this forum).
What I can't do is: if "Bend Up and Bend Down" are present in the properties everything works fine, but if the properties are not present the rule does not create new ones. where is it wrong? Thank you.

 

Imports Inventor

Public Sub Main()
    Try
        Dim partDoc As PartDocument = ThisApplication.ActiveDocument

        If partDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso partDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
            Dim sheetMetalDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition

            If Not sheetMetalDef.HasFlatPattern Then
                sheetMetalDef.Unfold()
            End If

            Dim flatPattern As FlatPattern = sheetMetalDef.FlatPattern

            Dim upsCount As Integer = 0
            Dim downsCount As Integer = 0

            For Each bendResult As FlatBendResult In flatPattern.FlatBendResults
                If Not bendResult.IsOnBottomFace Then
                    If bendResult.IsDirectionUp Then
                        upsCount += 1
                    Else
                        downsCount += 1
                    End If
                End If
            Next

            SetCustomProperty(partDoc, "Bend Up", upsCount)
            SetCustomProperty(partDoc, "Bend Down", downsCount)
        End If
    Catch ex As Exception
        MessageBox.Show("Errore durante l'elaborazione del documento: " & vbCrLf & ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Public Sub SetCustomProperty(doc As Document, name As String, value As Integer)
    Try
        Dim userDefinedProps As Object = doc.PropertySets.Item("User Defined Properties")
        Dim customProp As Object = Nothing

        ' Cerca la proprietà utente con il nome specificato
        For Each prop As Object In userDefinedProps
            If prop.Name = name Then
                customProp = prop
                Exit For
            End If
        Next

        ' Se la proprietà esiste, aggiorna il valore, altrimenti aggiungila
        If customProp IsNot Nothing Then
           customProp.Value = value  
        Else
            userDefinedProps.Add(value, name)
        End If
    Catch ex As Exception
        MessageBox.Show("Errore durante la gestione delle proprietà utente: " & ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub
0 Likes
Accepted solutions (1)
287 Views
3 Replies
Replies (3)
Message 2 of 4

FINET_Laurent
Advisor
Advisor

Hi @ch_giacomo,

 

From what I can tell, the rule is throwing an error when creating a new iProperty. The issue is related to the declaration of unspecified object types for the properties & the property set (which is called evil coding). Declaring the variables with an accurate type for the sub fixes the code : 

Public Sub SetCustomProperty(doc As Document, name As String, value As Integer)
    Try
        Dim userDefinedProps As Inventor.PropertySet = doc.PropertySets.Item("User Defined Properties")
        Dim customProp As Inventor.Property = Nothing

        ' Cerca la proprietà utente con il nome specificato
        For Each prop As Inventor.Property In userDefinedProps
            If prop.Name = name Then
                customProp = prop
				
                Exit For
            End If
        Next
			
        ' Se la proprietà esiste, aggiorna il valore, altrimenti aggiungila
        If customProp IsNot Nothing Then    
		   customProp.Value = value  
		   
        Else
			userDefinedProps.Add(value, name)
			
        End If
		
    Catch ex As Exception		
        MessageBox.Show("Errore durante la gestione delle proprietà utente: " & ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
		
    End Try
End Sub

 

FINET_Laurent_0-1704892825898.png

 

Kind regards, 

FINET L.

 

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

0 Likes
Message 3 of 4

FINET_Laurent
Advisor
Advisor
Accepted solution

@ch_giacomo 

 

Implementation of the fixed code :

Imports Inventor
Public Sub Main()
    Try
        Dim partDoc As PartDocument = ThisApplication.ActiveDocument

        If partDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject AndAlso partDoc.SubType = "{9C464203-9BAE-11D3-8BAD-0060B0CE6BB4}" Then
            Dim sheetMetalDef As SheetMetalComponentDefinition = partDoc.ComponentDefinition

            If Not sheetMetalDef.HasFlatPattern Then
                sheetMetalDef.Unfold()
            End If

            Dim flatPattern As FlatPattern = sheetMetalDef.FlatPattern

            Dim upsCount As Integer = 0
            Dim downsCount As Integer = 0

            For Each bendResult As FlatBendResult In flatPattern.FlatBendResults
                If Not bendResult.IsOnBottomFace Then
                    If bendResult.IsDirectionUp Then
                        upsCount += 1
                    Else
                        downsCount += 1
                    End If
                End If
            Next

            SetCustomProperty(partDoc, "Bend Up", upsCount)
            SetCustomProperty(partDoc, "Bend Down", downsCount)
        End If
    Catch ex As Exception
        MessageBox.Show("Errore durante l'elaborazione del documento: " & vbCrLf & ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Public Sub SetCustomProperty(doc As Document, name As String, value As Integer)
    Try
        Dim userDefinedProps As Inventor.PropertySet = doc.PropertySets.Item("User Defined Properties")
        Dim customProp As Inventor.Property = Nothing

        ' Cerca la proprietà utente con il nome specificato
        For Each prop As Inventor.Property In userDefinedProps
            If prop.Name = name Then
                customProp = prop
				
                Exit For
            End If
        Next
			
        ' Se la proprietà esiste, aggiorna il valore, altrimenti aggiungila
        If customProp IsNot Nothing Then    
		   customProp.Value = value  
		   
        Else
			userDefinedProps.Add(value, name)
			
        End If
		
    Catch ex As Exception		
        MessageBox.Show("Errore durante la gestione delle proprietà utente: " & ex.Message, "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
		
    End Try
End Sub

If this post solved your question, please kindly mark it as "Solution"

If this post helped out in any way to solve your question, please drop a "Like"

@LinkedIn     @JohnCockerill

Message 4 of 4

ch_giacomo
Enthusiast
Enthusiast

Thank you very much!! I hope it can be useful to others.