Jump to content

VBA code for length of selected line


priyanka_mehta
 Share

Recommended Posts

Hi all,

 

I need a VBA code for finding the length of a selected line/polyline..

 

i have the code for selectonscreen

and for length again object.length can be used

 

but somehow i m not being able to integrated these two to get the

length of my selected line.

 

Please Help!

 

Regards,

Priyanka

Link to comment
Share on other sites

This example shows an alternative method for isolating lines and polylines. The method employs a “Filter” to a select on screen call. The example also shows how to cast each entity to it’s proper type before querying the .Length property.

 

Note: some object properties are available while still cast only as a general AcadEntity. Some, however, are not. Casting the entity as the more specific type allows full access. And, as an additional note, Autodesk has built in a bit of confusion with the various names given to polylines in the different context.

 

 

Sub GetLengths()
Dim SOS As AcadSelectionSet
Dim objSS As AcadSelectionSet
Dim intCode(0) As Integer
Dim varData(0) As Variant
Dim objEnt As AcadEntity
Dim entLine As AcadLine
Dim entPoly As AcadPolyline
Dim entLWPoly As AcadLWPolyline
  For Each SOS In ThisDrawing.SelectionSets
     If SOS.Name = "MySS" Then
        ThisDrawing.SelectionSets("MySS").Delete
     Exit For
     End If
  Next
  intCode(0) = 0: varData(0) = "LINE,POLYLINE,LWPOLYLINE"
  ThisDrawing.SelectionSets.Add ("MySS")
  Set objSS = ThisDrawing.SelectionSets("MySS")
  objSS.SelectOnScreen intCode, varData
  
  
  If objSS.Count < 1 Then
     MsgBox "No lines and polylines selected!"
  Exit Sub
  End If
  
  For Each objEnt In objSS
     Select Case objEnt.ObjectName
     Case "AcDbLine"
        Set entLine = objEnt
        MsgBox "Line is " & entLine.Length & " units long."
     Case "AcDb2dPolyline"
        Set entPoly = objEnt
        MsgBox "Polyline is " & entPoly.Length & " units long."
     Case "AcDbPolyline"
        Set entLWPoly = objEnt
        MsgBox "LightWeight Polyline is " & entLWPoly.Length & " units long."
     End Select
  Next

End Sub

Link to comment
Share on other sites

Hey Thanks,

 

This was exactly what I wanted..

 

I was using the following code:

 

'*******************
Dim line As AcadLine
Dim block As AcadBlockReference
Dim a As String


On Error Resume Next

ThisDrawing.SelectionSets("TempSSet").Delete
Set objSelectionSet = ThisDrawing.SelectionSets.Add("TempSSet")
'ask user to pick entities on the screen
objSelectionSet.SelectOnScreen
'change the highlight status of each entity selected

For Each line In objSelectionSet

MsgBox line.length

Next

objSelectionSet.Delete

'***********

 

But i really required such filters.Thanks a lot

 

Regards,

Priyanka

Edited by SLW210
Code Tags!!
Link to comment
Share on other sites

  • 3 years later...

hi every body

id appreciate it if some body guide me

how i can have the length of polylines from start to the point of intersection with another polyline??

 

thank you all

Link to comment
Share on other sites

Welcome to the Forum, fati sohrabi!

Please tell me, are you a programmer? I mean, are you looking for an approach to calculate that value using a VBA code you have in progress? Or you just want a solution to list the said length on prompter?

Link to comment
Share on other sites

wow wonderful lee Mac,

 

i have two more questions,

1. how i can transfer this information(length of poly lines) to excel software?

is it possible?

2. if it is possible, how i can find (distinguish) that which length is belong to which poly lines?

 

i'd really appreciate it.

Edited by fati sohrabi
Link to comment
Share on other sites

i am a designer of pressurized irrigation system, in our project its a lots of pipe line

with two diameter that have intersect with together, so for calculate the pipe line in (27000ha):

1. i have to recognize the intersection point

2. and then calculate the length of line

the lisp that lee mac introduce me is good but not enough for me because:

first: how i can transfer this information to excel

second: lisp is not so trustworthy look like vba code

o:)

 

thank u if u can help me more

Link to comment
Share on other sites

  • 11 months later...

hi there i am new programmer here for vba in autocad

 

hence wanted some help for issues below:

 

code to find the length of a given arc

 

