Issue in C# addin with retrieving custom properties values

Issue in C# addin with retrieving custom properties values

ewu9
Enthusiast Enthusiast
399 Views
8 Replies
Message 1 of 9

Issue in C# addin with retrieving custom properties values

ewu9
Enthusiast
Enthusiast

Has anyone encountered an error where retrieving custom properties in an inventor part doesn't work? It only happens on some parts, and only via addins (I'm using C#, but it always works when I run the same script via ilogicVB). 

And one way I've found to solve the issue is to basically 'poke' the parameter; so either modify it to a different value then modify it back, or delete the parameter and re-add it. But that solution isn't practical when I have 300+ parts. I've attached the script I use in my addin and ilogic.

Any ideas? Thanks!

    Dim propName As String = "PICNumber"
    Dim customPropSet As PropertySet
    customPropSet = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties")
    
    Try
        Dim PIC_Number As Integer = CInt(customPropSet.Item(propName).Value)
            Return PIC_Number

    Catch
        ' Property doesn't exist or value isn't valid
        Messagebox.show("not working")
    End Try

 

            string prop_name = "PICNumber";
            PropertySet custom_property_set = part_doc.PropertySets["Inventor User Defined Properties"];

            try
            {
                int PIC_Number = (int)custom_property_set[prop_name].Value;
            }
            catch
            {
                System.Windows.Forms.MessageBox.Show("not working");
            }

 

0 Likes
Accepted solutions (1)
400 Views
8 Replies
Replies (8)
Message 2 of 9

Mike.Wohletz
Collaborator
Collaborator

your C# property set does not have an ".item" in the name so that is wrong. Try something like the following function and only deal with the conversion when needed or you have a value. 

 

public string GetCustomProperty(string propName)
{
    Application ThisApplication = g_inventorApplication;
    try
    {
        return ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties").item(propName).value;
    }
    catch (Exception ex)
    {
        return null;
    }
}


 

0 Likes
Message 3 of 9

ewu9
Enthusiast
Enthusiast

ewu9_0-1744307363497.png

The syntax in C# doesn't seem to use ".item"

I don't suspect a syntax problem as my issue isn't the function not working, but rather that it doesn't work sometimes for specific parts, despite those parts having the parameter I'm trying to extract. 

0 Likes
Message 4 of 9

jjstr8
Collaborator
Collaborator

@ewu9 : You didn't mention the property data type, "Number" or "Text". If it's text and there's no value, the cast to integer will fail. If you have a mix for some reason, you can try this.

try
{
    int PIC_Number;
    Property property = part_doc.PropertySets["Inventor User Defined Properties"]["PICNumber"];
    if (property.Value is int intValue)
    {
        PIC_Number = intValue;
    }
    else if (property.Value is string stringValue)
    {
        _ = int.TryParse(stringValue, out PIC_Number);
    }
}
catch (Exception)
{
    System.Windows.Forms.MessageBox.Show("not working");
}

0 Likes
Message 5 of 9

ewu9
Enthusiast
Enthusiast

The PICNumber parameter is assigned as a number type by another script.

I also double checked the parameter type of the parts manually while I was trying to debug. 

If it was a type issue where the .value is unable to cast the ilogicVB script would've returned that error on these exact instances. I'm only encountering this issue in the C# addin version.

0 Likes
Message 6 of 9

Mike.Wohletz
Collaborator
Collaborator

If you can make this happen then why not run the application in debug mode to find the issue as it should show up as an exception you can trace. 

0 Likes
Message 7 of 9

jjstr8
Collaborator
Collaborator
Accepted solution

@ewu9 : In this case, there are differences between (int) in C# and CInt() inVB. CInt() in VB is a type conversion with coercion, which means a boxed double will convert fine. In the case of numeric properties, what you get is a boxed version of the original value type. In C#, it must be unboxed to the original value type. After that you can cast it to a int even if it was a double. As @Mike.Wohletz mentioned, it's probably best to run it in the debugger and see what the property value object is when it fails.

 

The property value was a Number entered as "1"

jjstr8_0-1744638181549.png

In this case, the property was entered as "1.0" and is now a double. CInt in VB would fine with that, and return an integer 1.

jjstr8_1-1744638192590.png

 

 

0 Likes
Message 8 of 9

ewu9
Enthusiast
Enthusiast

Yup, it was a type issue. The parameters were getting converted to double sometimes, I ended doing this to resolve it.

(int)Convert.ToInt16(custom_property_set[prop_name].Value)

Thanks!



Message 9 of 9

jjstr8
Collaborator
Collaborator

Mystery solved! FYI, you don't need to convert it to Int16 and then cast to int. Convert.ToInt32 gives you exactly what you want.

 

Convert.ToInt32(custom_property_set[prop_name].Value)

 

jjstr8_0-1744659937883.png