enhanced save as revision with ilogic

enhanced save as revision with ilogic

Skanda_UNI
Advocate Advocate
847 Views
8 Replies
Message 1 of 9

enhanced save as revision with ilogic

Skanda_UNI
Advocate
Advocate

I use this tool and altered it for saving a revision.

What it does:

It takes the Custom iProperty Current Revision and adds 1.

Than it makes a save as with the new name

opens the corresponding drawing and makes a save as

and finally changes the model reference in the drawing.

 

Unfortunately there is some strange bug in the code. Sometimes it works sometimes it doesnt.

So for me its hard to find why this error occurs

 

Can anyone of you professionals take a look at the code and tell me what i need to alter?

Thank you

 

Sometimes my selfprogrammed codes are broken due to some changes in the ilogic syntax from one version of inventor to another. Could this be the case here? Where can i find the changelogs to fix such problems?

 

This is the genuine code

https://blogs.rand.com/manufacturing/2016/06/ilogic-inventor-enhanced-save-as.html

 

Here is my code:

 

SyntaxEditor Code Snippet

' Start of iLogic code -------------------------------------------------------------------------------------

' Note this iLogic rule takes a copy of the active assembly and associated drawing file
' ...then associates the drawing with the newly copied assembly.

' This rule is a variation on a previous blog by Luke Davenport located here:
' https://www.cadlinecommunity.co.uk/hc/en-us/articles/203347701-Inventor-2016-iLogic-Copy-Assembly-Model-and-Drawing

' This rule allows any name to be input for the copied drawing and assembly, the code in the blog linked above
' ...allows only a suffix to be entered.

' Set drawing extension you are using here by commenting out as required.
'Dim DWGType As String = ".dwg"
Dim DWGType As String = ".idw"

Dim oDoc As AssemblyDocument = Nothing

Try
    oDoc = ThisApplication.ActiveEditDocument
Catch
    MsgBox("Dieses Programm wird in einer Assembly-Datei ausgeführt! - Exiting...", 64, "Revision Speichern")
    Return
End Try

' Get current filename without extension
Dim CurrentFilename As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullDocumentName)
' Get current path
Dim Path As String = System.IO.Path.GetDirectoryName(oDoc.FullDocumentName)

' Check that the drawing for this assembly can be found
If Not System.IO.File.Exists(System.IO.Path.ChangeExtension(oDoc.FullDocumentName, DWGType)) Then
    MsgBox("Es konnte keine Zeichnungsdatei gefunden werden die zu diesem Bauteil passt!" & vbLf & vbLf & _
	CurrentFilename & DWGType & vbLf & vbLf & _
	"Bitte stelle sicher das sich das File im gleichen Ordner befindet, den gleichen Namen hat und die richtige Dateierweiterung '" & DWGType & "'", 64, "Revision Speichern")
    Return
End If

'Dim CurrRev As String = Mid(CurrentFilename, 8, 1) 'Gibt den aktuellen Revisionsstand anhand des Zeichnungsnamens wieder

'Dim CurrRev As String = iProperties.Value("Project", "Revision Number") 'Gibt den aktuellen Revisionsstand anhand des iProperties wieder
Dim CurrRev As String = iProperties.Value("Project", "Revision Number") 'Gibt den aktuellen Revisionsstand anhand des iProperties wieder
'Dim NextRev As String = Chr(Asc(CurrRev) + 1) 'Stellt den aktuellen Revisionsstand als ASCII Code dar und addiert eins gibt bei A also B wieder
Dim NextRev As Integer = CurrRev + 1 'Stellt den aktuellen Revisionsstand einen Wert nach oben
Dim StrNextRev As String = Microsoft.VisualBasic.Strings.Format(NextRev,"0#")
Dim NextRevFileName As String = iProperties.Value("Project", "Stock Number") & "-" & StrNextRev & "_" & iProperties.Value("Project", "Description")

