沿着B样条曲线放置单元


如下代码先取得指定ElementID(此时用的是820,需要根据您当前模型中B样条曲线的具体指修改这个数字)的B样条曲线,然后按1/10长度的比例取得线上的这些对应点坐标pts(i)和对应的u参数u(i)。根据u参数求得对应的切线方向tangent,进而求得旋转矩阵matrix,最后用pts(i)和matrix放置随曲线自动旋转的单元。

Option Explicit
Option Base 0
Sub PlaceCellAlongBsplineCurve()
    Dim oBCurveElem As BsplineCurveElement
    Dim oBCurve As BsplineCurve
    Set oBCurveElem = ActiveModelReference.GetElementByID(DLongFromLong(820))
    Set oBCurve = oBCurveElem.ExtractBsplineCurve
     
    '---- 1. Get 1/10 points and parameters(u) of BCurve ------------
    Dim pts(10) As Point3d
    Dim u(10) As Double
    Dim i As Integer
    Dim d As Double
    For i = 0 To 10
       d = oBCurveElem.Length * i / 10
       pts(i) = oBCurve.EvaluatePointAtDistance(u(i), d)
    Next
     
    '---- 2. Place cell at 1/10 points of BCurve ------------
    AttachCellLibrary "linepa.cel"
     
    Dim oCell As CellElement
    Dim tangent As Point3d
    Dim matrix As Matrix3d
    For i = 0 To 10
       oBCurve.EvaluatePointTangent tangent, u(i)
       matrix = Matrix3dFromRotationBetweenVectors(Point3dFromXYZ(0, 1, 0), tangent)
       Set oCell = CreateCellElement2("LT1", pts(i), Point3dFromXYZ(1, 1, 1), True, matrix)
       ActiveModelReference.AddElement oCell
    Next
End Sub