vb.net Create Waterdrop in a surface

vb.net Create Waterdrop in a surface

jmmarin
Advocate Advocate
605 Views
10 Replies
Message 1 of 11

vb.net Create Waterdrop in a surface

jmmarin
Advocate
Advocate

Hi all. 

I am trying to create a waterdrop in a surface. I have found some examples in Internet, but it does not work. I think it is due changes in versions from 2013.

I have loaded the following two references (not sure if they are the correct or additionals are needed):

 

Autodesk.AECC.Interop.Land

Autodesk.AECC.Interop.UILand

 

The code I found is the following, but it does not work when the function "CreateWaterdrop(Point2d location, WaterdropObjectType objectType)" is called:

 

Dim drops As New ObjectIdCollection()
For Each triangle As TinSurfaceTriangle In surface.Triangles
  Dim centroid As Point2d = _
           getTriangleCentroid(triangle)
  ' calculate water-drop for the centroid
  Dim oid As ObjectIdCollection = _ 
    surface.Analysis.CreateWaterdrop(centroid, _
    Autodesk.Civil.Land.WaterdropObjectType.Polyline3D)
    ' Save all the water-drops
	
    For Each id As ObjectId In oid
      drops.Add(id)
    Next
Next

I understand the code, but my problem is the function...

 

Thanks in advance.

0 Likes
Accepted solutions (1)
606 Views
10 Replies
Replies (10)
Message 2 of 11

Jeff_M
Consultant
Consultant

@jmmarin You do not need to reference the COM libraries. The waterdrop type should be:

Autodesk.Civil.WaterdropObjectType.Polyline3D

 

That should allow it to work. With the Land in there you were mixing COM and .NET API types.

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 3 of 11

jmmarin
Advocate
Advocate

Hi @Jeff_M .

Thanks for your answer.

Ok. I have done what you suggest. The object is correct but, however, the collection is filled with objects without IDs... I do not know why. Maybe lack of knowlege (again...). 
The code is the following. I use two auxiliary functions to find the SurfaceID and also to obtain the point.

 

I also attach the model with the surface.

 

I hope this helps to guide me. Thanks.

'Get surface ID
    Public Shared Function obtenerSurfacePorNombre(SURFACE_NAME As String) As TinSurface


        Dim docCol As Autodesk.AutoCAD.ApplicationServices.DocumentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager
        Dim aDoc As Document = docCol.MdiActiveDocument
        Dim acadDb As Database = aDoc.Database
        Dim m_Editor As Editor = aDoc.Editor
        Dim m_doc As CivilDocument = CivilApplication.ActiveDocument()

        Dim surfaceSel As TinSurface
        Dim surfaceCol As ObjectIdCollection
        Dim surfaceId As ObjectId
        Dim surf As TinSurface
        Dim auxName As String

        surfaceCol = m_doc.GetSurfaceIds

        Using m_Trans As Transaction = acadDb.TransactionManager.StartTransaction()

            For i = 0 To surfaceCol.Count - 1
                surfaceSel = surfaceCol.Item(i).GetObject(OpenMode.ForRead)
                auxName = surfaceSel.Name
                If auxName = SURFACE_NAME Then
                    surfaceId = surfaceSel.Id
                    Exit For
                End If
            Next

            surf = m_Trans.GetObject(surfaceId, OpenMode.ForRead)

            m_Trans.Commit()

        End Using

        Return surf

    End Function


    Public Shared Sub crearWDP()

        Dim drops As New ObjectIdCollection()
        Dim surfName As String
        Dim surf As TinSurface

        'Get objectID of the surface
        surfName = "ELMYA_Z1"
        surf = PV_DRENAJE_main.obtenerSurfacePorNombre(surfName)

        For Each triangle As TinSurfaceTriangle In surf.Triangles
            Dim centroid As Point2d = getTriangleCentroid(triangle)

            ' calculate water-drop for the centroid
            Dim oid As ObjectIdCollection = surf.Analysis.CreateWaterdrop(centroid, Autodesk.Civil.WaterdropObjectType.Polyline3D)

            ' Save all the water-drops
            For Each id As ObjectId In oid
                drops.Add(id)
            Next
        Next

    End Sub

    Public Shared Function getTriangleCentroid(triangle As TinSurfaceTriangle) As Point2d
        ' The centroid is calculated from the cx and cy being:
        ' cx = (v1x + v2x + v3x) / 3
        ' cy = (v1y + v2y + v3y) / 3
        Dim cx As Double = (triangle.Vertex1.Location.X + triangle.Vertex2.Location.X + triangle.Vertex3.Location.X) / 3
        Dim cy As Double = (triangle.Vertex1.Location.Y + triangle.Vertex2.Location.Y + triangle.Vertex3.Location.Y) / 3
        Return New Point2d(cx, cy)
    End Function

