Continue to Site

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!

Does anybody know how to extract the coordinates for the points in a polyline?

Status
Not open for further replies.

MecaTron101

Automotive
Jun 30, 2014
7
SE
I'm trying to write a macro that can create a coordinate table for all the points making up a polyline. So far I've been able to export the coordinates of ordinary points to Excel.
However, polylines have proven difficult.
I found code for the polyline.getElement method, but how is the result stored? Array, variable...?

Dim HybShpPolylineElement As Reference
Dim HybShpPolylineRadius As Reference
HybShpPolyline.GetElement 1, HybShpPolylineElement,HybShpPolylineRadius

I'm a beginner when it comes to vba for Catia so I'm at a loss here.
 
Replies continue below

Recommended for you

It's a 3D polyline, with ordinare 3D points, used for creating pipes and similar structures.
The problem is that I can't find out how the "GetElement" method should present the data when there are multiple variables in the method. Everything else works fine.
The goal is simply to create a table with coordinates and radiues for each point in the polyline.
Code below, problem area marked in red.

Code:
[indent]'variables[/indent]
Dim objGEXCELapp As Object
Dim objGEXCELwkBks As Object
Dim objGEXCELwkBk As Object
Dim objGEXCELwkShs As Object
Dim objGEXCELSh As Object
Dim coords(3) As Integer

Sub CATMain()[indent]'main sub[/indent]
startExcel
exportPolylinePoints
End Sub

