Length Of Object in a selection set

Length Of Object in a selection set

Anonymous
Not applicable
378 Views
3 Replies
Message 1 of 4

Length Of Object in a selection set

Anonymous
Not applicable
hey all, Working on a .net program that switches to the layout i want and activates the viewport to model space. I have it so the user selects a series of LWpolylines. I would like to get it to spit out the length of each line. I just need help getting started, i try doing a selection set but there isnt a length property.

Thanks in advance
Nick
0 Likes
379 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
/*
hey all, Working on a .net program that switches to the layout i want and
activates the viewport to model space. I have it so the user selects a
series of LWpolylines. I would like to get it to spit out the length of
each line. I just need help getting started, i try doing a selection set
but there isnt a length property.
*/

Did you find out from where Polyline is inherit? - it is from the Curve
class - then, have a look into the class members and in there you can use
GetDistanceAtParameter(); or GetDistAtPoint() methods.

HTH
0 Likes
Message 3 of 4

Anonymous
Not applicable
Hi,

First off state you're version (Ac2008/VS2005...)

first off get a ObjectId Array from the selectionset with:
.GetObjectIds()

Then loop these ObjectId's and see if there PolyLine object.
(use the GetObject methode from the transaction)

I myself use TypeOf to determine if it's a PolyLine object.
Set it to a Polyline instance, the Polyline Object has a Length property (also Area, etc)

Maybe You also can post the code you have so far.
I myself have a Palette that evals selectionsets, it shows total Length and Area of closed polylines and Circles, works perfect.

A Caddie.
(AutoCAD 2008, VS2005, Win XP Pro Sp2)
0 Likes
Message 4 of 4

Anonymous
Not applicable
Try this one that partially cutted from my project
'//
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.GraphicsInterface
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports AcadRT = Autodesk.AutoCAD.Runtime
Imports AcadED = Autodesk.AutoCAD.EditorInput
Imports AcadDB = Autodesk.AutoCAD.DatabaseServices
Imports AcadApp = Autodesk.AutoCAD.ApplicationServices.Application

Public Class Form1
'//
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
'//
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
Dim tot As Double = CalcLength()
Me.Label1.Text = "Total Length:"
Me.txtTotal.Text = String.Format("{0}", tot)
End Sub
'//
Private Shared Function CalcLength() As Double
Dim tot As Double = 0

Dim ed As Editor = AcadApp.DocumentManager.MdiActiveDocument.Editor

Dim dxf() As TypedValue = {New TypedValue(DxfCode.Start, "LWPOLYLINE")}
Dim filt As New SelectionFilter(dxf)

Dim opts As PromptSelectionOptions = New PromptSelectionOptions
opts.MessageForAdding = "Select polylines to calculate the total length:"
opts.AllowDuplicates = False
opts.SingleOnly = False

Dim res As PromptSelectionResult = ed.GetSelection(opts, filt)
If res.Status <> PromptStatus.OK Then
Return 0
End If

Dim db As Database = HostApplicationServices.WorkingDatabase
Using tr As Transaction = db.TransactionManager.StartTransaction()

Dim sset As SelectionSet = res.Value
Dim IDs() As ObjectId = sset.GetObjectIds
Dim n As Integer = 0
For Each Id As ObjectId In IDs
Dim ent As Entity = tr.GetObject(IDs(n), OpenMode.ForRead)
Dim pl As Polyline = tr.GetObject(ent.ObjectId, OpenMode.ForRead)
Dim ln As Double = pl.GetDistanceAtParameter(pl.EndParam)
tot = tot + ln
n += 1
Next

' MessageBox.Show(String.Format("Total length = {0}", tot)) ' for debug only

tr.Dispose()
End Using

Return tot

End Function

End Class
'//

~'J'~
0 Likes