There are multiple methods for exporting information to .TXT notepad files while running your CATIA macro. The first example is from my Export Spec Tree code found in the CATIA Macro Pack. The data is exported directly to .TXT but we’re not modifying anything other than the file name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | Language="VBSCRIPT" ‘Export the specification tree ‘by Emmett Ross Sub CATMain() Dim productDocument1 As Document Set productDocument1 = CATIA.ActiveDocument 'Input box to select txt or xls Dim exportFormat As String exportFormat = Inputbox ("Please choose format to export the tree as._ Type either 'xls' or 'txt'") IF exportFormat <> "xls" THEN IF exportFormat <> "txt" THEN MsgBox "Did not enter txt or xls. Program cancelled please retry macro." Else 'Input box to enter name of file Dim partName As String partName = Inputbox ("Please enter the file name.") 'Input box to enter file location Dim oLocation As String oLocation = "C:\Macro Files\" productDocument1.ExportData oLocation & partName & "." & _ exportFormat,"txt" End If End If End Sub |
The second option to export to TXT is a bit more dynamic as it allows you to write information to the .txt file at any point during your program. The following code illustrates how we can use the FileSystemObject to return a TextStream object that can be read from or written to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 'Macro to Export CATIA Data to TXT File Public poFileSys As Scripting.FileSystemObject Public poLogStream As Scripting.TextStream Sub CATMain() '--- Create logfile Set poFileSys = CreateObject("Scripting.FileSystemObject") Set poLogStream = poFileSys.CreateTextFile("C:\Users\DatatoFile.txt", True) poLogStream.WriteLine ("--- Walking down the Product Tree ---" & vbLf) poLogStream.Write ("Product: " & oInProduct.Name & " (" & oInstances.Count & " comps)" & vbCrLf) poLogStream.Write (vbTab & i & ": " & _ "InstName(" & oInst.Name & "), " & _ "PartNo(" & oInst.PartNumber & "), " & _ "RefProd(" & oInst.ReferenceProduct.Parent.Name & ")" & vbCrLf) '--- Close Logfile poLogStream.Close End Sub |
In my code shown above, the CreateObject function returns the FileSystemObject (fs). The CreateTextFile method then creates the TXT file and the WriteLine method allows us to write anything we want in the newly created TXT file. The Close method closes the file when we’re finished writing. vbTab is used to indent while vbLf is used to go to the next line. See a complete list of VB constants here.
Instead of using the BOM tool you can use this txt exportation method to create custom Bills of Material with a CATIA macro. It’s another good tool for troubleshooting and making sure variables at different points in your code are what you expect them to be.
Do you export data to TXT and if so how often?
Hello,
I have a short question regarding this topic.
Every time when you run the macro the files will be overwitten? I’m asking this because more or less I have a similar situation, I want to export a dxf file and every time when I run the macro I receive the following message: “Do you want it to be overwritten?” and instead of clicking “Yes” I would like that this process to be done automatically. What should I write in my macro?
These are the lines that I have so far:
Set drawingDocument1 = CATIA.ActiveDocument
drawingDocument1.ExportData “C:\Users\TEST\Desktop\TEST 11\MACRO\Part_No.dxf”, “dxf”
Thank you in advance
Hello,
I am seeking assistance with developing a VBA macro for CATIA V6 that can export properties of PDD and ATT files based on a specific filter. I need to extract the following properties via excel file:
For PDDs:
PLM ID (idPLM)
Designation in both French and English
Version
Maturity
For ATTs:
Ref PA
Designation in both French and English
ECO
Technical Attribute
Thank you in advance for your assistance!