Saving a DXF to its original path
Saving a DXF to its original path
(OP)
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:
It's that last line that fails. And I cannot figure out why. Any ideas?
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:
Quote (CATIA)
Error: The file has not been correctly created. Please check disk space, privileges, Memory...
CODE --> VBA
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, www.cpearson.com
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?





RE: Saving a DXF to its original path
So, I have no idea why I'm getting...
RE: Saving a DXF to its original path
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 --> CATScript
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 SubRegards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: Saving a DXF to its original path
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 --> VBA
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 SubRE: Saving a DXF to its original path
If you are doing something automatically (without stopping the script) then you don't need an additional text file to store the path and file name, you can do everything in one shot.
Regards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...
RE: Saving a DXF to its original path
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.
RE: Saving a DXF to its original path
CODE --> CATScript
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 SubRegards
Fernando
https://picasaweb.google.com/102257836106335725208
https://picasaweb.google.com/103462806772634246699...