Project Curve to Surface

By Xiaodong Liang

Question:
I would like to realize ‘Project Curve to Surface’ capability using Inventor API. This means the function below. Is it possible to implement using API ?

image

 

clip_image001

 

Solution:
As shown in the UI, there are 3 types of projections:

1. project along a vector: 
     projects the spline along the normal to the surface. i.e. project each point of the spline to the surface along the normal vector.  In UI, the projection of Inventor allows the user to select a direction to project. For 2d curve, the default direction is the normal of the plane the curve locates in. While if you try to project a 3d curve, you must select a direction. Otherwise, the [OK] button is disable.

2. project to the closet point: 
    finds the closest point of each spline point on the surface.

3. curl to the surface: 
    forms the projected curve around the curvature of selected face or faces. Note: For Wrap to Surface output, the faces must be a cylinder, cone, or planar. The curves must be planar and tangent to the selected faces

It is easier to implement 1 or 2. For 1, we just need to build the normal  and project the point by FindUsingRay. For 2, the method is Face.GetClosestPointTo . The following code asks the the user to select a spline  (2d/3d) and a surface. next project and create a 3d spline on the surface.

   Private Sub test()

 

        ‘ get inventor appliaction

        Dim _InvApp As Inventor.Application = Nothing

        _InvApp =

            System.Runtime.InteropServices.Marshal.

                GetActiveObject("Inventor.Application")

 

        Dim oPartDoc As PartDocument

        oPartDoc = _InvApp.ActiveDocument

 

        Dim oTG As TransientGeometry

        oTG = _InvApp.TransientGeometry

 

        Dim oDef As PartComponentDefinition

        oDef = oPartDoc.ComponentDefinition

 

        ‘select the spline to project

        Dim oObject As Object

        oObject =

            _InvApp.CommandManager.Pick(

                SelectionFilterEnum.kSketch3DCurveFilter,

                               "Pick a sketch spline")

 

        Dim oSplineToPro As Object

        oSplineToPro = oObject

 

        ‘select the surface to project to

        oObject =

            _InvApp.CommandManager.Pick(

                SelectionFilterEnum.kPartFaceFilter,

                "Pick a surface")

 

        Dim oSurface As Face

        oSurface = oObject

 

        ‘projection type

        Dim oProType As String

        oProType = "project along vector"

        ‘oProType = "project to the closet point"

 

 

        ‘get spline param extents

        Dim oEV As CurveEvaluator

        oEV = oSplineToPro.Geometry.Evaluator

        Dim oMinP As Double

        Dim oMaxP As Double

        Call oEV.GetParamExtents(oMinP, oMaxP)

 

        ‘ number of points we want to project

        Dim oNumOfPt As Integer

        oNumOfPt = 50

 

        ‘the result points of projection

        Dim oPointCoords As ObjectCollection

        oPointCo
ords =

            _InvApp.TransientObjects.

            CreateObjectCollection()

 

        ‘ select a vector for type 1: "project along vector"

        Dim oVectorForProject As UnitVector

        oVectorForProject =

                oTG.CreateUnitVector()

        If oProType = "project along vector" Then

            Dim oObjectTemp As Object

            oObjectTemp =

           _InvApp.CommandManager.Pick(

               SelectionFilterEnum.kAllLinearEntities,

               "Pick a linear edge")

 

            If Not oObjectTemp Is Nothing Then

                Dim oLine As LineSegment

                oLine = oObjectTemp.Geometry

                oVectorForProject =

                    oLine.Direction

            Else

                MsgBox("select a linear edge as" &

                        "the projection vector!!")

                Return

            End If

        End If

 

        ‘ iterate each point and get the projection point

        Dim i As Integer

        For i = 0 To oNumOfPt

 

            Dim params(0) As Double

            params(0) = oMinP + i * (oMaxP – oMinP) / oNumOfPt

 

            Dim points(2) As Double

            Call oEV.GetPointAtParam(params, points)

 

            Dim oInvPt As Inventor.Point

            oInvPt = oTG.CreatePoint(points(0), points(1), points(2))

 

         

            ‘type 1

            If oProType = "project along vector" Then

                Dim oFoundEntities As ObjectsEnumerator =

              _InvApp.TransientObjects.CreateObjectCollection()

 

                Dim oLocatePtArray As ObjectsEnumerator =

                    _InvApp.TransientObjects.

                      CreateObjectCollection()

                ‘ project each point of the spline to

                ‘the surface along the normal vector

                oDef.FindUsingRay(oInvPt,

                                  oVectorForProject,

                                  0.000000000000001,

                                  oFoundEntities,

                                  oLocatePtArray,

                                  True)

                If oLocatePtArray.Count > 0 Then

                    oPointCoords.Add(

                        oTG.CreatePoint(oLocatePtArray(1).x,

                                       oLocatePtArray(1).Y,

                                       oLocatePtArray(1).Z))

                End If

 

                ‘type 2

            ElseIf oProType = "project to the closet point" Then

 

                ‘finds the closest point of each spline point on the surface.

                Dim oClosestPt As Inventor.Point

                oClosestPt =

                    oSurface.GetClosestPointTo(oInvPt)

                oPointCoords.Add(

                      oTG.CreatePoint(oClosestPt.X,

                                      oClosestPt.Y,

                                      oClosestPt.Z))

            Else

 

            End If

 

 

        Next

 

        ‘ create the 3d sketch spline with the result points

        Dim oNew3dSketch As Sketch3D

        oNew3dSketch = oDef.Sketches3D.Add

 

        oNew3dSketch.SketchSplines3D.Add(oPointCoords)

    End Sub

 

 image


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading