Saving new drawing using forum button as same name and location as part.

Saving new drawing using forum button as same name and location as part.

Anonymous
Not applicable
667 Views
8 Replies
Message 1 of 9

Saving new drawing using forum button as same name and location as part.

Anonymous
Not applicable

Hi, I am having trouble writing a rule to save a drawing.  When a part is finished and a new drawing is made then finished, I would like to hit a button that saves the drawing the same as the part name/location without a window popping up. 

 

The problem with my code is that since it is a save as, it creates a file separate to my working drawing.  I was thinking it might be possible if the code closes the active drawing and opens the 'save as' drawing. So far this is what I have:

 

modelFullFileName = ThisDoc.ModelDocument.FullFileName
modelDirectoryName = IO.Path.GetDirectoryName(modelFullFileName)
modelFileName = IO.Path.GetFileName(modelFullFileName)
modelFileNamewithoutextentionsion = IO.Path.GetFileNameWithoutExtension(modelFullFileName)

ThisDoc.Document.SaveAs(modelDirectoryName & "\" & modelFileNamewithoutextentionsion & ".IDW", True)

 

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

mcgyvr
Consultant
Consultant

All right here..

ThisApplication.ActiveDocument.save

https://forums.autodesk.com/t5/inventor-customization/help-save-not-save-as-using-ilogic/td-p/642127...

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
Message 3 of 9

Anonymous
Not applicable

@mcgyvr 

 

The code you suggested would work for a drawing that has already been named and saved.  What I'm looking for is  a solution for a drawing that has not yet been initially named/saved.  The linked part would have the needed name and save location to automate this task.

0 Likes
Message 4 of 9

mcgyvr
Consultant
Consultant

@Anonymous  Did you see the link I provided?

I thought that might help you out..

 

You can change the true to false..

Or Open/Close as needed

 

oDoc = ThisDrawing.Document
modelFullFileName = ThisDoc.ModelDocument.FullFileName
modelDirectoryName = IO.Path.GetDirectoryName(modelFullFileName)
modelFileName = IO.Path.GetFileName(modelFullFileName)
modelFileNamewithoutextentionsion = IO.Path.GetFileNameWithoutExtension(modelFullFileName)

DFileName = modelFileNamewithoutextentionsion & ".idw"

ThisDoc.Document.SaveAs(modelDirectoryName & "\" & DFileName, True)
ThisApplication.Documents.Open(modelDirectoryName & "\" & DFileName, True)
oDoc.Close

 



-------------------------------------------------------------------------------------------
Inventor 2023 - Dell Precision 5570

Did you find this reply helpful ? If so please use the Accept Solution button below.
Maybe buy me a beer through Venmo @mcgyvr1269
Message 5 of 9

Curtis_Waguespack
Consultant
Consultant

Hi @Anonymous 

 

I'm not sure I completely understand your question. As written your code saves off a copy of an unsaved drawing to the same folder as the model and names it the same as the model +.idw

 

If you are looking to save the unsaved drawing and have it remain open, change the true to false in this line.

 

ThisDoc.Document.SaveAs(modelDirectoryName & "\" & modelFileNamewithoutextentionsion & ".IDW", False)

 

modelFullFileName = ThisDoc.ModelDocument.FullFileName
modelDirectoryName = IO.Path.GetDirectoryName(modelFullFileName)
modelFileName = IO.Path.GetFileName(modelFullFileName)
modelFileNamewithoutextentionsion = IO.Path.GetFileNameWithoutExtension(modelFullFileName)

ThisDoc.Document.SaveAs(modelDirectoryName & "\" & modelFileNamewithoutextentionsion & ".IDW", False)

 

Note too that programming questions of this type are better placed on the Inventor Customization forum :
http://forums.autodesk.com/t5/Autodesk-Inventor-Customization/bd-p/120

 

I'll ask the moderators to move this to that forum.

 

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

EESignature

0 Likes
Message 6 of 9

Anonymous
Not applicable

hi @Curtis_Waguespack 

 

Thank you for moving the forum, this is my first thread.  Adding 'FALSE' in place of 'TRUE' returned error:

 

Error in rule: SAVE DRAWING WITH SAME NAME AND LOCATION AS PART, in document: Partfile

Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))

 

You are correct, I am looking to save the unsaved drawing or if it has been saved already, I am looking to have it do a regular save in that case.  I assume this can be done with a simple 'IF' statement.  The problem is I cannot get the unsaved drawing to save as part name/location via form button with no windows popping up.

 

0 Likes
Message 7 of 9

Curtis_Waguespack
Consultant
Consultant

Hi @Anonymous 

 

From that last error message, I think maybe your drawing file already exists on disk and is causing an error.

 

Below is a version of your rule with some error checking.

 

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

 

 

modelFullFileName = ThisDoc.ModelDocument.FullFileName
modelDirectoryName = IO.Path.GetDirectoryName(modelFullFileName)
modelFileName = IO.Path.GetFileName(modelFullFileName)
modelFileNamewithoutextentionsion = IO.Path.GetFileNameWithoutExtension(modelFullFileName)

If modelDirectoryName Is Nothing Then 
	MessageBox.Show("modelDirectoryName is blank!", "ilogic")
	Return 'exit rule
End If

If modelFileNamewithoutextentionsion Is Nothing Then 
	MessageBox.Show("modelFileNamewithoutextentionsion is blank!", "ilogic")
	Return 'exit rule
End If

sName = modelDirectoryName & "\" & modelFileNamewithoutextentionsion & ".IDW"

' check if the file exists 
If System.IO.File.Exists(sName) Then
    MessageBox.Show("File of this name already exists!", "ilogic")
	Return 'exit rule
End If

Try
ThisDoc.Document.SaveAs(sName, False)
Catch ex As Exception
	MessageBox.Show("Problem saving file! " & vbLf & ex.Message, "ilogic")
End Try

 

EESignature

0 Likes
Message 8 of 9

Anonymous
Not applicable

Hi @mcgyvr 

 

I did see the forum, i've been working with your code and curtis's code suggestions.

 

I modified the code from the forum you suggested to fit my scenario a little better.  I removed the initial question from it but I do like that it checks to see if there is a file there first.  The only issue I have now is that it doesn't close the unsaved document.  I end up with the unsaved document open and the 'saved as' document open.  I tried the 'false'/'true' edits on the save as command and it errors when I change it to false.  The close command that I tried also failed.

 

 

modelFullFileName = ThisDoc.ModelDocument.FullFileName
modelDirectoryName = IO.Path.GetDirectoryName(modelFullFileName)
modelFileName = IO.Path.GetFileName(modelFullFileName)
modelFileNamewithoutextentionsion = IO.Path.GetFileNameWithoutExtension(modelFullFileName)


'Dim title As String = "converted to title case"
'    Console.WriteLine(StrConv(title, VbStrConv.ProperCase))
DrawingName = modelFileNamewithoutextentionsion
DesiredPath = modelFileName

NewFileName = DesiredPath & "\" & DrawingName & ".idw"


'check file name to see if it is used
	'if it is, ask the user to overwrite
	'if it's not, save the file

	If System.IO.File.Exists(NewFileName) Then
	
		question = MessageBox.Show("File exists, do you want to overwrite?", "iLogic Question",MessageBoxButtons.YesNo,MessageBoxIcon.Question)
	
			If question = vbYes Then 
				Dim oDrawDoc As DrawingDocument
				oDrawDoc = ThisApplication.ActiveDocument
				oDrawDoc.Save 'Modified Line 1
			Else If question =vbNo Then
				MessageBox.Show("File was not overwritten", "iLogic Message")
			End If
	
	Else 
		
		ThisDoc.Document.SaveAs(NewFileName,True)
	         ThisApplication.Documents.Open(NewFileName,True)'Modified Line 2
	End If 

 

0 Likes
Message 9 of 9

Anonymous
Not applicable

@Curtis_Waguespack 

 

The code you last sent had error in saving file.  I am deleting the drawing before every new try to avoid additional errors.

0 Likes