ilogic "Save As" command...

ilogic "Save As" command...

D.Wheeler3GADA
Advocate Advocate
1,249 Views
16 Replies
Message 1 of 17

ilogic "Save As" command...

D.Wheeler3GADA
Advocate
Advocate

Good Morning Forum,

       Is there a way to run an ilogic code that will take the assembly the rule is written in and save it as a different assembly (.iam)? I tried utilizing the "File SaveAs" snippet, but it only allows to save out to certain file extensions like .dwf, .bmp .jpeg ...etc. Perhaps I am looking in the wrong place for the ilogic code, but the "File SaveAs" snippet is all I saw that might have worked. Any help is appreciated. Thanks!

0 Likes
Accepted solutions (4)
1,250 Views
16 Replies
Replies (16)
Message 2 of 17

WCrihfield
Mentor
Mentor
Accepted solution

You can try this simple iLogic sample within a 'local' iLogic rule in an assembly.  I just tried it and it worked for me.

 

oFileName = ThisDoc.PathAndFileName(False)
oFileName = oFileName & " (iLogic copy).iam"
ThisDoc.Document.SaveAs(oFileName, True)

 

The original assembly must have already been saved though, otherwise it will not already have a file name, which will cause the first line to throw an error.

 

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

If you want and have time, I would appreciate your Vote(s) for My IDEAS :bulb: or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 17

D.Wheeler3GADA
Advocate
Advocate

@WCrihfield ,

        Thank you for the code and advice! The code works great on my end. Do you know if I can code this so there is a way to prevent a file overwrite and force a unique filename? 

 

Thanks again.

0 Likes
Message 4 of 17

WCrihfield
Mentor
Mentor

The most common tool that folks use to check if a file already exists in the file system for a FullFileName, is the IO.File.Exists() method, which returns a Boolean.  When using the ThisDoc tool, it will target the 'local' document (the document that the rule is saved within), by default, after that, it gets a little less clear.  Or you could use the ThisApplication.ActiveDocument phrase to specify which document to target, but even that can be tricky at times.  So, its up to you which document reference you want to use at the start of the rule, but if using the ActiveDocument route, I suggest not using the ThisDoc tool for any following purposes, because it might not be pointing to the same document.

Here is an example using entirely Inventor API code, instead of the iLogic shortcut snippets, just for reference.

I mentioned within that if you wanted, you could incorporate an Integer type variable as a Counter to try adding it to the end of the FileName (instead of " (iLogic Copy)"), then when it fails the test, try incrementing the Integer in a loop.  I am not doing that here though.

 

'oDoc = ThisDoc.Document 'the document the rule is saved within, by default
Dim oDoc As Document = ThisApplication.ActiveDocument
oPathAndName = System.IO.Path.ChangeExtension(oDoc.FullFileName, vbNullString)
oExt = System.IO.Path.GetExtension(oDoc.FullFileName)
oNewFullFileName = oPathAndName & " (iLogic Copy)" & oExt
'could create an Integer Counter to add to end of FileName in a loop of Try attempts
If System.IO.File.Exists(oNewFullFileName) Then
	oAns = MsgBox("A file with the following name already exists:" & vbCrLf & _
	oNewFullFileName & vbCrLf & _
	"Do you want to overwrite it?", vbYesNo + vbQuestion + vbDefaultButton2, "File Already Exists")
	If oAns = vbNo Then Exit Sub
End If
oDoc.SaveAs(oNewFullFileName, True)

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 17

D.Wheeler3GADA
Advocate
Advocate

@WCrihfield ,

     Thank you again for the advice. It is a bit more complicated than I was hoping for. The dream was for there to be a way to force the file not to overwrite using ilogic coding. I understand it is more complicated than that, but like I said, that was the dream... 

0 Likes
Message 6 of 17

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @D.Wheeler3GADA 

 

If you're just looking for a rule that saves out a copy as a unique name, but don't particularly care about the name, you could use something like this example that adds the current date/time to the file name, so that it is always unique:

 

 

Imports System.Text.RegularExpressions
oSuffix = "_" & Now() & IO.Path.GetExtension(ThisDoc.PathAndFileName(True))
oSuffix = Regex.Replace(oSuffix, "[:/ ]", String.Empty)
oFileName = ThisDoc.PathAndFileName(False) & oSuffix

If Not System.IO.File.Exists(oFileName) Then	
	ThisDoc.Document.SaveAs(oFileName, True)
End If

 

 

 

Or with a prefix:

 

 

