Copy Revision Number from Drawing to attached part

Copy Revision Number from Drawing to attached part

Gwennberg
Advocate Advocate
322 Views
3 Replies
Message 1 of 4

Copy Revision Number from Drawing to attached part

Gwennberg
Advocate
Advocate

Hi

I try to copy "Revision Number" from drawing to reference part and got Microsoft Copilot to help me but not to the goal line :-). The code is like this:

' Kontrollera att det aktiva dokumentet är en ritning
If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
    MessageBox.Show("Detta skript fungerar endast med ritningsdokument.", "Fel")
    Return
End If

' Hämta ritningsrevisionen
Dim oDrawingDoc As DrawingDocument
oDrawingDoc = ThisApplication.ActiveDocument

Dim revision As String
revision = iProperties.Value("Project","Revision Number")

' Kontrollera att det finns en kopplad partfil
If oDrawingDoc.ReferencedDocumentDescriptors.Count = 0 Then
    MessageBox.Show("Ingen kopplad partfil hittades.", "Fel")
    Return
End If


' Hämta den första kopplade partfilen
Dim oPartDescriptor As DocumentDescriptor
oPartDescriptor = oDrawingDoc.ReferencedDocumentDescriptors.Item(1)


' Kontrollera om partfilen redan är öppen
Dim oPartDoc As PartDocument
If oPartDescriptor.ReferencedDocument Is Nothing Then
    ' Öppna partfilen om den inte redan är öppen
    oPartDoc = ThisApplication.Documents.Open(oPartDescriptor.FullDocumentName, False)
Else
    ' Använd den redan öppna dokumentreferensen
    oPartDoc = oPartDescriptor.ReferencedDocument
End If

' Sätt iProperty "Revision Number" i den kopplade partfilen
iProperties.Value(oPartDoc,"Project","Revision Number") = revision
MessageBox.Show("Revisionsbokstaven har uppdaterats till: " & revision, "Uppdatering Klar")

 

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

WCrihfield
Mentor
Mentor
Accepted solution

Hi @Gwennberg.  Try changing the following line in your code:

iProperties.Value(oPartDoc,"Project","Revision Number") = revision

...to be like this:

iProperties.Value(oPartDoc.DisplayName,"Project","Revision Number") = revision

...because that first (of 3) inputs is wanting a String.  Sometimes the Document.DisplayName property value will work there, and if not, you can try extracting the file name (no file path) of that document, and using that in that position.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 4

Gwennberg
Advocate
Advocate

Thank's WCrihfield. It worked!
//Goran

0 Likes
Message 4 of 4

contact
Advocate
Advocate

Hi, I was facing the same challenge, thanks for the input. 

 

I have modified the code a little, so that it applies to any referenced part or assembly (first reference).

 

I am sharing this here, in case it is of use for someone else.

 

Best regards, Jan. 

 

' Check if active doc is a dwg
If ThisApplication.ActiveDocument.DocumentType <> DocumentTypeEnum.kDrawingDocumentObject Then
    MessageBox.Show("The active doc is not a drawing.", "Active")
    Return
End If

' Get drawing rev content
Dim oDrawingDoc As DrawingDocument
oDrawingDoc = ThisApplication.ActiveDocument

Dim revision As String
revision = iProperties.Value("Project","Revision Number")


' Get first connected model document
Dim oModelDescriptor As DocumentDescriptor
oModelDescriptor = oDrawingDoc.ReferencedDocumentDescriptors.Item(1)


' Check if model doc is already open
Dim oModelDoc As Document
If oModelDescriptor.ReferencedDocument Is Nothing Then
    ' Opens if not already open
    oModelDoc = ThisApplication.Documents.Open(oModelDescriptor.FullDocumentName, False)
Else
    ' Use already open reference
    oModelDoc = oModelDescriptor.ReferencedDocument
End If


' Set iProperty "Revision Number" to model file
iProperties.Value(oModelDoc.DisplayName, "Project", "Revision Number") = revision

MessageBox.Show("Revision Index: " & revision, "Rev Copy Successful")

 

0 Likes