and code to find the area of part of circle divided by chord

 

thanx in advance :):):)

 

Regards

Sunny Dedhia

Link to comment
Share on other sites

  • 2 weeks later...

look up the .IntersectWith method

 

it will return a point array with the intersection points for any two entities which you can use in your calculation code

 

length of an arc is simple PI math which you should be able to google - it's basic geometry

Link to comment
Share on other sites

dArcLength = n(degrees)/360(degrees) * 2 * PI * radius of arc

 

autocad stores angular values as radians tho, so you have to make sure your convert your radians to degrees or vice versa first - again, it's just basic math

Link to comment
Share on other sites

this will help you visualize it

 

http://math.about.com/od/formulas/ss/surfaceareavol_9.htm

 

so create a function that calculates the length of the arc when you need it

 

Function dArcLength(byval radius as double, byval nDegrees as integer) as double

 

' put the calculations here

 

'return value

dArcLength = whatever you calculated

 

)

Link to comment
Share on other sites

  • 11 months later...

hi all

 

its been a long time this thread strated but i would like to try my luck.

 

whenever i try "SEANT's" code i always counter with the errror

 

runtime error 424 on the line "For Each SOS In ThisDrawing.SelectionSets"

 

i am using excel 2010 and as you can see autocad 2007.

 

does anyone know the reason.

 

thanks in advance.

Link to comment
Share on other sites

Where are you running the code, Excel or AutoCAD's VBAIDE? The "ThisDrawing" object requires it be run in AutoCAD.

Edited by SEANT
Added Info
Link to comment
Share on other sites

Thanks god you heard me Seant :)

 

i am trying to run this code on Excel.

 

Because i am trying to measure the lengths of dwg's inside many different folders.

 

First take their names with Excel vba than send them to Autocad

 

and finally get their lenghts inside the Excel file.

Link to comment
Share on other sites

All of that sounds possible, and can be run from either environment - provided the proper references are set. That is not to say, though, that there is not complexity. The VBAIDEs of both AutoCAD and Excel offer shortcuts whilst dealing with their respective object models. A development effort combining the two environments will require more specific referencing.

 

 

Ultimately, ObjectDBX will be beneficial. It is an interface for manipulating AutoCAD drawings without having to open each one in the drawing editor. Web search discussion linking ObjectDBX and Excel - I'm sure there is plenty to explore.

Link to comment
Share on other sites

  • 7 months later...
This example shows an alternative method for isolating lines and polylines. The method employs a “Filter” to a select on screen call. The example also shows how to cast each entity to it’s proper type before querying the .Length property.

 

Note: some object properties are available while still cast only as a general AcadEntity. Some, however, are not. Casting the entity as the more specific type allows full access. And, as an additional note, Autodesk has built in a bit of confusion with the various names given to polylines in the different context.

 

 

Sub GetLengths()
Dim SOS As AcadSelectionSet
Dim objSS As AcadSelectionSet
Dim intCode(0) As Integer
Dim varData(0) As Variant
Dim objEnt As AcadEntity
Dim entLine As AcadLine
Dim entPoly As AcadPolyline
Dim entLWPoly As AcadLWPolyline
  For Each SOS In ThisDrawing.SelectionSets
     If SOS.Name = "MySS" Then
        ThisDrawing.SelectionSets("MySS").Delete
     Exit For
     End If
  Next
  intCode(0) = 0: varData(0) = "LINE,POLYLINE,LWPOLYLINE"
  ThisDrawing.SelectionSets.Add ("MySS")
  Set objSS = ThisDrawing.SelectionSets("MySS")
  objSS.SelectOnScreen intCode, varData
  
  
  If objSS.Count < 1 Then
     MsgBox "No lines and polylines selected!"
  Exit Sub
  End If
  
  For Each objEnt In objSS
     Select Case objEnt.ObjectName
     Case "AcDbLine"
        Set entLine = objEnt
        MsgBox "Line is " & entLine.Length & " units long."
     Case "AcDb2dPolyline"
        Set entPoly = objEnt
        MsgBox "Polyline is " & entPoly.Length & " units long."
     Case "AcDbPolyline"
        Set entLWPoly = objEnt
        MsgBox "LightWeight Polyline is " & entLWPoly.Length & " units long."
     End Select
  Next

End Sub

 

can i get the same code in vb.net...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...