' Prompt user for job number
Dim NewFileName As String = InputBox("Es wird eine Kopie der Baugruppe und seiner 2D-Zeichnung unter neuem Namen erzeugt." & vbLf & vbLf & _
"Vorschlag für neuen Dateinamen anhand aktuellem Revisionsstand." & vbLf & vbLf & _
"Die aktuelle Assembly wird nicht geschlossen jedoch die Neue zusätzlich geöffnet..." & vbLf & vbLf & _
"Fortfahren?", "Revision Speichern - Baugruppe und Zeichnung Kopieren", NextRevFileName)
If NewFileName = "" Then ' Cancel was hit
    Return
End If

Dim RevText As String = InputBox("Text für Revision " & StrNextRev & " eingeben.", "Was wurde geändert?", iProperties.Value("Custom", "Revision " & CurrRev))

' Perform Save As of Assembly
oDoc.SaveAs(Path & "\" & NewFileName & ".iam", False)

Dim AssemblyDoc As AssemblyDocument = ThisApplication.Documents.Open(Path & "\" & CurrentFilename & ".iam")
iProperties.Value("Project", "Revision Number") = StrNextRev
iProperties.Value("Custom", "Revision " & StrNextRev) = RevText
iProperties.Value("Custom", "Revision " & StrNextRev & " Datum") = Now  'aktuelles Datum
oDoc.Activate()

' Open the current drawing (in same folder location)
Dim DrawingDoc As DrawingDocument = ThisApplication.Documents.Open(Path & "\" & CurrentFilename & DWGType)

' Replace reference to assembly model
Dim oFD As FileDescriptor
oFD = DrawingDoc.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
oFD.ReplaceReference(NewFileName & ".iam")
DrawingDoc.Update()

' Perform 'Save As' on the drawing - change this to reflect your drawing type .dwg or .idw
DrawingDoc.SaveAs(Path & "\" & NewFileName & ".idw", False)

' Make the assembly model active again...
oDoc.Activate()

' End of iLogic code ---------------------------------------------------------------------------------------------
2018-12-14.png

 

0 Likes
848 Views
8 Replies
Replies (8)
Message 2 of 9

HermJan.Otterman
Advisor
Advisor

how do you change the references in you assemblies?... they still look at the old part..

why not use Vault for this?

If this answers your question then please select "Accept as Solution"
Kudo's are also appreciated Smiley Wink

Succes on your project, and have a nice day

Herm Jan


0 Likes
Message 3 of 9

Skanda_UNI
Advocate
Advocate

No i cant use Vault.

It should look at the old part reference and then add one to it.

PUts it in the reverence i property

0 Likes
Message 4 of 9

Skanda_UNI
Advocate
Advocate

Has anyone a solution to this or could help with the problem:

unfortunately the problem does not exist everytime. Sometimes it works sometimes it doesnt and i cant figure out what it depends on. 

Could you provide a linke where i can steal some code in order to bring it to life?

 

Thank you

0 Likes
Message 5 of 9

Skanda_UNI
Advocate
Advocate

I still have the same problem. It asks for the name of the Revision and also for the Revision text. Then it makes the save as of the current document with the right name of course. Then it opens the corresponding drawing and before it can make a save as of the drawing and exchaning the reverence, this message is poping up. 

Please can anyone help with this issue or tell me which part of the code i have to look  at in order to fix this. I assume it could be just a fragment of old code as there are things changing constantly and this code is quite old.

 

Thank you for your help. Any suggestions welcome. Except, i cant use Vault.

Skanda_UNI_0-1594888400746.png

Skanda_UNI_1-1594888501330.png

 

 

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor

Ich bin nicht sicher, ob dies ist, wo das Problem es, aber es kann helfen.

Gemäß der Fehlermeldung wird der Code in der Zeile angehalten, in der Sie 'ReplaceReference' verwenden, aber das könnte viele Dinge bedeuten, da der gesamte komplexe Code verwendet wird, um den verwendeten Namen zu beeinflussen.

In Ihrem Code, in dem Sie zuerst "CurrRev" und "NextRev" definieren", definieren Sie CurrRev als String und dann NextRev eine ganze Zahl. Dann versuchen, mathematisch 1 zur Zeichenfolge CurrRev hinzuzufügen. Dies kann später zu einem Fehler führen.

Versuchen Sie, CurrRev entweder auch als Ganzzahl definieren zu können, oder verwenden Sie beim Festlegen des Werts von NextRev die Verwendung von "(CInt(CurrRev)+1)".

Sehen Sie, ob das etwas behebt.

Ich entschuldige mich, wenn etwas in meinem Post nicht richtig scheint, denn Deutsch ist nicht meine Hauptsprache.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

WCrihfield
Mentor
Mentor

I'm not sure if this is where the problem is, but it can help.

According to the error message, the code stops on the line where you are using ReplaceReference, but that could mean many things because all the complex code is used to affect the name you are using.

In your code, where you first define CurrRev and NextRev, you define CurrRev as a string, and then NextRev defines an integer. Then try to mathematically add 1 to the string CurrRev. This may result in an error later.

Try either to define CurrRev as an integer, or use the "CInt(CurrRev)+1)" when setting the value of NextRev.

See if that fixes anything.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 8 of 9

Skanda_UNI
Advocate
Advocate

Hello! Thanks for the german answer, pretty impressive... 😉

I changed what you said. I use CurrRev as an integer and NextRev as an Integer but it doesnt work and i have another problem too.

In every part there is a custom iProperty that says: Revision 00 with text as value.
This is so that there can be something added to the generate Revision 01.
This should have this format "0#" which is defined in line 44. but when i run the code it says that there is no iproperty with value 0. of course not as it has the value 00. if i change it to 0 than the code works till it runs into the same problem. When making the save as of the drawing it stops.

Annotation 2020-08-12 085857.jpg

 

' Start of iLogic code -------------------------------------------------------------------------------------

' Note this iLogic rule takes a copy of the active part and associated drawing file
' ...then associates the drawing with the newly copied assembly.

' This rule is a variation on a previous blog by Luke Davenport located here:
' https://www.cadlinecommunity.co.uk/hc/en-us/articles/203347701-Inventor-2016-iLogic-Copy-Assembly-Model-and-Drawing

' This rule allows any name to be input for the copied drawing and assembly, the code in the blog linked above
' ...allows only a suffix to be entered.

' Set drawing extension you are using here by commenting out as required.
'Dim DWGType As String = ".dwg"
Dim DWGType As String = ".idw"

Dim oDoc As PartDocument = Nothing

Try
oDoc = ThisApplication.ActiveEditDocument
Catch
MsgBox("Dieses Programm wird in einer Bauteil-Datei ausgeführt! - Exiting...", 64, "Revision Speichern")
Return
End Try

' Get current filename without extension
Dim CurrentFilename As String = System.IO.Path.GetFileNameWithoutExtension(oDoc.FullDocumentName)
' Get current path
Dim Path As String = System.IO.Path.GetDirectoryName(oDoc.FullDocumentName)

' Check that the drawing for this part can be found
If Not System.IO.File.Exists(System.IO.Path.ChangeExtension(oDoc.FullDocumentName, DWGType)) Then
MsgBox("Es konnte keine Zeichnungsdatei gefunden werden die zu diesem Bauteil passt!" & vbLf & vbLf & _
CurrentFilename & DWGType & vbLf & vbLf & _
"Bitte stelle sicher das sich das File im gleichen Ordner befindet, den gleichen Namen hat und die richtige Dateierweiterung '" & DWGType & "'", 64, "Revision Speichern")
Return
End If

'Dim CurrRev As String = Mid(CurrentFilename, 8, 1) 'Gibt den aktuellen Revisionsstand anhand des Zeichnungsnamens wieder

'Dim CurrRev As String = iProperties.Value("Project", "Revision Number") 'Gibt den aktuellen Revisionsstand anhand des iProperties wieder
Dim CurrRev As Integer = iProperties.Value("Project", "Revision Number") 'Gibt den aktuellen Revisionsstand anhand des iProperties wieder
'Dim NextRev As String = Chr(Asc(CurrRev) + 1) 'Stellt den aktuellen Revisionsstand als ASCII Code dar und addiert eins gibt bei A also B wieder
Dim NextRev As Integer = CurrRev +1 'Stellt den aktuellen Revisionsstand einen Wert nach oben
Dim StrNextRev As String = Microsoft.VisualBasic.Strings.Format(NextRev,"0#")
Dim NextRevFileName As String = iProperties.Value("Project", "Stock Number") & "-" & StrNextRev & "_" & iProperties.Value("Project", "Description")

' Prompt user for job number
Dim NewFileName As String = InputBox("Es wird eine Kopie des Bauteils und seiner 2D-Zeichnung unter neuem Namen erzeugt." & vbLf & vbLf & _
"Vorschlag für neuen Dateinamen anhand aktuellem Revisionsstand." & vbLf & vbLf & _
"Der aktuelle Bauteil wird nicht geschlossen jedoch der Neue zusätzlich geöffnet..." & vbLf & vbLf & _
"Fortfahren?", "Revision Speichern - Bauteil und Zeichnung Kopieren", NextRevFileName)
If NewFileName = "" Then ' Cancel was hit
Return
End If

Dim RevText As String = InputBox("Text für Revision " & StrNextRev & " eingeben.", "Was wurde geändert?", iProperties.Value("Custom", "Revision " & CurrRev))

' Perform Save As of Part
oDoc.SaveAs(Path & "\" & NewFileName & ".ipt", False)

Dim PartDoc As PartDocument = ThisApplication.Documents.Open(Path & "\" & CurrentFilename & ".ipt")
iProperties.Value("Project", "Revision Number") = StrNextRev
iProperties.Value("Custom", "Revision " & StrNextRev) = RevText
'iProperties.Value("Custom", "Revision " & StrNextRev & "Datum") = Now 'aktuelles Datum
oDoc.Activate()

' Open the current drawing (in same folder location)
Dim DrawingDoc As DrawingDocument = ThisApplication.Documents.Open(Path & "\" & CurrentFilename & DWGType)

' Replace reference to part model
Dim oFD As FileDescriptor
oFD = DrawingDoc.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
oFD.ReplaceReference(NewFileName & ".ipt")
DrawingDoc.Update()

' Perform 'Save As' on the drawing - change this to reflect your drawing type .dwg or .idw
DrawingDoc.SaveAs(Path & "\" & NewFileName & ".idw", False)

' Make the assembly model active again...
oDoc.Activate()

' End of iLogic code ---------------------------------------------------------------------------------------------

 

0 Likes
Message 9 of 9

WCrihfield
Mentor
Mentor

So your custom iProperty's name is "Revision" and its value will always start out as "Revision 00", right?

Perhaps you could:

  • Capture the value of the iProperty to a string variable (for example oRevSt)
  • Capture the the last two characters of that string to a new Integer type variable using something like
    • Dim oNum As Integer =CInt(Right(oRevSt,2))
  • Add 1 to it
    • oNum = oNum + 1
  • Determine if the result is 1 or 2 digits, then either add a 0 or not when converting back & replacing the original
    • something like the following:
Dim oRevSt As String = iProperties.Value("Custom", "Revision")
Dim oNum As Integer = CInt(Right(oRevSt, 2))
oNum = oNum + 1
Dim oNewRev As String
If oNum > 10 Then
	oNewRev = Replace(oRevSt, Right(oRevSt, 2), "0" & CStr(oNum))
Else
	oNewRev = Replace(oRevSt, Right(oRevSt, 2), CStr(oNum))
End If

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes