Copying Custom iProperties from Existing Drawing to Another Drawing

Copying Custom iProperties from Existing Drawing to Another Drawing

Victor.Tuguinay
Enthusiast Enthusiast
451 Views
2 Replies
Message 1 of 3

Copying Custom iProperties from Existing Drawing to Another Drawing

Victor.Tuguinay
Enthusiast
Enthusiast

I have this code that tries to copy custom iproperties from a drawing to another drawing. It allows the user to specify which drawing to copy the iproperties from source drawing and it is supposed to assign copied values to the target drawing. Can someone please debug this? Thanks! 

 

Imports System.Windows.Forms
' Import necessary namespaces

' Create a file dialog box to select the source drawing
Dim oFileDialog As New OpenFileDialog()
oFileDialog.Filter = "AutoCAD Drawings (*.dwg)|*.dwg"
oFileDialog.Title = "Select Source Drawing"

' Show the dialog and get the result
If oFileDialog.ShowDialog() = DialogResult.OK Then
Dim sourceDrawingPath As String = oFileDialog.FileName

' Open the source drawing
Dim sourceDoc As Document = ThisApplication.Documents.Open(sourceDrawingPath, False)

' Get the active document (target drawing)
Dim targetDoc As Document = ThisApplication.ActiveDocument

' List of custom iProperties to copy
Dim customProperties As New List(Of String) From {"Client_1", "Client_2"}

' Loop through each custom property and copy its value
For Each propName As String In customProperties
Try
' Read the iProperty value from the source document
Dim propValue As String = iProperties.Value(sourceDoc, "Custom", propName)

' Write the iProperty value to the target document
iProperties.Value(targetDoc, "Custom", propName) = propValue

' Show the copied property value
MessageBox.Show("Copied property: " & propName & " with value: " & propValue, "Property Copied", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Error copying property: " & propName & ". " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Next

' Save the target drawing
targetDoc.Save()

' Close the source drawing
sourceDoc.Close(True)
Else
MessageBox.Show("No file selected. Please try again.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If

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

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Victor.Tuguinay.  I think this section of your code will need to change form:

 

' Read the iProperty value from the source document
Dim propValue As String = iProperties.Value(sourceDoc, "Custom", propName)

' Write the iProperty value to the target document
iProperties.Value(targetDoc, "Custom", propName) = propValue

 

...to this:

 

' Read the iProperty value from the source document
Dim propValue As String = sourceDoc.PropertySets.Item(4).Item(propName).Value

' Write the iProperty value to the target document
targetDoc.PropertySets.Item(4).Item(propName).Value = propValue

 

 When using the iLogic snippet 'iProperties.Value()', the first (of 3) inputs can not be the Document object itself.  It must either be the name of an assembly component, as seen in the model browser tree, or it must be the 'name' of a Document.  Since a Document does not actually have a property simply named 'Name', we can usually use the Document.DisplayName value there.  But the Inventor API example I am showing here is safer, because it stems from the Document objects directly.

One main difference in doing things this way is that...if the custom iProperty in the 'destination' document does not already exist yet, then trying to set its value will throw an error.  So, I would suggest enclosing the 'set' code within a Try...Catch...End Try block of code, on the Try side, then on the Catch side, have it create the Property object, using the PropertySet.Add method.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 3

Victor.Tuguinay
Enthusiast
Enthusiast

It works! Thank you!

0 Likes