to guide me...

 

 

 

 

0 Likes
Message 4 of 11

Jeff_M
Consultant
Consultant

Your code works just fine. It creates a waterdrop from each and every TinTriangle (7000+!). The ObjectIdCollection contains the ObjectId of each of those Polyline3d entities. It does make for a cool looking surface!

2023-03-03_7-20-39.png

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 5 of 11

jmmarin
Advocate
Advocate

Hi @Jeff_M ,

thanks for your answer.

I am surprised because it does not work in my project... The routine ends without drawing any polyline. I attach a capture of the ObjectIdCollection "drops". You can see that each element is empty and there are no objectIDs:

 

jmmarin_0-1677918914827.png

"drops" is being collecting objects but they do not have objectID. It should be the problem, but I do not know why. As you show, polylines are correctly created so "drops" should be filled with objectID... 

 

Thanks.

0 Likes
Message 6 of 11

Jeff_M
Consultant
Consultant

@jmmarin I just tested again, and it is working as expected for me. So let's check some things...what release of Civil 3D are you using? Are all Updates applied? I've tested in both C3D 2022 & 2023:

2023-03-04_9-46-45.png2023-03-04_9-46-21.png

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 7 of 11

jmmarin
Advocate
Advocate

Hi @Jeff_M

I am using C3D 2023:

 

jmmarin_0-1677953081896.png

It is the same that you show.

I have trying different options and I am not able to get a ObjectID collection as expected.

In order to discard issues, these are the imports that I have in the module. Do I need reference any additional library?

 

Imports System
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.DatabaseServices.ObjectId
Imports Autodesk.Civil.DatabaseServices.Styles
Imports Autodesk.Civil.ApplicationServices
Imports Autodesk.Civil.DatabaseServices
Imports Autodesk.AutoCAD.Colors
Imports AutoCAD
Imports System.Security.Cryptography.X509Certificates
Imports System.Windows.Forms
Imports Autodesk.AutoCAD.Windows
Imports Autodesk.AutoCAD.DatabaseServices.Filters
Imports System.Data.Common
Imports AXDBLib
Imports Autodesk.AutoCAD.ApplicationServices.Core
Imports Autodesk.Civil.Settings
Imports Autodesk.Aec.Geometry
Imports Profile = Autodesk.Civil.DatabaseServices.Profile

 

Thanks again.

0 Likes
Message 8 of 11

Jeff_M
Consultant
Consultant

These are all the Imports I have. 

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.Civil.ApplicationServices
Imports Autodesk.Civil.DatabaseServices

 

And this is how I am calling the Sub:

    <CommandMethod("jmSurfaceTest")>
    Public Sub surfacetest()
        crearWDP()
    End Sub
Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 9 of 11

jmmarin
Advocate
Advocate
Accepted solution

Hi @Jeff_M.

I have included a DocumentLock prior the loop and it has worked! "drops" are filled with 7000+ obtectIDs. 

I have remembered that you suggested me doing that for a previous routine.

Just to understand (and learn), why is it required?. 

Regards.

0 Likes
Message 10 of 11

Jeff_M
Consultant
Consultant

If you are calling the tool from a form you should always lock the document. It is always a good idea when asking for help to include the fact it is being run from a form.

Jeff_M, also a frequent Swamper
EESignature
0 Likes
Message 11 of 11

jmmarin
Advocate
Advocate

Ok Jeff. Understood.

I included the form later and I supposed it was not relevant.

Your help has been very useful. Thanks.

0 Likes