TheSwamp
Code Red => .NET => Topic started by: guohq on August 06, 2010, 09:45:25 AM
-
Public Function CurveLength(ByVal ID As ObjectId, ByRef Len As Double) As Boolean
CurveLength = True
Dim DB As Database = HostApplicationServices.WorkingDatabase
Using Trans As Transaction = DB.TransactionManager.StartTransaction
Dim Ent As Entity = Trans.GetObject(ID, OpenMode.ForRead)
Try
Dim Cur As Curve = Ent 'Error?
Len = Cur.GetDistanceAtParameter(Cur.EndParam)
Catch ex As Exception
CurveLength = False
End Try
End Using
End Function
Dim Cur As Curve = Ent 'If Ent is not a linear entity,the function will not go on. why the function not jump to "catch" ?
-
Hello!
I didn't test, but you can try:
Public Function CurveLength(ByVal ID As ObjectId, ByRef Len As Double) As Boolean
CurveLength = True
Dim DB As Database = HostApplicationServices.WorkingDatabase
Using Trans As Transaction = DB.TransactionManager.StartTransaction
Dim Ent As Entity = Trans.GetObject(ID, OpenMode.ForRead)
Try
Dim Cur As Curve =trycast( Ent,curve) 'Error?
Len = Cur.GetDistanceAtParameter(Cur.EndParam)
Catch ex As Exception
CurveLength = False
End Try
End Using
End Function
-
Try
Dim Cur As Curve =[b]trycast( Ent,curve)[/b] 'Error?
Len = Cur.GetDistanceAtParameter(Cur.EndParam)
Catch ex As Exception
CurveLength = False
End Try
Why use a try/catch at all? The type test operator in vb is Is, as far as I know.
If (TypeOf ent Is Curve) Then ...
But what do I know...
// F# ahead
open Autodesk.AutoCAD.DatabaseServices
open Autodesk.AutoCAD.EditorInput
open Autodesk.AutoCAD.Runtime
type acApp = Autodesk.AutoCAD.ApplicationServices.Application
[<CommandMethod "test">]
let test() =
let ed = acApp.DocumentManager.MdiActiveDocument.Editor
let per = ed.GetEntity "\nSelect entity: "
if per.Status = PromptStatus.OK then
let db = ed.Document.Database
use tr = db.TransactionManager.StartTransaction()
match tr.GetObject(per.ObjectId, OpenMode.ForRead) with
| :? Curve as cur ->
let len = cur.GetDistanceAtParameter cur.EndParam
"\nLength is " + string len |> ed.WriteMessage
| _ -> "\nNo curve selected " |> ed.WriteMessage
tr.Commit()
Regards
-
The CAD application will collapse when I run the function .
-
Public Function CurveLength(ByVal ID As ObjectId, ByRef Len As Double) As Boolean
CurveLength = True
Dim DB As Database = HostApplicationServices.WorkingDatabase
Using Trans As Transaction = DB.TransactionManager.StartTransaction
Dim Ent As Entity = Trans.GetObject(ID, OpenMode.ForRead)
Try
If TypeOf Ent Is Curve Then
Dim Cur As Curve = Ent
Len = Cur.GetDistanceAtParameter(Cur.EndParam)
Else
CurveLength = False
End If
Catch ex As Exception
CurveLength = False
End Try
End Using
End Function