create Random points with in the Shape using VBA.

Dear all,

          I am using Microstation v8 2004 Edition with VBA. I trying to create a random points within the shape i did create points in order wise(ex.add points every 1m interval within shape) but my client need random points with in the shape so any one give me the reference to do it.

Thanks & Regards,

Karthik M

  • Hi,

    please try this VBA code below as simple example to create random points within a shape.
    You will need to first select a shape before executing the code.
    If no parameter is used, 1000 points will be placed within the shape. You may also give the number of points as start parameter.

    I hope this helps?

    Artur

    Option Explicit
    Sub pointRandomShape()
    Dim counter As Long
    Dim i As Long
    Dim st As String
    Dim ele As Element
    Dim eE As ElementEnumerator
    Dim points() As Point3d
    Dim rng As Range3d
    Dim pRand As Point3d
    Dim oLine As LineElement
    
        st = KeyinArguments
        If Val(st) > 0 Then
            counter = Val(st)
        Else
            counter = 1000   ' arbitrary value if no parameter delivered
        End If
        Set eE = ActiveModelReference.GetSelectedElements
        If eE.MoveNext Then
            Set ele = eE.Current
            If ele.IsShapeElement Then
                points = ele.AsShapeElement.GetVertices
                rng = ele.AsShapeElement.Range
                i = 1
                Do While i <= counter
                    pRand.X = (rng.High.X - rng.Low.X) * Rnd + rng.Low.X
                    pRand.Y = (rng.High.Y - rng.Low.Y) * Rnd + rng.Low.Y
                    pRand.Z = 0
                    If Point3dInPolygonXY(pRand, points, -1) > 0 Then
                        Set oLine = CreateLineElement2(Nothing, pRand, pRand)
                        ActiveModelReference.AddElement oLine
                        i = i + 1
                    End If
                Loop
            End If
        End If
    End Sub
    

    With 3000 points I got this result:

      

  • Hi Artur,
    Thanks for your response, its working nice.

    Thanks & Regards,
    Karthik M