Copy Derived Custom iProperties (Pt 2) - Check for Existing

Copy Derived Custom iProperties (Pt 2) - Check for Existing

kennyj
Collaborator Collaborator
556 Views
2 Replies
Message 1 of 3

Copy Derived Custom iProperties (Pt 2) - Check for Existing

kennyj
Collaborator
Collaborator

This is Part 2 of a previous post:

 

https://forums.autodesk.com/t5/inventor-customization/copy-all-custom-iproperties/m-p/6941570#M70802

 

@HermJan.Otterman helped with this great rule (Below) to Copy all the Custom iProperties from a derive into the new part.

 

In using the rule we have found out that it does not check for existing first; if the Custom iProperty is already existing, it adds a new iProperty "Inventor User Defined Properties#".  I would like for it to overwrite the existing iProperty.

 

I have tried several "Try/Catch" and "If/Then" options, but they error out or run with no change in results.

 

Can someone provide guidance?

 

Format:HTML Format Version:1.0 StartHTML: 165 EndHTML: 7314 StartFragment: 314 EndFragment: 7282 StartSelection: 314 EndSelection: 314SyntaxEditor Code Snippet

'-----Start iLogic Rule "Copy all Custom iProperties from original derive"

Dim derivedComponent As Inventor.Document = ThisApplication.ActiveDocument

'if there is only one reference:

Dim referencedComponent As Inventor.Document = derivedComponent.ReferencedDocuments(1)

Dim deriProps As PropertySet = derivedComponent.PropertySets.Item("Inventor User Defined Properties")

Dim refiProps As PropertySet = referencedComponent.PropertySets.Item("Inventor User Defined Properties")

Dim oProp As Inventor.Property

For Each oProp In refiProps

Try

deriProps.Add(oProp.Value, oProp.Name)

Catch ex As Exception

deriProps.Add(oProp.Value)

End Try

Next

'-----End iLogic Rule

 

 

 

Secondary goal; is there a way to modify this code to have it pull only a specific iProperty?  I have one called "CMF_DESGINATION" that I need most of the time, so I was trying to change the above to ignore all except this one.

 

Thank you all for your help as I learn these processes.

 

*(And thank you to Mod the Machine, From the Trenches, If This Then That and all the other sites!)

 

Kenny

 

*Edit, I had tried (`) at the Catch phrase above during testing and forgot to remove it on the post.

 

0 Likes
Accepted solutions (1)
557 Views
2 Replies
Replies (2)
Message 2 of 3

Owner2229
Advisor
Advisor
Accepted solution

Here you go. "UCase()" ensures case insensitivity.

 

Dim oDerived As Inventor.Document = ThisApplication.ActiveDocument
If oDerived.ReferencedDocuments.Count = 0 Then Exit Sub

Dim oReferenced As Inventor.Document = oDerived.ReferencedDocuments(1)

Dim oDerProps As PropertySet = oDerived.PropertySets.Item("Inventor User Defined Properties")
Dim oRefProps As PropertySet = oReferenced.PropertySets.Item("Inventor User Defined Properties")

Dim oProList() As String = {"CMF_DESGINATION", "OTHER_iPRO"} 'Use "*" to copy all iPros
For Each oRefPro As Inventor.Property In oRefProps
    Dim oSkip As Boolean = True
    For Each ProName As String In oProList
        If Ucase(ProName) <> Ucase(oRefPro.Name) And ProName <> "*" Then Continue For
        oSkip = False
        Exit For
    Next
    If oSkip Then Continue For
    Dim oDerPro As Inventor.Property
    Try
        oDerPro = oDerProps(oRefPro.Name)
    Catch
        oDerPro = oDerProps.Add("", oRefPro.Name)
    End Try
    oDerPro.Value = oRefPro.Value
Next

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 3 of 3

kennyj
Collaborator
Collaborator

Wow, Mike.

 

Worked like a charm for both situations.

 

Thank you!

 

Kenny

 

(And thank you for explaining the changes you made - turns out I wasn't as close to a solution as I thought.)

0 Likes