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!

CATIA VB Programming, Get the name and place it as the text file name.

Status
Not open for further replies.

Netsurfer2

New member
Jan 2, 2015
3
US
I have learned how to get the name of the part file and place it as the name of the text file that I want to write to, but my problems are:

1. I can only get it to make the new text file write to drive C:
2. And I cannot internally populate the points at the same time using stream writer, because I have to make it get the file name from CATIA.

How do I accomplish this?

My Code:


Sub CATMain()


'==================Call Variables=====================================

Dim ObjFso
Dim SourceLocation
Dim DestinationLocation
Dim SourceFileName
Dim DestinationFileName
SourceLocation = "C:TestFolder1"
DestinationLocation = "C:TestFolder2"
SourceFileName = "Source File.txt"
DestinationFileName = "Destination File.txt"

Dim aToExport(5000, 3) As Variant
Dim iNumberOfPoint As Integer
Dim sTime As String
Dim sName As String
Dim FileObj


'=================CATIA Declarations===================================

On Error Resume Next

Set PartDocument1 = CATIA.ActiveDocument

Set part1 = PartDocument1.Part

Set hybridShapeFactory1 = part1.HybridShapeFactory

Set objNetwork = CreateObject("Wscript.Network")

Set hb1 = CATIA.ActiveDocument.Part.InWorkObject

Set hsf = CATIA.ActiveDocument.Part.HybridShapeFactory

Set prt = CATIA.ActiveDocument.Part

Set sel = CATIA.ActiveDocument.Selection

Set SPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")

Set PlaneZX = CATIA.ActiveDocument.Part.OriginElements.PlaneZX

' Reset the Selection

Set sSEL = CATIA.ActiveDocument.Selection

sSEL.Clear


'====================Create A Text File With CATIA Part Name===============


sTime = Replace(Time, ":", "-")

sName = "C:\" & Left(CATIA.ActiveDocument.Name, Len(CATIA.ActiveDocument.Name) - 8) & "-" & sTime & ".txt" '<----Creates A file with the Active Part Name in CATIA

Open sName For Output As #1 ' open file for writting


Set FileSys = CATIA.FileSystem


'=================Select The Objects In CATIA And Write To File============


'FileObj = FileSys.CreateFile("C:\pointsout.txt"=

Set TextsTr = FileObj.OpenAsTextStream("ForWriting")

Dim selection1

Set selection1 = PartDocument1.Selection

selection1.Add part1.FindObjectByName("RibPath")

selection1.Search "CATPrtSearch.Point,sel"



Dim Coord(2) '<-----Other Code

For I = 1 To selection1.Count '<-----Count Each Point Other Code

Dim CurrWorkbench '<----WorkBench Variable

Set CurrWorkbench = PartDocument1.GetWorkbench("SPAWorkbench") '<----Get Current WorkBench in CATIA

Dim Measure '<-----Measure Variable

Set Measure = CurrWorkbench.GetMeasurable(selection1.Item(I).Reference) '<----Get Current Measurement

Measure.GetPoint Coord '<------Getting the Points Using Measure Method

TextsTr.Write (Coord(0) / 25.4 & ", " & Coord(1) / 25.4 & ", " & Coord(2) / 25.4 & vbCrLf) '<-----Writes the Coordinates in inches

Set FileObj = FileSys.CreateFile("C:\pointsout.txt", True) '<----This does work in writing the points to a text file, but doesn't have the Part Name instead it makes the text file "pointsout.txt


Next



part1.Update


Close

MsgBox "Check the file : " & sName, vbInformation ' information about job done



'==============Create A Directory Folder If It Doesn't Exist==============


strFolderPath = FolderPath & "\" & FolderName
If Dir(strFolderPath, vbDirectory) = "" Then
Set FileObj = FileSys.CreateFolder("C:\Tube Production")

Else

Set FileObj = FileSys.CreateFolder("C:\Tube Production")

End If


'==============Copy A File To The File To Folder "Tube Production=========



Set FileSys = My.Computer.FileSystem
My.Computer.FileSystem.RenameFile "C:\Tube Production\pointsout.txt", "C:\Tube Production\" & Left(CATIA.ActiveDocument.Name, Len(CATIA.ActiveDocument.Name) - 8) & ".txt" <----Doesn't work, but I tried to make it change the name to CATIA Active Document Part Name



End Sub
 
Replies continue below

Recommended for you

I used to do some thing similar to this out of KeyCreator. it came in quite handy for cnc bend chart data using excel.. reminiscing aside..

i believe the answer to your question is this.

Code:
Sub CATMain()


'==================Call Variables=====================================

Dim ObjFso
Dim SourceLocation
Dim DestinationLocation
Dim SourceFileName
Dim DestinationFileName
SourceLocation = "C:TestFolder1"
DestinationLocation = "C:TestFolder2"
SourceFileName = "Source File.txt"
DestinationFileName = "Destination File.txt"

Dim aToExport(5000, 3) As Variant
Dim iNumberOfPoint As Integer
Dim sTime As String
Dim sPath As String '<---------------------------------------------------------------------------------------------------added
Dim sName As String
Dim FileObj


'=================CATIA Declarations===================================

On Error Resume Next

Set PartDocument1 = CATIA.ActiveDocument

Set part1 = PartDocument1.Part

Set hybridShapeFactory1 = part1.HybridShapeFactory

Set objNetwork = CreateObject("Wscript.Network")

Set hb1 = CATIA.ActiveDocument.Part.InWorkObject

Set hsf = CATIA.ActiveDocument.Part.HybridShapeFactory

Set prt = CATIA.ActiveDocument.Part

Set sel = CATIA.ActiveDocument.Selection

Set SPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")

Set PlaneZX = CATIA.ActiveDocument.Part.OriginElements.PlaneZX

' Reset the Selection

Set sSEL = CATIA.ActiveDocument.Selection

sSEL.Clear


'====================Create A Text File With CATIA Part Name===============


sTime = Replace(Time, ":", "-")

sPath = Left(CATIA.ActiveDocument.Path, len(CATIA.ActiveDocument.Path))   '<------------------------------------------------------------------added

sName = sPath & Left(CATIA.ActiveDocument.Name, Len(CATIA.ActiveDocument.Name) - 8) & "-" & sTime & ".txt" '<----Creates A file with the Active Part Name in CATIA <-- modified

Open sName For Output As #1 ' open file for writting


Set FileSys = CATIA.FileSystem


'=================Select The Objects In CATIA And Write To File============


'FileObj = FileSys.CreateFile("C:\pointsout.txt"=

Set TextsTr = FileObj.OpenAsTextStream("ForWriting")

Dim selection1

Set selection1 = PartDocument1.Selection

selection1.Add part1.FindObjectByName("RibPath")

selection1.Search "CATPrtSearch.Point,sel"



Dim Coord(2) '<-----Other Code

For I = 1 To selection1.Count '<-----Count Each Point Other Code

Dim CurrWorkbench '<----WorkBench Variable

Set CurrWorkbench = PartDocument1.GetWorkbench("SPAWorkbench") '<----Get Current WorkBench in CATIA

Dim Measure '<-----Measure Variable

Set Measure = CurrWorkbench.GetMeasurable(selection1.Item(I).Reference) '<----Get Current Measurement

Measure.GetPoint Coord '<------Getting the Points Using Measure Method

TextsTr.Write (Coord(0) / 25.4 & ", " & Coord(1) / 25.4 & ", " & Coord(2) / 25.4 & vbCrLf) '<-----Writes the Coordinates in inches

Set FileObj = FileSys.CreateFile("C:\pointsout.txt", True) '<----This does work in writing the points to a text file, but doesn't have the Part Name instead it makes the text file "pointsout.txt 


Next



part1.Update


Close

MsgBox "Check the file : " & sName, vbInformation ' information about job done



'==============Create A Directory Folder If It Doesn't Exist==============


strFolderPath = FolderPath & "\" & FolderName
If Dir(strFolderPath, vbDirectory) = "" Then
Set FileObj = FileSys.CreateFolder("C:\Tube Production")

Else

Set FileObj = FileSys.CreateFolder("C:\Tube Production")

End If


'==============Copy A File To The File To Folder "Tube Production"=========



Set FileSys = My.Computer.FileSystem
My.Computer.FileSystem.RenameFile "C:\Tube Production\pointsout.txt", "C:\Tube Production\" & Left(CATIA.ActiveDocument.Name, Len(CATIA.ActiveDocument.Name) - 8) & ".txt" <----Doesn't work, but I tried to make it change the name to CATIA Active Document Part Name



End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top