"Save Copy As" Multi-Sheet Drawing to AutoCAD DWG using VBA

"Save Copy As" Multi-Sheet Drawing to AutoCAD DWG using VBA

Anonymous
Not applicable
2,708 Views
8 Replies
Message 1 of 9

"Save Copy As" Multi-Sheet Drawing to AutoCAD DWG using VBA

Anonymous
Not applicable

We export copies of our IDW files to AutoCAD DWGs.  These files get processed in AutoCAD and added to some of our customer documentation.  I'm trying to write a macro in Inventor 2016 using VBA to export ("Save Copy As") the current file to an AutoCAD DWG.  We do some folder organization based off our part numbering system and the macro will find the correct folder automatically (that part is working fine).

 

The issue I'm having is that when the current document is a multi-sheet drawing, the macro still brings up a file dialog for each sheet and asks the user to give it a name.  I'm fine with the default suffix of "_Sheet1" or whatever it decides to do.  I just don't want the user to be prompted with the file dialog.  In addition, the file will usually be saving over top of an existing file and will also be saving outside of the current project.  I don't want any of the associated messages popping up during execution of the macro.

 

My code is as follows:

Sub PartsBookBOMColumnsAndExport()
    ' Set a reference to the drawing document.
    ' This assumes a drawing document is active.
    Dim DrawingRef As DrawingDocument
    Set DrawingRef = ThisApplication.ActiveDocument
    
    ' Export (Save Copy As) the drawing as an AutoCAD DWG
    ' Get the filename of the current drawing and extract just the filename without the extension
    CurrentFilename = DrawingRef.FullFileName
    
    Dim FilepathParts() As String
    FilepathParts() = Split(CurrentFilename, "\")
    CurrentFilename = FilepathParts(ArrayLen(FilepathParts) - 1)
    CurrentFilename = Left(CurrentFilename, Len(CurrentFilename) - 4)
    
    ' Extract the series number and the first two digits of the assembly number
    Dim SeriesNum As String, AssyNum As String, TargetFilepath As String
    SeriesNum = Left(CurrentFilename, 2)
    AssyNum = SeriesNum + "-" + Mid(CurrentFilename, 3, 2) + "00"
    
    ' Check if the proper folders exist and create them if they don't
    MsgBox Dir("T:\Inventor DWG\" + SeriesNum + "\")
    If Dir("T:\Inventor DWG\" + SeriesNum + "\") = "" Then
        MkDir "T:\Inventor DWG\" + SeriesNum
    End If
    If Dir("T:\Inventor DWG\" + SeriesNum + "\" + AssyNum + "\") = "" Then
        MkDir "T:\Inventor DWG\" + SeriesNum + "\" + AssyNum + "\"
    End If
    
    ' Save the drawing as an AutoCAD DWG
    TargetFilepath = "T:\Inventor DWG\" + SeriesNum + "\" + AssyNum + "\" + CurrentFilename + ".dwg"
    Call DrawingRef.SaveAs(TargetFilepath, True)
    
End Sub

I've been searching for other methods to use than SaveAs(), but I can't seem to find any that don't have the same behavior.

0 Likes
Accepted solutions (1)
2,709 Views
8 Replies
Replies (8)
Message 2 of 9

philip1009
Advisor
Advisor

Just to double check, does the SaveAs() methods include the SaveAsInventorDWG() method?  I'm on 2019 so I won't know what's available or not in 2016.

 

Have you tried using export methods by calling Add-Ins for publishing?  That would be the next possible method for multi-sheet drawings.

0 Likes
Message 3 of 9

Anonymous
Not applicable

That method is included in Inventor 2016, yes.  Just FYI we are going to be updating to 2019 within the next week or two so the version difference shouldn't be an issue long term.

 

I have not looked into that.  How would I find out how to use that method?

0 Likes
Message 4 of 9

philip1009
Advisor
Advisor
Accepted solution

I'm sure you're aware of the API guide that comes with Inventor:

API Help_edited.jpg

 

Under the Contents tab go to Sample Programs > Translators > Export, there's some examples including the Export to DWG sample:

 

SyntaxEditor Code Snippet

' Get the DWG translator Add-In.
Dim DWGAddIn As TranslatorAddIn
DWGAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")

'Set a reference to the active document (the document to be published).
Dim oDocument As Document
oDocument = ThisApplication.ActiveDocument

Dim oContext As TranslationContext
oContext = ThisApplication.TransientObjects.CreateTranslationContext
oContext.Type = kFileBrowseIOMechanism

' Create a NameValueMap object
Dim oOptions As NameValueMap
oOptions = ThisApplication.TransientObjects.CreateNameValueMap

' Create a DataMedium object
Dim oDataMedium As DataMedium
oDataMedium = ThisApplication.TransientObjects.CreateDataMedium

' Check whether the translator has 'SaveCopyAs' options
If DWGAddIn.HasSaveCopyAsOptions(oDocument, oContext, oOptions) Then

	Dim strIniFile As String
	strIniFile = "C:\tempDWGOut.ini"
	' Create the name-value that specifies the ini file to use.
	oOptions.Value("Export_Acad_IniFile") = strIniFile
End If

'Set the destination file name
oDataMedium.FileName = "c:\tempdwgout.dwg"

'Publish document.
Call DWGAddIn.SaveCopyAs(oDocument, oContext, oOptions, oDataMedium)

 

However there's no guide I'm aware of to list all of the options that can be specified.  After looking at the sample, take a further look around the forums.  If I find a good example listing all possible options I'll post here.

0 Likes
Message 5 of 9

Anonymous
Not applicable

This seems to be working.  Thanks so much for your assistance!

 

One other thing.  I get the following error when trying to save directly to my C drive:error.PNG

I assume that's because you need administrator permission to save files there.  This was just for testing purposes and the final file location where I'll be exporting these files is not administrator protected.  I can copy files directly to my C drive, but I get a pop-up message asking for permission before proceeding.  I am worried that I may need to export files to a folder with administrator restriction at some point.  Do you know a way to allow Inventor to export to these file locations using this method?  It's not mission-critical, but I figured I'd ask in case it becomes important later on down the line.

 

0 Likes
Message 6 of 9

philip1009
Advisor
Advisor

If it's something to do with folder permissions or admin privileges, then Inventor or iLogic can't help with that, you'll have to go to your IT admin for help with that.  You should be running Inventor with admin privileges already so the software doesn't get limited.  Have you tried directing the save to another folder such as your desktop or documents?

0 Likes
Message 7 of 9

Anonymous
Not applicable

I have not tried directing it to a different folder on my local drive.  The location I'm currently (successfully) exporting files to is a network server mapped to a drive letter.

0 Likes
Message 8 of 9

philip1009
Advisor
Advisor

If it's exporting correctly to a network drive, then it is IT limiting your file writing capabilities for security purposes, not a problem with the Inventor Add-In.  Saving directly in the root of the C drive is usually locked by IT since that's how malware and other security flaws can get into the system.

0 Likes
Message 9 of 9

Anonymous
Not applicable

Okay, fair enough.  Thanks for your help!

0 Likes