copy the custom properties the assebly and it are reflected in each part of this assembly in the same location

copy the custom properties the assebly and it are reflected in each part of this assembly in the same location

Luis.TorresPZK9R
Participant Participant
354 Views
2 Replies
Message 1 of 3

copy the custom properties the assebly and it are reflected in each part of this assembly in the same location

Luis.TorresPZK9R
Participant
Participant

hi,

I am trying to configure the assembly template, to ensure that the custom properties are reflected in each part of this assembly, as long as these parts are in the same location that the assembly was saved and that the parts that are not in the same location , remain with the properties of each part.

 

I have the following code:

 

Sub SetPropertiesBasedOnLocation()

    ' Gets the location of the assembly

    Dim assemblyDoc As AssemblyDocument = ThisApplication.ActiveDocument

    Dim assemblyPath As String = System.IO.Path.GetDirectoryName(assemblyDoc.FullFileName)

 

    ' Iterate through the assembly parts

    For Each occurrence As ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences

        ' Gets the location of the part

        Dim partPath As String = System.IO.Path.GetDirectoryName(occurrence.Definition.Document.FullFileName)

 

        ' Compare the part location to the assembly location

 

        If String.Compare(assemblyPath, partPath, StringComparison.OrdinalIgnoreCase) = 0 Then

            ' Matches the properties of the assembly to the part

            CopyProperties(assemblyDoc, occurrence.Definition.Document)

        End If

    Next

End Sub

 

Sub CopyProperties(sourceDoc As Document, targetDoc As Document)

    ' Copy the properties of the source document to the destination document

    For Each prop As Property In sourceDoc.PropertySets.Item("Inventor Summary Information").Properties

        Try

            targetDoc.PropertySets.Item("Inventor Summary Information").Set(prop.Name, prop.Value)

        Catch ex As Exception

            ' You can handle exceptions here if necessary

        End Try

    Next

End Sub

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

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Luis.TorresPZK9R.  I can see a few things in your code that might need to be fixed, and / or improved a bit to make it work better for you.  Try the following edited version of your code.  However, where are you planning on running this code from.  If you plan to run this from an iLogic rule, then you may need to change the "SetPropertiesBasedOnLocation" name to "Main", because an iLogic rule with multiple routines needs to have a Sub Main...some code here...End Sub before any other routines can be included.  But if running it from an external vb.net resource, it might be just fine.  But if running it from an external vb.net resource, you may need to define the "ThisApplication" variable somewhere at a higher level.

Sub SetPropertiesBasedOnLocation()
    ' Gets the location of the assembly
    Dim assemblyDoc As AssemblyDocument = ThisApplication.ActiveDocument
    Dim assemblyPath As String = System.IO.Path.GetDirectoryName(assemblyDoc.FullFileName)
    ' Iterate through the assembly parts
    For Each occurrence As ComponentOccurrence In assemblyDoc.ComponentDefinition.Occurrences
        ' Gets the location of the part
		Dim sFFN As String = occurrence.ReferencedDocumentDescriptor.ReferencedFileDescriptor.FullFileName
        Dim partPath As String = System.IO.Path.GetDirectoryName(sFFN)
        ' Compare the part location to the assembly location
        If String.Compare(assemblyPath, partPath, StringComparison.OrdinalIgnoreCase) = 0 Then
            ' Matches the properties of the assembly to the part
            CopyProperties(assemblyDoc, occurrence.Definition.Document)
        End If
    Next
End Sub

Sub CopyProperties(sourceDoc As Document, targetDoc As Document)
	If targetDoc.IsModifiable = False Then Return 'target is ReadOnly, so exit routine
	Dim oSourceCProps As Inventor.PropertySet = sourceDoc.PropertySets.Item("Inventor User Defined Properties")
	If oSourceCProps.Count = 0 Then Return 'exit routine if no custom iProperties in source document
	For Each prop As Inventor.Property In oSourceCProps
		Dim oTargetCProps As Inventor.PropertySet = targetDoc.PropertySets.Item("Inventor User Defined Properties")
		Dim oTargetCProp As Inventor.Property = Nothing
		Try
			oTargetCProp = oTargetCProps.Item(prop.Name)
		Catch ex As Exception
			'if that failed, try to create the custom iProperty
			oTargetCProp = oTargetCProps.Add(prop.Value, prop.Name)
		End Try
		'it may have found the existing property, but has not set its value yet, so check that here
		If oTargetCProp IsNot Nothing AndAlso oTargetCProp.Value <> prop.Value Then
			Try
				oTargetCProp.Value = prop.Value
			Catch
			End Try
		End If
	Next
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 3

Luis.TorresPZK9R
Participant
Participant

Hi @WCrihfield .

The coding works, but you may be able to choose a number of custom properties and project properties (example: Stock Number, Description, Revision Number) . instead of taking all the custom properties.

0 Likes