Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
' Define Selection
Dim sMessage 'as string
sMessage = "Please select Geometrical Set(s) with points to extract" 'Set variable as message to use in msgbox and selectelement3:)
MsgBox sMessage
UserSelection = sSel.SelectElement3(EnableSelectionFor, sMessage, False, CATMultiSelTriggWhenUserValidatesSelection, True)
' Evaluation if the selection is correct or not
If UserSelection <> "Normal" Then 'Technically this will never happen because your filter forces them to pick a geoset
MsgBox "Error with the selection"
Exit Sub
Else
'Make an array to store the geosets in
'You need to subtract 1 because arrays start at 0 not 1, but selections start at 1
Dim aGeosets(sSel.Count-1)
'Loop through geosets and store in array
For i = 1 to sSel.Count
aGeosets(i-1) = sSel.item(i).Value
Next
'Set ohybridbody = sSel.Item(1).Value
'MsgBox "The Geometrical Set selected is : " & ohybridbody.Name
End If
ReDim acoord(2) 'Do you need to redim acoord or can you just size it when you dim it? Like "Dim aCoord(2)"
'--------------------------------------------------------------------------------
' The location of the result file
'--------------------------------------------------------------------------------
'Dim filename As String
'filename = CATIA.FileSelectionBox("Where do you want to save the result file", "*.txt", CatFileSelectionModeSave)
Set Datos = CATIA.FileSystem.CreateFile(path & "\" & CATIA.ActiveDocument.Name & ".txt", True
Set ostream = Datos.OpenAsTextStream("ForAppending")
'Header
ostream.Write (oPartDoc.Name & Chr(10))
ostream.Write (" " & Chr(10)) 'Blank line
'ostream.Write ("The selected Geometrical Set was : " & ohybridbody.Name & Chr(10))
ostream.Write (" " & Chr(10))' Blank line
'start Loop to go through Goesets
For i = 0 to Ubound(aGeosets)
Set ohybridbody = aGeosets(i) 'Can't remember if this is aGeosets(i).value or not :(
Set oshapes = ohybridbody.HybridShapes
ostream.Write ("$* PLY" & Chr(32) & ohybridbody.Name & Chr(10)) 'First line in your picture
ostream.Write ("PENUP" & Chr(10)) ' Second line in your picture
'Get point coordinates from all points in geoset
For j = 1 To oshapes.Count
oshapes.Item(j).GetCoordinates acoord
Set reference1 = oshapes.Item(j) 'Doesn't appear to be used, can it be deleted?
Dim formatednumber(2)
For k = 0 To 2
formatednumber(k) = Int(acoord(k) * 10) / 10
If Int(acoord(k) * 10) / 10 - Int(acoord(k)) = 0 Then
formatednumber(k) = Int(acoord(k)) & ".0"
End If
Next
'Thrid and fifth+ lines in your picture
ostream.Write ("GOTO " & Chr(32) & formatednumber(0) & Chr(32) & "" & Chr(32) & formatednumber(1) & Chr(32) & "" & Chr(32) & formatednumber(2) & Chr(10))
If j = 1 then
ostream.Write ("PENDOWN" & Chr(10))'Fourth line in your picture
End if
'Just an idea, Instead of making a new array, you should be able to change the contents of the existing array
' For k = 0 To 2
' acoord(k) = Int(acoord(k) * 10) / 10
' If Int(acoord(k) * 10) / 10 - Int(acoord(k)) = 0 Then
' acoord(k) = Int(acoord(k)) & ".0"
' End If
' Next
'You would also need to change when you write to the text stream
' ostream.Write ("GOTO " & Chr(32) & acoord(0) & Chr(32) & "" & Chr(32) & acoord(1) & Chr(32) & "" & Chr(32) & acoord(2) & Chr(10))
Next 'goes to next point
ostream.Write ("PENUP" & Chr(10)) 'Last line in your picture
Next 'goes to next geoset
ostream.Close
'MsgBox "Points Exported :" & (i-1) & " POINTS" & Chr(10) & Chr(10) & "Please Check the following file for result : " & chr(10) & chr(10) & filename & chr(10)& chr(10) & "Process finished"
MsgBox "Check results in folder " & Chr(10) & path & "\" & Chr(10) & Chr(10) & "File:" & Chr(10) & partDoc.Name & ".txt" & Chr(10)
End Sub
'multiple geosets are selected
Dim oCurveGeoset 'as HybridBody 'Geoset that has all the curves
Dim oPointGeoset 'as HybridBody 'New geoset for the points on the curves, I am just guessing that you want this
Dim oCurve 'curve points will be added to
'Dim sResponse 'The number of points you want on the curves
Dim iCounter 'To make sure you dont get into an infinite loop
Dim oPoint 'As HybridShapePointOnCurve
Dim oGSD 'As Factory 'for accessing Generative shape design functions
Dim lRatio 'Ratio for spacing out points
'loop through selected geosets to get curves
For i = 1 to sSel.Count
Set oCurveGeoset = sSel.Item(i).Value
'You need to know how many points to put on the curves
'This will ask the user once for each geoset
'Maybe you want to do it automatically...maybe you have a standard
'Like you want a point every 1 mm on every curve
sResponse = "A" 'set to a non-number to get inside of the following Do Loop
iCounter = 0 'Used to limit the number of tries the user gets to enter a number
Do Until IsNumeric(sResponse) = True and Val(sResponse) = Int(Val(sResponse)) 'Make sure user enters a number that is not a decimal
sResponse = InputBox ("Enter the (integer) number of points to be placed on each curve in this geoset")
If iCounter = 3 then 'give user 3 chances to enter a number
MsgBox "You did not enter an integer, exiting"
Exit Sub
End if
iCounter = iCounter + 1 'Increments the counter if user keeps putting in non numbers
Loop
'Calculate ratio
lRatio = 1/(cInt(sResponse)-1)
'loop through curves to add Points
For j = 1 to oCurveGeoset.HybridShapes.Count
Set oCurve = oCurveGeoset.HybridShapes.Item(j)
Set oPointGeoset = oPartDoc.HybridBodies.Add 'Add the geoset
oPointGeoset.Name = "Points for " & oCurve.Name 'name the geoset
'If you name the point geosets a specific way with this macro, you can get them automatically
'When you create the text file, you won't need to select them;)
'Loop to add points at multiples of the calculated ratio
For k = 0 to cInt(sResponse)
Set oPoint = oGSD.AddNewPointOnCurveFromPercent (oCurve, k*lRatio, False)'Add the point
oPoint.Name = oCurve.Name & " Point " & k + 1 'name the point
oPointGeoset.AppendHybridShape oPoint 'Make the point appear in the tree
next
Next
Next
sSel.clear 'Clear the selection
Dim aGeosets(sSel.Count-1)
'sResponse = "A" 'set to a non-number to get inside of the following Do Loop
'iCounter = 0 'Used to limit the number of tries the user gets to enter a number
'Do Until IsNumeric(sResponse) = True and Val(sResponse) = Int(Val(sResponse)) 'Make sure user enters a number that is not a decimal
sResponse = InputBox ("Enter the (integer) number of points to be placed on each curve in this geoset")
'If iCounter = 3 then 'give user 3 chances to enter a number
'MsgBox "You did not enter an integer, exiting"
'Exit Sub
'End if
'iCounter = iCounter + 1 'Increments the counter if user keeps putting in non numbers
'Loop
me said:you don't Dim with a variable size, you ReDim
arraySize= 20
Dim myArray() : ReDim myArray(arraySize)
Set sSel = CATIA.ActiveDocument.Selection
Dim EnableSelectionFor(0)
EnableSelectionFor(0) = "HybridBody"
path = "c:\temp"
Sub CATMain()
Dim filename As String
filename = CATIA.ActiveDocument.Name
Dim path As String
path = CATIA.ActiveDocument.path
Dim partDoc As PartDocument
Set partDoc = CATIA.ActiveDocument
Dim oPartDoc As Part
On Error Resume Next
Set oPartDoc = CATIA.ActiveDocument.Part
If Err.Number <> 0 Then
Message = MsgBox("Sorry, This script works with a CATPart as Active document", vbCritical, "Error")
Exit Sub
End If
' What do want to select
Dim EnableSelectionFor(0)
EnableSelectionFor(0) = "HybridBodies"
' Reset the Selection
Set sSel = CATIA.ActiveDocument.selection
sSel.Clear
' Define Selection
Dim sMessage 'as string
sMessage = "Please select Geometrical Set(s) with points to extract" 'Set variable as message to use in msgbox and selectelement3:)
MsgBox sMessage
UserSelection = sSel.SelectElement3(EnableSelectionFor, sMessage, False, CATMultiSelTriggWhenUserValidatesSelection, True)
' Evaluation if the selection is correct or not
If UserSelection <> "Normal" Then 'Technically this will never happen because your filter forces them to pick a geoset
MsgBox "Error with the selection"
Exit Sub
Else
'Make an array to store the geosets in
'You need to subtract 1 because arrays start at 0 not 1, but selections start at 1
ReDim aGeosets(sSel.Count - 1)
'Loop through geosets and store in array
For i = 1 To sSel.Count
aGeosets(i - 1) = sSel.Item(i).Value
Next
'Set ohybridbody = sSel.Item(1).Value
'MsgBox "The Geometrical Set selected is : " & ohybridbody.Name
End If
ReDim acoord(2) 'Do you need to redim acoord or can you just size it when you dim it? Like "Dim aCoord(2)"
'--------------------------------------------------------------------------------
' The location of the result file
'--------------------------------------------------------------------------------
'Dim filename As String
'filename = CATIA.FileSelectionBox("Where do you want to save the result file", "*.txt", CatFileSelectionModeSave)
Set Datos = CATIA.FileSystem.CreateFile(path & "\" & CATIA.ActiveDocument.Name & ".txt", True)
Set ostream = Datos.OpenAsTextStream("ForAppending")
'Header
ostream.Write (oPartDoc.Name & Chr(10))
ostream.Write (" " & Chr(10)) 'Blank line
'ostream.Write ("The selected Geometrical Set was : " & ohybridbody.Name & Chr(10))
ostream.Write (" " & Chr(10)) ' Blank line
'start Loop to go through Goesets
For i = 0 To UBound(aGeosets)
Set ohybridbody = aGeosets(i) 'Can't remember if this is aGeosets(i).value or not :(
Set oshapes = ohybridbody.HybridShapes
ostream.Write ("$* PLY" & Chr(32) & ohybridbody.Name & Chr(10)) 'First line in your picture
ostream.Write ("PENUP" & Chr(10)) ' Second line in your picture
'Get point coordinates from all points in geoset
For j = 1 To oshapes.Count
oshapes.Item(j).GetCoordinates acoord
Set reference1 = oshapes.Item(j) 'Doesn't appear to be used, can it be deleted?
Dim formatednumber(2)
For k = 0 To 2
formatednumber(k) = Int(acoord(k) * 10) / 10
If Int(acoord(k) * 10) / 10 - Int(acoord(k)) = 0 Then
formatednumber(k) = Int(acoord(k)) & ".0"
End If
Next
'Thrid and fifth+ lines in your picture
ostream.Write ("GOTO " & Chr(32) & formatednumber(0) & Chr(32) & "" & Chr(32) & formatednumber(1) & Chr(32) & "" & Chr(32) & formatednumber(2) & Chr(10))
If j = 1 Then
ostream.Write ("PENDOWN" & Chr(10)) 'Fourth line in your picture
End If
'Just an idea, Instead of making a new array, you should be able to change the contents of the existing array
' For k = 0 To 2
' acoord(k) = Int(acoord(k) * 10) / 10
' If Int(acoord(k) * 10) / 10 - Int(acoord(k)) = 0 Then
' acoord(k) = Int(acoord(k)) & ".0"
' End If
' Next
'You would also need to change when you write to the text stream
'ostream.Write ("GOTO " & Chr(32) & acoord(0) & Chr(32) & "" & Chr(32) & acoord(1) & Chr(32) & "" & Chr(32) & acoord(2) & Chr(10))
Next 'goes to next point
ostream.Write ("PENUP" & Chr(10)) 'Last line in your picture
Next 'goes to next geoset
ostream.Close
'MsgBox "Points Exported :" & (i-1) & " POINTS" & Chr(10) & Chr(10) & "Please Check the following file for result : " & chr(10) & chr(10) & filename & chr(10)& chr(10) & "Process finished"
MsgBox "Check results in folder " & Chr(10) & path & "\" & Chr(10) & Chr(10) & "File:" & Chr(10) & partDoc.Name & ".txt" & Chr(10)
End Sub
me said:when you work on a script if for any GOOD reason you use on error resume next, you should also use on error goto 0 in order to localize the effect of 'resume next', so you can actually catch error when they arrive.