[COLOR=#4E9A06][COLOR=#8AE234]'****************************************************************************
Sub StartEXCEL()[indent]'function for opening excel[/indent]
'****************************************************************************
Err.Clear
On Error Resume Next
Set objGEXCELapp = GetObject(, "EXCEL.Application")

If Err.Number <> 0 Then
Err.Clear
Set objGEXCELapp = CreateObject("EXCEL.Application")
End If

objGEXCELapp.Application.Visible = True
Set objGEXCELwkBks = objGEXCELapp.Application.WorkBooks
Set objGEXCELwkBk = objGEXCELwkBks.add
Set objGEXCELwkShs = objGEXCELwkBk.Worksheets(1)
Set objGEXCELSh = objGEXCELwkBk.Sheets(1)
objGEXCELSh.Cells(1, "A") = "ITEM X"
objGEXCELSh.Cells(2, "A") = "POINT"
objGEXCELSh.Cells(2, "B") = "X"
objGEXCELSh.Cells(2, "C") = "Y"
objGEXCELSh.Cells(2, "D") = "Z"
objGEXCELSh.Cells(2, "E") = "RADIUS" 

End Sub[/color][/color]
'***********************************************************
Sub exportPolylinePoints[indent]'function for exporting the points[/indent]
'***********************************************************
 Dim selection1 As selection		'selection in Catia
    Set Selection1 = CATIA.ActiveDocument.selection
        
        'pick first element (the polyline) of the selection in Catia
		Dim polyline1 As HybridShapePolyline
        	Set polyline1 = selection1.Item(1).value
		
		'get the number of elements in the polyline
		dim polyline1NrOfElements as long		
		polyline1NrOfElements = polyline1.NumberOfElements
			MsgBox "The selected polyline has " & polyline1NrOfElements & " points."		'display number of elements
	
		 	for i = 1 to polyline1NrOfElements						
				[highlight #EF2929]dim pointRef as reference
				dim pointRad as double
				polyline1.GetElement(i), pointRef, pointRad[/highlight]
				'How is the result from GetElement presented? This is my problem.

				 
				'Write PointData to Excel Sheet
				objGEXCELSh.Cells(i + 2, "A") = pointRef.Name
				objGEXCELSh.Cells(i + 2, "B") = coordsArray(0)
				objGEXCELSh.Cells(i + 2, "C") = coordsArray(1)
				objGEXCELSh.Cells(i + 2, "D") = coordsArray(2)
				objGEXCELSh.Cells(i + 2, "E") = coordsArray(3)	'the radius
				objGEXCELSh.Cells(i + 3, "A") = "TOTAL LENGTH = "

			next

end Sub
 
try this:

Code:
Dim pointRef As Reference
Dim pointRad  [COLOR=#EF2929]As Length[/color]
polyline1.GetElement i, pointRef, pointRad

but you still have other errors

Eric N.
indocti discant et ament meminisse periti
 
I could not check everything (I have some VBA ref file conflict) but that should help:

Code:
'variables
Dim objGEXCELapp As Object
Dim objGEXCELwkBks As Object
Dim objGEXCELwkBk As Object
Dim objGEXCELwkShs As Object
Dim objGEXCELSh As Object
Dim coords[COLOR=#EF2929](2) As Variant
Dim TheMeasurable As Variant
Dim TheSPAWorkbench As Workbench[/color]


Sub CATMain()

StartEXCEL
exportPolylinePoints
End Sub

'****************************************************************************
Sub StartEXCEL()
'function for opening excel
'****************************************************************************
Err.Clear
On Error Resume Next
Set objGEXCELapp = GetObject(, "EXCEL.Application")

If Err.Number <> 0 Then
Err.Clear
Set objGEXCELapp = CreateObject("EXCEL.Application")
End If

objGEXCELapp.Application.Visible = True
Set objGEXCELwkBks = objGEXCELapp.Application.WorkBooks
Set objGEXCELwkBk = objGEXCELwkBks.Add
Set objGEXCELwkShs = objGEXCELwkBk.Worksheets(1)
Set objGEXCELSh = objGEXCELwkBk.Sheets(1)
objGEXCELSh.Cells(1, "A") = "ITEM X"
objGEXCELSh.Cells(2, "A") = "POINT"
objGEXCELSh.Cells(2, "B") = "X"
objGEXCELSh.Cells(2, "C") = "Y"
objGEXCELSh.Cells(2, "D") = "Z"
objGEXCELSh.Cells(2, "E") = "RADIUS"

End Sub
'***********************************************************
Sub exportPolylinePoints()
'function for exporting the points
'***********************************************************
 Dim selection1 As Selection        'selection in Catia
    Set selection1 = CATIA.ActiveDocument.Selection
        
        'pick first element (the polyline) of the selection in Catia
[COLOR=#EF2929]        Dim polyline1 
        Set polyline1 = selection1.Item(1)
        Dim oPolyline As HybridShapePolyline
        Set oPolyline = polyline1.Value[/color]' Dont ask me why but I had to do that to get it to work properly
           
        
        'get the number of elements in the polyline
        Dim polyline1NrOfElements 'As Long
        polyline1NrOfElements = oPolyline.NumberOfElements
            MsgBox "The selected polyline has " & polyline1NrOfElements & " points."        'display number of elements
    
            For i = 1 To polyline1NrOfElements
                Dim pointRef As Reference
                [COLOR=#EF2929]Dim oRad  As Length
                oPolyline.GetElement i, pointRef, oRad[/color]
                
                [COLOR=#EF2929]Set TheSPAWorkbench = CATIA.ActiveDocument.GetWorkbench("SPAWorkbench")[/color] ' set TheSPAWorkbench
                [COLOR=#EF2929]Set TheMeasurable = TheSPAWorkbench.GetMeasurable(pointRef)[/color]             ' set Measurable with reference
                [COLOR=#EF2929]TheMeasurable.GetPoint coords[/color]                                           ' get coordinates from Measurable

                If oRad Is Nothing Then RadiusAtPoint = False Else RadiusAtPoint = True ' check if Radius at point


                 
'                Write PointData to Excel Sheet
                objGEXCELSh.Cells(i + 2, "A") = [COLOR=#EF2929]pointRef.DisplayName[/color]
                objGEXCELSh.Cells(i + 2, "B") = coords(0)
                objGEXCELSh.Cells(i + 2, "C") = coords(1)
                objGEXCELSh.Cells(i + 2, "D") = coords(2)
                [COLOR=#EF2929]If RadiusAtPoint then[/color] objGEXCELSh.Cells(i + 2, "E") = [COLOR=#EF2929]oRad.Value[/color]  'the radius
                objGEXCELSh.Cells(i + 3, "A") = "TOTAL LENGTH = "

            Next

End Sub

Eric N.
indocti discant et ament meminisse periti
 
Thanks alot!
This worked perfectly. I was even able to add length measurement of the polyline aswell.
Awsome, thanks.

Btw, does this forum have a "collection thread" for macros/ functions like this?
I guess alot of people could benefit from the soulutions that are created here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Top