Opening, editing and saving dxf files

Opening, editing and saving dxf files

Anonymous
Not applicable
3,776 Views
4 Replies
Message 1 of 5

Opening, editing and saving dxf files

Anonymous
Not applicable

Hi all,

 

I've got a pretty simple question. I'm trying to open a DXF file, then edit and close the same file as part of a sheet metal routine... The problem I'm having is saving the DXF file I'm editing... I keep getting errors using the .Save or .SaveAs functions. I expect it's because AutoCAD is trying to persuade me to save in .dwg format and/or because the file I'm saving as is open.

 

Is there a way to just save the file I'm editing without specifying a new filename, or do I need to save the file as a temporary dwg file, then export to dxf (deleting the old dxf file if required)?

 

Thanks in advance.

 

-G

0 Likes
Accepted solutions (1)
3,777 Views
4 Replies
Replies (4)
Message 2 of 5

Ed__Jobe
Mentor
Mentor

When you open a dxf, Autocad automatically converts it to a dwg. So, a SAVE command saves as dwg. You should be able to SAVEAS, overwriting the previous dxf.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 3 of 5

Anonymous
Not applicable

Thanks Ed... 

 

I think you're right... but it seems that AutoCAD opens the dxf as a dwg into memory (not disk), so it still has the dxf open and locked on disk. When I try to write to the dxf file I get an error - "Error saving the document", but maybe I'm doing something else wrong...

 

Here's  a condensed version of the code:

 

 

Sub CleanAndClose()

Dim Drawing As AcadDocument

Set Drawing = Application.ActiveDocument

Drawing.SaveAs Left(Drawing.FullName, Len(Drawing.FullName) - 4) & ".dxf", ac2000_dxf

End Sub

 

This results in "error saving the document". I suspect it's a write permissions error... If I change the filename then it works OK... 

 

0 Likes
Message 4 of 5

Ed__Jobe
Mentor
Mentor

Your function name indicates that you are going to be closing the dwg anyways, so, how about trying this: save to a temp file, close the current file, then rename the temp file to the original file name.

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 5 of 5

Anonymous
Not applicable
Accepted solution

Ok, Well incase someone else comes looking. I caved, and just saved the file as a DWG in the temp location, then I delete the file when I'm done... I haven't tested it extensively yet, but it seems to do the trick.

 

Sub CleanAndClose()

Dim Drawing As AcadDocument
Dim SS As AcadSelectionSet
Dim FilterType(0) As Integer
Dim FilterData(0) As Variant

On Error GoTo SubErr

Set Drawing = Application.ActiveDocument

For Each SS In Drawing.SelectionSets
    If SS.Name = "SS" Then
        SS.Delete
    End If
Next

Set SS = Drawing.SelectionSets.Add("SS")
FilterType(0) = 8                   'DXF Code for layers
FilterData(0) = "JUNK"              'Layer Name

SS.Select acSelectionSetAll, , , FilterType, FilterData

SS.Erase

ZoomExtents

'Need to save file in temp location, before exporting dxf
Dim TempFilePath As String
Dim DXFFilePath As String

TempFilePath = Environ("Temp") & "\" & Left(Drawing.Name, Len(Drawing.Name) - 4) & ".dwg"
DXFFilePath = Left(Drawing.FullName, Len(Drawing.FullName) - 4) & ".dxf"

Drawing.SaveAs TempFilePath
Drawing.SaveAs DXFFilePath, ac2000_dxf
Drawing.Close False

DeleteFile TempFilePath

SubExit:
Exit Sub

SubErr:
'Need to check to see if a temp file has been created and delete it if it has.
If TempFilePath <> "" Then
    modSupport.DeleteFile TempFilePath
End If
End Sub

Function FileExists(ByVal FileToTest As String) As Boolean
'http://stackoverflow.com/questions/67835/deleting-a-file-in-vba
   FileExists = (Dir(FileToTest) <> "")
End Function

Sub DeleteFile(ByVal FileToDelete As String)
'http://stackoverflow.com/questions/67835/deleting-a-file-in-vba
   If FileExists(FileToDelete) Then 'See above
      ' First remove readonly attribute, if set
      SetAttr FileToDelete, vbNormal
      ' Then delete the file
      Kill FileToDelete
   End If
End Sub
0 Likes