Ilogic rule: save as ACAD Dwg but how to overwrite existing file

Ilogic rule: save as ACAD Dwg but how to overwrite existing file

J.Troquay
Contributor Contributor
2,180 Views
8 Replies
Message 1 of 9

Ilogic rule: save as ACAD Dwg but how to overwrite existing file

J.Troquay
Contributor
Contributor

I have a functional ilogic rule to export my inventor dwg/idw as an Autocad file in a dedicated folder.

What I don't get to work is to overwrite the existing Acad file if it allready existst. In that case the rule does nothing.

Meaning I have to delete the existing file by hand and then run the rule again.

remark: strangly enough I use the same code to export to Pdf, and here the problem does not exist.

 

However: what do I need to do to get it to overwrite, preferrably without an extra question as in "the file exists; overwrite y/n" as we would want to overwrite by default.

 

here my Original irule:

 

Sub Main()
  
  question = MessageBox.Show("Wilt u een AutoCad kopie opslaan?", "AutoCad kopie",MessageBoxButtons.YesNo,MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
  If question = vbYes Then
  
  'Setup Translator to dwg
  Dim DWGAddIn As TranslatorAddIn = ThisApplication.ApplicationAddIns.ItemById("{C24E3AC2-122E-11D5-8E91-0010B541CD80}")
 
  Dim oContext As TranslationContext = ThisApplication.TransientObjects.CreateTranslationContext
  oContext.Type = kFileBrowseIOMechanism
 
  ' Create a NameValueMap object
  Dim oOptions As NameValueMap = ThisApplication.TransientObjects.CreateNameValueMap
 
  ' Create a DataMedium object
  Dim oDataMedium As DataMedium = ThisApplication.TransientObjects.CreateDataMedium

  'get DWG target folder path
  oPath = ThisDoc.Path
  oFolder = oPath & "/ACAD"
 
  'Check for the DWG folder and create it if it does not exist
  If Not System.IO.Directory.Exists(oFolder) Then
   System.IO.Directory.CreateDirectory(oFolder)
  End If

   If ThisDrawing.Document.DocumentType = kDrawingDocumentObject Then
    'Get the directory file is saved in. Can replace this with specific directory
    Dim dwgDir = SysIO.Path.GetDirectoryName(ThisDrawing.Document.FullFileName)
    'Get name of file without the extension and add _acad2k to it.
    oDataMedium.MediumType = kFileNameMedium
    oDataMedium.FileName = dwgDir & "/ACAD" & "\\" & _
                                    SysIO.Path.GetFileNameWithoutExtension(ThisDrawing.Document.FullFileName) & _
                                     "_acad.dwg"
    ' Check whether the translator has 'SaveCopyAs' options
    If DWGAddIn.HasSaveCopyAsOptions(ThisDrawing.Document, oContext, oOptions) Then
     'Use Export To DWG to save drawing configuration and set here
     Dim strIniFile As String = "P:\_VIRO ZME\inventor\macro\Inventor rules\save_to_ACAD_dwg.ini"
     ' Create the name-value that specifies the ini file to use.
     oOptions.Value("Export_Acad_IniFile") = strIniFile
     'Save File
     DWGAddIn.SaveCopyAs(ThisDrawing.Document, oContext, oOptions, oDataMedium)
    End If
   End If
  End If
End Sub

 

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

philip1009
Advisor
Advisor

First I'll let you know that coding and other related questions like this are better suited for the Inventor Customization forum.

 

If you always want to overwrite by default, then I suggest adding another If statement before doing the save file to check if a file of the same name already exists and if so delete that file, hopefully it doesn't generate any pop-ups.

 

SyntaxEditor Code Snippet

If System.IO.File.Exists("C:\File.dwg") = True Then System.IO.File.Delete("C:\File.dwg")

 UPDATE:  I just did a test myself, it deleted the file without any pop-ups, however I did notice that files deleted from the local drive don't go to the Recycle Bin like they usually do, so be careful.

0 Likes
Message 3 of 9

J.Troquay
Contributor
Contributor

I tried the delete and save script and this works. But: this works for single sheet drawings. With Multi sheet you get _sheet 1, sheet_2 suffix which makes it more complex.

Also i discovered that dwg will be overwritten without notice if the file is on my local drive. As soon as it is on our server this is not the case.

0 Likes
Message 4 of 9

philip1009
Advisor
Advisor

I think the multi-sheet thing can be fixed with another oOptions.Value line telling the code an option we're not aware of yet, it's strange that it exports to multiple dwgs when there isn't an option to do so if the export is done manually.  I'll do some digging to find the full list of options for dwg export.

 

As for the notification from network, can you post a screenshot of that.  It seems like a windows thing, if I delete a dwg file from my network location with the same iLogic code it doesn't raise a fuss.

0 Likes
Message 5 of 9

J.Troquay
Contributor
Contributor
Delete with the irule over network works fine. That's no issue. Just doing a save locally (without fist deleting the file) does overwrite the file, as on the network it does not.

Thanks in advance for looking into the oOptions options. Very helpful.
0 Likes
Message 6 of 9

philip1009
Advisor
Advisor

Here's a webpage of someone detailing a rule for listing save as options for translators, you'll just have to convert it to work in iLogic and to reference the DWG translator instead of the DWF translator:

http://adndevblog.typepad.com/manufacturing/2014/02/get-option-names-and-values-supported-by-invento...

 

I'd also look at the .ini file in Notepad or similar text program to see if there's a multi-sheet option that can be changed.

 

I'll see if there's a whole other method to saving as a dwg since doing the manual export doesn't have the multi-sheet issue.

0 Likes
Message 7 of 9

Anonymous
Not applicable

Hi i'm looking into doing the same thing, please upload an example, i have difficulties, implementering the delete function. 

0 Likes
Message 8 of 9

J.Troquay
Contributor
Contributor
Accepted solution

 

following a part of the code where the file is deleted if existing.

 

'Set the DWG target file name
oDataMedium.FileName = oNewfilename

DWGerror=False
On Error Goto handleDWGLock

' Check whether the translator has 'SaveCopyAs' options
If DWGAddIn.HasSaveCopyAsOptions(ThisDrawing.Document, oContext, oOptions) Then
'Use Export To DWG to save drawing configuration and set here
Dim strIniFile As String = "<type filepath here>\save_to_ACAD_dwg.ini"
' Create the name-value that specifies the ini file to use.
oOptions.Value("Export_Acad_IniFile") = strIniFile
'Delete existing file if it exists
If SysIO.File.Exists(oDataMedium.FileName) = True Then
SysIO.File.Delete(oDataMedium.FileName)
End If
'Save File
DWGAddIn.SaveCopyAs(ThisDrawing.Document, oContext, oOptions, oDataMedium)
End If

0 Likes
Message 9 of 9

philip1009
Advisor
Advisor
Accepted solution

Here a line of code that will delete a file from your hard drive, if this doesn't work, then you need to talk with your IT person about permissions to delete files from that folder:

 

sFile = "C:\testdrawing.idw"
If System.IO.File.Exists(sFile) = True Then System.IO.File.Delete(sFile)

 

0 Likes