Imports System.Text.RegularExpressions
oPrefix = Now() & "_"
oPrefix = Regex.Replace(oPrefix, "[:/ ]", String.Empty)
oFileName = ThisDoc.Path & "\" & oPrefix & ThisDoc.FileName(True)

If Not System.IO.File.Exists(oFileName) Then	
	ThisDoc.Document.SaveAs(oFileName, True)
End If

 

 

 

If you're wanting to control the naming syntax, then you'd need to provide a bit more information about what you expect the name to look like.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

EESignature

Message 7 of 17

D.Wheeler3GADA
Advocate
Advocate

@Curtis_Waguespack ,

          Thanks again for the help. Basically, I want to be able to save out ilogic driven variations of the same assembly and save the file out with a letter suffix to differentiate between the variations.

 

i.e 45001-103A.iam, 45001-103B.iam, 45001-103C.iam

 

I am not opposed to having to enter the suffix manually as though you were just using "Save Copy As".  I would actually prefer that to automating the suffix addition, but I'll use whatever is functional...

 

I believe the top snippet of code you gave below is 90% of the way there as far as coding it, I would just need to switch out the date/time addition to a alphabetic one.

 

Have a great day!

0 Likes
Message 8 of 17

WCrihfield
Mentor
Mentor

Hi @D.Wheeler3GADA.  Here is something similar to that too.  It basically uses an Array of Characters from the uppercase alphabet letters.  Then loops through them, tries adding one to the end of the current file name, then checks to see if that file already exists.  If that file exists, then it tries the next letter, until it find one that does not already have an existing file.  Then it shows you the file name it chose and asks you if you want to save it with this new name.  The question can be removed if you wanted.  I just put that in there because you sounded a bit insure about the file names using these letters.

 

oFileName = ThisDoc.PathAndFileName(False)
oExt = System.IO.Path.GetExtension(ThisDoc.PathAndFileName(True))
oChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray
For i As Integer = 0 To UBound(oChars)
	oNewFullFileName = oFileName & oChars(i) & oExt
	If System.IO.File.Exists(oNewFullFileName) Then Continue For
	oAns = MsgBox("Do you want to save a copy of this document out" _
	& vbCrLf & "using the following new FullFileName:" & vbCrLf & _
	oNewFullFileName, vbYesNo + vbQuestion + vbDefaultButton1, "SaveCopyAs")
	If oAns = vbNo Then
		Exit Sub 'or use an InputBox to let user edit this name before save
	Else
		ThisDoc.Document.SaveAs(oNewFullFileName, True)
		Exit Sub
	End If
Next

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 9 of 17

Curtis_Waguespack
Consultant
Consultant

H1 @D.Wheeler3GADA 

In addition to WCrihfield's reply, here is a version that increments the suffix from A to Z. 

 

If you go beyond Z, then it gives you a message that it can not save.

 

If you have a need to go beyond Z, then we'd need to know what you want the suffix to look like.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

 

Sub Main
	Dim oSuffix As Char = "A"

	TryAgain :
	oFileName = ThisDoc.PathAndFileName(False) & _
		oSuffix & _
		IO.Path.GetExtension(ThisDoc.PathAndFileName(True))

	If Not System.IO.File.Exists(oFileName) Then
		ThisDoc.Document.SaveAs(oFileName, True)
	Else
		If oSuffix = "Z" Then
			MsgBox("Can not save, suffix is beyond Z",,"iLogic")
			Exit Sub
		End If
		
		oSuffix = IncrementSuffix(oSuffix)
		If oConfirm = vbCancel Then Exit Sub
		GoTo TryAgain
	End If
End Sub

Function IncrementSuffix(oSuffix As Char)

	oSuffix = Chr(Asc(oSuffix) + 1)
	Return oSuffix

End Function

 

 

EESignature

0 Likes
Message 10 of 17

Curtis_Waguespack
Consultant
Consultant

Hi @D.Wheeler3GADA 

 

Building on @WCrihfield's example:

 

This version increments from A to Z...

and beyond Z from AA to AZ...

and beyond AZ from AAA to AAZ...

and so on.

 

i.e:

45001-103A.iam, 45001-103B.iam, 45001-103C.iam ... to 45001-103Z.iam

45001-103AA.iam, 45001-103AB.iam, 45001-103AC.iam ... to 45001-103AZ.iam

45001-103AAA.iam, 45001-103AAB.iam, 45001-103AAC.iam ... to 45001-103AAZ.iam

and so on.

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

