I haven't written VB code in a long time and am now getting back into it and starting to use Visual Studio 2013 and I have a question regarding Custom Properties.
I am having trouble trying to access the Custom Properties to check to see if an existing Custom property is there. The examples I have are old and wanted to know if any of you have done this in VS2013
Welcome back! There are a few different approaches (APIs) of determining if a custom property exists. If the file is already open in Solid Edge or you don't mind having Solid Edge open it, you can use the Solid Edge API. If you want to check without opening the file in Solid Edge, we can use the RevisionManager API or the FileProperties API. I can provide an example for any of them. Just need to know which one you're after.
I have the Draft file open in Solid Edge because if the Custom Property does not exisit, I want to add it.
I have the code working to add the Custom Property, but not the Check for existing.
Thanks for your help.
Something like this. Let me know if you have any questions.
Option Infer On Imports System Imports System.Collections.Generic Imports System.Linq Imports System.Runtime.InteropServices Imports System.Text Namespace CustomPropertiesDemo Friend Class Program <STAThread> _ Shared Sub Main(ByVal args() As String) Dim application As SolidEdgeFramework.Application = DirectCast(Marshal.GetActiveObject("SolidEdge.Application"), SolidEdgeFramework.Application) Dim draftDocument As SolidEdgeDraft.DraftDocument = CType(application.ActiveDocument, SolidEdgeDraft.DraftDocument) Dim propertySets = CType(draftDocument.Properties, SolidEdgeFramework.PropertySets) Dim invalidProperty = GetCustomPropertyWithChanceOfException(propertySets, "My String Doesn't Exist") Dim validProperty = GetCustomPropertyWithChanceOfException(propertySets, "My String") validProperty = GetCustomPropertyWithoutChanceOfException(propertySets, "My String") End Sub Private Shared Function GetCustomPropertyWithChanceOfException(ByVal propertySets As SolidEdgeFramework.PropertySets, ByVal propertyName As String) As SolidEdgeFramework.Property Dim properties = propertySets.Item("Custom") ' Trying to access a property by name that doesn't exist will throw an exception. ' Excpetions are very expensive time wise and can slow down your app. Try Return properties.Item(propertyName) Catch ex As System.Exception Console.WriteLine(ex.Message) End Try Return Nothing End Function Private Shared Function GetCustomPropertyWithoutChanceOfException(ByVal propertySets As SolidEdgeFramework.PropertySets, ByVal propertyName As String) As SolidEdgeFramework.Property Dim properties = propertySets.Item("Custom") ' Loop through the existing properties until we find the one we're looking for. For i As Integer = 1 To properties.Count Dim [property] = properties.Item(i) ' Propety names are not case sensitive. If [property].Name.Equals(propertyName, StringComparison.OrdinalIgnoreCase) Then Return [property] End If Next i Return Nothing End Function End Class End Namespace