Hi,
I'm struggling with placing a cell along an arc in VBA.
I have a Complex Chain where I will place a cell along the whol chain with a fixed distance.
I have solved the rotation on the "Line" part of the chainm but having trouble with the arc.
The rotation is almost correct, but almost doesn't count :-)
Any one have a suggestion?
The code snippet for placing the cell:
Sub showLineElm(LinElm As ComplexStringElement) Dim elm As ElementEnumerator Dim aelm As ArcElement Dim lelm As LineElement Dim oPH As PropertyHandler Dim lastsegment As String Dim streng As String, streng1 As String Dim start As Point3d Dim skala As Point3d Dim oCell As CellElement Dim z As Integer Dim ant_cell As Double, distance As Double Dim vinkel As Double Dim mtrxRotation As Matrix3d skala.X = 1 skala.Y = 1 skala.z = 1 If LinElm.Type = msdElementTypeComplexString Then If LinElm.IsTraversableElement Then Set elm = LinElm.GetSubElements Do While elm.MoveNext
Select Case elm.Current.Type Case msdElementTypeLine Set lelm = elm.Current.AsLineElement lastsegment = "Segments[" & (lelm.VerticesCount - 2) & "]." Set oPH = CreatePropertyHandler(lelm) oPH.SelectByAccessString (lastsegment & "Start") start = oPH.GetValueAsPoint3d oPH.SelectByAccessString (lastsegment & "Direction") streng = oPH.GetValue vinkel = Degrees(CDbl(streng)) mtrxRotation = Matrix3dFromAxisAndRotationAngle(2, CDbl(streng)) Set oCell = CreateCellElement2("cellname", start, skala, True, mtrxRotation) ActiveModelReference.AddElement oCell ant_cell = lelm.Length / 0.854 ant_cell = ant_cell \ 1 distance = 0 For z = 1 To ant_cell distance = distance + 0.854 If distance > lelm.Length Then Exit For End If start = lelm.PointAtDistance(distance) Set oCell = CreateCellElement2("cellname", start, skala, True, mtrxRotation) ActiveModelReference.AddElement oCell Next z
Case msdElementTypeArc Set aelm = elm.Current.AsArcElement Set oPH = CreatePropertyHandler(aelm) oPH.SelectByAccessString ("SweepAngle") vinkel = oPH.GetValue mtrxRotation = Matrix3dFromAxisAndRotationAngle(2, vinkel) start = aelm.StartPoint Set oCell = CreateCellElement2("cellname", start, skala, True, mtrxRotation) distance = 0 oCell.Transform Transform3dFromMatrix3dAndFixedPoint3d(mtrxRotation, start) ActiveModelReference.AddElement oCell ant_cell = aelm.Length / 0.854 ant_cell = ant_cell \ 1 For z = 1 To ant_cell distance = distance + 0.854 If distance > aelm.Length Then Exit For End If start = aelm.PointAtDistance(distance) Set oCell = CreateCellElement2("cellname", start, skala, True, mtrxRotation) oCell.Transform Transform3dFromMatrix3dAndFixedPoint3d(mtrxRotation, start) ActiveModelReference.AddElement oCell Next z End Select Loop End If End IferrExit:End Sub 'showLineElm
Finn Mejding said:ant_cell = lelm.Length / 0.854
In addition to Jan's comments, avoid using magic numbers...
Finn Mejding said:distance = distance + 0.854
... especially when those magic numbers are used plurally.
Prefer to use a symbolic constant. It will help you in 2 years time when you come to maintain or improve your code!
Const ALPHA_FACTOR As Double = 0.854 ... ant_cell = aelm.Length / ALPHA_FACTOR ... distance = distance + ALPHA_FACTOR
Finn Mejding said:
Sub showLineElm(LinElm As ComplexStringElement)
Your subroutine name is at odds with your comment! Comments wear out: they become disconnected from their object. Prefer to make the subroutine name tell its own story...
Sub CreateCellAtOffset (ByVal oTraversableElement As ComplexStringElement)
Regards, Jon Summers LA Solutions