Help. intersection point between an sketch and a WorkPlane with VBA?

Help. intersection point between an sketch and a WorkPlane with VBA?

Anonymous
Not applicable
1,036 Views
4 Replies
Message 1 of 5

Help. intersection point between an sketch and a WorkPlane with VBA?

Anonymous
Not applicable

Hi all!

 

How could I find the intersection point between an sketch and a WorkPlane? (but using VBA)

I got an example:

Interce.PNG

I want to use this function:

(in English is: "intersection of line and plane/Surface)

 

Sin título.png

 

But I don´t know how to program it.

Thanks in advance!

0 Likes
Accepted solutions (1)
1,037 Views
4 Replies
Replies (4)
Message 2 of 5

BrianEkins
Mentor
Mentor

Here's some code that demonstrates using the API (WorkPoints.AddByCurveAndEntity method) to do the equivalent of the command you pointed out.  The command and the API are limited to using a linear entity (sketch line, linear edge, work axis) and a plane (planar face or work plane).  The sample expects you to have a work plane and sketch line selected before you run the macro.

 

 

Public Sub LinePlaneWPCreate()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    
    If partDoc.SelectSet.Count <> 2 Then
        MsgBox "The work plane and sketch line must be selected."
        Return
    End If
    
    Dim skLine As SketchLine
    Set skLine = Nothing
    Dim wp As WorkPlane
    Set wp = Nothing
    If TypeName(partDoc.SelectSet.Item(1)) = "SketchLine" Then
        Set skLine = partDoc.SelectSet.Item(1)
    End If
    If TypeName(partDoc.SelectSet.Item(2)) = "SketchLine" Then
        Set skLine = partDoc.SelectSet.Item(2)
    End If
    If TypeName(partDoc.SelectSet.Item(1)) = "WorkPlane" Then
        Set wp = partDoc.SelectSet.Item(1)
    End If
    If TypeName(partDoc.SelectSet.Item(2)) = "WorkPlane" Then
        Set wp = partDoc.SelectSet.Item(2)
    End If
    
    If skLine Is Nothing Or wp Is Nothing Then
        MsgBox "The work plane and sketch line must be selected."
        Return
    End If
    
    Dim newWP As WorkPoint
    Set newWP = partDoc.ComponentDefinition.WorkPoints.AddByCurveAndEntity(skLine, wp)
End Sub

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 3 of 5

Anonymous
Not applicable

Excellent. Thanks for your help.

It actually Works because I get the workpoint... but i need the coordinates and with your method I can't.

 

I modified the code for my case:

 

Dim partDoc As PartDocument
Set partDoc = ThisApplication.ActiveDocument

Dim skLine As SketchControlPointSpline
Set skLine = partDoc.ComponentDefinition.Sketches(2).SketchControlPointSplines(1)
Dim wp As WorkPlane
Set wp = partDoc.ComponentDefinition.WorkPlanes.Item(3)

Dim newWP As WorkPoint
Set newWP = partDoc.ComponentDefinition.WorkPoints.AddByCurveAndEntity(skLine, wp)

 

but in this way I don't know how to get the coordinates because I wanna use it later.

0 Likes
Message 4 of 5

BrianEkins
Mentor
Mentor
Accepted solution

Since you referenced a specific work point command, I assumed that you wanted to create a work point.  Here's the additional code to get the coordinate of the created work point.

 

Dim newWP As WorkPoint
Set newWP = partDoc.ComponentDefinition.WorkPoints.AddByCurveAndEntity(skLine, wp)
Dim coord as Point
Set coord = newWP.Point
MsgBox coord.X & ", " & coord.Y & ", " & coord.Z

If all you need is the coordinate and don't care about the work point, then there's another way that just calculates the intersection without creating any new entities.

 

Public Sub SketchPlaneIntersection()
    Dim sketchCurve As SketchEntity
    Set sketchCurve = ThisApplication.CommandManager.Pick(kSketchCurveFilter, "Select sketch entity.")
    Dim sl As SketchLine
    Dim wp As WorkPlane
    Set wp = ThisApplication.CommandManager.Pick(kWorkPlaneFilter, "Select work plane.")
    
    Dim results As ObjectsEnumerator
    Set results = ThisApplication.TransientGeometry.CurveSurfaceIntersection(sketchCurve.Geometry3d, wp.plane)
    
    If results Is Nothing Then
        MsgBox "No intersection was found."
    Else
        Dim i As Integer
        For i = 1 To results.Count
            MsgBox "Intersection " & i & ": " & results.Item(i).X & ", " & _
                                                results.Item(i).Y & ", " & _
                                                results.Item(i).Z
        Next
    End If
End Sub
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 5 of 5

Anonymous
Not applicable

Thanks MR. Brian!

It worked good. Thanks for all your Help.

0 Likes