Creat a polyline from database

Creat a polyline from database

nucliabrasil
Advocate Advocate
1,729 Views
4 Replies
Message 1 of 5

Creat a polyline from database

nucliabrasil
Advocate
Advocate

Hello !
I have an access table and I have the east and north coordinates. I want to search the table and generate a polyline in autocad. How do I do ?
thankful

0 Likes
Accepted solutions (1)
1,730 Views
4 Replies
Replies (4)
Message 2 of 5

norman.yuan
Mentor
Mentor

The point/coordinate data being in database, or anywhere outside of AutoCAD should have no impact on how AutoCAD create a polyline (assume it is AcadLWPolyline usually). If you need to write code to do it, just remember to separate the code that somehow obtain the points/coordinates from somewhere, and the code that actually create the polyline (or any AcadEntity, for that matter).

 

Say, you have a separate code module that handle database access, which has a public function to return a double array, representing coordinates of a polyline, like this:

 

Public Function GetPolylineCoordinates(dbConnectionString As String) As Variant

    Dim coords() As Double

    '' use ADO (or DAO) to connect the Access DB and retrieve the data and fill out the variable "coords"

    GetPolylineCoordinates = coords

End Function

 

Then in another module, you can have a macro like this:

 

Public Sub DrawPolylineFromData()

    Dim dbConnection As String

    dbConnection = "???????????"  '' CnnectionString

    Dim coords As Variant

    coords=GetPolylineCoordinates(dbConnection)

    Dim pl As AcadLWPolyline 

    Set pl = ThisDrawing.AddLIghtWeightPolyline(coords)

    '' set polyline's other properties, if necessary, such as Layer, Color..

   pl.Update

End Sub

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 5

nucliabrasil
Advocate
Advocate

Thanks norman,

but help me understand why it does not create the polyline in this code. What is missing?

 

Dim pl As AcadLWPolyline
Dim coords(0 To 1) As Double
Dim i As Integer


Conectar '<<< conection database
Set rsPP.ActiveConnection = dbcon
rsPP.Open "select * from pp", dbcon, adOpenStatic, adLockOptimistic

npts = rsPP.RecordCount

 

For i = 1 To npts

 

rsPP.Filter = "pt= '" & i & "'"

coords(0) = rsPP.Fields("cx")
coords(1) = rsPP.Fields("cy")

 

Set pl = ThisDrawing.ModelSpace.AddLightWeightPolyline(coords)

 

If i = npts + 1 Then Exit For 

 

Next i

 

0 Likes
Message 4 of 5

norman.yuan
Mentor
Mentor
Accepted solution

I assume you do know that to create a polyline, you need AT LEAST 2 points. That is, the coordinate array that is passed to AcadModelSpace.AddLightWeightPolyline() method MUST HAVE at least 4 elements.

 

In your code, each loop in the For...Next, you obtain 1 point (one x value, one y value), and then you try to create a LWPolyline with this ONLY point. I did not see you have "On Error Resume Next", and wondering why the AddLightWeightPolyline(coords) did not raise exception, as it should.

 

BTW, this line

 

If i = npts + 1 Then Exit For

 

is completely unnecessary: the "For i to npts"  already guarantees that the i will not go past npts!

 

I suspect that the x/y values in the recordset is meant to a single polyline with many vertices, and you use RecordSet filter to get points in order (you may sort the recordset in SQL statement rather than filter the recordset in each For... loop for better performance). If this is the case, code would be something like:

 

Dim coords() as double

Dim i as Integer

Dim j As Integer

 

'' Open and get RecordSet

 

For i = 1 to npts

  ''Get 1 point

  rsPP.Filter = "pt='" & i & "'"

  ''Add the point's X value and add it to the coordinate array

  Redim Preserve coords(j)

  coords(j)=rsPP!cx

  j = j + 1

  ''Add the point's Y value and add it to the coordinate array

  Redim Preserve coords(j)

  coords(j)=rsPP!cy

  j = j + 1

Next

'' Now you create a polyline with Vertex/point count = npts

'' with coordinate array with element count = 2 x npts

Set pl = ThisDrawing.AddLightWeightPolyline(coords)

 

HTH

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 5

nucliabrasil
Advocate
Advocate

Perfect norman.

It's worked with success !

Thanks !

0 Likes