add custom iProperties to each component with c# add-in

add custom iProperties to each component with c# add-in

Anonymous
Not applicable
1,896 Views
5 Replies
Message 1 of 6

add custom iProperties to each component with c# add-in

Anonymous
Not applicable

Hi

i have a simple application that runs as an add-in in Inventor, that i use to change the visibility of several parts in an assembly document.

it changes the visibility of all the selected parts and components.

its code is like this:

SelectSet selSet = default(SelectSet);
                    selSet = asmDoc.SelectSet;

                    try
                    {
                        ComponentOccurrence compOcc = default(ComponentOccurrence);
                        object obj = null;
                        foreach (object obj_loopVariable in selSet)
                        {
                            obj = obj_loopVariable;
                            compOcc = (ComponentOccurrence)obj;
                            System.Diagnostics.Debug.Print(compOcc.Name);
                            compOcc.Visible = !compOcc.Visible;
//add costum iproperties for each selected component } } catch (Exception ex) { return; }

i would like to add a costume i properties to each component,  can anybody suggest me how to do that?

0 Likes
Accepted solutions (1)
1,897 Views
5 Replies
Replies (5)
Message 2 of 6

JamieVJohnson2
Collaborator
Collaborator

Give this a convert from VB.Net and then check it out.

    ''' <summary>
    ''' Add Custom iProperty To Inventor Document
    ''' </summary>
    ''' <param name="propName"></param>
    ''' <param name="propValue"></param>
    ''' <param name="invDoc"></param>
    ''' <param name="OverWriteProperty"></param>
    ''' <returns>True if file was changed</returns>
    ''' <remarks>Includes all file states, readonly and unsaved new files</remarks>
    Public Function CreateAndEditCustomProperty(ByVal propName As String, ByVal propValue As Object, ByVal invDoc As Inventor.Document, Optional OverWriteProperty As Boolean = True) As Boolean
        Try
            eventLog += vbCrLf & invDoc.FullFileName
            Dim propSet As PropertySet = invDoc.PropertySets.Item("Inventor User Defined Properties")
            Dim custProp As Inventor.Property = FindiProperty(propSet.Name, propName, invDoc)
            If custProp Is Nothing Then
                custProp = propSet.Add(propValue, propName)
                filesEffected += 1
                eventLog += vbCrLf & "Added custom property: " & propName & ":  " & propValue
                Return True
            Else
                If Not String.IsNullOrWhiteSpace(custProp.Value) Then ' has a value first check if expression, then if regular property
                    If OverWriteProperty = True Then
                        If custProp.Expression.StartsWith("=") Then 'is expression
                            If custProp.Expression <> propValue Then
                                custProp.Value = propValue
                                filesEffected += 1
                                eventLog += vbCrLf & "Modified custom property: " & propName & ":  " & propValue
                                Return True
                            End If
                        Else 'is value
                            If custProp.Value <> propValue Then
                                custProp.Value = propValue
                                filesEffected += 1
                                eventLog += vbCrLf & "Modified custom property: " & propName & ":  " & propValue
                                Return True
                            End If
                        End If
                    End If
                Else 'is emtpy - overwrite command does snot apply
                    custProp.Value = propValue
                    filesEffected += 1
                    eventLog += vbCrLf & "Modified custom property: " & propName & ":  " & propValue
                    Return True
                End If
            End If
            Return False
        Catch ex As Exception
            'if property fails to write such as user does not check out or allow file to save, then retun without error.
            MsgBox("Can not write custom property: " & propName)
            Return False
        End Try
    End Function

btw eventLog and filesEffected are a public shared string and integer values I use when running this program as part of a much larger system.  It reports possible errors for the user to read about.  You can omit that feature, or change it up as you see fit.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 3 of 6

Anonymous
Not applicable

thank you @JamieVJohnson2 

but I think this will add the custom property to active document if it's not added already, or change its value if it is present.  am i right? or I am misunderstanding something here

I am about to add the property to each one of the components that is selected by the user (or in some cases all component in the active document_that is not my question now) in an assembly document.

 

0 Likes
Message 4 of 6

chandra.shekar.g
Autodesk Support
Autodesk Support
Accepted solution

@Anonymous ,

 

Try below C# code to add custom iProperties to occurrence.

 

public void occ_customipropertty(AssemblyDocument asmDoc)
        {
            SelectSet selSet = default(SelectSet);
            selSet = asmDoc.SelectSet;

            try
            {
                ComponentOccurrence compOcc = default(ComponentOccurrence);
                object obj = null;
                foreach (object obj_loopVariable in selSet)
                {
                    obj = obj_loopVariable;
                    compOcc = (ComponentOccurrence)obj;
                    System.Diagnostics.Debug.Print(compOcc.Name);
                    compOcc.Visible = !compOcc.Visible;
                    //add costum iproperties for each selected component
                    CreateAndEditCustomProperty("Property Name", "Property value", compOcc.ReferencedDocumentDescriptor.ReferencedDocument);
                }
            }
            catch (Exception ex)
            {
                return;
            }
        }
        public void CreateAndEditCustomProperty(string propName, object propValue, Inventor.Document invDoc)
        { 
            PropertySet propSet = invDoc.PropertySets["Inventor User Defined Properties"];
            Inventor.Property prop;
            try
            {
                prop = propSet[propName];
            }
            catch (Exception e)
            {
                prop = propSet.Add("",propName);                    
            }
            prop.Value = propValue;
            
        }

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 6

JamieVJohnson2
Collaborator
Collaborator

Just like the other code posted, the routine I posted, requires you to input the desired document for it to do work on.  So it would not be the 'active document' per se, it would be any document you acquire in advance, and then send into it.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 6 of 6

Anonymous
Not applicable

my actual problem was to accessing to the referenced document through component occurrence.

that was solved by this line:

compOcc.ReferencedDocumentDescriptor.ReferencedDocument

 

0 Likes