oFileName = ThisDoc.PathAndFileName(False)
oExt = System.IO.Path.GetExtension(ThisDoc.PathAndFileName(True))
oChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray

TryAgain :
For i As Integer = 0 To UBound(oChars)
	oSuffix = oChars(i)
	oNewFullFileName = oFileName & oSuffix & oExt

	If Not System.IO.File.Exists(oNewFullFileName) Then
		ThisDoc.Document.SaveAs(oNewFullFileName, True)
		Exit Sub
	Else
		If oChars(i) = "Z" Then
			oFileName = oFileName & "A"
			GoTo TryAgain
		End If
		Continue For
	End If
Next

 

 

 

EESignature

Message 11 of 17

D.Wheeler3GADA
Advocate
Advocate

@WCrihfield 

 Is there a way to get the routine below to end without selecting "NO" ? My application will only need to run the routine once to save out the newly created assembly to the new suffix and then exit. Thank you again for all of your coding help! By the time I actually get into training, I'll have some great practical experience under my belt...

0 Likes
Message 12 of 17

WCrihfield
Mentor
Mentor
Accepted solution

Yes.  That was my hasty mistake.  I forgot to put "Exit Sub" on the next line after the SaveAs line.  Adding that little bit of code to the rule would have caused it to exit the rule once it has saved the document.  Without that in there, it will continue to loop through the alphabet and asking the question each time that file is not found.  Sorry about that.  I noticed it after I saw that bit of code in @Curtis_Waguespack proposed solution, but had not gone back and edit it yet.  I will though.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 13 of 17

D.Wheeler3GADA
Advocate
Advocate

@WCrihfield 

          That worked just swimmingly. With your help, I have nearly accomplished what I had hoped for. I have 1 more thing I would like to accomplish with ilogic:

 

Is there a way to replace the assembly in the model with the new "suffix-ed" assy that the rule just created? This could either be a part of the "save as" rule, or a separate rule I can launch after running the "save as" rule. 

 

Out of curiosity, is there a way to use ilogic to replace a certain component by naming it in the routine and then launching the browser to select the replacement part from the same folder? (I am thinking ahead to some future coding I'd like to tackle)

 

Thanks!!!

 

0 Likes
Message 14 of 17

WCrihfield
Mentor
Mentor
Accepted solution

Hi @D.Wheeler3GADA.  I'm not sure I understand what you mean by "replace the assembly in the model with the new "suffix-ed" assy that the rule just created?".  Are you using this code as an external iLogic rule, or as a 'local' rule (saved within a document)?  When you use the SaveAs line, and you specify True as the second input variable, it does a SaveCopyAs command, which leaves the original document open, and generates a copy of it out to the file system.  If we specified False within the SaveAs line, then the newly created copy would be the 'active' one.  Is that what you mean?  Or are you talking about literally deleting all components out of the 'active' assembly, after the SaveCopyAs, then inserting the suffixed copy of the assembly as a component within the 'active' assembly?

 

The second question is totally doable by code, but if utilizing the browser to select a replacement, may not end up being more efficient than the manual tool.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 15 of 17

D.Wheeler3GADA
Advocate
Advocate

@WCrihfield 

     This accomplishes EVERYTHING I set out to do with these assemblies! I am grateful for your sharing of coding knowledge with a greenhorn like myself. I am learning a ton while working on these automation tasks here at work. Have a great day and an awesome weekend!

0 Likes
Message 16 of 17

donnie.morris
Enthusiast
Enthusiast

Can you make it "SaveAs" the part number?

0 Likes
Message 17 of 17

WCrihfield
Mentor
Mentor

Hi @donnie.morris.  I'm not sure about the overall task you may be referring to, but would something simple like this work for you?

oDoc = ThisDoc.Document
oPath = ThisDoc.Path
'oPath = System.IO.Path.GetDirectoryName(oDoc.FullFileName)
oExt = System.IO.Path.GetExtension(oDoc.FullFileName)
Dim oPN As String = iProperties.Value("Project", "Part Number")
If oPN = "" Then Exit Sub
oFileName = oPN & oExt
oNewFullFileName = System.IO.Path.Combine(oPath, oFileName)
If System.IO.File.Exists(oNewFullFileName) Then
	oAns = MsgBox("A file with the following name already exists:" & vbCrLf & _
	oNewFullFileName & vbCrLf & _
	"Do you want to overwrite it?", vbYesNo + vbQuestion + vbDefaultButton2, "File Already Exists")
	If oAns = vbNo Then Exit Sub
End If
oDoc.SaveAs(oNewFullFileName, True)

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes