Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

VBA Macro TO Export Splines from catia to text file(.dat) 1

Status
Not open for further replies.

Hadiza Hamza

New member
Dec 15, 2016
6
0
0
NL
Hello every one!! I am sort of new to the whole VBA thing. so i was wondering if i could get some help here. so basically i am trying to find out how to write a dat file that wiil be able to import splines into Catia. These splines when imported are supposed to act like meshes on a structure, that is, picture a meshed structure, but instead of mesh it will be splines on it. so right now i thought to learn a macro that exports a few splines i created on a structure into a text(.dat) file. but i have been having troubles with the macro i have as it asks me to select a spline, but wont allow me to click on the spline in spec tree. The thing is that i have lots of splines and i would like the macro to just select splines automatically without asking and export them..... PLS HELP ME. thanks alot.

the code is written below:

[highlight #F57900]Sub CATMain()

'*** *** Definition Variables
Dim CtrlPoint()
Dim oCoordinates(1)
Dim StartKrit As Integer

'*** Query document type ***
StartKrit = 0
Set oDoc = CATIA.ActiveDocument
ObjType = TypeName(oDoc)
If ObjType = "PartDocument" Then
DocType = "Part"
StartKrit = 1
ElseIf ObjType = "DrawingDocument" Then
DocType = "Drawing "
StartKrit = 1
End If

If StartKrit = 0 Then
box = MsgBox(" The active document is neither a CATPart still CATDrawing! " + Chr(10) + _
" The macro can not continue and will now exit " + Chr(10) + _
"Please select a CATPart or a CATDrawing and start the macro again!", vbCritical + vbOKOnly, "incorrect document type")
Exit Sub
End If


'*** Create the * .txt files ***
StorePath = "C: \"
StoreName = "Splinekoordinaten" & Date
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(StorePath & StoreName & ".txt ") = True Then
box = MsgBox(" file ==> " + StorePath + StoreName + " <== already exists! " + Chr(10) + " Do you want to overwrite the file? ", vbCritical + vbYesNo, "file already exists ")
If box = vbNo Then
box = MsgBox(" The macro is now finished ", vbInformation + vbOKOnly, " the user stops ")
Exit Sub
End If
End If
Set A = fs.CreateTextFile("D:\school\INTERNSHIP\Macro\Newest.txt ", True)
A.WriteLine (" points coordinates of a spline ")
A.WriteLine (" ")
If DocType = " Part " Then
A.WriteLine (" name of CATParts: " & oDoc.Name)
ElseIf DocType = " Drawing " Then
A.WriteLine ("name of CATDrawing:" & oDoc.Name)
End If
A.WriteLine ("")

'*** Readout from the CATDrawing ***
If DocType = "Drawing" Then
Dim otype2D(0)
Dim Selection
Set mysel = oDoc.Selection
mysel.Clear
otype2D(0) = "Spline2D"
mysel.Clear
box = MsgBox(" Please select now the spline ", vbInformation + vbOKCancel, " spline Select ")
If box = vbCancel Then
box = MsgBox(" you have the selection canceled " + Chr(10) + _
" the macro is now finished! ", vbCritical, "abort by user")
If fs.FileExists(StorePath & StoreName & ".txt") = True Then
A.Close
fs.DeleteFile (StorePath & StoreName & ".txt")
End If
Exit Sub
End If
Selection = mysel.SelectElement2(otype2D, "Please select the spline", False)
If Selection = "Normal" Then
oSplineName = mysel.Item(1).Value.Name
Set oSpline = mysel.Item(1).Value
A.WriteLine ("name of the selected spline:" & oSplineName)
A.WriteLine ("")
A.WriteLine ("")
Else
box = MsgBox("you have canceled the selection" + Chr(10) + _
"the macro is now finished! ", vbCritical, " abort by user ")
If fs.FileExists(StorePath & StoreName & " .txt ") = True Then
A.Close
fs.DeleteFile (StorePath & StoreName & " .txt ")
End If
Exit Sub
End If
mysel.Clear

Set QuCtrlP = oSpline.EndPoint
QuCtrlPRaw = QuCtrlP.Name
QuCtrlPSemi = Split(QuCtrlPRaw, ".")
QuCtrlPFin = QuCtrlPSemi(1) - 1
ReDim Preserve CtrlPoint(QuCtrlPFin)
For j = 0 To QuCtrlPFin
Set CtrlPoint(j) = oSpline.GetItem("Ktrl-point." & j + 1)
CtrlPoint(j).GetCoordinates oCoordinates
A.WriteLine ("point" & j + 1 & "X / Y")
A.WriteLine (oCoordinates(0))
A.WriteLine (oCoordinates(1))
A.WriteLine ("")
Next

'*** readout from the CATPart ***
ElseIf DocType = "Part" Then
Dim otype3D(0)
Set mysel = oDoc.Selection
mysel.Clear
otype3D(0) = "Spline2D"
mysel.Clear
box = MsgBox("Please select now the spline", vbInformation + vbOKCancel, "spline Select")
If box = vbCancel Then
box = MsgBox("you have canceled the selection" + Chr(10) + _
"The macro is now finished!", vbCritical, "abort by user")
If fs.FileExists(StorePath & StoreName & ".txt") = True Then
A.Close
fs.DeleteFile (StorePath & StoreName & ".txt ")
End If
Exit Sub
End If
Selection = mysel.SelectElement2(otype3D, " Please select the spline ", False)
If Selection = " Normal " Then
oSplineName = mysel.Item(1).Value.Name
Set oSpline = mysel.Item(1).Value
A.WriteLine ("name of the selected spline:" & oSplineName)
A.WriteLine ("")
A.WriteLine ("")
Else
box = MsgBox("you have canceled the selection" + Chr(10) + _
"The macro is now finished!", vbCritical, "abort by user")
If fs.FileExists(StorePath & StoreName & ".txt") = True Then
A.Close
fs.DeleteFile (StorePath & StoreName & " .txt ")
End If
Exit Sub
End If

mysel.Clear
Set QuCtrlP = oSpline.EndPoint
QuCtrlPRaw = QuCtrlP.Name
QuCtrlPSemi = Split(QuCtrlPRaw, " ")
QuCtrlPFin = QuCtrlPSemi(1) - 1
ReDim Preserve CtrlPoint(QuCtrlPFin)
For j = 0 To QuCtrlPFin
Set CtrlPoint(j) = oSpline.GetItem("Ktrl-point." & j + 1)
CtrlPoint(j).GetCoordinates oCoordinates
A.WriteLine ("point" & j + 1 & "X / Y")
A.WriteLine (oCoordinates(0))
A.WriteLine (oCoordinates(1))
A.WriteLine ("")
Next
End If

'**** Issue Storage Location ****
Ml = "The macro has completed successfully"
M2 = "The * .txt file is saved under the following path:"
M2_ZU_1 = "==>"
M2_ZU_2 = "<== "
M3 = " Are you in the path now oeffnen? "
Title = "memory data"
skin = vbInformation + vbYesNo
query = MsgBox(Ml + Chr(10) + Chr(10) + M2 + Chr(10) + Chr(10) + M2_ZU_1 + StorePath + StoreName + M2_ZU_2 + Chr(10) + Chr(10) + M3, skin, Title)

If query = vbYes Then
ExplorerPath = "C: \ WINDOWS \ explorer.exe"
Explorer = CATIA.SystemService.ExecuteProcessus(ExplorerPath & "" & StorePath)
End If


End Sub
[/highlight]
 
Replies continue below

Recommended for you

just check the type of the element you want to filter at selection with the following script (select one element then run the script)

Code:
Sub CATMain()
    
    Set mySelection = CATIA.ActiveDocument.Selection
    
    MsgBox (TypeName(mySelection.Item(1).Value))

End Sub

for your drawing

Code:
 otype2D(0) = "Spline2D"
and
Code:
 Selection = mysel.SelectElement2(otype2D, "Please select the spline", False)

or for your 3D

Code:
otype3D(0) = "Spline2D"
and
Code:
Selection = mysel.SelectElement2(otype3D, " Please select the spline ", False)

...control what is selectable... maybe you should change the value for the 3D to

Code:
otype3D(0) = "[b]HybridShapeSpline"[/b]



Eric N.
indocti discant et ament meminisse periti
 
Hello,

I only took a look at 3D section so:
What kind of object do you have in 3D? Isn't it a HybridShapeSpline? If yes, then

Code:
otype3D(0) = "HybridShapeSpline"

If you want to avoid user interaction, just collect them from Specification Tree in your own collection and parse that collection.
Something like this:

Code:
Sub Splines()
    Dim oPart As Part
    Set oPart = CATIA.ActiveDocument.Part
    
    Dim mySplines As Collection
    Set mySplines = New Collection
    
    Dim oHybridBody As HybridBody
    Dim oHybridShape As HybridShape
    
    For Each oHybridBody In oPart.HybridBodies
        For Each oHybridShape In oHybridBody.HybridShapes
            If TypeName(oHybridShape) = "HybridShapeSpline" Then
                mySplines.Add oHybridShape
            End If
        Next
    Next
    
    Dim mySpline As HybridShapeSpline
    For Each mySpline In mySplines
        'do your stuff here
    Next
End Sub

Hope it helps.
 
Thanks for the reply... i am using spline2D .i tried what you said and it worked for that part!! now i have another problem!! The code shows an error when it reaches the highlighted part. I dont even understand what that part really does or why the code has an error ther! coykd someone pls help me.
mysel.Clear
Set QuCtrlP = oSpline.EndPoint
QuCtrlPRaw = QuCtrlP.Name
QuCtrlPSemi = Split(QuCtrlPRaw, " ")
[highlight #C4A000]QuCtrlPFin = QuCtrlPSemi(1) - 1[/highlight]
ReDim Preserve CtrlPoint(QuCtrlPFin)
For j = 0 To QuCtrlPFin
Set CtrlPoint(j) = oSpline.GetItem("Ktrl-point." & j + 1)
CtrlPoint(j).GetCoordinates oCoordinates
A.WriteLine ("point" & j + 1 & "X / Y")
A.WriteLine (oCoordinates(0))
A.WriteLine (oCoordinates(1))
A.WriteLine ("")
Next
End If


And also could someone tell me how to change the part where it asks me to select the spline so that it just automaitcally selcts all splines in the code? "[highlight #C4A000]Selection = mysel.SelectElement2(otype3D, " Please select the spline ", False)[/highlight]"
 
The error you get is because this bit here

Code:
QuCtrlPSemi = Split(QuCtrlPRaw, " ")

does not return the expected array. Put a breakpoint on the line above and Watch the value of QuCtrlPSemi

Did you really create the script you posted? Because you are asking some basic questions in obvious contradiction with the coding skills shown above.

If I would have the time I would recreate it for you but I don't.

Calin
 
Actually i dint create the Code, i just copied it from a website online which stated that the code exports splines!! but i have been trying to adjust it to fit what i am trying to do!! as u can see that has not been going well for me.. I am very new to all this VBA Stuff. As for what you advised, i did it and it shows that it is a string type, is that going to work or does it only work with variant? i believe the array should be numbers(coordinates) right?! so far i have tried replacing the [highlight #C4A000]QuCtrlP.Name[/highlight] with various other properties but is still gives the same error..... if you have any more advice for me, i would gladly accept it....@Calin
 
Status
Not open for further replies.
Back
Top