Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving a DXF to its original path

Status
Not open for further replies.

2briancox

Aerospace
Dec 22, 2014
34
US
I have been working on a solution that allows a user to save a DXF file to where it had been originally opened. CATIA immediately forgets the PATH for any DXF file, instead it recalls the document name to be CATDrawing1.CATDrawing or something of that sort.

To add this functionality, I created a VBA script that successfully allows the user to choose a DXF file to open, then it opens that file, then it opens a TXT file and saves the name and PATH to that text file.

I then have another script which attempts to check the ActiveDocument and look up its CATIA.ActiveWindow.Name (title of the window it is in) in the TXT file and then grab the PATH from the TXT file so I can pefrom: drawingDocument1.ExportData strFilePath, "dxf"

Unfortunately, when I get to the very end, I get an error:
CATIA said:
Error: The file has not been correctly created. Please check disk space, privileges, Memory...

Code:
Option Explicit
Sub CATMain()
    Dim openDXFRecord As File
    Dim iStream As TextStream
    Dim alltext() As String
    Dim strFilePath As String
    Dim strFileName As String
    Dim test As Boolean
    Dim x As Long
    Dim documents1 As Documents
    Dim dump As Variant
    
    
    Set documents1 = CATIA.Documents
    Dim drawingDocument1 As DrawingDocument
    Set drawingDocument1 = CATIA.ActiveDocument
    strFileName = CATIA.ActiveWindow.Name
    
    Call CleanFileList.CATMain
    
    test = True
    Set openDXFRecord = CATIA.FileSystem.GetFile("C:\temp\OpenDXFlist-19657621943.txt")
    Set iStream = openDXFRecord.OpenAsTextStream("ForReading")
    Do While test
        If ArrayMod.IsArrayAllocated(alltext) Then
            Call ArrayMod.ChangeBoundsOfArray(alltext, 1, UBound(alltext) + 1)
        Else
            ReDim alltext(1 To 1)
        End If
        alltext(UBound(alltext)) = iStream.ReadLine
        If iStream.AtEndOfStream Then
            test = False
        End If
    Loop
    iStream.Close

    ''' THIS is where I begin checking the filename for the path. It works correctly until the last line. All variables have the values I would expect until the end of this script. All external function and subroutine calls work fine.  The function ArrayMod is modArraySupport By Chip Pearson, chip@cpearson.com, [URL unfurl="true"]www.cpearson.com[/URL]

    If ArrayMod.IsArrayEmpty(alltext) Then
        dump = MsgBox("The current file was not opened with the 'OpenDXF' Feature.", , "File Error:")
        Exit Sub
    End If
    strFilePath = ""
    For x = 1 To UBound(alltext) Step 2
        If strFileName = Left(alltext(x), Len(strFileName)) Then
            strFilePath = Left(alltext(x + 1), Len(alltext(x + 1)) - 1)
            x = UBound(alltext)
        End If
    Next
    If strFilePath = "" Then
        dump = MsgBox("The current file was not opened with the 'OpenDXF' Feature.", , "File Error:")
        Exit Sub
    End If
    drawingDocument1.Activate
    drawingDocument1.ExportData strFilePath, "dxf"
    
End Sub

It's that last line that fails. And I cannot figure out why. Any ideas?
 
Replies continue below

Recommended for you

I should mention, I am able to perform the document1.ExportData strFilePath, "dxf" command in other scripts that save a file that had been opened from THE SAME VBA script that is trying to export it. Also, I can manually export the file that was giving me an error. I just can't do it in VBA within this particular script.

So, I have no idea why I'm getting...

CATIA said:
Error:
The file has not been correctly created.
Please check disk space, privileges, Memory...
 
Hi,

I suppose you are using "BrowseForFolder" method to impose to the user from where he has to open the dxf file.

In this case I would suggest to store that path and use it later. An example bellow, in CATScript.

Code:
Sub CATMain()

Const WINDOW_HANDLE = 0
Const NO_OPTIONS = 0
Set objShell = CreateObject("Shell.Application") 
Set objFolder = objShell.BrowseForFolder _
 (WINDOW_HANDLE, "Select a folder:", NO_OPTIONS, "C:\") 
Set objFolderItem = objFolder.Self 
objPath = objFolderItem.Path
MsgBox objPath

End Sub

Regards
Fernando

 
Fernando,

I was not using that method but I would love to know more about it. Does CATIA recall the definition of that object once the script has "Open File" Script has finished running? I know Global Variables are lost once their defining script has finished.

I'm a bit new to VBA and haven't even used the CreateObject function before. I can't find it in the book I have by Ziethen. I assume it's a standard VBA function?

My method for opening the DXF was as such:
Code:
Option Explicit

Sub CATMain()
    
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' This script allows the user to chose 1 DXF file to open and opens it in the new Managed DXF process.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    Call CleanFileList.CATMain
    
    Dim dump As Variant
    Dim strFilePath As String
    Dim documents1 As Documents
    Dim document1 As Document

    Set documents1 = CATIA.Documents

    strFilePath = CATIA.FileSelectionBox("Select DXF file:", "*.dxf", CatFileSelectionModeOpen)
    If strFilePath = "" Then
        dump = MsgBox("No DXF file was chosen.", , "File Error")
        Exit Sub
    End If

    Set document1 = documents1.Open(strFilePath)
    Call AddFileList.CATMain(document1.Name, strFilePath) 'Script to update the file list of DXF's currently opened with this managed method.  It's been debugged and seems to work perfectly.
        
End Sub
 
Unfortunately, this part of the process must be manually performed. This is where they perform Sketch Analysis and manually fix any broken geometry. That's why I'm using the text file.

But do you have any idea why my solution won't work? It fails on drawingDocument1.ExportData strFilePath "dxf" with a reason I do not understand.
 
I didn't look at your code but you can compare with what I'm doing in CATScript to read a txt file line by line

Code:
Sub CATMain()

Dim objFSO, strTextFile, strData, strLine, arrLines
CONST ForReading = 1

'name of the text file
strTextFile = "c:\temp\test.TXT"

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject") '<<< 

'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

'Split the text file into lines
arrLines = Split(strData,vbCrLf)

'~ 'Step through the lines
For Each strLine in arrLines
'~ ''''''''''''''''''''''''''''''''''''''''
MsgBox strLine
Next
'Cleanup objFSO
Set objFSO = Nothing

End Sub

Regards
Fernando

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top